【问题标题】:Weird behaviour of shadows in THREE.jsTHREE.js 中阴影的奇怪行为
【发布时间】:2011-12-17 04:04:43
【问题描述】:

我正在尝试渲染一组位于 3 维空间中的照片,它们相互投射阴影。我从两个矩形开始,这是我的代码

function loadScene() {
    var WIDTH = 1200,
        HEIGHT = 500,
        VIEW_ANGLE = 45,
        ASPECT = WIDTH / HEIGHT,
        NEAR = 0.1,
        FAR = 10000,

        world = document.getElementById('world'),
        renderer = new THREE.WebGLRenderer(),
        camera = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT, NEAR, FAR),
        scene = new THREE.Scene(),
        texture = THREE.ImageUtils.loadTexture('image.jpg', {}, function() {
            renderer.render(scene, camera);
        }),
        material = new THREE.MeshLambertMaterial({map: texture}),
        solidMaterial = new THREE.MeshPhongMaterial({color: 0xCC0000}),
        rectangle = new THREE.PlaneGeometry(200, 120),
        mesh = new THREE.Mesh(rectangle, material),
        mesh1 = new THREE.Mesh(rectangle, material),
        spotLight = new THREE.SpotLight(0xFFFFFF, 1.3);

    camera.position.set(0, 0, 200);
    mesh.translateX(140);
    mesh.translateZ(-70);
    mesh.receiveShadow = true;
    mesh1.castShadow = true;
    spotLight.position.x = -100;
    spotLight.position.y = 230;
    spotLight.position.z = 230;
    spotLight.castShadow = true;

    scene.add(spotLight);
    scene.add(mesh);
    scene.add(mesh1);
    renderer.setSize(WIDTH, HEIGHT);
    renderer.shadowMapEnabled = true;
    renderer.render(scene, camera);
    world.appendChild(renderer.domElement);
}

这里solidMaterial 是纯红色,material 是纹理材质。我得到的是以下内容:

  • 如果我对两个网格都使用material,矩形会按预期显示,但没有阴影。如果我对两者都使用 solidMaterial,则相同。
  • 如果我将material 用于mesh(更远的那个),将solidMaterial 用于mesh1,我会看到一个红色矩形,它在带纹理的矩形上投射阴影。这是唯一符合我预期的情况。
  • 如果我将solidMaterial 用于mesh(更远的那个)和material 用于mesh1,我会看到一个带有阴影的红色矩形,但前面的纹理矩形根本没有绘制.

阴影的正确用法是什么?

【问题讨论】:

    标签: javascript 3d webgl three.js


    【解决方案1】:

    事实证明,当两个矩形具有相同的材质时,阴影不会出现。我想知道这是否是 THREE.js 中的一个错误。

    另一方面,如果我使用两种不同的材质,阴影会按预期显示,即使它们具有相同的纹理。

    【讨论】:

    • 我不知道 Three.js 的内部结构,但我会大胆猜测它会在一次调用中绘制所有共享材质的几何体。不幸的是,这使得阴影变得非常困难,但这可能是一个合理的权衡,可以在其他地方获得更好的性能。此外,由于您为您的案例找到了如此简单的解决方法,我怀疑是否有必要“纠正”这种行为。
    猜你喜欢
    • 1970-01-01
    • 2016-04-19
    • 2021-02-19
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多