#include "renderShaders.h" // GLSL bodies shared by the macOS (GLSL 150) and iOS (GLSL ES 300) builds. // The first line is the RENDER_SHADER_VERSION_TOKEN placeholder; callers // substitute it with RENDER_SHADER_VERSION_GL or RENDER_SHADER_VERSION_ES // (a #version directive must precede everything else in a shader, so it // cannot be selected with a preprocessor conditional). // // The fragment bodies carry "precision highp float;" because GLSL ES has no // default float precision in the fragment stage; desktop GLSL 1.30+ accepts // and ignores it. Vertex shaders default to highp in GLSL ES, so they need no // precision statement and stay identical on both platforms. const char *const RENDER_SHADER_VERSION_GL = "#version 150"; const char *const RENDER_SHADER_VERSION_ES = "#version 300 es"; const char *const RENDER_SHADER_MAIN_FS_T = "%VERSION%\n" "precision highp float;\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_normalMap;\n" "uniform float u_normalStrength;\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" "mat3 cotangentFrame(vec3 N, vec3 p, vec2 uv)\n" "{\n" " vec3 dp1 = dFdx(p);\n" " vec3 dp2 = dFdy(p);\n" " vec2 duv1 = dFdx(uv);\n" " vec2 duv2 = dFdy(uv);\n" " vec3 dp2perp = cross(dp2, N);\n" " vec3 dp1perp = cross(N, dp1);\n" " vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;\n" " vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;\n" " float invmax = inversesqrt(max(dot(T, T), dot(B, B)));\n" " return mat3(T * invmax, B * invmax, N);\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_normalStrength > 0.0)\n" " {\n" " vec3 nm = texture(u_normalMap, vTexCoord).rgb * 2.0 - 1.0;\n" " nm.xy *= u_normalStrength;\n" " N = normalize(cotangentFrame(N, vWorldPos, vTexCoord) * nm);\n" " }\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"; const char *const RENDER_SHADER_MAIN_VS_T = "%VERSION%\n" "in vec3 vertexPosition;\n" "in vec2 vertexTexCoord;\n" "in vec3 vertexNormal;\n" "\n" "uniform mat4 mvp;\n" "uniform mat4 matModel;\n" "uniform mat4 u_lightMVP;\n" "\n" "out vec3 vWorldPos;\n" "out vec3 vNormal;\n" "out vec2 vTexCoord;\n" "out vec4 vLightClip;\n" "\n" "void main()\n" "{\n" " vec4 worldPos = matModel * vec4(vertexPosition, 1.0);\n" " vWorldPos = worldPos.xyz;\n" " vNormal = normalize(mat3(matModel) * vertexNormal);\n" " vTexCoord = vertexTexCoord;\n" " vLightClip = u_lightMVP * vec4(vertexPosition, 1.0);\n" " gl_Position = mvp * vec4(vertexPosition, 1.0);\n" "}\n"; const char *const RENDER_SHADER_SHADOW_FS_T = "%VERSION%\n" "precision highp float;\n" "out vec4 finalColor;\n" "void main()\n" "{\n" " finalColor = vec4(1.0);\n" "}\n"; const char *const RENDER_SHADER_SHADOW_VS_T = "%VERSION%\n" "in vec3 vertexPosition;\n" "uniform mat4 mvp;\n" "void main()\n" "{\n" " gl_Position = mvp * vec4(vertexPosition, 1.0);\n" "}\n";