Hello, I am working on a code in webgl in the p5js web editor that calls in a random number of spinning torus, spinning at different speeds and angles, which are all defined. I have set the texture to pull from an array of image assets, but as of now when the code loads all the torus have the same image as texture. I would like each torus to display a different image from an array upon reload, without changing the random functions of the rest of the code (numtorus, width, radius, angle, etc). I have tried to define an array of images but have run into problems with defining texture as random even if the images are set in an array.
let bg
let img
let numTorus
let angle = []
let radius = []
let torusWidth = []
function setup() {
bg = loadImage('');
createCanvas(600, 600, WEBGL)
setAttributes("antialias", true)
textureWrap(REPEAT)
randomSeed(42)
}
function preload()
{
bg = loadImage('lava2.jpg')
img = random([
loadImage("lava2.jpg"),
loadImage("cable tangle.jpg"),
loadImage("ocean trash.jpg"),
loadImage("pangea1.png"),
loadImage("openpits small.jpg"),
loadImage("undersea cable.jpg"),
loadImage("content aware yareta.jpg")
])
// 1-6, 6-15, 15-30: 5, 90, 5
numTorus = Math.floor(random(1, 30))
console.log('numTorus:', numTorus)
for (let i = 0; i < numTorus; ++i) {
// 0.5-2, 0.5-6
torusWidth.push(random(0.5, 50))
radius.push(random(1, 225))
angle.push(random(0.00009, 0.005))
console.log('angle:',i, angle[i])
console.log('radius:',i, radius[i])
console.log('torusWidth:', i, torusWidth[i])
}
}
function draw() {
noStroke()
// texture(bg)
// translate(0,0,-400)
// plane(1200, 1200)
// translate(0,0,400)
texture(img)
background('black')
directionalLight(250, 250, 250, 0, 0, -1)
for (let i = 0; i < numTorus; ++i) {
rotateX(frameCount * angle[i])
rotateY(frameCount * angle[i])
rotateZ(frameCount * angle[i])
torus(radius[i], torusWidth[i], 50, 50)
}
}
Im a beginner with p5js, any help appreciated. Thank you so much!