forked from BabylonJS/Babylon.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpuRenderParticles.vertex.fx
More file actions
173 lines (141 loc) · 4.95 KB
/
Copy pathgpuRenderParticles.vertex.fx
File metadata and controls
173 lines (141 loc) · 4.95 KB
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
precision highp float;
uniform mat4 view;
uniform mat4 projection;
uniform vec2 translationPivot;
uniform vec3 worldOffset;
#ifdef LOCAL
uniform mat4 emitterWM;
#endif
// Particles state
attribute vec3 position;
attribute float age;
attribute float life;
attribute vec3 size;
#ifndef BILLBOARD
attribute vec3 initialDirection;
#endif
#ifdef BILLBOARDSTRETCHED
attribute vec3 direction;
#endif
attribute float angle;
#ifdef ANIMATESHEET
attribute float cellIndex;
#endif
attribute vec2 offset;
attribute vec2 uv;
varying vec2 vUV;
varying vec4 vColor;
varying vec3 vPositionW;
#if defined(BILLBOARD) && !defined(BILLBOARDY) && !defined(BILLBOARDSTRETCHED)
uniform mat4 invView;
#endif
#include<clipPlaneVertexDeclaration2>
#ifdef COLORGRADIENTS
uniform sampler2D colorGradientSampler;
#else
uniform vec4 colorDead;
attribute vec4 color;
#endif
#ifdef ANIMATESHEET
uniform vec3 sheetInfos;
#endif
#ifdef BILLBOARD
uniform vec3 eyePosition;
#endif
vec3 rotate(vec3 yaxis, vec3 rotatedCorner) {
vec3 xaxis = normalize(cross(vec3(0., 1.0, 0.), yaxis));
vec3 zaxis = normalize(cross(yaxis, xaxis));
vec3 row0 = vec3(xaxis.x, xaxis.y, xaxis.z);
vec3 row1 = vec3(yaxis.x, yaxis.y, yaxis.z);
vec3 row2 = vec3(zaxis.x, zaxis.y, zaxis.z);
mat3 rotMatrix = mat3(row0, row1, row2);
vec3 alignedCorner = rotMatrix * rotatedCorner;
#ifdef LOCAL
return ((emitterWM * vec4(position, 1.0)).xyz + worldOffset) + alignedCorner;
#else
return (position + worldOffset) + alignedCorner;
#endif
}
#ifdef BILLBOARDSTRETCHED
vec3 rotateAlign(vec3 toCamera, vec3 rotatedCorner) {
vec3 normalizedToCamera = normalize(toCamera);
vec3 normalizedCrossDirToCamera = normalize(cross(normalize(direction), normalizedToCamera));
vec3 crossProduct = normalize(cross(normalizedToCamera, normalizedCrossDirToCamera));
vec3 row0 = vec3(normalizedCrossDirToCamera.x, normalizedCrossDirToCamera.y, normalizedCrossDirToCamera.z);
vec3 row1 = vec3(crossProduct.x, crossProduct.y, crossProduct.z);
vec3 row2 = vec3(normalizedToCamera.x, normalizedToCamera.y, normalizedToCamera.z);
mat3 rotMatrix = mat3(row0, row1, row2);
vec3 alignedCorner = rotMatrix * rotatedCorner;
#ifdef LOCAL
return ((emitterWM * vec4(position, 1.0)).xyz + worldOffset) + alignedCorner;
#else
return (position + worldOffset) + alignedCorner;
#endif
}
#endif
void main() {
#ifdef ANIMATESHEET
float rowOffset = floor(cellIndex / sheetInfos.z);
float columnOffset = cellIndex - rowOffset * sheetInfos.z;
vec2 uvScale = sheetInfos.xy;
vec2 uvOffset = vec2(uv.x , 1.0 - uv.y);
vUV = (uvOffset + vec2(columnOffset, rowOffset)) * uvScale;
#else
vUV = uv;
#endif
float ratio = age / life;
#ifdef COLORGRADIENTS
vColor = texture2D(colorGradientSampler, vec2(ratio, 0));
#else
vColor = color * vec4(1.0 - ratio) + colorDead * vec4(ratio);
#endif
vec2 cornerPos = (offset - translationPivot) * size.yz * size.x + translationPivot;
#ifdef BILLBOARD
vec4 rotatedCorner;
rotatedCorner.w = 0.;
#ifdef BILLBOARDY
rotatedCorner.x = cornerPos.x * cos(angle) - cornerPos.y * sin(angle);
rotatedCorner.z = cornerPos.x * sin(angle) + cornerPos.y * cos(angle);
rotatedCorner.y = 0.;
vec3 yaxis = (position + worldOffset) - eyePosition;
yaxis.y = 0.;
vPositionW = rotate(normalize(yaxis), rotatedCorner.xyz);
vec4 viewPosition = (view * vec4(vPositionW, 1.0));
#elif defined(BILLBOARDSTRETCHED)
rotatedCorner.x = cornerPos.x * cos(angle) - cornerPos.y * sin(angle);
rotatedCorner.y = cornerPos.x * sin(angle) + cornerPos.y * cos(angle);
rotatedCorner.z = 0.;
vec3 toCamera = (position + worldOffset) - eyePosition;
vPositionW = rotateAlign(toCamera, rotatedCorner.xyz);
vec4 viewPosition = (view * vec4(vPositionW, 1.0));
#else
// Rotate
rotatedCorner.x = cornerPos.x * cos(angle) - cornerPos.y * sin(angle);
rotatedCorner.y = cornerPos.x * sin(angle) + cornerPos.y * cos(angle);
rotatedCorner.z = 0.;
// Expand position
#ifdef LOCAL
vec4 viewPosition = view * vec4(((emitterWM * vec4(position, 1.0)).xyz + worldOffset), 1.0) + rotatedCorner;
#else
vec4 viewPosition = view * vec4((position + worldOffset), 1.0) + rotatedCorner;
#endif
vPositionW = (invView * viewPosition).xyz;
#endif
#else
// Rotate
vec3 rotatedCorner;
rotatedCorner.x = cornerPos.x * cos(angle) - cornerPos.y * sin(angle);
rotatedCorner.y = 0.;
rotatedCorner.z = cornerPos.x * sin(angle) + cornerPos.y * cos(angle);
vec3 yaxis = normalize(initialDirection);
vPositionW = rotate(yaxis, rotatedCorner);
// Expand position
vec4 viewPosition = view * vec4(vPositionW, 1.0);
#endif
gl_Position = projection * viewPosition;
// Clip plane
#if defined(CLIPPLANE) || defined(CLIPPLANE2) || defined(CLIPPLANE3) || defined(CLIPPLANE4) || defined(CLIPPLANE5) || defined(CLIPPLANE6)
vec4 worldPos = vec4(vPositionW, 1.0);
#endif
#include<clipPlaneVertex>
}