|
楼主 |
发表于 2016-5-12 12:03:47
|
显示全部楼层
给点核心程序吧。这也是我老师整的。好东西就要分享,
1 A9 p5 t; z; c1 M5 Zvar loader = new THREE.STLLoader();
6 ` C9 \9 l! u var group = new THREE.Object3D();
1 }# A8 s- }0 F+ B" H2 c x4 X loader.load("../assets/models/a.stl", function (geometry) {' r% w/ U0 M& o2 L3 c: R
console.log(geometry);
7 n! S4 I4 Q. I1 E+ E2 {( w! E& F" `* L- |0 z- h1 s
//加载到 mesh
1 \. ~+ P) V" ^5 q3 W var mat = new THREE.MeshLambertMaterial({color: 0x444444});
2 Y( y: x& a( \: [) _0 c& ` mesh = new THREE.Mesh(geometry, mat);
9 R, c6 G/ W& n mesh .rotation.x = -0.5 * Math.PI; S' Y5 h( y S4 f, S9 L
mesh .scale.set(0.6, 0.6, 0.6);
+ o. N9 q% ^% ]* ~4 O- U' h, f A$ n3 ?
//使用 box 生成边界,将物体调整到原点
q7 U- p' s. C! w: w% C Q var box = new THREE.Box3().setFromObject( mesh );+ u' P, w$ {9 _0 }3 U$ M5 e# o
box.center( mesh.position ); // this re-sets the mesh position# H1 [0 q1 r; @, p: z P
mesh.position.multiplyScalar( - 1 );& _" ?+ [' l1 J/ a
//将mesh 加到组中
$ F" l: R$ M) @+ C& q6 s' W group = new THREE.Group();$ e6 Z, L% ?4 p9 L
scene.add( group );4 b/ K z3 [* ~# e$ r# i7 l
group.add( mesh );9 D. s4 i3 a+ S+ B" W
7 h8 _4 U; h. u4 O8 D8 B
//设置组的坐标为mesh显示到原点时mesh的中心点坐标取负值,移到加载时的位置7 C# N8 ^' A, U7 Q; `2 D; q
group.position.y = -mesh.position.y;3 c% W. c& c) d) O$ N
group.position.x = -mesh.position.x;! z% p \& |3 D/ I0 Y
group.position.z = -mesh.position.z;) j; f R R8 h5 v/ f- b
: H. ~0 Q$ f; B2 `/ P$ a& g1 O: v }); |
|