mirror of
https://github.com/raysan5/raylib.git
synced 2026-04-26 00:27:25 -04:00
[example] cel-shading and outline using inverted hull (#5615)
* added cel-shading and outline using inverted hull example * new screenshot * added glsl100+120 compat * updated view * unnecessary spacing
This commit is contained in:
56
examples/shaders/resources/shaders/glsl120/cel.fs
Normal file
56
examples/shaders/resources/shaders/glsl120/cel.fs
Normal file
@ -0,0 +1,56 @@
|
||||
#version 120
|
||||
|
||||
varying vec3 fragPosition;
|
||||
varying vec2 fragTexCoord;
|
||||
varying vec4 fragColor;
|
||||
varying vec3 fragNormal;
|
||||
|
||||
uniform sampler2D texture0;
|
||||
uniform vec4 colDiffuse;
|
||||
uniform vec3 viewPos;
|
||||
uniform float numBands;
|
||||
|
||||
struct Light {
|
||||
int enabled;
|
||||
int type;
|
||||
vec3 position;
|
||||
vec3 target;
|
||||
vec4 color;
|
||||
};
|
||||
uniform Light lights[4];
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 texColor = texture2D(texture0, fragTexCoord);
|
||||
vec3 baseColor = texColor.rgb * fragColor.rgb * colDiffuse.rgb;
|
||||
vec3 norm = normalize(fragNormal);
|
||||
|
||||
float lightAccum = 0.08; // ambient floor
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
if (lights[i].enabled == 1)
|
||||
{
|
||||
vec3 lightDir;
|
||||
if (lights[i].type == 0)
|
||||
{
|
||||
// Directional: direction is from position toward target.
|
||||
lightDir = normalize(lights[i].position - lights[i].target);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Point: direction from surface to light.
|
||||
lightDir = normalize(lights[i].position - fragPosition);
|
||||
}
|
||||
|
||||
float NdotL = max(dot(norm, lightDir), 0.0);
|
||||
|
||||
// Quantize NdotL into numBands discrete steps.
|
||||
float quantized = min(floor(NdotL * numBands), numBands - 1.0) / (numBands - 1.0);
|
||||
lightAccum += quantized * lights[i].color.r;
|
||||
}
|
||||
}
|
||||
|
||||
lightAccum = clamp(lightAccum, 0.0, 1.0);
|
||||
gl_FragColor = vec4(baseColor * lightAccum, texColor.a * colDiffuse.a);
|
||||
}
|
||||
48
examples/shaders/resources/shaders/glsl120/cel.vs
Normal file
48
examples/shaders/resources/shaders/glsl120/cel.vs
Normal file
@ -0,0 +1,48 @@
|
||||
#version 120
|
||||
|
||||
attribute vec3 vertexPosition;
|
||||
attribute vec2 vertexTexCoord;
|
||||
attribute vec3 vertexNormal;
|
||||
attribute vec4 vertexColor;
|
||||
|
||||
uniform mat4 mvp;
|
||||
uniform mat4 matModel;
|
||||
|
||||
varying vec3 fragPosition;
|
||||
varying vec2 fragTexCoord;
|
||||
varying vec4 fragColor;
|
||||
varying vec3 fragNormal;
|
||||
|
||||
// inverse() and transpose() are not built-in until GLSL 1.40
|
||||
mat3 inverse(mat3 m)
|
||||
{
|
||||
float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];
|
||||
float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];
|
||||
float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];
|
||||
float b01 = a22*a11 - a12*a21;
|
||||
float b11 = -a22*a10 + a12*a20;
|
||||
float b21 = a21*a10 - a11*a20;
|
||||
float det = a00*b01 + a01*b11 + a02*b21;
|
||||
return mat3(b01, (-a22*a01 + a02*a21), ( a12*a01 - a02*a11),
|
||||
b11, ( a22*a00 - a02*a20), (-a12*a00 + a02*a10),
|
||||
b21, (-a21*a00 + a01*a20), ( a11*a00 - a01*a10)) / det;
|
||||
}
|
||||
|
||||
mat3 transpose(mat3 m)
|
||||
{
|
||||
return mat3(m[0][0], m[1][0], m[2][0],
|
||||
m[0][1], m[1][1], m[2][1],
|
||||
m[0][2], m[1][2], m[2][2]);
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
fragPosition = vec3(matModel * vec4(vertexPosition, 1.0));
|
||||
fragTexCoord = vertexTexCoord;
|
||||
fragColor = vertexColor;
|
||||
|
||||
mat3 normalMatrix = transpose(inverse(mat3(matModel)));
|
||||
fragNormal = normalize(normalMatrix * vertexNormal);
|
||||
|
||||
gl_Position = mvp * vec4(vertexPosition, 1.0);
|
||||
}
|
||||
@ -0,0 +1,6 @@
|
||||
#version 120
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_FragColor = vec4(0.05, 0.05, 0.05, 1.0);
|
||||
}
|
||||
15
examples/shaders/resources/shaders/glsl120/outline_hull.vs
Normal file
15
examples/shaders/resources/shaders/glsl120/outline_hull.vs
Normal file
@ -0,0 +1,15 @@
|
||||
#version 120
|
||||
|
||||
attribute vec3 vertexPosition;
|
||||
attribute vec3 vertexNormal;
|
||||
attribute vec2 vertexTexCoord;
|
||||
attribute vec4 vertexColor;
|
||||
|
||||
uniform mat4 mvp;
|
||||
uniform float outlineThickness;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 extruded = vertexPosition + vertexNormal * outlineThickness;
|
||||
gl_Position = mvp * vec4(extruded, 1.0);
|
||||
}
|
||||
Reference in New Issue
Block a user