Three.JS Shadow to object

杀马特。学长 韩版系。学妹 提交于 2020-01-24 15:11:22

问题


I want to add castShadow and receiveShadow on a object. But what do I wrong with the following code?...

var mtlLoader = new THREE.MTLLoader();
mtlLoader.setPath("objects/Tree/");
mtlLoader.load("tree.mtl", function(materials) {
    materials.preload();
    var objLoader = new THREE.OBJLoader();
    objLoader.setMaterials(materials);
    objLoader.setPath("objects/Tree/");
    objLoader.load("tree.obj", function(gate) {

        var positionX = 0;
        var positionY = 4;
        var positionZ = 0;

        gate.position.x = positionX;
        gate.position.y = positionY;
        gate.position.z = positionZ;
        gate.scale.x = 2
        gate.scale.y = 2;
        gate.scale.z = 2;
        scene.add(gate);

        gate.castShadow = true;
        gate.receiveShadow = true;
    });
});

回答1:


I think you need to traverse through the child and then set castShadow as given below.

gate.traverse( function ( child ) {
    if ( child instanceof THREE.Mesh ) {
      child.castShadow = true;
    }
});

Here is the working sample http://jsfiddle.net/4Txgp/703/

I think really it should be helpful



来源:https://stackoverflow.com/questions/42399613/three-js-shadow-to-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!