Skip to content

Commit db4f48e

Browse files
committed
lighting shaders with AR color shift
1 parent 876b56c commit db4f48e

File tree

4 files changed

+344
-9
lines changed

4 files changed

+344
-9
lines changed

mode/libraries/ar/src/assets/shaders/ARLightFrag.glsl

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ precision mediump float;
2525
precision mediump int;
2626
#endif
2727

28+
uniform vec4 colorCorrection;
29+
2830
varying vec4 vertColor;
2931
varying vec4 backVertColor;
30-
varying vec4 colorCorrection;
3132

32-
void main() {
33-
// Approximate sRGB gamma.
34-
const float kGamma = 0.4545454;
35-
const float kInverseGamma = 2.2;
36-
const float kMiddleGrayGamma = 0.466;
33+
// Approximate sRGB gamma parameters
34+
const float kGamma = 0.4545454;
35+
const float kMiddleGrayGamma = 0.466;
3736

37+
void main() {
3838
vec3 colorShift = colorCorrection.rgb;
3939
float averagePixelIntensity = colorCorrection.a;
4040

@@ -45,6 +45,5 @@ void main() {
4545

4646
// Apply average pixel intensity and color shift
4747
color.rgb *= colorShift * (averagePixelIntensity / kMiddleGrayGamma);
48-
gl_FragColor.rgb = color;
49-
gl_FragColor.a = objectColor.a;
48+
gl_FragColor = color;
5049
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
Part of the Processing project - http://processing.org
3+
4+
Copyright (c) 2012-16 The Processing Foundation
5+
Copyright (c) 2004-12 Ben Fry and Casey Reas
6+
Copyright (c) 2001-04 Massachusetts Institute of Technology
7+
8+
This library is free software; you can redistribute it and/or
9+
modify it under the terms of the GNU Lesser General Public
10+
License as published by the Free Software Foundation, version 2.1.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General
18+
Public License along with this library; if not, write to the
19+
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20+
Boston, MA 02111-1307 USA
21+
*/
22+
23+
uniform mat4 modelviewMatrix;
24+
uniform mat4 transformMatrix;
25+
uniform mat3 normalMatrix;
26+
27+
uniform int lightCount;
28+
uniform vec4 lightPosition[8];
29+
uniform vec3 lightNormal[8];
30+
uniform vec3 lightAmbient[8];
31+
uniform vec3 lightDiffuse[8];
32+
uniform vec3 lightSpecular[8];
33+
uniform vec3 lightFalloff[8];
34+
uniform vec2 lightSpot[8];
35+
36+
attribute vec4 position;
37+
attribute vec4 color;
38+
attribute vec3 normal;
39+
40+
attribute vec4 ambient;
41+
attribute vec4 specular;
42+
attribute vec4 emissive;
43+
attribute float shininess;
44+
45+
varying vec4 vertColor;
46+
varying vec4 backVertColor;
47+
48+
const float zero_float = 0.0;
49+
const float one_float = 1.0;
50+
const vec3 zero_vec3 = vec3(0);
51+
52+
const float kInverseGamma = 2.2;
53+
54+
float falloffFactor(vec3 lightPos, vec3 vertPos, vec3 coeff) {
55+
vec3 lpv = lightPos - vertPos;
56+
vec3 dist = vec3(one_float);
57+
dist.z = dot(lpv, lpv);
58+
dist.y = sqrt(dist.z);
59+
return one_float / dot(dist, coeff);
60+
}
61+
62+
float spotFactor(vec3 lightPos, vec3 vertPos, vec3 lightNorm, float minCos, float spotExp) {
63+
vec3 lpv = normalize(lightPos - vertPos);
64+
vec3 nln = -one_float * lightNorm;
65+
float spotCos = dot(nln, lpv);
66+
return spotCos <= minCos ? zero_float : pow(spotCos, spotExp);
67+
}
68+
69+
float lambertFactor(vec3 lightDir, vec3 vecNormal) {
70+
return max(zero_float, dot(lightDir, vecNormal));
71+
}
72+
73+
float blinnPhongFactor(vec3 lightDir, vec3 vertPos, vec3 vecNormal, float shine) {
74+
vec3 np = normalize(vertPos);
75+
vec3 ldp = normalize(lightDir - np);
76+
return pow(max(zero_float, dot(ldp, vecNormal)), shine);
77+
}
78+
79+
void main() {
80+
// Vertex in clip coordinates
81+
gl_Position = transformMatrix * position;
82+
83+
// Vertex in eye coordinates
84+
vec3 ecVertex = vec3(modelviewMatrix * position);
85+
86+
// Normal vector in eye coordinates
87+
vec3 ecNormal = normalize(normalMatrix * normal);
88+
vec3 ecNormalInv = ecNormal * -one_float;
89+
90+
// Light calculations
91+
vec3 totalAmbient = vec3(0, 0, 0);
92+
93+
vec3 totalFrontDiffuse = vec3(0, 0, 0);
94+
vec3 totalFrontSpecular = vec3(0, 0, 0);
95+
96+
vec3 totalBackDiffuse = vec3(0, 0, 0);
97+
vec3 totalBackSpecular = vec3(0, 0, 0);
98+
99+
for (int i = 0; i < 8; i++) {
100+
if (lightCount == i) break;
101+
102+
vec3 lightPos = lightPosition[i].xyz;
103+
bool isDir = lightPosition[i].w < one_float;
104+
float spotCos = lightSpot[i].x;
105+
float spotExp = lightSpot[i].y;
106+
107+
vec3 lightDir;
108+
float falloff;
109+
float spotf;
110+
111+
if (isDir) {
112+
falloff = one_float;
113+
lightDir = -one_float * lightNormal[i];
114+
} else {
115+
falloff = falloffFactor(lightPos, ecVertex, lightFalloff[i]);
116+
lightDir = normalize(lightPos - ecVertex);
117+
}
118+
119+
spotf = spotExp > zero_float ? spotFactor(lightPos, ecVertex, lightNormal[i],
120+
spotCos, spotExp)
121+
: one_float;
122+
123+
if (any(greaterThan(lightAmbient[i], zero_vec3))) {
124+
totalAmbient += lightAmbient[i] * falloff;
125+
}
126+
127+
if (any(greaterThan(lightDiffuse[i], zero_vec3))) {
128+
totalFrontDiffuse += lightDiffuse[i] * falloff * spotf *
129+
lambertFactor(lightDir, ecNormal);
130+
totalBackDiffuse += lightDiffuse[i] * falloff * spotf *
131+
lambertFactor(lightDir, ecNormalInv);
132+
}
133+
134+
if (any(greaterThan(lightSpecular[i], zero_vec3))) {
135+
totalFrontSpecular += lightSpecular[i] * falloff * spotf *
136+
blinnPhongFactor(lightDir, ecVertex, ecNormal, shininess);
137+
totalBackSpecular += lightSpecular[i] * falloff * spotf *
138+
blinnPhongFactor(lightDir, ecVertex, ecNormalInv, shininess);
139+
}
140+
}
141+
142+
vec4 gcolor = vec4(pow(color.rgb, vec3(kInverseGamma)), color.a);
143+
vec4 gambient = vec4(pow(ambient.rgb, vec3(kInverseGamma)), ambient.a);
144+
145+
// Calculating final color as result of all lights (plus emissive term).
146+
// Transparency is determined exclusively by the diffuse component.
147+
vertColor = vec4(totalAmbient, 0) * gambient +
148+
vec4(totalFrontDiffuse, 1) * gcolor +
149+
vec4(totalFrontSpecular, 0) * specular +
150+
vec4(emissive.rgb, 0);
151+
152+
backVertColor = vec4(totalAmbient, 0) * gambient +
153+
vec4(totalBackDiffuse, 1) * gcolor +
154+
vec4(totalBackSpecular, 0) * specular +
155+
vec4(emissive.rgb, 0);
156+
}

mode/libraries/ar/src/assets/shaders/ARTexLightFrag.glsl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ varying vec4 vertColor;
3232
varying vec4 backVertColor;
3333
varying vec4 vertTexCoord;
3434

35+
// Approximate sRGB gamma parameters
36+
const float kGamma = 0.4545454;
37+
const float kInverseGamma = 2.2;
38+
const float kMiddleGrayGamma = 0.466;
39+
3540
void main() {
36-
gl_FragColor = texture2D(texture, vertTexCoord.st) * (gl_FrontFacing ? vertColor : backVertColor);
41+
vec3 colorShift = colorCorrection.rgb;
42+
float averagePixelIntensity = colorCorrection.a;
43+
44+
vec4 tex = texture2D(texture, vertTexCoord.st);
45+
vec4 gtex = vec4(pow(tex.rgb, vec3(kInverseGamma)), tex.a);
46+
47+
vec4 color = gtex * (gl_FrontFacing ? vertColor : backVertColor);
48+
49+
// Apply SRGB gamma before writing the fragment color.
50+
color.rgb = pow(color.rgb, vec3(kGamma));
51+
52+
// Apply average pixel intensity and color shift
53+
color.rgb *= colorShift * (averagePixelIntensity / kMiddleGrayGamma);
54+
gl_FragColor = color;
3755
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
Part of the Processing project - http://processing.org
3+
4+
Copyright (c) 2012-16 The Processing Foundation
5+
Copyright (c) 2004-12 Ben Fry and Casey Reas
6+
Copyright (c) 2001-04 Massachusetts Institute of Technology
7+
8+
This library is free software; you can redistribute it and/or
9+
modify it under the terms of the GNU Lesser General Public
10+
License as published by the Free Software Foundation, version 2.1.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General
18+
Public License along with this library; if not, write to the
19+
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20+
Boston, MA 02111-1307 USA
21+
*/
22+
23+
uniform mat4 modelviewMatrix;
24+
uniform mat4 transformMatrix;
25+
uniform mat3 normalMatrix;
26+
uniform mat4 texMatrix;
27+
28+
uniform int lightCount;
29+
uniform vec4 lightPosition[8];
30+
uniform vec3 lightNormal[8];
31+
uniform vec3 lightAmbient[8];
32+
uniform vec3 lightDiffuse[8];
33+
uniform vec3 lightSpecular[8];
34+
uniform vec3 lightFalloff[8];
35+
uniform vec2 lightSpot[8];
36+
37+
attribute vec4 position;
38+
attribute vec4 color;
39+
attribute vec3 normal;
40+
attribute vec2 texCoord;
41+
42+
attribute vec4 ambient;
43+
attribute vec4 specular;
44+
attribute vec4 emissive;
45+
attribute float shininess;
46+
47+
varying vec4 vertColor;
48+
varying vec4 backVertColor;
49+
varying vec4 vertTexCoord;
50+
51+
const float zero_float = 0.0;
52+
const float one_float = 1.0;
53+
const vec3 zero_vec3 = vec3(0);
54+
55+
const float kInverseGamma = 2.2;
56+
57+
float falloffFactor(vec3 lightPos, vec3 vertPos, vec3 coeff) {
58+
vec3 lpv = lightPos - vertPos;
59+
vec3 dist = vec3(one_float);
60+
dist.z = dot(lpv, lpv);
61+
dist.y = sqrt(dist.z);
62+
return one_float / dot(dist, coeff);
63+
}
64+
65+
float spotFactor(vec3 lightPos, vec3 vertPos, vec3 lightNorm, float minCos, float spotExp) {
66+
vec3 lpv = normalize(lightPos - vertPos);
67+
vec3 nln = -one_float * lightNorm;
68+
float spotCos = dot(nln, lpv);
69+
return spotCos <= minCos ? zero_float : pow(spotCos, spotExp);
70+
}
71+
72+
float lambertFactor(vec3 lightDir, vec3 vecNormal) {
73+
return max(zero_float, dot(lightDir, vecNormal));
74+
}
75+
76+
float blinnPhongFactor(vec3 lightDir, vec3 vertPos, vec3 vecNormal, float shine) {
77+
vec3 np = normalize(vertPos);
78+
vec3 ldp = normalize(lightDir - np);
79+
return pow(max(zero_float, dot(ldp, vecNormal)), shine);
80+
}
81+
82+
void main() {
83+
// Vertex in clip coordinates
84+
gl_Position = transformMatrix * position;
85+
86+
// Vertex in eye coordinates
87+
vec3 ecVertex = vec3(modelviewMatrix * position);
88+
89+
// Normal vector in eye coordinates
90+
vec3 ecNormal = normalize(normalMatrix * normal);
91+
vec3 ecNormalInv = ecNormal * -one_float;
92+
93+
// Light calculations
94+
vec3 totalAmbient = vec3(0, 0, 0);
95+
96+
vec3 totalFrontDiffuse = vec3(0, 0, 0);
97+
vec3 totalFrontSpecular = vec3(0, 0, 0);
98+
99+
vec3 totalBackDiffuse = vec3(0, 0, 0);
100+
vec3 totalBackSpecular = vec3(0, 0, 0);
101+
102+
for (int i = 0; i < 8; i++) {
103+
if (lightCount == i) break;
104+
105+
vec3 lightPos = lightPosition[i].xyz;
106+
bool isDir = lightPosition[i].w < one_float;
107+
float spotCos = lightSpot[i].x;
108+
float spotExp = lightSpot[i].y;
109+
110+
vec3 lightDir;
111+
float falloff;
112+
float spotf;
113+
114+
if (isDir) {
115+
falloff = one_float;
116+
lightDir = -one_float * lightNormal[i];
117+
} else {
118+
falloff = falloffFactor(lightPos, ecVertex, lightFalloff[i]);
119+
lightDir = normalize(lightPos - ecVertex);
120+
}
121+
122+
spotf = spotExp > zero_float ? spotFactor(lightPos, ecVertex, lightNormal[i],
123+
spotCos, spotExp)
124+
: one_float;
125+
126+
if (any(greaterThan(lightAmbient[i], zero_vec3))) {
127+
totalAmbient += lightAmbient[i] * falloff;
128+
}
129+
130+
if (any(greaterThan(lightDiffuse[i], zero_vec3))) {
131+
totalFrontDiffuse += lightDiffuse[i] * falloff * spotf *
132+
lambertFactor(lightDir, ecNormal);
133+
totalBackDiffuse += lightDiffuse[i] * falloff * spotf *
134+
lambertFactor(lightDir, ecNormalInv);
135+
}
136+
137+
if (any(greaterThan(lightSpecular[i], zero_vec3))) {
138+
totalFrontSpecular += lightSpecular[i] * falloff * spotf *
139+
blinnPhongFactor(lightDir, ecVertex, ecNormal, shininess);
140+
totalBackSpecular += lightSpecular[i] * falloff * spotf *
141+
blinnPhongFactor(lightDir, ecVertex, ecNormalInv, shininess);
142+
}
143+
}
144+
145+
vec4 gcolor = vec4(pow(color.rgb, vec3(kInverseGamma)), color.a);
146+
vec4 gambient = vec4(pow(ambient.rgb, vec3(kInverseGamma)), ambient.a);
147+
148+
// Calculating final color as result of all lights (plus emissive term).
149+
// Transparency is determined exclusively by the diffuse component.
150+
vertColor = vec4(totalAmbient, 0) * gambient +
151+
vec4(totalFrontDiffuse, 1) * gcolor +
152+
vec4(totalFrontSpecular, 0) * specular +
153+
vec4(emissive.rgb, 0);
154+
155+
backVertColor = vec4(totalAmbient, 0) * gambient +
156+
vec4(totalBackDiffuse, 1) * gcolor +
157+
vec4(totalBackSpecular, 0) * specular +
158+
vec4(emissive.rgb, 0);
159+
160+
// Calculating texture coordinates, with r and q set both to one
161+
vertTexCoord = texMatrix * vec4(texCoord, 1.0, 1.0);
162+
}

0 commit comments

Comments
 (0)