@@ -7,12 +7,6 @@ class PartPreview {
7
7
this . canvas = canvas
8
8
this . scene = new THREE . Scene ( )
9
9
this . renderer = new THREE . WebGLRenderer ( { canvas : this . canvas } )
10
- this . objects = new THREE . Group ( )
11
- this . scene . add ( this . objects )
12
- this . material = new THREE . MeshNormalMaterial ( {
13
- flatShading : true
14
- } )
15
- this . geometry = null
16
10
this . camera = new THREE . PerspectiveCamera ( 45 , this . canvas . width / this . canvas . height , 0.1 , 1000 )
17
11
this . camera . position . z = 50
18
12
// Trigger loading when canvas becomes visible
@@ -44,21 +38,30 @@ class PartPreview {
44
38
}
45
39
46
40
onLoad ( model ) {
41
+ const material = new THREE . MeshNormalMaterial ( {
42
+ flatShading : true
43
+ } )
44
+ let object = null
47
45
if ( model . type === 'BufferGeometry' ) {
48
- this . geometry = model
46
+ object = new THREE . Mesh ( model , material )
49
47
} else {
50
- this . geometry = model . geometry || model . children [ 0 ] . geometry
48
+ model . traverse ( function ( node ) {
49
+ if ( node instanceof THREE . Mesh ) {
50
+ node . material = material
51
+ }
52
+ } )
53
+ object = model
51
54
}
52
- // Create mesh and transform to screen coords from print
55
+ // Transform to screen coords from print
53
56
const coordSystemTransform = new THREE . Matrix4 ( )
54
57
coordSystemTransform . set (
55
58
1 , 0 , 0 , 0 , // x -> x
56
59
0 , 0 , 1 , 0 , // z -> y
57
60
0 , - 1 , 0 , 0 , // y -> -z
58
61
0 , 0 , 0 , 1 )
59
- const mesh = new THREE . Mesh ( this . geometry . applyMatrix4 ( coordSystemTransform ) , this . material )
62
+ object . applyMatrix4 ( coordSystemTransform )
60
63
// Calculate bounding volumes
61
- const bbox = new THREE . Box3 ( ) . setFromObject ( mesh )
64
+ const bbox = new THREE . Box3 ( ) . setFromObject ( object )
62
65
const centre = new THREE . Vector3 ( )
63
66
bbox . getCenter ( centre )
64
67
const bsphere = new THREE . Sphere ( )
@@ -69,11 +72,11 @@ class PartPreview {
69
72
this . camera . position . y = bsphere . radius * 0.75
70
73
this . camera . lookAt ( 0 , modelheight / 2 , 0 )
71
74
// Centre the model
72
- mesh . position . set ( - centre . x , - bbox . min . y , - centre . z )
73
- this . objects . add ( mesh )
75
+ object . position . set ( - centre . x , - bbox . min . y , - centre . z )
76
+ this . scene . add ( object )
74
77
// Add the grid
75
78
this . gridHelper = new THREE . GridHelper ( 260 , 26 , 'magenta' , 'cyan' )
76
- this . objects . add ( this . gridHelper )
79
+ this . scene . add ( this . gridHelper )
77
80
}
78
81
79
82
onLoadError ( error ) {
@@ -82,7 +85,7 @@ class PartPreview {
82
85
83
86
animate ( ) {
84
87
if ( this . canvas . closest ( 'html' ) ) { // There's probably more efficient way to do this than checking every frame, but I can't make MutationObserver work right now
85
- this . objects . rotation . y += 0.01
88
+ this . scene . rotation . y += 0.01
86
89
this . renderer . render ( this . scene , this . camera )
87
90
window . requestAnimationFrame ( this . animate . bind ( this ) )
88
91
} else {
@@ -91,9 +94,12 @@ class PartPreview {
91
94
}
92
95
93
96
cleanup ( ) {
94
- if ( this . geometry ) this . geometry . dispose ( )
95
- if ( this . gridHelper ) this . gridHelper . geometry . dispose ( )
96
- this . material . dispose ( )
97
+ this . scene . traverse ( function ( node ) {
98
+ if ( node instanceof THREE . Mesh ) {
99
+ node . geometry . dispose ( )
100
+ node . material . dispose ( )
101
+ }
102
+ } )
97
103
this . renderer . dispose ( )
98
104
}
99
105
}
0 commit comments