gpt4 book ai didi

javascript - 试图理解 THREEJS 中使用的回调函数

转载 作者:行者123 更新时间:2023-11-30 17:33:09 25 4
gpt4 key购买 nike

以下代码来自 THREE.JS 示例;效果很好。我用 three.js 代码逆向工程制作了自己的艺术画廊。我用来挂画的代码源自 three.js 示例:webgl_materials_texture_manualmipmap.html

下面的代码有效,但我不明白如何在回调函数中使用 materialPainting1,直到回调函数之后才完全定义它。

谁能向我解释一下这是如何工作的。 materialpainting1 如何在回调函数中使用,即使它是在之后定义的,因为它在示例中运行良好,但在我自己的代码中,我必须使用 fiddle 来加载绘画。

var callbackPainting = function () {
var image = texturePainting1.image;
texturePainting2.image = image;
texturePainting2.needsUpdate = true;
scene1.add(meshCanvas1);
scene2.add(meshCanvas2);
var geometry = new THREE.PlaneGeometry(100, 100);
var mesh1 = new THREE.Mesh(geometry, materialPainting1);
var mesh2 = new THREE.Mesh(geometry, materialPainting2);
addPainting(scene1, mesh1);
addPainting(scene2, mesh2);

function addPainting(zscene, zmesh) {
zmesh.scale.x = image.width / 100;
zmesh.scale.y = image.height / 100;
zscene.add(zmesh);
var meshFrame = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({
color: 0x000000,
polygonOffset: true,
polygonOffsetFactor: 1,
polygonOffsetUnits: 5
}));
meshFrame.scale.x = 1.1 * image.width / 100;
meshFrame.scale.y = 1.1 * image.height / 100;
zscene.add(meshFrame);
var meshShadow = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({
color: 0x000000,
opacity: 0.75,
transparent: true
}));
meshShadow.position.y = -1.1 * image.height / 2;
meshShadow.position.z = -1.1 * image.height / 2;
meshShadow.rotation.x = -Math.PI / 2;
meshShadow.scale.x = 1.1 * image.width / 100;
meshShadow.scale.y = 1.1 * image.height / 100;
zscene.add(meshShadow);
var floorHeight = -1.117 * image.height / 2;
meshCanvas1.position.y = meshCanvas2.position.y = floorHeight;
}
};
var texturePainting1 = THREE.ImageUtils.loadTexture("textures/758px-Canestra_di_frutta_(Caravaggio).jpg", THREE.UVMapping, callbackPainting),
texturePainting2 = new THREE.Texture(),
materialPainting1 = new THREE.MeshBasicMaterial({
color: 0xffffff,
map: texturePainting1
}),
materialPainting2 = new THREE.MeshBasicMaterial({
color: 0xffccaa,
map: texturePainting2
});

最佳答案

纹理加载是异步的;回调函数直到纹理加载后才被执行。到那时,您的 Material 已经定义好了。

不过,作为一种良好做法,我会先定义 Material 。

three.js r.66

关于javascript - 试图理解 THREEJS 中使用的回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22576514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com