-
Notifications
You must be signed in to change notification settings - Fork 46
/
mesh.ts
212 lines (189 loc) · 6.53 KB
/
mesh.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { Renderer, ObjectRenderer } from "@pixi/core"
import { PlaneGeometry } from "./geometry/plane-geometry"
import { CubeGeometry } from "./geometry/cube-geometry"
import { MeshGeometry3D } from "./geometry/mesh-geometry"
import { Container3D } from "../container"
import { QuadGeometry } from "./geometry/quad-geometry"
import { Skin } from "../skinning/skin"
import { InstancedMesh3D } from "./instanced-mesh"
import { Material } from "../material/material"
import { StandardMaterial } from "../material/standard/standard-material"
import { MeshDestroyOptions } from "./mesh-destroy-options"
import { Vec3 } from "../math/vec3"
import { AABB } from "../math/aabb"
import { CircleGeometry, CircleGeometryOptions } from "./geometry/circle-geometry"
import { CylinderGeometry, CylinderGeometryOptions } from "./geometry/cylinder-geometry"
import { SphereGeometry, SphereGeometryOptions } from "./geometry/sphere-geometry"
/**
* Represents a mesh which contains geometry and has a material.
*/
export class Mesh3D extends Container3D {
/** The name of the plugin used for rendering the mesh. */
pluginName = "pipeline"
/** Array of weights used for morphing between geometry targets. */
targetWeights?: number[]
/** The skin used for vertex skinning. */
skin?: Skin
/** The enabled render passes for this mesh. */
enabledRenderPasses: { [name: string]: unknown } = { "material": {} }
/** Used for sorting the mesh before render. */
renderSortOrder = 0
/**
* Creates a new mesh with the specified geometry and material.
* @param geometry The geometry for the mesh.
* @param material The material for the mesh. If the material is empty the mesh won't be rendered.
*/
constructor(public geometry: MeshGeometry3D, public material?: Material) {
super()
if (!geometry) {
throw new Error("PIXI3D: Geometry is required when creating a mesh.")
}
}
private _instances: InstancedMesh3D[] = []
/** An array of instances created from this mesh. */
get instances() {
return this._instances
}
/**
* Creates a new instance of this mesh.
*/
createInstance() {
if (this.material && !this.material.isInstancingSupported) {
throw new Error("PIXI3D: Can't create instance of mesh, material does not support instancing.")
}
return this._instances[
this._instances.push(new InstancedMesh3D(this, this.material?.createInstance())) - 1
]
}
/**
* Removes an instance from this mesh.
* @param instance The instance to remove.
*/
removeInstance(instance: InstancedMesh3D) {
const index = this._instances.indexOf(instance)
if (index >= 0) {
this._instances.splice(index, 1)
}
}
/**
* Enables the render pass with the specified name.
* @param name The name of the render pass to enable.
*/
enableRenderPass(name: string, options?: unknown) {
if (!this.enabledRenderPasses[name]) {
this.enabledRenderPasses[name] = options || {}
}
}
/**
* Disables the render pass with the specified name.
* @param name The name of the render pass to disable.
* @param options The options for the render pass.
*/
disableRenderPass(name: string) {
if (this.enabledRenderPasses[name]) {
delete this.enabledRenderPasses[name]
}
}
/**
* Returns a value indicating if the specified render pass is enabled.
* @param name The name of the render pass to check.
*/
isRenderPassEnabled(name: string) {
return !!this.enabledRenderPasses[name]
}
/**
* Destroys the mesh and it's used resources.
*/
destroy(options?: boolean | MeshDestroyOptions) {
if (options === true || (options && options.geometry)) {
this.geometry.destroy()
}
if (options === true || (options && options.material)) {
if (this.material) {
this.material.destroy()
}
}
super.destroy(options)
}
_render(renderer: Renderer) {
renderer.batch.setObjectRenderer(
<ObjectRenderer>(<any>renderer.plugins)[this.pluginName]
);
if (this.skin) {
this.skin.calculateJointMatrices()
}
<ObjectRenderer>(<any>renderer.plugins)[this.pluginName].render(this)
}
/**
* Calculates and returns a axis-aligned bounding box of the mesh in world space.
*/
getBoundingBox() {
if (!this.geometry.positions?.min) {
return undefined
}
if (!this.geometry.positions?.max) {
return undefined
}
let min = Vec3.transformMat4(
<any>this.geometry.positions.min, this.worldTransform.array)
let max = Vec3.transformMat4(
<any>this.geometry.positions.max, this.worldTransform.array)
for (let i = 0; i < 3; i++) {
let temp = min[i]
min[i] = Math.min(min[i], max[i])
max[i] = Math.max(temp, max[i])
}
return AABB.from({ min, max })
}
/**
* Creates a new quad (flat square) mesh with the specified material.
* @param material The material to use.
*/
static createQuad(material: Material = new StandardMaterial()) {
return new Mesh3D(QuadGeometry.create(), material)
}
/**
* Creates a new cube (six faces) mesh with the specified material.
* @param material The material to use.
*/
static createCube(material: Material = new StandardMaterial()) {
return new Mesh3D(CubeGeometry.create(), material)
}
/**
* Creates a new plane (flat square) mesh with the specified material.
* @param material The material to use.
*/
static createPlane(material: Material = new StandardMaterial()) {
return new Mesh3D(PlaneGeometry.create(), material)
}
/**
* Creates a new uv sphere mesh with the specified material.
* @param material The material to use.
* @param options The options used when creating the geometry.
*/
static createSphere(material: Material = new StandardMaterial(), options?: SphereGeometryOptions) {
return new Mesh3D(SphereGeometry.create(options), material)
}
/**
* Creates a new uv circle mesh with the specified material.
* @param material The material to use.
* @param options The options used when creating the geometry.
*/
static createCircle(
material: Material = new StandardMaterial(),
options: CircleGeometryOptions = {}
) {
return new Mesh3D(CircleGeometry.create(options), material)
}
/**
* Creates a new uv cylinder mesh with the specified material.
* @param material The material to use.
* @param options The options used when creating the geometry.
*/
static createCylinder(
material: Material = new StandardMaterial(),
options: CylinderGeometryOptions = {}
) {
return new Mesh3D(CylinderGeometry.create(options), material)
}
}