为什么我的点光源只能作用于cube和plane上,而sphere和octahedron对点光源毫无反应

来源:11-4 点光源

慕哥7265293

2023-01-15

图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
    </style>
    <script src="lib/three/three.js"></script>
</head>
<body>
<script>
    const scene = new THREE.Scene();
    const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
    camera.position.set(0, 0, 20);
    const renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);

    const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
    const cubeMaterial = new THREE.MeshLambertMaterial({color: 0xff0000, wireframe: false});
    const cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
    scene.add(cube);

    const sphereGeometry = new THREE.SphereGeometry(1, 10, 10);
    const sphereMaterial = new THREE.MeshLambertMaterial({color: 0x00ff00, wireframe: true});
    const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
    sphere.position.set(3, 1, 0)
    scene.add(sphere);

    const octahedronGeometry = new THREE.OctahedronGeometry(1, 0);
    const octahedronMaterial = new THREE.MeshLambertMaterial({color: 0x00ff00, wireframe: false});
    const octahedron = new THREE.Mesh(octahedronGeometry, octahedronMaterial);
    octahedron.position.set(-3, 0, 0)
    scene.add(octahedron);

    const planeGeometry = new THREE.PlaneGeometry(20, 30);
    const planeMaterial = new THREE.MeshLambertMaterial({color: 0x999999});
    const plane = new THREE.Mesh(planeGeometry, planeMaterial);
    plane.rotateZ(20);
    plane.position.z = -10;
    plane.position.x = 3;
    scene.add(plane);

    const ambientLight = new THREE.AmbientLight(0xffff00, 0.1);
    scene.add(ambientLight);

    // PointLight(color, intensity, distance, decay)
    // 参数1:颜色
    // 参数2:光照强度
    // 参数3:光源到光源结束的距离
    // 参数4:衰减程度
    const pointLight = new THREE.PointLight(0xff0000, 1, 1000, 2);
    pointLight.position.set(0, 0, 10);
    scene.add(pointLight)
    
    const animation = ()=>{
        cube.rotation.x += 0.01
        cube.rotation.y += 0.01
        sphere.rotation.x += 0.01
        octahedron.rotation.x += 0.01
        renderer.render(scene, camera);
        requestAnimationFrame(animation)
    }
    animation()
</script>
</body>
</html>
写回答

1回答

yancy

2023-01-16

程序没什么问题,需要注意的是在声明材质的时候需要注意材质颜色,因为光照颜色会根据夹角、物体颜色进行计算,如果最后计算出来的颜色为黑色的话,会表现出对光源不反应的情况。可以先把所有material的颜色删掉,使用默认颜色再看下。

0
0

WebGL+Three.js 入门与实战,系统学习 Web3D 技术

前端的技术蓝海,涨薪好选择

1081 学习 · 294 问题

查看课程