Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

three.js - ThreeJS - GLTF No Shadow Casting

I am having issues with my meshes not casting shadows on other meshes in the scene. I have looked around the Stack Overflow and three.js forums, implemented all suggestions and nothing happened/improved.

Here is the code snippet:

camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 1000 );
camera.position.set( - 20, 20, 20);

scene = new THREE.Scene();


const loader = new GLTFLoader();
loader.load( 'here_is_path_to_brown_model', function ( gltf ) {     
    gltf.scene.traverse( function ( node ) {
        if ( node.isMesh ) {
            node.castShadow = true;
            node.receiveShadow = true;
        }
    } );
    scene.add( gltf.scene );
    render();
} );

const cubeloader = new GLTFLoader();
cubeloader.load( 'here_is_path_to_white_model', function ( gltf ) {
    gltf.scene.traverse( function ( node) {
        if ( node.isMesh ) {
            node.castShadow = true;
            node.receiveShadow = true;
        }
    } );
    scene.add( gltf.scene );
    render();
} );


const geometry = new THREE.PlaneGeometry( 5, 20, 512,512);
const groundMaterial = new THREE.MeshPhongMaterial( { color: 0xFFFFFF ,side: THREE.DoubleSide} );
//const material = new THREE.MeshBasicMaterial( {color: 0xffff00, side: THREE.DoubleSide} );
const plane = new THREE.Mesh( geometry, groundMaterial );
plane.rotation.x = -Math.PI / 2;
plane.receiveShadow = true;
scene.add( plane ); 

const spotLight = new THREE.SpotLight( 0xffffff );
//var spotLight = new THREE.DirectionalLight(0xffffff,1);
spotLight.position.set( 10, 5, 10 );
spotLight.castShadow = true;
spotLight.shadow.mapSize.width = 1024;
spotLight.shadow.mapSize.height = 1024;
spotLight.shadow.camera.near = 50;
spotLight.shadow.camera.far = 400;
spotLight.shadow.camera.fov = 10;
spotLight.shadow.camera.left = -500;
spotLight.shadow.camera.bottom = -500;
spotLight.shadow.camera.right = 500;
spotLight.shadow.camera.top = 500;
scene.add( spotLight );

renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.2;
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.gammaOutput = true;
renderer.gammaFactor = 1;

And the picture of the outcome: enter image description here

As you can see, even the ThreeJS mesh with Phong does not receive any shadow.

I am stuck here. Any hints?

question from:https://stackoverflow.com/questions/65907675/threejs-gltf-no-shadow-casting

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...