-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathGridMesh.ts
55 lines (49 loc) · 1.44 KB
/
GridMesh.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
import {
Buffer,
BufferBindFlag,
BufferUsage,
ContentRestorer,
Engine,
MeshTopology,
ModelMesh,
VertexElement,
VertexElementFormat
} from "@galacean/engine";
export class GridMesh {
static createGridPlane(engine: Engine): ModelMesh {
const mesh = new ModelMesh(engine);
GridMesh._updateGridData(mesh);
engine.resourceManager.addContentRestorer(new GridMeshRestorer(mesh));
return mesh;
}
static _updateGridData(mesh: ModelMesh) {
// No-FlipY: POSITION_FLIP.xy, FlipY: POSITION_FLIP.zw
// prettier-ignore
const vertices = new Float32Array([
-1, -1, 1, -1, // left-bottom
1, -1, -1, -1, // right-bottom
-1, 1, 1, 1, // left-top
1, 1, -1, 1]); // right-top
mesh.setVertexElements([new VertexElement("POSITION_FLIP", 0, VertexElementFormat.Vector4, 0)]);
mesh.setVertexBufferBinding(new Buffer(mesh.engine, BufferBindFlag.VertexBuffer, vertices, BufferUsage.Static), 16);
mesh.addSubMesh(0, 4, MeshTopology.TriangleStrip);
const { bounds } = mesh;
bounds.min.set(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
bounds.max.set(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
return mesh;
}
}
/**
* @internal
*/
export class GridMeshRestorer extends ContentRestorer<ModelMesh> {
constructor(resource: ModelMesh) {
super(resource);
}
/**
* @override
*/
restoreContent(): void {
GridMesh._updateGridData(this.resource);
}
}