move to const
This commit is contained in:
1 parent
127277e762
commit
b1f0b7f637
4 files changed
+128
-120
No files matched your search
@@ -88,122 +88,11 @@ static Matrix sr_transform(Vector3 pos, float scale, float rx, float ry, float r
|
||||
return MatrixMultiply(MatrixMultiply(mat_scale, mat_rotation), mat_translation);
|
||||
}
|
||||
|
||||
|
||||
static const char *SR_MAIN_FS =
|
||||
"#version 330\n"
|
||||
"\n"
|
||||
"in vec3 vWorldPos;\n"
|
||||
"in vec3 vNormal;\n"
|
||||
"in vec2 vTexCoord;\n"
|
||||
"in vec4 vLightClip;\n"
|
||||
"\n"
|
||||
"uniform sampler2D texture0;\n"
|
||||
"uniform sampler2D u_shadowMap;\n"
|
||||
"uniform vec3 u_lightPos;\n"
|
||||
"uniform vec3 u_lightColor;\n"
|
||||
"uniform vec3 u_ambient;\n"
|
||||
"uniform vec3 u_albedoColor;\n"
|
||||
"uniform float u_lightEnabled;\n"
|
||||
"uniform float u_emissive;\n"
|
||||
"uniform float u_shadowBias;\n"
|
||||
"uniform float u_shadowSoftness;\n"
|
||||
"uniform float u_isGround;\n"
|
||||
"uniform vec3 u_gridColor;\n"
|
||||
"uniform vec3 u_cameraPos;\n"
|
||||
"uniform vec3 u_fogColor;\n"
|
||||
"uniform float u_fogStart;\n"
|
||||
"uniform float u_fogEnd;\n"
|
||||
"\n"
|
||||
"out vec4 finalColor;\n"
|
||||
"\n"
|
||||
"bool isPixelInShadowMap(vec3 proj)\n"
|
||||
"{\n"
|
||||
" return proj.x >= 0.0 && proj.x <= 1.0 &&\n"
|
||||
" proj.y >= 0.0 && proj.y <= 1.0 &&\n"
|
||||
" proj.z >= 0.0 && proj.z <= 1.0;\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"float shadowFactor()\n"
|
||||
"{\n"
|
||||
" vec3 proj = vLightClip.xyz / vLightClip.w;\n"
|
||||
" proj = proj * 0.5 + 0.5;\n"
|
||||
"\n"
|
||||
" if (!isPixelInShadowMap(proj)) return 0.0;\n"
|
||||
"\n"
|
||||
" int radius = int(u_shadowSoftness);\n"
|
||||
" vec2 texel = 1.0 / vec2(textureSize(u_shadowMap, 0));\n"
|
||||
" float sum = 0.0;\n"
|
||||
" float count = 0.0;\n"
|
||||
" for (int x = -radius; x <= radius; x++)\n"
|
||||
" {\n"
|
||||
" for (int y = -radius; y <= radius; y++)\n"
|
||||
" {\n"
|
||||
" float d = texture(u_shadowMap, proj.xy + vec2(float(x), float(y)) * texel).r;\n"
|
||||
" sum += (proj.z - u_shadowBias > d) ? 1.0 : 0.0;\n"
|
||||
" count += 1.0;\n"
|
||||
" }\n"
|
||||
" }\n"
|
||||
" return sum / count;\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" vec4 texel = texture(texture0, vTexCoord);\n"
|
||||
" vec3 albedo = texel.rgb * u_albedoColor;\n"
|
||||
" vec3 N = normalize(vNormal);\n"
|
||||
"\n"
|
||||
" if (u_isGround > 0.5)\n"
|
||||
" {\n"
|
||||
" vec2 uv = vWorldPos.xz;\n"
|
||||
" vec2 gg = abs(fract(uv - 0.5) - 0.5) / fwidth(uv);\n"
|
||||
" float line = min(gg.x, gg.y);\n"
|
||||
" float grid = 1.0 - min(line, 1.0);\n"
|
||||
" albedo = mix(albedo, u_gridColor, grid * 0.7);\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" vec3 color = albedo * u_ambient;\n"
|
||||
"\n"
|
||||
" if (u_lightEnabled > 0.5)\n"
|
||||
" {\n"
|
||||
" vec3 toLight = u_lightPos - vWorldPos;\n"
|
||||
" float dist = length(toLight);\n"
|
||||
" vec3 L = toLight / max(dist, 0.0001);\n"
|
||||
" float diff = max(dot(N, L), 0.0);\n"
|
||||
" float attenuation = 1.0 / (1.0 + 0.14 * dist + 0.07 * dist * dist);\n"
|
||||
" float shadow = shadowFactor();\n"
|
||||
" color += albedo * u_lightColor * diff * attenuation * (1.0 - shadow);\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" color += albedo * u_emissive;\n"
|
||||
"\n"
|
||||
" float fogDist = length(u_cameraPos - vWorldPos);\n"
|
||||
" float fog = clamp((fogDist - u_fogStart) / (u_fogEnd - u_fogStart), 0.0, 1.0);\n"
|
||||
" color = mix(color, u_fogColor, fog);\n"
|
||||
"\n"
|
||||
" finalColor = vec4(color, 1.0);\n"
|
||||
"}\n";
|
||||
|
||||
static const char *SR_SHADOW_VS =
|
||||
"#version 330\n"
|
||||
"layout(location = 0) in vec3 vertexPosition;\n"
|
||||
"uniform mat4 mvp;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" gl_Position = mvp * vec4(vertexPosition, 1.0);\n"
|
||||
"}\n";
|
||||
|
||||
static const char *SR_SHADOW_FS =
|
||||
"#version 330\n"
|
||||
"out vec4 finalColor;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" finalColor = vec4(1.0);\n"
|
||||
"}\n";
|
||||
|
||||
static void sr_load_shaders(const char *main_vs)
|
||||
static void sr_load_shaders(const char *main_vs, const char *main_fs,
|
||||
const char *shadow_vs, const char *shadow_fs)
|
||||
{
|
||||
sr_main_shader = LoadShaderFromMemory(main_vs, SR_MAIN_FS);
|
||||
sr_shadow_shader = LoadShaderFromMemory(SR_SHADOW_VS, SR_SHADOW_FS);
|
||||
sr_main_shader = LoadShaderFromMemory(main_vs, main_fs);
|
||||
sr_shadow_shader = LoadShaderFromMemory(shadow_vs, shadow_fs);
|
||||
|
||||
loc_light_pos = GetShaderLocation(sr_main_shader, "u_lightPos");
|
||||
loc_light_color = GetShaderLocation(sr_main_shader, "u_lightColor");
|
||||
@@ -246,7 +135,8 @@ static void sr_build_ground(void)
|
||||
sr_ground_shadow_material.shader = sr_shadow_shader;
|
||||
}
|
||||
|
||||
void sr_init(int width, int height, const char *main_vs)
|
||||
void sr_init(int width, int height, const char *main_vs, const char *main_fs,
|
||||
const char *shadow_vs, const char *shadow_fs)
|
||||
{
|
||||
if (width > 0) sr_width = width;
|
||||
if (height > 0) sr_height = height;
|
||||
@@ -254,7 +144,7 @@ void sr_init(int width, int height, const char *main_vs)
|
||||
rlLoadExtensions(sr_gl_loader);
|
||||
rlglInit(sr_width, sr_height);
|
||||
|
||||
sr_load_shaders(main_vs);
|
||||
sr_load_shaders(main_vs, main_fs, shadow_vs, shadow_fs);
|
||||
sr_build_ground();
|
||||
|
||||
sr_ready = 1;
|
||||
|
||||
Reference in new issue
Block a user