-
Notifications
You must be signed in to change notification settings - Fork 0
/
tfx.frag.glsl
197 lines (165 loc) · 5.64 KB
/
tfx.frag.glsl
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
194
195
196
197
#version 300 es
precision highp float;
precision highp int;
precision highp usampler2D;
// precision highp sampler2D;
uniform int u_displayMode;
uniform vec3 u_cameraPosition;
uniform vec2 u_viewportSize;
// ao
uniform sampler2D u_aoTex;
uniform float u_aoStrength;
uniform float u_aoExp;
// Shadow
uniform sampler2D u_directionalShadowDepthTex;
uniform vec4 u_directionalShadowCasterPosition; // [position.xyz, bias (negative if pcss)]
uniform int u_directionalShadowSampleRadius;
uniform float u_maxShadowContribution;
#define BIAS_FROM_UI (u_directionalShadowCasterPosition.w)
#define USE_PCSS_SHADOWS (u_directionalShadowCasterPosition.w < 0.0f)
// Lights
uniform vec4 u_lightAmbient;
uniform vec3 u_light0_Position;
uniform vec4 u_light0_Color;
uniform vec3 u_light1_Position;
uniform vec4 u_light1_Color;
uniform vec3 u_light2_Position;
uniform vec4 u_light2_Color;
// material
uniform vec3 u_specularColor1;
uniform vec3 u_specularColor2;
uniform vec3 u_albedo;
uniform float u_primaryShift;
uniform float u_secondaryShift;
uniform float u_specularPower1;
uniform float u_specularPower2;
uniform float u_specularStrength1;
uniform float u_specularStrength2;
flat in int v_hairInstanceId;
in float v_vertexRootToTipFactor;
in vec3 v_position;
in vec3 v_normal;
in vec3 v_tangent;
in vec4 v_positionLightShadowSpace;
layout(location = 0) out vec4 outColor1;
layout(location = 1) out vec4 outColor2;
@import ./_utils;
@import ./_material; // for light struct
@import ./_shadows;
@import ./_kajiyakay;
const int DISPLAY_MODE_FINAL = 0;
const int DISPLAY_MODE_FLAT = 1;
const int DISPLAY_MODE_FOLLOW_GROUPS = 2;
const int DISPLAY_MODE_ROOT_TIP_PERCENTAGE = 3;
const int DISPLAY_MODE_SHADOW = 4;
vec3 getColorFromInstance (int instanceId) {
switch (instanceId) {
case 1: return vec3(0.0, 1.0, 0.0);
case 2: return vec3(0.0, 0.0, 1.0);
case 3: return vec3(1.0, 1.0, 0.0);
case 4: return vec3(0.0, 1.0, 1.0);
case 5: return vec3(1.0, 0.0, 1.0);
case 6: return vec3(1.0, 1.0, 1.0);
case 7: return vec3(0.0, 0.0, 0.0);
case 8: return vec3(0.0, 0.5, 0.0);
case 9: return vec3(0.5, 0.5, 0.5);
case 10: return vec3(0.0, 0.0, 0.5);
case 11: return vec3(0.5, 0.5, 0.0);
case 12: return vec3(0.0, 0.5, 0.5);
case 13: return vec3(0.5, 0.0, 0.5);
default:
case 0: return vec3(1.0, 0.0, 0.0);
}
}
float calculateShadow () {
vec3 toCaster = normalize(u_directionalShadowCasterPosition.xyz - v_position);
vec3 normal = normalize(v_normal); // TODO use tangent per http://developer.amd.com/wordpress/media/2012/10/Scheuermann_HairRendering.pdf s7?
return 1.0 - calculateDirectionalShadow(
v_positionLightShadowSpace, normal, toCaster
);
}
KajiyaKayParams createKajiyakayParams() {
KajiyaKayParams params;
params.V = normalize(u_cameraPosition - v_position); // viewDir
params.T = normalize(v_tangent); // tangentDir
params.N = normalize(v_normal); // normalDir
// params.L // filled later
params.shift = 0.0; // TODO
params.primaryShift = u_primaryShift;
params.secondaryShift = u_secondaryShift;
params.specularPower1 = u_specularPower1;
params.specularPower2 = u_specularPower2;
return params;
}
vec3 doShading(Light lights[3]) {
vec3 ambient = u_lightAmbient.rgb * u_lightAmbient.a;
vec3 radianceSum = vec3(0.0);
KajiyaKayParams params = createKajiyakayParams();
for (uint i = 0u; i < 3u; i++) {
Light light = lights[i];
vec3 L = normalize(light.position - v_position); // wi in integral
// float NdotL = dotMax0(v_normal, L); // no, cause it's hair
float NdotL = dotMax0(v_tangent, L);
vec3 radiance = light.color * light.intensity; // incoming color from light
// specular
params.L = L;
vec2 specularHighlight = kajiyakay(params);
vec3 specular1 = specularHighlight.x * u_specularColor1 * u_specularStrength1;
vec3 specular2 = specularHighlight.y * u_specularColor2 * u_specularStrength2;
// combine
// NOTE: this is different then usual Kajiya-Kay, I like it more
vec3 fr = u_albedo * NdotL + specular1 + specular2;
radianceSum += fr * radiance;
// debug:
// radianceSum += u_albedo * NdotL * radiance;
// radianceSum += NdotL;
// radianceSum += specularHighlight.x;
// radianceSum += specularHighlight.y;
// radianceSum += specular1;
// radianceSum += specular2;
// radianceSum += specular1 + specular2;
}
// ambient occlusion
float ao = texture(u_aoTex, gl_FragCoord.xy / u_viewportSize).r;
ao = getCustom_AO(ao, u_aoStrength, u_aoExp);
radianceSum *= ao;
ambient *= ao;
float shadow = calculateShadow();
radianceSum = radianceSum * clamp(shadow, 1.0 - u_maxShadowContribution, 1.0);
return ambient + radianceSum;
}
void main() {
Light lights[3];
lights[0] = unpackLight(u_light0_Position, u_light0_Color);
lights[1] = unpackLight(u_light1_Position, u_light1_Color);
lights[2] = unpackLight(u_light2_Position, u_light2_Color);
vec3 result;
switch (u_displayMode) {
case DISPLAY_MODE_FOLLOW_GROUPS: {
result = getColorFromInstance(v_hairInstanceId);
break;
}
case DISPLAY_MODE_ROOT_TIP_PERCENTAGE: {
result = vec3(v_vertexRootToTipFactor);
// result = mix(vec3(0.0, 1.0, 1.0), vec3(0.0, 1.0, 0.0), v_vertexRootToTipFactor);
// result += getColorFromInstance(v_hairInstanceId);
break;
}
case DISPLAY_MODE_SHADOW: {
float shadow = calculateShadow();
result = vec3(shadow);
break;
}
case DISPLAY_MODE_FLAT: {
result = vec3(0.8);
break;
}
default:
case DISPLAY_MODE_FINAL: {
result = doShading(lights);
break;
}
}
outColor1 = vec4(result, 1.0);
outColor2 = vec4(to_0_1(normalize(v_normal)), 1.0);
}