13
13
* limitations under the License.
14
14
*/
15
15
16
- import { Group , Intersection , Material as ThreeMaterial , Mesh , MeshStandardMaterial , Object3D , Raycaster } from 'three' ;
16
+ import { Intersection , Material as ThreeMaterial , Mesh , MeshStandardMaterial , Object3D } from 'three' ;
17
17
18
18
import { CorrelatedSceneGraph , GLTFElementToThreeObjectMap , ThreeObjectSet } from '../../three-components/gltf-instance/correlated-scene-graph.js' ;
19
19
import { GLTF , GLTFElement , Material as GLTFMaterial } from '../../three-components/gltf-instance/gltf-2.0.js' ;
20
20
21
21
import { Model as ModelInterface } from './api.js' ;
22
22
import { $setActive , $variantSet , Material } from './material.js' ;
23
- import { $children , Node , PrimitiveNode } from './nodes/primitive-node.js' ;
23
+ import { Node , PrimitiveNode } from './nodes/primitive-node.js' ;
24
24
import { $correlatedObjects , $sourceObject } from './three-dom-element.js' ;
25
25
26
26
@@ -33,9 +33,9 @@ export const $loadVariant = Symbol('loadVariant');
33
33
export const $correlatedSceneGraph = Symbol ( 'correlatedSceneGraph' ) ;
34
34
export const $prepareVariantsForExport = Symbol ( 'prepareVariantsForExport' ) ;
35
35
export const $switchVariant = Symbol ( 'switchVariant' ) ;
36
- export const $threeScene = Symbol ( 'threeScene' ) ;
37
- export const $materialsFromPoint = Symbol ( 'materialsFromPoint' ) ;
38
36
export const $materialFromPoint = Symbol ( 'materialFromPoint' ) ;
37
+ export const $nodeFromPoint = Symbol ( 'nodeFromPoint' ) ;
38
+ export const $nodeFromIndex = Symbol ( 'nodeFromIndex' ) ;
39
39
export const $variantData = Symbol ( 'variantData' ) ;
40
40
export const $availableVariants = Symbol ( 'availableVariants' ) ;
41
41
const $modelOnUpdate = Symbol ( 'modelOnUpdate' ) ;
@@ -78,7 +78,6 @@ export class Model implements ModelInterface {
78
78
private [ $hierarchy ] = new Array < Node > ( ) ;
79
79
private [ $roots ] = new Array < Node > ( ) ;
80
80
private [ $primitivesList ] = new Array < PrimitiveNode > ( ) ;
81
- private [ $threeScene ] : Object3D | Group ;
82
81
private [ $modelOnUpdate ] : ( ) => void = ( ) => { } ;
83
82
private [ $correlatedSceneGraph ] : CorrelatedSceneGraph ;
84
83
private [ $variantData ] = new Map < string , VariantData > ( ) ;
@@ -89,7 +88,6 @@ export class Model implements ModelInterface {
89
88
this [ $modelOnUpdate ] = onUpdate ;
90
89
this [ $correlatedSceneGraph ] = correlatedSceneGraph ;
91
90
const { gltf, threeGLTF, gltfElementMap} = correlatedSceneGraph ;
92
- this [ $threeScene ] = threeGLTF . scene ;
93
91
94
92
for ( const [ i , material ] of gltf . materials ! . entries ( ) ) {
95
93
const correlatedMaterial =
@@ -168,7 +166,7 @@ export class Model implements ModelInterface {
168
166
169
167
const parent : Node | undefined = parentMap . get ( object ) ;
170
168
if ( parent != null ) {
171
- parent [ $ children] . push ( node ) ;
169
+ parent . children . push ( node ) ;
172
170
} else {
173
171
this [ $roots ] . push ( node ) ;
174
172
}
@@ -213,46 +211,37 @@ export class Model implements ModelInterface {
213
211
return null ;
214
212
}
215
213
216
-
217
- /**
218
- * Intersects a ray with the Model and returns a list of materials whose
219
- * objects were intersected.
220
- */
221
- [ $materialsFromPoint ] ( raycaster : Raycaster ) : Material [ ] {
222
- const hits = raycaster . intersectObject ( this [ $threeScene ] , true ) ;
223
-
224
- // Map the object hits to primitives and then to the active material of
225
- // the primitive.
226
- return hits . map ( ( hit : Intersection < Object3D > ) => {
227
- const found = this [ $hierarchy ] . find ( ( node : Node ) => {
228
- if ( node instanceof PrimitiveNode ) {
229
- const primitive = node as PrimitiveNode ;
230
- if ( primitive . mesh === hit . object ) {
231
- return true ;
232
- }
214
+ [ $nodeFromIndex ] ( mesh : number , primitive : number ) : PrimitiveNode | null {
215
+ const found = this [ $hierarchy ] . find ( ( node : Node ) => {
216
+ if ( node instanceof PrimitiveNode ) {
217
+ const { meshes, primitives} = node . mesh . userData . associations ;
218
+ if ( meshes == mesh && primitives == primitive ) {
219
+ return true ;
233
220
}
234
- return false ;
235
- } ) as PrimitiveNode ;
221
+ }
222
+ return false ;
223
+ } ) ;
224
+ return found == null ? null : found as PrimitiveNode ;
225
+ }
236
226
237
- if ( found != null ) {
238
- return found . getActiveMaterial ( ) ;
227
+ [ $nodeFromPoint ] ( hit : Intersection < Object3D > ) : PrimitiveNode {
228
+ return this [ $hierarchy ] . find ( ( node : Node ) => {
229
+ if ( node instanceof PrimitiveNode ) {
230
+ const primitive = node as PrimitiveNode ;
231
+ if ( primitive . mesh === hit . object ) {
232
+ return true ;
233
+ }
239
234
}
240
- return null ;
241
- } ) as Material [ ] ;
235
+ return false ;
236
+ } ) as PrimitiveNode ;
242
237
}
243
238
244
239
/**
245
240
* Intersects a ray with the Model and returns the first material whose
246
241
* object was intersected.
247
242
*/
248
- [ $materialFromPoint ] ( raycaster : Raycaster ) : Material | null {
249
- const materials = this [ $materialsFromPoint ] ( raycaster ) ;
250
-
251
- if ( materials . length > 0 ) {
252
- return materials [ 0 ] ;
253
- }
254
-
255
- return null ;
243
+ [ $materialFromPoint ] ( hit : Intersection < Object3D > ) : Material {
244
+ return this [ $nodeFromPoint ] ( hit ) . getActiveMaterial ( ) ;
256
245
}
257
246
258
247
/**
0 commit comments