@multiplekex/shallot 0.2.5 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/package.json +1 -1
  2. package/src/core/component.ts +1 -1
  3. package/src/core/index.ts +1 -13
  4. package/src/core/math.ts +186 -0
  5. package/src/core/state.ts +1 -1
  6. package/src/core/xml.ts +56 -41
  7. package/src/extras/orbit/index.ts +1 -1
  8. package/src/extras/text/index.ts +10 -65
  9. package/src/extras/{water.ts → water/index.ts} +59 -4
  10. package/src/standard/raster/batch.ts +149 -0
  11. package/src/standard/raster/forward.ts +832 -0
  12. package/src/standard/raster/index.ts +146 -472
  13. package/src/standard/raster/shadow.ts +408 -0
  14. package/src/standard/raytracing/bvh/blas.ts +335 -87
  15. package/src/standard/raytracing/bvh/radix.ts +225 -228
  16. package/src/standard/raytracing/bvh/refit.ts +711 -0
  17. package/src/standard/raytracing/bvh/structs.ts +0 -55
  18. package/src/standard/raytracing/bvh/tlas.ts +153 -141
  19. package/src/standard/raytracing/bvh/traverse.ts +72 -64
  20. package/src/standard/raytracing/index.ts +233 -204
  21. package/src/standard/raytracing/instance.ts +30 -18
  22. package/src/standard/raytracing/ray.ts +1 -1
  23. package/src/standard/raytracing/shaders.ts +23 -40
  24. package/src/standard/render/camera.ts +10 -28
  25. package/src/standard/render/data.ts +1 -1
  26. package/src/standard/render/index.ts +68 -12
  27. package/src/standard/render/light.ts +2 -2
  28. package/src/standard/render/mesh.ts +404 -0
  29. package/src/standard/render/overlay.ts +5 -2
  30. package/src/standard/render/postprocess.ts +263 -267
  31. package/src/standard/render/surface/index.ts +81 -12
  32. package/src/standard/render/surface/shaders.ts +265 -11
  33. package/src/standard/render/surface/structs.ts +10 -0
  34. package/src/standard/tween/tween.ts +44 -115
  35. package/src/standard/render/mesh/box.ts +0 -20
  36. package/src/standard/render/mesh/index.ts +0 -315
  37. package/src/standard/render/mesh/plane.ts +0 -11
  38. package/src/standard/render/mesh/sphere.ts +0 -40
  39. package/src/standard/render/mesh/unified.ts +0 -96
  40. package/src/standard/render/surface/compile.ts +0 -65
  41. package/src/standard/render/surface/noise.ts +0 -58
@@ -0,0 +1,149 @@
1
+ import { MAX_ENTITIES } from "../../core";
2
+ import { MAX_SURFACES, MAX_BATCH_SLOTS, Mesh, getMesh, createMeshBuffers } from "../render/mesh";
3
+ import type { MeshBuffers } from "../render/mesh";
4
+
5
+ const INVALID_SHAPE = 0xffffffff;
6
+ const SLOT_STRIDE = 4;
7
+
8
+ export interface Batch {
9
+ entityIds: GPUBuffer;
10
+ buffers: Map<number, MeshBuffers>;
11
+ opaque: Uint32Array;
12
+ transparent: Uint32Array;
13
+ }
14
+
15
+ export function createBatch(device: GPUDevice): Batch {
16
+ return {
17
+ entityIds: device.createBuffer({
18
+ label: "raster-entity-ids",
19
+ size: MAX_ENTITIES * 4,
20
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
21
+ }),
22
+ buffers: new Map(),
23
+ opaque: new Uint32Array(MAX_BATCH_SLOTS * SLOT_STRIDE),
24
+ transparent: new Uint32Array(MAX_BATCH_SLOTS * SLOT_STRIDE),
25
+ };
26
+ }
27
+
28
+ export function slotInstanceCount(slots: Uint32Array, i: number): number {
29
+ return slots[i * SLOT_STRIDE + 1];
30
+ }
31
+
32
+ export function slotIndexCount(slots: Uint32Array, i: number): number {
33
+ return slots[i * SLOT_STRIDE];
34
+ }
35
+
36
+ export function slotFirstInstance(slots: Uint32Array, i: number): number {
37
+ return slots[i * SLOT_STRIDE + 2];
38
+ }
39
+
40
+ export function slotShapeId(slots: Uint32Array, i: number): number {
41
+ return slots[i * SLOT_STRIDE + 3];
42
+ }
43
+
44
+ const entityIdsData = new Uint32Array(MAX_ENTITIES);
45
+ const opaqueCounts = new Uint32Array(MAX_BATCH_SLOTS);
46
+ const transparentCounts = new Uint32Array(MAX_BATCH_SLOTS);
47
+ const opaqueOffsets = new Uint32Array(MAX_BATCH_SLOTS);
48
+ const transparentOffsets = new Uint32Array(MAX_BATCH_SLOTS);
49
+ const opaqueWriteOffsets = new Uint32Array(MAX_BATCH_SLOTS);
50
+ const transparentWriteOffsets = new Uint32Array(MAX_BATCH_SLOTS);
51
+
52
+ const opaqueEids = new Uint32Array(MAX_ENTITIES);
53
+ const opaqueBatchIds = new Uint32Array(MAX_ENTITIES);
54
+ const transparentEids = new Uint32Array(MAX_ENTITIES);
55
+ const transparentBatchIds = new Uint32Array(MAX_ENTITIES);
56
+
57
+ export function uploadBatch(
58
+ device: GPUDevice,
59
+ entities: Iterable<number>,
60
+ getSurface: (eid: number) => number,
61
+ getOpacity: (eid: number) => number,
62
+ state: Batch
63
+ ): void {
64
+ opaqueCounts.fill(0);
65
+ transparentCounts.fill(0);
66
+ let opaqueLen = 0;
67
+ let transparentLen = 0;
68
+
69
+ for (const eid of entities) {
70
+ const shape = Mesh.shape[eid];
71
+ if (shape === INVALID_SHAPE) continue;
72
+
73
+ const surface = getSurface(eid);
74
+ const batchIndex = shape * MAX_SURFACES + surface;
75
+ if (batchIndex >= MAX_BATCH_SLOTS) continue;
76
+
77
+ if (getOpacity(eid) < 1.0) {
78
+ transparentEids[transparentLen] = eid;
79
+ transparentBatchIds[transparentLen] = batchIndex;
80
+ transparentLen++;
81
+ transparentCounts[batchIndex]++;
82
+ } else {
83
+ opaqueEids[opaqueLen] = eid;
84
+ opaqueBatchIds[opaqueLen] = batchIndex;
85
+ opaqueLen++;
86
+ opaqueCounts[batchIndex]++;
87
+ }
88
+ }
89
+
90
+ let offset = 0;
91
+ for (let i = 0; i < MAX_BATCH_SLOTS; i++) {
92
+ opaqueOffsets[i] = offset;
93
+ offset += opaqueCounts[i];
94
+ }
95
+ for (let i = 0; i < MAX_BATCH_SLOTS; i++) {
96
+ transparentOffsets[i] = offset;
97
+ offset += transparentCounts[i];
98
+ }
99
+
100
+ opaqueWriteOffsets.fill(0);
101
+ for (let i = 0; i < opaqueLen; i++) {
102
+ const batchIndex = opaqueBatchIds[i];
103
+ const idx = opaqueOffsets[batchIndex] + opaqueWriteOffsets[batchIndex];
104
+ entityIdsData[idx] = opaqueEids[i];
105
+ opaqueWriteOffsets[batchIndex]++;
106
+ }
107
+
108
+ transparentWriteOffsets.fill(0);
109
+ for (let i = 0; i < transparentLen; i++) {
110
+ const batchIndex = transparentBatchIds[i];
111
+ const idx = transparentOffsets[batchIndex] + transparentWriteOffsets[batchIndex];
112
+ entityIdsData[idx] = transparentEids[i];
113
+ transparentWriteOffsets[batchIndex]++;
114
+ }
115
+
116
+ const totalEntities = offset;
117
+ if (totalEntities > 0) {
118
+ device.queue.writeBuffer(state.entityIds, 0, entityIdsData, 0, totalEntities);
119
+ }
120
+
121
+ for (let batchIndex = 0; batchIndex < MAX_BATCH_SLOTS; batchIndex++) {
122
+ const shapeId = Math.floor(batchIndex / MAX_SURFACES);
123
+ const o = batchIndex * SLOT_STRIDE;
124
+
125
+ const opaqueCount = opaqueCounts[batchIndex];
126
+ const transparentCount = transparentCounts[batchIndex];
127
+
128
+ let indexCount = 0;
129
+ if (opaqueCount > 0 || transparentCount > 0) {
130
+ const mesh = getMesh(shapeId);
131
+ if (mesh) {
132
+ indexCount = mesh.indexCount;
133
+ if (!state.buffers.has(shapeId)) {
134
+ state.buffers.set(shapeId, createMeshBuffers(device, mesh));
135
+ }
136
+ }
137
+ }
138
+
139
+ state.opaque[o] = opaqueCount > 0 ? indexCount : 0;
140
+ state.opaque[o + 1] = opaqueCount;
141
+ state.opaque[o + 2] = opaqueOffsets[batchIndex];
142
+ state.opaque[o + 3] = shapeId;
143
+
144
+ state.transparent[o] = transparentCount > 0 ? indexCount : 0;
145
+ state.transparent[o + 1] = transparentCount;
146
+ state.transparent[o + 2] = transparentOffsets[batchIndex];
147
+ state.transparent[o + 3] = shapeId;
148
+ }
149
+ }