2

我正在尝试使用 OBJLoader 对象在我的场景中添加一盏灯。该对象是从 Blender 中导出的。如果我将其children[i].castShadow属性设置true为以下错误,则会出现但它仍然出现并且它的阴影可以在墙上看到:

错误信息

我究竟做错了什么?如果当我向场景中添加其他对象并将其castShadow属性设置为时,true我不会收到任何错误消息。这是我目前唯一有问题的对象。

还有一件事。我的项目无法在 Firefox 中运行,但它会在 Edge 或 Chrome 中显示。Firefox 有什么特别之处?

<!DOCTYPE html>

<html>

<head>
    <title>Jaj de jó</title>
    <script type="text/javascript" src="libs/three.js"></script>
    <script type="text/javascript" src="libs/OBJLoader.js"></script>
    <script type="text/javascript" src="libs/MTLLoader.js"></script>
    <script type="text/javascript" src="libs/OBJMTLLoader.js"></script>
    <script type="text/javascript" src="libs/OrbitControls.js"></script>
    <script type="text/javascript" src="libs/ThreeBSP.js"></script>
    <script type="text/javascript" src="libs/stats.js"></script>
    <script type="text/javascript" src="libs/dat.gui.js"></script>
    <style>
        body {
            margin: 0;
            overflow: hidden;
        }
    </style>
    <meta charset="UTF-8">
</head>
<body>

<div id="WebGL-output"></div>

<script type="text/javascript">
    var webGLRenderer;

    function init() {
        var scene = new THREE.Scene();

        var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);

        webGLRenderer = new THREE.WebGLRenderer();
        webGLRenderer.setClearColor(new THREE.Color(0x666666, 1.0));
        webGLRenderer.setSize(window.innerWidth, window.innerHeight);
        webGLRenderer.shadowMapEnabled = true;
        webGLRenderer.shadowMapType = THREE.PCFShadowMap;

        camera.position.x = -30;
        camera.position.y = 40;
        camera.position.z = 50;
        camera.lookAt(new THREE.Vector3(0, 0, 0));

        var orbitControls = new THREE.OrbitControls(camera);
        orbitControls.autoRotate = false;
        var clock = new THREE.Clock();

        document.getElementById("WebGL-output").appendChild(webGLRenderer.domElement);

        /* prepare, load textures and add geometries to the scene */
        var planeGeometry = new THREE.PlaneBufferGeometry(200, 200, 1, 1);
        var texture = THREE.ImageUtils.loadTexture("textures/floor_wood.jpg");
        var planeMaterial = new THREE.MeshPhongMaterial();
        planeMaterial.map = texture;

        texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
        texture.repeat.x = 200 / 100;
        texture.repeat.y = 200 / 100;

        var plane = new THREE.Mesh(planeGeometry, planeMaterial);
        plane.receiveShadow = true;

        plane.rotation.x = -0.5 * Math.PI;

        plane.position.x = 0;
        plane.position.y = 0;
        plane.position.z = -20;

        scene.add(plane);
        /* FLOOR ADDED */

        /* WALLS START */
        var wallGeometry = new THREE.BoxGeometry(150, 100, 175);
        texture = THREE.ImageUtils.loadTexture("textures/brick-wall.jpg");

        var material = new THREE.MeshPhongMaterial();
        material.map = texture;
        var wall = new THREE.Mesh(wallGeometry, material);

        wall.receiveShadow = true;

        wall.material.side = THREE.BackSide;

        wall.material.map.repeat.set(200 / 100, 175 / 100);

        wall.material.map.wrapS = THREE.RepeatWrapping;
        wall.material.map.wrapT = THREE.RepeatWrapping;

        wall.material.needsUpdate = true;

        wall.position.y = 25;
        wall.position.z -= 25;

        scene.add(wall);
        /* WALLS END */

        /* LAMP */
        var loader = new THREE.OBJLoader();
        loader.load("models/lamp.obj", function (object) {
            console.log("LAMP");
            console.log(object);

            var blackMaterial = new THREE.MeshPhongMaterial({ color: 0x222222 });

            for(var i = 0; i < object.children.length; i++) {
                object.children[i].material = blackMaterial;
                object.children[i].castShadow = true;
            }

            object.scale.set(5, 5, 5);

            object.position.x = -14.5;
            object.position.y = 40;
            object.position.z = -17;


            scene.add(object);
        });
        /* LAMP END */

        // add spotlight for the shadows
        var spotLight = new THREE.SpotLight(0xffffff, 1);

        spotLight.position.set(75, 75, 15);
        spotLight.castShadow = true;
        spotLight.shadowCameraNear = 20;
        spotLight.shadowDarkness = .6;
        spotLight.shadowMapWidth = 512;
        spotLight.shadowMapHeight = 512;
        spotLight.shadowCameraFov = 64;

        scene.add(spotLight);

        render();

        function render() {
            var delta = clock.getDelta();
            orbitControls.update(delta);

            requestAnimationFrame(render);
            webGLRenderer.render(scene, camera);
        }
    }

    window.onload = init;
</script>
</body>
</html>
4

0 回答 0