半球光

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

        // create a plane
        // create texture
        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);

        // add hemisphere light
        /**
         * HemisphereLight( skyColor : Integer, groundColor : Integer, intensity : Float )
         * skyColor - (optional) hexadecimal color of the sky. Default is 0xffffff.
         * groundColor - (optional) hexadecimal color of the ground. Default is 0xffffff.
         * intensity - (optional) numeric value of the light's strength/intensity. Default is 1.
         * @type {HemisphereLight}
         */
        let hemiLight = new THREE.HemisphereLight(0xffffff, 0x00ff00, 0.6);
        hemiLight.intensity = 1;
        hemiLight.position.set(0, 500, 0);
        scene.add(hemiLight);

        // 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);

        // add a sphere
        let sphereGeometry = new THREE.SphereGeometry(4, 25, 25);
        let sphereMaterial = new THREE.MeshPhongMaterial({
            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);

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

        renderScene();

        function initGUI(){
            let gui = new dat.GUI();
            gui.addColor(controls, 'skyColor').onChange(function (e) {
                hemiLight.color = new THREE.Color(e);
            });

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

            gui.add(controls, 'intensity', 0, 5).onChange(function (e) {
                hemiLight.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>

运行结果:


相关文章

  • 半球光

    运行结果:

  • Three.js光源梳理4——半球光(HemisphereLig

    半球光是一个比较有意思的光,Unity中是没有这种光的,半球光的图示如下: 半球光的原理由两部分组成,一部分是从下...

  • 活着或死去的

    活着或死去的 是我的心 孤独地晃悠 在冷热的风景 活着的 心的左半球 日夜发着光 是热风景 死去的 心的右半球 日...

  • 半球

    時空的變化是會給人帶來影響的關於這一點她原來就曉得現在就更曉得了 比如 在這裡在異國的他鄉她會問 「我是誰我從哪裡...

  • Unity官方教程-Creating Believable Vi

    6. 灯光设置 6. 1 户外灯光设置 半球光(Hemisphere lighting):这是户外灯光设置的第一项...

  • 南半球北半球

    以前一直想到底有没有史前文明,或者更智慧的生物存在,读过这本书后确信是有的,这不是科幻,不是科普,是考察报告 一个...

  • 北半球南半球

    天天有人在张望着南半球 期待南半球 雄起

  • 七大洲

    地球可以划分为东半球和西半球。西半球上有南、北美洲,东半球上有亚洲、欧洲、非洲和大洋洲。南极洲则横跨东西半球。地球...

  • 百日随笔(十九)

    此刻北半球的冬至,到了南半球就成了夏至;北半球冬至夜晚时间最长,南半球夏至夜晚时间最短。赤道线划分出南北半...

  • 立秋

    我在南半球,想念北半球的立秋

网友评论

      本文标题:半球光

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