聚光灯

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Spot Light</title>
    <script src="../../three-part/threejs/three.js"></script>
    <script src="../../three-part/utils/stats.min.js"></script>
    <script src="../../three-part/utils/dat.gui.min.js"></script>
    <script src="../controls/TrackballControls.js"></script>
    <script src="../util/util.js"></script>
    <style>
        body {
            margin: 0;
            overflow: hidden;
        }
    </style>
</head>
<body>
<div id="container"></div>
<script type="text/javascript">
    init();
    function init() {
        // show FPS
        let stats = initStats();
        // resize
        window.addEventListener('resize', onResize, false);

        let scene = new THREE.Scene();
        let camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.x = -30;
        camera.position.y = 40;
        camera.position.z = 30;
        camera.lookAt(scene.position);

        let renderer = new THREE.WebGLRenderer();
        renderer.setClearColor(new THREE.Color(0x000000));
        renderer.setSize(window.innerWidth, window.innerHeight);
        renderer.shadowMap.enabled = true;
        document.getElementById("container").appendChild(renderer.domElement);

        // init trackball control
        let trackballControls = initTrackballControls(camera, renderer);
        let clock = new THREE.Clock();

        // add a plane
        let planeGeometry = new THREE.PlaneGeometry(60, 60, 1, 1);
        let planeMaterial = new THREE.MeshLambertMaterial({
            color: 0xffffff
        });
        let plane = new THREE.Mesh(planeGeometry, planeMaterial);
        plane.receiveShadow = true;
        plane.rotation.x = -0.5 * Math.PI;
        scene.add(plane);

        // add a sphere
        let sphereGeometry = new THREE.SphereGeometry(4, 20, 20);
        let sphereMatrial = new THREE.MeshLambertMaterial({
            color : 0x7777ff
        });
        let sphere = new THREE.Mesh(sphereGeometry, sphereMatrial);
        sphere.position.set(0, 4, 2);
        sphere.castShadow = true;
        scene.add(sphere);

        /**
         * SpotLight( color : Integer, intensity : Float, distance : Float, angle : Radians, penumbra : Float, decay : Float )
         * color - (optional) hexadecimal color of the light. Default is 0xffffff (white).
         * intensity - (optional) numeric value of the light's strength/intensity. Default is 1.
         * distance - Maximum range of the light. Default is 0 (no limit).
         * angle - Maximum angle of light dispersion from its direction whose upper bound is Math.PI/2.
         * penumbra - Percent of the spotlight cone that is attenuated due to penumbra. Takes values between zero and 1. Default is zero.
         * decay - The amount the light dims along the distance of the light.
         * @type {SpotLight}
         */
        let spotLight = new THREE.SpotLight("#ffffff");
        spotLight.position.set(0, 30, 2);
        spotLight.castShadow = true;
        spotLight.shadow.camera.near = 1;
        spotLight.shadow.camera.far = 100;
        spotLight.target = plane;
        spotLight.distance = 100;
        spotLight.angle = 0.4;
        spotLight.shadow.camera.fov = 120;
        scene.add(spotLight);

        // add spot light helper
        let spotLightHelper = new THREE.SpotLightHelper(spotLight);
        scene.add(spotLightHelper);


        // attributes which can be modified in GUI
        const controls = {
            "pointColor" : spotLight.color.getStyle(),
            "intensity" : spotLight.intensity,
            "distance" : spotLight.distance,
            "angle" : spotLight.angle,
            "castShadow" : true,
            "penumbra" : spotLight.penumbra
        };
        // init GUI
        initGUI();

        renderScene();

        function initGUI(){
            let gui = new dat.GUI();

            gui.addColor(controls, 'pointColor').onChange(function (e) {
                spotLight.color = new THREE.Color(e);
            });

            gui.add(controls, 'angle', 0, Math.PI * 2, 0.05).onChange(function (e) {
                spotLight.angle = e;
                spotLightHelper.update();
            });

            gui.add(controls, 'intensity', 0, 5).onChange(function (e) {
                spotLight.intensity = e;
            });

            gui.add(controls, 'penumbra', 0, 1, 0.1).onChange(function (e) {
                spotLight.penumbra = e;
            });

            gui.add(controls, 'distance', 0, 200).onChange(function (e) {
                spotLight.distance = e;
                spotLightHelper.update();
            });

            gui.add(controls, 'castShadow').onChange(function (e) {
                spotLight.castShadow = e;
            });
        }

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

        function renderScene(){
            trackballControls.update(clock.getDelta());
            stats.update();
            requestAnimationFrame(renderScene);
            renderer.render(scene, camera);
        }
    }
</script>
</body>
</html>

运行结果:


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容