2

I'm trying to add a lamp to my scene with an OBJLoader object. The object is exported from Blender. If I set its children[i].castShadow property to true the following errors show up but still it appears and its shadow can be seen on the wall:

the error message

What am I doing wrong? If and when I add some other object to my scene and set its castShadow property to true I get no error messages whatsoever. This is the only problematic object I have currently.

One more thing. My project won't work in Firefox, but it displays in Edge or Chrome for example. What is so special about 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>
Wilt
  • 41,477
  • 12
  • 152
  • 203
masm64
  • 1,222
  • 3
  • 14
  • 31

0 Answers0