方向光

作者: Codifier | 来源:发表于2019-11-11 16:37 被阅读0次
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Directional 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 : 0xff0000
        });
        let sphere = new THREE.Mesh(sphereGeometry, sphereMatrial);
        sphere.position.set(0, 4, 2);
        sphere.castShadow = true;
        scene.add(sphere);

        // add directional light
        /**
         * DirectionalLight( color : Integer, intensity : 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.
         * @type {DirectionalLight}
         */
        let directionalLight = new THREE.DirectionalLight("#ff5808");
        directionalLight.position.set(-20, 30, -5);
        directionalLight.intensity = 0.5;
        directionalLight.castShadow = true;
        // orthographic camera
        directionalLight.shadow.camera.near = 2;
        directionalLight.shadow.camera.far = 80;
        directionalLight.shadow.camera.left = -30;
        directionalLight.shadow.camera.right = 30;
        directionalLight.shadow.camera.top = 30;
        directionalLight.shadow.camera.bottom = -30;
        directionalLight.shadow.mapSize.width = 1024;
        directionalLight.shadow.mapSize.height = 1024;
        scene.add(directionalLight);

        // add directional light helper
        let helper = new THREE.DirectionalLightHelper(directionalLight);
        scene.add(helper);


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

        renderScene();

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

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

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

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

运行结果:


相关文章

  • 向光亮那方

    晏城是一座临海的小城,东面环山,本应是富奢之地的晏城,如今却乌云笼罩,未曾有过一丝光芒。 因为没有光照的缘故,寸草...

  • 向光亮那方奔跑

    黑暗将我一点一点地吞噬 我无力挣脱 但内心不甘屈服 我想逃离 我想寻找光亮 我想发光 我想照亮这黑暗 我看到了那束...

  • 向光亮的那方

    最难的时候有多难呢?挺不过的时候怎么办?我觉得就是告诉自己,没关系,只要我度过去,现在是一座大山的障碍,以后回首的...

  • 向光班的故事16

    今天开始,我和孩子们一起商量把班级定位向光班,向光而行,我们现在可能成为不了光,但我们可以向光,向光学习,最后成为...

  • 【对天使的表达.记录点滴】

    【2022年1月31日晚】 新的一年,祝快乐开心!/::> /::> /::> 虎年吉祥,挥斥方遒,一路向光! 妈...

  • 向光

    “在我们的眼中没有太阳,一直都是夜晚,但却不暗淡,因为有能代替太阳的东西。” ——《白夜行》 1 7-11的灯光在...

  • 向光

    在天堂,那绽放光芒的地方。 引一条白色被剥离的蚕丝,缠绕着飞蛾幻境的灵魂,向我娓娓而来。 那炽热的光芒令我忧伤,突...

  • 向光

    浩瀚的星辰 总有你想仰望着的那一处 最闪耀的那一颗 也许是你 追寻自己想要的 努力 会眷顾你 所以星河为你而铺 照...

  • 向光

    掸灰、脱尘 苦头、欢颜 蛙鸣蝉歌 衣解发散 炕头、娇娘, 烛焰、瞎蛾 扑一往情深, 扑微光闪烁, 缠绵微晕 落得焦...

  • 向光

    说我迷恋向日葵 倒不如说 我在疯狂的迷恋着阳光的味道 想活着 想怎样活着 又能活多久 我不知道 未来怎样 现在怎样...

网友评论

      本文标题:方向光

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