some
This commit is contained in:
1 parent
01594353d8
commit
96a5684709
12 files changed
+262
-250
No files matched your search
@@ -1,21 +1,205 @@
|
||||
import RaylibBridge
|
||||
// GLSL sources shared by the macOS (GLSL 150) and iOS (GLSL ES 300)
|
||||
// builds. A shader's #version directive must precede everything else (even
|
||||
// "precision"), so it cannot be chosen with an #ifdef inside the GLSL; the
|
||||
// templates below start with RENDER_SHADER_VERSION_TOKEN and the platform
|
||||
// version is substituted with replacingOccurrences.
|
||||
//
|
||||
// The fragment templates carry "precision highp float;" because GLSL ES has
|
||||
// no default float precision in the fragment stage; desktop GLSL 1.30+
|
||||
// accepts and ignores it. GLSL ES vertex shaders default to highp, so the
|
||||
// vertex templates need no precision statement.
|
||||
|
||||
// The GLSL bodies live in components/raybri/renderShaders.c so the macOS and
|
||||
// iOS builds share a single copy. Only the #version directive differs, and it
|
||||
// is substituted here (it must be the first token, so it cannot be selected by
|
||||
// a preprocessor conditional inside the shader).
|
||||
let RENDER_SHADER_VERSION_TOKEN = "%VERSION%"
|
||||
let RENDER_SHADER_VERSION_GL = "#version 150"
|
||||
let RENDER_SHADER_VERSION_ES = "#version 300 es"
|
||||
|
||||
let RENDER_SHADER_MAIN_FS = renderShaderSource(RENDER_SHADER_MAIN_FS_T, RENDER_SHADER_VERSION_GL)
|
||||
let RENDER_SHADER_MAIN_VS = renderShaderSource(RENDER_SHADER_MAIN_VS_T, RENDER_SHADER_VERSION_GL)
|
||||
let RENDER_SHADER_SHADOW_FS = renderShaderSource(RENDER_SHADER_SHADOW_FS_T, RENDER_SHADER_VERSION_GL)
|
||||
let RENDER_SHADER_SHADOW_VS = renderShaderSource(RENDER_SHADER_SHADOW_VS_T, RENDER_SHADER_VERSION_GL)
|
||||
#if os(iOS)
|
||||
let RENDER_SHADER_VERSION = RENDER_SHADER_VERSION_ES
|
||||
#else
|
||||
let RENDER_SHADER_VERSION = RENDER_SHADER_VERSION_GL
|
||||
#endif
|
||||
|
||||
private func renderShaderSource(
|
||||
_ template: UnsafePointer<CChar>,
|
||||
_ version: UnsafePointer<CChar>
|
||||
) -> String {
|
||||
String(cString: template).replacingOccurrences(
|
||||
of: RENDER_SHADER_VERSION_TOKEN,
|
||||
with: String(cString: version)
|
||||
)
|
||||
let RENDER_SHADER_MAIN_FS_T = """
|
||||
%VERSION%
|
||||
precision highp float;
|
||||
|
||||
in vec3 vWorldPos;
|
||||
in vec3 vNormal;
|
||||
in vec2 vTexCoord;
|
||||
in vec4 vLightClip;
|
||||
|
||||
uniform sampler2D texture0;
|
||||
uniform sampler2D u_normalMap;
|
||||
uniform float u_normalStrength;
|
||||
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;
|
||||
}
|
||||
|
||||
mat3 cotangentFrame(vec3 N, vec3 p, vec2 uv)
|
||||
{
|
||||
vec3 dp1 = dFdx(p);
|
||||
vec3 dp2 = dFdy(p);
|
||||
vec2 duv1 = dFdx(uv);
|
||||
vec2 duv2 = dFdy(uv);
|
||||
vec3 dp2perp = cross(dp2, N);
|
||||
vec3 dp1perp = cross(N, dp1);
|
||||
vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;
|
||||
vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;
|
||||
float invmax = inversesqrt(max(dot(T, T), dot(B, B)));
|
||||
return mat3(T * invmax, B * invmax, N);
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 texel = texture(texture0, vTexCoord);
|
||||
vec3 albedo = texel.rgb * u_albedoColor;
|
||||
vec3 N = normalize(vNormal);
|
||||
|
||||
if (u_normalStrength > 0.0)
|
||||
{
|
||||
vec3 nm = texture(u_normalMap, vTexCoord).rgb * 2.0 - 1.0;
|
||||
nm.xy *= u_normalStrength;
|
||||
N = normalize(cotangentFrame(N, vWorldPos, vTexCoord) * nm);
|
||||
}
|
||||
|
||||
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 RENDER_SHADER_MAIN_FS = RENDER_SHADER_MAIN_FS_T.replacingOccurrences(
|
||||
of: RENDER_SHADER_VERSION_TOKEN,
|
||||
with: RENDER_SHADER_VERSION
|
||||
)
|
||||
|
||||
let RENDER_SHADER_MAIN_VS_T = """
|
||||
%VERSION%
|
||||
in vec3 vertexPosition;
|
||||
in vec2 vertexTexCoord;
|
||||
in vec3 vertexNormal;
|
||||
|
||||
uniform mat4 mvp;
|
||||
uniform mat4 matModel;
|
||||
uniform mat4 u_lightMVP;
|
||||
|
||||
out vec3 vWorldPos;
|
||||
out vec3 vNormal;
|
||||
out vec2 vTexCoord;
|
||||
out vec4 vLightClip;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 worldPos = matModel * vec4(vertexPosition, 1.0);
|
||||
vWorldPos = worldPos.xyz;
|
||||
vNormal = normalize(mat3(matModel) * vertexNormal);
|
||||
vTexCoord = vertexTexCoord;
|
||||
vLightClip = u_lightMVP * vec4(vertexPosition, 1.0);
|
||||
gl_Position = mvp * vec4(vertexPosition, 1.0);
|
||||
}
|
||||
"""
|
||||
|
||||
let RENDER_SHADER_MAIN_VS = RENDER_SHADER_MAIN_VS_T.replacingOccurrences(
|
||||
of: RENDER_SHADER_VERSION_TOKEN,
|
||||
with: RENDER_SHADER_VERSION
|
||||
)
|
||||
|
||||
let RENDER_SHADER_SHADOW_FS_T = """
|
||||
%VERSION%
|
||||
precision highp float;
|
||||
out vec4 finalColor;
|
||||
void main()
|
||||
{
|
||||
finalColor = vec4(1.0);
|
||||
}
|
||||
"""
|
||||
|
||||
let RENDER_SHADER_SHADOW_FS = RENDER_SHADER_SHADOW_FS_T.replacingOccurrences(
|
||||
of: RENDER_SHADER_VERSION_TOKEN,
|
||||
with: RENDER_SHADER_VERSION
|
||||
)
|
||||
|
||||
let RENDER_SHADER_SHADOW_VS_T = """
|
||||
%VERSION%
|
||||
in vec3 vertexPosition;
|
||||
uniform mat4 mvp;
|
||||
void main()
|
||||
{
|
||||
gl_Position = mvp * vec4(vertexPosition, 1.0);
|
||||
}
|
||||
"""
|
||||
|
||||
let RENDER_SHADER_SHADOW_VS = RENDER_SHADER_SHADOW_VS_T.replacingOccurrences(
|
||||
of: RENDER_SHADER_VERSION_TOKEN,
|
||||
with: RENDER_SHADER_VERSION
|
||||
)
|
||||
Reference in new issue
Block a user