-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshaders.js
More file actions
194 lines (170 loc) · 7.48 KB
/
shaders.js
File metadata and controls
194 lines (170 loc) · 7.48 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
var shaderScripts = {
low : {
vertexShader : `attribute vec4 a_position;
attribute vec2 a_textCoord;
uniform mat4 u_worldViewProjection;
varying vec2 v_textCoord;
void main(void) {
gl_Position = u_worldViewProjection * a_position;
v_textCoord = a_textCoord;
}`,
fragmentShader : `precision mediump float;
uniform vec4 u_color;
uniform sampler2D u_texture;
varying vec2 v_textCoord;
void main(void) {
vec4 tex = texture2D(u_texture, v_textCoord);
gl_FragColor = tex + u_color * (1.-tex.a); //oltre alla normale applicazione della texture applico anche la trasparenza nel caso di texture png
}`,
},
high : {
vertexShader: `attribute vec4 a_position;
attribute vec3 a_normal;
attribute vec2 a_textCoord;
varying vec2 v_textCoord;
uniform mat4 u_worldViewProjection, u_world;
uniform vec3 u_pointLightPosition;
uniform vec3 u_cameraPosition;
varying vec3 v_normal;
varying vec3 v_surfaceToLight;
varying vec3 v_surfaceToCamera;
void main() {
gl_Position = u_worldViewProjection * a_position; //Dovrei farmi dare solo vp e qui fare vp * worldPos
v_textCoord = a_textCoord;
v_normal = vec3(a_normal.x, a_normal.y, a_normal.z); //Perche le normali erano sbagliate, non farlo se non serve
v_normal = mat3(u_world) * v_normal;
vec3 surfaceWorldPosition = (u_world * a_position).xyz;
v_surfaceToLight = u_pointLightPosition - surfaceWorldPosition; //Vettore direzione dalla superficie verso la luce
v_surfaceToCamera = u_cameraPosition - surfaceWorldPosition; //Vettore direzione dalla superficie verso la camera
}`,
fragmentShader: `precision mediump float;
uniform float u_ambient;
uniform float u_shininess;
uniform vec4 u_color;
uniform sampler2D u_texture;
varying vec3 v_normal, v_surfaceToLight, v_surfaceToCamera;
varying vec2 v_textCoord;
void main() {
vec3 normal = normalize(v_normal);
vec3 surfaceToLightDirection = normalize(v_surfaceToLight);
vec3 surfaceToViewDirection = normalize(v_surfaceToCamera);
vec3 halfVector = normalize(surfaceToLightDirection + surfaceToViewDirection); //Prendo il vettore "in mezzo" tra le direzioni luce e camera
float pointLight = dot(normal, surfaceToLightDirection);
float specularLight = 0.0;
if(pointLight > 0.0){
specularLight = dot(normal, halfVector);
specularLight = pow(specularLight, u_shininess);
}
float light = u_ambient + pointLight;
if(light < u_ambient) {
light = u_ambient;
}
vec4 tex = texture2D(u_texture, v_textCoord);
gl_FragColor = tex + u_color * (1.-tex.a);
gl_FragColor.rgb *= light;
gl_FragColor.rgb += specularLight;
}`,
},
shadows : {
vertexShader: `attribute vec4 a_position;
attribute vec3 a_normal;
attribute vec2 a_textCoord;
varying vec2 v_textCoord;
uniform mat4 u_worldViewProjection, u_world;
uniform vec3 u_pointLightPosition;
uniform vec3 u_cameraPosition;
varying vec3 v_normal;
varying vec3 v_surfaceToLight;
varying vec3 v_surfaceToCamera;
//Shadows
uniform mat4 u_depthProjectionMatrix;
varying vec4 v_projectedTexcoord;
void main() {
gl_Position = u_worldViewProjection * a_position; //Dovrei farmi dare solo vp e qui fare vp * worldPos
v_textCoord = a_textCoord;
v_normal = vec3(a_normal.x, a_normal.y, a_normal.z); //Perche le normali erano sbagliate, non farlo se non serve
v_normal = mat3(u_world) * v_normal;
vec3 surfaceWorldPosition = (u_world * a_position).xyz;
v_surfaceToLight = u_pointLightPosition - surfaceWorldPosition; //Vettore direzione dalla superficie verso la luce
v_surfaceToCamera = u_cameraPosition - surfaceWorldPosition; //Vettore direzione dalla superficie verso la camera
//Shadows
vec4 worldPosition = u_world * a_position;
v_projectedTexcoord = u_depthProjectionMatrix * worldPosition;
}`,
fragmentShader: `precision mediump float;
uniform float u_ambient;
uniform float u_shininess;
uniform vec4 u_color;
uniform sampler2D u_texture;
varying vec3 v_normal, v_surfaceToLight, v_surfaceToCamera;
varying vec2 v_textCoord;
//Shadows
uniform sampler2D u_depthTexture;
varying vec4 v_projectedTexcoord;
void main() {
vec3 normal = normalize(v_normal);
vec3 surfaceToLightDirection = normalize(v_surfaceToLight);
vec3 surfaceToViewDirection = normalize(v_surfaceToCamera);
vec3 halfVector = normalize(surfaceToLightDirection + surfaceToViewDirection); //Prendo il vettore "in mezzo" tra le direzioni luce e camera
float pointLight = dot(normal, surfaceToLightDirection);
float specularLight = 0.0;
if(pointLight > 0.0){
specularLight = dot(normal, halfVector);
specularLight = pow(specularLight, u_shininess);
}
float light = u_ambient + pointLight;
//Shadows
vec3 projectedTexcoord = v_projectedTexcoord.xyz / v_projectedTexcoord.w;
float currentDepth = projectedTexcoord.z - 0.002;
bool inProjectionRange =
projectedTexcoord.x >= 0.0 &&
projectedTexcoord.x <= 1.0 &&
projectedTexcoord.y >= 0.0 &&
projectedTexcoord.y <= 1.0;
float projectedDepth = texture2D(u_depthTexture, projectedTexcoord.xy).r;
float shadowLight = projectedDepth <= currentDepth ? 0.5 : 1.0;
if(inProjectionRange){
light *= shadowLight;
}
if(light < u_ambient) {
light = u_ambient;
}
vec4 tex = texture2D(u_texture, v_textCoord);
gl_FragColor = tex + u_color * (1.-tex.a);
gl_FragColor.rgb *= light;
gl_FragColor.rgb += specularLight * shadowLight;
}`,
},
skybox: {
vertexShader:`
attribute vec4 a_position;
varying vec4 v_position;
void main() {
v_position = a_position;
gl_Position = a_position;
gl_Position.z = 1.0;
}
`,
fragmentShader:`
precision mediump float;
uniform samplerCube u_skybox;
uniform mat4 u_viewDirectionProjectionInverse;
varying vec4 v_position;
void main() {
vec4 t = u_viewDirectionProjectionInverse * v_position;
gl_FragColor = textureCube(u_skybox, normalize(t.xyz / t.w));
}
`
},
shadowProjection : {
vertexShader : `attribute vec4 a_position;
uniform mat4 u_worldViewProjection;
void main(void) {
gl_Position = u_worldViewProjection * a_position;
}`,
fragmentShader : `precision mediump float;
void main(void) {
gl_FragColor = vec4(1,1,1,1);
}`,
},
};