Add iOS support #4
No files matched your search
@@ -38,7 +38,7 @@ components/ SHARED source-of-truth for all components.
|
|||||||
pixelSize, startTime, timer)
|
pixelSize, startTime, timer)
|
||||||
render/renderConst.swift RENDER_CONFIG (raybri_config), RENDER_FPS,
|
render/renderConst.swift RENDER_CONFIG (raybri_config), RENDER_FPS,
|
||||||
RENDER_MODEL_CUBE, RENDER_OPENGL_ATTRS
|
RENDER_MODEL_CUBE, RENDER_OPENGL_ATTRS
|
||||||
render/renderConstShader.swift RENDER_SHADER_* GLSL sources (GLSL 150)
|
render/renderConstShader.swift Substitutes %VERSION% in the shared templates
|
||||||
render/renderAux.swift renderDraw(vm), renderPixelSize(view)
|
render/renderAux.swift renderDraw(vm), renderPixelSize(view)
|
||||||
render/renderEffect.swift renderInit, renderLoadCubeModel,
|
render/renderEffect.swift renderInit, renderLoadCubeModel,
|
||||||
renderStartTimer, renderDeinit
|
renderStartTimer, renderDeinit
|
||||||
@@ -149,13 +149,21 @@ behind the SwiftUI UI in a `ZStack`. A textured cube is drawn from `Resources/as
|
|||||||
node-rotation glTF animation cannot be played by the renderer — hence the cube is
|
node-rotation glTF animation cannot be played by the renderer — hence the cube is
|
||||||
app-driven.) `packageMac` bundles `Resources/assets/` into the app.
|
app-driven.) `packageMac` bundles `Resources/assets/` into the app.
|
||||||
|
|
||||||
Shaders and render constants live in **Swift**, not in C: GLSL sources are
|
Shaders live in **C** so the macOS and iOS builds share one copy: the GLSL
|
||||||
`RENDER_SHADER_*` in `components/render/renderConstShader.swift`, and
|
bodies are the `RENDER_SHADER_*_T` templates in
|
||||||
`RENDER_CONFIG` / `raybri_config` tunables are in `renderConst.swift`. `raybri_init`
|
`components/raybri/renderShaders.c`, each starting with the `%VERSION%`
|
||||||
compiles the shaders in place (no storage/`strdup`). `cube_normal.png` drives
|
placeholder, and `components/render/renderConstShader.swift` builds the
|
||||||
tangentless normal mapping via the derivative-based `cotangentFrame`. The
|
`RENDER_SHADER_*` sources the macOS side passes to `raybri_init` by replacing
|
||||||
context is OpenGL 3.2 Core (`RENDER_OPENGL_ATTRS`); on Apple Silicon the driver
|
`%VERSION%` with `RENDER_SHADER_VERSION_GL` (`#version 150`); the iOS bridge
|
||||||
still reports 4.1/GLSL 4.10.
|
(`components/render/ios/renderIOS.mm`) substitutes `RENDER_SHADER_VERSION_ES`
|
||||||
|
(`#version 300 es`). The directive must be the first shader token, so it cannot
|
||||||
|
be picked by an `#ifdef` inside the GLSL. Fragment templates also carry
|
||||||
|
`precision highp float;` — required by GLSL ES, accepted and ignored by desktop
|
||||||
|
GLSL. `RENDER_CONFIG` / `raybri_config` tunables stay in `renderConst.swift`.
|
||||||
|
`raybri_init` compiles the shaders in place (no storage/`strdup`).
|
||||||
|
`cube_normal.png` drives tangentless normal mapping via the derivative-based
|
||||||
|
`cotangentFrame`. The context is OpenGL 3.2 Core (`RENDER_OPENGL_ATTRS`); on
|
||||||
|
Apple Silicon the driver still reports 4.1/GLSL 4.10.
|
||||||
|
|
||||||
## Render architecture
|
## Render architecture
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,8 @@ typedef struct raybri_config {
|
|||||||
} raybri_config;
|
} raybri_config;
|
||||||
|
|
||||||
// Initialise the OpenGL-backed raylib scene (no raylib window is created).
|
// Initialise the OpenGL-backed raylib scene (no raylib window is created).
|
||||||
// The four shader sources come from renderConstShader.swift.
|
// The four shader sources are the platform-substituted templates from
|
||||||
|
// renderShaders.c (see renderShaders.h).
|
||||||
void raybri_init(
|
void raybri_init(
|
||||||
const char *main_vs,
|
const char *main_vs,
|
||||||
const char *main_fs,
|
const char *main_fs,
|
||||||
|
|||||||
@@ -0,0 +1,175 @@
|
|||||||
|
#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";
|
||||||
|
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
#ifndef OGS_CHKGFX_RENDER_SHADERS_H
|
||||||
|
#define OGS_CHKGFX_RENDER_SHADERS_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Placeholder that starts every shader template in renderShaders.c.
|
||||||
|
#define RENDER_SHADER_VERSION_TOKEN "%VERSION%"
|
||||||
|
|
||||||
|
// Version directives substituted for the placeholder. A shader's #version
|
||||||
|
// must precede everything else (including "precision"), so it cannot be
|
||||||
|
// chosen with an #ifdef inside the GLSL source; callers do the substitution.
|
||||||
|
extern const char *const RENDER_SHADER_VERSION_GL;
|
||||||
|
extern const char *const RENDER_SHADER_VERSION_ES;
|
||||||
|
|
||||||
|
// Shader templates shared by the macOS and iOS builds. Replace
|
||||||
|
// RENDER_SHADER_VERSION_TOKEN with the platform version directive before
|
||||||
|
// handing them to raybri_init().
|
||||||
|
extern const char *const RENDER_SHADER_MAIN_FS_T;
|
||||||
|
extern const char *const RENDER_SHADER_MAIN_VS_T;
|
||||||
|
extern const char *const RENDER_SHADER_SHADOW_FS_T;
|
||||||
|
extern const char *const RENDER_SHADER_SHADOW_VS_T;
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,157 +0,0 @@
|
|||||||
#ifndef OGS_CHKGFX_RENDER_CONST_SHADER_IOS_H
|
|
||||||
#define OGS_CHKGFX_RENDER_CONST_SHADER_IOS_H
|
|
||||||
|
|
||||||
// OpenGL ES 3.0 (GLSL ES 3.00) shaders for the iOS build. They mirror the
|
|
||||||
// macOS GLSL 150 sources in renderConstShader.swift, with the desktop-only
|
|
||||||
// bits replaced:
|
|
||||||
// - "#version 150" -> "#version 300 es"
|
|
||||||
// - fragment shaders need an explicit float precision qualifier
|
|
||||||
// raylib's LoadShaderFromMemory() passes the code straight to
|
|
||||||
// glShaderSource(), so the version/precision lines must live here.
|
|
||||||
//
|
|
||||||
// They live in C (not Swift) because the iOS bridge
|
|
||||||
// (components/render/ios/renderIOS.mm) feeds them to raybri_init() directly.
|
|
||||||
|
|
||||||
static const char RENDER_SHADER_MAIN_FS[] =
|
|
||||||
"#version 300 es\n"
|
|
||||||
"precision highp float;\n"
|
|
||||||
"in vec3 vWorldPos;\n"
|
|
||||||
"in vec3 vNormal;\n"
|
|
||||||
"in vec2 vTexCoord;\n"
|
|
||||||
"in vec4 vLightClip;\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"
|
|
||||||
"out vec4 finalColor;\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"
|
|
||||||
"float shadowFactor()\n"
|
|
||||||
"{\n"
|
|
||||||
" vec3 proj = vLightClip.xyz / vLightClip.w;\n"
|
|
||||||
" proj = proj * 0.5 + 0.5;\n"
|
|
||||||
" if (!isPixelInShadowMap(proj)) return 0.0;\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"
|
|
||||||
"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"
|
|
||||||
"void main()\n"
|
|
||||||
"{\n"
|
|
||||||
" vec4 texel = texture(texture0, vTexCoord);\n"
|
|
||||||
" vec3 albedo = texel.rgb * u_albedoColor;\n"
|
|
||||||
" vec3 N = normalize(vNormal);\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"
|
|
||||||
" 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"
|
|
||||||
" vec3 color = albedo * u_ambient;\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"
|
|
||||||
" color += albedo * u_emissive;\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"
|
|
||||||
" finalColor = vec4(color, 1.0);\n"
|
|
||||||
"}\n";
|
|
||||||
|
|
||||||
static const char RENDER_SHADER_MAIN_VS[] =
|
|
||||||
"#version 300 es\n"
|
|
||||||
"in vec3 vertexPosition;\n"
|
|
||||||
"in vec2 vertexTexCoord;\n"
|
|
||||||
"in vec3 vertexNormal;\n"
|
|
||||||
"uniform mat4 mvp;\n"
|
|
||||||
"uniform mat4 matModel;\n"
|
|
||||||
"uniform mat4 u_lightMVP;\n"
|
|
||||||
"out vec3 vWorldPos;\n"
|
|
||||||
"out vec3 vNormal;\n"
|
|
||||||
"out vec2 vTexCoord;\n"
|
|
||||||
"out vec4 vLightClip;\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";
|
|
||||||
|
|
||||||
static const char RENDER_SHADER_SHADOW_FS[] =
|
|
||||||
"#version 300 es\n"
|
|
||||||
"precision highp float;\n"
|
|
||||||
"out vec4 finalColor;\n"
|
|
||||||
"void main()\n"
|
|
||||||
"{\n"
|
|
||||||
" finalColor = vec4(1.0);\n"
|
|
||||||
"}\n";
|
|
||||||
|
|
||||||
static const char RENDER_SHADER_SHADOW_VS[] =
|
|
||||||
"#version 300 es\n"
|
|
||||||
"in vec3 vertexPosition;\n"
|
|
||||||
"uniform mat4 mvp;\n"
|
|
||||||
"void main()\n"
|
|
||||||
"{\n"
|
|
||||||
" gl_Position = mvp * vec4(vertexPosition, 1.0);\n"
|
|
||||||
"}\n";
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,15 +1,17 @@
|
|||||||
#import <Foundation/Foundation.h>
|
#import <Foundation/Foundation.h>
|
||||||
#import <UIKit/UIKit.h>
|
#import <UIKit/UIKit.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
#include "raybri.h"
|
#include "raybri.h"
|
||||||
#include "renderConstShaderIOS.h"
|
#include "renderShaders.h"
|
||||||
|
|
||||||
// iOS entry points. The prebuilt raylib.framework (ghera/raylib-iOS) owns
|
// iOS entry points. The prebuilt raylib.framework (ghera/raylib-iOS) owns
|
||||||
// main()/AppDelegate/SceneDelegate and a full-screen EGL/ANGLE window; it
|
// main()/AppDelegate/SceneDelegate and a full-screen EGL/ANGLE window; it
|
||||||
// calls these three callbacks (see src/platforms/rcore_ios.c). We therefore
|
// calls these three callbacks (see src/platforms/rcore_ios.c). We therefore
|
||||||
// drive the same raybri_* scene as the macOS build from here, and present the
|
// drive the same raybri_* scene as the macOS build from here, and present the
|
||||||
// SwiftUI UI as a transparent overlay window stacked above raylib's window.
|
// SwiftUI UI inside raylib's own window (see RenderIOS.swift).
|
||||||
//
|
//
|
||||||
// The prebuilt framework already ran rlLoadExtensions()/rlglInit() inside
|
// The prebuilt framework already ran rlLoadExtensions()/rlglInit() inside
|
||||||
// InitWindow(), so raybri_init() only compiles the shaders and builds meshes.
|
// InitWindow(), so raybri_init() only compiles the shaders and builds meshes.
|
||||||
@@ -20,6 +22,16 @@ extern "C" void rcios_present_overlay(void); // implemented in Swift (RenderIOS.
|
|||||||
void rcios_present_overlay(void);
|
void rcios_present_overlay(void);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// The shader templates in renderShaders.c start with %VERSION%; substitute the
|
||||||
|
// GLSL ES version directive (which must be the first token of the shader).
|
||||||
|
static std::string sr_shader(const char *template_source)
|
||||||
|
{
|
||||||
|
NSString *replaced = [[NSString stringWithUTF8String:template_source]
|
||||||
|
stringByReplacingOccurrencesOfString:[NSString stringWithUTF8String:RENDER_SHADER_VERSION_TOKEN]
|
||||||
|
withString:[NSString stringWithUTF8String:RENDER_SHADER_VERSION_ES]];
|
||||||
|
return std::string([replaced UTF8String]);
|
||||||
|
}
|
||||||
|
|
||||||
static double sr_start_time = 0.0;
|
static double sr_start_time = 0.0;
|
||||||
static int sr_window_ready = 0;
|
static int sr_window_ready = 0;
|
||||||
|
|
||||||
@@ -37,11 +49,15 @@ extern "C" void ios_ready(void)
|
|||||||
|
|
||||||
// On iOS raylib's InitWindow loaded the GL functions and called rlglInit,
|
// On iOS raylib's InitWindow loaded the GL functions and called rlglInit,
|
||||||
// so raybri_init() must not repeat it (see raybri.c, RAYBRI_PLATFORM_IOS).
|
// so raybri_init() must not repeat it (see raybri.c, RAYBRI_PLATFORM_IOS).
|
||||||
|
std::string main_vs = sr_shader(RENDER_SHADER_MAIN_VS_T);
|
||||||
|
std::string main_fs = sr_shader(RENDER_SHADER_MAIN_FS_T);
|
||||||
|
std::string shadow_vs = sr_shader(RENDER_SHADER_SHADOW_VS_T);
|
||||||
|
std::string shadow_fs = sr_shader(RENDER_SHADER_SHADOW_FS_T);
|
||||||
raybri_init(
|
raybri_init(
|
||||||
RENDER_SHADER_MAIN_VS,
|
main_vs.c_str(),
|
||||||
RENDER_SHADER_MAIN_FS,
|
main_fs.c_str(),
|
||||||
RENDER_SHADER_SHADOW_VS,
|
shadow_vs.c_str(),
|
||||||
RENDER_SHADER_SHADOW_FS,
|
shadow_fs.c_str(),
|
||||||
(raybri_config){ 45.0f, 0.1f, 1000.0f, 256, 40.0f }
|
(raybri_config){ 45.0f, 0.1f, 1000.0f, 256, 40.0f }
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -1,162 +1,21 @@
|
|||||||
let RENDER_SHADER_MAIN_FS = """
|
import RaylibBridge
|
||||||
#version 150
|
|
||||||
|
|
||||||
in vec3 vWorldPos;
|
// The GLSL bodies live in components/raybri/renderShaders.c so the macOS and
|
||||||
in vec3 vNormal;
|
// iOS builds share a single copy. Only the #version directive differs, and it
|
||||||
in vec2 vTexCoord;
|
// is substituted here (it must be the first token, so it cannot be selected by
|
||||||
in vec4 vLightClip;
|
// a preprocessor conditional inside the shader).
|
||||||
|
|
||||||
uniform sampler2D texture0;
|
let RENDER_SHADER_MAIN_FS = renderShaderSource(RENDER_SHADER_MAIN_FS_T, RENDER_SHADER_VERSION_GL)
|
||||||
uniform sampler2D u_normalMap;
|
let RENDER_SHADER_MAIN_VS = renderShaderSource(RENDER_SHADER_MAIN_VS_T, RENDER_SHADER_VERSION_GL)
|
||||||
uniform float u_normalStrength;
|
let RENDER_SHADER_SHADOW_FS = renderShaderSource(RENDER_SHADER_SHADOW_FS_T, RENDER_SHADER_VERSION_GL)
|
||||||
uniform sampler2D u_shadowMap;
|
let RENDER_SHADER_SHADOW_VS = renderShaderSource(RENDER_SHADER_SHADOW_VS_T, RENDER_SHADER_VERSION_GL)
|
||||||
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;
|
private func renderShaderSource(
|
||||||
|
_ template: UnsafePointer<CChar>,
|
||||||
bool isPixelInShadowMap(vec3 proj)
|
_ version: UnsafePointer<CChar>
|
||||||
{
|
) -> String {
|
||||||
return proj.x >= 0.0 && proj.x <= 1.0 &&
|
String(cString: template).replacingOccurrences(
|
||||||
proj.y >= 0.0 && proj.y <= 1.0 &&
|
of: RENDER_SHADER_VERSION_TOKEN,
|
||||||
proj.z >= 0.0 && proj.z <= 1.0;
|
with: String(cString: version)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
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_VS = """
|
|
||||||
#version 150
|
|
||||||
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_SHADOW_FS = """
|
|
||||||
#version 150
|
|
||||||
out vec4 finalColor;
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
finalColor = vec4(1.0);
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
|
|
||||||
let RENDER_SHADER_SHADOW_VS = """
|
|
||||||
#version 150
|
|
||||||
in vec3 vertexPosition;
|
|
||||||
uniform mat4 mvp;
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
gl_Position = mvp * vec4(vertexPosition, 1.0);
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
../../../../components/raybri/renderShaders.c
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
../../../../components/raybri/renderShaders.h
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
../../../../components/raybri/renderShaders.c
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
../../../../components/raybri/renderShaders.h
|
||||||
Reference in new issue
Block a user