-
Notifications
You must be signed in to change notification settings - Fork 34
/
Sprite2dRenderer.ts
137 lines (115 loc) · 4.82 KB
/
Sprite2dRenderer.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
namespace pixi_projection {
import TYPES = PIXI.TYPES;
import premultiplyTint = PIXI.utils.premultiplyTint;
const shaderVert =
`precision highp float;
attribute vec3 aVertexPosition;
attribute vec2 aTextureCoord;
attribute vec4 aColor;
attribute float aTextureId;
uniform mat3 projectionMatrix;
varying vec2 vTextureCoord;
varying vec4 vColor;
varying float vTextureId;
void main(void){
gl_Position.xyw = projectionMatrix * aVertexPosition;
gl_Position.z = 0.0;
vTextureCoord = aTextureCoord;
vTextureId = aTextureId;
vColor = aColor;
}
`;
const shaderFrag = `
varying vec2 vTextureCoord;
varying vec4 vColor;
varying float vTextureId;
uniform sampler2D uSamplers[%count%];
void main(void){
vec4 color;
%forloop%
gl_FragColor = color * vColor;
}`;
export class Batch3dGeometry extends PIXI.Geometry
{
_buffer: PIXI.Buffer;
_indexBuffer : PIXI.Buffer;
constructor(_static = false)
{
super();
this._buffer = new PIXI.Buffer(null, _static, false);
this._indexBuffer = new PIXI.Buffer(null, _static, true);
this.addAttribute('aVertexPosition', this._buffer, 3, false, TYPES.FLOAT)
.addAttribute('aTextureCoord', this._buffer, 2, false, TYPES.FLOAT)
.addAttribute('aColor', this._buffer, 4, true, TYPES.UNSIGNED_BYTE)
.addAttribute('aTextureId', this._buffer, 1, true, TYPES.FLOAT)
.addIndex(this._indexBuffer);
}
}
export class Batch2dPluginFactory {
static create(options: any): any
{
const { vertex, fragment, vertexSize, geometryClass } = (Object as any).assign({
vertex: shaderVert,
fragment: shaderFrag,
geometryClass: Batch3dGeometry,
vertexSize: 7,
}, options);
return class BatchPlugin extends PIXI.AbstractBatchRenderer
{
constructor(renderer: PIXI.Renderer)
{
super(renderer);
this.shaderGenerator = new PIXI.BatchShaderGenerator(vertex, fragment);
this.geometryClass = geometryClass;
this.vertexSize = vertexSize;
}
vertexSize: number;
packInterleavedGeometry(element: any, attributeBuffer: PIXI.ViewableBuffer, indexBuffer: Uint16Array, aIndex: number, iIndex: number)
{
const {
uint32View,
float32View,
} = attributeBuffer;
const p = aIndex / this.vertexSize;// float32View.length / 6 / 2;
const uvs = element.uvs;
const indicies = element.indices;// geometry.getIndex().data;// indicies;
const vertexData = element.vertexData;
const vertexData2d = element.vertexData2d;
const textureId = element._texture.baseTexture._id;
const alpha = Math.min(element.worldAlpha, 1.0);
const argb = alpha < 1.0 && element._texture.baseTexture.premultiplyAlpha ? premultiplyTint(element._tintRGB, alpha)
: element._tintRGB + (alpha * 255 << 24);
if (vertexData2d) {
let j = 0;
for (let i = 0; i < vertexData2d.length; i += 3, j += 2)
{
float32View[aIndex++] = vertexData2d[i];
float32View[aIndex++] = vertexData2d[i + 1];
float32View[aIndex++] = vertexData2d[i + 2];
float32View[aIndex++] = uvs[j];
float32View[aIndex++] = uvs[j + 1];
uint32View[aIndex++] = argb;
float32View[aIndex++] = textureId;
}
} else {
for (let i = 0; i < vertexData.length; i += 2)
{
float32View[aIndex++] = vertexData[i];
float32View[aIndex++] = vertexData[i + 1];
float32View[aIndex++] = 1.0;
float32View[aIndex++] = uvs[i];
float32View[aIndex++] = uvs[i + 1];
uint32View[aIndex++] = argb;
float32View[aIndex++] = textureId;
}
}
for (let i = 0; i < indicies.length; i++)
{
indexBuffer[iIndex++] = p + indicies[i];
}
}
};
}
}
PIXI.Renderer.registerPlugin('batch2d', Batch2dPluginFactory.create({}));
}