move to const
This commit is contained in:
1 parent
127277e762
commit
b1f0b7f637
4 files changed
+128
-120
No files matched your search
@@ -1,3 +1,98 @@
|
||||
let RAYLIB_SHADER_MAIN_FS = """
|
||||
#version 330
|
||||
|
||||
in vec3 vWorldPos;
|
||||
in vec3 vNormal;
|
||||
in vec2 vTexCoord;
|
||||
in vec4 vLightClip;
|
||||
|
||||
uniform sampler2D texture0;
|
||||
uniform sampler2D u_shadowMap;
|
||||
uniform vec3 u_lightPos;
|
||||
uniform vec3 u_lightColor;
|
||||
uniform vec3 u_ambient;
|
||||
uniform vec3 u_albedoColor;
|
||||
uniform float u_lightEnabled;
|
||||
uniform float u_emissive;
|
||||
uniform float u_shadowBias;
|
||||
uniform float u_shadowSoftness;
|
||||
uniform float u_isGround;
|
||||
uniform vec3 u_gridColor;
|
||||
uniform vec3 u_cameraPos;
|
||||
uniform vec3 u_fogColor;
|
||||
uniform float u_fogStart;
|
||||
uniform float u_fogEnd;
|
||||
|
||||
out vec4 finalColor;
|
||||
|
||||
bool isPixelInShadowMap(vec3 proj)
|
||||
{
|
||||
return proj.x >= 0.0 && proj.x <= 1.0 &&
|
||||
proj.y >= 0.0 && proj.y <= 1.0 &&
|
||||
proj.z >= 0.0 && proj.z <= 1.0;
|
||||
}
|
||||
|
||||
float shadowFactor()
|
||||
{
|
||||
vec3 proj = vLightClip.xyz / vLightClip.w;
|
||||
proj = proj * 0.5 + 0.5;
|
||||
|
||||
if (!isPixelInShadowMap(proj)) return 0.0;
|
||||
|
||||
int radius = int(u_shadowSoftness);
|
||||
vec2 texel = 1.0 / vec2(textureSize(u_shadowMap, 0));
|
||||
float sum = 0.0;
|
||||
float count = 0.0;
|
||||
for (int x = -radius; x <= radius; x++)
|
||||
{
|
||||
for (int y = -radius; y <= radius; y++)
|
||||
{
|
||||
float d = texture(u_shadowMap, proj.xy + vec2(float(x), float(y)) * texel).r;
|
||||
sum += (proj.z - u_shadowBias > d) ? 1.0 : 0.0;
|
||||
count += 1.0;
|
||||
}
|
||||
}
|
||||
return sum / count;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 texel = texture(texture0, vTexCoord);
|
||||
vec3 albedo = texel.rgb * u_albedoColor;
|
||||
vec3 N = normalize(vNormal);
|
||||
|
||||
if (u_isGround > 0.5)
|
||||
{
|
||||
vec2 uv = vWorldPos.xz;
|
||||
vec2 gg = abs(fract(uv - 0.5) - 0.5) / fwidth(uv);
|
||||
float line = min(gg.x, gg.y);
|
||||
float grid = 1.0 - min(line, 1.0);
|
||||
albedo = mix(albedo, u_gridColor, grid * 0.7);
|
||||
}
|
||||
|
||||
vec3 color = albedo * u_ambient;
|
||||
|
||||
if (u_lightEnabled > 0.5)
|
||||
{
|
||||
vec3 toLight = u_lightPos - vWorldPos;
|
||||
float dist = length(toLight);
|
||||
vec3 L = toLight / max(dist, 0.0001);
|
||||
float diff = max(dot(N, L), 0.0);
|
||||
float attenuation = 1.0 / (1.0 + 0.14 * dist + 0.07 * dist * dist);
|
||||
float shadow = shadowFactor();
|
||||
color += albedo * u_lightColor * diff * attenuation * (1.0 - shadow);
|
||||
}
|
||||
|
||||
color += albedo * u_emissive;
|
||||
|
||||
float fogDist = length(u_cameraPos - vWorldPos);
|
||||
float fog = clamp((fogDist - u_fogStart) / (u_fogEnd - u_fogStart), 0.0, 1.0);
|
||||
color = mix(color, u_fogColor, fog);
|
||||
|
||||
finalColor = vec4(color, 1.0);
|
||||
}
|
||||
"""
|
||||
|
||||
let RAYLIB_SHADER_MAIN_VS = """
|
||||
#version 330
|
||||
layout(location = 0) in vec3 vertexPosition;
|
||||
@@ -23,3 +118,22 @@ void main()
|
||||
gl_Position = mvp * vec4(vertexPosition, 1.0);
|
||||
}
|
||||
"""
|
||||
|
||||
let RAYLIB_SHADER_SHADOW_FS = """
|
||||
#version 330
|
||||
out vec4 finalColor;
|
||||
void main()
|
||||
{
|
||||
finalColor = vec4(1.0);
|
||||
}
|
||||
"""
|
||||
|
||||
let RAYLIB_SHADER_SHADOW_VS = """
|
||||
#version 330
|
||||
layout(location = 0) in vec3 vertexPosition;
|
||||
uniform mat4 mvp;
|
||||
void main()
|
||||
{
|
||||
gl_Position = mvp * vec4(vertexPosition, 1.0);
|
||||
}
|
||||
"""
|
||||
@@ -57,7 +57,9 @@ final class RaylibGLNSView: NSOpenGLView {
|
||||
context.setValues(&swapInterval, for: .swapInterval)
|
||||
|
||||
updatePixelSize()
|
||||
sr_init(pixelWidth, pixelHeight, RAYLIB_SHADER_MAIN_VS)
|
||||
sr_init(pixelWidth, pixelHeight,
|
||||
RAYLIB_SHADER_MAIN_VS, RAYLIB_SHADER_MAIN_FS,
|
||||
RAYLIB_SHADER_SHADOW_VS, RAYLIB_SHADER_SHADOW_FS)
|
||||
loadModel()
|
||||
startTime = CACurrentMediaTime()
|
||||
ready = true
|
||||
|
||||
@@ -6,8 +6,10 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
// Initialise the OpenGL-backed raylib scene (no raylib window is created).
|
||||
// main_vs is the main vertex shader source (see raylibConstShader.swift).
|
||||
void sr_init(int width, int height, const char *main_vs);
|
||||
// The four shader sources come from raylibConstShader.swift.
|
||||
void sr_init(int width, int height,
|
||||
const char *main_vs, const char *main_fs,
|
||||
const char *shadow_vs, const char *shadow_fs);
|
||||
|
||||
// Load the textured cube glTF model.
|
||||
void sr_load_model(const char *gltf_path);
|
||||
|
||||
@@ -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