forked from magcius/noclip.website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDkrSprites.ts
288 lines (245 loc) · 12.7 KB
/
DkrSprites.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import { mat4, vec3 } from 'gl-matrix';
import { Camera, computeViewMatrix } from '../Camera';
import { makeStaticDataBuffer } from '../gfx/helpers/BufferHelpers';
import { setAttachmentStateSimple } from '../gfx/helpers/GfxMegaStateDescriptorHelpers';
import { fillMatrix4x3, fillMatrix4x4 } from '../gfx/helpers/UniformBufferHelpers';
import { GfxBlendFactor, GfxBlendMode, GfxBuffer, GfxBufferUsage, GfxDevice, GfxFormat, GfxInputLayout, GfxInputLayoutBufferDescriptor, GfxInputState, GfxMipFilterMode, GfxProgram, GfxTexFilterMode,
GfxVertexAttributeDescriptor,
GfxVertexBufferFrequency,
GfxWrapMode, makeTextureDescriptor2D } from '../gfx/platform/GfxPlatform';
import { GfxRenderCache } from '../gfx/render/GfxRenderCache';
import { GfxRendererLayer, GfxRenderInst, GfxRenderInstManager, makeSortKey, setSortKeyDepth } from '../gfx/render/GfxRenderInstManager';
import { CalcBillboardFlags, calcBillboardMatrix } from '../MathHelpers';
import { TextureMapping } from '../TextureHolder';
import { assert } from '../util';
import { ViewerRenderInput } from '../viewer';
import { DataManager } from './DataManager';
import { DkrControlGlobals } from './DkrControlGlobals';
import { DkrObject, MODEL_TYPE_2D_BILLBOARD } from './DkrObject';
import { F3DDKR_Sprite_Program, MAX_NUM_OF_SPRITE_FRAMES, MAX_NUM_OF_SPRITE_INSTANCES } from './F3DDKR_Sprite_Program';
const viewMatrixScratch = mat4.create();
const viewMatrixCalcScratch = mat4.create();
const viewMatrixCalc2Scratch = mat4.create();
const mirrorMatrix = mat4.fromValues(
-1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
export const SPRITE_LAYER_SOLID = 0;
export const SPRITE_LAYER_TRANSPARENT = 1;
export class DkrSprites {
private spritesInfo: any[];
private spriteSheetWidth = 0;
private spriteSheetHeight = 0;
private spriteIndexOffsets = new Array<number>();
public spriteData: Float32Array;
private inputLayout: GfxInputLayout;
private inputState: GfxInputState;
private textureMappings: TextureMapping[];
private gfxProgram: GfxProgram | null = null;
private program: F3DDKR_Sprite_Program;
private currentFrame = 0;
private spriteInstances = [new Array<DkrObject>(), new Array<DkrObject>()];
private indexBuffer: GfxBuffer;
private vertexBuffer: GfxBuffer;
private hasBeenDestroyed = false;
constructor(device: GfxDevice, cache: GfxRenderCache, dataManager: DataManager) {
this.program = new F3DDKR_Sprite_Program();
dataManager.getSpriteSheet().then(([spritesInfo, spriteSheetImageData]) => {
this.spritesInfo = spritesInfo;
this.spriteSheetWidth = spriteSheetImageData.width;
this.spriteSheetHeight = spriteSheetImageData.height;
let totalNumberOfFrames = 0;
for(let i = 0; i < spritesInfo.length; i++) {
this.spriteIndexOffsets.push(totalNumberOfFrames);
for(let j = 0; j < spritesInfo[i].length; j++) {
totalNumberOfFrames++;
}
}
let currentFrame = 0;
this.spriteData = new Float32Array(totalNumberOfFrames * 4);
for(let i = 0; i < spritesInfo.length; i++) {
for(let j = 0; j < spritesInfo[i].length; j++) {
let currentSpriteInfo = spritesInfo[i][j];
this.spriteData[currentFrame * 4 + 0] = currentSpriteInfo.x / this.spriteSheetWidth;
this.spriteData[currentFrame * 4 + 1] = currentSpriteInfo.y / this.spriteSheetHeight;
this.spriteData[currentFrame * 4 + 2] = currentSpriteInfo.w / this.spriteSheetWidth;
this.spriteData[currentFrame * 4 + 3] = currentSpriteInfo.h / this.spriteSheetHeight;
currentFrame++;
}
}
// Create input state & layout
const halfsize = 500.0;
const vertices = new Float32Array([
-halfsize, halfsize,
halfsize, halfsize,
-halfsize, -halfsize,
halfsize, -halfsize,
]);
const indices = new Uint16Array([
// Real Indices
1, 0, 2,
1, 2, 3
]);
this.vertexBuffer = makeStaticDataBuffer(device, GfxBufferUsage.Vertex, vertices.buffer as ArrayBuffer);
this.indexBuffer = makeStaticDataBuffer(device, GfxBufferUsage.Index, indices.buffer as ArrayBuffer);
const vertexAttributeDescriptors: GfxVertexAttributeDescriptor[] = [
{ location: F3DDKR_Sprite_Program.a_Position, bufferIndex: 0, format: GfxFormat.F32_RG, bufferByteOffset: 0 * 0x04, }
];
const vertexBufferDescriptors: GfxInputLayoutBufferDescriptor[] = [
{ byteStride: 2 * 0x04, frequency: GfxVertexBufferFrequency.PerVertex, },
];
this.inputLayout = device.createInputLayout({
indexBufferFormat: GfxFormat.U16_R,
vertexAttributeDescriptors,
vertexBufferDescriptors,
});
this.inputState = device.createInputState(
this.inputLayout,
[{ buffer: this.vertexBuffer, byteOffset: 0 }],
{ buffer: this.indexBuffer, byteOffset: 0 }
);
// Setup sprite sheet texture
const sampler = cache.createSampler({
wrapS: GfxWrapMode.Clamp,
wrapT: GfxWrapMode.Clamp,
minFilter: GfxTexFilterMode.Point,
magFilter: GfxTexFilterMode.Point,
mipFilter: GfxMipFilterMode.Linear,
minLOD: 0, maxLOD: 0,
});
this.textureMappings = [new TextureMapping()];
const gfxTexture = device.createTexture(makeTextureDescriptor2D(GfxFormat.U8_RGBA_NORM,
spriteSheetImageData.width, spriteSheetImageData.height, 1));
device.uploadTextureData(gfxTexture, 0, [spriteSheetImageData.data]);
this.textureMappings[0].gfxSampler = sampler;
this.textureMappings[0].gfxTexture = gfxTexture;
});
}
public destroy(device: GfxDevice): void {
if(!this.hasBeenDestroyed) {
device.destroyBuffer(this.indexBuffer);
device.destroyBuffer(this.vertexBuffer);
device.destroyInputLayout(this.inputLayout);
device.destroyInputState(this.inputState);
device.destroyTexture(this.textureMappings[0].gfxTexture!);
// The sampler is already destroyed from renderHelper.destroy()
this.hasBeenDestroyed = true;
}
}
private bind(renderInst: GfxRenderInst): void {
renderInst.setSamplerBindingsFromTextureMappings(this.textureMappings);
}
public addInstances(instances: Array<DkrObject>): void {
for(let i = 0; i < instances.length; i++) {
if(instances[i].getModelType() != MODEL_TYPE_2D_BILLBOARD) {
continue;
}
const layer = instances[i].getSpriteLayer();
this.spriteInstances[layer].push(instances[i]);
}
}
private lastCameraPosition = vec3.create();
private hasCameraMoved(camera: Camera): boolean {
return !(
camera.worldMatrix[12] === this.lastCameraPosition[0] &&
camera.worldMatrix[13] === this.lastCameraPosition[1] &&
camera.worldMatrix[14] === this.lastCameraPosition[2]
);
}
private checkCameraDistanceToObjects(camera: Camera, layer: number): void {
if(this.hasCameraMoved(camera)) {
this.lastCameraPosition[0] = camera.worldMatrix[12];
this.lastCameraPosition[1] = camera.worldMatrix[13];
this.lastCameraPosition[2] = camera.worldMatrix[14];
for(let instance of this.spriteInstances[layer]) {
instance.updateDistanceToCamera(this.lastCameraPosition);
};
}
}
private sortInstances(layer: number): void {
this.spriteInstances[layer].sort((a, b) => (a.getDistanceToCamera() < b.getDistanceToCamera()) ? 1 : -1);
}
public prepareToRender(device: GfxDevice, renderInstManager: GfxRenderInstManager, viewerInput: ViewerRenderInput, layer: number) {
if(!this.spritesInfo || !this.spriteInstances || !DkrControlGlobals.ENABLE_TEXTURES.on) {
return;
}
const template = renderInstManager.pushTemplateRenderInst();
template.setBindingLayouts([{ numUniformBuffers: 3, numSamplers: 1, },]);
template.setInputLayoutAndState(this.inputLayout, this.inputState);
if(layer === SPRITE_LAYER_TRANSPARENT) {
this.checkCameraDistanceToObjects(viewerInput.camera, layer);
this.sortInstances(layer);
template.setMegaStateFlags(setAttachmentStateSimple({
depthWrite: true
}, {
blendMode: GfxBlendMode.Add,
blendSrcFactor: GfxBlendFactor.SrcAlpha,
blendDstFactor: GfxBlendFactor.OneMinusSrcAlpha,
}));
template.sortKey = makeSortKey(GfxRendererLayer.TRANSLUCENT);
if(viewerInput.deltaTime > 0.0) {
this.currentFrame += 0.1 * ((1000 / 30) / viewerInput.deltaTime);
}
}
if (this.gfxProgram === null) {
this.gfxProgram = renderInstManager.gfxRenderCache.createProgram(this.program);
}
const renderInst = renderInstManager.newRenderInst();
// Set scene parameters
let offs = renderInst.allocateUniformBuffer(F3DDKR_Sprite_Program.ub_SceneParams, 16);
const d = renderInst.mapUniformBufferF32(F3DDKR_Sprite_Program.ub_SceneParams);
offs += fillMatrix4x4(d, offs, viewerInput.camera.projectionMatrix);
// Set draw parameters
let offs2 = renderInst.allocateUniformBuffer(F3DDKR_Sprite_Program.ub_DrawParams, 4 + 16 + (20 * MAX_NUM_OF_SPRITE_INSTANCES));
const d2 = renderInst.mapUniformBufferF32(F3DDKR_Sprite_Program.ub_DrawParams);
d2[offs2] = this.currentFrame;
offs2 += 4;
// Use the texture.
this.bind(renderInst);
renderInst.sortKey = setSortKeyDepth(renderInst.sortKey, 0);
computeViewMatrix(viewMatrixScratch, viewerInput.camera);
assert(this.spriteInstances[layer].length <= MAX_NUM_OF_SPRITE_INSTANCES);
for(let i = 0; i < this.spriteInstances[layer].length; i++) {
const instanceObject: DkrObject = this.spriteInstances[layer][i];
const spriteIndex = instanceObject.getSpriteIndex();
d2[offs2] = this.spriteIndexOffsets[spriteIndex];
d2[offs2 + 1] = this.spritesInfo[spriteIndex].length;
d2[offs2 + 2] = instanceObject.getSpriteAlphaTest();
d2[offs2 + 3] = instanceObject.isSpriteCentered() ? 0.0 : 500.0;
offs2 += 4;
const color = instanceObject.getSpriteColor();
d2[offs2] = color[0];
d2[offs2 + 1] = color[1];
d2[offs2 + 2] = color[2];
d2[offs2 + 3] = color[3];
offs2 += 4;
if(DkrControlGlobals.ADV2_MIRROR.on) {
mat4.mul(viewMatrixCalcScratch, mirrorMatrix, instanceObject.getModelMatrix());
mat4.mul(viewMatrixCalcScratch, viewMatrixScratch, viewMatrixCalcScratch);
} else {
mat4.mul(viewMatrixCalcScratch, viewMatrixScratch, instanceObject.getModelMatrix());
}
calcBillboardMatrix(viewMatrixCalc2Scratch, viewMatrixCalcScratch, CalcBillboardFlags.UseRollLocal | CalcBillboardFlags.PriorityZ | CalcBillboardFlags.UseZPlane);
offs2 += fillMatrix4x3(d2, offs2, viewMatrixCalc2Scratch);
}
// Set tex parameters
let offs3 = renderInst.allocateUniformBuffer(F3DDKR_Sprite_Program.ub_TexParams, 4 * MAX_NUM_OF_SPRITE_FRAMES);
const d3 = renderInst.mapUniformBufferF32(F3DDKR_Sprite_Program.ub_TexParams);
for(let i = 0; i < this.spriteData.length; i++) {
d3[offs3++] = this.spriteData[i];
}
renderInst.setGfxProgram(this.gfxProgram);
if(this.spriteInstances[layer].length > 0) {
renderInst.drawIndexesInstanced(6, this.spriteInstances[layer].length);
}
/*
renderInst.setMegaStateFlags({
cullMode: GfxCullMode.BACK
});
*/
renderInstManager.submitRenderInst(renderInst);
renderInstManager.popTemplateRenderInst();
}
}