由於項目需要展示3d模型,所以對three做了點研究,分享出來 希望能幫到大家
先看看效果:

three.js整體來說 不是很難 只要你靜下心來研究研究 很快就會上手的
首先我們在頁面上需要創建一個能夠放置3D模型的畫布 也可以說是初始化 Three
1 var WIDTH,HEIGHT; 2 var renderer; 3 function initThree() { 4 WIDTH = document.documentElement.clientWidth/2; <!--{foreach from=$recommended_goods item=rgoods}--> <!-- {/foreach} --> 5 HEIGHT = document.documentElement.clientHeight/2; 6 /* 渲染器 */ 7 renderer = new THREE.WebGLRenderer(); 8 renderer.setSize(WIDTH , HEIGHT); 9 renderer.setClearColor(new THREE.Color(0x66666)); 10 renderer.gammaInput = true; 11 renderer.gammaOutput = true; 12 13 document.body.appendChild(renderer.domElement); 14 }
通過上面的代碼不難看出 我們設置了 在body里追加了一塊畫布 寬高是 client的一半顏色為 0x66666 這里要注意的是 renderer = new THREE.WebGLRenderer(); 因為我們所有的設置都是以renderer為對象設置
下來 我們需要調整攝像頭 即視覺角度
1 /* 攝像頭 */ 2 var camera; 3 function initCamera() { 4 var VIEW_ANGLE = 45, 5 ASPECT = WIDTH / HEIGHT, 6 NEAR = 0.1, 7 FAR = 10000; 8 camera = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT, NEAR, FAR); 9 camera.position.set(20, 0, 0); 10 //設置視野的中心坐標 11 camera.lookAt(scene.position); 12 }
以上代碼主要是控制視覺角度 數值可以在后期根據自己的需求去調整
加載場景:
1 /* 場景 */ 2 var scene; 3 function initScene() { 4 scene = new THREE.Scene(); 5 }
加載燈光效果
1 /* 燈光 */ 2 var light,light2,light3; 3 function initLight() { 4 //平行光 5 light = new THREE.DirectionalLight(0xFFFFFF); 6 light.position.set(0, 99, 0).normalize(); 7 scene.add(light); 8 //環境光 9 light2 = new THREE.AmbientLight(0x999999); 10 scene.add(light2); 11 //點光源 12 light3 = new THREE.PointLight(0x00FF00); 13 light3.position.set(300, 0, 0); 14 scene.add(light3); 15 }
顯示模型對象:
1 /* 顯示對象 */ 2 var cube; 3 function initObject(){ 4 // ASCII file 5 var loader = new THREE.STLLoader(); 6 7 loader.addEventListener( 'load', function ( event ) { 8 var loading = document.getElementById("Loading"); 9 loading.parentNode.removeChild(loading); 10 var geometry = event.content; 11 //磚紅色 12 var material = new THREE.MeshPhongMaterial( { ambient: 0xff5533, color: 0xff5533, specular: 0x111111, shininess: 200 } ); 13 //純黑色 14 // var material = new THREE.MeshBasicMaterial( { envMap: THREE.ImageUtils.loadTexture( 'http://localhost:8080/textures/metal.jpg', new THREE.SphericalReflectionMapping() ), overdraw: true } ) ; 15 //粉色 帶陰影 16 // var material = new THREE.MeshLambertMaterial( { color:0xff5533, side: THREE.DoubleSide } ); 17 //灰色 18 // var material = new THREE.MeshLambertMaterial({color: 000000}); //材質設定 (顏色) 19 20 21 var mesh = new THREE.Mesh( geometry, material ); 22 23 var center = THREE.GeometryUtils.center(geometry); 24 var boundbox=geometry.boundingBox; 25 var vector3 = boundbox.size(null); 26 var vector3 = boundbox.size(null); 27 console.log(vector3); 28 var scale = vector3.length(); 29 camera.position.set(scale, 0, 0); 30 camera.lookAt(scene.position); 31 scene.add(camera); 32 33 34 //利用一個軸對象以可視化的3軸以簡單的方式。X軸是紅色的。Y軸是綠色的。Z軸是藍色的。這有助於理解在空間的所有三個軸的方向。 35 var axisHelper = new THREE.AxisHelper(800); 36 scene.add(axisHelper); 37 38 //周圍邊框 39 bboxHelper = new THREE.BoxHelper(); 40 bboxHelper.visible = true; 41 var meshMaterial = material; 42 mainModel = new THREE.Mesh(geometry, meshMaterial); 43 bboxHelper.update(mainModel); 44 bboxHelper.geometry.computeBoundingBox(); 45 scene.add(bboxHelper); 46 47 //地板網格 48 // var gridHelper = new THREE.GridHelper(500, 40); // 500 is grid size, 20 is grid step 49 // gridHelper.position = new THREE.Vector3(0, 0, 0); 50 // gridHelper.rotation = new THREE.Euler(0, 0, 0); 51 // scene.add(gridHelper); 52 // var gridHelper2 = gridHelper.clone(); 53 // gridHelper2.rotation = new THREE.Euler(Math.PI / 2, 0, 0); 54 // scene.add(gridHelper2); 55 // var gridHelper3 = gridHelper.clone(); 56 // gridHelper3.rotation = new THREE.Euler(Math.PI / 2, 0, Math.PI / 2); 57 // scene.add(gridHelper3); 58 // 59 // var grid = new THREE.GridHelper(300, 40, 25, [0, 0, 1], 0x000055, 0.2, true, "#FFFFFF", "left"); 60 // scene.add(grid); 61 62 63 var x = (boundbox.max.x - boundbox.min.x).toFixed(2); 64 var y = (boundbox.max.y - boundbox.min.y).toFixed(2); 65 var z = (boundbox.max.z - boundbox.min.z).toFixed(2); 66 67 console.log(x); 68 console.log(y); 69 console.log(z); 70 console.log(boundbox); 71 72 mesh.position.set(0,0,0); 73 // mesh.position.x = scene.position.x; 74 // mesh.position.y = scene.position.y ; 75 // mesh.position.z = scene.position.z; 76 scene.add(mesh); 77 78 79 renderer.clear(); 80 renderer.render(scene, camera); 81 } ); 82 loader.load( '3dfile/莫比烏斯環.STL' ); 83 }
這里根據文件類型選擇相對應的js引入即可 我加載的是STL模型 所以我引入的是 STLLoader.js
1 <script src="js/STLLoader.js"></script>
如果需要顯示網格標尺 將 網格部分代碼 去掉注釋即可
下來是控制方法 (雖然我沒有在顯示代碼里面寫根據鍵盤按鍵放大縮小 但還是提供給大家 參考)
1 //控制 2 var effect; 3 var controls; 4 function initControl(){ 5 effect = new THREE.AsciiEffect( renderer ); 6 effect.setSize( WIDTH, HEIGHT ); 7 controls = new THREE.TrackballControls( camera,renderer.domElement); 8 }
最后就是一個初始調用了
1 function animate() { 2 requestAnimationFrame( animate ); 3 controls.update(); 4 effect.render( scene, camera ); 5 } 6 7 function threeStart() { 8 initThree(); 9 initScene(); 10 initCamera(); 11 initLight(); 12 initObject(); 13 initControl(); 14 animate(); 15 }
附上完整代碼
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8" /> 5 <title>WebGL</title> 6 <script type="text/javascript" charset="utf-8" src="js/three.js"></script> 7 <script src="js/STLLoader.js"></script> 8 <script src="js/TrackballControls.js"></script> 9 <script src="js/AsciiEffect.js"></script> 10 <style>body{overflow:hidden;background:#eee}</style> 11 </head> 12 <script> 13 var WIDTH,HEIGHT; 14 var renderer; 15 function initThree() { 16 WIDTH = document.documentElement.clientWidth/2; <!--{foreach from=$recommended_goods item=rgoods}--> <!-- {/foreach} --> 17 HEIGHT = document.documentElement.clientHeight/2; 18 /* 渲染器 */ 19 renderer = new THREE.WebGLRenderer(); 20 renderer.setSize(WIDTH , HEIGHT); 21 renderer.setClearColor(new THREE.Color(0x66666)); 22 renderer.gammaInput = true; 23 renderer.gammaOutput = true; 24 25 document.body.appendChild(renderer.domElement); 26 } 27 28 /* 攝像頭 */ 29 var camera; 30 function initCamera() { 31 var VIEW_ANGLE = 45, 32 ASPECT = WIDTH / HEIGHT, 33 NEAR = 0.1, 34 FAR = 10000; 35 camera = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT, NEAR, FAR); 36 camera.position.set(20, 0, 0); 37 //設置視野的中心坐標 38 camera.lookAt(scene.position); 39 } 40 41 /* 場景 */ 42 var scene; 43 function initScene() { 44 scene = new THREE.Scene(); 45 } 46 47 /* 燈光 */ 48 var light,light2,light3; 49 function initLight() { 50 //平行光 51 light = new THREE.DirectionalLight(0xFFFFFF); 52 light.position.set(0, 99, 0).normalize(); 53 scene.add(light); 54 //環境光 55 light2 = new THREE.AmbientLight(0x999999); 56 scene.add(light2); 57 //點光源 58 light3 = new THREE.PointLight(0x00FF00); 59 light3.position.set(300, 0, 0); 60 scene.add(light3); 61 } 62 63 /* 顯示對象 */ 64 var cube; 65 function initObject(){ 66 // ASCII file 67 var loader = new THREE.STLLoader(); 68 69 loader.addEventListener( 'load', function ( event ) { 70 var loading = document.getElementById("Loading"); 71 loading.parentNode.removeChild(loading); 72 var geometry = event.content; 73 //磚紅色 74 var material = new THREE.MeshPhongMaterial( { ambient: 0xff5533, color: 0xff5533, specular: 0x111111, shininess: 200 } ); 75 //純黑色 76 // var material = new THREE.MeshBasicMaterial( { envMap: THREE.ImageUtils.loadTexture( 'http://localhost:8080/textures/metal.jpg', new THREE.SphericalReflectionMapping() ), overdraw: true } ) ; 77 //粉色 帶陰影 78 // var material = new THREE.MeshLambertMaterial( { color:0xff5533, side: THREE.DoubleSide } ); 79 //灰色 80 // var material = new THREE.MeshLambertMaterial({color: 000000}); //材質設定 (顏色) 81 82 83 var mesh = new THREE.Mesh( geometry, material ); 84 85 var center = THREE.GeometryUtils.center(geometry); 86 var boundbox=geometry.boundingBox; 87 var vector3 = boundbox.size(null); 88 var vector3 = boundbox.size(null); 89 console.log(vector3); 90 var scale = vector3.length(); 91 camera.position.set(scale, 0, 0); 92 camera.lookAt(scene.position); 93 scene.add(camera); 94 95 96 //利用一個軸對象以可視化的3軸以簡單的方式。X軸是紅色的。Y軸是綠色的。Z軸是藍色的。這有助於理解在空間的所有三個軸的方向。 97 var axisHelper = new THREE.AxisHelper(800); 98 scene.add(axisHelper); 99 100 //周圍邊框 101 bboxHelper = new THREE.BoxHelper(); 102 bboxHelper.visible = true; 103 var meshMaterial = material; 104 mainModel = new THREE.Mesh(geometry, meshMaterial); 105 bboxHelper.update(mainModel); 106 bboxHelper.geometry.computeBoundingBox(); 107 scene.add(bboxHelper); 108 109 //地板網格 110 // var gridHelper = new THREE.GridHelper(500, 40); // 500 is grid size, 20 is grid step 111 // gridHelper.position = new THREE.Vector3(0, 0, 0); 112 // gridHelper.rotation = new THREE.Euler(0, 0, 0); 113 // scene.add(gridHelper); 114 // var gridHelper2 = gridHelper.clone(); 115 // gridHelper2.rotation = new THREE.Euler(Math.PI / 2, 0, 0); 116 // scene.add(gridHelper2); 117 // var gridHelper3 = gridHelper.clone(); 118 // gridHelper3.rotation = new THREE.Euler(Math.PI / 2, 0, Math.PI / 2); 119 // scene.add(gridHelper3); 120 // 121 // var grid = new THREE.GridHelper(300, 40, 25, [0, 0, 1], 0x000055, 0.2, true, "#FFFFFF", "left"); 122 // scene.add(grid); 123 124 125 var x = (boundbox.max.x - boundbox.min.x).toFixed(2); 126 var y = (boundbox.max.y - boundbox.min.y).toFixed(2); 127 var z = (boundbox.max.z - boundbox.min.z).toFixed(2); 128 129 console.log(x); 130 console.log(y); 131 console.log(z); 132 console.log(boundbox); 133 134 mesh.position.set(0,0,0); 135 // mesh.position.x = scene.position.x; 136 // mesh.position.y = scene.position.y ; 137 // mesh.position.z = scene.position.z; 138 scene.add(mesh); 139 140 141 renderer.clear(); 142 renderer.render(scene, camera); 143 } ); 144 loader.load( '3dfile/莫比烏斯環.STL' ); 145 } 146 147 //控制 148 var effect; 149 var controls; 150 function initControl(){ 151 effect = new THREE.AsciiEffect( renderer ); 152 effect.setSize( WIDTH, HEIGHT ); 153 controls = new THREE.TrackballControls( camera,renderer.domElement); 154 } 155 156 function animate() { 157 requestAnimationFrame( animate ); 158 controls.update(); 159 effect.render( scene, camera ); 160 } 161 162 function threeStart() { 163 initThree(); 164 initScene(); 165 initCamera(); 166 initLight(); 167 initObject(); 168 initControl(); 169 animate(); 170 } 171 </script> 172 <body onload="threeStart()"> 173 <div id="Loading" style="color:#fff">Loading...</div> 174 </body> 175 </html>
哦 我的文件結構

如果想要所有文件的小伙伴 給我留言即可
補充一點,由於在顯示模型的方法里我加入了 bboxHelper = new THREE.BoxHelper() 所以我們可以獲取到模型的 X Y Z三軸的尺寸 也可以當作 模型的長寬高

