光斑

作者: Codifier | 来源:发表于2019-11-12 09:44 被阅读0次
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Lens Flare</title>
        <script src="../../three-part/threejs/three.js"></script>
        <script src="../../three-part/threejs/Lensflare.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();
    
            let textureGrass = new THREE.TextureLoader().load("../assets/textures/ground/grasslight-big.jpg");
            textureGrass.wrapS = THREE.RepeatWrapping;
            textureGrass.wrapT = THREE.RepeatWrapping;
            textureGrass.repeat.set(10, 10);
    
            let planeGeometry = new THREE.PlaneGeometry(1000, 1000, 20, 20);
            let planeMaterial = new THREE.MeshLambertMaterial({
                map: textureGrass
            });
            let plane = new THREE.Mesh(planeGeometry, planeMaterial);
            plane.receiveShadow = true;
            plane.rotation.x = -0.5 * Math.PI;
            plane.position.x = 15;
            plane.position.y = 0;
            plane.position.z = 0;
            scene.add(plane);
    
            // create a cube
            let cubeGeometry = new THREE.BoxGeometry(4, 4, 4);
            let cubeMaterial = new THREE.MeshLambertMaterial({
                color: 0xff3333
            });
            let cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
            cube.castShadow = true;
            cube.position.x = -4;
            cube.position.y = 3;
            cube.position.z = 0;
            scene.add(cube);
    
            let sphereGeometry = new THREE.SphereGeometry(4, 25, 25);
            let sphereMaterial = new THREE.MeshLambertMaterial({
                color: 0x7777ff
            });
            let sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
            sphere.position.x = 10;
            sphere.position.y = 5;
            sphere.position.z = 10;
            sphere.castShadow = true;
            scene.add(sphere);
    
            let directionalLight = new THREE.DirectionalLight("#ffffff");
            directionalLight.position.set(30, 30, -50);
            directionalLight.castShadow = true;
            directionalLight.intensity = 3;
            directionalLight.target = plane;
            directionalLight.distance = 0;
            directionalLight.shadow.camera.near = 2;
            directionalLight.shadow.camera.far = 200;
            directionalLight.shadow.camera.left = -100;
            directionalLight.shadow.camera.right = 100;
            directionalLight.shadow.camera.top = 100;
            directionalLight.shadow.camera.bottom = -100;
            directionalLight.shadow.mapSize.width = 2048;
            directionalLight.shadow.mapSize.height = 2048;
            scene.add(directionalLight);
    
            let textureFlare0 = new THREE.TextureLoader().load("../assets/textures/flares/lensflare0.png");
            let textureFlare3 = new THREE.TextureLoader().load("../assets/textures/flares/lensflare3.png");
            let flareColor = new THREE.Color(0xffaacc);
    
            let lensFlare = new THREE.Lensflare();
            /**
             * LensflareElement( texture : Texture, size : Float, distance : Float, color : Color )
             * texture - THREE.Texture to use for the flare.
             * size - (optional) size in pixels
             * distance - (optional) (0-1) from light source (0 = at light source)
             * color - (optional) the Color of the lens flare
             * @type {THREE.LensflareElement}
             */
            lensFlare.addElement(new THREE.LensflareElement(textureFlare0, 350, 0.0, flareColor));
            lensFlare.addElement(new THREE.LensflareElement(textureFlare3, 60, 0.6, flareColor));
            lensFlare.addElement(new THREE.LensflareElement(textureFlare3, 70, 0.7, flareColor));
            lensFlare.addElement(new THREE.LensflareElement(textureFlare3, 120, 0.9, flareColor));
            lensFlare.addElement(new THREE.LensflareElement(textureFlare3, 70, 1.0, flareColor));
            directionalLight.add(lensFlare);
    
            // attributes which can be modified in GUI
            const controls = {
    
            };
            // init GUI
            initGUI();
    
            renderScene();
    
            function initGUI(){
                let gui = new dat.GUI();
            }
    
            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>
    

    运行结果:



    总结:
    使用光斑效果时要先把Lensflare.js文件加入进来,否则不生效。

    相关文章

      网友评论

          本文标题:光斑

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