three.js入门2

<html>
    <head>
        <meta charset="UTF-8">
        <title>lesson1-by-shawn.xie</title>
        <!--引入Three.js-->
        <script src="../build/three.min.js"></script>
        <script type="text/javascript">
            //开启Three.js渲染器
            var renderer; //声明全局变量(对象)
            function initThree() {
                width = document.getElementById('canvas3d').clientWidth; //获取画布「canvas3d」的宽
                height = document.getElementById('canvas3d').clientHeight; //获取画布「canvas3d」的高
                renderer = new THREE.WebGLRenderer({ antialias: true }); //生成渲染器对象(属性:抗锯齿效果为设置有效)
                renderer.setSize(width, height); //指定渲染器的高宽(和画布框大小一致)
                document.getElementById('canvas3d').appendChild(renderer.domElement); //追加 【canvas】 元素到 【canvas3d】 元素中。
                renderer.setClearColorHex(0xFFFFFF, 1.0); //设置canvas背景色(clearColor)
            }
            //设置相机
            var camera;
            function initCamera() {
                camera = new THREE.PerspectiveCamera(45, width / height, 1, 5000); //设置透视投影的相机,默认情况下相机的上方向为Y轴,右方向为X轴,沿着Z轴朝里(视野角:fov 纵横比:aspect 相机离视体积最近的距离:near 相机离视体积最远的距离:far)
                camera.position.x = 0; //设置相机的位置坐标
                camera.position.y = 50; //设置相机的位置坐标
                camera.position.z = 100; //设置相机的位置坐标
                camera.up.x = 0; //设置相机的上为「x」轴方向
                camera.up.y = 1; //设置相机的上为「y」轴方向
                camera.up.z = 0; //设置相机的上为「z」轴方向
                camera.lookAt({ x: 0, y: 0, z: 0 }); //设置视野的中心坐标
            }
            //设置场景
            var scene;
            function initScene() {
                scene = new THREE.Scene();
            }

            //设置光源
            var light;
            function initLight() {
                light = new THREE.DirectionalLight(0xff0000, 1.0, 0); //设置平行光源
                light.position.set(200, 200, 200); //设置光源向量
                scene.add(light); // 追加光源到场景
            }
            //设置物体
            var sphere;
            function initObject() {
                sphere = new THREE.Mesh(
                     new THREE.SphereGeometry(20, 20),                //width,height,depth
                     new THREE.MeshLambertMaterial({ color: 0xff0000 }) //材质设定
                );
                scene.add(sphere);
                sphere.position.set(0, 0, 0);
            }
            //执行
            function threeStart() {
                initThree();
                initCamera();
                initScene();
                initLight();
                initObject();
                renderer.clear();
                renderer.render(scene, camera);
            }
        </script>
        <style type="text/css">
            div#canvas3d{
                  border: none;
                  cursor: move;
                  width: 1400px;
                  height: 600px;
                  background-color: #EEEEEE;
                }
        </style>
    </head>

    <body onload='threeStart();'>
        <!--盛放canvas的容器-->
        <div id="canvas3d"></div>
    </body>
</html>
接下来,就准备开始一点点理解开源社区three.js包里的examples了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值