区域光

作者: Codifier | 来源:发表于2019-11-12 09:21 被阅读0次
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../../three-part/threejs/three.js"></script>
    <script src="../../three-part/threejs/RectAreaLightUniformsLib.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;
        renderer.shadowMapSoft = true;
        renderer.shadowMap.type = THREE.PCFSoftShadowMap;
        document.getElementById("container").appendChild(renderer.domElement);

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

        // add area light
        /**
         * RectAreaLight( color : Integer, intensity : Float, width : Float, height : Float )
         * color - (optional) hexadecimal color of the light. Default is 0xffffff (white).
         * intensity - (optional) the light's intensity, or brightness. Default is 1.
         * width - (optional) width of the light. Default is 10.
         * height - (optional) height of the light. Default is 10.
         * @type {RectAreaLight}
         */
        let areaLight = new THREE.RectAreaLight(0xff0000, 10, 4, 10);
        areaLight.position.set(0, 20, 0);
        areaLight.rotation.y = Math.PI;
        scene.add(areaLight);

        // add area light helper
        let helper = new THREE.RectAreaLightHelper(areaLight);
        scene.add(helper);

        // create a plane
        let planeGeometry = new THREE.PlaneGeometry(70, 70, 1, 1);
        let planeMaterial = new THREE.MeshStandardMaterial({
            roughness: 0.044676705160855,
            metalness: 0.0
        });
        let plane = new THREE.Mesh(planeGeometry, planeMaterial);
        plane.rotation.x = -0.5 * Math.PI;
        scene.add(plane);

        // add a spotlight
        let spotLight = new THREE.SpotLight(0xcccccc);
        spotLight.position.set(-40, 60, -10);
        spotLight.intensity = 0.1;
        spotLight.lookAt(plane);
        scene.add(spotLight);

        // attributes which can be modified in GUI
        const controls = {
            "color" : areaLight.color.getStyle(),
            "intensity" : areaLight.intensity
        };
        // init GUI
        initGUI();

        renderScene();

        function initGUI(){
            let gui = new dat.GUI();
            gui.addColor(controls, 'color').onChange(function (e) {
                areaLight.color = new THREE.Color(e);
                helper.update();
            });
            gui.add(controls, 'intensity', 0, 1000).onChange(function (e) {
                areaLight.intensity = 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>

运行结果:



总结:
使用区域光的时候一定要先引入RectAreaLightUniformsLib.js,否则无法生效

相关文章

网友评论

      本文标题:区域光

      本文链接:https://www.haomeiwen.com/subject/mrlfictx.html