some
This commit is contained in:
1 parent
01594353d8
commit
96a5684709
12 files changed
+262
-250
No files matched your search
@@ -149,17 +149,16 @@ 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 live in **C** so the macOS and iOS builds share one copy: the GLSL
|
Shaders live in **Swift** and are shared by both builds: the GLSL bodies are
|
||||||
bodies are the `RENDER_SHADER_*_T` templates in
|
the `RENDER_SHADER_*_T` templates in
|
||||||
`components/raybri/renderShaders.c`, each starting with the `%VERSION%`
|
`components/render/renderConstShader.swift`, each starting with the `%VERSION%`
|
||||||
placeholder, and `components/render/renderConstShader.swift` builds the
|
placeholder. The `RENDER_SHADER_*` sources passed to `raybri_init` are produced
|
||||||
`RENDER_SHADER_*` sources the macOS side passes to `raybri_init` by replacing
|
by `replacingOccurrences`, choosing `RENDER_SHADER_VERSION_GL` (`#version 150`)
|
||||||
`%VERSION%` with `RENDER_SHADER_VERSION_GL` (`#version 150`); the iOS bridge
|
or `RENDER_SHADER_VERSION_ES` (`#version 300 es`) via `#if os(iOS)`. The
|
||||||
(`components/render/ios/renderIOS.mm`) substitutes `RENDER_SHADER_VERSION_ES`
|
directive must be the first shader token, so it cannot be picked by an `#ifdef`
|
||||||
(`#version 300 es`). The directive must be the first shader token, so it cannot
|
inside the GLSL. Fragment templates also carry `precision highp float;` —
|
||||||
be picked by an `#ifdef` inside the GLSL. Fragment templates also carry
|
required by GLSL ES, accepted and ignored by desktop GLSL. The iOS ObjC++ bridge
|
||||||
`precision highp float;` — required by GLSL ES, accepted and ignored by desktop
|
reaches the strings through `@_cdecl` accessors in `RenderIOS.swift`. `RENDER_CONFIG` / `raybri_config` tunables stay in `renderConst.swift`.
|
||||||
GLSL. `RENDER_CONFIG` / `raybri_config` tunables stay in `renderConst.swift`.
|
|
||||||
`raybri_init` compiles the shaders in place (no storage/`strdup`).
|
`raybri_init` compiles the shaders in place (no storage/`strdup`).
|
||||||
`cube_normal.png` drives tangentless normal mapping via the derivative-based
|
`cube_normal.png` drives tangentless normal mapping via the derivative-based
|
||||||
`cotangentFrame`. The context is OpenGL 3.2 Core (`RENDER_OPENGL_ATTRS`); on
|
`cotangentFrame`. The context is OpenGL 3.2 Core (`RENDER_OPENGL_ATTRS`); on
|
||||||
|
|||||||
@@ -15,8 +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 are the platform-substituted templates from
|
// The four shader sources come from renderConstShader.swift (with the
|
||||||
// renderShaders.c (see renderShaders.h).
|
// platform #version already substituted).
|
||||||
void raybri_init(
|
void raybri_init(
|
||||||
const char *main_vs,
|
const char *main_vs,
|
||||||
const char *main_fs,
|
const char *main_fs,
|
||||||
|
|||||||
@@ -1,175 +0,0 @@
|
|||||||
#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";
|
|
||||||
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
#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
|
|
||||||
@@ -26,6 +26,30 @@ enum RenderIOS {
|
|||||||
static let renderVM = RenderVM()
|
static let renderVM = RenderVM()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The GLSL sources live in renderConstShader.swift (shared with the macOS
|
||||||
|
// build, with the platform #version already substituted). Expose them to the
|
||||||
|
// ObjC++ bridge as C strings; each call returns a malloc'd copy the caller
|
||||||
|
// must free().
|
||||||
|
@_cdecl("rcios_shader_main_vs")
|
||||||
|
public func rciosShaderMainVS() -> UnsafeMutablePointer<CChar>? {
|
||||||
|
strdup(RENDER_SHADER_MAIN_VS)
|
||||||
|
}
|
||||||
|
|
||||||
|
@_cdecl("rcios_shader_main_fs")
|
||||||
|
public func rciosShaderMainFS() -> UnsafeMutablePointer<CChar>? {
|
||||||
|
strdup(RENDER_SHADER_MAIN_FS)
|
||||||
|
}
|
||||||
|
|
||||||
|
@_cdecl("rcios_shader_shadow_vs")
|
||||||
|
public func rciosShaderShadowVS() -> UnsafeMutablePointer<CChar>? {
|
||||||
|
strdup(RENDER_SHADER_SHADOW_VS)
|
||||||
|
}
|
||||||
|
|
||||||
|
@_cdecl("rcios_shader_shadow_fs")
|
||||||
|
public func rciosShaderShadowFS() -> UnsafeMutablePointer<CChar>? {
|
||||||
|
strdup(RENDER_SHADER_SHADOW_FS)
|
||||||
|
}
|
||||||
|
|
||||||
@_cdecl("rcios_present_overlay")
|
@_cdecl("rcios_present_overlay")
|
||||||
public func rciosPresentOverlay() {
|
public func rciosPresentOverlay() {
|
||||||
otherSetupConsoleLogging(RenderIOS.chkgfx.ctrl, "Chkgfx")
|
otherSetupConsoleLogging(RenderIOS.chkgfx.ctrl, "Chkgfx")
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
#import <Foundation/Foundation.h>
|
#import <Foundation/Foundation.h>
|
||||||
#import <UIKit/UIKit.h>
|
#import <UIKit/UIKit.h>
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
#include "raybri.h"
|
#include "raybri.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
|
||||||
@@ -22,14 +22,26 @@ 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
|
// The GLSL sources live in renderConstShader.swift (shared with the macOS
|
||||||
// GLSL ES version directive (which must be the first token of the shader).
|
// build, with the platform #version already substituted there). These Swift
|
||||||
static std::string sr_shader(const char *template_source)
|
// accessors return malloc'd copies that sr_shader() releases.
|
||||||
|
#if defined(__cplusplus)
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
char *rcios_shader_main_vs(void);
|
||||||
|
char *rcios_shader_main_fs(void);
|
||||||
|
char *rcios_shader_shadow_vs(void);
|
||||||
|
char *rcios_shader_shadow_fs(void);
|
||||||
|
#if defined(__cplusplus)
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static std::string sr_shader(char *(*accessor)(void))
|
||||||
{
|
{
|
||||||
NSString *replaced = [[NSString stringWithUTF8String:template_source]
|
char *source = accessor();
|
||||||
stringByReplacingOccurrencesOfString:[NSString stringWithUTF8String:RENDER_SHADER_VERSION_TOKEN]
|
std::string shader = source ? source : "";
|
||||||
withString:[NSString stringWithUTF8String:RENDER_SHADER_VERSION_ES]];
|
free(source);
|
||||||
return std::string([replaced UTF8String]);
|
return shader;
|
||||||
}
|
}
|
||||||
|
|
||||||
static double sr_start_time = 0.0;
|
static double sr_start_time = 0.0;
|
||||||
@@ -49,10 +61,10 @@ 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_vs = sr_shader(rcios_shader_main_vs);
|
||||||
std::string main_fs = sr_shader(RENDER_SHADER_MAIN_FS_T);
|
std::string main_fs = sr_shader(rcios_shader_main_fs);
|
||||||
std::string shadow_vs = sr_shader(RENDER_SHADER_SHADOW_VS_T);
|
std::string shadow_vs = sr_shader(rcios_shader_shadow_vs);
|
||||||
std::string shadow_fs = sr_shader(RENDER_SHADER_SHADOW_FS_T);
|
std::string shadow_fs = sr_shader(rcios_shader_shadow_fs);
|
||||||
raybri_init(
|
raybri_init(
|
||||||
main_vs.c_str(),
|
main_vs.c_str(),
|
||||||
main_fs.c_str(),
|
main_fs.c_str(),
|
||||||
|
|||||||
@@ -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
|
let RENDER_SHADER_VERSION_TOKEN = "%VERSION%"
|
||||||
// iOS builds share a single copy. Only the #version directive differs, and it
|
let RENDER_SHADER_VERSION_GL = "#version 150"
|
||||||
// is substituted here (it must be the first token, so it cannot be selected by
|
let RENDER_SHADER_VERSION_ES = "#version 300 es"
|
||||||
// a preprocessor conditional inside the shader).
|
|
||||||
|
|
||||||
let RENDER_SHADER_MAIN_FS = renderShaderSource(RENDER_SHADER_MAIN_FS_T, RENDER_SHADER_VERSION_GL)
|
#if os(iOS)
|
||||||
let RENDER_SHADER_MAIN_VS = renderShaderSource(RENDER_SHADER_MAIN_VS_T, RENDER_SHADER_VERSION_GL)
|
let RENDER_SHADER_VERSION = RENDER_SHADER_VERSION_ES
|
||||||
let RENDER_SHADER_SHADOW_FS = renderShaderSource(RENDER_SHADER_SHADOW_FS_T, RENDER_SHADER_VERSION_GL)
|
#else
|
||||||
let RENDER_SHADER_SHADOW_VS = renderShaderSource(RENDER_SHADER_SHADOW_VS_T, RENDER_SHADER_VERSION_GL)
|
let RENDER_SHADER_VERSION = RENDER_SHADER_VERSION_GL
|
||||||
|
#endif
|
||||||
|
|
||||||
private func renderShaderSource(
|
let RENDER_SHADER_MAIN_FS_T = """
|
||||||
_ template: UnsafePointer<CChar>,
|
%VERSION%
|
||||||
_ version: UnsafePointer<CChar>
|
precision highp float;
|
||||||
) -> String {
|
|
||||||
String(cString: template).replacingOccurrences(
|
in vec3 vWorldPos;
|
||||||
of: RENDER_SHADER_VERSION_TOKEN,
|
in vec3 vNormal;
|
||||||
with: String(cString: version)
|
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
|
||||||
|
)
|
||||||
@@ -10,6 +10,7 @@ targets:
|
|||||||
- path: Assets.xcassets
|
- path: Assets.xcassets
|
||||||
- path: src
|
- path: src
|
||||||
- path: ../../components/render/ios
|
- path: ../../components/render/ios
|
||||||
|
- path: ../../components/render/renderConstShader.swift
|
||||||
- path: ../../components/raybri
|
- path: ../../components/raybri
|
||||||
- path: ../../components/other
|
- path: ../../components/other
|
||||||
- path: ../../components/chkgfx/chkgfx.swift
|
- path: ../../components/chkgfx/chkgfx.swift
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
../../../../components/raybri/renderShaders.c
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../../components/raybri/renderShaders.h
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../../components/raybri/renderShaders.c
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../../components/raybri/renderShaders.h
|
|
||||||
Reference in new issue
Block a user