【发布时间】:2021-09-30 05:43:24
【问题描述】:
我正在尝试动态加载一组图像并以恒定速度沿 Z 方向平移它们,同时为 X 和 Y 提供随机值
我正在使用以下代码,但我不知道如何使用图像 texture 属性。
在渲染以下我使用的代码时,我得到一个空白的黑色画布。任何帮助将不胜感激。
let imgs = [];
let imgs_arr = [];
let num = 15;
function preload() {
for (let i = 0; i < num; i++) {
imgs[i] = loadImage("https://picsum.photos/400/400/?image=" + i * 10);
}
}
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
colorMode(HSB, 360, 100, 100, 100);
for (let i = 0; i < num; i++) {
let x = random(-width / 2, width / 2);
let y = random(-height / 2, height / 2);
let z = random(-width*5, width/2);
let texture = new Type(imgs[i], x, y, z)
imgs_arr.push(texture);
}
}
function draw() {
background(0,0,0);
orbitControl();
for (let i = 0; i < num; i++) {
imgs_arr[i].update();
imgs_arr[i].display();
}
}
class Type {
constructor(_img, _x, _y, _z) {
this.img = _img;
this.x = _x;
this.y = _y;
this.z = _z;
}
update() {
this.z += 10;
if(this.z > width/2){
this.z = -width*5;
}
}
display() {
push();
translate(this.x, this.y, this.z);
texture(this.img)
pop();
}
}
【问题讨论】:
标签: javascript processing webgl p5.js