Three.Js开发WebGL创建三维空间
首先我们需要创建一个 HTML 文件 如:index.html
导入Three.Js 与 设置网页初始化 (边距为0 溢出隐藏)
| <!doctype html><html lang="zh-CN">
 <head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="IE=Edge">
 <title>Demo-01</title>
 <script src="libs/Three.js"></script>
 <style>
 body{
 margin: 0;
 overflow: hidden;
 }
 </style>
 </head>
 
 <body></body>
 </html>
 
 | 
Three.Js GitHub地址:点击跳转
Three.Js API说明文档:点击跳转
或复制网页端Three.js源码使用:点击跳转
在 HTML文件中我们要创建容器
| <!doctype html><html lang="zh-CN">
 <head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="IE=Edge">
 <title>Demo-01</title>
 <script src="libs/Three.js"></script>
 <style>
 body{
 margin: 0;
 overflow: hidden;
 }
 </style>
 </head>
 
 <body>
 
 <div id="webGl-output">
 
 </div>
 
 
 <script></script>
 </body>
 </html>
 
 | 
WEBGL 三维画面效果的创建(放置在Script标签下):
| function init(){
 var scene = new THREE.Scene();
 
 var camera = new THREE.PerspectiveCamera(45,window.innerWidth/window.innerHeight,0.1,2000);
 
 var renderer = new THREE.WebGLRenderer();
 
 renderer.setClearColor(new THREE.Color(0xEEEEEE));
 
 renderer.setSize(window.innerWidth,window.innerHeight);
 
 var axes = new THREE.AxisHelper(20);
 
 scene.add(axes);
 
 var planeGeometry = new THREE.PlaneGeometry(60,20);
 
 var PlaneMaterial = new THREE.MeshBasicMaterial({color:0xCCCCCC});
 
 var plane = new THREE.Mesh(planeGeometry,PlaneMaterial);
 
 plane.rotation.x = -0.5*Math.PI;
 plane.position.x = 15;
 plane.position.y = 0;
 plane.position.z = 0;
 
 scene.add(plane);
 
 camera.position.x = -30;
 camera.position.y = 40;
 camera.position.z = 30;
 camera.lookAt(scene.position)
 
 document.getElementById('webGl-output').appendChild(renderer.domElement);
 
 renderer.render(scene,camera);
 }
 window.onload = init
 
 | 
完整实例代码:
| <!doctype html><html lang="zh-CN">
 <head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="IE=Edge">
 <title>Demo-01</title>
 <script src="libs/Three.js"></script>
 <style>
 body{
 margin: 0;
 overflow: hidden;
 }
 </style>
 </head>
 
 <body>
 
 <div id="webGl-output">
 
 </div>
 
 
 <script>
 function init(){
 
 var scene = new THREE.Scene();
 
 var camera = new THREE.PerspectiveCamera(45,window.innerWidth/window.innerHeight,0.1,2000);
 
 var renderer = new THREE.WebGLRenderer();
 
 renderer.setClearColor(new THREE.Color(0xEEEEEE));
 
 renderer.setSize(window.innerWidth,window.innerHeight);
 
 var axes = new THREE.AxisHelper(20);
 
 scene.add(axes);
 
 var planeGeometry = new THREE.PlaneGeometry(60,20);
 
 var PlaneMaterial = new THREE.MeshBasicMaterial({color:0xCCCCCC});
 
 var plane = new THREE.Mesh(planeGeometry,PlaneMaterial);
 
 plane.rotation.x = -0.5*Math.PI;
 plane.position.x = 15;
 plane.position.y = 0;
 plane.position.z = 0;
 
 scene.add(plane);
 
 camera.position.x = -30;
 camera.position.y = 40;
 camera.position.z = 30;
 camera.lookAt(scene.position)
 
 document.getElementById('webGl-output').appendChild(renderer.domElement);
 
 renderer.render(scene,camera);
 }
 window.onload = init
 </script>
 </body>
 </html>
 
 | 
版權聲明: 此文章版權歸Arvin所有,如有轉載,請註明來自原作者