v4k-git-backup/engine/split/v4k_shaders.c

697 lines
25 KiB
C
Raw Normal View History

2023-08-10 21:53:51 +00:00
static const char *const fs_0_0_shadowmap_lit = "//" FILELINE "\n"
2023-08-10 22:46:04 +00:00
"// uniform mat4 view = mat4(1.0);\n"
2023-08-10 21:53:51 +00:00
"uniform vec3 lightPos = vec3(1.0);\n"
"uniform float doTexture = 1.;\n"
#if VSMCUBE
2023-08-10 22:46:04 +00:00
"uniform samplerCube shadowMap; // VSMCUBE\n"
2023-08-10 21:53:51 +00:00
#else
2023-08-10 22:46:04 +00:00
"uniform sampler2D shadowMap; // !VSMCUBE\n"
2023-08-10 21:53:51 +00:00
#endif
"\n"
"struct light {\n"
2023-08-10 22:46:04 +00:00
" vec3 position; // world-space\n"
2023-08-10 21:53:51 +00:00
" vec4 diffuse;\n"
" vec4 specular;\n"
" float constantAttenuation, linearAttenuation, quadraticAttenuation;\n"
" };\n"
" light light0 = light(\n"
" lightPos,\n"
2023-08-10 22:46:04 +00:00
" vec4(1,1,1,1), // diffuse\n"
" vec4(1,1,1,1), // specular\n"
" 1.0, 0.0, 0.0 // attenuation (const, linear, quad)\n"
2023-08-10 21:53:51 +00:00
" );\n"
" \n"
2023-08-10 22:46:04 +00:00
" // From http://fabiensanglard.net/shadowmappingVSM/index.php\n"
2023-08-10 21:53:51 +00:00
#if VSMCUBE
" float chebyshevUpperBound(float distance, vec3 dir) {\n"
" distance = distance/20 ;\n"
" vec2 moments = texture(shadowMap, dir).rg;\n"
#else
" float chebyshevUpperBound(float distance, vec4 scPostW) {\n"
" vec2 moments = texture(shadowMap,scPostW.xy).rg;\n"
#endif
2023-08-10 22:46:04 +00:00
" // Surface is fully lit. as the current fragment is before the light occluder\n"
2023-08-10 21:53:51 +00:00
" if (distance <= moments.x)\n"
" return 1.0;\n"
" \n"
2023-08-10 22:46:04 +00:00
" // The fragment is either in shadow or penumbra. We now use chebyshev's upperBound to check\n"
" // How likely this pixel is to be lit (p_max)\n"
2023-08-10 21:53:51 +00:00
" float variance = moments.y - (moments.x*moments.x);\n"
2023-08-10 22:46:04 +00:00
" //variance = max(variance, 0.000002);\n"
2023-08-10 21:53:51 +00:00
" variance = max(variance, 0.00002);\n"
" \n"
" float d = distance - moments.x;\n"
" float p_max = variance / (variance + d*d);\n"
" \n"
" return p_max;\n"
" }\n"
" \n"
" vec4 shadowmap(in vec4 vpeye, in vec4 vneye, in vec2 uv, in vec4 sc) {\n"
#ifndef VSMCUBE
" return vec4(1.);\n"
#else
" \n"
" vec3 fragment = vec3(vpeye);\n"
" vec3 normal = vec3(normalize(vneye));\n"
" vec3 viewDir = normalize(-fragment);\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Lighting\n"
" // Convert to eye-space\n"
2023-08-10 21:53:51 +00:00
" vec3 light = vec3(view * vec4(light0.position, 1.0));\n"
" \n"
#if VSMCUBE
2023-08-10 22:46:04 +00:00
" // Vectors\n"
2023-08-10 21:53:51 +00:00
" vec3 fragmentToLight = light - fragment;\n"
" vec3 fragmentToLightDir = normalize(fragmentToLight);\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Shadows\n"
2023-08-10 21:53:51 +00:00
" vec4 fragmentToLight_world = inverse(view) * vec4(fragmentToLightDir, 0.0);\n"
" float shadowFactor = chebyshevUpperBound(length(fragmentToLight), -fragmentToLight_world.xyz);\n"
#else
2023-08-10 22:46:04 +00:00
" // Shadows\n"
2023-08-10 21:53:51 +00:00
" vec4 scPostW = sc / sc.w;\n"
" scPostW = scPostW * 0.5 + 0.5;\n"
" \n"
2023-08-10 22:46:04 +00:00
" float shadowFactor = 1.0; // Not in shadow\n"
2023-08-10 21:53:51 +00:00
" \n"
" bool outsideShadowMap = sc.w <= 0.0f || (scPostW.x < 0 || scPostW.y < 0) || (scPostW.x >= 1 || scPostW.y >= 1);\n"
" if (!outsideShadowMap) {\n"
" shadowFactor = chebyshevUpperBound(scPostW.z, scPostW);\n"
" }\n"
#endif
" \n"
" vec4 diffColor = vec4(1,1,1,1);\n"
#if VSMCUBE
" if(doTexture != 0) diffColor = vec4(vec3(texture(shadowMap, -fragmentToLight_world.xyz).r), 1.0);\n"
#else
" if(doTexture != 0) diffColor = vec4(vec3(texture(shadowMap, vec2(uv.x, 1.0 - uv.y)).r), 1.0);\n"
#endif
" \n"
#if 1
" vec3 positionToLight = light - fragment;\n"
" vec3 lightDir = normalize(positionToLight);\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Angle between fragment-normal and incoming light\n"
2023-08-10 21:53:51 +00:00
" float cosAngIncidence = dot(lightDir, normal);\n"
" cosAngIncidence = clamp(cosAngIncidence, 0, 1);\n"
" \n"
" float attenuation = 1.0f;\n"
" attenuation = 1.0 / (light0.constantAttenuation + light0.linearAttenuation * length(positionToLight) + light0.quadraticAttenuation * pow(length(positionToLight),2));\n"
" \n"
" vec4 diffuse = diffColor * light0.diffuse * cosAngIncidence * attenuation;\n"
" \n"
" vec4 total_lighting;\n"
2023-08-10 22:46:04 +00:00
" total_lighting += vec4(0.1, 0.1, 0.1, 1.0) * diffColor; // Ambient\n"
" total_lighting += diffuse * shadowFactor; // Diffuse\n"
2023-08-10 21:53:51 +00:00
#else
" vec4 total_lighting = diffColor;\n"
#endif
" return vec4(clamp(vec3(total_lighting), 0., 1.), 1.0);\n"
#endif
" }\n";
static const char *const fs_0_0_shadowmap_unlit = "//" FILELINE "\n"
2023-08-10 22:46:04 +00:00
"// uniform mat4 view = mat4(1.0);\n"
2023-08-10 21:53:51 +00:00
"uniform vec3 lightPos = vec3(1.0);\n"
"uniform float doTexture = 0.;\n"
"uniform sampler2D shadowMap;\n"
"\n"
"vec4 shadowmap(in vec4 vpeye, in vec4 vneye, in vec2 Texcoord, in vec4 sc) {\n"
" return vec4(1.);\n"
" };\n";
static const char *const fs_24_4_sprite = "//" FILELINE "\n"
"uniform sampler2D u_texture;\n"
"\n"
"in vec2 vTexCoord;\n"
"in vec4 vColor;\n"
"out vec4 fragColor;\n"
"\n"
2023-08-10 22:46:04 +00:00
"// [src] https://www.shadertoy.com/view/MllBWf CC1.0\n"
2023-08-10 21:53:51 +00:00
"vec4 texture_AA(sampler2D tx, vec2 uv) {\n"
" vec2 res = vec2(textureSize(tx, 0));\n"
" uv = uv*res + 0.5;\n"
2023-08-10 22:46:04 +00:00
" // tweak fractionnal value of the texture coordinate\n"
2023-08-10 21:53:51 +00:00
" vec2 fl = floor(uv);\n"
" vec2 fr = fract(uv);\n"
" vec2 aa = fwidth(uv)*0.75;\n"
" fr = smoothstep( vec2(0.5)-aa, vec2(0.5)+aa, fr);\n"
2023-08-10 22:46:04 +00:00
" // return value\n"
2023-08-10 21:53:51 +00:00
" uv = (fl+fr-0.5) / res;\n"
" return texture(tx, uv);\n"
"}\n"
"\n"
2023-08-10 22:46:04 +00:00
"// [src] https://www.shadertoy.com/view/MllBWf CC1.0\n"
2023-08-10 21:53:51 +00:00
"vec4 texture_AA2( sampler2D tex, vec2 uv) {\n"
" vec2 res = vec2(textureSize(tex,0));\n"
" uv = uv*res;\n"
" vec2 seam = floor(uv+0.5);\n"
" uv = seam + clamp( (uv-seam)/fwidth(uv), -0.5, 0.5);\n"
" return texture(tex, uv/res);\n"
"}\n"
"\n"
2023-08-10 22:46:04 +00:00
"// [src] https://www.shadertoy.com/view/ltBfRD\n"
2023-08-10 21:53:51 +00:00
"vec4 texture_AA3(sampler2D tex, vec2 uv) {\n"
" vec2 res = vec2(textureSize(tex,0));\n"
" float width = 2.0;\n"
" uv = uv * res;\n"
2023-08-10 22:46:04 +00:00
" // ---\n"
2023-08-10 21:53:51 +00:00
" vec2 uv_floor = floor(uv + 0.5);\n"
" vec2 uv_fract = fract(uv + 0.5);\n"
" vec2 uv_aa = fwidth(uv) * width * 0.5;\n"
" uv_fract = smoothstep(\n"
" vec2(0.5) - uv_aa,\n"
" vec2(0.5) + uv_aa,\n"
" uv_fract\n"
" );\n"
" uv = (uv_floor + uv_fract - 0.5) / res;\n"
" return texture(tex, uv);\n"
"}\n"
"\n"
"void main() {\n"
" vec4 texColor = texture_AA2(u_texture, vTexCoord);\n"
" if(texColor.a < 0.9) discard;\n"
" fragColor = vColor * texColor;\n"
"}\n";
static const char *const fs_2_4_preamble = "//" FILELINE "\n"
"#define texture2D texture\n"
"#define texture2DLod textureLod\n"
"#define FRAGCOLOR fragColor\n"
"#define texcoord uv\n"
"#define TEXCOORD uv\n"
"uniform sampler2D iChannel0;\n"
"uniform sampler2D iChannel1;\n"
"uniform float iWidth, iHeight, iTime, iFrame, iMousex, iMousey;\n"
"uniform float iChannelRes0x, iChannelRes0y;\n"
"uniform float iChannelRes1x, iChannelRes1y;\n"
"vec2 iResolution = vec2(iWidth, iHeight);\n"
"vec2 iMouse = vec2(iMousex, iMousey);\n"
"vec2 iChannelResolution[2] = vec2[2]( vec2(iChannelRes0x, iChannelRes0y),vec2(iChannelRes1x, iChannelRes1y) );\n"
"float iGlobalTime = iTime;\n"
"in vec2 texcoord;\n"
"out vec4 fragColor;\n";
static const char *const fs_2_4_texel_inv_gamma = "//" FILELINE "\n"
2023-08-10 22:46:04 +00:00
"uniform sampler2D texture0; /*unit0*/\n"
2023-08-10 21:53:51 +00:00
"uniform float u_inv_gamma;\n"
"\n"
"in vec2 uv;\n"
"out vec4 fragcolor;\n"
"\n"
"void main() {\n"
" vec4 texel = texture( texture0, uv );\n"
" fragcolor = texel;\n"
2023-08-10 22:46:04 +00:00
" fragcolor.rgb = pow( fragcolor.rgb, vec3( u_inv_gamma ) ); // defaults: 1.0/2.2 gamma\n"
2023-08-10 21:53:51 +00:00
"}\n";
static const char *const fs_2_4_texel_ycbr_gamma_saturation = "//" FILELINE "\n"
2023-08-10 22:46:04 +00:00
"uniform sampler2D u_texture_y; /*unit0*/\n"
"uniform sampler2D u_texture_cb; /*unit1*/\n"
"uniform sampler2D u_texture_cr; /*unit2*/\n"
2023-08-10 21:53:51 +00:00
"uniform float u_gamma;\n"
"\n"
"in vec2 uv;\n"
"out vec4 fragcolor;\n"
"\n"
"void main() {\n"
" float y = texture(u_texture_y, uv).r;\n"
" float cb = texture(u_texture_cb, uv).r;\n"
" float cr = texture(u_texture_cr, uv).r;\n"
" \n"
" const mat4 to_rgb = mat4(\n"
" 1.0000, 1.0000, 1.0000, 0.0000,\n"
" 0.0000, -0.3441, 1.7720, 0.0000,\n"
" 1.4020, -0.7141, 0.0000, 0.0000,\n"
" -0.7010, 0.5291, -0.8860, 1.0000\n"
" );\n"
" vec4 texel = to_rgb * vec4(y, cb, cr, 1.0);\n"
2023-08-10 22:46:04 +00:00
" /* same as:\n"
" vec3 yCbCr = vec3(y,cb-0.5,cr-0.5);\n"
" vec4 texel = vec4( dot( vec3( 1.0, 0.0, 1.402 ), yCbCr ),\n"
" dot( vec3( 1.0 , -0.34414 , -0.71414 ), yCbCr ),\n"
" dot( vec3( 1.0, 1.772, 0.0 ), yCbCr ), 1.0);\n"
" */\n"
" // gamma correction\n"
2023-08-10 21:53:51 +00:00
" texel.rgb = pow(texel.rgb, vec3(1.0 / u_gamma));\n"
" \n"
2023-08-10 22:46:04 +00:00
" // saturation (algorithm from Chapter 16 of OpenGL Shading Language)\n"
2023-08-10 21:53:51 +00:00
" if(false) { float saturation = 2.0; const vec3 W = vec3(0.2125, 0.7154, 0.0721);\n"
" vec3 intensity = vec3(dot(texel.rgb, W));\n"
"texel.rgb = mix(intensity, texel.rgb, saturation); }\n"
"\n"
"fragcolor = vec4(texel.rgb, 1.0);\n"
"}\n";
static const char *const fs_32_4_model = "//" FILELINE "\n"
"uniform mat4 model, view;\n"
"uniform sampler2D u_texture2d;\n"
"uniform vec3 u_coefficients_sh[9];\n"
"uniform bool u_textured = true;\n"
"uniform bool u_lit = false;\n"
"uniform bool u_matcaps = false;\n"
"uniform vec4 u_diffuse = vec4(1.0,1.0,1.0,1.0);\n"
"\n"
#ifdef RIM
"in vec3 v_position;\n"
#endif
"in vec3 v_normal, v_normal_ws;\n"
"in vec2 v_texcoord;\n"
"in vec4 v_color;\n"
"out vec4 fragcolor;\n"
"\n"
"{{include-shadowmap}}\n"
"in vec4 vpeye;\n"
"in vec4 vneye;\n"
"in vec4 sc;\n"
"vec4 shadowing() {\n"
"return shadowmap(vpeye, vneye, v_texcoord, sc);\n"
"}\n"
"\n"
"void main() {\n"
2023-08-10 22:46:04 +00:00
"vec3 n = /*normalize*/(v_normal);\n"
2023-08-10 21:53:51 +00:00
"\n"
2023-08-10 22:46:04 +00:00
"// SH lighting\n"
2023-08-10 21:53:51 +00:00
"vec4 lit = vec4(1.0, 1.0, 1.0, 1.0);\n"
"vec3 SHLightResult[9];\n"
"SHLightResult[0] = 0.282095f * u_coefficients_sh[0];\n"
"SHLightResult[1] = -0.488603f * u_coefficients_sh[1] * n.y;\n"
"SHLightResult[2] = 0.488603f * u_coefficients_sh[2] * n.z;\n"
"SHLightResult[3] = -0.488603f * u_coefficients_sh[3] * n.x;\n"
"SHLightResult[4] = 1.092548f * u_coefficients_sh[4] * n.x * n.y;\n"
"SHLightResult[5] = -1.092548f * u_coefficients_sh[5] * n.y * n.z;\n"
"SHLightResult[6] = 0.315392f * u_coefficients_sh[6] * (3.0f * n.z * n.z - 1.0f);\n"
"SHLightResult[7] = -1.092548f * u_coefficients_sh[7] * n.x * n.z;\n"
"SHLightResult[8] = 0.546274f * u_coefficients_sh[8] * (n.x * n.x - n.y * n.y);\n"
"vec3 result = vec3(0.0);\n"
"for (int i = 0; i < 9; ++i)\n"
"result += SHLightResult[i];\n"
"if( (result.x*result.x+result.y*result.y+result.z*result.z) > 0.0 ) lit = vec4(result, 1.0);\n"
"\n"
"\n"
"\n"
2023-08-10 22:46:04 +00:00
"// base\n"
2023-08-10 21:53:51 +00:00
"vec4 diffuse;\n"
"if(u_matcaps) {\n"
2023-08-10 22:46:04 +00:00
" vec2 muv = vec2(view * vec4(v_normal_ws, 0))*0.5+vec2(0.5,0.5); // normal (model space) to view space\n"
2023-08-10 21:53:51 +00:00
" diffuse = texture(u_texture2d, vec2(muv.x, 1.0-muv.y));\n"
" } else if(u_textured) {\n"
" diffuse = texture(u_texture2d, v_texcoord);\n"
" } else {\n"
2023-08-10 22:46:04 +00:00
" diffuse = u_diffuse; // * v_color;\n"
2023-08-10 21:53:51 +00:00
" }\n"
" \n"
2023-08-10 22:46:04 +00:00
" // lighting mix\n"
2023-08-10 21:53:51 +00:00
" fragcolor = diffuse * lit * shadowing();\n"
" \n"
2023-08-10 22:46:04 +00:00
" // rimlight\n"
2023-08-10 21:53:51 +00:00
#ifdef RIM
2023-08-10 22:46:04 +00:00
" {vec3 n = normalize(mat3(M) * v_normal); // convert normal to view space\n"
" vec3 p = (M * vec4(v_position,1.0)).xyz; // convert position to view space\n"
" vec3 v = normalize(-p); // eye vector\n"
" float rim = 1.0 - max(dot(v, n), 0.0); // rimlight\n"
" rim = smoothstep(1.0-0.01, 1.0, rim); // intensity (0.01)\n"
" fragcolor += vec4(0.0, 0.0, rim, 1.0);} // blue\n"
#endif
" \n"
" }\n";
2023-08-10 21:53:51 +00:00
static const char *const fs_32_4_model_basic = "//" FILELINE "\n"
"uniform sampler2D fsDiffTex;\n"
"uniform sampler2D fsNormalTex;\n"
"uniform sampler2D fsPositionTex;\n"
"uniform mat4 MVP;\n"
"\n"
"in vec3 v_normal;\n"
"in vec2 v_texcoord;\n"
"out vec4 fragColor;\n"
"\n"
"void main() {\n"
" vec4 diff = texture(fsDiffTex, v_texcoord).rgba;\n"
2023-08-10 22:46:04 +00:00
" vec3 n = normalize(mat3(MVP) * v_normal); // transform normal to eye space\n"
" fragColor = diff;// * vec4(v_normal.xyz, 1);\n"
2023-08-10 21:53:51 +00:00
"}\n";
static const char *const fs_3_4_skybox = "//" FILELINE "\n"
"uniform samplerCube u_cubemap;\n"
"\n"
"in vec3 v_direction;\n"
"out vec4 fragcolor;\n"
"\n"
"void main() {\n"
" fragcolor = vec4(texture(u_cubemap, v_direction).rgb, 1.0);\n"
"}\n";
static const char *const fs_3_4_skybox_rayleigh = "//" FILELINE "\n"
2023-08-10 22:46:04 +00:00
"uniform vec3 uSunPos = vec3( 0, 0.1, -1 ); // = [0, Math.cos(theta) * 0.3 + 0.2, -1];\n"
2023-08-10 21:53:51 +00:00
"\n"
"in vec3 v_direction;\n"
"out vec4 fragcolor;\n"
"\n"
"vec3 atmosphere(vec3 r, vec3 r0, vec3 pSun, float iSun, float rPlanet, float rAtmos, vec3 kRlh, float kMie, float shRlh, float shMie, float g);\n"
"\n"
"void main() {\n"
" vec3 color = atmosphere(\n"
2023-08-10 22:46:04 +00:00
" normalize(v_direction), // normalized ray direction\n"
" vec3(0,6372e3,0), // ray origin\n"
" uSunPos, // position of the sun\n"
" 22.0, // intensity of the sun\n"
" 6371e3, // radius of the planet in meters\n"
" 6471e3, // radius of the atmosphere in meters\n"
" vec3(5.5e-6, 13.0e-6, 22.4e-6), // Rayleigh scattering coefficient\n"
" 21e-6, // Mie scattering coefficient\n"
" 8e3, // Rayleigh scale height\n"
" 1.2e3, // Mie scale height\n"
" 0.758 // Mie preferred scattering direction\n"
2023-08-10 21:53:51 +00:00
" );\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Apply exposure.\n"
2023-08-10 21:53:51 +00:00
" color = 1.0 - exp(-1.0 * color);\n"
" \n"
" fragcolor = vec4(color, 1);\n"
"}\n"
"\n"
2023-08-10 22:46:04 +00:00
"// [src] https://github.com/wwwtyro/glsl-atmosphere by wwwtyro (Unlicensed)\n"
"// For more information, please refer to <http://unlicense.org>\n"
2023-08-10 21:53:51 +00:00
"\n"
"#define PI 3.141592\n"
"#define iSteps 16\n"
"#define jSteps 8\n"
"\n"
"vec2 rsi(vec3 r0, vec3 rd, float sr) {\n"
2023-08-10 22:46:04 +00:00
" // ray-sphere intersection that assumes\n"
" // the sphere is centered at the origin.\n"
" // No intersection when result.x > result.y\n"
2023-08-10 21:53:51 +00:00
" float a = dot(rd, rd);\n"
" float b = 2.0 * dot(rd, r0);\n"
" float c = dot(r0, r0) - (sr * sr);\n"
" float d = (b*b) - 4.0*a*c;\n"
" if (d < 0.0) return vec2(1e5,-1e5);\n"
" return vec2(\n"
" (-b - sqrt(d))/(2.0*a),\n"
" (-b + sqrt(d))/(2.0*a)\n"
" );\n"
"}\n"
"\n"
"vec3 atmosphere(vec3 r, vec3 r0, vec3 pSun, float iSun, float rPlanet, float rAtmos, vec3 kRlh, float kMie, float shRlh, float shMie, float g) {\n"
2023-08-10 22:46:04 +00:00
" // Normalize the sun and view directions.\n"
2023-08-10 21:53:51 +00:00
" pSun = normalize(pSun);\n"
" r = normalize(r);\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Calculate the step size of the primary ray.\n"
2023-08-10 21:53:51 +00:00
" vec2 p = rsi(r0, r, rAtmos);\n"
" if (p.x > p.y) return vec3(0,0,0);\n"
" p.y = min(p.y, rsi(r0, r, rPlanet).x);\n"
" float iStepSize = (p.y - p.x) / float(iSteps);\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Initialize the primary ray time.\n"
2023-08-10 21:53:51 +00:00
" float iTime = 0.0;\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Initialize accumulators for Rayleigh and Mie scattering.\n"
2023-08-10 21:53:51 +00:00
" vec3 totalRlh = vec3(0,0,0);\n"
" vec3 totalMie = vec3(0,0,0);\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Initialize optical depth accumulators for the primary ray.\n"
2023-08-10 21:53:51 +00:00
" float iOdRlh = 0.0;\n"
" float iOdMie = 0.0;\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Calculate the Rayleigh and Mie phases.\n"
2023-08-10 21:53:51 +00:00
" float mu = dot(r, pSun);\n"
" float mumu = mu * mu;\n"
" float gg = g * g;\n"
" float pRlh = 3.0 / (16.0 * PI) * (1.0 + mumu);\n"
" float pMie = 3.0 / (8.0 * PI) * ((1.0 - gg) * (mumu + 1.0)) / (pow(1.0 + gg - 2.0 * mu * g, 1.5) * (2.0 + gg));\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Sample the primary ray.\n"
2023-08-10 21:53:51 +00:00
" for (int i = 0; i < iSteps; i++) {\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Calculate the primary ray sample position.\n"
2023-08-10 21:53:51 +00:00
" vec3 iPos = r0 + r * (iTime + iStepSize * 0.5);\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Calculate the height of the sample.\n"
2023-08-10 21:53:51 +00:00
" float iHeight = length(iPos) - rPlanet;\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Calculate the optical depth of the Rayleigh and Mie scattering for this step.\n"
2023-08-10 21:53:51 +00:00
" float odStepRlh = exp(-iHeight / shRlh) * iStepSize;\n"
" float odStepMie = exp(-iHeight / shMie) * iStepSize;\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Accumulate optical depth.\n"
2023-08-10 21:53:51 +00:00
" iOdRlh += odStepRlh;\n"
" iOdMie += odStepMie;\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Calculate the step size of the secondary ray.\n"
2023-08-10 21:53:51 +00:00
" float jStepSize = rsi(iPos, pSun, rAtmos).y / float(jSteps);\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Initialize the secondary ray time.\n"
2023-08-10 21:53:51 +00:00
" float jTime = 0.0;\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Initialize optical depth accumulators for the secondary ray.\n"
2023-08-10 21:53:51 +00:00
" float jOdRlh = 0.0;\n"
" float jOdMie = 0.0;\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Sample the secondary ray.\n"
2023-08-10 21:53:51 +00:00
" for (int j = 0; j < jSteps; j++) {\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Calculate the secondary ray sample position.\n"
2023-08-10 21:53:51 +00:00
" vec3 jPos = iPos + pSun * (jTime + jStepSize * 0.5);\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Calculate the height of the sample.\n"
2023-08-10 21:53:51 +00:00
" float jHeight = length(jPos) - rPlanet;\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Accumulate the optical depth.\n"
2023-08-10 21:53:51 +00:00
" jOdRlh += exp(-jHeight / shRlh) * jStepSize;\n"
" jOdMie += exp(-jHeight / shMie) * jStepSize;\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Increment the secondary ray time.\n"
2023-08-10 21:53:51 +00:00
" jTime += jStepSize;\n"
" }\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Calculate attenuation.\n"
2023-08-10 21:53:51 +00:00
" vec3 attn = exp(-(kMie * (iOdMie + jOdMie) + kRlh * (iOdRlh + jOdRlh)));\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Accumulate scattering.\n"
2023-08-10 21:53:51 +00:00
" totalRlh += odStepRlh * attn;\n"
" totalMie += odStepMie * attn;\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Increment the primary ray time.\n"
2023-08-10 21:53:51 +00:00
" iTime += iStepSize;\n"
" \n"
" }\n"
" \n"
2023-08-10 22:46:04 +00:00
" // Calculate and return the final color.\n"
2023-08-10 21:53:51 +00:00
" return iSun * (pRlh * kRlh * totalRlh + pMie * kMie * totalMie);\n"
"}\n";
static const char *const fs_main_shadertoy = "//" FILELINE "\n"
"void mainImage( out vec4 fragColor, in vec2 fragCoord );\n"
"void main() {\n"
" mainImage(fragColor, texcoord.xy * iResolution);\n"
"}\n";
static const char *const vs_0_2_fullscreen_quad_A = "//" FILELINE "\n"
"out vec2 texcoord;\n"
"\n"
"void main() {\n"
" texcoord = vec2( (gl_VertexID << 1) & 2, gl_VertexID & 2 );\n"
" gl_Position = vec4( texCoord * 2.0 - 1.0, 0.0, 1.0 );\n"
"}\n";
static const char *const vs_0_2_fullscreen_quad_B = "//" FILELINE "\n"
"out vec2 uv;\n"
"\n"
"void main() {\n"
" float x = float(((uint(gl_VertexID) + 2u) / 3u)%2u);\n"
" float y = float(((uint(gl_VertexID) + 1u) / 3u)%2u);\n"
2023-08-10 22:46:04 +00:00
" gl_Position = vec4(-1.0 + x*2.0, 0.0+(-1.0+y*2.0), 0.0, 1.0); // normal(0+),flipped(0-)\n"
" uv = vec2(x, y); // normal(y),flipped(1.0-y)\n"
2023-08-10 21:53:51 +00:00
"}\n";
static const char *const vs_0_2_fullscreen_quad_B_flipped = "//" FILELINE "\n"
"out vec2 uv;\n"
"\n"
"void main() {\n"
" float x = float(((uint(gl_VertexID) + 2u) / 3u)%2u);\n"
" float y = float(((uint(gl_VertexID) + 1u) / 3u)%2u);\n"
2023-08-10 22:46:04 +00:00
" gl_Position = vec4(-1.0 + x*2.0, 0.0-(-1.0+y*2.0), 0.0, 1.0); // normal(0+),flipped(0-)\n"
" uv = vec2(x, y); // normal(y),flipped(1.0-y)\n"
2023-08-10 21:53:51 +00:00
"}\n";
static const char *const vs_323444143_16_332_model = "//" FILELINE "\n"
#ifndef MAX_BONES
"#define MAX_BONES 110\n"
#endif
"uniform mat3x4 vsBoneMatrix[MAX_BONES];\n"
"uniform bool SKINNED = false;\n"
2023-08-10 22:46:04 +00:00
"// uniform mat4 M; // RIM\n"
2023-08-10 21:53:51 +00:00
"uniform mat4 VP;\n"
"\n"
#if 0
2023-08-10 22:46:04 +00:00
"// Fetch blend channels from all attached blend deformers.\n"
2023-08-10 21:53:51 +00:00
"for (size_t di = 0; di < mesh->blend_deformers.count; di++) {\n"
" ufbx_blend_deformer *deformer = mesh->blend_deformers.data[di];\n"
" for (size_t ci = 0; ci < deformer->channels.count; ci++) {\n"
" ufbx_blend_channel *chan = deformer->channels.data[ci];\n"
" if (chan->keyframes.count == 0) continue;\n"
" if (num_blend_shapes < MAX_BLEND_SHAPES) {\n"
" blend_channels[num_blend_shapes] = chan;\n"
" vmesh->blend_channel_indices[num_blend_shapes] = (int32_t)chan->typed_id;\n"
" num_blend_shapes++;\n"
" }\n"
" }\n"
"}\n"
"if (num_blend_shapes > 0) {\n"
" vmesh->blend_shape_image = pack_blend_channels_to_image(mesh, blend_channels, num_blend_shapes);\n"
" vmesh->num_blend_shapes = num_blend_shapes;\n"
"}\n"
"\n"
"ubo.f_num_blend_shapes = (float)mesh->num_blend_shapes;\n"
"for (size_t i = 0; i < mesh->num_blend_shapes; i++) {\n"
" ubo.blend_weights[i] = view->scene.blend_channels[mesh->blend_channel_indices[i]].weight;\n"
"}\n"
"\n"
"sg_image blend_shapes = mesh->num_blend_shapes > 0 ? mesh->blend_shape_image : view->empty_blend_shape_image;\n"
#endif
"\n"
2023-08-10 22:46:04 +00:00
"// for blendshapes\n"
2023-08-10 21:53:51 +00:00
#ifndef MAX_BLENDSHAPES
"#define MAX_BLENDSHAPES 16\n"
#endif
2023-08-10 22:46:04 +00:00
"uniform vec4 blend_weights[MAX_BLENDSHAPES]; // @todo: implement me\n"
"uniform float f_num_blend_shapes; // @todo: implement me\n"
"uniform sampler2DArray blend_shapes; // @todo: implement me\n"
2023-08-10 21:53:51 +00:00
"\n"
2023-08-10 22:46:04 +00:00
"in vec3 att_position; // @todo: reorder ass2iqe to emit p3 n3 u2 t3 b3 c4B i4 w4 instead\n"
2023-08-10 21:53:51 +00:00
"in vec2 att_texcoord;\n"
"in vec3 att_normal;\n"
2023-08-10 22:46:04 +00:00
"in vec4 att_tangent; // vec3 + bi sign\n"
"in mat4 att_instanced_matrix; // for instanced rendering\n"
"in vec4 att_indexes; // @fixme: gles might use ivec4 instead?\n"
"in vec4 att_weights; // @todo: downgrade from float to byte\n"
"in float att_vertexindex; // for blendshapes\n"
2023-08-10 21:53:51 +00:00
"in vec4 att_color;\n"
2023-08-10 22:46:04 +00:00
"in vec3 att_bitangent; // @todo: remove? also, ass2iqe might output this\n"
2023-08-10 21:53:51 +00:00
"out vec4 v_color;\n"
"out vec3 v_position;\n"
"out vec3 v_normal, v_normal_ws;\n"
"out vec2 v_texcoord;\n"
"\n"
"\n"
2023-08-10 22:46:04 +00:00
"// shadow\n"
2023-08-10 21:53:51 +00:00
"uniform mat4 model, view;\n"
"uniform mat4 cameraToShadowProjector;\n"
"out vec4 vneye;\n"
"out vec4 vpeye;\n"
"out vec4 sc;\n"
"void do_shadow() {\n"
" vneye = view * model * vec4(att_normal, 0.0f);\n"
" vpeye = view * model * vec4(att_position, 1.0);\n"
" sc = cameraToShadowProjector * model * vec4(att_position, 1.0f);\n"
"}\n"
"\n"
2023-08-10 22:46:04 +00:00
"// blendshapes\n"
2023-08-10 21:53:51 +00:00
"vec3 evaluate_blend_shape(int vertex_index) {\n"
" ivec2 coord = ivec2(vertex_index & (2048 - 1), vertex_index >> 11);\n"
" int num_blend_shapes = int(f_num_blend_shapes);\n"
" vec3 offset = vec3(0.0);\n"
" for (int i = 0; i < num_blend_shapes; i++) {\n"
" vec4 packedw = blend_weights[i >> 2];\n"
" float weight = packedw[i & 3];\n"
" offset += weight * texelFetch(blend_shapes, ivec3(coord, i), 0).xyz;\n"
" }\n"
" return offset;\n"
"}\n"
"\n"
"void main() {\n"
" vec3 objPos;\n"
" if(!SKINNED) {\n"
" objPos = att_position;\n"
" v_normal = att_normal;\n"
2023-08-10 23:15:15 +00:00
" } else {\n"
" mat3x4 m = vsBoneMatrix[int(att_indexes.x)] * att_weights.x;\n"
" m += vsBoneMatrix[int(att_indexes.y)] * att_weights.y;\n"
" m += vsBoneMatrix[int(att_indexes.z)] * att_weights.z;\n"
" m += vsBoneMatrix[int(att_indexes.w)] * att_weights.w;\n"
" objPos = vec4(att_position, 1.0) * m;\n"
2023-08-10 21:53:51 +00:00
" \n"
2023-08-10 23:15:15 +00:00
" // blendshapes\n"
" // objPos += evaluate_blend_shape(int(att_vertexindex));\n"
2023-08-10 21:53:51 +00:00
" \n"
2023-08-10 23:15:15 +00:00
" v_normal = vec4(att_normal, 0.0) * m;\n"
" //@todo: tangents\n"
" }\n"
" \n"
" // vec3 tangent = att_tangent.xyz;\n"
" // vec3 bitangent = cross(att_normal, att_tangent.xyz) * att_tangent.w;\n"
" \n"
" v_normal_ws = normalize(vec3(model * vec4(v_normal, 0.))); // normal to world/model space\n"
" v_normal = normalize(v_normal);\n"
" v_position = att_position;\n"
" v_texcoord = att_texcoord;\n"
" v_color = att_color;\n"
" gl_Position = VP * att_instanced_matrix * vec4( objPos, 1.0 );\n"
" do_shadow();\n"
"}\n"
"\n";
2023-08-10 21:53:51 +00:00
static const char *const vs_324_24_sprite = "//" FILELINE "\n"
"uniform mat4 u_mvp;\n"
"\n"
"in vec3 att_Position;\n"
"in vec2 att_TexCoord;\n"
"in vec4 att_Color;\n"
"out vec2 vTexCoord;\n"
"out vec4 vColor;\n"
"\n"
"void main() {\n"
" vColor = att_Color;\n"
" vTexCoord = att_TexCoord;\n"
" gl_Position = u_mvp * vec4(att_Position, 1.0);\n"
"}\n";
static const char *const vs_332_32 = "//" FILELINE "\n"
2023-08-10 22:46:04 +00:00
"//uniform mat4 u_model, u_view, u_proj;\n"
2023-08-10 21:53:51 +00:00
"uniform mat4 u_mvp;\n"
"\n"
"in vec3 att_position;\n"
"in vec3 att_normal;\n"
"in vec2 att_texcoord;\n"
"in vec4 att_color;\n"
"out vec4 v_color;\n"
"out vec3 v_normal;\n"
"out vec3 v_normal_ws;\n"
"out vec2 v_texcoord;\n"
"\n"
2023-08-10 22:46:04 +00:00
"// shadow\n"
2023-08-10 21:53:51 +00:00
"uniform mat4 model, view, proj;\n"
2023-08-10 22:46:04 +00:00
"uniform mat4 cameraToShadowProjector; // !VSMCUBE\n"
2023-08-10 21:53:51 +00:00
"out vec4 vneye;\n"
"out vec4 vpeye;\n"
2023-08-10 22:46:04 +00:00
"out vec4 sc; // !VSMCUBE\n"
2023-08-10 21:53:51 +00:00
"void do_shadow() {\n"
" vneye = view * model * vec4(att_normal, 0.0f);\n"
" vpeye = view * model * vec4(att_position, 1.0);\n"
" sc = cameraToShadowProjector * model * vec4(att_position, 1.0f);\n"
"}\n"
"\n"
"void main() {\n"
2023-08-10 22:46:04 +00:00
" // gl_Position = proj * view * model * vec4(att_position, 1.0);\n"
2023-08-10 21:53:51 +00:00
" gl_Position = u_mvp * vec4(att_position, 1.0);\n"
" v_normal = normalize(att_normal);\n"
2023-08-10 22:46:04 +00:00
" v_normal_ws = normalize(vec3(model * vec4(att_normal, 0.))); // normal world/model space\n"
2023-08-10 21:53:51 +00:00
" v_texcoord = att_texcoord;\n"
" v_color = att_color;\n"
" do_shadow();\n"
"}\n";
static const char *const vs_3_3_skybox = "//" FILELINE "\n"
"uniform mat4 u_mvp;\n"
"\n"
"in vec3 att_position;\n"
"out vec3 v_direction;\n"
"\n"
"void main() {\n"
" vec4 position = u_mvp * vec4(att_position, 0.0);\n"
" gl_Position = position.xyww;\n"
" v_direction = att_position;\n"
"}\n";