Only AmbientLight works, the others don't

I’m a Three.js novice.Today I have some trouble.My model only AmbientLight works, the others don’t.I tried to search for information, but it didn’t work out.Please help me!

Case: AmbientLight

Case: SpotLight

Code:

request.addEventListener(‘load’, function (event) {

var contents = event.target.response;

var geometry = new Array();
geometry = new THREE.XMLLoader().parse(contents);

// light
// var scene = editor.scene;
// var AmbientLight = new THREE.AmbientLight( 0xffffff, 1);
//
// AmbientLight.name = 'AmbientLight ';
//
// scene.add(AmbientLight);

var spotLight = new THREE.SpotLight( 0xffffff, 1 );

spotLight.position.set( 15, 100, 35 );
spotLight.angle = Math.PI / 6;
spotLight.penumbra = 0.05;
spotLight.decay = 2;
spotLight.distance = 0;

spotLight.castShadow = true;
spotLight.shadow.mapSize.width = 1024;
spotLight.shadow.mapSize.height = 1024;
spotLight.shadow.camera.near = 10;
spotLight.shadow.camera.far = 200;

editor.execute(new AddObjectCommand(spotLight));

var material = new Array(), mesh = new Array(), texture = new Array(), color = new Array(),MaterialImagePath;

for (let i = 0; i < geometry.length; i++) {

    // Texture
    if (geometry[i].Materials.DiffuseMap) {
        MaterialImagePath = textureDir + "/" + geometry[i].Materials.DiffuseMap;

        material[i] = new THREE.MeshLambertMaterial();

            texture = new THREE.TextureLoader().load(
            MaterialImagePath,
            function (texture) {
                material[i].map = texture;
            },
        );

        // TextureColor
        color = new THREE.Color("rgb(" + geometry[i].Materials.DiffuseColor + ")");

        if (geometry[i].Materials.Transparency === "0") {
            material[i].transparent = false;
            material[i].map = texture;
            material[i].color = color;
        } else {
            material[i].transparent = true;
            material[i].opacity = geometry[i].Materials.Transparency;
            material[i].map = texture;
            material[i].color = color;
        }

    } else {
        // TextureColor
        color = new THREE.Color("rgb(" + geometry[i].Materials.Color + ")");

        if (geometry[i].Materials.Transparency === "0") {
            material[i] = new THREE.MeshLambertMaterial({
                transparent: false,
                color: color,
            });
        } else {
            material[i] = new THREE.MeshLambertMaterial({
                transparent: true,
                opacity: geometry[i].Materials.Transparency,
                color: color,
            });
        }
    }

    geometry[i].sourceType = "xml";
    geometry[i].sourceFile = filename;


    mesh[i] = new THREE.Mesh(geometry[i], material[i]);
    mesh[i].name = geometry[i].name;
    mesh[i].scale.set(0.001, 0.001, 0.001);
    mesh[i].rotation.x = -(90 * Math.PI) / 180;

    editor.execute(new AddObjectCommand(mesh[i]));
}

}, false);

Can you please ensure that your model has valid normal vectors? These data are necessary for lighting calculation.

3 Likes

Oh, man, you’re so handsome! Thank you for solving my problem!

1 Like