|

楼主 |
发表于 2016-5-12 12:03:47
|
显示全部楼层
给点核心程序吧。这也是我老师整的。好东西就要分享,6 j& R! O9 |. y1 Z2 i2 B
var loader = new THREE.STLLoader();
$ P; m, @/ D: X, t9 _ var group = new THREE.Object3D();2 f$ v `6 Z& S' {
loader.load("../assets/models/a.stl", function (geometry) {7 v3 `5 a& c6 k, J, v$ g4 @
console.log(geometry);
1 n( k5 q, G3 G8 S1 W. y! S6 R. _, z B; w b
//加载到 mesh, Y% H6 N. I1 v
var mat = new THREE.MeshLambertMaterial({color: 0x444444});
/ h- m8 d( q b! O/ L3 R mesh = new THREE.Mesh(geometry, mat);
c( j5 }. e3 m mesh .rotation.x = -0.5 * Math.PI;: \+ \/ Y! ]4 b% u) C( S( {
mesh .scale.set(0.6, 0.6, 0.6);- o4 s( q9 V3 U4 `
7 q& c. s/ q4 h3 r+ M6 e //使用 box 生成边界,将物体调整到原点
5 S7 F) `* A6 { var box = new THREE.Box3().setFromObject( mesh );
7 a* ^* J3 f0 ^2 _$ l% I box.center( mesh.position ); // this re-sets the mesh position/ i( @& S" c1 L5 F$ I- k
mesh.position.multiplyScalar( - 1 );; Z3 s! `, E% }6 {2 z% _
//将mesh 加到组中0 N( c/ M6 H. t, T$ n$ f( W3 b3 R- Z
group = new THREE.Group();
! {, H8 q3 O6 b2 b1 Z# N$ k( P' l scene.add( group );
! l# p, G9 o4 H6 I9 D6 L* ^% ~1 s group.add( mesh );
8 L- }7 K' V# C# p* d! n( z/ g: u' {% v- _# B9 \+ N! V) T$ L6 ?
//设置组的坐标为mesh显示到原点时mesh的中心点坐标取负值,移到加载时的位置+ V* r* v5 n w6 q
group.position.y = -mesh.position.y;/ I: d6 m- f6 P' d* U
group.position.x = -mesh.position.x;
. ]% r. Y- h" _% B group.position.z = -mesh.position.z;- S& N y# \! j: Y3 q |
& a+ p9 k- v: Y" E4 I
}); |
|