【three.js练习程序】旋转物体自身

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>ceshi</title>
    <script type="text/javascript" src=".\build\three.js"></script>
    <style>
        body
        {
            margin: 0;
            overflow: hidden;
        }
    </style>
</head>
<body>
    <!-- 作为Three.js渲染器输出元素 -->
    <div id="WebGL-output">
    </div>
    <!-- 第一个 Three.js 样例代码 -->
    <script type="text/javascript">


        var cube = new Array(100);
        var rotx = new Array(100);
        var roty = new Array(100);
        var rotz = new Array(100);
        var camera, scene, renderer;
        var id = null;
        var fov = 45;
        var near = 0.1;
        var far = 1000;
        init();

        function init() {

            scene = new THREE.Scene();

            camera = new THREE.PerspectiveCamera(fov, window.innerWidth / window.innerHeight, near, far);

            camera.position.x = 100;
            camera.position.y = 100;
            camera.position.z = 100;
            camera.lookAt(scene.position);

            //创建一个WebGL渲染器并设置其大小
            renderer = new THREE.WebGLRenderer();
            renderer.setClearColor(new THREE.Color(0x000000));
            renderer.setSize(window.innerWidth, window.innerHeight);

            var axes = new THREE.AxisHelper(200);
            scene.add(axes);

            for (var i = 0; i < 100; i++) {
                var geometry = new THREE.BoxGeometry(5, 5, 5);
                for (var j = 0; j < geometry.faces.length; j += 2) {
                    var hex = Math.random() * 0xffffff;
                    geometry.faces[j].color.setHex(hex);
                    geometry.faces[j + 1].color.setHex(hex);
                }

                rotx[i] = Math.random() * 0.005;
                roty[i] = Math.random() * 0.005;
                rotz[i] = Math.random() * 0.005;
                var material = new THREE.MeshBasicMaterial({ vertexColors: 0xff0000 });
                cube[i] = new THREE.Mesh(geometry, material);

                cube[i].position.x = Math.random() * 100 - 50;
                cube[i].position.y = Math.random() * 100 - 50;
                cube[i].position.z = Math.random() * 100 - 50;
                cube[i].receiveShadow = true;
                scene.add(cube[i]);
                id = setInterval(render, 33);
            }

            document.addEventListener('mousewheel', mousewheel, false);

        }
        function render() {
            for (var i = 0; i < 100; i++) 
            {
                cube[i].rotation.x += rotx[i];
                cube[i].rotation.y += roty[i];
                cube[i].rotation.z += rotz[i];
            }

           renderer.render(scene, camera);
        }

        function mousewheel(e) {
            e.preventDefault();
            //e.stopPropagation();  
            if (e.wheelDelta) {  //判断浏览器IE,谷歌滑轮事件  
                if (e.wheelDelta > 0) { //当滑轮向上滚动时  
                    fov -= (near < fov ? 1 : 0);
                }
                if (e.wheelDelta < 0) { //当滑轮向下滚动时  
                    fov += (fov < far ? 1 : 0);
                }
            } else if (e.detail) {  //Firefox滑轮事件  
                if (e.detail > 0) { //当滑轮向上滚动时  
                    fov -= 1;
                }
                if (e.detail < 0) { //当滑轮向下滚动时  
                    fov += 1;
                }
            }
            camera.fov = fov;
            camera.updateProjectionMatrix();
            renderer.render(scene, camera);

        }

        document.getElementById("WebGL-output").appendChild(renderer.domElement);
   

    </script>
</body>
</html>

 

 

posted @ 2018-02-15 13:56  Dsp Tian  阅读(3078)  评论(0编辑  收藏  举报