【问题标题】:spotLight not attached to camera properly聚光灯未正确连接到相机
【发布时间】:2021-12-18 16:31:53
【问题描述】:

我仍在努力让聚光灯粘在相机上。我可以看到光,但看起来它停留在一个地方(?)。 参考video

//Camera
camera = new THREE.PerspectiveCamera( 90, window.innerWidth / window.innerHeight, 0.1, 1000 );
camera.position.set( 0, 1, 0 );
//spotLight attached to camera
spotlight = new THREE.SpotLight( 0xffffff, 55 );
spotlight.angle = 0.20*(Math.PI / 3);
spotlight.penumbra = 0.1;
spotlight.decay = 2;
spotlight.distance = 200;
camera.add( spotlight);
camera.add( spotlight.target );
                
spotlight.target.position.set( 0, 0, 1 );
spotlight.target=camera;
spotlight.position.copy( camera.position );

controls = new PointerLockControls( camera, document.body );
//adding first person camera from PointerLockControls
scene.add( controls.getObject() );

我也尝试将相机和聚光灯分组:

const group = new THREE.Group();
group.add(camera);
group.add(spotlight);
spotlight.target=camera;
spotlight.position.copy( camera.position );
controls = new PointerLockControls( group, document.body );

但这也不起作用。我应该改变什么?这里缺少什么?

//编辑这是我当前代码的样子

import * as THREE from "../node_modules/three/build/three.module.js";
            import { GUI } from './jsm/libs/dat.gui.module.js';
            
            let renderer, scene, camera, gui;
            let spotlight, lightHelper;

            function init() {
                renderer = new THREE.WebGLRenderer();
                renderer.setPixelRatio( window.devicePixelRatio );
                renderer.setSize( window.innerWidth, window.innerHeight );
                document.body.appendChild( renderer.domElement );
                scene = new THREE.Scene();

                camera = new THREE.PerspectiveCamera( 90, window.innerWidth / window.innerHeight, 0.1, 1000 );
                camera.position.set(0,0,1);
                const boxgeometry = new THREE.BoxGeometry( 25, 25, 25 );
                const boxmaterial = new THREE.MeshBasicMaterial( {color: 0x00ff00} );
                const cube = new THREE.Mesh( boxgeometry, boxmaterial );
                scene.add( cube );
                cube.position.set(-20,0,1);

                const ambient = new THREE.AmbientLight( 0xffffff, 0.2 );
                scene.add(ambient);

                **scene.add(camera);
                spotlight = new THREE.SpotLight(0xffffff, 55, 80, 0.8*Math.PI);
                camera.add(spotlight);
                camera.add(spotlight.target);**

                let material = new THREE.MeshPhongMaterial( { color: 0x808080, dithering: true } );
                let geometry = new THREE.PlaneGeometry( 2000, 2000 );
                let floor= new THREE.Mesh( geometry, material );
                floor.position.set( 0, - 1, 0 );
                floor.rotation.x = - Math.PI * 0.5;
                floor.receiveShadow = true;
                scene.add(floor);
                render();

                window.addEventListener( 'resize', onWindowResize );

            }
        function animate() 
            {
                requestAnimationFrame( animate );
                camera.rotation.y+=0.01;
                renderer.render( scene, camera );
            }
            animate();

【问题讨论】:

    标签: javascript three.js lighting


    【解决方案1】:

    设置应如下所示:

    let camera, scene, renderer;
    
    init();
    
    function init() {
    
      camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 10);
      camera.position.z = 1;
    
      scene = new THREE.Scene();
      scene.add(camera);
    
      const ambientLight = new THREE.AmbientLight(0xffffff, 0.4);
      scene.add(ambientLight);
    
      const spotLight = new THREE.SpotLight(0xffffff, 0.6, 0, Math.PI * 0.05);
      camera.add(spotLight);
    
      const geometry = new THREE.PlaneGeometry();
      const material = new THREE.MeshPhongMaterial();
    
      const mesh = new THREE.Mesh(geometry, material);
      scene.add(mesh);
    
      renderer = new THREE.WebGLRenderer({
        antialias: true
      });
      renderer.setPixelRatio(window.devicePixelRatio);
      renderer.setSize(window.innerWidth, window.innerHeight);
      renderer.setAnimationLoop(animation);
      document.body.appendChild(renderer.domElement);
    
    }
    
    function animation(time) {
    
      const t = time * 0.001;
    
      camera.position.x = Math.sin(t) * 0.25;
      camera.position.y = Math.cos(t) * 0.25;
    
      renderer.render(scene, camera);
    
    }
    body {
      margin: 0;
    }
    <script src="https://cdn.jsdelivr.net/npm/three@0.134.0/build/three.min.js"></script>

    将聚光灯作为子级添加到相机并将相机本身添加到场景中很重要。

    【讨论】:

    • 嗨,请原谅我回复晚了,但我不得不休息一下。我将您的代码与我的代码合并,聚光灯消失了,不再可见。我试图改变强度、距离、角度但无济于事,看不到它。这可能是 PointLockControls 的问题吗?
    • 好吧,如果您不使用PointLockControls,它是否有效?
    • 检查了仅使用默认摄像头的效果,结果很奇怪……摄像头出于某种原因一直在晃动。 [VideoShowcase]() 在 animate() 函数中我只有这个:`requestAnimationFrame(animate);常量时间 = 性能.now(); camera.rotation.y+=0.03; renderer.render(scene, camera);` 我应该上传删除垃圾代码/样式的整个脚本吗?
    • 视频链接:video
    猜你喜欢
    • 2016-11-18
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    相关资源
    最近更新 更多