|
3#

樓主 |
發(fā)表于 2016-5-12 12:03:47
|
只看該作者
給點(diǎn)核心程序吧。這也是我老師整的。好東西就要分享,
3 h& H; b' Z2 {, a, D5 Rvar loader = new THREE.STLLoader();& s. d5 B6 \ ~( I
var group = new THREE.Object3D();
0 I2 I, X/ f! |% E, k0 c loader.load("../assets/models/a.stl", function (geometry) {4 Z: a8 [' Y" }" q$ u4 V
console.log(geometry);
. \0 O9 m4 y) y. k C& E( F- E: _2 P* H1 u6 `: B& B7 Y
//加載到 mesh
+ b* M- S$ i- r/ l6 d. W) v6 `# l var mat = new THREE.MeshLambertMaterial({color: 0x444444});
* L* n6 z. Q- n- J+ S: j mesh = new THREE.Mesh(geometry, mat);% T, H; b6 A d. F S
mesh .rotation.x = -0.5 * Math.PI;* H- X3 }$ f' g# _. v% C
mesh .scale.set(0.6, 0.6, 0.6);
- }* Y7 m7 c( p F. K. ]' V
+ w; W( a3 _+ |# W9 u+ q //使用 box 生成邊界,將物體調(diào)整到原點(diǎn)* Q) Y, y, E3 ?/ p. z- Y1 z3 I, T
var box = new THREE.Box3().setFromObject( mesh );" s$ s+ ~/ N# G) w
box.center( mesh.position ); // this re-sets the mesh position
, Z6 m# u- p. S- n: g mesh.position.multiplyScalar( - 1 );
6 `$ T: l, t" h8 G C //將mesh 加到組中) d: o# }3 J9 s$ l* m. E/ b8 @
group = new THREE.Group();5 o* @7 A: n2 h5 u3 c, V
scene.add( group );; N' L+ G2 `! s8 t' b
group.add( mesh );
# [8 ^! l ~0 ^1 k: Q. E
7 y G1 ~( W k% a //設(shè)置組的坐標(biāo)為mesh顯示到原點(diǎn)時(shí)mesh的中心點(diǎn)坐標(biāo)取負(fù)值,移到加載時(shí)的位置
9 \% [) ]" M+ U5 b& v) n group.position.y = -mesh.position.y;+ N6 M0 Y9 ]& I/ M
group.position.x = -mesh.position.x;" o7 [* w, }! s! g
group.position.z = -mesh.position.z;" R' f9 r% }0 O6 }" Y
6 E7 C. y9 |- E, S }); |
|