【问题标题】:Light Not showing on object灯光不显示在物体上
【发布时间】:2015-10-12 09:26:46
【问题描述】:

所以我一直在搞乱this,我终于让一切都为我工作了。我是任何类型 javascript 的初学者,所以我不知道这是否是一个简单的修复。基本上我不能像original 那样让光出现在月球上。

这就是我目前所拥有的。

<script type="text/javascript" id="mainCode">

var container, 
    renderer, 
    scene, 
    camera, 
    mesh,
    light = {
       speed: 0.1,
       distance: 1000,
       position: new THREE.Vector3(0, 0, 0),
       orbit: function (center, time) {
            this.position.x =
                (center.x + this.distance) * Math.sin(time * -this.speed);

            this.position.z =
                (center.z + this.distance) * Math.cos(time * this.speed);
        }
    },
    clock,
    controls;


        init();

        function init() {

        // grab the container from the DOM
        container = document.getElementById( "container" );


        scene = new THREE.Scene();

        var fov = 35,
        aspect = window.innerWidth / window.innerHeight,
        near = 1,
        far = 65536;

        renderer = new THREE.WebGLRenderer({antialias: true, preserveDrawingBuffer: true});   
        renderer.setClearColor(0x000000, 1);
        renderer.setSize(window.innerWidth, window.innerHeight);
        container.appendChild( renderer.domElement );

        camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
        camera.position.set(0, 0, 800);

        scene.add(camera);

        controls = new THREE.TrackballControls(camera);
        controls.rotateSpeed = 0.5;
        controls.dynamicDampingFactor = 0.5;

        clock = new THREE.Clock();

        var radius = 100;
        var xSegments = 50;
        var ySegments = 50;
        var geo = new THREE.SphereGeometry(radius, xSegments, ySegments);

        var mat = new THREE.ShaderMaterial({
            uniforms: {
                lightPosition: {
                    type: 'v3',
                    value: light.position
                },
                textureMap: {
                    type: 't',
                    value: THREE.ImageUtils.loadTexture( "img/maps/moon.jpg" )
                },
                normalMap: {
                    type: 't',
                    value: THREE.ImageUtils.loadTexture( "img/maps/normal.jpg" )
                },
                uvScale: {
                    type: 'v2',
                    value: new THREE.Vector2(1.0, 1.0)

                }


            },
            vertexShader:document.getElementById('vertexShader').textContent,
            fragmentShader:document.getElementById('fragmentShader').textContent

        });

          mesh = new THREE.Mesh(geo, mat);     
          mesh.geometry.computeTangents();
          mesh.position.set(0, 0, 0);
          mesh.rotation.set(0, 180, 0);
          scene.add(mesh);


    }

    function onWindowResize() {
        renderer.setSize(window.innerWidth, window.innerHeight);
        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();
    }

    function animate() {
        requestAnimationFrame(animate);
        light.orbit(mesh.position, clock.getElapsedTime());
        controls.update(camera);
        renderer.render(scene, camera);

    }
    animate();

    window.addEventListener('resize', onWindowResize, false);


</script>

【问题讨论】:

  • 从 three.js r.72 中删除了切线支持。如果您想继续使用现有代码,可以尝试以前的 three.js 版本:cdnjs.com/libraries/three.jsMeshPhongMaterial 改为使用“导数切线”。

标签: javascript three.js


【解决方案1】:

您永远不会在场景中添加任何灯光。月亮示例使用了比您现在需要的更复杂的自定义着色器。例如,您需要创建一个常规的三灯并将其添加到您的场景中

http://threejs.org/docs/#Reference/Lights/PointLight

var light = new THREE.PointLight( 0xff0000, 1, 100 );
light.position.set( 50, 50, 50 );
scene.add( light );

【讨论】:

  • 这是否允许我只在月球上进行灯光秀并像示例中那样围绕月球旋转?
  • 我觉得我知道我的问题,除了你已经指出的。我不知道我是否应该将其发布在新线程中,但最新版本似乎不支持切线,就像该脚本使用该函数一样,我认为这没关系,但我只是注意到我的着色器严重依赖它们将光线映射到纹理,因为它围绕我的对象旋转。所以这就是我的代码的样子,有什么办法可以更新它,以便它适用于新版本。
猜你喜欢
  • 1970-01-01
  • 2019-06-30
  • 2014-12-21
  • 1970-01-01
  • 2020-04-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-27
  • 2016-08-22
相关资源
最近更新 更多