threeJS-Helper07-GridHelper(网格助手)

需要电子档书籍或者源码可以Q群:828202939   希望可以和大家一起学习、一起进步!!

如有错别字或有理解不到位的地方,可以留言或者加微信15250969798,博主会及时修改!!!!!

博主的案例并不难,只是为了更好的给想入门threeJS的同学一点点经验!!!!!

本章节学习的内容可以从的官方文档中找到

涉及的知识点博主已经从three源码库里面摘要出来放在对应的注释里面

今天学习Helper里面的GridHelper助手!

顺便加入了鼠标控制相机的组件OrbitControls,不懂可以先忽略!

效果图:

代码:

<html>

<head>
    <title>Helper07-GridHelper(网格助手)</title>
    <style>
        body {
            margin: 0;
        }

        canvas {
            width: 100%;
            height: 100%
        }
    </style>
</head>

<body>
    <script src="../../../build/three.js"></script>
    <script src="../../js/controls/OrbitControls.js"></script>
    <script>
        var camera, scene, renderer, geometry, material, animate, controls; //常用变量
        var cube, light, helper; //自定义对象变量
        var target = new THREE.Vector3(0, 0, 0);
        init();
        animate();

        function init() {
            scene = new THREE.Scene();
            scene.background = new THREE.Color(0xcfcfcf);
            camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
            camera.position.set(0, 50, 150);
            camera.lookAt(target);
            camera.updateProjectionMatrix(); //相机更新

            // DirectionalLight( color, intensity )
            light = new THREE.DirectionalLight(0xFFFFFF, 2);
            light.position.set(20, 20, 50);

            //网格助手模块
            // GridHelper( size:网格的大小, divisions:跨网格的分割数, color1:中心线的颜色, color2:网格线条的颜色 ) 
            var size = 120;
            var divisions = 20;
            var gridHelper = new THREE.GridHelper(size, divisions);
            gridHelper.position.set(0,-20,0)
            scene.add(gridHelper);

            renderer = new THREE.WebGLRenderer();
            renderer.setSize(window.innerWidth, window.innerHeight);
            document.body.appendChild(renderer.domElement);

            //OrbitControls控件操作模块
            controls = new THREE.OrbitControls(camera, renderer.domElement);
            controls.addEventListener('change', function () {
                renderer.render(scene, camera);
            });

            // BoxGeometry( width, height, depth, widthSegments, heightSegments, depthSegments )
            geometry = new THREE.BoxGeometry(30, 30, 30, 3, 3, 3);
            material = new THREE.MeshBasicMaterial({
                color: 0xcfffff,
                wireframe: false
            }); //wireframe默认为false

            cube = new THREE.Mesh(geometry, material);

            // FaceNormalsHelper( object, size, hex, linewidth )
            helper = new THREE.FaceNormalsHelper(cube, 10, 0x00ffcc, 1);
            scene.add(cube, helper);
        }

        function animate() {
            requestAnimationFrame(animate);
            controls.update();
            cube.rotation.x=cube.rotation.y+= 0.01;
            if (helper) helper.update(); //判断是否有法向助手,有则更新
            renderer.render(scene, camera);
        };
    </script>
</body>
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章