点光

作者: Codifier | 来源:发表于2019-11-11 16:24 被阅读0次
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Point 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);

        // add point light
        /**
         * PointLight( color : Integer, intensity : Float, distance : Number, 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).
         * decay - The amount the light dims along the distance of the light. Default is 1.
         * @type {PointLight}
         */
        let pointLight = new THREE.PointLight("#ccffcc");
        pointLight.decay = 0.1;
        pointLight.castShadow = true;
        pointLight.distance = 100;
        pointLight.intensity = 1;
        pointLight.position.set(0, 20, 2);
        scene.add(pointLight);

        // add point light helper
        let helper = new THREE.PointLightHelper(pointLight);
        scene.add(helper);


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

        renderScene();

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

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

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

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

            gui.add(controls, 'decay', 0, 1, 0.05).onChange(function (e) {
                pointLight.decay = e;
            });

            gui.add(controls, 'castShadow').onChange(function (e) {
                pointLight.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>

运行结果:


相关文章

  • 光点

    奥普拉说过:“一个人可以非常清贫、困顿、低微,但是不可以没有梦想。只要梦想存在一天,就可以改变自己的处境。...

  • 光点

    月下灯光闪烁 亮如白昼的广场上 人们正描绘着不同的图景 适宜的气温 沁人的芳香 和着浓重的青草气息 招展出五月的多...

  • 点光

    太阳被未知追赶 黑暗压塌光明的脊梁 入目,是不知颜色的霜 我的步伐,带着些许踉跄 悄无声息的晦暗 刺透我微小的躯体...

  • 光点

    (一) 夕阳落在树梢的那一刻,公园里的一角如同一张泛黄的老照片。 阴绿的树叶悲切地轻轻颤动着,晦黄的暮光从叶片尖端...

  • 光点

    窗户是透明的 黑夜是它的沉默背景 而夜空失去了它的 两个最好的朋友 星星和月亮 窗内 没有一丝光亮 更别提他人送来...

  • 点光

    城市的街道两边空荡荡 空中飘浮着迷茫 路口的霓虹灯不停闪耀 是孤单无助的求救信号 路边的橱窗黑着 黑着 热闹的小...

  • 光点

    人这一生总要为了某个人而暂时忘记自我,在旁人看来出奇的疯狂,无法理解也没关系。山重水复疑无路,柳暗花明又一村。我终...

  • 点光

    运行结果:

  • 光点

    已入大暑,天气燥热,外面的太阳似炉火,溶着自己能溶的一切。 被爬山虎所占据的墙面上留着一扇孤零零发旧的木窗,透过布...

  • 光点

    当你成为我生命中一个耀眼的光点,在心灵的每一个角落逐渐驱逐黑暗!成为我生命的一部分,我会每一天寻找那个光点,他是否...

网友评论

      本文标题:点光

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