/
legacyMeshFactory.ts
84 lines (73 loc) · 2.3 KB
/
legacyMeshFactory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* @module vim-loader
*/
import { InsertableMesh } from './insertableMesh'
import { Scene } from '../../vim-loader/scene'
import { G3dMaterial, G3d, MeshSection } from 'vim-format'
import { InstancedMeshFactory } from './instancedMeshFactory'
import { G3dSubset } from './g3dSubset'
/**
* Mesh factory to load a standard vim using the progressive pipeline.
*/
export class VimMeshFactory {
readonly g3d: G3d
private _materials: G3dMaterial
private _instancedFactory: InstancedMeshFactory
private _scene: Scene
constructor (g3d: G3d, materials: G3dMaterial, scene: Scene) {
this.g3d = g3d
this._materials = materials
this._scene = scene
this._instancedFactory = new InstancedMeshFactory(materials)
}
/**
* Adds all instances from subset to the scene
*/
public add (subset: G3dSubset) {
const uniques = subset.filterUniqueMeshes()
const nonUniques = subset.filterNonUniqueMeshes()
// Create and add meshes to scene
this.addInstancedMeshes(this._scene, nonUniques)
this.addMergedMesh(this._scene, uniques)
}
private addMergedMesh (scene: Scene, subset: G3dSubset) {
const opaque = this.createMergedMesh(subset, 'opaque', false)
const transparents = this.createMergedMesh(subset, 'transparent', true)
scene.addMesh(opaque)
scene.addMesh(transparents)
}
private createMergedMesh (
subset: G3dSubset,
section: MeshSection,
transparent: boolean
) {
const offsets = subset.getOffsets(section)
const opaque = new InsertableMesh(offsets, this._materials, transparent)
const count = subset.getMeshCount()
for (let m = 0; m < count; m++) {
opaque.insertFromVim(this.g3d, m)
}
opaque.update()
return opaque
}
private addInstancedMeshes (scene: Scene, subset: G3dSubset) {
const count2 = subset.getMeshCount()
for (let m = 0; m < count2; m++) {
const mesh = subset.getSourceMesh(m)
const instances =
subset.getMeshInstances(m) ?? this.g3d.meshInstances[mesh]
const opaque = this._instancedFactory.createOpaqueFromVim(
this.g3d,
mesh,
instances
)
const transparent = this._instancedFactory.createTransparentFromVim(
this.g3d,
mesh,
instances
)
scene.addMesh(opaque)
scene.addMesh(transparent)
}
}
}