-
Notifications
You must be signed in to change notification settings - Fork 46
/
sprite-batch-renderer.ts
60 lines (51 loc) · 2.32 KB
/
sprite-batch-renderer.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
import { Renderer, BatchShaderGenerator, IBatchableElement, ViewableBuffer } from "@pixi/core"
import { premultiplyTint } from "@pixi/utils"
import { SpriteBatchGeometry } from "./sprite-batch-geometry"
import { Shader as Vertex } from "./shader/sprite.vert"
import { Shader as Fragment } from "./shader/sprite.frag"
import { Compatibility } from "../compatibility/compatibility"
import { BatchRenderer } from "../compatibility/batch-renderer"
import { Matrix4x4 } from "../transform/matrix"
export class SpriteBatchRenderer extends BatchRenderer {
constructor(renderer: Renderer) {
super(renderer)
this.shaderGenerator = new BatchShaderGenerator(Vertex.source, Fragment.source)
this.geometryClass = SpriteBatchGeometry
// The vertex size when rendering 2D sprites is 6. Here, 16 is being added
// to hold the model matrix.
this.vertexSize = 6 + 16
Object.assign(this.state, {
culling: false, clockwiseFrontFace: false, depthTest: true
})
}
packInterleavedGeometry(element: IBatchableElement, attributeBuffer: ViewableBuffer, indexBuffer: Uint16Array, aIndex: number, iIndex: number) {
const { uint32View, float32View } = attributeBuffer
const packedVertices = aIndex / this.vertexSize
const uvs = element.uvs
const indicies = element.indices
const vertexData = element.vertexData
const textureId = element._texture.baseTexture._batchLocation
const alpha = Math.min(element.worldAlpha, 1.0)
const argb = (alpha < 1.0
&& element._texture.baseTexture.alphaMode)
? premultiplyTint(element._tintRGB, alpha)
: element._tintRGB + (alpha * 255 << 24)
for (let i = 0; i < vertexData.length; i += 2) {
float32View[aIndex++] = vertexData[i]
float32View[aIndex++] = vertexData[i + 1]
float32View[aIndex++] = uvs[i]
float32View[aIndex++] = uvs[i + 1]
uint32View[aIndex++] = argb
float32View[aIndex++] = textureId
// @ts-ignore Element does have a modelViewProjection
let modelViewProjection: Float32Array = element.modelViewProjection.array
for (let j = 0; j < 16; j++) {
float32View[aIndex++] = modelViewProjection[j]
}
}
for (let i = 0; i < indicies.length; i++) {
indexBuffer[iIndex++] = packedVertices + indicies[i]
}
}
}
Compatibility.installRendererPlugin("sprite3d", SpriteBatchRenderer)