├── .gitignore ├── LICENSE ├── README.md ├── build.bat ├── clean.bat ├── include ├── MinecraftRenderer.Materials │ ├── ActorUtil.dragonh │ ├── DynamicUtil.dragonh │ ├── FogUtil.dragonh │ ├── GlintUtil.dragonh │ └── TAAUtil.dragonh ├── bgfx_compute.sh └── bgfx_shader.sh ├── materials ├── Actor │ ├── defines.json │ └── src │ │ ├── Actor.fragment.sc │ │ ├── Actor.varying.def.sc │ │ └── Actor.vertex.sc ├── ActorBanner │ ├── defines.json │ └── src │ │ ├── ActorBanner.fragment.sc │ │ ├── ActorBanner.varying.def.sc │ │ └── ActorBanner.vertex.sc ├── ActorGlint │ ├── defines.json │ └── src │ │ ├── ActorGlint.fragment.sc │ │ ├── ActorGlint.varying.def.sc │ │ └── ActorGlint.vertex.sc ├── BlockSelectionOverlay │ └── src │ │ ├── BlockSelectionOverlay.fragment.sc │ │ ├── BlockSelectionOverlay.varying.def.sc │ │ └── BlockSelectionOverlay.vertex.sc ├── Bloom │ └── src │ │ ├── Bloom.fragment.sc │ │ ├── Bloom.varying.def.sc │ │ └── Bloom.vertex.sc ├── ClearVolume │ └── src │ │ ├── ClearVolume.compute.sc │ │ ├── ClearVolume.fragment.sc │ │ ├── ClearVolume.varying.def.sc │ │ └── ClearVolume.vertex.sc ├── Cracks │ └── src │ │ ├── Cracks.fragment.sc │ │ ├── Cracks.varying.def.sc │ │ └── Cracks.vertex.sc ├── CracksBlockEntity │ └── src │ │ ├── CracksBlockEntity.fragment.sc │ │ ├── CracksBlockEntity.varying.def.sc │ │ └── CracksBlockEntity.vertex.sc ├── Cubemap │ └── src │ │ ├── Cubemap.fragment.sc │ │ ├── Cubemap.varying.def.sc │ │ └── Cubemap.vertex.sc ├── CubemapHDRi │ └── src │ │ ├── CubemapHDRi.fragment.sc │ │ ├── CubemapHDRi.varying.def.sc │ │ └── CubemapHDRi.vertex.sc ├── DeferredShading │ └── src │ │ ├── DeferredShading.fragment.sc │ │ ├── DeferredShading.varying.def.sc │ │ ├── DeferredShading.vertex.sc │ │ └── DoDeferredShading.sc ├── Flipbook │ └── src │ │ ├── Flipbook.fragment.sc │ │ ├── Flipbook.varying.def.sc │ │ └── Flipbook.vertex.sc ├── ItemInHandColor │ └── src │ │ ├── ItemInHandColor.fragment.sc │ │ ├── ItemInHandColor.varying.def.sc │ │ └── ItemInHandColor.vertex.sc ├── ItemInHandColorGlint │ └── src │ │ ├── ItemInHandColorGlint.fragment.sc │ │ ├── ItemInHandColorGlint.varying.def.sc │ │ └── ItemInHandColorGlint.vertex.sc ├── ItemInHandTextured │ └── src │ │ ├── ItemInHandTextured.fragment.sc │ │ ├── ItemInHandTextured.varying.def.sc │ │ └── ItemInHandTextured.vertex.sc ├── LegacyCubemap │ └── src │ │ ├── LegacyCubemap.fragment.sc │ │ ├── LegacyCubemap.varying.def.sc │ │ └── LegacyCubemap.vertex.sc ├── LightClustering │ └── src │ │ ├── ClusterLights.sc │ │ ├── ClusterLightsManhattan.sc │ │ ├── LightClustering.compute.sc │ │ ├── LightClustering.fragment.sc │ │ ├── LightClustering.varying.def.sc │ │ └── LightClustering.vertex.sc ├── LuminanceHistogram │ └── src │ │ ├── BuildHistogram.sc │ │ ├── CalculateAverage.sc │ │ ├── CleanUp.sc │ │ ├── LuminanceHistogram.compute.sc │ │ ├── LuminanceHistogram.fragment.sc │ │ ├── LuminanceHistogram.varying.def.sc │ │ └── LuminanceHistogram.vertex.sc ├── MeshFallbackPosUVNormalColor │ └── src │ │ ├── MeshFallbackPosUVNormalColor.fragment.sc │ │ ├── MeshFallbackPosUVNormalColor.varying.def.sc │ │ └── MeshFallbackPosUVNormalColor.vertex.sc ├── Particle │ └── src │ │ ├── Particle.fragment.sc │ │ ├── Particle.varying.def.sc │ │ └── Particle.vertex.sc ├── PopulateVolume │ └── src │ │ ├── PopulateVolume.compute.sc │ │ ├── PopulateVolume.fragment.sc │ │ ├── PopulateVolume.varying.def.sc │ │ └── PopulateVolume.vertex.sc ├── RenderChunk │ └── src │ │ ├── RenderChunk.fragment.sc │ │ ├── RenderChunk.varying.def.sc │ │ └── RenderChunk.vertex.sc ├── RenderChunkForwardPBR │ ├── defines.json │ └── src │ │ ├── RenderChunkForwardPBR.fragment.sc │ │ ├── RenderChunkForwardPBR.varying.def.sc │ │ └── RenderChunkForwardPBR.vertex.sc ├── RenderChunkPrepass │ └── src │ │ ├── RenderChunkPrepass.fragment.sc │ │ ├── RenderChunkPrepass.varying.def.sc │ │ └── RenderChunkPrepass.vertex.sc ├── ScreenBlit │ └── src │ │ ├── ScreenBlit.fragment.sc │ │ ├── ScreenBlit.varying.def.sc │ │ └── ScreenBlit.vertex.sc ├── Sky │ └── src │ │ ├── Sky.fragment.sc │ │ ├── Sky.varying.def.sc │ │ └── Sky.vertex.sc ├── Stars │ └── src │ │ ├── Stars.fragment.sc │ │ ├── Stars.varying.def.sc │ │ └── Stars.vertex.sc ├── StarsForwardPBR │ └── src │ │ ├── StarsForwardPBR.fragment.sc │ │ ├── StarsForwardPBR.varying.def.sc │ │ └── StarsForwardPBR.vertex.sc ├── SunMoon │ └── src │ │ ├── SunMoon.fragment.sc │ │ ├── SunMoon.varying.def.sc │ │ └── SunMoon.vertex.sc ├── SunMoonForwardPBR │ └── src │ │ ├── SunMoonForwardPBR.fragment.sc │ │ ├── SunMoonForwardPBR.varying.def.sc │ │ └── SunMoonForwardPBR.vertex.sc ├── ToneMapping │ └── src │ │ ├── ToneMapping.fragment.sc │ │ ├── ToneMapping.varying.def.sc │ │ └── ToneMapping.vertex.sc ├── UIBase │ └── src │ │ ├── UIBase.fragment.sc │ │ ├── UIBase.varying.def.sc │ │ └── UIBase.vertex.sc ├── UIBlit │ └── src │ │ ├── UIBlit.fragment.sc │ │ ├── UIBlit.varying.def.sc │ │ └── UIBlit.vertex.sc ├── UIFill │ └── src │ │ ├── UIFill.fragment.sc │ │ ├── UIFill.varying.def.sc │ │ └── UIFill.vertex.sc ├── UIGlint │ └── src │ │ ├── UIGlint.fragment.sc │ │ ├── UIGlint.varying.def.sc │ │ └── UIGlint.vertex.sc ├── UISprite │ └── src │ │ ├── UISprite.fragment.sc │ │ ├── UISprite.varying.def.sc │ │ └── UISprite.vertex.sc ├── UIText │ ├── defines.json │ └── src │ │ ├── UIText.fragment.sc │ │ ├── UIText.varying.def.sc │ │ └── UIText.vertex.sc ├── Upscaling │ ├── defines.json │ └── src │ │ ├── Upscaling.fragment.sc │ │ ├── Upscaling.varying.def.sc │ │ └── Upscaling.vertex.sc ├── VolumeScattering │ └── src │ │ ├── VolumeScattering.compute.sc │ │ ├── VolumeScattering.fragment.sc │ │ ├── VolumeScattering.varying.def.sc │ │ └── VolumeScattering.vertex.sc └── Weather │ └── src │ ├── Weather.fragment.sc │ ├── Weather.varying.def.sc │ └── Weather.vertex.sc └── setup_build_environment.bat /.gitignore: -------------------------------------------------------------------------------- 1 | /data 2 | /env 3 | build 4 | *.material.bin 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 SurvivalApparatusCommunication 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RenderDragonSourceCodeInv 2 | 3 | Reverse render dragon shaders source code from .bin file 4 | 5 | REBORN 6 | 7 | ## Building 8 | 9 | Recommended to build on Windows, because bgfx shaderc does not support compiling Direct3D shaders on other platforms 10 | 11 | ### Windows 12 | 13 | 1. Run `setup_build_environment.bat`. This script will automatically download MaterialBinTool with shaderc and the data used to build materials. 14 | 2. Run `build.bat` without arguments to build all materials for all platforms, or use arguments to specify which materials and platforms to build. 15 | 16 | The available arguments are: 17 | ``` 18 | -t, --threads: Specifies the number of threads to use for compilation. The default value is 1. 19 | -p, --platform: Specifies one or more target platforms. If not specified, build for all platforms. 20 | -m, --material: Specifies one or more materials to build. If not specified, build all materials. 21 | --debug: Enables debug information. 22 | ``` 23 | Examples: 24 | 25 | Build all materials for all platforms: 26 | ``` 27 | ./build.bat 28 | ``` 29 | Build RenderChunk and Sky for Windows and Android: 30 | ``` 31 | ./build.bat -p Windows Android -m RenderChunk Sky 32 | ``` 33 | -------------------------------------------------------------------------------- /build.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal 3 | cd %~dp0 4 | call setup_build_environment.bat 5 | 6 | set MBT_THREADS=1 7 | set MBT_DEBUG=0 8 | set TARGET_PLATFORM= 9 | set MATERIALS= 10 | 11 | set prevArg= 12 | :parse_args 13 | set arg=%1 14 | if "%arg%"=="" goto :end 15 | 16 | if "%arg%"=="--threads" ( 17 | set prevArg=threads 18 | goto :continue 19 | ) 20 | if "%arg%"=="-t" ( 21 | set prevArg=threads 22 | goto :continue 23 | ) 24 | if "%arg%"=="--platform" ( 25 | set prevArg=platform 26 | goto :continue 27 | ) 28 | if "%arg%"=="-p" ( 29 | set prevArg=platform 30 | goto :continue 31 | ) 32 | if "%arg%"=="--material" ( 33 | set prevArg=material 34 | goto :continue 35 | ) 36 | if "%arg%"=="-m" ( 37 | set prevArg=material 38 | goto :continue 39 | ) 40 | if "%arg%"=="--debug" ( 41 | set MBT_DEBUG=1 42 | goto :continue 43 | ) 44 | 45 | if "%prevArg%"=="threads" ( 46 | set prevArg= 47 | set MBT_THREADS=%arg% 48 | goto :continue 49 | ) 50 | if "%prevArg%"=="platform" ( 51 | set TARGET_PLATFORM=%TARGET_PLATFORM% %arg% 52 | goto :continue 53 | ) 54 | if "%prevArg%"=="material" ( 55 | set MATERIALS=%MATERIALS% %arg% 56 | goto :continue 57 | ) 58 | 59 | set TARGET_PLATFORM=%TARGET_PLATFORM% %arg% 60 | :continue 61 | shift 62 | goto :parse_args 63 | :end 64 | 65 | set MBT_ARGS=--compile --shaderc %SHADERC% --include include --threads %MBT_THREADS% 66 | if %MBT_DEBUG%==1 ( 67 | set MBT_ARGS=%MBT_ARGS% --debug 68 | ) 69 | 70 | set ALL_PLATFORM=0 71 | if "%TARGET_PLATFORM%"=="" ( 72 | set TARGET_PLATFORM=Windows Android iOS 73 | set ALL_PLATFORM=1 74 | ) 75 | for %%p in (%TARGET_PLATFORM%) do ( 76 | echo Building materials for %%p 77 | if "%MATERIALS%"=="" ( 78 | for /d %%m in (materials\*) do ( 79 | echo Building %%~nxm 80 | call :build %%p %%~nxm 81 | ) 82 | ) else ( 83 | for %%m in (%MATERIALS%) do ( 84 | echo Building %%m 85 | call :build %%p %%m 86 | ) 87 | ) 88 | echo. 89 | ) 90 | goto :end 91 | 92 | @REM %1 platform 93 | @REM %2 material 94 | :build 95 | if exist materials\%2\data\%1\%2.json ( 96 | %MBT% %MBT_ARGS% --output build\%1 --data materials\%2\data\%1\%2.json materials\%2 97 | ) else ( 98 | if exist %DATA_DIR%\%1 ( 99 | %MBT% %MBT_ARGS% --output build\%1 --data %DATA_DIR%\%1\%2 materials\%2 100 | ) else ( 101 | echo No data found for material %2 on platform %1 102 | ) 103 | ) 104 | goto :EOF 105 | 106 | :end 107 | endlocal 108 | -------------------------------------------------------------------------------- /clean.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | for %%p in (Windows Android iOS Merged) do ( 3 | if exist build\%%p\*.material.bin ( 4 | del build\%%p\*.material.bin 5 | ) 6 | ) 7 | -------------------------------------------------------------------------------- /include/MinecraftRenderer.Materials/ActorUtil.dragonh: -------------------------------------------------------------------------------- 1 | #ifndef ACTOR_UTIL_H_HEADER_GUARD 2 | #define ACTOR_UTIL_H_HEADER_GUARD 3 | 4 | #include "DynamicUtil.dragonh" 5 | 6 | vec4 applyEmissiveLighting(vec4 diffuse, vec4 light) { 7 | #if EMISSIVE || EMISSIVE_ONLY 8 | diffuse.rgb *= mix(vec3(1.0, 1.0, 1.0), light.rgb, diffuse.a); //make glowy stuff 9 | #else 10 | diffuse = applyLighting(diffuse, light); // #line 27 11 | #endif 12 | return diffuse; 13 | } 14 | 15 | bool shouldDiscard(vec3 diffuse, float alpha, float epsilon) { 16 | bool result = false; 17 | 18 | #if EMISSIVE || EMISSIVE_ONLY 19 | #if EMISSIVE 20 | result = dot(vec4(diffuse, alpha), vec4(1.0, 1.0, 1.0, 1.0)) < epsilon; 21 | #else 22 | result = (alpha < epsilon) || (alpha > 1.0 - epsilon); 23 | #endif 24 | #else 25 | #if !CHANGE_COLOR && !CHANGE_COLOR_MULTI 26 | result = alpha < 0.5; //#line 48 27 | #else 28 | result = alpha < epsilon; // #line 51 29 | #endif 30 | #endif 31 | 32 | return result; // #line 55 33 | } 34 | 35 | vec4 applyActorDiffuse(vec4 albedo, vec3 color, vec4 light, float colorBased, vec4 overlayColor) { 36 | albedo.rgb *= mix(vec3(1, 1, 1), color, colorBased); 37 | 38 | albedo = applyOverlayColor(albedo, overlayColor); 39 | albedo = applyEmissiveLighting(albedo, light); 40 | 41 | return albedo; 42 | } 43 | 44 | vec4 applyHudOpacity(vec4 diffuse, float hudOpacity) { 45 | diffuse.a *= hudOpacity; 46 | return diffuse; 47 | } 48 | 49 | vec4 getActorAlbedoNoColorChange(vec2 uv, sampler2D matTexture, sampler2D matTexture1, vec4 matColor) { 50 | vec4 albedo = matColor; 51 | albedo *= texture2D(matTexture, uv); 52 | 53 | #if MASKED_MULTITEXTURE 54 | vec4 tex1 = texture2D(matTexture1, uv); 55 | 56 | // If tex1 has a non-black color and no alpha, use color; otherwise use tex1 57 | float maskedTexture = float((tex1.r + tex1.g + tex1.b) * (1.0 - tex1.a) > 0.0); 58 | albedo = mix(tex1, albedo, maskedTexture); 59 | #endif // MASKED_MULTITEXTURE 60 | return albedo; // #line 69 61 | } 62 | 63 | vec4 getActorAlbedoNoColorChange(vec2 uv, sampler2D matTexture, vec4 matColor) { 64 | vec4 albedo = matColor; 65 | albedo *= texture2D(matTexture, uv); 66 | 67 | return albedo; 68 | } 69 | 70 | #endif // ACTOR_UTIL_H_HEADER_GUARD -------------------------------------------------------------------------------- /include/MinecraftRenderer.Materials/DynamicUtil.dragonh: -------------------------------------------------------------------------------- 1 | #ifndef DYNAMIC_UTIL_H_HEADER_GUARD 2 | #define DYNAMIC_UTIL_H_HEADER_GUARD 3 | vec2 applyUvAnimation(vec2 uv, const vec4 uvAnimation) { // #line 3 4 | uv = uvAnimation.xy + (uv * uvAnimation.zw); 5 | return uv; 6 | } 7 | 8 | vec4 applyOverlayColor(vec4 diffuse, const vec4 overlayColor) { 9 | diffuse.rgb = mix(diffuse.rgb, overlayColor.rgb, overlayColor.a); 10 | 11 | return diffuse; 12 | } 13 | 14 | vec4 applyColorChange(vec4 originalColor, vec4 changeColor, float alpha) { 15 | originalColor.rgb = mix(originalColor, originalColor * changeColor, alpha).rgb; 16 | return originalColor; 17 | } 18 | 19 | vec4 applyMultiColorChange(vec4 diffuse, vec3 changeColor, vec3 multiplicativeTintColor) { 20 | // Texture is a mask for tinting with two colors 21 | vec2 colorMask = diffuse.rg; 22 | 23 | // Apply the base color tint 24 | diffuse.rgb = colorMask.rrr * changeColor; 25 | 26 | // Apply the secondary color mask and tint so long as its grayscale value is not 0 27 | diffuse.rgb = mix(diffuse.rgb, colorMask.ggg * multiplicativeTintColor.rgb, ceil(colorMask.g)); 28 | 29 | return diffuse; 30 | } 31 | 32 | vec4 applyLighting(vec4 diffuse, const vec4 light) { 33 | diffuse.rgb *= light.rgb; 34 | 35 | return diffuse; 36 | } 37 | 38 | float calculateLightIntensity(const mat4 world, const vec4 normal, const vec4 tileLightColor) { 39 | #if FANCY 40 | const float AMBIENT = 0.45; 41 | const float XFAC = -0.1; 42 | const float ZFAC = 0.1; 43 | 44 | vec3 N = normalize(mul(world, normal)).xyz; 45 | N.y *= tileLightColor.a; 46 | float yLight = (1.0 + N.y) * 0.5; 47 | 48 | return yLight * (1.0 - AMBIENT) + N.x * N.x * XFAC + N.z * N.z * ZFAC + AMBIENT; 49 | #else 50 | return 1.0; // #line 50 51 | #endif 52 | } // #line 52 53 | 54 | #endif // DYNAMIC_UTIL_H_HEADER_GUARD -------------------------------------------------------------------------------- /include/MinecraftRenderer.Materials/FogUtil.dragonh: -------------------------------------------------------------------------------- 1 | #ifndef FOG_UTIL_H_HEADER_GUARD 2 | #define FOG_UTIL_H_HEADER_GUARD 3 | 4 | 5 | 6 | 7 | //calculateFogIntensityVanilla 8 | float calculateFogIntensity(float cameraDepth, float maxDistance, float fogStart, float fogEnd) { // #line 8 9 | float distance = cameraDepth / maxDistance; 10 | return saturate((distance - fogStart) / (fogEnd - fogStart)); 11 | } 12 | //calculateFogIntensityFadedVanilla 13 | float calculateFogIntensityFaded(float cameraDepth, float maxDistance, float fogStart, float fogEnd, float fogAlpha) { 14 | float distance = cameraDepth / maxDistance; 15 | distance += fogAlpha; 16 | return saturate((distance - fogStart) / (fogEnd - fogStart)); 17 | } 18 | //applyFogVanilla 19 | vec3 applyFog(vec3 diffuse, vec3 fogColor, float fogIntensity) { 20 | return mix(diffuse, fogColor, fogIntensity); 21 | } 22 | 23 | //float calculateFogIntensity(float cameraDepth, float fogStart, float fogEndMinusStartReciprocal) { 24 | // return saturate((cameraDepth - fogStart) * fogEndMinusStartReciprocal); 25 | //} 26 | 27 | //float calculateFogIntensityFaded(float cameraDepth, float maxDistance, float fogStart, float fogEndMinusStartReciprocal, float fogAlpha) { 28 | // float distance = cameraDepth / maxDistance; 29 | // distance += fogAlpha; 30 | // return saturate((distance - fogStart) * fogEndMinusStartReciprocal); 31 | //} 32 | 33 | #endif // FOG_UTIL_H_HEADER_GUARD -------------------------------------------------------------------------------- /include/MinecraftRenderer.Materials/GlintUtil.dragonh: -------------------------------------------------------------------------------- 1 | #ifndef GLINT_UTIL_H_HEADER_GUARD 2 | #define GLINT_UTIL_H_HEADER_GUARD 3 | 4 | vec2 calculateLayerUV(const vec2 origUV, const float offset, const float rotation, const vec2 scale) { // #line 1 5 | vec2 uv = origUV; 6 | uv -= 0.5; 7 | 8 | float rsin = sin(rotation); 9 | float rcos = cos(rotation); 10 | 11 | uv = mul(uv, mat2(rcos, -rsin, rsin, rcos)); 12 | uv.x += offset; 13 | uv += 0.5; 14 | 15 | return uv * scale; 16 | } 17 | 18 | vec4 glintBlend(const vec4 dest, const vec4 source) { 19 | return vec4(source.rgb * source.rgb, abs(source.a)) + vec4(dest.rgb, 0.0); 20 | } 21 | 22 | vec4 applyGlint(const vec4 diffuse, const vec4 layerUV, const sampler2D glintTexture, const vec4 glintColor, const vec4 tileLightColor) { 23 | vec4 tex1 = texture2D(glintTexture, fract(layerUV.xy)).rgbr * glintColor; 24 | vec4 tex2 = texture2D(glintTexture, fract(layerUV.zw)).rgbr * glintColor; 25 | 26 | vec4 glint = (tex1 + tex2) * tileLightColor; 27 | glint = glintBlend(diffuse, glint); 28 | 29 | return glint; 30 | } 31 | 32 | #endif // GLINT_UTIL_H_HEADER_GUARD -------------------------------------------------------------------------------- /include/MinecraftRenderer.Materials/TAAUtil.dragonh: -------------------------------------------------------------------------------- 1 | #ifndef TAA_UTIL_H_HEADER_GUARD 2 | #define TAA_UTIL_H_HEADER_GUARD 3 | 4 | uniform vec4 SubPixelOffset; 5 | 6 | vec4 jitterVertexPosition(vec3 worldPosition) { // #line 1 7 | mat4 offsetProj = u_proj; 8 | 9 | #if BGFX_SHADER_LANGUAGE_GLSL 10 | offsetProj[2][0] += SubPixelOffset.x; 11 | offsetProj[2][1] -= SubPixelOffset.y; 12 | #else 13 | offsetProj[0][2] += SubPixelOffset.x; // #line 8 14 | offsetProj[1][2] -= SubPixelOffset.y; 15 | #endif 16 | return mul(offsetProj, mul(u_view, vec4(worldPosition, 1.0f))); 17 | } 18 | 19 | #endif // TAA_UTIL_H_HEADER_GUARD -------------------------------------------------------------------------------- /include/bgfx_compute.sh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011-2023 Branimir Karadzic. All rights reserved. 3 | * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE 4 | */ 5 | 6 | #ifndef BGFX_COMPUTE_H_HEADER_GUARD 7 | #define BGFX_COMPUTE_H_HEADER_GUARD 8 | 9 | #include "bgfx_shader.sh" 10 | 11 | #ifndef __cplusplus 12 | 13 | #if BGFX_SHADER_LANGUAGE_HLSL > 0 && BGFX_SHADER_LANGUAGE_HLSL < 400 14 | # error "Compute is not supported!" 15 | #endif // BGFX_SHADER_LANGUAGE_HLSL 16 | 17 | #if BGFX_SHADER_LANGUAGE_METAL || BGFX_SHADER_LANGUAGE_SPIRV 18 | # define FORMAT(_format) [[spv::format_ ## _format]] 19 | # define WRITEONLY [[spv::nonreadable]] 20 | #else 21 | # define FORMAT(_format) 22 | # define WRITEONLY 23 | #endif // BGFX_SHADER_LANGUAGE_METAL || BGFX_SHADER_LANGUAGE_SPIRV 24 | 25 | #if BGFX_SHADER_LANGUAGE_GLSL 26 | 27 | #define SHARED shared 28 | 29 | #define __IMAGE_XX(_name, _format, _reg, _image, _access) \ 30 | layout(_format, binding=_reg) _access uniform highp _image _name 31 | 32 | #define readwrite 33 | #define IMAGE2D_RO( _name, _format, _reg) __IMAGE_XX(_name, _format, _reg, image2D, readonly) 34 | #define UIMAGE2D_RO(_name, _format, _reg) __IMAGE_XX(_name, _format, _reg, uimage2D, readonly) 35 | #define IMAGE2D_WR( _name, _format, _reg) __IMAGE_XX(_name, _format, _reg, image2D, writeonly) 36 | #define UIMAGE2D_WR(_name, _format, _reg) __IMAGE_XX(_name, _format, _reg, uimage2D, writeonly) 37 | #define IMAGE2D_RW( _name, _format, _reg) __IMAGE_XX(_name, _format, _reg, image2D, readwrite) 38 | #define UIMAGE2D_RW(_name, _format, _reg) __IMAGE_XX(_name, _format, _reg, uimage2D, readwrite) 39 | 40 | #define IMAGE2D_ARRAY_RO( _name, _format, _reg) __IMAGE_XX(_name, _format, _reg, image2DArray, readonly) 41 | #define UIMAGE2D_ARRAY_RO(_name, _format, _reg) __IMAGE_XX(_name, _format, _reg, uimage2DArray, readonly) 42 | #define IMAGE2D_ARRAY_WR( _name, _format, _reg) __IMAGE_XX(_name, _format, _reg, image2DArray, writeonly) 43 | #define UIMAGE2D_ARRAY_WR(_name, _format, _reg) __IMAGE_XX(_name, _format, _reg, uimage2DArray, writeonly) 44 | #define IMAGE2D_ARRAY_RW( _name, _format, _reg) __IMAGE_XX(_name, _format, _reg, image2DArray, readwrite) 45 | #define UIMAGE2D_ARRAY_RW(_name, _format, _reg) __IMAGE_XX(_name, _format, _reg, uimage2DArray, readwrite) 46 | 47 | #define IMAGE3D_RO( _name, _format, _reg) __IMAGE_XX(_name, _format, _reg, image3D, readonly) 48 | #define UIMAGE3D_RO(_name, _format, _reg) __IMAGE_XX(_name, _format, _reg, uimage3D, readonly) 49 | #define IMAGE3D_WR( _name, _format, _reg) __IMAGE_XX(_name, _format, _reg, image3D, writeonly) 50 | #define UIMAGE3D_WR(_name, _format, _reg) __IMAGE_XX(_name, _format, _reg, uimage3D, writeonly) 51 | #define IMAGE3D_RW( _name, _format, _reg) __IMAGE_XX(_name, _format, _reg, image3D, readwrite) 52 | #define UIMAGE3D_RW(_name, _format, _reg) __IMAGE_XX(_name, _format, _reg, uimage3D, readwrite) 53 | 54 | #define __BUFFER_XX(_name, _type, _reg, _access) \ 55 | layout(std430, binding=_reg) _access buffer _name ## Buffer \ 56 | { \ 57 | _type _name[]; \ 58 | } 59 | 60 | #define __BUFFER(_name, _type, _reg) \ 61 | layout(std430, binding=_reg) buffer _name ## Buffer \ 62 | { \ 63 | _type _name[]; \ 64 | } 65 | 66 | #define BUFFER_RO(_name, _type, _reg) __BUFFER_XX(_name, _type, _reg, readonly) 67 | #define BUFFER_RW(_name, _type, _reg) __BUFFER(_name, _type, _reg) 68 | #define BUFFER_WR(_name, _type, _reg) __BUFFER_XX(_name, _type, _reg, writeonly) 69 | 70 | #define NUM_THREADS(_x, _y, _z) layout (local_size_x = _x, local_size_y = _y, local_size_z = _z) in; 71 | 72 | #define atomicFetchAndAdd(_mem, _data, _original) _original = atomicAdd(_mem, _data) 73 | #define atomicFetchAndAnd(_mem, _data, _original) _original = atomicAnd(_mem, _data) 74 | #define atomicFetchAndMax(_mem, _data, _original) _original = atomicMax(_mem, _data) 75 | #define atomicFetchAndMin(_mem, _data, _original) _original = atomicMin(_mem, _data) 76 | #define atomicFetchAndOr(_mem, _data, _original) _original = atomicOr(_mem, _data) 77 | #define atomicFetchAndXor(_mem, _data, _original) _original = atomicXor(_mem, _data) 78 | #define atomicFetchAndExchange(_mem, _data, _original) _original = atomicExchange(_mem, _data) 79 | #define atomicFetchCompareExchange(_mem, _compare, _data, _original) _original = atomicCompSwap(_mem,_compare, _data) 80 | 81 | #else 82 | 83 | #define SHARED groupshared 84 | 85 | #define COMP_r32ui uint 86 | #define COMP_rg32ui uint2 87 | #define COMP_rgba32ui uint4 88 | #define COMP_r32f float 89 | #define COMP_r16f float 90 | #define COMP_rg16f float2 91 | #define COMP_rgba16f float4 92 | #if BGFX_SHADER_LANGUAGE_HLSL 93 | # define COMP_rgba8 unorm float4 94 | # define COMP_rg8 unorm float2 95 | # define COMP_r8 unorm float 96 | #else 97 | # define COMP_rgba8 float4 98 | # define COMP_rg8 float2 99 | # define COMP_r8 float 100 | #endif // BGFX_SHADER_LANGUAGE_HLSL 101 | #define COMP_rgba32f float4 102 | 103 | #define IMAGE2D_RO( _name, _format, _reg) \ 104 | FORMAT(_format) Texture2D _name : REGISTER(t, _reg); \ 105 | 106 | #define UIMAGE2D_RO(_name, _format, _reg) IMAGE2D_RO(_name, _format, _reg) 107 | 108 | #define IMAGE2D_WR( _name, _format, _reg) \ 109 | WRITEONLY FORMAT(_format) RWTexture2D _name : REGISTER(u, _reg); \ 110 | 111 | #define UIMAGE2D_WR(_name, _format, _reg) IMAGE2D_WR(_name, _format, _reg) 112 | 113 | #define IMAGE2D_RW( _name, _format, _reg) \ 114 | FORMAT(_format) RWTexture2D _name : REGISTER(u, _reg); \ 115 | 116 | #define UIMAGE2D_RW(_name, _format, _reg) IMAGE2D_RW(_name, _format, _reg) 117 | 118 | #define IMAGE2D_ARRAY_RO(_name, _format, _reg) \ 119 | FORMAT(_format) Texture2DArray _name : REGISTER(t, _reg); \ 120 | 121 | #define UIMAGE2D_ARRAY_RO(_name, _format, _reg) IMAGE2D_ARRAY_RO(_name, _format, _reg) 122 | 123 | #define IMAGE2D_ARRAY_WR( _name, _format, _reg) \ 124 | WRITEONLY FORMAT(_format) RWTexture2DArray _name : REGISTER(u, _reg); \ 125 | 126 | #define UIMAGE2D_ARRAY_WR(_name, _format, _reg) IMAGE2D_ARRAY_WR(_name, _format, _reg) 127 | 128 | #define IMAGE2D_ARRAY_RW(_name, _format, _reg) \ 129 | FORMAT(_format) RWTexture2DArray _name : REGISTER(u, _reg); \ 130 | 131 | #define UIMAGE2D_ARRAY_RW(_name, _format, _reg) IMAGE2D_ARRAY_RW(_name, _format, _reg) 132 | 133 | #define IMAGE3D_RO( _name, _format, _reg) \ 134 | FORMAT(_format) Texture3D _name : REGISTER(t, _reg); 135 | 136 | #define UIMAGE3D_RO(_name, _format, _reg) IMAGE3D_RO(_name, _format, _reg) 137 | 138 | #define IMAGE3D_WR( _name, _format, _reg) \ 139 | WRITEONLY FORMAT(_format) RWTexture3D _name : REGISTER(u, _reg); 140 | 141 | #define UIMAGE3D_WR(_name, _format, _reg) IMAGE3D_RW(_name, _format, _reg) 142 | 143 | #define IMAGE3D_RW( _name, _format, _reg) \ 144 | FORMAT(_format) RWTexture3D _name : REGISTER(u, _reg); \ 145 | 146 | #define UIMAGE3D_RW(_name, _format, _reg) IMAGE3D_RW(_name, _format, _reg) 147 | 148 | #define BUFFER_RO(_name, _struct, _reg) StructuredBuffer<_struct> _name : REGISTER(t, _reg) 149 | #define BUFFER_RW(_name, _struct, _reg) RWStructuredBuffer <_struct> _name : REGISTER(u, _reg) 150 | #define BUFFER_WR(_name, _struct, _reg) BUFFER_RW(_name, _struct, _reg) 151 | 152 | #define NUM_THREADS(_x, _y, _z) [numthreads(_x, _y, _z)] 153 | 154 | #define __IMAGE_IMPL_A(_format, _storeComponents, _type, _loadComponents) \ 155 | _type imageLoad(Texture2D<_format> _image, ivec2 _uv) \ 156 | { \ 157 | return _image[_uv]._loadComponents; \ 158 | } \ 159 | \ 160 | ivec2 imageSize(Texture2D<_format> _image) \ 161 | { \ 162 | uvec2 result; \ 163 | _image.GetDimensions(result.x, result.y); \ 164 | return ivec2(result); \ 165 | } \ 166 | \ 167 | _type imageLoad(RWTexture2D<_format> _image, ivec2 _uv) \ 168 | { \ 169 | return _image[_uv]._loadComponents; \ 170 | } \ 171 | \ 172 | void imageStore(RWTexture2D<_format> _image, ivec2 _uv, _type _value) \ 173 | { \ 174 | _image[_uv] = _value._storeComponents; \ 175 | } \ 176 | \ 177 | ivec2 imageSize(RWTexture2D<_format> _image) \ 178 | { \ 179 | uvec2 result; \ 180 | _image.GetDimensions(result.x, result.y); \ 181 | return ivec2(result); \ 182 | } \ 183 | \ 184 | _type imageLoad(Texture2DArray<_format> _image, ivec3 _uvw) \ 185 | { \ 186 | return _image[_uvw]._loadComponents; \ 187 | } \ 188 | \ 189 | ivec3 imageSize(Texture2DArray<_format> _image) \ 190 | { \ 191 | uvec3 result; \ 192 | _image.GetDimensions(result.x, result.y, result.z); \ 193 | return ivec3(result); \ 194 | } \ 195 | \ 196 | _type imageLoad(RWTexture2DArray<_format> _image, ivec3 _uvw) \ 197 | { \ 198 | return _image[_uvw]._loadComponents; \ 199 | } \ 200 | \ 201 | void imageStore(RWTexture2DArray<_format> _image, ivec3 _uvw, _type _value) \ 202 | { \ 203 | _image[_uvw] = _value._storeComponents; \ 204 | } \ 205 | \ 206 | ivec3 imageSize(RWTexture2DArray<_format> _image) \ 207 | { \ 208 | uvec3 result; \ 209 | _image.GetDimensions(result.x, result.y, result.z); \ 210 | return ivec3(result); \ 211 | } \ 212 | \ 213 | _type imageLoad(Texture3D<_format> _image, ivec3 _uvw) \ 214 | { \ 215 | return _image[_uvw]._loadComponents; \ 216 | } \ 217 | \ 218 | ivec3 imageSize(Texture3D<_format> _image) \ 219 | { \ 220 | uvec3 result; \ 221 | _image.GetDimensions(result.x, result.y, result.z); \ 222 | return ivec3(result); \ 223 | } \ 224 | \ 225 | _type imageLoad(RWTexture3D<_format> _image, ivec3 _uvw) \ 226 | { \ 227 | return _image[_uvw]._loadComponents; \ 228 | } \ 229 | \ 230 | void imageStore(RWTexture3D<_format> _image, ivec3 _uvw, _type _value) \ 231 | { \ 232 | _image[_uvw] = _value._storeComponents; \ 233 | } \ 234 | \ 235 | ivec3 imageSize(RWTexture3D<_format> _image) \ 236 | { \ 237 | uvec3 result; \ 238 | _image.GetDimensions(result.x, result.y, result.z); \ 239 | return ivec3(result); \ 240 | } 241 | 242 | #define __IMAGE_IMPL_ATOMIC(_format, _storeComponents, _type, _loadComponents) \ 243 | \ 244 | void imageAtomicAdd(RWTexture2D<_format> _image, ivec2 _uv, _type _value) \ 245 | { \ 246 | InterlockedAdd(_image[_uv], _value._storeComponents); \ 247 | } \ 248 | 249 | 250 | __IMAGE_IMPL_A(float, x, vec4, xxxx) 251 | __IMAGE_IMPL_A(float2, xy, vec4, xyyy) 252 | __IMAGE_IMPL_A(float4, xyzw, vec4, xyzw) 253 | 254 | __IMAGE_IMPL_A(uint, x, uvec4, xxxx) 255 | __IMAGE_IMPL_A(uint2, xy, uvec4, xyyy) 256 | __IMAGE_IMPL_A(uint4, xyzw, uvec4, xyzw) 257 | 258 | #if BGFX_SHADER_LANGUAGE_HLSL 259 | __IMAGE_IMPL_A(unorm float, x, vec4, xxxx) 260 | __IMAGE_IMPL_A(unorm float2, xy, vec4, xyyy) 261 | __IMAGE_IMPL_A(unorm float4, xyzw, vec4, xyzw) 262 | #endif 263 | 264 | __IMAGE_IMPL_ATOMIC(uint, x, uvec4, xxxx) 265 | 266 | 267 | #define atomicAdd(_mem, _data) InterlockedAdd(_mem, _data) 268 | #define atomicAnd(_mem, _data) InterlockedAnd(_mem, _data) 269 | #define atomicMax(_mem, _data) InterlockedMax(_mem, _data) 270 | #define atomicMin(_mem, _data) InterlockedMin(_mem, _data) 271 | #define atomicOr(_mem, _data) InterlockedOr(_mem, _data) 272 | #define atomicXor(_mem, _data) InterlockedXor(_mem, _data) 273 | #define atomicFetchAndAdd(_mem, _data, _original) InterlockedAdd(_mem, _data, _original) 274 | #define atomicFetchAndAnd(_mem, _data, _original) InterlockedAnd(_mem, _data, _original) 275 | #define atomicFetchAndMax(_mem, _data, _original) InterlockedMax(_mem, _data, _original) 276 | #define atomicFetchAndMin(_mem, _data, _original) InterlockedMin(_mem, _data, _original) 277 | #define atomicFetchAndOr(_mem, _data, _original) InterlockedOr(_mem, _data, _original) 278 | #define atomicFetchAndXor(_mem, _data, _original) InterlockedXor(_mem, _data, _original) 279 | #define atomicFetchAndExchange(_mem, _data, _original) InterlockedExchange(_mem, _data, _original) 280 | #define atomicFetchCompareExchange(_mem, _compare, _data, _original) InterlockedCompareExchange(_mem,_compare, _data, _original) 281 | 282 | // InterlockedCompareStore 283 | 284 | #define barrier() GroupMemoryBarrierWithGroupSync() 285 | #define memoryBarrier() GroupMemoryBarrierWithGroupSync() 286 | #define memoryBarrierAtomicCounter() GroupMemoryBarrierWithGroupSync() 287 | #define memoryBarrierBuffer() AllMemoryBarrierWithGroupSync() 288 | #define memoryBarrierImage() GroupMemoryBarrierWithGroupSync() 289 | #define memoryBarrierShared() GroupMemoryBarrierWithGroupSync() 290 | #define groupMemoryBarrier() GroupMemoryBarrierWithGroupSync() 291 | 292 | #endif // BGFX_SHADER_LANGUAGE_GLSL 293 | 294 | #define IMAGE2D_RO_AUTOREG( _name, _format) IMAGE2D_RO(_name, _format, _name ## _REG) 295 | #define UIMAGE2D_RO_AUTOREG(_name, _format) UIMAGE2D_RO(_name, _format, _name ## _REG) 296 | #define IMAGE2D_WR_AUTOREG( _name, _format) IMAGE2D_WR(_name, _format, _name ## _REG) 297 | #define UIMAGE2D_WR_AUTOREG(_name, _format) UIMAGE2D_WR(_name, _format, _name ## _REG) 298 | #define IMAGE2D_RW_AUTOREG( _name, _format) IMAGE2D_RW(_name, _format, _name ## _REG) 299 | #define UIMAGE2D_RW_AUTOREG(_name, _format) UIMAGE2D_RW(_name, _format, _name ## _REG) 300 | 301 | #define IMAGE2D_ARRAY_RO_AUTOREG( _name, _format) IMAGE2D_ARRAY_RO(_name, _format, _name ## _REG) 302 | #define UIMAGE2D_ARRAY_RO_AUTOREG(_name, _format) UIMAGE2D_ARRAY_RO(_name, _format, _name ## _REG) 303 | #define IMAGE2D_ARRAY_WR_AUTOREG( _name, _format) IMAGE2D_ARRAY_WR(_name, _format, _name ## _REG) 304 | #define UIMAGE2D_ARRAY_WR_AUTOREG(_name, _format) UIMAGE2D_ARRAY_WR(_name, _format, _name ## _REG) 305 | #define IMAGE2D_ARRAY_RW_AUTOREG( _name, _format) IMAGE2D_ARRAY_RW(_name, _format, _name ## _REG) 306 | #define UIMAGE2D_ARRAY_RW_AUTOREG(_name, _format) UIMAGE2D_ARRAY_RW(_name, _format, _name ## _REG) 307 | 308 | #define IMAGE3D_RO_AUTOREG( _name, _format) IMAGE3D_RO(_name, _format, _name ## _REG) 309 | #define UIMAGE3D_RO_AUTOREG(_name, _format) UIMAGE3D_RO(_name, _format, _name ## _REG) 310 | #define IMAGE3D_WR_AUTOREG( _name, _format) IMAGE3D_WR(_name, _format, _name ## _REG) 311 | #define UIMAGE3D_WR_AUTOREG(_name, _format) UIMAGE3D_WR(_name, _format, _name ## _REG) 312 | #define IMAGE3D_RW_AUTOREG( _name, _format) IMAGE3D_RW(_name, _format, _name ## _REG) 313 | #define UIMAGE3D_RW_AUTOREG(_name, _format) UIMAGE3D_RW(_name, _format, _name ## _REG) 314 | 315 | #define BUFFER_RO_AUTOREG(_name, _type) BUFFER_RO(_name, _type, _name ## _REG) 316 | #define BUFFER_RW_AUTOREG(_name, _type) BUFFER_RW(_name, _type, _name ## _REG) 317 | #define BUFFER_WR_AUTOREG(_name, _type) BUFFER_WR(_name, _type, _name ## _REG) 318 | 319 | #define dispatchIndirect( \ 320 | _buffer \ 321 | , _offset \ 322 | , _numX \ 323 | , _numY \ 324 | , _numZ \ 325 | ) \ 326 | _buffer[(_offset)*2+0] = uvec4(_numX, _numY, _numZ, 0u) 327 | 328 | #define drawIndirect( \ 329 | _buffer \ 330 | , _offset \ 331 | , _numVertices \ 332 | , _numInstances \ 333 | , _startVertex \ 334 | , _startInstance \ 335 | ) \ 336 | _buffer[(_offset)*2+0] = uvec4(_numVertices, _numInstances, _startVertex, _startInstance) 337 | 338 | #define drawIndexedIndirect( \ 339 | _buffer \ 340 | , _offset \ 341 | , _numIndices \ 342 | , _numInstances \ 343 | , _startIndex \ 344 | , _startVertex \ 345 | , _startInstance \ 346 | ) \ 347 | _buffer[(_offset)*2+0] = uvec4(_numIndices, _numInstances, _startIndex, _startVertex); \ 348 | _buffer[(_offset)*2+1] = uvec4(_startInstance, 0u, 0u, 0u) 349 | 350 | #endif // __cplusplus 351 | 352 | #endif // BGFX_COMPUTE_H_HEADER_GUARD 353 | -------------------------------------------------------------------------------- /materials/Actor/defines.json: -------------------------------------------------------------------------------- 1 | { 2 | "flag": { 3 | "Change_Color": { 4 | "On": ["CHANGE_COLOR"], 5 | "Multi": ["CHANGE_COLOR_MULTI"], 6 | "Off": [] 7 | }, 8 | "Emissive": { 9 | "Emissive": ["EMISSIVE"], 10 | "EmissiveOnly": ["EMISSIVE_ONLY"], 11 | "Off": [] 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /materials/Actor/src/Actor.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_color0, v_fog, v_light, v_texcoord0 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | uniform vec4 ColorBased; 8 | uniform vec4 ChangeColor; 9 | uniform vec4 UseAlphaRewrite; 10 | uniform vec4 TintedAlphaTestEnabled; 11 | uniform vec4 MatColor; 12 | uniform vec4 OverlayColor; 13 | uniform vec4 TileLightColor; 14 | uniform vec4 MultiplicativeTintColor; 15 | uniform vec4 FogColor; 16 | uniform vec4 FogControl; 17 | uniform vec4 ActorFPEpsilon; 18 | uniform vec4 LightDiffuseColorAndIlluminance; 19 | uniform vec4 LightWorldSpaceDirection; 20 | uniform vec4 HudOpacity; 21 | uniform vec4 UVAnimation; 22 | uniform mat4 Bones[8]; 23 | 24 | SAMPLER2D_AUTOREG(s_MatTexture); 25 | SAMPLER2D_AUTOREG(s_MatTexture1); 26 | 27 | void main() { 28 | #if DEPTH_ONLY 29 | gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); 30 | return; 31 | #elif DEPTH_ONLY_OPAQUE 32 | gl_FragColor = vec4(applyFog(vec3(1.0, 1.0, 1.0), v_fog.rgb, v_fog.a), 1.0); 33 | return; 34 | #else 35 | 36 | vec4 albedo = getActorAlbedoNoColorChange(v_texcoord0, s_MatTexture, s_MatTexture1, MatColor); 37 | 38 | #if ALPHA_TEST 39 | float alpha = mix(albedo.a, (albedo.a * OverlayColor.a), TintedAlphaTestEnabled.x); 40 | if(shouldDiscard(albedo.rgb, alpha, ActorFPEpsilon.x)) { 41 | discard; 42 | } 43 | #endif // ALPHA_TEST 44 | 45 | #if CHANGE_COLOR_MULTI 46 | albedo = applyMultiColorChange(albedo, ChangeColor.rgb, MultiplicativeTintColor.rgb); 47 | #elif CHANGE_COLOR 48 | albedo = applyColorChange(albedo, ChangeColor, albedo.a); 49 | albedo.a *= ChangeColor.a; 50 | #endif // CHANGE_COLOR_MULTI 51 | 52 | #if ALPHA_TEST 53 | albedo.a = max(UseAlphaRewrite.r, albedo.a); 54 | #endif 55 | 56 | albedo = applyActorDiffuse(albedo, v_color0.rgb, v_light, ColorBased.x, OverlayColor); 57 | 58 | #if TRANSPARENT 59 | albedo = applyHudOpacity(albedo, HudOpacity.x); 60 | #endif 61 | 62 | albedo.rgb = applyFog(albedo.rgb, v_fog.rgb, v_fog.a); 63 | gl_FragColor = albedo; 64 | #endif // DEPTH_ONLY 65 | } 66 | -------------------------------------------------------------------------------- /materials/Actor/src/Actor.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec4 a_color0 : COLOR0; 2 | vec4 a_normal : NORMAL; 3 | vec3 a_position : POSITION; 4 | vec2 a_texcoord0 : TEXCOORD0; 5 | 6 | #if BGFX_SHADER_LANGUAGE_HLSL 7 | int a_indices : BLENDINDICES; 8 | #else 9 | float a_indices : BLENDINDICES; 10 | #endif 11 | 12 | vec4 i_data0 : TEXCOORD7; 13 | vec4 i_data1 : TEXCOORD6; 14 | vec4 i_data2 : TEXCOORD5; 15 | 16 | vec4 v_color0 : COLOR0; 17 | vec4 v_fog : COLOR2; 18 | vec4 v_light : COLOR3; 19 | centroid vec2 v_texcoord0 : TEXCOORD0; 20 | -------------------------------------------------------------------------------- /materials/Actor/src/Actor.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_color0, a_texcoord0, a_indices, a_normal 2 | #ifdef INSTANCING 3 | $input i_data0, i_data1, i_data2 4 | #endif 5 | 6 | $output v_color0, v_fog, v_light, v_texcoord0 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | uniform vec4 ColorBased; 14 | uniform vec4 ChangeColor; 15 | uniform vec4 UseAlphaRewrite; 16 | uniform vec4 TintedAlphaTestEnabled; 17 | uniform vec4 MatColor; 18 | uniform vec4 OverlayColor; 19 | uniform vec4 TileLightColor; 20 | uniform vec4 MultiplicativeTintColor; 21 | uniform vec4 FogColor; 22 | uniform vec4 FogControl; 23 | uniform vec4 ActorFPEpsilon; 24 | uniform vec4 LightDiffuseColorAndIntensity; 25 | uniform vec4 LightWorldSpaceDirection; 26 | uniform vec4 HudOpacity; 27 | uniform vec4 UVAnimation; 28 | uniform mat4 Bones[8]; 29 | 30 | void main() { 31 | mat4 World = u_model[0]; 32 | 33 | //StandardTemplate_InvokeVertexPreprocessFunction 34 | World = mul(World, Bones[int(a_indices)]); 35 | 36 | vec2 texcoord0 = a_texcoord0; 37 | texcoord0 = applyUvAnimation(texcoord0, UVAnimation); 38 | 39 | float lightIntensity = calculateLightIntensity(World, vec4(a_normal.xyz, 0.0), TileLightColor); 40 | lightIntensity += OverlayColor.a * 0.35; 41 | vec4 light = vec4(lightIntensity * TileLightColor.rgb, 1.0); 42 | 43 | //StandardTemplate_VertSharedTransform 44 | vec3 worldPosition; 45 | #ifdef INSTANCING 46 | mat4 model; 47 | model[0] = vec4(i_data0.x, i_data1.x, i_data2.x, 0); 48 | model[1] = vec4(i_data0.y, i_data1.y, i_data2.y, 0); 49 | model[2] = vec4(i_data0.z, i_data1.z, i_data2.z, 0); 50 | model[3] = vec4(i_data0.w, i_data1.w, i_data2.w, 1); 51 | worldPosition = instMul(model, vec4(a_position, 1.0)).xyz; 52 | #else 53 | worldPosition = mul(World, vec4(a_position, 1.0)).xyz; 54 | #endif 55 | 56 | vec4 position;// = mul(u_viewProj, vec4(worldPosition, 1.0)); 57 | 58 | //StandardTemplate_InvokeVertexOverrideFunction 59 | position = jitterVertexPosition(worldPosition); 60 | float cameraDepth = position.z; 61 | float fogIntensity = calculateFogIntensity(cameraDepth, FogControl.z, FogControl.x, FogControl.y); 62 | vec4 fog = vec4(FogColor.rgb, fogIntensity); 63 | 64 | #if defined(DEPTH_ONLY) 65 | v_texcoord0 = vec2(0.0, 0.0); 66 | v_color0 = vec4(0.0, 0.0, 0.0, 0.0); 67 | #else 68 | v_texcoord0 = texcoord0; 69 | v_color0 = a_color0; 70 | #endif 71 | 72 | v_fog = fog; 73 | v_light = light; 74 | gl_Position = position; 75 | } 76 | -------------------------------------------------------------------------------- /materials/ActorBanner/defines.json: -------------------------------------------------------------------------------- 1 | { 2 | "flag": { 3 | "Change_Color": { 4 | "On": ["CHANGE_COLOR"], 5 | "Multi": ["CHANGE_COLOR_MULTI"], 6 | "Off": [] 7 | }, 8 | "Emissive": { 9 | "Emissive": ["EMISSIVE"], 10 | "EmissiveOnly": ["EMISSIVE_ONLY"], 11 | "Off": [] 12 | }, 13 | "Tinting": { 14 | "Enabled": ["TINTING"], 15 | "Disabled": [] 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /materials/ActorBanner/src/ActorBanner.fragment.sc: -------------------------------------------------------------------------------- 1 | //Not yet completed, may have some issues 2 | 3 | $input v_color0, v_fog, v_light, v_texcoord0, v_texcoords 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | uniform vec4 ColorBased; 10 | uniform vec4 ChangeColor; 11 | uniform vec4 UseAlphaRewrite; 12 | uniform vec4 TintedAlphaTestEnabled; 13 | uniform vec4 MatColor; 14 | uniform vec4 OverlayColor; 15 | uniform vec4 TileLightColor; 16 | uniform vec4 MultiplicativeTintColor; 17 | uniform vec4 FogColor; 18 | uniform vec4 FogControl; 19 | uniform vec4 ActorFPEpsilon; 20 | uniform vec4 LightDiffuseColorAndIlluminance; 21 | uniform vec4 LightWorldSpaceDirection; 22 | uniform vec4 HudOpacity; 23 | uniform vec4 UVAnimation; 24 | uniform mat4 Bones[8]; 25 | uniform vec4 BannerColors[7]; 26 | uniform vec4 BannerUVOffsetsAndScales[7]; 27 | 28 | SAMPLER2D_AUTOREG(s_MatTexture); 29 | SAMPLER2D_AUTOREG(s_MatTexture1); 30 | 31 | void main() { 32 | #if DEPTH_ONLY 33 | gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); 34 | return; 35 | #elif DEPTH_ONLY_OPAQUE 36 | gl_FragColor = vec4(applyFog(vec3(1.0, 1.0, 1.0), v_fog.rgb, v_fog.a), 1.0); 37 | return; 38 | #else 39 | 40 | #if !ALPHA_TEST 41 | vec4 diffuse = texture2D(s_MatTexture, v_texcoords.xy); 42 | vec4 base = texture2D(s_MatTexture, v_texcoords.zw); 43 | 44 | #if TINTING 45 | base.a = mix(diffuse.r * diffuse.a, diffuse.a, v_color0.a); 46 | base.rgb *= v_color0.rgb; 47 | #endif 48 | 49 | base = applyLighting(base, v_light); 50 | base = applyHudOpacity(base, HudOpacity.x); 51 | base.rgb = applyFog(base.rgb, v_fog.rgb, v_fog.a); 52 | 53 | gl_FragColor = base; 54 | #else 55 | vec4 albedo = getActorAlbedoNoColorChange(v_texcoord0, s_MatTexture, s_MatTexture1, MatColor); 56 | 57 | float alpha = mix(albedo.a, (albedo.a * OverlayColor.a), TintedAlphaTestEnabled.x); 58 | if(shouldDiscard(albedo.rgb, alpha, ActorFPEpsilon.x)) { 59 | discard; 60 | } 61 | 62 | #if CHANGE_COLOR_MULTI 63 | albedo = applyMultiColorChange(albedo, ChangeColor.rgb, MultiplicativeTintColor.rgb); 64 | #elif CHANGE_COLOR 65 | albedo = applyColorChange(albedo, ChangeColor, albedo.a); 66 | albedo.a *= ChangeColor.a; 67 | #endif // CHANGE_COLOR_MULTI 68 | 69 | albedo.a = max(UseAlphaRewrite.r, albedo.a); 70 | albedo = applyActorDiffuse(albedo, v_color0.rgb, v_light, ColorBased.x, OverlayColor); 71 | albedo = applyHudOpacity(albedo, HudOpacity.x); 72 | albedo.rgb = applyFog(albedo.rgb, v_fog.rgb, v_fog.a); 73 | gl_FragColor = albedo; 74 | #endif // !ALPHA_TEST 75 | 76 | #endif // DEPTH_ONLY 77 | } 78 | -------------------------------------------------------------------------------- /materials/ActorBanner/src/ActorBanner.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec4 a_color0 : COLOR0; 2 | vec4 a_normal : NORMAL; 3 | vec3 a_position : POSITION; 4 | vec2 a_texcoord0 : TEXCOORD0; 5 | 6 | #if BGFX_SHADER_LANGUAGE_HLSL 7 | int a_indices : BLENDINDICES; 8 | #else 9 | float a_indices : BLENDINDICES; 10 | #endif 11 | 12 | vec4 i_data0 : TEXCOORD7; 13 | vec4 i_data1 : TEXCOORD6; 14 | vec4 i_data2 : TEXCOORD5; 15 | 16 | vec4 v_color0 : COLOR0; 17 | vec4 v_fog : COLOR2; 18 | vec4 v_light : COLOR3; 19 | centroid vec2 v_texcoord0 : TEXCOORD0; 20 | centroid vec4 v_texcoords : TEXCOORD2; 21 | -------------------------------------------------------------------------------- /materials/ActorBanner/src/ActorBanner.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_color0, a_texcoord0, a_indices, a_normal 2 | #ifdef INSTANCING 3 | $input i_data0, i_data1, i_data2 4 | #endif 5 | 6 | $output v_color0, v_fog, v_light, v_texcoord0, v_texcoords 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | uniform vec4 ColorBased; 14 | uniform vec4 ChangeColor; 15 | uniform vec4 UseAlphaRewrite; 16 | uniform vec4 TintedAlphaTestEnabled; 17 | uniform vec4 MatColor; 18 | uniform vec4 OverlayColor; 19 | uniform vec4 TileLightColor; 20 | uniform vec4 MultiplicativeTintColor; 21 | uniform vec4 FogColor; 22 | uniform vec4 FogControl; 23 | uniform vec4 ActorFPEpsilon; 24 | uniform vec4 LightDiffuseColorAndIntensity; 25 | uniform vec4 LightWorldSpaceDirection; 26 | uniform vec4 HudOpacity; 27 | uniform vec4 UVAnimation; 28 | uniform mat4 Bones[8]; 29 | uniform vec4 BannerColors[7]; 30 | uniform vec4 BannerUVOffsetsAndScales[7]; 31 | 32 | void main() { 33 | mat4 World = u_model[0]; 34 | 35 | //StandardTemplate_InvokeVertexPreprocessFunction 36 | World = mul(World, Bones[int(a_indices)]); 37 | 38 | vec2 texcoord0 = a_texcoord0; 39 | texcoord0 = applyUvAnimation(texcoord0, UVAnimation); 40 | 41 | float lightIntensity = calculateLightIntensity(World, vec4(a_normal.xyz, 0.0), TileLightColor); 42 | lightIntensity += OverlayColor.a * 0.35; 43 | vec4 light = vec4(lightIntensity * TileLightColor.rgb, 1.0); 44 | 45 | //StandardTemplate_VertSharedTransform 46 | vec3 worldPosition; 47 | #ifdef INSTANCING 48 | mat4 model; 49 | model[0] = vec4(i_data0.x, i_data1.x, i_data2.x, 0); 50 | model[1] = vec4(i_data0.y, i_data1.y, i_data2.y, 0); 51 | model[2] = vec4(i_data0.z, i_data1.z, i_data2.z, 0); 52 | model[3] = vec4(i_data0.w, i_data1.w, i_data2.w, 1); 53 | worldPosition = instMul(model, vec4(a_position, 1.0)).xyz; 54 | #else 55 | worldPosition = mul(World, vec4(a_position, 1.0)).xyz; 56 | #endif 57 | 58 | vec4 position;// = mul(u_viewProj, vec4(worldPosition, 1.0)); 59 | 60 | //StandardTemplate_InvokeVertexOverrideFunction 61 | position = jitterVertexPosition(worldPosition); 62 | float cameraDepth = position.z; 63 | float fogIntensity = calculateFogIntensity(cameraDepth, FogControl.z, FogControl.x, FogControl.y); 64 | vec4 fog = vec4(FogColor.rgb, fogIntensity); 65 | 66 | vec4 texcoords; 67 | int frameIndex = int(a_color0.w * 255.0); 68 | texcoords.xy = (texcoord0 * BannerUVOffsetsAndScales[frameIndex].zw) + BannerUVOffsetsAndScales[frameIndex].xy; 69 | texcoords.zw = (texcoord0 * BannerUVOffsetsAndScales[0].zw) + BannerUVOffsetsAndScales[0].xy; 70 | 71 | vec4 color; 72 | #if !ALPHA_TEST && !DEPTH_ONLY_OPAQUE && TINTING 73 | color = BannerColors[frameIndex]; 74 | color.a = 1.0; 75 | if (frameIndex > 0) { 76 | color.a = 0.0; 77 | } 78 | #else 79 | color = a_color0; 80 | #endif 81 | 82 | #if DEPTH_ONLY 83 | v_texcoord0 = vec2(0.0, 0.0); 84 | v_color0 = vec4(0.0, 0.0, 0.0, 0.0); 85 | #else 86 | v_texcoord0 = texcoord0; 87 | v_color0 = color; 88 | #endif 89 | 90 | #if ALPHA_TEST || DEPTH_ONLY_OPAQUE 91 | v_texcoords = vec4(0.0, 0.0, 0.0, 0.0); 92 | #else 93 | v_texcoords = texcoords; 94 | #endif 95 | 96 | v_fog = fog; 97 | v_light = light; 98 | gl_Position = position; 99 | } 100 | -------------------------------------------------------------------------------- /materials/ActorGlint/defines.json: -------------------------------------------------------------------------------- 1 | { 2 | "flag": { 3 | "Change_Color": { 4 | "On": ["CHANGE_COLOR"], 5 | "Multi": ["CHANGE_COLOR_MULTI"], 6 | "Off": [] 7 | }, 8 | "Emissive": { 9 | "Emissive": ["EMISSIVE"], 10 | "EmissiveOnly": ["EMISSIVE_ONLY"], 11 | "Off": [] 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /materials/ActorGlint/src/ActorGlint.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_color0, v_fog, v_light, v_texcoord0, v_layerUv 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | uniform vec4 ColorBased; 9 | uniform vec4 ChangeColor; 10 | uniform vec4 UseAlphaRewrite; 11 | uniform vec4 TintedAlphaTestEnabled; 12 | uniform vec4 MatColor; 13 | uniform vec4 OverlayColor; 14 | uniform vec4 TileLightColor; 15 | uniform vec4 MultiplicativeTintColor; 16 | uniform vec4 FogColor; 17 | uniform vec4 FogControl; 18 | uniform vec4 ActorFPEpsilon; 19 | uniform vec4 LightDiffuseColorAndIlluminance; 20 | uniform vec4 LightWorldSpaceDirection; 21 | uniform vec4 HudOpacity; 22 | uniform vec4 UVAnimation; 23 | uniform mat4 Bones[8]; 24 | uniform vec4 UVScale; 25 | uniform vec4 GlintColor; 26 | 27 | SAMPLER2D_AUTOREG(s_MatTexture); 28 | SAMPLER2D_AUTOREG(s_MatTexture1); 29 | 30 | void main() { 31 | #if DEPTH_ONLY 32 | gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); 33 | return; 34 | #elif DEPTH_ONLY_OPAQUE 35 | gl_FragColor = vec4(applyFog(vec3(1.0, 1.0, 1.0), v_fog.rgb, v_fog.a), 1.0); 36 | return; 37 | #else 38 | 39 | vec4 albedo = getActorAlbedoNoColorChange(v_texcoord0, s_MatTexture, s_MatTexture1, MatColor); 40 | 41 | #if ALPHA_TEST 42 | float alpha = mix(albedo.a, (albedo.a * OverlayColor.a), TintedAlphaTestEnabled.x); 43 | if(shouldDiscard(albedo.rgb, alpha, ActorFPEpsilon.x)) { 44 | discard; 45 | } 46 | #endif // ALPHA_TEST 47 | 48 | #if CHANGE_COLOR_MULTI 49 | albedo = applyMultiColorChange(albedo, ChangeColor.rgb, MultiplicativeTintColor.rgb); 50 | #elif CHANGE_COLOR 51 | albedo = applyColorChange(albedo, ChangeColor, albedo.a); 52 | albedo.a *= ChangeColor.a; 53 | #endif // CHANGE_COLOR_MULTI 54 | 55 | #if ALPHA_TEST 56 | albedo.a = max(UseAlphaRewrite.r, albedo.a); 57 | #endif 58 | 59 | albedo = applyActorDiffuse(albedo, v_color0.rgb, v_light, ColorBased.x, OverlayColor); 60 | albedo = applyGlint(albedo, v_layerUv, s_MatTexture1, GlintColor, TileLightColor); 61 | 62 | #if TRANSPARENT 63 | albedo = applyHudOpacity(albedo, HudOpacity.x); 64 | #endif 65 | 66 | albedo.rgb = applyFog(albedo.rgb, v_fog.rgb, v_fog.a); 67 | gl_FragColor = albedo; 68 | #endif // DEPTH_ONLY 69 | } 70 | -------------------------------------------------------------------------------- /materials/ActorGlint/src/ActorGlint.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec4 a_color0 : COLOR0; 2 | vec4 a_normal : NORMAL; 3 | vec3 a_position : POSITION; 4 | vec2 a_texcoord0 : TEXCOORD0; 5 | 6 | #if BGFX_SHADER_LANGUAGE_HLSL 7 | int a_indices : BLENDINDICES; 8 | #else 9 | float a_indices : BLENDINDICES; 10 | #endif 11 | 12 | vec4 i_data0 : TEXCOORD7; 13 | vec4 i_data1 : TEXCOORD6; 14 | vec4 i_data2 : TEXCOORD5; 15 | 16 | vec4 v_color0 : COLOR0; 17 | vec4 v_fog : COLOR2; 18 | vec4 v_light : COLOR3; 19 | centroid vec2 v_texcoord0 : TEXCOORD0; 20 | vec4 v_layerUv : TEXCOORD2; -------------------------------------------------------------------------------- /materials/ActorGlint/src/ActorGlint.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_color0, a_texcoord0, a_indices, a_normal 2 | #ifdef INSTANCING 3 | $input i_data0, i_data1, i_data2 4 | #endif 5 | 6 | $output v_color0, v_fog, v_light, v_texcoord0, v_layerUv 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | uniform vec4 ColorBased; 15 | uniform vec4 ChangeColor; 16 | uniform vec4 UseAlphaRewrite; 17 | uniform vec4 TintedAlphaTestEnabled; 18 | uniform vec4 MatColor; 19 | uniform vec4 OverlayColor; 20 | uniform vec4 TileLightColor; 21 | uniform vec4 MultiplicativeTintColor; 22 | uniform vec4 FogColor; 23 | uniform vec4 FogControl; 24 | uniform vec4 ActorFPEpsilon; 25 | uniform vec4 LightDiffuseColorAndIntensity; 26 | uniform vec4 LightWorldSpaceDirection; 27 | uniform vec4 HudOpacity; 28 | uniform vec4 UVAnimation; 29 | uniform mat4 Bones[8]; 30 | uniform vec4 UVScale; 31 | uniform vec4 GlintColor; 32 | 33 | void main() { 34 | mat4 World = u_model[0]; 35 | 36 | //StandardTemplate_InvokeVertexPreprocessFunction 37 | World = mul(World, Bones[int(a_indices)]); 38 | 39 | vec2 texcoord0 = a_texcoord0; 40 | #if DEPTH_ONLY_OPAQUE 41 | texcoord0 = applyUvAnimation(texcoord0, UVAnimation); 42 | #endif 43 | 44 | vec2 layer1UV = calculateLayerUV(a_texcoord0, UVAnimation.x, UVAnimation.z, UVScale.xy); 45 | vec2 layer2UV = calculateLayerUV(a_texcoord0, UVAnimation.y, UVAnimation.w, UVScale.xy); 46 | vec4 layerUV = vec4(layer1UV, layer2UV); 47 | 48 | float lightIntensity = calculateLightIntensity(World, vec4(a_normal.xyz, 0.0), TileLightColor); 49 | lightIntensity += OverlayColor.a * 0.35; 50 | vec4 light = vec4(lightIntensity * TileLightColor.rgb, 1.0); 51 | 52 | //StandardTemplate_VertSharedTransform 53 | vec3 worldPosition; 54 | #ifdef INSTANCING 55 | mat4 model; 56 | model[0] = vec4(i_data0.x, i_data1.x, i_data2.x, 0); 57 | model[1] = vec4(i_data0.y, i_data1.y, i_data2.y, 0); 58 | model[2] = vec4(i_data0.z, i_data1.z, i_data2.z, 0); 59 | model[3] = vec4(i_data0.w, i_data1.w, i_data2.w, 1); 60 | worldPosition = instMul(model, vec4(a_position, 1.0)).xyz; 61 | #else 62 | worldPosition = mul(World, vec4(a_position, 1.0)).xyz; 63 | #endif 64 | 65 | vec4 position;// = mul(u_viewProj, vec4(worldPosition, 1.0)); 66 | 67 | //StandardTemplate_InvokeVertexOverrideFunction 68 | position = jitterVertexPosition(worldPosition); 69 | float cameraDepth = position.z; 70 | float fogIntensity = calculateFogIntensity(cameraDepth, FogControl.z, FogControl.x, FogControl.y); 71 | vec4 fog = vec4(FogColor.rgb, fogIntensity); 72 | 73 | #if defined(DEPTH_ONLY) 74 | v_texcoord0 = vec2(0.0, 0.0); 75 | v_color0 = vec4(0.0, 0.0, 0.0, 0.0); 76 | #else 77 | v_texcoord0 = texcoord0; 78 | v_color0 = a_color0; 79 | #endif 80 | 81 | v_layerUv = layerUV; 82 | v_fog = fog; 83 | v_light = light; 84 | gl_Position = position; 85 | } 86 | -------------------------------------------------------------------------------- /materials/BlockSelectionOverlay/src/BlockSelectionOverlay.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_texcoord0 2 | 3 | #include 4 | uniform vec4 MatColor; 5 | 6 | SAMPLER2D_AUTOREG(s_MatTexture); 7 | 8 | void main() { 9 | vec4 diffuse = texture2D(s_MatTexture, v_texcoord0); 10 | 11 | const float ALPHA_THRESHOLD = 0.5; 12 | if (diffuse.a < ALPHA_THRESHOLD) { 13 | discard; 14 | } 15 | 16 | gl_FragColor = MatColor; 17 | } 18 | -------------------------------------------------------------------------------- /materials/BlockSelectionOverlay/src/BlockSelectionOverlay.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec3 a_position : POSITION; 2 | vec2 a_texcoord0 : TEXCOORD0; 3 | 4 | vec2 v_texcoord0 : TEXCOORD0; -------------------------------------------------------------------------------- /materials/BlockSelectionOverlay/src/BlockSelectionOverlay.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_texcoord0 2 | $output v_texcoord0 3 | 4 | #include 5 | 6 | void main() { 7 | v_texcoord0 = a_texcoord0; 8 | vec4 position = mul(u_modelViewProj, vec4(a_position, 1.0)); 9 | position.z -= 1.0 / 8192.0; 10 | gl_Position = position; 11 | } 12 | -------------------------------------------------------------------------------- /materials/Bloom/src/Bloom.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_texcoord0 2 | 3 | #include 4 | 5 | uniform vec4 BloomParams1; 6 | uniform vec4 BloomParams2; 7 | uniform vec4 RenderMode; 8 | uniform vec4 ScreenSize; 9 | 10 | SAMPLER2D_AUTOREG(s_HDRi); 11 | SAMPLER2D_AUTOREG(s_BlurPyramidTexture); 12 | SAMPLER2D_AUTOREG(s_RasterColor); 13 | SAMPLER2D_AUTOREG(s_DepthTexture); 14 | 15 | float luminance(vec3 clr) { 16 | return dot(clr, vec3(0.2126, 0.7152, 0.0722)); 17 | } 18 | 19 | vec4 HighPass(vec4 col) { 20 | float lum = luminance(col.rgb); 21 | return vec4(col.rgb, lum); 22 | } 23 | 24 | vec4 HighPassDFDownsample(sampler2D srcImg, sampler2D depthImg, vec2 uv, vec2 pixelOffsets) { 25 | vec4 col = vec4(0, 0, 0, 0); 26 | col += 0.5 * HighPass(texture2D(srcImg, uv)); 27 | col += 0.125 * HighPass(texture2D(srcImg, uv + vec2(pixelOffsets.x, pixelOffsets.y))); 28 | col += 0.125 * HighPass(texture2D(srcImg, uv + vec2(-pixelOffsets.x, pixelOffsets.y))); 29 | col += 0.125 * HighPass(texture2D(srcImg, uv + vec2(pixelOffsets.x, -pixelOffsets.y))); 30 | col += 0.125 * HighPass(texture2D(srcImg, uv + vec2(-pixelOffsets.x, -pixelOffsets.y))); 31 | if (bool(BloomParams2.z)) { 32 | float minRange = BloomParams2.x; 33 | float maxRange = BloomParams2.y; 34 | float depth = texture2D(depthImg, uv).r; 35 | depth = ((depth * maxRange) - minRange) / (maxRange - minRange); 36 | depth = clamp(depth, BloomParams1.z, 1.0); 37 | col *= pow(depth, BloomParams1.y); 38 | } 39 | return col; 40 | } 41 | 42 | vec4 DualFilterDownsample(sampler2D srcImg, vec2 uv, vec2 pixelOffsets) { 43 | vec4 col = vec4(0, 0, 0, 0); 44 | col += 0.5 * texture2D(srcImg, uv); 45 | col += 0.125 * texture2D(srcImg, uv + vec2(pixelOffsets.x, pixelOffsets.y)); 46 | col += 0.125 * texture2D(srcImg, uv + vec2(-pixelOffsets.x, pixelOffsets.y)); 47 | col += 0.125 * texture2D(srcImg, uv + vec2(pixelOffsets.x, -pixelOffsets.y)); 48 | col += 0.125 * texture2D(srcImg, uv + vec2(-pixelOffsets.x, -pixelOffsets.y)); 49 | return col; 50 | } 51 | 52 | vec4 DualFilterDownsampleWithDepthErosion(sampler2D srcImg, vec2 uv, vec2 pixelOffsets) { 53 | vec4 col = vec4(0, 0, 0, 0); 54 | vec4 a = texture2D(srcImg, uv); 55 | vec4 b = texture2D(srcImg, uv + vec2(pixelOffsets.x, pixelOffsets.y)); 56 | vec4 c = texture2D(srcImg, uv + vec2(-pixelOffsets.x, pixelOffsets.y)); 57 | vec4 d = texture2D(srcImg, uv + vec2(pixelOffsets.x, -pixelOffsets.y)); 58 | vec4 e = texture2D(srcImg, uv + vec2(-pixelOffsets.x, -pixelOffsets.y)); 59 | col.rgb += 0.5 * a.rgb; 60 | col.rgb += 0.125 * b.rgb; 61 | col.rgb += 0.125 * c.rgb; 62 | col.rgb += 0.125 * d.rgb; 63 | col.rgb += 0.125 * e.rgb; 64 | col.a = max(a.a, max(b.a, max(c.a, max(d.a, e.a)))); 65 | return col; 66 | } 67 | 68 | vec4 DualFilterUpsample(sampler2D srcImg, vec2 uv, vec2 pixelOffsets) { 69 | vec4 col = vec4(0, 0, 0, 0); 70 | col += 0.166 * texture2D(srcImg, uv + vec2(0.5 * pixelOffsets.x, 0.5 * pixelOffsets.y)); 71 | col += 0.166 * texture2D(srcImg, uv + vec2(-0.5 * pixelOffsets.x, 0.5 * pixelOffsets.y)); 72 | col += 0.166 * texture2D(srcImg, uv + vec2(0.5 * pixelOffsets.x, -0.5 * pixelOffsets.y)); 73 | col += 0.166 * texture2D(srcImg, uv + vec2(-0.5 * pixelOffsets.x, -0.5 * pixelOffsets.y)); 74 | col += 0.083 * texture2D(srcImg, uv + vec2(pixelOffsets.x, pixelOffsets.y)); 75 | col += 0.083 * texture2D(srcImg, uv + vec2(-pixelOffsets.x, pixelOffsets.y)); 76 | col += 0.083 * texture2D(srcImg, uv + vec2(pixelOffsets.x, -pixelOffsets.y)); 77 | col += 0.083 * texture2D(srcImg, uv + vec2(-pixelOffsets.x, -pixelOffsets.y)); 78 | return col; 79 | } 80 | 81 | vec4 BloomHighPass(vec2 texcoord0) { 82 | float xOffset = 1.5 * abs(dFdx(texcoord0.x)); 83 | float yOffset = 1.5 * abs(dFdy(texcoord0.y)); 84 | vec2 uv = texcoord0; 85 | return HighPassDFDownsample(s_HDRi, s_DepthTexture, uv, vec2(xOffset, yOffset)); 86 | } 87 | 88 | vec4 DFDownSample(vec2 texcoord0) { 89 | float xOffset = 1.5 * abs(dFdx(texcoord0.x)); 90 | float yOffset = 1.5 * abs(dFdy(texcoord0.y)); 91 | vec2 uv = texcoord0; 92 | return DualFilterDownsample(s_BlurPyramidTexture, uv, vec2(xOffset, yOffset)); 93 | } 94 | 95 | vec4 DFDownSampleWithDepthErosion(vec2 texcoord0) { 96 | float xOffset = 1.5 * abs(dFdx(texcoord0.x)); 97 | float yOffset = 1.5 * abs(dFdy(texcoord0.y)); 98 | vec2 uv = texcoord0; 99 | return DualFilterDownsampleWithDepthErosion(s_BlurPyramidTexture, uv, vec2(xOffset, yOffset)); 100 | } 101 | 102 | vec4 DFUpSample(vec2 texcoord0) { 103 | float xOffset = 4.0 * abs(dFdx(texcoord0.x)); 104 | float yOffset = 4.0 * abs(dFdy(texcoord0.y)); 105 | vec2 uv = texcoord0; 106 | return DualFilterUpsample(s_BlurPyramidTexture, uv, vec2(xOffset, yOffset)); 107 | } 108 | 109 | vec4 BloomBlend(vec2 texcoord0) { 110 | float xOffset = 4.0 * abs(dFdx(texcoord0.x)); 111 | float yOffset = 4.0 * abs(dFdy(texcoord0.y)); 112 | vec2 uv = texcoord0; 113 | vec4 bloom = DualFilterUpsample(s_BlurPyramidTexture, uv, vec2(xOffset, yOffset)); 114 | vec3 baseColor = texture2D(s_HDRi, uv).rgb; 115 | float intensity = BloomParams1.x; 116 | vec3 bloomedColor = baseColor + (intensity * bloom.rgb); 117 | return vec4(bloomedColor, 1.0); 118 | } 119 | 120 | void main() { 121 | #if BLOOM_HIGH_PASS 122 | gl_FragColor = BloomHighPass(v_texcoord0); 123 | #elif DFDOWN_SAMPLE 124 | gl_FragColor = DFDownSample(v_texcoord0); 125 | #elif DFDOWN_SAMPLE_WITH_DEPTH_EROSION 126 | gl_FragColor = DFDownSampleWithDepthErosion(v_texcoord0); 127 | #elif DFUP_SAMPLE 128 | gl_FragColor = DFUpSample(v_texcoord0); 129 | #elif BLOOM_BLEND 130 | gl_FragColor = BloomBlend(v_texcoord0); 131 | #endif 132 | } 133 | -------------------------------------------------------------------------------- /materials/Bloom/src/Bloom.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec4 a_position : POSITION; 2 | vec2 a_texcoord0 : TEXCOORD0; 3 | 4 | vec2 v_texcoord0 : TEXCOORD0; -------------------------------------------------------------------------------- /materials/Bloom/src/Bloom.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_texcoord0 2 | $output v_texcoord0 3 | 4 | #include 5 | 6 | void main() { 7 | vec2 pos = (a_position.xy * 2.0) - 1.0; 8 | v_texcoord0 = a_texcoord0; 9 | gl_Position = vec4(pos, 0.0, 1.0); 10 | } 11 | -------------------------------------------------------------------------------- /materials/ClearVolume/src/ClearVolume.compute.sc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | uniform vec4 ClearValue; 4 | uniform vec4 VolumeDimensions; 5 | 6 | IMAGE2D_ARRAY_WR_AUTOREG(s_Volume, rgba16f); 7 | 8 | void Clear(uvec3 GlobalInvocationID) { 9 | int volumeWidth = int(VolumeDimensions.x); 10 | int volumeHeight = int(VolumeDimensions.y); 11 | int volumeDepth = int(VolumeDimensions.z); 12 | int x = int(GlobalInvocationID.x); 13 | int y = int(GlobalInvocationID.y); 14 | int z = int(GlobalInvocationID.z); 15 | if (x >= volumeWidth || y >= volumeHeight || z >= volumeDepth) { 16 | return; 17 | } 18 | imageStore(s_Volume, ivec3(x, y, z), ClearValue); 19 | } 20 | 21 | NUM_THREADS(8, 8, 8) 22 | void main() { 23 | Clear(gl_GlobalInvocationID); 24 | } -------------------------------------------------------------------------------- /materials/ClearVolume/src/ClearVolume.fragment.sc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() { 4 | gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); 5 | } 6 | -------------------------------------------------------------------------------- /materials/ClearVolume/src/ClearVolume.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec3 a_position : POSITION; 2 | -------------------------------------------------------------------------------- /materials/ClearVolume/src/ClearVolume.vertex.sc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() { 4 | gl_Position = vec4(0.0, 0.0, 0.0, 0.0); 5 | } 6 | -------------------------------------------------------------------------------- /materials/Cracks/src/Cracks.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_texcoord0 2 | 3 | #include 4 | 5 | SAMPLER2D_AUTOREG(s_CracksTexture); 6 | 7 | void main() { 8 | vec4 diffuse = texture2D(s_CracksTexture, v_texcoord0); 9 | 10 | const float ALPHA_THRESHOLD = 0.5; 11 | if (diffuse.a < ALPHA_THRESHOLD) { 12 | discard; 13 | } 14 | 15 | gl_FragColor = diffuse; 16 | } 17 | -------------------------------------------------------------------------------- /materials/Cracks/src/Cracks.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec3 a_position : POSITION; 2 | vec2 a_texcoord0 : TEXCOORD0; 3 | 4 | vec2 v_texcoord0 : TEXCOORD0; 5 | -------------------------------------------------------------------------------- /materials/Cracks/src/Cracks.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_texcoord0 2 | $output v_texcoord0 3 | 4 | #include 5 | #include 6 | 7 | void main() { 8 | v_texcoord0 = a_texcoord0; 9 | gl_Position = jitterVertexPosition(a_position); 10 | } 11 | -------------------------------------------------------------------------------- /materials/CracksBlockEntity/src/CracksBlockEntity.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_texcoord0 2 | 3 | #include 4 | 5 | SAMPLER2D_AUTOREG(s_CracksTexture); 6 | 7 | void main() { 8 | gl_FragColor = texture2D(s_CracksTexture, v_texcoord0); 9 | } 10 | -------------------------------------------------------------------------------- /materials/CracksBlockEntity/src/CracksBlockEntity.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec3 a_position : POSITION; 2 | vec2 a_texcoord0 : TEXCOORD0; 3 | 4 | #if BGFX_SHADER_LANGUAGE_HLSL 5 | int a_indices : BLENDINDICES; 6 | #else 7 | float a_indices : BLENDINDICES; 8 | #endif 9 | 10 | vec2 v_texcoord0 : TEXCOORD0; 11 | -------------------------------------------------------------------------------- /materials/CracksBlockEntity/src/CracksBlockEntity.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_texcoord0, a_indices 2 | $output v_texcoord0 3 | 4 | #include 5 | #include 6 | 7 | uniform mat4 Bones[8]; 8 | uniform vec4 UVScale; 9 | 10 | void main() { 11 | mat4 World = u_model[0]; 12 | World = mul(World, Bones[int(a_indices)]); 13 | v_texcoord0 = a_texcoord0 * UVScale.xy; 14 | vec3 worldPosition = mul(World, vec4(a_position, 1.0)).xyz; 15 | gl_Position = jitterVertexPosition(worldPosition); 16 | } 17 | -------------------------------------------------------------------------------- /materials/Cubemap/src/Cubemap.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_texcoord0 2 | 3 | #include 4 | 5 | SAMPLERCUBE_AUTOREG(s_MatTexture); 6 | 7 | void main() { 8 | vec3 dir = normalize(v_texcoord0); 9 | dir.x = dir.x * -1.0; 10 | gl_FragColor = textureCube(s_MatTexture, dir); 11 | } 12 | -------------------------------------------------------------------------------- /materials/Cubemap/src/Cubemap.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec3 a_position : POSITION; 2 | 3 | vec3 v_texcoord0 : TEXCOORD0; 4 | -------------------------------------------------------------------------------- /materials/Cubemap/src/Cubemap.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position 2 | $output v_texcoord0 3 | 4 | #include 5 | #include 6 | 7 | uniform mat4 CubemapRotation; 8 | 9 | void main() { 10 | vec3 worldPosition = mul(u_model[0], vec4(a_position, 1.0)).xyz; 11 | gl_Position = jitterVertexPosition(worldPosition); 12 | v_texcoord0 = mul(CubemapRotation, vec4(worldPosition, 0.0)).xyz; 13 | } 14 | -------------------------------------------------------------------------------- /materials/CubemapHDRi/src/CubemapHDRi.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_texcoord0 2 | 3 | #include 4 | 5 | SAMPLERCUBE_AUTOREG(s_MatTexture); 6 | 7 | vec3 color_degamma(vec3 clr) { 8 | float e = 2.2; 9 | return pow(max(clr, vec3(0.0, 0.0, 0.0)), vec3(e, e, e)); 10 | } 11 | 12 | vec4 color_degamma(vec4 clr) { 13 | return vec4(color_degamma(clr.rgb), clr.a); 14 | } 15 | 16 | void main() { 17 | vec3 dir = normalize(v_texcoord0); 18 | dir.x = dir.x * -1.0; 19 | 20 | vec4 sampledColor = textureCube(s_MatTexture, dir); 21 | gl_FragColor = color_degamma(sampledColor); 22 | } 23 | -------------------------------------------------------------------------------- /materials/CubemapHDRi/src/CubemapHDRi.varying.def.sc: -------------------------------------------------------------------------------- 1 | #include "../../Cubemap/src/Cubemap.varying.def.sc" 2 | -------------------------------------------------------------------------------- /materials/CubemapHDRi/src/CubemapHDRi.vertex.sc: -------------------------------------------------------------------------------- 1 | #include "../../Cubemap/src/Cubemap.vertex.sc" 2 | -------------------------------------------------------------------------------- /materials/DeferredShading/src/DeferredShading.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_projPosition, v_texcoord0 2 | 3 | #include 4 | #include 5 | 6 | #if !defined(FALLBACK) && (BGFX_SHADER_LANGUAGE_GLSL >= 310 || BGFX_SHADER_LANGUAGE_HLSL >= 500 || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_SPIRV || BGFX_SHADER_LANGUAGE_METAL) 7 | 8 | #include "DoDeferredShading.sc" 9 | 10 | SAMPLER2D_AUTOREG(s_ColorMetalness); 11 | SAMPLER2D_AUTOREG(s_SceneDepth); 12 | SAMPLER2D_AUTOREG(s_Normal); 13 | SAMPLER2D_AUTOREG(s_EmissiveAmbientLinearRoughness); 14 | 15 | PBRFragmentInfo getPBRFragmentInfo(vec3 projPosition, vec2 texcoord0) { 16 | vec2 uv = texcoord0; 17 | float z = texture2D(s_SceneDepth, uv).r; 18 | #if BGFX_SHADER_LANGUAGE_GLSL 19 | z = z * 2.0 - 1.0; 20 | #endif 21 | vec4 viewPosition = projToView(vec4(projPosition.xy, z, 1.0), u_invProj); 22 | vec4 worldPosition = mul(u_invView, vec4(viewPosition.xyz, 1.0)); 23 | vec2 n = texture2D(s_Normal, uv).xy; 24 | vec3 worldNorm = normalize(octToNdirSnorm(n.xy)); 25 | vec3 viewNorm = normalize(mul(u_view, vec4(worldNorm, 0.0)).xyz); 26 | vec4 cm = texture2D(s_ColorMetalness, uv); 27 | vec4 ear = texture2D(s_EmissiveAmbientLinearRoughness, uv); 28 | float blockAmbientContribution = ear.g; 29 | float skyAmbientContribution = ear.b; 30 | float roughness = ear.a; 31 | 32 | PBRFragmentInfo result; 33 | result.lightClusterUV = uv; 34 | result.worldPosition = worldPosition.xyz; 35 | result.viewPosition = viewPosition.xyz; 36 | result.ndcPosition = vec3(projPosition.xy, z); 37 | result.worldNormal = worldNorm; 38 | result.viewNormal = viewNorm; 39 | result.albedo = color_degamma(cm.rgb); 40 | result.metalness = cm.a; 41 | result.roughness = roughness; 42 | result.emissive = ear.r; 43 | result.blockAmbientContribution = blockAmbientContribution; 44 | result.skyAmbientContribution = skyAmbientContribution; 45 | return result; 46 | } 47 | 48 | vec4 DeferredLighting(vec3 projPosition, vec2 texcoord0) { 49 | PBRFragmentInfo fragmentInfo = getPBRFragmentInfo(projPosition, texcoord0); 50 | return evaluateFragmentColor(fragmentInfo); 51 | } 52 | 53 | #endif 54 | 55 | void main() { 56 | #if !defined(FALLBACK) && (BGFX_SHADER_LANGUAGE_GLSL >= 310 || BGFX_SHADER_LANGUAGE_HLSL >= 500 || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_SPIRV || BGFX_SHADER_LANGUAGE_METAL) 57 | gl_FragColor = DeferredLighting(v_projPosition, v_texcoord0); 58 | #else 59 | gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); 60 | #endif 61 | } -------------------------------------------------------------------------------- /materials/DeferredShading/src/DeferredShading.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec3 a_position : POSITION; 2 | vec2 a_texcoord0 : TEXCOORD0; 3 | 4 | vec3 v_projPosition : COLOR1; 5 | vec2 v_texcoord0 : TEXCOORD0; 6 | -------------------------------------------------------------------------------- /materials/DeferredShading/src/DeferredShading.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_texcoord0 2 | $output v_projPosition, v_texcoord0 3 | 4 | #include 5 | 6 | void main() { 7 | vec4 position = vec4(a_position, 1.0); 8 | position.xy = position.xy * 2.0 - 1.0; 9 | 10 | vec3 projPosition = a_position.xyz; 11 | projPosition.xy = projPosition.xy * 2.0 - 1.0; 12 | 13 | vec2 texcoord0 = a_texcoord0; 14 | 15 | v_projPosition = projPosition; 16 | v_texcoord0 = texcoord0; 17 | gl_Position = position; 18 | } 19 | -------------------------------------------------------------------------------- /materials/Flipbook/src/Flipbook.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_texcoord0, v_texcoord1 2 | 3 | #include 4 | 5 | uniform vec4 VBlendControl; 6 | 7 | SAMPLER2D_AUTOREG(s_BlitTexture); 8 | 9 | void main() { 10 | vec4 colorFrom = texture2D(s_BlitTexture, v_texcoord0); 11 | vec4 colorTo = texture2D(s_BlitTexture, v_texcoord1); 12 | vec4 color = colorFrom; 13 | if (colorFrom.a < 0.01) { 14 | color = colorTo; 15 | } 16 | else if (colorTo.a >= 0.01) { 17 | color = mix(colorFrom, colorTo, VBlendControl.z); 18 | } 19 | gl_FragColor = color; 20 | } -------------------------------------------------------------------------------- /materials/Flipbook/src/Flipbook.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec3 a_position : POSITION; 2 | vec2 a_texcoord0 : TEXCOORD0; 3 | 4 | vec2 v_texcoord0 : TEXCOORD0; 5 | vec2 v_texcoord1 : TEXCOORD1; 6 | -------------------------------------------------------------------------------- /materials/Flipbook/src/Flipbook.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_texcoord0 2 | $output v_texcoord0, v_texcoord1 3 | 4 | #include 5 | 6 | uniform vec4 VBlendControl; 7 | 8 | void main() { 9 | v_texcoord0 = a_texcoord0; 10 | v_texcoord0.y += VBlendControl.x; 11 | v_texcoord1 = a_texcoord0; 12 | v_texcoord1.y += VBlendControl.y; 13 | gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0)); 14 | } -------------------------------------------------------------------------------- /materials/ItemInHandColor/src/ItemInHandColor.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_texcoord0, v_color0, v_light, v_fog 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | uniform vec4 ChangeColor; 8 | uniform vec4 OverlayColor; 9 | uniform vec4 ColorBased; 10 | uniform vec4 MultiplicativeTintColor; 11 | 12 | void main() { 13 | #if DEPTH_ONLY 14 | gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); 15 | return; 16 | #else 17 | vec4 albedo; 18 | albedo.rgb = mix(vec3(1.0, 1.0, 1.0), v_color0.rgb, ColorBased.x); 19 | albedo.a = 1.0; 20 | 21 | #if MULTI_COLOR_TINT 22 | albedo = applyMultiColorChange(albedo, ChangeColor.rgb, MultiplicativeTintColor.rgb); 23 | #else 24 | albedo = applyColorChange(albedo, ChangeColor, v_color0.a); 25 | #endif 26 | 27 | albedo = applyOverlayColor(albedo, OverlayColor); 28 | albedo = applyLighting(albedo, v_light); 29 | 30 | #if ALPHA_TEST 31 | if (albedo.a < 0.5) { 32 | discard; 33 | } 34 | #endif 35 | 36 | albedo.rgb = applyFog(albedo.rgb, v_fog.rgb, v_fog.a); 37 | gl_FragColor = albedo; 38 | #endif // DEPTH_ONLY 39 | } 40 | -------------------------------------------------------------------------------- /materials/ItemInHandColor/src/ItemInHandColor.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec3 a_position : POSITION; 2 | vec4 a_normal : NORMAL; 3 | vec4 a_color0 : COLOR0; 4 | vec2 a_texcoord0 : TEXCOORD0; 5 | 6 | vec4 i_data0 : TEXCOORD7; 7 | vec4 i_data1 : TEXCOORD6; 8 | vec4 i_data2 : TEXCOORD5; 9 | 10 | vec2 v_texcoord0 : TEXCOORD0; 11 | vec4 v_color0 : COLOR0; 12 | vec4 v_light : COLOR1; 13 | vec4 v_fog : COLOR2; 14 | -------------------------------------------------------------------------------- /materials/ItemInHandColor/src/ItemInHandColor.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_normal, a_texcoord0, a_color0 2 | #if INSTANCING 3 | $input i_data0, i_data1, i_data2 4 | #endif 5 | 6 | $output v_texcoord0, v_color0, v_light, v_fog 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | uniform vec4 FogControl; 14 | uniform vec4 OverlayColor; 15 | uniform vec4 FogColor; 16 | uniform vec4 TileLightColor; 17 | 18 | void main() { 19 | mat4 World = u_model[0]; 20 | 21 | float lightIntensity = calculateLightIntensity(World, vec4(a_normal.xyz, 0.0), TileLightColor); 22 | lightIntensity += OverlayColor.a * 0.35; 23 | vec4 light = vec4(lightIntensity * TileLightColor.rgb, 1.0); 24 | 25 | vec3 worldPosition; 26 | #if INSTANCING 27 | mat4 model = mtxFromCols(i_data0, i_data1, i_data2, vec4(0.0, 0.0, 0.0, 1.0)); 28 | worldPosition = instMul(model, vec4(a_position, 1.0)).xyz; 29 | #else 30 | worldPosition = mul(World, vec4(a_position, 1.0)).xyz; 31 | #endif 32 | 33 | vec4 position = jitterVertexPosition(worldPosition); 34 | float cameraDepth = position.z; 35 | float fogIntensity = calculateFogIntensity(cameraDepth, FogControl.z, FogControl.x, FogControl.y); 36 | vec4 fog = vec4(FogColor.rgb, fogIntensity); 37 | 38 | #if DEPTH_ONLY 39 | v_color0 = vec4(0.0, 0.0, 0.0, 0.0); 40 | #else 41 | v_color0 = a_color0; 42 | #endif 43 | 44 | v_light = light; 45 | v_fog = fog; 46 | v_texcoord0 = a_texcoord0; 47 | gl_Position = position; 48 | } 49 | -------------------------------------------------------------------------------- /materials/ItemInHandColorGlint/src/ItemInHandColorGlint.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_texcoord0, v_color0, v_light, v_fog, v_glintUV 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | uniform vec4 ChangeColor; 9 | uniform vec4 OverlayColor; 10 | uniform vec4 ColorBased; 11 | uniform vec4 MultiplicativeTintColor; 12 | uniform vec4 TileLightColor; 13 | uniform vec4 GlintColor; 14 | 15 | SAMPLER2D_AUTOREG(s_GlintTexture); 16 | 17 | void main() { 18 | #if DEPTH_ONLY 19 | gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); 20 | return; 21 | #else 22 | vec4 albedo; 23 | albedo.rgb = mix(vec3(1.0, 1.0, 1.0), v_color0.rgb, ColorBased.x); 24 | albedo.a = 1.0; 25 | 26 | #if MULTI_COLOR_TINT 27 | albedo = applyMultiColorChange(albedo, ChangeColor.rgb, MultiplicativeTintColor.rgb); 28 | #else 29 | albedo = applyColorChange(albedo, ChangeColor, v_color0.a); 30 | #endif 31 | 32 | albedo = applyOverlayColor(albedo, OverlayColor); 33 | albedo = applyLighting(albedo, v_light); 34 | albedo = applyGlint(albedo, v_glintUV, s_GlintTexture, GlintColor, TileLightColor); 35 | 36 | #if ALPHA_TEST 37 | if (albedo.a < 0.5) { 38 | discard; 39 | } 40 | #endif 41 | 42 | albedo.rgb = applyFog(albedo.rgb, v_fog.rgb, v_fog.a); 43 | gl_FragColor = albedo; 44 | #endif // DEPTH_ONLY 45 | } 46 | -------------------------------------------------------------------------------- /materials/ItemInHandColorGlint/src/ItemInHandColorGlint.varying.def.sc: -------------------------------------------------------------------------------- 1 | #include "../../ItemInHandColor/src/ItemInHandColor.varying.def.sc" 2 | 3 | vec4 v_glintUV : TEXCOORD2; 4 | -------------------------------------------------------------------------------- /materials/ItemInHandColorGlint/src/ItemInHandColorGlint.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_normal, a_texcoord0, a_color0 2 | #if INSTANCING 3 | $input i_data0, i_data1, i_data2 4 | #endif 5 | 6 | $output v_texcoord0, v_color0, v_light, v_fog, v_glintUV 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | uniform vec4 FogControl; 15 | uniform vec4 OverlayColor; 16 | uniform vec4 FogColor; 17 | uniform vec4 TileLightColor; 18 | uniform vec4 UVScale; 19 | uniform vec4 UVAnimation; 20 | 21 | void main() { 22 | mat4 World = u_model[0]; 23 | 24 | vec2 layer1UV = calculateLayerUV(a_texcoord0, UVAnimation.x, UVAnimation.z, UVScale.xy); 25 | vec2 layer2UV = calculateLayerUV(a_texcoord0, UVAnimation.y, UVAnimation.w, UVScale.xy); 26 | vec4 glintUV = vec4(layer1UV, layer2UV); 27 | 28 | float lightIntensity = calculateLightIntensity(World, vec4(a_normal.xyz, 0.0), TileLightColor); 29 | lightIntensity += OverlayColor.a * 0.35; 30 | vec4 light = vec4(lightIntensity * TileLightColor.rgb, 1.0); 31 | 32 | vec3 worldPosition; 33 | #if INSTANCING 34 | mat4 model = mtxFromCols(i_data0, i_data1, i_data2, vec4(0.0, 0.0, 0.0, 1.0)); 35 | worldPosition = instMul(model, vec4(a_position, 1.0)).xyz; 36 | #else 37 | worldPosition = mul(World, vec4(a_position, 1.0)).xyz; 38 | #endif 39 | 40 | vec4 position = jitterVertexPosition(worldPosition); 41 | float cameraDepth = position.z; 42 | float fogIntensity = calculateFogIntensity(cameraDepth, FogControl.z, FogControl.x, FogControl.y); 43 | vec4 fog = vec4(FogColor.rgb, fogIntensity); 44 | 45 | #if DEPTH_ONLY 46 | v_color0 = vec4(0.0, 0.0, 0.0, 0.0); 47 | #else 48 | v_color0 = a_color0; 49 | #endif 50 | 51 | v_glintUV = glintUV; 52 | v_light = light; 53 | v_fog = fog; 54 | v_texcoord0 = a_texcoord0; 55 | gl_Position = position; 56 | } 57 | -------------------------------------------------------------------------------- /materials/ItemInHandTextured/src/ItemInHandTextured.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_texcoord0, v_color0, v_light, v_fog 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | uniform vec4 ChangeColor; 9 | uniform vec4 OverlayColor; 10 | uniform vec4 ColorBased; 11 | uniform vec4 MultiplicativeTintColor; 12 | uniform vec4 MatColor; 13 | 14 | SAMPLER2D_AUTOREG(s_MatTexture); 15 | 16 | void main() { 17 | #if DEPTH_ONLY 18 | gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); 19 | return; 20 | #else 21 | vec4 albedo = getActorAlbedoNoColorChange(v_texcoord0, s_MatTexture, MatColor); 22 | albedo.rgb *= mix(vec3(1.0, 1.0, 1.0), v_color0.rgb, ColorBased.x); 23 | 24 | #if MULTI_COLOR_TINT 25 | albedo = applyMultiColorChange(albedo, ChangeColor.rgb, MultiplicativeTintColor.rgb); 26 | #else 27 | albedo = applyColorChange(albedo, ChangeColor, v_color0.a); 28 | #endif 29 | 30 | albedo = applyOverlayColor(albedo, OverlayColor); 31 | albedo = applyLighting(albedo, v_light); 32 | 33 | #if ALPHA_TEST 34 | if (albedo.a < 0.5) { 35 | discard; 36 | } 37 | #endif 38 | 39 | albedo.rgb = applyFog(albedo.rgb, v_fog.rgb, v_fog.a); 40 | gl_FragColor = albedo; 41 | #endif // DEPTH_ONLY 42 | } 43 | -------------------------------------------------------------------------------- /materials/ItemInHandTextured/src/ItemInHandTextured.varying.def.sc: -------------------------------------------------------------------------------- 1 | #include "../../ItemInHandColor/src/ItemInHandColor.varying.def.sc" 2 | -------------------------------------------------------------------------------- /materials/ItemInHandTextured/src/ItemInHandTextured.vertex.sc: -------------------------------------------------------------------------------- 1 | #include "../../ItemInHandColor/src/ItemInHandColor.vertex.sc" 2 | -------------------------------------------------------------------------------- /materials/LegacyCubemap/src/LegacyCubemap.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_texcoord0 2 | 3 | #include 4 | 5 | SAMPLER2D_AUTOREG(s_MatTexture); 6 | 7 | void main() { 8 | gl_FragColor = texture2D(s_MatTexture, v_texcoord0); 9 | } 10 | -------------------------------------------------------------------------------- /materials/LegacyCubemap/src/LegacyCubemap.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec3 a_position : POSITION; 2 | vec2 a_texcoord0 : TEXCOORD0; 3 | 4 | vec2 v_texcoord0 : TEXCOORD0; 5 | -------------------------------------------------------------------------------- /materials/LegacyCubemap/src/LegacyCubemap.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_texcoord0 2 | $output v_texcoord0 3 | 4 | #include 5 | 6 | uniform mat4 CubemapRotation; 7 | 8 | void main() { 9 | v_texcoord0 = a_texcoord0; 10 | gl_Position = mul(u_modelViewProj, mul(CubemapRotation, vec4(a_position, 1.0))); 11 | } 12 | -------------------------------------------------------------------------------- /materials/LightClustering/src/ClusterLights.sc: -------------------------------------------------------------------------------- 1 | float getViewSpaceCoordByRatio(float clusterIndex, float clusterSize, float screenDim, float farPlaneDim, float planeRatio) { 2 | return ((clusterIndex * clusterSize - screenDim / 2.0f) / screenDim) * (farPlaneDim * planeRatio); 3 | } 4 | 5 | float getClusterDepthByIndex(float index, float maxSlices, vec2 clusterNearFar) { 6 | float zNear = clusterNearFar.x; 7 | float zFar = clusterNearFar.y; 8 | if (index == 0.0f) { 9 | return zNear; 10 | } 11 | if (index == 1.0f) { 12 | return 1.0f; 13 | } 14 | float nearFarLog = log2(zFar / 1.5f); 15 | float logDepth = nearFarLog * (index - 2.0f) / (maxSlices - 2.0f); 16 | return pow(2.0f, logDepth); 17 | } 18 | 19 | void ClusterLights(uvec3 GlobalInvocationID) { 20 | float x = float(GlobalInvocationID.x); 21 | float y = float(GlobalInvocationID.y); 22 | float z = float(GlobalInvocationID.z); 23 | int lightCount = int(ClusterDimensions.w); 24 | int maxLights = int(LightsPerCluster.x); 25 | if (x >= ClusterDimensions.x || y >= ClusterDimensions.y || z >= ClusterDimensions.z) { 26 | return; 27 | } 28 | highp int idx = int(x + y * ClusterDimensions.x + z * ClusterDimensions.x * ClusterDimensions.y); 29 | highp int rangeStart = int(idx * maxLights); 30 | highp int rangeEnd = int(rangeStart + maxLights); 31 | float viewDepth = getClusterDepthByIndex(z + 0.5f, ClusterDimensions.z, ClusterNearFarWidthHeight.xy); 32 | float planeRatio = viewDepth / CameraFarPlane.z; 33 | float centerX = getViewSpaceCoordByRatio(x + 0.5f, ClusterSize.x, ClusterNearFarWidthHeight.z, CameraFarPlane.x, planeRatio); 34 | float centerY = getViewSpaceCoordByRatio(y + 0.5f, ClusterSize.y, ClusterNearFarWidthHeight.w, CameraFarPlane.y, planeRatio); 35 | vec3 center = vec3(centerX, centerY, -viewDepth); 36 | LightContribution distanceToCenter[64]; 37 | int countResult = 0; 38 | for (int l = 0; l < lightCount; l++) { 39 | LightExtends bound = s_Extends[l]; 40 | if (z < bound.min.z || z > bound.max.z) 41 | continue; 42 | if (y < bound.min.y || y > bound.max.y) 43 | continue; 44 | if (x < bound.min.x || x > bound.max.x) 45 | continue; 46 | float curDistance = length(bound.pos.xyz - center); 47 | if (countResult >= maxLights && curDistance >= distanceToCenter[countResult - 1].contribution) { 48 | continue; 49 | } 50 | int low = 0; 51 | int high = countResult; 52 | while (low < high) { 53 | int mid = (low + high) / 2; 54 | if (distanceToCenter[mid].contribution < curDistance) { 55 | low = mid + 1; 56 | } else { 57 | high = mid; 58 | } 59 | } 60 | if (countResult < maxLights) { 61 | for (int j = countResult - 1; j >= low; j--) { 62 | distanceToCenter[j + 1] = distanceToCenter[j]; 63 | } 64 | distanceToCenter[low].contribution = curDistance; 65 | distanceToCenter[low].indexInLookUp = countResult; 66 | s_LightLookupArray[idx * maxLights + countResult].lookup = float(bound.index); 67 | countResult++; 68 | } else { 69 | int insertPos = distanceToCenter[maxLights - 1].indexInLookUp; 70 | for (int j = maxLights - 2; j >= low; j--) { 71 | distanceToCenter[j + 1] = distanceToCenter[j]; 72 | } 73 | distanceToCenter[low].contribution = curDistance; 74 | distanceToCenter[low].indexInLookUp = insertPos; 75 | s_LightLookupArray[idx * maxLights + insertPos].lookup = float(bound.index); 76 | } 77 | } 78 | if (countResult < maxLights) { 79 | s_LightLookupArray[rangeStart + countResult].lookup = -1.0; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /materials/LightClustering/src/ClusterLightsManhattan.sc: -------------------------------------------------------------------------------- 1 | float clusterIndexToScreenCoord(float index, float clusterSize, float dimension) { 2 | return (min(index * clusterSize, dimension) - dimension / 2.0f) / dimension; 3 | } 4 | 5 | float getClusterDepthByIndex(float index, float maxSlices, vec2 clusterNearFar) { 6 | float zNear = clusterNearFar.x; 7 | float zFar = clusterNearFar.y; 8 | if (index == 0.0f) { 9 | return zNear; 10 | } 11 | if (index == 1.0f) { 12 | return 1.0f; 13 | } 14 | float nearFarLog = log2(zFar / 1.5f); 15 | float logDepth = nearFarLog * (index - 2.0f) / (maxSlices - 2.0f); 16 | return pow(2.0f, logDepth); 17 | } 18 | 19 | void ClusterLights(uvec3 GlobalInvocationID) { 20 | float x = float(GlobalInvocationID.x); 21 | float y = float(GlobalInvocationID.y); 22 | float z = float(GlobalInvocationID.z); 23 | int lightCount = int(ClusterDimensions.w); 24 | int maxLights = int(LightsPerCluster.x); 25 | if (x >= ClusterDimensions.x || y >= ClusterDimensions.y || z >= ClusterDimensions.z) { 26 | return; 27 | } 28 | highp int idx = int(x + y * ClusterDimensions.x + z * ClusterDimensions.x * ClusterDimensions.y); 29 | highp int rangeStart = int(idx * maxLights); 30 | highp int rangeEnd = int(rangeStart + maxLights); 31 | LightContribution contribution[64]; 32 | vec2 corners[7]; 33 | corners[0] = vec2(1.0f, 0.0f); 34 | corners[1] = vec2(0.0f, 1.0f); 35 | corners[2] = vec2(1.0f, 1.0f); 36 | corners[3] = vec2(1.0f, 1.0f); 37 | corners[4] = vec2(0.0f, 1.0f); 38 | corners[5] = vec2(1.0f, 0.0f); 39 | corners[6] = vec2(0.0f, 0.0f); 40 | int closestCornerToCameraIndex = 3; 41 | float left = x; 42 | float right = x + 1.0f; 43 | float bottom = y; 44 | float top = y + 1.0f; 45 | if (right < ClusterDimensions.x / 2.0f && top < ClusterDimensions.y / 2.0f) { 46 | closestCornerToCameraIndex = 2; 47 | } else if (left > ClusterDimensions.x / 2.0f && top < ClusterDimensions.y / 2.0f) { 48 | closestCornerToCameraIndex = 1; 49 | } else if (right < ClusterDimensions.x / 2.0f && bottom > ClusterDimensions.y / 2.0f) { 50 | closestCornerToCameraIndex = 0; 51 | } else if (left > ClusterDimensions.x / 2.0f && bottom > ClusterDimensions.y / 2.0f) { 52 | closestCornerToCameraIndex = -1; 53 | } 54 | int oppositeCornerIndex = 4 + closestCornerToCameraIndex; 55 | int countResult = 0; 56 | for (int l = 0; l < lightCount; l++) { 57 | LightExtends bound = s_Extends[l]; 58 | if (z < bound.min.z || z > bound.max.z) 59 | continue; 60 | if (y < bound.min.y || y > bound.max.y) 61 | continue; 62 | if (x < bound.min.x || x > bound.max.x) 63 | continue; 64 | vec3 closestCornerToCameraView = vec3(0.0f, 0.0f, 0.0f); 65 | vec3 oppositeCornerView = vec3(0.0f, 0.0f, 0.0f); 66 | vec3 lightWorldGrid = floor(bound.pos.xyz - WorldOrigin.xyz); 67 | float cornerViewZ = getClusterDepthByIndex(z, ClusterDimensions.z, ClusterNearFarWidthHeight.xy); 68 | float cornerScreenX = clusterIndexToScreenCoord(x, ClusterSize.x, ClusterNearFarWidthHeight.z); 69 | float cornerScreenY = clusterIndexToScreenCoord(y, ClusterSize.y, ClusterNearFarWidthHeight.w); 70 | float cornerViewX = cornerViewZ * cornerScreenX / ProjMat[0][0]; 71 | float cornerViewY = cornerViewZ * cornerScreenY / ProjMat[1][1]; 72 | vec3 clusterWorld = mul(InvViewMat, vec4(cornerViewX, cornerViewY, -cornerViewZ, 1.0f)).xyz; 73 | vec3 clusterWorldGrid = floor(clusterWorld - WorldOrigin.xyz); 74 | vec3 clusterWorldGridMin = clusterWorldGrid; 75 | vec3 clusterWorldGridMax = clusterWorldGrid; 76 | if (closestCornerToCameraIndex == -1) { 77 | closestCornerToCameraView = vec3(cornerViewX, cornerViewY, -cornerViewZ); 78 | } 79 | if (closestCornerToCameraIndex == 3) { 80 | cornerScreenX = clusterIndexToScreenCoord(x + 0.5f, ClusterSize.x, ClusterNearFarWidthHeight.z); 81 | cornerScreenY = clusterIndexToScreenCoord(y + 0.5f, ClusterSize.y, ClusterNearFarWidthHeight.w); 82 | cornerViewX = cornerViewZ * cornerScreenX / ProjMat[0][0]; 83 | cornerViewY = cornerViewZ * cornerScreenY / ProjMat[1][1]; 84 | closestCornerToCameraView = vec3(cornerViewX, cornerViewY, -cornerViewZ); 85 | } 86 | for (int cf = 0; cf < 3; cf++) { 87 | cornerScreenX = clusterIndexToScreenCoord(x + corners[cf].x, ClusterSize.x, ClusterNearFarWidthHeight.z); 88 | cornerScreenY = clusterIndexToScreenCoord(y + corners[cf].y, ClusterSize.y, ClusterNearFarWidthHeight.w); 89 | cornerViewX = cornerViewZ * cornerScreenX / ProjMat[0][0]; 90 | cornerViewY = cornerViewZ * cornerScreenY / ProjMat[1][1]; 91 | clusterWorld = mul(InvViewMat, vec4(cornerViewX, cornerViewY, -cornerViewZ, 1.0f)).xyz; 92 | clusterWorldGrid = floor(clusterWorld - WorldOrigin.xyz); 93 | clusterWorldGridMin = min(clusterWorldGrid, clusterWorldGridMin); 94 | clusterWorldGridMax = max(clusterWorldGrid, clusterWorldGridMax); 95 | if (closestCornerToCameraIndex == cf) { 96 | closestCornerToCameraView = vec3(cornerViewX, cornerViewY, -cornerViewZ); 97 | } 98 | } 99 | cornerViewZ = getClusterDepthByIndex(z + 1.0f, ClusterDimensions.z, ClusterNearFarWidthHeight.xy); 100 | for (int cb = 3; cb < 7; cb++) { 101 | cornerScreenX = clusterIndexToScreenCoord(x + corners[cb].x, ClusterSize.x, ClusterNearFarWidthHeight.z); 102 | cornerScreenY = clusterIndexToScreenCoord(y + corners[cb].y, ClusterSize.y, ClusterNearFarWidthHeight.w); 103 | cornerViewX = cornerViewZ * cornerScreenX / ProjMat[0][0]; 104 | cornerViewY = cornerViewZ * cornerScreenY / ProjMat[1][1]; 105 | clusterWorld = mul(InvViewMat, vec4(cornerViewX, cornerViewY, -cornerViewZ, 1.0f)).xyz; 106 | clusterWorldGrid = floor(clusterWorld - WorldOrigin.xyz); 107 | clusterWorldGridMin = min(clusterWorldGrid, clusterWorldGridMin); 108 | clusterWorldGridMax = max(clusterWorldGrid, clusterWorldGridMax); 109 | if (oppositeCornerIndex == cb) { 110 | oppositeCornerView = vec3(cornerViewX, cornerViewY, -cornerViewZ); 111 | } 112 | } 113 | if (oppositeCornerIndex == 7) { 114 | cornerScreenX = clusterIndexToScreenCoord(x + 0.5f, ClusterSize.x, ClusterNearFarWidthHeight.z); 115 | cornerScreenY = clusterIndexToScreenCoord(y + 0.5f, ClusterSize.y, ClusterNearFarWidthHeight.w); 116 | cornerViewX = cornerViewZ * cornerScreenX / ProjMat[0][0]; 117 | cornerViewY = cornerViewZ * cornerScreenY / ProjMat[1][1]; 118 | oppositeCornerView = vec3(cornerViewX, cornerViewY, -cornerViewZ); 119 | } 120 | cornerViewZ = getClusterDepthByIndex(z + 0.5f, ClusterDimensions.z, ClusterNearFarWidthHeight.xy); 121 | cornerScreenX = clusterIndexToScreenCoord(x + 0.5f, ClusterSize.x, ClusterNearFarWidthHeight.z); 122 | cornerScreenY = clusterIndexToScreenCoord(y + 0.5f, ClusterSize.y, ClusterNearFarWidthHeight.w); 123 | cornerViewX = cornerViewZ * cornerScreenX / ProjMat[0][0]; 124 | cornerViewY = cornerViewZ * cornerScreenY / ProjMat[1][1]; 125 | vec3 centerView = vec3(cornerViewX, cornerViewY, -cornerViewZ); 126 | vec3 lightView = mul(ViewMat, vec4(bound.pos.xyz, 1.0f)).xyz; 127 | float lightDistance = length(lightView); 128 | float cameraDistanceContribution = max(1.0f - lightDistance / length(oppositeCornerView), 0.0f); 129 | float clusterContribution = max(1.0f - length(lightView - centerView) / (length(oppositeCornerView - closestCornerToCameraView) / 2.0f), 0.0f); 130 | float curContribution = CameraClusterWeight.x * cameraDistanceContribution + CameraClusterWeight.y * clusterContribution; 131 | if (countResult >= maxLights && curContribution <= contribution[countResult - 1].contribution) { 132 | continue; 133 | } 134 | bool lightInCluster = true; 135 | for (int gridX = int(clusterWorldGridMin.x); gridX <= int(clusterWorldGridMax.x); gridX++) { 136 | for (int gridY = int(clusterWorldGridMin.y); gridY <= int(clusterWorldGridMax.y); gridY++) { 137 | for (int gridZ = int(clusterWorldGridMin.z); gridZ <= int(clusterWorldGridMax.z); gridZ++) { 138 | vec3 curWorldGrid = vec3(float(gridX), float(gridY), float(gridZ)); 139 | vec3 dir = curWorldGrid - lightWorldGrid; 140 | float manhattan = abs(dir.x) + abs(dir.y) + abs(dir.z); 141 | if (manhattan <= bound.radius) { 142 | lightInCluster = true; 143 | break; 144 | } 145 | } 146 | } 147 | } 148 | if (lightInCluster) { 149 | int low = 0; 150 | int high = countResult; 151 | while (low < high) { 152 | int mid = (low + high) / 2; 153 | if (contribution[mid].contribution >= curContribution) { 154 | low = mid + 1; 155 | } else { 156 | high = mid; 157 | } 158 | } 159 | if (countResult < maxLights) { 160 | for (int j = countResult - 1; j >= low; j--) { 161 | contribution[j + 1] = contribution[j]; 162 | } 163 | contribution[low].contribution = curContribution; 164 | contribution[low].indexInLookUp = countResult; 165 | s_LightLookupArray[idx * maxLights + countResult].lookup = float(bound.index); 166 | countResult++; 167 | } else { 168 | int insertPos = contribution[maxLights - 1].indexInLookUp; 169 | for (int j = maxLights - 2; j >= low; j--) { 170 | contribution[j + 1] = contribution[j]; 171 | } 172 | contribution[low].contribution = curContribution; 173 | contribution[low].indexInLookUp = insertPos; 174 | s_LightLookupArray[idx * maxLights + insertPos].lookup = float(bound.index); 175 | } 176 | } 177 | } 178 | if (countResult < maxLights) { 179 | s_LightLookupArray[rangeStart + countResult].lookup = -1.0; 180 | } 181 | } 182 | -------------------------------------------------------------------------------- /materials/LightClustering/src/LightClustering.compute.sc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct LightCluster { 4 | int count; 5 | }; 6 | 7 | struct LightData { 8 | float lookup; 9 | }; 10 | 11 | struct LightExtends { 12 | vec4 min; 13 | vec4 max; 14 | vec4 pos; 15 | int index; 16 | float radius; 17 | int pad1; 18 | int pad2; 19 | }; 20 | 21 | struct LightContribution { 22 | float contribution; 23 | int indexInLookUp; 24 | }; 25 | 26 | uniform vec4 LightsPerCluster; 27 | uniform vec4 ClusterNearFarWidthHeight; 28 | uniform vec4 CameraFarPlane; 29 | uniform vec4 ClusterDimensions; 30 | uniform vec4 ClusterSize; 31 | uniform vec4 CameraClusterWeight; 32 | uniform mat4 InvViewMat; 33 | uniform mat4 ProjMat; 34 | uniform mat4 ViewMat; 35 | uniform vec4 WorldOrigin; 36 | 37 | BUFFER_WR_AUTOREG(s_LightLookupArray, LightData); 38 | BUFFER_RO_AUTOREG(s_Extends, LightExtends); 39 | 40 | #if defined(CLUSTER_LIGHTS) 41 | #include "ClusterLights.sc" 42 | #elif defined(CLUSTER_LIGHTS_MANHATTAN) 43 | #include "ClusterLightsManhattan.sc" 44 | #endif 45 | 46 | NUM_THREADS(4, 4, 4) 47 | void main() { 48 | ClusterLights(gl_GlobalInvocationID); 49 | } 50 | -------------------------------------------------------------------------------- /materials/LightClustering/src/LightClustering.fragment.sc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() { 4 | gl_FragColor = vec4(0.0f, 0.0f, 0.0f, 0.0f); 5 | } 6 | -------------------------------------------------------------------------------- /materials/LightClustering/src/LightClustering.varying.def.sc: -------------------------------------------------------------------------------- 1 | float placeholder : PLACEHOLDER; 2 | -------------------------------------------------------------------------------- /materials/LightClustering/src/LightClustering.vertex.sc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() { 4 | gl_Position = vec4(0.0f, 0.0f, 0.0f, 0.0f); 5 | } 6 | -------------------------------------------------------------------------------- /materials/LuminanceHistogram/src/BuildHistogram.sc: -------------------------------------------------------------------------------- 1 | SHARED uint curFrameLuminanceHistogramShared[256]; 2 | 3 | uint luminanceToHistogramBin(float luminance) { 4 | if (luminance < 0.00095f) { 5 | return 0u; 6 | } 7 | float logLuminance = clamp((log2(luminance) - MinLogLuminance.x) * 1.0f / LogLuminanceRange.x, 0.0f, 1.0f); 8 | return uint(logLuminance * 254.0 + 1.0); 9 | } 10 | 11 | void Build(uint LocalInvocationIndex, uvec3 GlobalInvocationID) { 12 | uint x = GlobalInvocationID.x; 13 | uint y = GlobalInvocationID.y; 14 | curFrameLuminanceHistogramShared[LocalInvocationIndex] = 0u; 15 | barrier(); 16 | if (x < uint(ScreenSize.x) && y < uint(ScreenSize.y)) { 17 | ivec2 pixel = ivec2(x, y); 18 | vec2 uv = vec2(pixel) / ScreenSize.xy; 19 | vec3 color = texture2DLod(s_GameColor, uv, 0.0f).rgb; 20 | float luminance = dot(color.rgb, vec3(0.2126f, 0.7152f, 0.0722f)); 21 | uint index = luminanceToHistogramBin(luminance); 22 | atomicAdd(curFrameLuminanceHistogramShared[index], 1u); 23 | } 24 | barrier(); 25 | atomicAdd(s_CurFrameLuminanceHistogram[LocalInvocationIndex].count, curFrameLuminanceHistogramShared[LocalInvocationIndex]); 26 | } 27 | 28 | NUM_THREADS(16, 16, 1) 29 | void main() { 30 | Build(gl_LocalInvocationIndex, gl_GlobalInvocationID); 31 | } 32 | -------------------------------------------------------------------------------- /materials/LuminanceHistogram/src/CalculateAverage.sc: -------------------------------------------------------------------------------- 1 | SHARED float curFrameLuminanceHistogramShared[256]; 2 | 3 | void Average(uint LocalInvocationIndex) { 4 | uint histogramCount = s_CurFrameLuminanceHistogram[LocalInvocationIndex].count; 5 | if (EnableCustomWeight.x != 0.0) { 6 | vec2 uv = vec2((float(LocalInvocationIndex) + 0.5f) / float(256), 0.5f); 7 | curFrameLuminanceHistogramShared[LocalInvocationIndex] = float(histogramCount) * texture2DLod(s_CustomWeight, uv, 0.0f).r; 8 | } else { 9 | curFrameLuminanceHistogramShared[LocalInvocationIndex] = float(histogramCount) * float(LocalInvocationIndex); 10 | } 11 | barrier(); 12 | for (uint cutoff = uint(256 >> 1); cutoff > 0u;) { 13 | if (uint(LocalInvocationIndex) < cutoff) { 14 | curFrameLuminanceHistogramShared[LocalInvocationIndex] = curFrameLuminanceHistogramShared[LocalInvocationIndex] + curFrameLuminanceHistogramShared[LocalInvocationIndex + cutoff]; 15 | } 16 | barrier(); 17 | cutoff = (cutoff >> 1u); 18 | } 19 | if (LocalInvocationIndex == 0u) { 20 | float weightedLogAverage = (curFrameLuminanceHistogramShared[0] / max(ScreenSize.x * ScreenSize.y - float(histogramCount), 1.0f)) - 1.0f; 21 | if (weightedLogAverage < 1.f) { 22 | weightedLogAverage = 0.0f; 23 | } 24 | float weightedAverageLuminance = exp2(((weightedLogAverage * LogLuminanceRange.x) / 254.0f) + MinLogLuminance.x); 25 | float prevLuminance = imageLoad(s_AdaptedFrameAverageLuminance, ivec2(0, 0)).r; 26 | bool isBrighter = (prevLuminance < weightedAverageLuminance); 27 | float speedParam = isBrighter ? AdaptiveParameters.y : AdaptiveParameters.z; 28 | float adaptedLuminance = prevLuminance + (weightedAverageLuminance - prevLuminance) * (1.0f - exp(-DeltaTime.x * AdaptiveParameters.x * speedParam)); 29 | if (isBrighter) { 30 | adaptedLuminance = adaptedLuminance > weightedAverageLuminance ? weightedAverageLuminance : adaptedLuminance; 31 | } else { 32 | adaptedLuminance = adaptedLuminance > weightedAverageLuminance ? adaptedLuminance : weightedAverageLuminance; 33 | } 34 | imageStore(s_AdaptedFrameAverageLuminance, ivec2(0, 0), vec4(adaptedLuminance, adaptedLuminance, adaptedLuminance, adaptedLuminance)); 35 | int maxLuminanceBin = 0; 36 | for (int i = 256 - 1; i > 0; i--) { 37 | if (float(s_CurFrameLuminanceHistogram[i].count) >= AdaptiveParameters.w) { 38 | maxLuminanceBin = i; 39 | break; 40 | } 41 | } 42 | vec2 uv = vec2((float(maxLuminanceBin) + 0.5f) / float(256), 0.5f); 43 | float maxLuminance = 0.0f; 44 | if (EnableCustomWeight.x != 0.0) { 45 | maxLuminance = exp2(((texture2DLod(s_CustomWeight, uv, 0.0f).r - 1.0f) * LogLuminanceRange.x) / 254.0f + MinLogLuminance.x); 46 | } else { 47 | maxLuminance = exp2(((float(maxLuminanceBin) - 1.0f) * LogLuminanceRange.x) / 254.0f + MinLogLuminance.x); 48 | } 49 | imageStore(s_MaxFrameLuminance, ivec2(0, 0), vec4(maxLuminance, maxLuminance, maxLuminance, maxLuminance)); 50 | } 51 | } 52 | 53 | NUM_THREADS(16, 16, 1) 54 | void main() { 55 | Average(gl_LocalInvocationIndex); 56 | } 57 | -------------------------------------------------------------------------------- /materials/LuminanceHistogram/src/CleanUp.sc: -------------------------------------------------------------------------------- 1 | void Clean(uint LocalInvocationIndex) { 2 | s_CurFrameLuminanceHistogram[LocalInvocationIndex].count = 0u; 3 | } 4 | 5 | NUM_THREADS(16, 16, 1) 6 | void main() { 7 | Clean(gl_LocalInvocationIndex); 8 | } 9 | -------------------------------------------------------------------------------- /materials/LuminanceHistogram/src/LuminanceHistogram.compute.sc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Histogram { 4 | uint count; 5 | }; 6 | 7 | uniform vec4 MinLogLuminance; 8 | uniform vec4 AdaptiveParameters; 9 | uniform vec4 DeltaTime; 10 | uniform vec4 ScreenSize; 11 | uniform vec4 LogLuminanceRange; 12 | uniform vec4 EnableCustomWeight; 13 | 14 | SAMPLER2D_AUTOREG(s_GameColor); 15 | SAMPLER2D_AUTOREG(s_CustomWeight); 16 | BUFFER_RW_AUTOREG(s_CurFrameLuminanceHistogram, Histogram); 17 | IMAGE2D_RW_AUTOREG(s_AdaptedFrameAverageLuminance, r32f); 18 | IMAGE2D_RW_AUTOREG(s_MaxFrameLuminance, r32f); 19 | 20 | 21 | #if BUILD_HISTOGRAM 22 | #include "BuildHistogram.sc" 23 | #elif CALCULATE_AVERAGE 24 | #include "CalculateAverage.sc" 25 | #elif CLEAN_UP 26 | #include "CleanUp.sc" 27 | #endif 28 | -------------------------------------------------------------------------------- /materials/LuminanceHistogram/src/LuminanceHistogram.fragment.sc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() { 4 | gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); 5 | } 6 | -------------------------------------------------------------------------------- /materials/LuminanceHistogram/src/LuminanceHistogram.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec3 a_position : POSITION; 2 | -------------------------------------------------------------------------------- /materials/LuminanceHistogram/src/LuminanceHistogram.vertex.sc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() { 4 | gl_Position = vec4(0.0, 0.0, 0.0, 0.0); 5 | } 6 | -------------------------------------------------------------------------------- /materials/MeshFallbackPosUVNormalColor/src/MeshFallbackPosUVNormalColor.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_color, v_fog, v_light, v_texCoords 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | uniform vec4 DiscardValue; 8 | uniform vec4 HudOpacity; 9 | uniform vec4 OverlayColor; 10 | uniform vec4 ChangeColor; 11 | uniform vec4 CurrentColor; 12 | 13 | SAMPLER2D_AUTOREG(s_MatTexture); 14 | 15 | void main() { 16 | vec4 diffuse; 17 | #ifndef USE_TEXTURES 18 | diffuse = vec4(1.0, 1.0, 1.0, 1.0); 19 | #endif 20 | 21 | #if defined(USE_TEXTURES) && (defined(MULTI_COLOR_TINT) || defined(ALPHA_TEST)) 22 | diffuse = texture2D(s_MatTexture, v_texCoords); 23 | #endif 24 | 25 | #if defined(ALPHA_TEST) && (defined(LIT) || defined(MULTI_COLOR_TINT)) 26 | if (diffuse.a <= 0.0) discard; 27 | #endif 28 | 29 | #ifdef MULTI_COLOR_TINT 30 | vec2 colorMask = diffuse.rg; 31 | diffuse.rgb = colorMask.rrr * v_color.rgb; 32 | diffuse.rgb = mix(diffuse.rgb, colorMask.ggg * ChangeColor.rgb, ceil(colorMask.g)); 33 | #endif 34 | 35 | #if !defined(ALPHA_TEST) && !defined(MULTI_COLOR_TINT) && defined(USE_TEXTURES) 36 | diffuse = texture2D(s_MatTexture, v_texCoords); 37 | #endif 38 | #if defined(ALPHA_TEST) && !defined(LIT) && !defined(MULTI_COLOR_TINT) 39 | if (diffuse.a <= 0.0) discard; 40 | 41 | #endif 42 | diffuse.rgb = mix(diffuse.rgb, OverlayColor.rgb, OverlayColor.a); 43 | #ifdef LIT 44 | diffuse *= v_light; 45 | #endif 46 | #ifndef MULTI_COLOR_TINT 47 | diffuse = CurrentColor * v_color * diffuse; 48 | #endif 49 | 50 | diffuse.rgb = applyFog(diffuse.rgb, v_fog.rgb, v_fog.a); 51 | 52 | gl_FragColor = diffuse; 53 | } 54 | -------------------------------------------------------------------------------- /materials/MeshFallbackPosUVNormalColor/src/MeshFallbackPosUVNormalColor.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec4 a_color0 : COLOR0; 2 | vec4 a_normal : NORMAL; 3 | vec3 a_position : POSITION; 4 | vec2 a_texcoord0 : TEXCOORD0; 5 | 6 | 7 | 8 | vec4 v_color : COLOR0; 9 | vec4 v_fog : COLOR2; 10 | vec4 v_light : COLOR1; 11 | centroid vec2 v_texCoords : TEXCOORD0; 12 | -------------------------------------------------------------------------------- /materials/MeshFallbackPosUVNormalColor/src/MeshFallbackPosUVNormalColor.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_color0, a_texcoord0, a_normal 2 | 3 | 4 | $output v_color, v_fog, v_light, v_texCoords 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | uniform vec4 ZShiftValue; 12 | uniform vec4 UVAnimation; 13 | uniform vec4 TileLightColor; 14 | uniform vec4 OverlayColor; 15 | uniform vec4 FogControl; 16 | uniform vec4 FogColor; 17 | 18 | void main() { 19 | vec4 position; 20 | #if !defined(RASTERIZED_ALPHA_TEST_PASS) && !defined(RASTERIZED_OPAQUE_PASS) && !defined(RASTERIZED_TRANSPARENT_PASS) 21 | mat4 World = u_model[0]; 22 | position = jitterVertexPosition(mul(World, vec4(a_position, 1.0)).xyz); 23 | #endif 24 | #if defined(RASTERIZED_ALPHA_TEST_PASS) || defined(RASTERIZED_OPAQUE_PASS) || defined(RASTERIZED_TRANSPARENT_PASS) 25 | position = mul(u_modelViewProj, vec4(a_position, 1.0)); 26 | #endif 27 | 28 | v_texCoords = UVAnimation.xy + (a_texcoord0.xy * UVAnimation.zw); 29 | v_color = a_color0; 30 | position.z += ZShiftValue.x; 31 | 32 | #ifdef LIT 33 | vec4 light; 34 | float L = 1.0; 35 | vec3 N = normalize(mul(World, a_normal).xyz); 36 | N.y *= TileLightColor.w; 37 | float yLight = (1.0+N.y) * 0.5; 38 | L = yLight * (1.0 - 0.45) + N.x*N.x * -0.1 + N.z*N.z * 0.1 + 0.45; 39 | light = vec4(vec3(L, L, L) * TileLightColor.xyz, 1.0); 40 | light += OverlayColor.a * 0.35; 41 | v_light = light; 42 | #endif 43 | 44 | float cameraDepth = position.z; 45 | float fogIntensity = calculateFogIntensity(cameraDepth, FogControl.z, FogControl.x, FogControl.y); 46 | v_fog = vec4(FogColor.rgb, fogIntensity); 47 | gl_Position = position; 48 | } 49 | -------------------------------------------------------------------------------- /materials/Particle/src/Particle.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_color0, v_fog, v_texcoord0 2 | 3 | #include 4 | #include 5 | 6 | SAMPLER2D_AUTOREG(s_ParticleTexture); 7 | 8 | void main() { 9 | vec4 diffuse = texture2D(s_ParticleTexture, v_texcoord0); 10 | #if ALPHA_TEST 11 | if (diffuse.w < 0.5) { 12 | discard; 13 | } 14 | #endif 15 | 16 | diffuse *= v_color0; 17 | diffuse.rgb = applyFog(diffuse.rgb, v_fog.rgb, v_fog.a); 18 | 19 | #if ALPHA_TEST 20 | diffuse.a = 1.0; 21 | #endif 22 | 23 | gl_FragColor = diffuse; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /materials/Particle/src/Particle.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec4 a_color0 : COLOR0; 2 | vec3 a_position : POSITION; 3 | vec2 a_texcoord0 : TEXCOORD0; 4 | 5 | vec4 v_color0 : COLOR0; 6 | vec4 v_fog : COLOR1; 7 | vec2 v_texcoord0 : TEXCOORD0; 8 | -------------------------------------------------------------------------------- /materials/Particle/src/Particle.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_color0, a_position, a_texcoord0 2 | $output v_color0, v_fog, v_texcoord0 3 | 4 | #include 5 | #include 6 | 7 | uniform vec4 FogAndDistanceControl; 8 | uniform vec4 FogColor; 9 | 10 | void main() { 11 | vec4 pos = vec4(a_position, 1.0); 12 | vec4 worldPos = vec4(mul(u_model[0], pos).xyz, 1.0); 13 | vec4 position = mul(u_viewProj, worldPos); 14 | 15 | float fogIntensity = calculateFogIntensity(position.z, FogAndDistanceControl.z, FogAndDistanceControl.x, FogAndDistanceControl.y); 16 | vec4 fog = vec4(FogColor.rgb, fogIntensity); 17 | 18 | v_texcoord0 = a_texcoord0; 19 | v_color0 = a_color0; 20 | v_fog = fog; 21 | gl_Position = position; 22 | } 23 | -------------------------------------------------------------------------------- /materials/PopulateVolume/src/PopulateVolume.compute.sc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct LightData { 4 | float lookup; 5 | }; 6 | 7 | struct Light { 8 | vec4 position; 9 | vec4 color; 10 | int shadowProbeIndex; 11 | int pad0; 12 | int pad1; 13 | int pad2; 14 | }; 15 | 16 | struct LightSourceWorldInfo { 17 | vec4 worldSpaceDirection; 18 | vec4 diffuseColorAndIlluminance; 19 | vec4 shadowDirection; 20 | mat4 shadowProj0; 21 | mat4 shadowProj1; 22 | mat4 shadowProj2; 23 | mat4 shadowProj3; 24 | int isSun; 25 | int shadowCascadeNumber; 26 | int pad0; 27 | int pad1; 28 | }; 29 | 30 | uniform mat4 PrevInvProj; 31 | uniform mat4 PointLightProj; 32 | uniform vec4 PointLightShadowParams1; 33 | uniform vec4 SunDir; 34 | uniform vec4 ShadowBias; 35 | uniform vec4 ShadowSlopeBias; 36 | uniform vec4 BlockBaseAmbientLightColorIntensity; 37 | uniform vec4 PointLightAttenuationWindowEnabled; 38 | uniform vec4 ManhattanDistAttenuationEnabled; 39 | uniform vec4 JitterOffset; 40 | uniform vec4 CascadeShadowResolutions; 41 | uniform vec4 ShadowPCFWidth; 42 | uniform vec4 FogColor; 43 | uniform vec4 VolumeDimensions; 44 | uniform vec4 AlbedoExtinction; 45 | uniform vec4 SkyZenithColor; 46 | uniform vec4 AtmosphericScatteringToggles; 47 | uniform vec4 AmbientContribution; 48 | uniform vec4 FogAndDistanceControl; 49 | uniform vec4 AtmosphericScattering; 50 | uniform vec4 ClusterSize; 51 | uniform vec4 ClusterNearFarWidthHeight; 52 | uniform vec4 CameraLightIntensity; 53 | uniform vec4 WorldOrigin; 54 | uniform mat4 CloudShadowProj; 55 | uniform vec4 ClusterDimensions; 56 | uniform vec4 DiffuseSpecularEmissiveAmbientTermToggles; 57 | uniform vec4 DirectionalLightToggleAndCountAndMaxDistanceAndMaxCascadesPerLight; 58 | uniform vec4 TemporalSettings; 59 | uniform vec4 DirectionalShadowModeAndCloudShadowToggleAndPointLightToggleAndShadowToggle; 60 | uniform vec4 EmissiveMultiplierAndDesaturationAndCloudPCFAndContribution; 61 | uniform vec4 ShadowParams; 62 | uniform vec4 MoonColor; 63 | uniform vec4 FirstPersonPlayerShadowsEnabledAndResolutionAndFilterWidth; 64 | uniform vec4 FogSkyBlend; 65 | uniform vec4 HeightFogScaleBias; 66 | uniform vec4 IBLParameters; 67 | uniform vec4 PointLightDiffuseFadeOutParameters; 68 | uniform vec4 MoonDir; 69 | uniform mat4 PlayerShadowProj; 70 | uniform vec4 PointLightAttenuationWindow; 71 | uniform vec4 SunColor; 72 | uniform vec4 PointLightSpecularFadeOutParameters; 73 | uniform vec4 RenderChunkFogAlpha; 74 | uniform vec4 SkyAmbientLightColorIntensity; 75 | uniform vec4 SkyHorizonColor; 76 | uniform vec4 VolumeNearFar; 77 | uniform vec4 VolumeScatteringEnabled; 78 | uniform vec4 VolumeShadowSettings; 79 | 80 | IMAGE2D_ARRAY_WR_AUTOREG(s_CurrentLightingBuffer, rgba16f); 81 | SAMPLER2DARRAY_AUTOREG(s_PreviousLightingBuffer); 82 | 83 | SAMPLER2D_AUTOREG(s_BrdfLUT); 84 | SAMPLERCUBE_AUTOREG(s_SpecularIBLCurrent); 85 | SAMPLERCUBE_AUTOREG(s_SpecularIBLPrevious); 86 | 87 | SAMPLER2DARRAYSHADOW_AUTOREG(s_ShadowCascades); 88 | SAMPLER2DSHADOW_AUTOREG(s_PlayerShadowMap); 89 | SAMPLER2DARRAYSHADOW_AUTOREG(s_PointLightShadowTextureArray); 90 | 91 | SAMPLER2DARRAY_AUTOREG(s_ScatteringBuffer); 92 | 93 | #if BGFX_SHADER_LANGUAGE_GLSL 94 | BUFFER_RW_AUTOREG(s_DirectionalLightSources, LightSourceWorldInfo); 95 | BUFFER_RW_AUTOREG(s_LightLookupArray, LightData); 96 | BUFFER_RW_AUTOREG(s_Lights, Light); 97 | #else 98 | BUFFER_RO_AUTOREG(s_DirectionalLightSources, LightSourceWorldInfo); 99 | BUFFER_RO_AUTOREG(s_LightLookupArray, LightData); 100 | BUFFER_RO_AUTOREG(s_Lights, Light); 101 | #endif 102 | 103 | bool areCascadedShadowsEnabled(float mode) { 104 | return int(mode) == 1; 105 | } 106 | 107 | int GetShadowCascade(int lightIndex, vec3 worldPos, out vec4 projPos) { 108 | LightSourceWorldInfo light = s_DirectionalLightSources[lightIndex]; 109 | for(int c = 0; c < 4; ++ c) { 110 | mat4 proj; 111 | if (c == 0) { 112 | proj = light.shadowProj0; 113 | } else if (c == 1) { 114 | proj = light.shadowProj1; 115 | } else if (c == 2) { 116 | proj = light.shadowProj2; 117 | } else if (c == 3) { 118 | proj = light.shadowProj3; 119 | } 120 | projPos = mul(proj, vec4(worldPos, 1.0)); 121 | projPos /= projPos.w; 122 | vec3 posDiff = clamp(projPos.xyz, vec3(-1.0, -1.0, -1.0), vec3(1.0, 1.0, 1.0)) - projPos.xyz; 123 | if (length(posDiff) == 0.0) { 124 | return c; 125 | } 126 | } 127 | return - 1; 128 | } 129 | 130 | float GetFilteredCloudShadow(vec3 worldPos, float NdL) { 131 | const int cloudCascade = 0; 132 | vec4 cloudProjPos = mul(CloudShadowProj, vec4(worldPos, 1.0)); 133 | cloudProjPos /= cloudProjPos.w; 134 | #if !BGFX_SHADER_LANGUAGE_GLSL 135 | cloudProjPos.y *= -1.0; 136 | #endif 137 | float bias = ShadowBias[cloudCascade] + ShadowSlopeBias[cloudCascade] * clamp(tan(acos(NdL)), 0.0, 1.0); 138 | cloudProjPos.z -= bias / cloudProjPos.w; 139 | vec2 cloudUv = (vec2(cloudProjPos.x, cloudProjPos.y) * 0.5f + 0.5f) * CascadeShadowResolutions[cloudCascade]; 140 | const int MaxFilterWidth = 9; 141 | int filterWidth = clamp(int(EmissiveMultiplierAndDesaturationAndCloudPCFAndContribution.z * 1.0 + 0.5f), 1, MaxFilterWidth); 142 | int filterOffset = filterWidth / 2; 143 | float amt = 0.f; 144 | #if BGFX_SHADER_LANGUAGE_GLSL 145 | cloudProjPos.z = cloudProjPos.z * 0.5 + 0.5; 146 | cloudUv.y += 1.0 - CascadeShadowResolutions[cloudCascade]; 147 | #endif 148 | for(int iy = 0; iy < filterWidth; ++ iy) { 149 | for(int ix = 0; ix < filterWidth; ++ ix) { 150 | float y = float(iy - filterOffset) + 0.5f; 151 | float x = float(ix - filterOffset) + 0.5f; 152 | vec2 offset = vec2(x, y) * ShadowParams.x; 153 | amt += shadow2DArray(s_ShadowCascades, vec4(cloudUv + (offset * CascadeShadowResolutions[cloudCascade]), DirectionalLightToggleAndCountAndMaxDistanceAndMaxCascadesPerLight.w * float(2), cloudProjPos.z)); 154 | } 155 | } 156 | return amt / float(filterWidth * filterWidth); 157 | } 158 | 159 | float GetPlayerShadow(vec3 worldPos, float NdL) { 160 | const int playerCascade = 0; 161 | vec4 playerProjPos = mul(PlayerShadowProj, vec4(worldPos, 1.0)); 162 | playerProjPos /= playerProjPos.w; 163 | #if !BGFX_SHADER_LANGUAGE_GLSL 164 | playerProjPos.y *= -1.0; 165 | #endif 166 | float bias = ShadowBias[playerCascade] + ShadowSlopeBias[playerCascade] * clamp(tan(acos(NdL)), 0.0, 1.0); 167 | playerProjPos.z -= bias / playerProjPos.w; 168 | playerProjPos.z = min(playerProjPos.z, 1.0); 169 | vec2 playerUv = (vec2(playerProjPos.x, playerProjPos.y) * 0.5f + 0.5f) * FirstPersonPlayerShadowsEnabledAndResolutionAndFilterWidth.y; 170 | const int MaxFilterWidth = 9; 171 | int filterWidth = clamp(int(2.0 * 1.0 + 0.5f), 1, MaxFilterWidth); 172 | int filterOffset = filterWidth / 2; 173 | float amt = 0.f; 174 | #if BGFX_SHADER_LANGUAGE_GLSL 175 | playerProjPos.z = playerProjPos.z * 0.5 + 0.5; 176 | playerUv.y += 1.0 - FirstPersonPlayerShadowsEnabledAndResolutionAndFilterWidth.y; 177 | #endif 178 | for(int iy = 0; iy < filterWidth; ++ iy) { 179 | for(int ix = 0; ix < filterWidth; ++ ix) { 180 | float y = float(iy - filterOffset) + 0.5f; 181 | float x = float(ix - filterOffset) + 0.5f; 182 | vec2 offset = vec2(x, y) * FirstPersonPlayerShadowsEnabledAndResolutionAndFilterWidth.z; 183 | vec2 newUv = playerUv + (offset * FirstPersonPlayerShadowsEnabledAndResolutionAndFilterWidth.y); 184 | if (newUv.x >= 0.0 && newUv.x < 1.0 && newUv.y >= 0.0 && newUv.y < 1.0) { 185 | amt += shadow2D(s_PlayerShadowMap, vec3(newUv, playerProjPos.z)); 186 | } 187 | else { 188 | amt += 1.0f; 189 | } 190 | } 191 | } 192 | return amt / float(filterWidth * filterWidth); 193 | } 194 | 195 | float GetFilteredShadow(int cascadeIndex, float projZ, int cascade, vec2 uv) { 196 | const int MaxFilterWidth = 9; 197 | int filterWidth = clamp(int(ShadowPCFWidth[cascade] * 1.0 + 0.5), 1, MaxFilterWidth); 198 | int filterOffset = filterWidth / 2; 199 | float amt = 0.f; 200 | vec2 baseUv = uv * CascadeShadowResolutions[cascade]; 201 | #if BGFX_SHADER_LANGUAGE_GLSL 202 | projZ = projZ * 0.5 + 0.5; 203 | baseUv.y += 1.0 - CascadeShadowResolutions[cascade]; 204 | #endif 205 | for(int iy = 0; iy < filterWidth; ++ iy) { 206 | for(int ix = 0; ix < filterWidth; ++ ix) { 207 | float y = float(iy - filterOffset) + 0.5f; 208 | float x = float(ix - filterOffset) + 0.5f; 209 | vec2 offset = vec2(x, y) * ShadowParams.x; 210 | if (cascadeIndex >= 0) { 211 | amt += shadow2DArray(s_ShadowCascades, vec4(baseUv + (offset * CascadeShadowResolutions[cascade]), (float(cascadeIndex) * DirectionalLightToggleAndCountAndMaxDistanceAndMaxCascadesPerLight.w) + float(cascade), projZ)); 212 | } else { 213 | amt += 1.0; 214 | } 215 | } 216 | } 217 | return amt / float(filterWidth * filterWidth); 218 | } 219 | 220 | float GetShadowAmount(int lightIndex, vec3 worldPos, float NdL, float viewDepth) { 221 | float amt = 1.0; 222 | float cloudAmt = 1.0; 223 | float playerAmt = 1.0; 224 | vec4 projPos; 225 | int cascade = GetShadowCascade(lightIndex, worldPos, projPos); 226 | if (cascade != -1) { 227 | #if !BGFX_SHADER_LANGUAGE_GLSL 228 | projPos.y *= -1.0; 229 | #endif 230 | float bias = ShadowBias[cascade] + ShadowSlopeBias[cascade] * clamp(tan(acos(NdL)), 0.0, 1.0); 231 | projPos.z -= bias / projPos.w; 232 | vec2 uv = vec2(projPos.x, projPos.y) * 0.5f + 0.5f; 233 | amt = GetFilteredShadow(s_DirectionalLightSources[lightIndex].shadowCascadeNumber, projPos.z, cascade, uv); 234 | if (int(FirstPersonPlayerShadowsEnabledAndResolutionAndFilterWidth.x) > 0) { 235 | playerAmt = GetPlayerShadow(worldPos, NdL); 236 | amt = min(amt, playerAmt); 237 | } 238 | if (s_DirectionalLightSources[lightIndex].isSun > 0 && int(DirectionalShadowModeAndCloudShadowToggleAndPointLightToggleAndShadowToggle.y) > 0) { 239 | cloudAmt = GetFilteredCloudShadow(worldPos, NdL); 240 | if (cloudAmt < 1.0) { 241 | cloudAmt = max(cloudAmt, 1.0 - EmissiveMultiplierAndDesaturationAndCloudPCFAndContribution.w); 242 | amt = min(amt, cloudAmt); 243 | } 244 | } 245 | float shadowFade = smoothstep(max(0.0, ShadowParams.y - 8.0), ShadowParams.y, -viewDepth); 246 | amt = mix(amt, 1.0, shadowFade); 247 | } 248 | return amt; 249 | } 250 | 251 | float linearToLogDepth(float linearDepth) { 252 | return log((exp(4.0) - 1.0) * linearDepth + 1.0) / 4.0; 253 | } 254 | 255 | float logToLinearDepth(float logDepth) { 256 | return (exp(4.0 * logDepth) - 1.0) / (exp(4.0) - 1.0); 257 | } 258 | 259 | vec3 ndcToVolume(vec3 ndc, mat4 inverseProj, vec2 nearFar) { 260 | vec2 uv = 0.5 * (ndc.xy + vec2(1.0, 1.0)); 261 | vec4 view = mul(inverseProj, vec4(ndc, 1.0)); 262 | float viewDepth = -view.z / view.w; 263 | float wLinear = (viewDepth - nearFar.x) / (nearFar.y - nearFar.x); 264 | return vec3(uv, linearToLogDepth(wLinear)); 265 | } 266 | 267 | vec3 volumeToNdc(vec3 uvw, mat4 proj, vec2 nearFar) { 268 | vec2 xy = 2.0 * uvw.xy - vec2(1.0, 1.0); 269 | float wLinear = logToLinearDepth(uvw.z); 270 | float viewDepth = -((1.0 - wLinear) * nearFar.x + wLinear * nearFar.y); 271 | vec4 ndcDepth = mul(proj, vec4(0.0, 0.0, viewDepth, 1.0)); 272 | float z = ndcDepth.z / ndcDepth.w; 273 | return vec3(xy, z); 274 | } 275 | 276 | vec3 worldToVolume(vec3 world, mat4 viewProj, mat4 invProj, vec2 nearFar) { 277 | vec4 proj = mul(viewProj, vec4(world, 1.0)); 278 | vec3 ndc = proj.xyz / proj.w; 279 | return ndcToVolume(ndc, invProj, nearFar); 280 | } 281 | 282 | vec3 volumeToWorld(vec3 uvw, mat4 invViewProj, mat4 proj, vec2 nearFar) { 283 | vec3 ndc = volumeToNdc(uvw, proj, nearFar); 284 | vec4 world = mul(invViewProj, vec4(ndc, 1.0)); 285 | return world.xyz / world.w; 286 | } 287 | 288 | vec4 sampleVolume(highp sampler2DArray volume, ivec3 dimensions, vec3 uvw) { 289 | float depth = uvw.z * float(dimensions.z) - 0.5; 290 | int index = clamp(int(depth), 0, dimensions.z - 2); 291 | float offset = clamp(depth - float(index), 0.0, 1.0); 292 | vec4 a = texture2DArrayLod(volume, vec3(uvw.xy, index), 0.0).rgba; 293 | vec4 b = texture2DArrayLod(volume, vec3(uvw.xy, index + 1), 0.0).rgba; 294 | return mix(a, b, offset); 295 | } 296 | 297 | struct TemporalAccumulationParameters { 298 | ivec3 dimensions; 299 | vec3 previousUvw; 300 | vec4 currentValue; 301 | float historyWeight; 302 | float frustumBoundaryFalloff; 303 | }; 304 | 305 | TemporalAccumulationParameters createTemporalAccumulationParameters(ivec3 dimensions, vec3 previousUvw, vec4 currentValue, float historyWeight, float frustumBoundaryFalloff) { 306 | TemporalAccumulationParameters params; 307 | params.dimensions = dimensions; 308 | params.previousUvw = previousUvw; 309 | params.currentValue = currentValue; 310 | params.historyWeight = historyWeight; 311 | params.frustumBoundaryFalloff = frustumBoundaryFalloff; 312 | return params; 313 | } 314 | 315 | vec4 blendHistory(TemporalAccumulationParameters params, highp sampler2DArray previousVolume) { 316 | vec4 previousValue = sampleVolume(previousVolume, params.dimensions, params.previousUvw); 317 | vec3 previousTexelCoord = VolumeDimensions.xyz * params.previousUvw; 318 | vec3 previousTexelCoordClamped = clamp(previousTexelCoord, vec3(0.0, 0.0, 0.0), vec3(params.dimensions)); 319 | float distanceFromBoundary = length(previousTexelCoordClamped - previousTexelCoord); 320 | float rejectHistory = clamp(distanceFromBoundary * params.frustumBoundaryFalloff, 0.0, 1.0); 321 | float blendWeight = mix(params.historyWeight, 0.0, rejectHistory); 322 | return mix(params.currentValue, previousValue, blendWeight); 323 | } 324 | 325 | float calculateFogIntensityFaded(float cameraDepth, float maxDistance, float fogStart, float fogEndMinusStartReciprocal, float fogAlpha) { 326 | float distance = cameraDepth / maxDistance; 327 | distance += fogAlpha; 328 | return clamp((distance - fogStart) * fogEndMinusStartReciprocal, 0.0, 1.0); 329 | } 330 | 331 | void Populate(uvec3 GlobalInvocationID) { 332 | int volumeWidth = int(VolumeDimensions.x); 333 | int volumeHeight = int(VolumeDimensions.y); 334 | int volumeDepth = int(VolumeDimensions.z); 335 | int x = int(GlobalInvocationID.x); 336 | int y = int(GlobalInvocationID.y); 337 | int z = int(GlobalInvocationID.z); 338 | if (x >= volumeWidth || y >= volumeHeight || z >= volumeDepth) { 339 | return; 340 | } 341 | vec3 uvw = (vec3(x, y, z) + vec3(0.5, 0.5, 0.5) + JitterOffset.xyz) / VolumeDimensions.xyz; 342 | vec3 worldPosition = volumeToWorld(uvw, u_invViewProj, u_proj, VolumeNearFar.xy); 343 | vec3 viewPosition = mul(u_view, vec4(worldPosition, 1.0)).xyz; 344 | vec3 worldAbsolute = worldPosition - WorldOrigin.xyz; 345 | float density = clamp(HeightFogScaleBias.x * worldPosition.y + HeightFogScaleBias.y, 0.0, 1.0); 346 | float viewDistance = length(viewPosition); 347 | float fogIntensity = calculateFogIntensityFaded(viewDistance, FogAndDistanceControl.z, FogAndDistanceControl.x, FogAndDistanceControl.y, RenderChunkFogAlpha.x); 348 | density = mix(density, 0.0, fogIntensity); 349 | vec3 scattering = density * AlbedoExtinction.rgb * AlbedoExtinction.a; 350 | float extinction = density * AlbedoExtinction.a; 351 | vec3 source = vec3(0.0, 0.0, 0.0); 352 | vec3 blockAmbient = AmbientContribution.x * BlockBaseAmbientLightColorIntensity.rgb * BlockBaseAmbientLightColorIntensity.a; 353 | vec3 skyAmbient = AmbientContribution.y * SkyAmbientLightColorIntensity.rgb * SkyAmbientLightColorIntensity.a; 354 | vec3 ambient = blockAmbient + skyAmbient; 355 | ambient = max(ambient, vec3_splat(AmbientContribution.z)); 356 | source += scattering * ambient * DiffuseSpecularEmissiveAmbientTermToggles.w; 357 | int lightCount = int(DirectionalLightToggleAndCountAndMaxDistanceAndMaxCascadesPerLight.y); 358 | for (int i = 0; i < lightCount; i++) { 359 | float directOcclusion = 1.0; 360 | if (areCascadedShadowsEnabled(DirectionalShadowModeAndCloudShadowToggleAndPointLightToggleAndShadowToggle.x)) { 361 | directOcclusion = GetShadowAmount( 362 | i, 363 | worldPosition, 364 | 1.0, 365 | 0.0 366 | ); 367 | } 368 | vec4 colorAndIlluminance = s_DirectionalLightSources[i].diffuseColorAndIlluminance; 369 | vec3 illuminance = colorAndIlluminance.rgb * colorAndIlluminance.a; 370 | source += scattering * directOcclusion * illuminance; 371 | } 372 | if (TemporalSettings.x > 0.0) { 373 | vec3 uvwUnjittered = (vec3(x, y, z) + vec3(0.5, 0.5, 0.5)) / VolumeDimensions.xyz; 374 | vec3 worldPositionUnjittered = volumeToWorld(uvwUnjittered, u_invViewProj, u_proj, VolumeNearFar.xy); 375 | vec3 viewPositionUnjittered = mul(u_view, vec4(worldPositionUnjittered, 1.0)).xyz; 376 | vec3 previousWorldPosition = worldPositionUnjittered - u_prevWorldPosOffset.xyz; 377 | vec3 previousUvw = worldToVolume(previousWorldPosition, u_prevViewProj, PrevInvProj, VolumeNearFar.xy); 378 | vec4 currentValue = vec4(source, extinction); 379 | TemporalAccumulationParameters params = createTemporalAccumulationParameters( 380 | ivec3(VolumeDimensions.xyz), 381 | previousUvw, 382 | currentValue, 383 | TemporalSettings.z, 384 | TemporalSettings.y 385 | ); 386 | vec4 result = blendHistory(params, s_PreviousLightingBuffer); 387 | imageStore(s_CurrentLightingBuffer, ivec3(x, y, z), result); 388 | } else { 389 | imageStore(s_CurrentLightingBuffer, ivec3(x, y, z), vec4(source, extinction)); 390 | } 391 | } 392 | 393 | NUM_THREADS(8, 8, 8) 394 | void main() { 395 | Populate(gl_GlobalInvocationID); 396 | } 397 | -------------------------------------------------------------------------------- /materials/PopulateVolume/src/PopulateVolume.fragment.sc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() { 4 | gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); 5 | } 6 | -------------------------------------------------------------------------------- /materials/PopulateVolume/src/PopulateVolume.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec3 a_position : POSITION; 2 | -------------------------------------------------------------------------------- /materials/PopulateVolume/src/PopulateVolume.vertex.sc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() { 4 | gl_Position = vec4(0.0, 0.0, 0.0, 0.0); 5 | } 6 | -------------------------------------------------------------------------------- /materials/RenderChunk/src/RenderChunk.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_color0, v_fog, v_texcoord0, v_lightmapUV 2 | 3 | #include 4 | 5 | uniform vec4 FogColor; 6 | 7 | SAMPLER2D_AUTOREG(s_MatTexture); 8 | SAMPLER2D_AUTOREG(s_SeasonsTexture); 9 | SAMPLER2D_AUTOREG(s_LightMapTexture); 10 | 11 | void main() { 12 | vec4 diffuse; 13 | 14 | #if defined(DEPTH_ONLY_OPAQUE) || defined(DEPTH_ONLY) 15 | diffuse.rgb = vec3(1.0, 1.0, 1.0); 16 | #else 17 | diffuse = texture2D(s_MatTexture, v_texcoord0); 18 | 19 | #if defined(ALPHA_TEST) 20 | if (diffuse.a < 0.5) { 21 | discard; 22 | } 23 | #endif 24 | 25 | #if defined(SEASONS) && (defined(OPAQUE) || defined(ALPHA_TEST)) 26 | diffuse.rgb *= 27 | mix(vec3(1.0, 1.0, 1.0), 28 | texture2D(s_SeasonsTexture, v_color0.xy).rgb * 2.0, v_color0.b); 29 | diffuse.rgb *= v_color0.aaa; 30 | #else 31 | diffuse *= v_color0; 32 | #endif 33 | #endif 34 | 35 | #ifndef TRANSPARENT 36 | diffuse.a = 1.0; 37 | #endif 38 | 39 | diffuse.rgb *= texture2D(s_LightMapTexture, v_lightmapUV).rgb; 40 | 41 | diffuse.rgb = mix(diffuse.rgb, FogColor.rgb, v_fog.a); 42 | gl_FragColor = diffuse; 43 | } -------------------------------------------------------------------------------- /materials/RenderChunk/src/RenderChunk.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec4 a_color0 : COLOR0; 2 | vec3 a_position : POSITION; 3 | vec2 a_texcoord0 : TEXCOORD0; 4 | vec2 a_texcoord1 : TEXCOORD1; 5 | 6 | vec4 i_data0 : TEXCOORD7; 7 | vec4 i_data1 : TEXCOORD6; 8 | vec4 i_data2 : TEXCOORD5; 9 | 10 | vec4 v_color0 : COLOR0; 11 | vec4 v_fog : COLOR2; 12 | centroid vec2 v_texcoord0 : TEXCOORD0; 13 | vec2 v_lightmapUV : TEXCOORD1; 14 | -------------------------------------------------------------------------------- /materials/RenderChunk/src/RenderChunk.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_color0, a_position, a_texcoord0, a_texcoord1 2 | #ifdef INSTANCING 3 | $input i_data0, i_data1, i_data2 4 | #endif 5 | $output v_color0, v_fog, v_texcoord0, v_lightmapUV 6 | 7 | #include 8 | 9 | uniform vec4 RenderChunkFogAlpha; 10 | uniform vec4 FogAndDistanceControl; 11 | uniform vec4 ViewPositionAndTime; 12 | uniform vec4 FogColor; 13 | 14 | void main() { 15 | mat4 model; 16 | #ifdef INSTANCING 17 | model = mtxFromCols(i_data0, i_data1, i_data2, vec4(0, 0, 0, 1)); 18 | #else 19 | model = u_model[0]; 20 | #endif 21 | 22 | vec3 worldPos = mul(model, vec4(a_position, 1.0)).xyz; 23 | vec4 color; 24 | #ifdef RENDER_AS_BILLBOARDS 25 | worldPos += vec3(0.5, 0.5, 0.5); 26 | vec3 viewDir = normalize(worldPos - ViewPositionAndTime.xyz); 27 | vec3 boardPlane = normalize(vec3(viewDir.z, 0.0, -viewDir.x)); 28 | worldPos = (worldPos - 29 | ((((viewDir.yzx * boardPlane.zxy) - (viewDir.zxy * boardPlane.yzx)) * 30 | (a_color0.z - 0.5)) + 31 | (boardPlane * (a_color0.x - 0.5)))); 32 | color = vec4(1.0, 1.0, 1.0, 1.0); 33 | #else 34 | color = a_color0; 35 | #endif 36 | 37 | vec3 modelCamPos = (ViewPositionAndTime.xyz - worldPos); 38 | float camDis = length(modelCamPos); 39 | vec4 fogColor; 40 | fogColor.rgb = FogColor.rgb; 41 | fogColor.a = clamp(((((camDis / FogAndDistanceControl.z) + RenderChunkFogAlpha.x) - 42 | FogAndDistanceControl.x) / (FogAndDistanceControl.y - FogAndDistanceControl.x)), 0.0, 1.0); 43 | 44 | #ifdef TRANSPARENT 45 | if(a_color0.a < 0.95) { 46 | color.a = mix(a_color0.a, 1.0, clamp((camDis / FogAndDistanceControl.w), 0.0, 1.0)); 47 | }; 48 | #endif 49 | 50 | v_texcoord0 = a_texcoord0; 51 | v_lightmapUV = a_texcoord1; 52 | v_color0 = color; 53 | v_fog = fogColor; 54 | gl_Position = mul(u_viewProj, vec4(worldPos, 1.0)); 55 | } 56 | -------------------------------------------------------------------------------- /materials/RenderChunkForwardPBR/defines.json: -------------------------------------------------------------------------------- 1 | { 2 | "pass": { 3 | "ForwardPBRTransparent": ["FORWARD_PBR_TRANSPARENT"] 4 | } 5 | } -------------------------------------------------------------------------------- /materials/RenderChunkForwardPBR/src/RenderChunkForwardPBR.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_color0, v_fog, v_normal, v_tangent, v_bitangent, v_texcoord0, v_lightmapUV, v_worldPos, v_blockAmbientContribution, v_skyAmbientContribution 2 | #if defined(FORWARD_PBR_TRANSPARENT) 3 | $input v_pbrTextureId 4 | #endif 5 | 6 | #include 7 | #include 8 | 9 | #if defined(FORWARD_PBR_TRANSPARENT) 10 | 11 | #include "../../DeferredShading/src/DoDeferredShading.sc" 12 | 13 | SAMPLER2D_AUTOREG(s_MatTexture); 14 | SAMPLER2D_AUTOREG(s_SeasonsTexture); 15 | SAMPLER2D_AUTOREG(s_LightMapTexture); 16 | BUFFER_RO_AUTOREG(s_PBRData, PBRTextureData); 17 | 18 | float saturatedLinearRemapZeroToOne(float value, float zeroValue, float oneValue) { 19 | return saturate((((value) * (1.f / (oneValue - zeroValue))) + -zeroValue / (oneValue - zeroValue))); 20 | } 21 | 22 | vec3 calculateTangentNormalFromHeightmap(sampler2D heightmapTexture, vec2 heightmapUV, float mipLevel) { 23 | vec3 tangentNormal = vec3(0.f, 0.f, 1.f); 24 | const float kHeightMapPixelEdgeWidth = 1.0f / 12.0f; 25 | const float kHeightMapDepth = 4.0f; 26 | const float kRecipHeightMapDepth = 1.0f / kHeightMapDepth; 27 | float fadeForLowerMips = saturatedLinearRemapZeroToOne(mipLevel, 2.f, 1.f); 28 | if (fadeForLowerMips > 0.f) { 29 | vec2 widthHeight = vec2(textureSize(heightmapTexture, 0)); 30 | vec2 pixelCoord = heightmapUV * widthHeight; 31 | { 32 | const float kNudgePixelCentreDistEpsilon = 0.0625f; 33 | const float kNudgeUvEpsilon = 0.25f / 65536.f; 34 | vec2 nudgeSampleCoord = fract(pixelCoord); 35 | if (abs(nudgeSampleCoord.x - 0.5) < kNudgePixelCentreDistEpsilon) { 36 | heightmapUV.x += (nudgeSampleCoord.x > 0.5f) ? kNudgeUvEpsilon : -kNudgeUvEpsilon; 37 | } 38 | if (abs(nudgeSampleCoord.y - 0.5) < kNudgePixelCentreDistEpsilon) { 39 | heightmapUV.y += (nudgeSampleCoord.y > 0.5f) ? kNudgeUvEpsilon : -kNudgeUvEpsilon; 40 | } 41 | } 42 | vec4 heightSamples = textureGather(heightmapTexture, heightmapUV, 0); 43 | vec2 subPixelCoord = fract(pixelCoord + 0.5f); 44 | const float kBevelMode = 0.0f; 45 | vec2 axisSamplePair = (subPixelCoord.y > 0.5f) ? heightSamples.xy : heightSamples.wz; 46 | float axisBevelCentreSampleCoord = subPixelCoord.x; 47 | axisBevelCentreSampleCoord += ((axisSamplePair.x > axisSamplePair.y) ? kHeightMapPixelEdgeWidth : -kHeightMapPixelEdgeWidth) * kBevelMode; 48 | ivec2 axisSampleIndices = ivec2(saturate(vec2(axisBevelCentreSampleCoord - kHeightMapPixelEdgeWidth, axisBevelCentreSampleCoord + kHeightMapPixelEdgeWidth) * 2.f)); 49 | tangentNormal.x = (axisSamplePair[axisSampleIndices.x] - axisSamplePair[axisSampleIndices.y]); 50 | axisSamplePair = (subPixelCoord.x > 0.5f) ? heightSamples.zy : heightSamples.wx; 51 | axisBevelCentreSampleCoord = subPixelCoord.y; 52 | axisBevelCentreSampleCoord += ((axisSamplePair.x > axisSamplePair.y) ? kHeightMapPixelEdgeWidth : -kHeightMapPixelEdgeWidth) * kBevelMode; 53 | axisSampleIndices = ivec2(saturate(vec2(axisBevelCentreSampleCoord - kHeightMapPixelEdgeWidth, axisBevelCentreSampleCoord + kHeightMapPixelEdgeWidth) * 2.f)); 54 | tangentNormal.y = (axisSamplePair[axisSampleIndices.x] - axisSamplePair[axisSampleIndices.y]); 55 | tangentNormal.z = kRecipHeightMapDepth; 56 | tangentNormal = normalize(tangentNormal); 57 | tangentNormal.xy *= fadeForLowerMips; 58 | } 59 | return tangentNormal; 60 | } 61 | 62 | vec2 getPBRDataUV(vec2 surfaceUV, vec2 uvScale, vec2 uvBias) { 63 | return (((surfaceUV) * (uvScale)) + uvBias); 64 | } 65 | 66 | #endif // FORWARD_PBR_TRANSPARENT 67 | 68 | void main() { 69 | #if FORWARD_PBR_TRANSPARENT 70 | vec4 diffuse = texture2D(s_MatTexture, v_texcoord0); 71 | 72 | const float ALPHA_THRESHOLD = 0.5; 73 | if (diffuse.a < ALPHA_THRESHOLD) { 74 | discard; 75 | } 76 | 77 | diffuse.rgb *= v_color0.rgb; 78 | diffuse.a *= v_color0.a; 79 | 80 | //RenderChunkSurfTransparent 81 | //applyPBRValuesToSurfaceOutput 82 | PBRTextureData pbrTextureData = s_PBRData[v_pbrTextureId]; 83 | vec2 normalUVScale = vec2(pbrTextureData.colourToNormalUvScale0, pbrTextureData.colourToNormalUvScale1); 84 | vec2 normalUVBias = vec2(pbrTextureData.colourToNormalUvBias0, pbrTextureData.colourToNormalUvBias1); 85 | vec2 materialUVScale = vec2(pbrTextureData.colourToMaterialUvScale0, pbrTextureData.colourToMaterialUvScale1); 86 | vec2 materialUVBias = vec2(pbrTextureData.colourToMaterialUvBias0, pbrTextureData.colourToMaterialUvBias1); 87 | 88 | int kPBRTextureDataFlagHasMaterialTexture = (1 << 0); 89 | // These two are mutually exclusive 90 | int kPBRTextureDataFlagHasNormalTexture = (1 << 1); 91 | int kPBRTextureDataFlagHasHeightMapTexture = (1 << 2); 92 | 93 | vec3 tangentNormal = vec3(0, 0, 1); 94 | if ((pbrTextureData.flags & kPBRTextureDataFlagHasNormalTexture) == kPBRTextureDataFlagHasNormalTexture) { 95 | vec2 uv = getPBRDataUV(v_texcoord0, normalUVScale, normalUVBias); 96 | tangentNormal = texture2D(s_MatTexture, uv).xyz * 2.f - 1.f; 97 | } else if ((pbrTextureData.flags & kPBRTextureDataFlagHasHeightMapTexture) == kPBRTextureDataFlagHasHeightMapTexture) { 98 | vec2 normalUv = getPBRDataUV(v_texcoord0, normalUVScale, normalUVBias); 99 | float normalMipLevel = min(pbrTextureData.maxMipNormal - pbrTextureData.maxMipColour, pbrTextureData.maxMipNormal); 100 | tangentNormal = calculateTangentNormalFromHeightmap(s_MatTexture, normalUv, normalMipLevel); 101 | } 102 | 103 | float emissive = pbrTextureData.uniformEmissive; 104 | float metalness = pbrTextureData.uniformMetalness; 105 | float linearRoughness = pbrTextureData.uniformRoughness; 106 | if ((pbrTextureData.flags & kPBRTextureDataFlagHasMaterialTexture) == kPBRTextureDataFlagHasMaterialTexture) { 107 | vec2 uv = getPBRDataUV(v_texcoord0, materialUVScale, materialUVBias); 108 | vec3 texel = texture2D(s_MatTexture, uv).rgb; 109 | metalness = texel.r; 110 | emissive = texel.g; 111 | linearRoughness = texel.b; 112 | } 113 | 114 | mat3 tbn = mtxFromRows( 115 | normalize(v_tangent), 116 | normalize(v_bitangent), 117 | normalize(v_normal)); 118 | tbn = transpose(tbn); 119 | vec3 normal = mul(tbn, tangentNormal).xyz; 120 | 121 | //computeLighting_RenderChunk_SplitLightMapValues 122 | vec2 newLightmapUV = min(v_lightmapUV.xy, vec2(1.0f, 1.0f)); 123 | vec3 blockLight = texture2D(s_LightMapTexture, min(vec2(newLightmapUV.x, 1.5f * 1.0f / 16.0f), vec2(1.0, 1.0))).rgb; 124 | vec3 skyLight = texture2D(s_LightMapTexture, min(vec2(newLightmapUV.y, 0.5f * 1.0f / 16.0f), vec2(1.0, 1.0))).rgb; 125 | 126 | PBRFragmentInfo fragmentData; 127 | vec4 viewPosition = mul(u_view, vec4(v_worldPos, 1.0)); 128 | vec4 clipPosition = mul(u_proj, viewPosition); 129 | vec3 ndcPosition = clipPosition.xyz / clipPosition.w; 130 | vec2 uv = (ndcPosition.xy + vec2(1.0, 1.0)) / 2.0; 131 | vec4 worldNormal = vec4(normal, 0.0); 132 | vec4 viewNormal = mul(u_view, worldNormal); 133 | fragmentData.lightClusterUV = uv; 134 | fragmentData.worldPosition = v_worldPos; 135 | fragmentData.viewPosition = viewPosition.xyz; 136 | fragmentData.ndcPosition = ndcPosition; 137 | fragmentData.worldNormal = worldNormal.xyz; 138 | fragmentData.viewNormal = viewNormal.xyz; 139 | fragmentData.albedo = diffuse.rgb; 140 | fragmentData.roughness = linearRoughness; 141 | fragmentData.metalness = metalness; 142 | fragmentData.emissive = emissive; 143 | fragmentData.blockAmbientContribution = blockLight.x; 144 | fragmentData.skyAmbientContribution = skyLight.x; 145 | 146 | gl_FragColor.rgb = evaluateFragmentColor(fragmentData).rgb; 147 | gl_FragColor.a = diffuse.a; 148 | 149 | #else 150 | 151 | gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); 152 | #endif 153 | } 154 | -------------------------------------------------------------------------------- /materials/RenderChunkForwardPBR/src/RenderChunkForwardPBR.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec4 a_color0 : COLOR0; 2 | vec4 a_normal : NORMAL; 3 | vec4 a_tangent : TANGENT; 4 | vec3 a_position : POSITION; 5 | vec2 a_texcoord0 : TEXCOORD0; 6 | vec2 a_texcoord1 : TEXCOORD1; 7 | #if BGFX_SHADER_LANGUAGE_HLSL 8 | int a_texcoord4 : TEXCOORD4; 9 | #else 10 | float a_texcoord4 : TEXCOORD4; 11 | #endif 12 | 13 | vec4 i_data0 : TEXCOORD7; 14 | vec4 i_data1 : TEXCOORD6; 15 | vec4 i_data2 : TEXCOORD5; 16 | 17 | vec4 v_color0 : COLOR0; 18 | vec4 v_fog : COLOR2; 19 | vec3 v_normal : NORMAL; 20 | vec3 v_tangent : TANGENT; 21 | vec3 v_bitangent : BITANGENT; 22 | centroid vec2 v_texcoord0 : TEXCOORD0; 23 | vec2 v_lightmapUV : TEXCOORD1; 24 | vec3 v_worldPos : TEXCOORD3; 25 | flat int v_pbrTextureId : TEXCOORD4; 26 | float v_blockAmbientContribution : TEXCOORD5; 27 | float v_skyAmbientContribution : TEXCOORD6; -------------------------------------------------------------------------------- /materials/RenderChunkForwardPBR/src/RenderChunkForwardPBR.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_color0, a_position, a_texcoord0, a_texcoord1, a_normal, a_tangent 2 | #if defined(FORWARD_PBR_TRANSPARENT) 3 | $input a_texcoord4 4 | #endif 5 | #ifdef INSTANCING 6 | $input i_data0, i_data1, i_data2 7 | #endif 8 | 9 | $output v_color0, v_fog, v_normal, v_tangent, v_bitangent, v_texcoord0, v_lightmapUV, v_worldPos, v_blockAmbientContribution, v_skyAmbientContribution 10 | #if defined(FORWARD_PBR_TRANSPARENT) 11 | $output v_pbrTextureId 12 | #endif 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | uniform mat4 PointLightProj; 19 | uniform vec4 PointLightShadowParams1; 20 | uniform vec4 ShadowBias; 21 | uniform vec4 SunDir; 22 | uniform vec4 ShadowSlopeBias; 23 | uniform vec4 PrepassUVOffset; 24 | uniform vec4 BlockBaseAmbientLightColorIntensity; 25 | uniform vec4 CascadeShadowResolutions; 26 | uniform vec4 LightWorldSpaceDirection; 27 | uniform vec4 LightDiffuseColorAndIlluminance; 28 | uniform vec4 AtmosphericScatteringEnabled; 29 | uniform vec4 ViewPositionAndTime; 30 | uniform mat4 CloudShadowProj; 31 | uniform vec4 VolumeScatteringEnabled; 32 | uniform vec4 FogSkyBlend; 33 | uniform vec4 PointLightDiffuseFadeOutParameters; 34 | uniform vec4 MoonDir; 35 | uniform vec4 ClusterSize; 36 | uniform vec4 AtmosphericScattering; 37 | uniform vec4 FogAndDistanceControl; 38 | uniform vec4 RenderChunkFogAlpha; 39 | uniform vec4 DiffuseSpecularEmissiveAmbientTermToggles; 40 | uniform vec4 DirectionalLightToggleAndCountAndMaxDistance; 41 | uniform vec4 EmissiveMultiplierAndDesaturationAndCloudPCFAndContribution; 42 | uniform vec4 DirectionalShadowModeAndCloudShadowToggleAndPointLightToggleAndShadowToggle; 43 | uniform vec4 FogColor; 44 | uniform vec4 VolumeDimensions; 45 | uniform vec4 ShadowPCFWidth; 46 | uniform vec4 MoonColor; 47 | uniform vec4 ShadowParams; 48 | uniform vec4 SkyAmbientLightColorIntensity; 49 | uniform vec4 ClusterDimensions; 50 | uniform vec4 ClusterNearFarWidthHeight; 51 | uniform vec4 SunColor; 52 | uniform vec4 PointLightSpecularFadeOutParameters; 53 | uniform vec4 VolumeNearFar; 54 | uniform vec4 IBLParameters; 55 | uniform vec4 SkyZenithColor; 56 | uniform vec4 SkyHorizonColor; 57 | 58 | void main() { 59 | //StandardTemplate_VertSharedTransform 60 | vec3 worldPosition; 61 | #ifdef INSTANCING 62 | mat4 model; 63 | model[0] = vec4(i_data0.x, i_data1.x, i_data2.x, 0); 64 | model[1] = vec4(i_data0.y, i_data1.y, i_data2.y, 0); 65 | model[2] = vec4(i_data0.z, i_data1.z, i_data2.z, 0); 66 | model[3] = vec4(i_data0.w, i_data1.w, i_data2.w, 1); 67 | worldPosition = instMul(model, vec4(a_position, 1.0)).xyz; 68 | #else 69 | worldPosition = mul(u_model[0], vec4(a_position, 1.0)).xyz; 70 | #endif 71 | 72 | vec4 position;// = mul(u_viewProj, vec4(worldPosition, 1.0)); 73 | vec2 texcoord0 = a_texcoord0; 74 | vec4 color0 = a_color0; 75 | 76 | //StandardTemplate_InvokeVertexOverrideFunction 77 | position = jitterVertexPosition(worldPosition); 78 | 79 | vec4 fog; 80 | vec3 normal = vec3(0.0, 0.0, 0.0); 81 | vec3 tangent = vec3(0.0, 0.0, 0.0); 82 | vec3 bitangent = vec3(0.0, 0.0, 0.0); 83 | int pbrTextureId = 0; 84 | #if FORWARD_PBR_TRANSPARENT 85 | float cameraDepth = length(ViewPositionAndTime.xyz - worldPosition); 86 | // float fogIntensity = calculateFogIntensityFadedVanilla(cameraDepth, FogAndDistanceControl.z, FogAndDistanceControl.x, FogAndDistanceControl.y, RenderChunkFogAlpha.x); 87 | float fogIntensity = calculateFogIntensityFaded(cameraDepth, FogAndDistanceControl.z, FogAndDistanceControl.x, FogAndDistanceControl.y, RenderChunkFogAlpha.x); 88 | fog = vec4(FogColor.rgb, fogIntensity); 89 | 90 | pbrTextureId = int(a_texcoord4) & 0xffff; 91 | 92 | vec3 n = a_normal.xyz; 93 | vec3 t = a_tangent.xyz; 94 | vec3 b = cross(n, t) * a_tangent.w; 95 | 96 | normal = mul(u_model[0], vec4(n, 0.0)).xyz; 97 | tangent = mul(u_model[0], vec4(t, 0.0)).xyz; 98 | bitangent = mul(u_model[0], vec4(b, 0.0)).xyz; 99 | 100 | #else 101 | 102 | #endif 103 | 104 | //StandardTemplate_InvokeLightingVertexFunction 105 | vec2 lightmapUV = a_texcoord1; 106 | 107 | v_texcoord0 = texcoord0; 108 | v_color0 = color0; 109 | v_fog = fog; 110 | v_lightmapUV = lightmapUV; 111 | v_tangent = tangent; 112 | v_normal = normal; 113 | v_bitangent = bitangent; 114 | v_worldPos = worldPosition; 115 | v_skyAmbientContribution = 0.0; 116 | v_blockAmbientContribution = 0.0; 117 | 118 | #if defined(FORWARD_PBR_TRANSPARENT) 119 | v_pbrTextureId = pbrTextureId; 120 | #endif 121 | 122 | gl_Position = position; 123 | } 124 | -------------------------------------------------------------------------------- /materials/RenderChunkPrepass/src/RenderChunkPrepass.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_color0, v_fog, v_texcoord0, v_lightmapUV, v_normal, v_tangent, v_bitangent, v_worldPos, v_blockAmbientContribution, v_skyAmbientContribution 2 | #if defined(GEOMETRY_PREPASS) || defined(GEOMETRY_PREPASS_ALPHA_TEST) 3 | $input v_pbrTextureId 4 | #endif 5 | 6 | #include 7 | #include 8 | 9 | struct PBRTextureData { 10 | float colourToMaterialUvScale0; 11 | float colourToMaterialUvScale1; 12 | float colourToMaterialUvBias0; 13 | float colourToMaterialUvBias1; 14 | float colourToNormalUvScale0; 15 | float colourToNormalUvScale1; 16 | float colourToNormalUvBias0; 17 | float colourToNormalUvBias1; 18 | int flags; 19 | float uniformRoughness; 20 | float uniformEmissive; 21 | float uniformMetalness; 22 | float maxMipColour; 23 | float maxMipMer; 24 | float maxMipNormal; 25 | float pad; 26 | }; 27 | 28 | uniform vec4 LightWorldSpaceDirection; 29 | uniform vec4 GlobalRoughness; 30 | uniform vec4 FogColor; 31 | uniform vec4 FogAndDistanceControl; 32 | uniform vec4 LightDiffuseColorAndIlluminance; 33 | uniform vec4 ViewPositionAndTime; 34 | uniform vec4 RenderChunkFogAlpha; 35 | 36 | SAMPLER2D_AUTOREG(s_MatTexture); 37 | SAMPLER2D_AUTOREG(s_SeasonsTexture); 38 | SAMPLER2D_AUTOREG(s_LightMapTexture); 39 | 40 | #if defined(GEOMETRY_PREPASS) || defined(GEOMETRY_PREPASS_ALPHA_TEST) 41 | 42 | BUFFER_RO_AUTOREG(s_PBRData, PBRTextureData); 43 | 44 | vec2 octWrap(vec2 v) { 45 | return (1.0 - abs(v.yx)) * ((2.0 * step(0.0, v)) - 1.0); 46 | } 47 | 48 | vec2 ndirToOctSnorm(vec3 n) { 49 | vec2 p = n.xy * (1.0 / (abs(n.x) + abs(n.y) + abs(n.z))); 50 | p = (n.z < 0.0) ? octWrap(p) : p; 51 | return p; 52 | } 53 | 54 | vec2 ndirToOctUnorm(vec3 n) { 55 | vec2 p = ndirToOctSnorm(n); 56 | return p * 0.5f + 0.5f; 57 | } 58 | 59 | vec3 octToNdirSnorm(vec2 p) { 60 | vec3 n = vec3(p.xy, 1.0 - abs(p.x) - abs(p.y)); 61 | n.xy = (n.z < 0.0) ? octWrap(n.xy) : n.xy; 62 | return normalize(n); 63 | } 64 | 65 | vec3 octToNdirUnorm(vec2 p) { 66 | vec2 pSnorm = p * 2.0f - 1.0f; 67 | return octToNdirSnorm(pSnorm); 68 | } 69 | 70 | float saturatedLinearRemapZeroToOne(float value, float zeroValue, float oneValue) { 71 | return saturate((((value) * (1.f / (oneValue - zeroValue))) + -zeroValue / (oneValue - zeroValue))); 72 | } 73 | 74 | vec3 calculateTangentNormalFromHeightmap(sampler2D heightmapTexture, vec2 heightmapUV, float mipLevel) { 75 | vec3 tangentNormal = vec3(0.f, 0.f, 1.f); 76 | const float kHeightMapPixelEdgeWidth = 1.0f / 12.0f; 77 | const float kHeightMapDepth = 4.0f; 78 | const float kRecipHeightMapDepth = 1.0f / kHeightMapDepth; 79 | float fadeForLowerMips = saturatedLinearRemapZeroToOne(mipLevel, 2.f, 1.f); 80 | if (fadeForLowerMips > 0.f) { 81 | vec2 widthHeight = vec2(textureSize(heightmapTexture, 0)); 82 | vec2 pixelCoord = heightmapUV * widthHeight; 83 | { 84 | const float kNudgePixelCentreDistEpsilon = 0.0625f; 85 | const float kNudgeUvEpsilon = 0.25f / 65536.f; 86 | vec2 nudgeSampleCoord = fract(pixelCoord); 87 | if (abs(nudgeSampleCoord.x - 0.5) < kNudgePixelCentreDistEpsilon) { 88 | heightmapUV.x += (nudgeSampleCoord.x > 0.5f) ? kNudgeUvEpsilon : -kNudgeUvEpsilon; 89 | } 90 | if (abs(nudgeSampleCoord.y - 0.5) < kNudgePixelCentreDistEpsilon) { 91 | heightmapUV.y += (nudgeSampleCoord.y > 0.5f) ? kNudgeUvEpsilon : -kNudgeUvEpsilon; 92 | } 93 | } 94 | vec4 heightSamples = textureGather(heightmapTexture, heightmapUV, 0); 95 | vec2 subPixelCoord = fract(pixelCoord + 0.5f); 96 | const float kBevelMode = 0.0f; 97 | vec2 axisSamplePair = (subPixelCoord.y > 0.5f) ? heightSamples.xy : heightSamples.wz; 98 | float axisBevelCentreSampleCoord = subPixelCoord.x; 99 | axisBevelCentreSampleCoord += ((axisSamplePair.x > axisSamplePair.y) ? kHeightMapPixelEdgeWidth : -kHeightMapPixelEdgeWidth) * kBevelMode; 100 | ivec2 axisSampleIndices = ivec2(saturate(vec2(axisBevelCentreSampleCoord - kHeightMapPixelEdgeWidth, axisBevelCentreSampleCoord + kHeightMapPixelEdgeWidth) * 2.f)); 101 | tangentNormal.x = (axisSamplePair[axisSampleIndices.x] - axisSamplePair[axisSampleIndices.y]); 102 | axisSamplePair = (subPixelCoord.x > 0.5f) ? heightSamples.zy : heightSamples.wx; 103 | axisBevelCentreSampleCoord = subPixelCoord.y; 104 | axisBevelCentreSampleCoord += ((axisSamplePair.x > axisSamplePair.y) ? kHeightMapPixelEdgeWidth : -kHeightMapPixelEdgeWidth) * kBevelMode; 105 | axisSampleIndices = ivec2(saturate(vec2(axisBevelCentreSampleCoord - kHeightMapPixelEdgeWidth, axisBevelCentreSampleCoord + kHeightMapPixelEdgeWidth) * 2.f)); 106 | tangentNormal.y = (axisSamplePair[axisSampleIndices.x] - axisSamplePair[axisSampleIndices.y]); 107 | tangentNormal.z = kRecipHeightMapDepth; 108 | tangentNormal = normalize(tangentNormal); 109 | tangentNormal.xy *= fadeForLowerMips; 110 | } 111 | return tangentNormal; 112 | } 113 | 114 | vec2 getPBRDataUV(vec2 surfaceUV, vec2 uvScale, vec2 uvBias) { 115 | return (((surfaceUV) * (uvScale)) + uvBias); 116 | } 117 | 118 | float lumaPerceptual(vec3 color) { 119 | vec3 perceptualLuminance = vec3(0.299, 0.587, 0.114); 120 | return dot(perceptualLuminance, color); 121 | } 122 | 123 | #endif 124 | 125 | vec4 applySeasons(vec3 vertexColor, float vertexAlpha, vec4 diffuse) { 126 | vec2 uv = vertexColor.xy; 127 | diffuse.rgb *= mix(vec3(1.0, 1.0, 1.0), texture2D(s_SeasonsTexture, uv).rgb * 2.0, vertexColor.b); 128 | diffuse.rgb *= vec3_splat(vertexAlpha); 129 | diffuse.a = 1.0; 130 | return diffuse; 131 | } 132 | 133 | 134 | 135 | void main() { 136 | vec4 diffuse = texture2D(s_MatTexture, v_texcoord0); 137 | 138 | #if defined(ALPHA_TEST) || defined(GEOMETRY_PREPASS_ALPHA_TEST) || defined(DEPTH_ONLY) 139 | const float ALPHA_THRESHOLD = 0.5; 140 | if (diffuse.a < ALPHA_THRESHOLD) { 141 | discard; 142 | } 143 | #endif 144 | 145 | #if defined(SEASONS) && !defined(TRANSPARENT) && !defined(TRANSPARENT_PBR) 146 | diffuse = applySeasons(v_color0.rgb, v_color0.a, diffuse); 147 | #else 148 | diffuse.rgb *= v_color0.rgb; 149 | diffuse.a *= v_color0.a; 150 | #endif 151 | 152 | #if (defined(GEOMETRY_PREPASS) || defined(GEOMETRY_PREPASS_ALPHA_TEST)) && (BGFX_SHADER_LANGUAGE_GLSL >= 310 || BGFX_SHADER_LANGUAGE_HLSL >= 500 || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_SPIRV || BGFX_SHADER_LANGUAGE_METAL) 153 | //RenderChunkSurfGeometryPrepass 154 | //applyPBRValuesToSurfaceOutput 155 | PBRTextureData pbrTextureData = s_PBRData[v_pbrTextureId]; 156 | vec2 normalUVScale = vec2(pbrTextureData.colourToNormalUvScale0, pbrTextureData.colourToNormalUvScale1); 157 | vec2 normalUVBias = vec2(pbrTextureData.colourToNormalUvBias0, pbrTextureData.colourToNormalUvBias1); 158 | vec2 materialUVScale = vec2(pbrTextureData.colourToMaterialUvScale0, pbrTextureData.colourToMaterialUvScale1); 159 | vec2 materialUVBias = vec2(pbrTextureData.colourToMaterialUvBias0, pbrTextureData.colourToMaterialUvBias1); 160 | 161 | int kPBRTextureDataFlagHasMaterialTexture = (1 << 0); 162 | // These two are mutually exclusive 163 | int kPBRTextureDataFlagHasNormalTexture = (1 << 1); 164 | int kPBRTextureDataFlagHasHeightMapTexture = (1 << 2); 165 | 166 | vec3 tangentNormal = vec3(0, 0, 1); 167 | if ((pbrTextureData.flags & kPBRTextureDataFlagHasNormalTexture) == kPBRTextureDataFlagHasNormalTexture) { 168 | vec2 uv = getPBRDataUV(v_texcoord0, normalUVScale, normalUVBias); 169 | tangentNormal = texture2D(s_MatTexture, uv).xyz * 2.f - 1.f; 170 | } else if ((pbrTextureData.flags & kPBRTextureDataFlagHasHeightMapTexture) == kPBRTextureDataFlagHasHeightMapTexture) { 171 | vec2 normalUv = getPBRDataUV(v_texcoord0, normalUVScale, normalUVBias); 172 | float normalMipLevel = min(pbrTextureData.maxMipNormal - pbrTextureData.maxMipColour, pbrTextureData.maxMipNormal); 173 | tangentNormal = calculateTangentNormalFromHeightmap(s_MatTexture, normalUv, normalMipLevel); 174 | } 175 | 176 | float emissive = pbrTextureData.uniformEmissive; 177 | float metalness = pbrTextureData.uniformMetalness; 178 | float linearRoughness = pbrTextureData.uniformRoughness; 179 | if ((pbrTextureData.flags & kPBRTextureDataFlagHasMaterialTexture) == kPBRTextureDataFlagHasMaterialTexture) { 180 | vec2 uv = getPBRDataUV(v_texcoord0, materialUVScale, materialUVBias); 181 | vec3 texel = texture2D(s_MatTexture, uv).rgb; 182 | metalness = texel.r; 183 | emissive = texel.g; 184 | linearRoughness = texel.b; 185 | } 186 | 187 | mat3 tbn = mtxFromRows( 188 | normalize(v_tangent), 189 | normalize(v_bitangent), 190 | normalize(v_normal)); 191 | tbn = transpose(tbn); 192 | vec3 viewSpaceNormal = mul(tbn, tangentNormal).xyz; 193 | 194 | //computeLighting_RenderChunk_SplitLightMapValues 195 | vec2 newLightmapUV = min(v_lightmapUV.xy, vec2(1.0f, 1.0f)); 196 | vec3 blockLight = texture2D(s_LightMapTexture, min(vec2(newLightmapUV.x, 1.5f * 1.0f / 16.0f), vec2(1.0, 1.0))).rgb; 197 | vec3 skyLight = texture2D(s_LightMapTexture, min(vec2(newLightmapUV.y, 0.5f * 1.0f / 16.0f), vec2(1.0, 1.0))).rgb; 198 | 199 | //RenderChunkGeometryPrepass 200 | //applyPrepassSurfaceToGBuffer 201 | gl_FragData[0].rgb = diffuse.rgb; 202 | gl_FragData[0].a = metalness; 203 | 204 | vec3 viewNormal = normalize(viewSpaceNormal).xyz; 205 | gl_FragData[1].xy = ndirToOctSnorm(viewNormal); 206 | 207 | vec3 worldPosition = v_worldPos; 208 | vec3 prevWorldPosition = v_worldPos - u_prevWorldPosOffset.xyz; 209 | 210 | vec4 screenSpacePos = mul(u_viewProj, vec4(worldPosition, 1.0)); 211 | screenSpacePos /= screenSpacePos.w; 212 | screenSpacePos = screenSpacePos * 0.5 + 0.5; 213 | 214 | vec4 prevScreenSpacePos = mul(u_prevViewProj, vec4(prevWorldPosition, 1.0)); 215 | prevScreenSpacePos /= prevScreenSpacePos.w; 216 | prevScreenSpacePos = prevScreenSpacePos * 0.5 + 0.5; 217 | 218 | gl_FragData[1].zw = screenSpacePos.xy - prevScreenSpacePos.xy; 219 | 220 | gl_FragData[2] = vec4( 221 | emissive, 222 | lumaPerceptual(blockLight), 223 | lumaPerceptual(skyLight), 224 | linearRoughness); 225 | 226 | #else 227 | 228 | #if defined(DEPTH_ONLY) || defined(DEPTH_ONLY_OPAQUE) 229 | diffuse = vec4(1.0, 1.0, 1.0, 1.0); 230 | #endif 231 | 232 | #if defined(TRANSPARENT_PBR) 233 | //computeLighting_RenderChunk_Split 234 | vec3 blockLight = texture2D(s_LightMapTexture, min(vec2(v_lightmapUV.x, 0.09375), 1.0)).xyz; 235 | vec3 skyLight = texture2D(s_LightMapTexture, min(vec2(v_lightmapUV.y, 0.03125), 1.0)).xyz; 236 | diffuse.rgb *= saturate(blockLight + skyLight); 237 | #endif 238 | 239 | gl_FragData[0].rgb = mix(diffuse.rgb, FogColor.rgb, v_fog.a); 240 | gl_FragData[0].a = diffuse.a; 241 | gl_FragData[1] = vec4(0.0, 0.0, 0.0, 0.0); 242 | gl_FragData[2] = vec4(0.0, 0.0, 0.0, 0.0); 243 | 244 | #endif 245 | } 246 | -------------------------------------------------------------------------------- /materials/RenderChunkPrepass/src/RenderChunkPrepass.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec4 a_color0 : COLOR0; 2 | vec4 a_normal : NORMAL; 3 | vec4 a_tangent : TANGENT; 4 | vec3 a_position : POSITION; 5 | vec2 a_texcoord0 : TEXCOORD0; 6 | vec2 a_texcoord1 : TEXCOORD1; 7 | #if BGFX_SHADER_LANGUAGE_HLSL 8 | int a_texcoord4 : TEXCOORD4; 9 | #else 10 | float a_texcoord4 : TEXCOORD4; 11 | #endif 12 | 13 | vec4 i_data0 : TEXCOORD7; 14 | vec4 i_data1 : TEXCOORD6; 15 | vec4 i_data2 : TEXCOORD5; 16 | 17 | vec4 v_color0 : COLOR0; 18 | vec4 v_fog : COLOR2; 19 | vec3 v_normal : NORMAL; 20 | vec3 v_tangent : TANGENT; 21 | vec3 v_bitangent : BITANGENT; 22 | centroid vec2 v_texcoord0 : TEXCOORD0; 23 | vec2 v_lightmapUV : TEXCOORD1; 24 | vec3 v_worldPos : TEXCOORD3; 25 | flat int v_pbrTextureId : TEXCOORD4; 26 | vec3 v_blockAmbientContribution : TEXCOORD5; 27 | vec3 v_skyAmbientContribution : TEXCOORD6; 28 | -------------------------------------------------------------------------------- /materials/RenderChunkPrepass/src/RenderChunkPrepass.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_color0, a_position, a_texcoord0, a_texcoord1, a_normal, a_tangent 2 | #if defined(GEOMETRY_PREPASS) || defined(GEOMETRY_PREPASS_ALPHA_TEST) 3 | $input a_texcoord4 4 | #endif 5 | #ifdef INSTANCING 6 | $input i_data0, i_data1, i_data2 7 | #endif 8 | 9 | $output v_color0, v_fog, v_normal, v_tangent, v_bitangent, v_texcoord0, v_lightmapUV, v_worldPos, v_blockAmbientContribution, v_skyAmbientContribution 10 | #if defined(GEOMETRY_PREPASS) || defined(GEOMETRY_PREPASS_ALPHA_TEST) 11 | $output v_pbrTextureId 12 | #endif 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | uniform vec4 RenderChunkFogAlpha; 19 | uniform vec4 FogAndDistanceControl; 20 | uniform vec4 ViewPositionAndTime; 21 | uniform vec4 FogColor; 22 | uniform vec4 GlobalRoughness; 23 | uniform vec4 LightWorldSpaceDirection; 24 | uniform vec4 LightDiffuseColorAndIlluminance; 25 | 26 | void transformAsBillboardVertex(inout vec3 worldPos, vec4 color0, out vec4 position) { 27 | worldPos += vec3(0.5, 0.5, 0.5); 28 | vec3 forward = normalize(worldPos - ViewPositionAndTime.xyz); 29 | vec3 right = normalize(cross(vec3(0.0, 1.0, 0.0), forward)); 30 | vec3 up = cross(forward, right); 31 | vec3 offsets = color0.xyz; 32 | worldPos -= up * (offsets.z - 0.5) + right * (offsets.x - 0.5); 33 | position = mul(u_viewProj, vec4(worldPos, 1.0)); 34 | } 35 | 36 | void main() { 37 | //StandardTemplate_VertSharedTransform 38 | vec3 worldPosition; 39 | #ifdef INSTANCING 40 | mat4 model; 41 | model[0] = vec4(i_data0.x, i_data1.x, i_data2.x, 0); 42 | model[1] = vec4(i_data0.y, i_data1.y, i_data2.y, 0); 43 | model[2] = vec4(i_data0.z, i_data1.z, i_data2.z, 0); 44 | model[3] = vec4(i_data0.w, i_data1.w, i_data2.w, 1); 45 | worldPosition = instMul(model, vec4(a_position, 1.0)).xyz; 46 | #else 47 | worldPosition = mul(u_model[0], vec4(a_position, 1.0)).xyz; 48 | #endif 49 | 50 | vec4 position;// = mul(u_viewProj, vec4(worldPosition, 1.0)); 51 | vec2 texcoord0 = a_texcoord0; 52 | vec4 color0 = a_color0; 53 | 54 | //StandardTemplate_InvokeVertexOverrideFunction 55 | position = jitterVertexPosition(worldPosition); 56 | 57 | vec4 fog; 58 | vec3 normal = vec3(0.0, 0.0, 0.0); 59 | vec3 tangent = vec3(0.0, 0.0, 0.0); 60 | vec3 bitangent = vec3(0.0, 0.0, 0.0); 61 | int pbrTextureId = 0; 62 | #if defined(GEOMETRY_PREPASS) || defined(GEOMETRY_PREPASS_ALPHA_TEST) 63 | //RenderChunkPrepassVert 64 | //applyPBRValuesToVertexOutput 65 | float cameraDepth = length(ViewPositionAndTime.xyz - worldPosition); 66 | // float fogIntensity = calculateFogIntensityFadedVanilla(cameraDepth, FogAndDistanceControl.z, FogAndDistanceControl.x, FogAndDistanceControl.y, RenderChunkFogAlpha.x); 67 | float fogIntensity = calculateFogIntensityFaded(cameraDepth, FogAndDistanceControl.z, FogAndDistanceControl.x, FogAndDistanceControl.y, RenderChunkFogAlpha.x); 68 | fog = vec4(FogColor.rgb, fogIntensity); 69 | 70 | pbrTextureId = int(a_texcoord4) & 0xffff; 71 | 72 | vec3 n = a_normal.xyz; 73 | vec3 t = a_tangent.xyz; 74 | vec3 b = cross(n, t) * a_tangent.w; 75 | 76 | normal = mul(u_model[0], vec4(n, 0.0)).xyz; 77 | tangent = mul(u_model[0], vec4(t, 0.0)).xyz; 78 | bitangent = mul(u_model[0], vec4(b, 0.0)).xyz; 79 | 80 | #else 81 | //RenderChunkVert 82 | #ifdef RENDER_AS_BILLBOARDS 83 | color0 = vec4(1.0, 1.0, 1.0, 1.0); 84 | transformAsBillboardVertex(worldPosition, a_color0, position); 85 | #endif 86 | 87 | float cameraDepth = length(ViewPositionAndTime.xyz - worldPosition); 88 | // float fogIntensity = calculateFogIntensityFadedVanilla(cameraDepth, FogAndDistanceControl.z, FogAndDistanceControl.x, FogAndDistanceControl.y, RenderChunkFogAlpha.x); 89 | float fogIntensity = calculateFogIntensityFaded(cameraDepth, FogAndDistanceControl.z, FogAndDistanceControl.x, FogAndDistanceControl.y, RenderChunkFogAlpha.x); 90 | fog = vec4(FogColor.rgb, fogIntensity); 91 | 92 | #if defined(TRANSPARENT) || defined(TRANSPARENT_PBR) 93 | //RenderChunkVertTransparent 94 | bool shouldBecomeOpaqueInTheDistance = a_color0.a < 0.95; 95 | if (shouldBecomeOpaqueInTheDistance) { 96 | float cameraDistance = cameraDepth / FogAndDistanceControl.w; 97 | float alphaFadeOut = clamp(cameraDistance, 0.0, 1.0); 98 | color0.a = mix(a_color0.a, 1.0, alphaFadeOut); 99 | } 100 | #endif 101 | 102 | #endif 103 | 104 | //StandardTemplate_InvokeLightingVertexFunction 105 | vec2 lightmapUV = a_texcoord1; 106 | 107 | v_texcoord0 = texcoord0; 108 | v_color0 = color0; 109 | v_fog = fog; 110 | v_lightmapUV = lightmapUV; 111 | v_tangent = tangent; 112 | v_normal = normal; 113 | v_bitangent = bitangent; 114 | v_worldPos = worldPosition; 115 | v_skyAmbientContribution = vec3(0, 0, 0); 116 | v_blockAmbientContribution = vec3(0, 0, 0); 117 | 118 | #if defined(GEOMETRY_PREPASS) || defined(GEOMETRY_PREPASS_ALPHA_TEST) 119 | v_pbrTextureId = pbrTextureId; 120 | #endif 121 | 122 | gl_Position = position; 123 | } 124 | -------------------------------------------------------------------------------- /materials/ScreenBlit/src/ScreenBlit.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_texcoord0 2 | 3 | #include 4 | 5 | SAMPLER2D_AUTOREG(s_MatTexture); 6 | 7 | void main() { 8 | gl_FragColor = texture2D(s_MatTexture, v_texcoord0); 9 | } 10 | -------------------------------------------------------------------------------- /materials/ScreenBlit/src/ScreenBlit.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec3 a_position : POSITION; 2 | vec2 a_texcoord0 : TEXCOORD0; 3 | 4 | vec2 v_texcoord0 : TEXCOORD0; 5 | -------------------------------------------------------------------------------- /materials/ScreenBlit/src/ScreenBlit.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_texcoord0 2 | $output v_texcoord0 3 | 4 | #include 5 | 6 | void main() { 7 | vec2 pos = (a_position.xy * 2.0) - 1.0; 8 | v_texcoord0 = a_texcoord0; 9 | gl_Position = vec4(pos, 0.0, 1.0); 10 | } 11 | -------------------------------------------------------------------------------- /materials/Sky/src/Sky.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_color0 2 | #if defined(GEOMETRY_PREPASS) 3 | $input v_texcoord0, v_normal, v_worldPos, v_prevWorldPos 4 | #endif 5 | 6 | #include 7 | 8 | vec2 octWrap(vec2 v) { 9 | return (1.0 - abs(v.yx)) * ((2.0 * step(0.0, v)) - 1.0); 10 | } 11 | 12 | vec2 ndirToOctSnorm(vec3 n) { 13 | vec2 p = n.xy * (1.0 / (abs(n.x) + abs(n.y) + abs(n.z))); 14 | p = (n.z < 0.0) ? octWrap(p) : p; 15 | return p; 16 | } 17 | 18 | void main() { 19 | #if defined(OPAQUE) 20 | //Opaque 21 | gl_FragColor = v_color0; 22 | 23 | #elif defined(GEOMETRY_PREPASS) && (BGFX_SHADER_LANGUAGE_GLSL >= 310 || BGFX_SHADER_LANGUAGE_HLSL >= 500 || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_SPIRV || BGFX_SHADER_LANGUAGE_METAL) 24 | //GeometryPrepass 25 | vec3 viewSpaceNormal = vec3(0, 1, 0); 26 | vec3 viewNormal = normalize(viewSpaceNormal).xyz; 27 | 28 | vec4 screenSpacePos = mul(u_viewProj, vec4(v_worldPos, 1.0)); 29 | screenSpacePos /= screenSpacePos.w; 30 | screenSpacePos = screenSpacePos * 0.5 + 0.5; 31 | 32 | vec4 prevScreenSpacePos = mul(u_prevViewProj, vec4(v_worldPos.xyz - u_prevWorldPosOffset.xyz, 1.0)); 33 | prevScreenSpacePos /= prevScreenSpacePos.w; 34 | prevScreenSpacePos = prevScreenSpacePos * 0.5 + 0.5; 35 | 36 | gl_FragData[0].rgb = v_color0.rgb; 37 | gl_FragData[0].a = 0.0; 38 | 39 | gl_FragData[1].xy = ndirToOctSnorm(viewNormal); 40 | gl_FragData[1].zw = screenSpacePos.xy - prevScreenSpacePos.xy; 41 | 42 | gl_FragData[2] = vec4(1.0, 0.0, 0.0, 0.5); 43 | 44 | #else 45 | //Fallback 46 | gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); 47 | 48 | #endif 49 | } -------------------------------------------------------------------------------- /materials/Sky/src/Sky.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec4 a_color0 : COLOR0; 2 | vec3 a_position : POSITION; 3 | vec2 a_texcoord0 : TEXCOORD0; 4 | 5 | vec4 i_data0 : TEXCOORD7; 6 | vec4 i_data1 : TEXCOORD6; 7 | vec4 i_data2 : TEXCOORD5; 8 | 9 | vec4 v_color0 : COLOR0; 10 | vec2 v_texcoord0 : TEXCOORD0; 11 | vec3 v_normal : NORMAL; 12 | vec3 v_worldPos : TEXCOORD3; 13 | vec3 v_prevWorldPos : TEXCOORD4; -------------------------------------------------------------------------------- /materials/Sky/src/Sky.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_color0, a_position 2 | #ifdef GEOMETRY_PREPASS 3 | $input a_texcoord0 4 | #ifdef INSTANCING 5 | $input i_data0, i_data1, i_data2 6 | #endif 7 | #endif 8 | 9 | $output v_color0 10 | #ifdef GEOMETRY_PREPASS 11 | $output v_texcoord0, v_normal, v_worldPos, v_prevWorldPos 12 | #endif 13 | 14 | #include 15 | 16 | uniform vec4 SkyColor; 17 | uniform vec4 FogColor; 18 | 19 | void main() { 20 | #if defined(OPAQUE) 21 | //Opaque 22 | v_color0 = mix(SkyColor, FogColor, a_color0.x); 23 | gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0)); 24 | 25 | #elif defined(GEOMETRY_PREPASS) && (BGFX_SHADER_LANGUAGE_GLSL >= 310 || BGFX_SHADER_LANGUAGE_HLSL >= 500 || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_SPIRV || BGFX_SHADER_LANGUAGE_METAL) 26 | //GeometryPrepass 27 | mat4 model; 28 | #ifdef INSTANCING 29 | model = mtxFromCols(i_data0, i_data1, i_data2, vec4(0, 0, 0, 1)); 30 | #else 31 | model = u_model[0]; 32 | #endif 33 | 34 | v_normal = vec3(0.0, 0.0, 0.0); 35 | v_texcoord0 = a_texcoord0; 36 | v_worldPos = mul(model, vec4(a_position, 1.0)).xyz; 37 | v_prevWorldPos = mul(u_model[0], vec4(a_position, 1.0)).xyz; 38 | v_color0 = mix(SkyColor, FogColor, a_color0.x); 39 | gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0)); 40 | 41 | #else 42 | //Fallback 43 | v_color0 = vec4(0.0, 0.0, 0.0, 0.0); 44 | gl_Position = vec4(0.0, 0.0, 0.0, 0.0); 45 | 46 | #endif 47 | } -------------------------------------------------------------------------------- /materials/Stars/src/Stars.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_color0 2 | 3 | #include 4 | 5 | uniform vec4 StarsColor; 6 | 7 | void main() { 8 | vec4 starColor; 9 | starColor.a = v_color0.a; 10 | starColor.rgb = v_color0.rgb * StarsColor.rgb * v_color0.a; 11 | gl_FragColor = starColor; 12 | } -------------------------------------------------------------------------------- /materials/Stars/src/Stars.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec4 a_color0 : COLOR0; 2 | vec3 a_position : POSITION; 3 | 4 | vec4 v_color0 : COLOR0; 5 | -------------------------------------------------------------------------------- /materials/Stars/src/Stars.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_color0, a_position 2 | $output v_color0 3 | 4 | #include 5 | 6 | void main() { 7 | v_color0 = a_color0; 8 | gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0)); 9 | } -------------------------------------------------------------------------------- /materials/StarsForwardPBR/src/StarsForwardPBR.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_color0, v_ndcPosition 2 | 3 | #include 4 | 5 | uniform mat4 PointLightProj; 6 | uniform vec4 ShadowBias; 7 | uniform vec4 PointLightShadowParams1; 8 | uniform vec4 ShadowSlopeBias; 9 | uniform vec4 BlockBaseAmbientLightColorIntensity; 10 | uniform vec4 PrepassUVOffset; 11 | uniform vec4 CascadeShadowResolutions; 12 | uniform vec4 DirectionalShadowModeAndCloudShadowToggleAndPointLightToggleAndShadowToggle; 13 | uniform vec4 VolumeScatteringEnabled; 14 | uniform vec4 DiffuseSpecularEmissiveAmbientTermToggles; 15 | uniform vec4 EmissiveMultiplierAndDesaturationAndCloudPCFAndContribution; 16 | uniform vec4 DirectionalLightToggleAndCountAndMaxDistance; 17 | uniform vec4 ShadowPCFWidth; 18 | uniform vec4 VolumeDimensions; 19 | uniform vec4 ShadowParams; 20 | uniform vec4 SkyAmbientLightColorIntensity; 21 | uniform vec4 ClusterDimensions; 22 | uniform vec4 ClusterNearFarWidthHeight; 23 | uniform vec4 ClusterSize; 24 | uniform vec4 PointLightDiffuseFadeOutParameters; 25 | uniform vec4 PointLightSpecularFadeOutParameters; 26 | uniform vec4 VolumeNearFar; 27 | uniform vec4 StarsColor; 28 | uniform mat4 CloudShadowProj; 29 | 30 | SAMPLER2DARRAY_AUTOREG(s_ScatteringBuffer); 31 | 32 | float linearToLogDepth(float linearDepth) { 33 | return log((exp(4.0) - 1.0) * linearDepth + 1.0) / 4.0; 34 | } 35 | 36 | vec3 ndcToVolume(vec3 ndc, mat4 inverseProj, vec2 nearFar) { 37 | vec2 uv = 0.5 * (ndc.xy + vec2(1.0, 1.0)); 38 | vec4 view = mul(inverseProj, vec4(ndc, 1.0)); 39 | float viewDepth = -view.z / view.w; 40 | float wLinear = (viewDepth - nearFar.x) / (nearFar.y - nearFar.x); 41 | return vec3(uv, linearToLogDepth(wLinear)); 42 | } 43 | 44 | vec4 sampleVolume(highp sampler2DArray volume, ivec3 dimensions, vec3 uvw) { 45 | float depth = uvw.z * float(dimensions.z) - 0.5; 46 | int index = clamp(int(depth), 0, dimensions.z - 2); 47 | float offset = clamp(depth - float(index), 0.0, 1.0); 48 | vec4 a = texture2DArray(volume, vec3(uvw.xy, index)).rgba; 49 | vec4 b = texture2DArray(volume, vec3(uvw.xy, index + 1)).rgba; 50 | return mix(a, b, offset); 51 | } 52 | 53 | void main() { 54 | #if FALLBACK 55 | gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); 56 | #else 57 | vec3 starColor = v_color0.rgb; 58 | starColor.rgb *= StarsColor.rgb * v_color0.a; 59 | 60 | vec3 outColor; 61 | if (VolumeScatteringEnabled.x != 0.0) { 62 | vec3 uvw = ndcToVolume(v_ndcPosition, u_invProj, VolumeNearFar.xy); 63 | vec4 sourceExtinction = sampleVolume(s_ScatteringBuffer, ivec3(VolumeDimensions.xyz), uvw); 64 | outColor = sourceExtinction.a * starColor.rgb; 65 | } else { 66 | outColor = starColor.rgb; 67 | } 68 | 69 | gl_FragColor = vec4(outColor.r, outColor.g, outColor.b, v_color0.a); 70 | #endif 71 | } 72 | -------------------------------------------------------------------------------- /materials/StarsForwardPBR/src/StarsForwardPBR.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec3 a_position : POSITION; 2 | vec4 a_color0 : COLOR0; 3 | 4 | vec4 v_color0 : COLOR0; 5 | vec3 v_ndcPosition : COLOR1; 6 | -------------------------------------------------------------------------------- /materials/StarsForwardPBR/src/StarsForwardPBR.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_color0 2 | $output v_color0, v_ndcPosition 3 | 4 | #include 5 | 6 | void main() { 7 | #if FALLBACK 8 | gl_Position = vec4(0.0, 0.0, 0.0, 0.0); 9 | #else 10 | vec4 clipPosition = mul(u_modelViewProj, vec4(a_position, 1.0)); 11 | vec3 ndcPosition = clipPosition.xyz / clipPosition.w; 12 | 13 | v_ndcPosition = ndcPosition; 14 | v_color0 = a_color0; 15 | gl_Position = clipPosition; 16 | #endif 17 | } 18 | -------------------------------------------------------------------------------- /materials/SunMoon/src/SunMoon.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_texcoord0 2 | 3 | #include 4 | 5 | uniform vec4 SunMoonColor; 6 | 7 | SAMPLER2D_AUTOREG(s_SunMoonTexture); 8 | 9 | void main() { 10 | gl_FragColor = SunMoonColor * texture2D(s_SunMoonTexture, v_texcoord0); 11 | } -------------------------------------------------------------------------------- /materials/SunMoon/src/SunMoon.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec3 a_position : POSITION; 2 | vec2 a_texcoord0 : TEXCOORD0; 3 | 4 | vec2 v_texcoord0 : TEXCOORD0; -------------------------------------------------------------------------------- /materials/SunMoon/src/SunMoon.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_texcoord0 2 | $output v_texcoord0 3 | 4 | #include 5 | 6 | void main() { 7 | v_texcoord0 = a_texcoord0; 8 | gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0)); 9 | } -------------------------------------------------------------------------------- /materials/SunMoonForwardPBR/src/SunMoonForwardPBR.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_ndcPosition, v_texcoord0 2 | 3 | #include 4 | 5 | uniform mat4 PointLightProj; 6 | uniform vec4 ShadowBias; 7 | uniform vec4 PointLightShadowParams1; 8 | uniform vec4 ShadowSlopeBias; 9 | uniform vec4 BlockBaseAmbientLightColorIntensity; 10 | uniform vec4 PrepassUVOffset; 11 | uniform vec4 CascadeShadowResolutions; 12 | uniform vec4 DirectionalShadowModeAndCloudShadowToggleAndPointLightToggleAndShadowToggle; 13 | uniform vec4 VolumeScatteringEnabled; 14 | uniform vec4 DiffuseSpecularEmissiveAmbientTermToggles; 15 | uniform vec4 EmissiveMultiplierAndDesaturationAndCloudPCFAndContribution; 16 | uniform vec4 DirectionalLightToggleAndCountAndMaxDistance; 17 | uniform vec4 ShadowPCFWidth; 18 | uniform vec4 VolumeDimensions; 19 | uniform vec4 ShadowParams; 20 | uniform vec4 SkyAmbientLightColorIntensity; 21 | uniform vec4 ClusterDimensions; 22 | uniform vec4 ClusterNearFarWidthHeight; 23 | uniform vec4 ClusterSize; 24 | uniform vec4 PointLightDiffuseFadeOutParameters; 25 | uniform vec4 PointLightSpecularFadeOutParameters; 26 | uniform vec4 VolumeNearFar; 27 | uniform vec4 SunMoonColor; 28 | uniform mat4 CloudShadowProj; 29 | 30 | SAMPLER2D_AUTOREG(s_SunMoonTexture); 31 | SAMPLER2DARRAY_AUTOREG(s_ScatteringBuffer); 32 | 33 | float linearToLogDepth(float linearDepth) { 34 | return log((exp(4.0) - 1.0) * linearDepth + 1.0) / 4.0; 35 | } 36 | 37 | vec3 ndcToVolume(vec3 ndc, mat4 inverseProj, vec2 nearFar) { 38 | vec2 uv = 0.5 * (ndc.xy + vec2(1.0, 1.0)); 39 | vec4 view = mul(inverseProj, vec4(ndc, 1.0)); 40 | float viewDepth = -view.z / view.w; 41 | float wLinear = (viewDepth - nearFar.x) / (nearFar.y - nearFar.x); 42 | return vec3(uv, linearToLogDepth(wLinear)); 43 | } 44 | 45 | vec4 sampleVolume(highp sampler2DArray volume, ivec3 dimensions, vec3 uvw) { 46 | float depth = uvw.z * float(dimensions.z) - 0.5; 47 | int index = clamp(int(depth), 0, dimensions.z - 2); 48 | float offset = clamp(depth - float(index), 0.0, 1.0); 49 | vec4 a = texture2DArray(volume, vec3(uvw.xy, index)).rgba; 50 | vec4 b = texture2DArray(volume, vec3(uvw.xy, index + 1)).rgba; 51 | return mix(a, b, offset); 52 | } 53 | 54 | void main() { 55 | #if FALLBACK 56 | gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); 57 | #else 58 | vec4 spriteColor = texture2D(s_SunMoonTexture, v_texcoord0); 59 | vec3 sunMoonColor = SunMoonColor.rgb * spriteColor.rgb * SunMoonColor.a; 60 | vec3 outColor; 61 | if (VolumeScatteringEnabled.x != 0.0) { 62 | vec3 uvw = ndcToVolume(v_ndcPosition, u_invProj, VolumeNearFar.xy); 63 | vec4 sourceExtinction = sampleVolume(s_ScatteringBuffer, ivec3(VolumeDimensions.xyz), uvw); 64 | outColor = sourceExtinction.a * sunMoonColor; 65 | } else { 66 | outColor = sunMoonColor; 67 | } 68 | 69 | gl_FragColor = vec4(outColor.r, outColor.g, outColor.b, 1.0); 70 | #endif 71 | } 72 | -------------------------------------------------------------------------------- /materials/SunMoonForwardPBR/src/SunMoonForwardPBR.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec3 a_position : POSITION; 2 | vec2 a_texcoord0 : TEXCOORD0; 3 | 4 | vec3 v_ndcPosition : COLOR1; 5 | vec2 v_texcoord0 : TEXCOORD0; -------------------------------------------------------------------------------- /materials/SunMoonForwardPBR/src/SunMoonForwardPBR.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_texcoord0 2 | $output v_ndcPosition, v_texcoord0 3 | 4 | #include 5 | 6 | void main() { 7 | #if FALLBACK 8 | v_texcoord0 = vec2(0.0, 0.0); 9 | v_ndcPosition = vec3(0.0, 0.0, 0.0); 10 | gl_Position = vec4(0.0, 0.0, 0.0, 0.0); 11 | #else 12 | vec4 clipPosition = mul(u_modelViewProj, vec4(a_position, 1.0)); 13 | vec3 ndcPosition = clipPosition.xyz / clipPosition.w; 14 | 15 | v_ndcPosition = ndcPosition; 16 | v_texcoord0 = a_texcoord0; 17 | gl_Position = clipPosition; 18 | #endif 19 | } 20 | -------------------------------------------------------------------------------- /materials/ToneMapping/src/ToneMapping.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_texcoord0 2 | 3 | #include 4 | 5 | uniform vec4 ExposureCompensation; 6 | uniform vec4 LuminanceMinMax; 7 | uniform vec4 RenderMode; 8 | uniform vec4 ScreenSize; 9 | uniform vec4 TonemapCorrection; 10 | uniform vec4 TonemapParams0; 11 | 12 | SAMPLER2D_AUTOREG(s_RasterColor); 13 | SAMPLER2D_AUTOREG(s_ColorTexture); 14 | SAMPLER2D_AUTOREG(s_AverageLuminance); 15 | SAMPLER2D_AUTOREG(s_MaxLuminance); 16 | SAMPLER2D_AUTOREG(s_RasterizedColor); 17 | SAMPLER2D_AUTOREG(s_CustomExposureCompensation); 18 | 19 | vec3 color_gamma(vec3 clr) { 20 | float e = 1.0 / 2.2; 21 | return pow(max(clr, vec3(0.0, 0.0, 0.0)), vec3(e, e, e)); 22 | } 23 | 24 | vec4 color_gamma(vec4 clr) { 25 | return vec4(color_gamma(clr.rgb), clr.a); 26 | } 27 | 28 | float luminance(vec3 clr) { 29 | return dot(clr, vec3(0.2126, 0.7152, 0.0722)); 30 | } 31 | 32 | float luminanceToEV100(float luminance) { 33 | return log2(luminance) + 3.0f; 34 | } 35 | 36 | vec3 TonemapReinhard(vec3 rgb, float W) { 37 | vec3 color = rgb / (1.0 + rgb); 38 | return color; 39 | } 40 | 41 | vec3 TonemapReinhardLuminance(vec3 rgb, float W) { 42 | float l_old = luminance(rgb); 43 | float l_new = (l_old * (1.0 + (l_old / W))) / (1.0 + l_old); 44 | return rgb * (l_new / l_old); 45 | } 46 | 47 | vec3 TonemapReinhardJodie(vec3 rgb) { 48 | float l = luminance(rgb); 49 | vec3 tc = rgb / (1.0 + rgb); 50 | return mix(rgb / (1.0 + l), tc, tc); 51 | } 52 | 53 | vec3 Uncharted2Tonemap(vec3 x) { 54 | float A = 0.15; 55 | float B = 0.50; 56 | float C = 0.10; 57 | float D = 0.20; 58 | float E = 0.02; 59 | float F = 0.30; 60 | return ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F; 61 | } 62 | 63 | vec3 TonemapUncharted2(vec3 rgb, float W) { 64 | const float ExposureBias = 2.0; 65 | vec3 curr = Uncharted2Tonemap(ExposureBias * rgb); 66 | vec3 whiteScale = 1.0 / Uncharted2Tonemap(vec3_splat(W)); 67 | return curr * whiteScale; 68 | } 69 | 70 | vec3 RRTAndODTFit(vec3 v) { 71 | vec3 a = v * (v + 0.0245786) - 0.000090537; 72 | vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081; 73 | return a / b; 74 | } 75 | 76 | vec3 ACESFitted(vec3 rgb) { 77 | const mat3 ACESInputMat = mat3( 78 | 0.59719, 0.35458, 0.04823, 79 | 0.07600, 0.90834, 0.01566, 80 | 0.02840, 0.13383, 0.83777 81 | ); 82 | const mat3 ACESOutputMat = mat3( 83 | 1.60475, -0.53108, -0.07367, 84 | -0.10208, 1.10813, -0.00605, 85 | -0.00327, -0.07276, 1.07602 86 | ); 87 | rgb = mul(ACESInputMat, rgb); 88 | rgb = RRTAndODTFit(rgb); 89 | rgb = mul(ACESOutputMat, rgb); 90 | rgb = clamp(rgb, 0.0, 1.0); 91 | return rgb; 92 | } 93 | 94 | vec3 TonemapACES(vec3 rgb) { 95 | return ACESFitted(rgb); 96 | } 97 | 98 | vec3 TonemapColorCorrection(vec3 rgb, float luminance, float brightness, float contrast, float saturation) { 99 | rgb = (rgb - 0.5) * contrast + 0.5 + brightness; 100 | return mix(vec3_splat(luminance), rgb, max(0.0, saturation)); 101 | } 102 | 103 | vec3 ApplyTonemap(vec3 sceneColor, float averageLuminance, float brightness, float contrast, float saturation, float compensation, float whitePoint, int tonemapper) { 104 | float exposure = (0.18f / averageLuminance) * compensation; 105 | sceneColor *= exposure; 106 | float scaledWhitePoint = exposure * whitePoint; 107 | float whitePointSquared = scaledWhitePoint * scaledWhitePoint; 108 | if (tonemapper == 1) { 109 | sceneColor = TonemapReinhardLuminance(sceneColor, whitePointSquared); 110 | } else if (tonemapper == 2) { 111 | sceneColor = TonemapReinhardJodie(sceneColor); 112 | } else if (tonemapper == 3) { 113 | sceneColor = TonemapUncharted2(sceneColor, whitePointSquared); 114 | } else if (tonemapper == 4) { 115 | sceneColor = TonemapACES(sceneColor); 116 | } else { 117 | sceneColor = TonemapReinhard(sceneColor, whitePointSquared); 118 | } 119 | float finalLuminance = luminance(sceneColor); 120 | sceneColor = color_gamma(sceneColor); 121 | return TonemapColorCorrection(sceneColor, finalLuminance, brightness, contrast, saturation); 122 | } 123 | 124 | void main() { 125 | vec3 sceneColor = texture2D(s_ColorTexture, v_texcoord0.xy).rgb; 126 | vec3 finalColor = sceneColor; 127 | if (TonemapParams0.y <= 0.5f) { 128 | finalColor.rgb = color_gamma(sceneColor.rgb); 129 | } else { 130 | float averageLuminance = clamp(texture2D(s_AverageLuminance, vec2(0.5f, 0.5f)).r, LuminanceMinMax.x, LuminanceMinMax.y); 131 | float compensation = ExposureCompensation.y; 132 | int exposureCurveType = int(ExposureCompensation.x); 133 | if (exposureCurveType > 0 && exposureCurveType < 2) { 134 | compensation = 1.03f - 2.0f / ((1.0f / log(10.0f)) * log(averageLuminance + 1.0f) + 2.0f); 135 | } else if (exposureCurveType > 1) { 136 | vec2 uv = vec2(LuminanceMinMax.x == LuminanceMinMax.y ? 0.5f : (luminanceToEV100(averageLuminance) - luminanceToEV100(LuminanceMinMax.x)) / (luminanceToEV100(LuminanceMinMax.y) - luminanceToEV100(LuminanceMinMax.x)), 0.5f); 137 | compensation = texture2D(s_CustomExposureCompensation, uv).r; 138 | } 139 | float whitePoint = texture2D(s_MaxLuminance, vec2(0.5f, 0.5f)).r; 140 | whitePoint = whitePoint < TonemapCorrection.w ? TonemapCorrection.w : whitePoint; 141 | finalColor.rgb = ApplyTonemap( 142 | sceneColor, 143 | averageLuminance, 144 | TonemapCorrection.x, 145 | TonemapCorrection.y, 146 | TonemapCorrection.z, 147 | compensation, 148 | whitePoint, 149 | int(TonemapParams0.x) 150 | ); 151 | } 152 | finalColor.rgb = clamp(finalColor.rgb, vec3(0.0, 0.0, 0.0), vec3(1.0, 1.0, 1.0)); 153 | vec4 rasterized = texture2D(s_RasterizedColor, v_texcoord0); 154 | finalColor.rgb *= 1.0 - rasterized.a; 155 | finalColor.rgb += rasterized.rgb; 156 | 157 | gl_FragColor = vec4(finalColor.rgb, 1.0); 158 | } 159 | -------------------------------------------------------------------------------- /materials/ToneMapping/src/ToneMapping.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec4 a_position : POSITION; 2 | vec2 a_texcoord0 : TEXCOORD0; 3 | 4 | vec2 v_texcoord0 : TEXCOORD0; -------------------------------------------------------------------------------- /materials/ToneMapping/src/ToneMapping.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_texcoord0 2 | $output v_texcoord0 3 | 4 | #include 5 | 6 | void main() { 7 | vec2 pos = (a_position.xy * 2.0) - 1.0; 8 | v_texcoord0 = a_texcoord0; 9 | gl_Position = vec4(pos, 0.0, 1.0); 10 | } 11 | -------------------------------------------------------------------------------- /materials/UIBase/src/UIBase.fragment.sc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | uniform vec4 TintColor; 4 | uniform vec4 HudOpacity; 5 | 6 | void main() { 7 | vec4 diffuse = TintColor; 8 | diffuse.a = diffuse.a * HudOpacity.x; 9 | gl_FragColor = diffuse; 10 | } 11 | -------------------------------------------------------------------------------- /materials/UIBase/src/UIBase.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec3 a_position : POSITION; 2 | -------------------------------------------------------------------------------- /materials/UIBase/src/UIBase.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position 2 | 3 | #include 4 | 5 | void main() { 6 | gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0)); 7 | } 8 | -------------------------------------------------------------------------------- /materials/UIBlit/src/UIBlit.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_texcoord0 2 | 3 | #include 4 | 5 | uniform vec4 TintColor; 6 | uniform vec4 HudOpacity; 7 | 8 | SAMPLER2D_AUTOREG(s_MatTexture); 9 | 10 | void main() { 11 | vec4 tex = texture2D(s_MatTexture, v_texcoord0); 12 | vec4 diffuse = tex * TintColor; 13 | diffuse.a = diffuse.a * HudOpacity.x; 14 | gl_FragColor = diffuse; 15 | } 16 | -------------------------------------------------------------------------------- /materials/UIBlit/src/UIBlit.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec3 a_position : POSITION; 2 | vec2 a_texcoord0 : TEXCOORD0; 3 | 4 | vec2 v_texcoord0 : TEXCOORD0; 5 | -------------------------------------------------------------------------------- /materials/UIBlit/src/UIBlit.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_texcoord0 2 | $output v_texcoord0 3 | 4 | #include 5 | 6 | void main() { 7 | v_texcoord0 = a_texcoord0; 8 | gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0)); 9 | } 10 | -------------------------------------------------------------------------------- /materials/UIFill/src/UIFill.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_color0 2 | 3 | #include 4 | 5 | uniform vec4 TintColor; 6 | uniform vec4 HudOpacity; 7 | 8 | void main() { 9 | vec4 diffuse = v_color0 * TintColor; 10 | diffuse.a = diffuse.a * HudOpacity.x; 11 | gl_FragColor = diffuse; 12 | } 13 | -------------------------------------------------------------------------------- /materials/UIFill/src/UIFill.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec4 a_color0 : COLOR0; 2 | vec3 a_position : POSITION; 3 | 4 | vec4 v_color0 : COLOR0; 5 | -------------------------------------------------------------------------------- /materials/UIFill/src/UIFill.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_color0 2 | $output v_color0 3 | 4 | #include 5 | 6 | void main() { 7 | v_color0 = a_color0; 8 | gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0)); 9 | } 10 | -------------------------------------------------------------------------------- /materials/UIGlint/src/UIGlint.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_texcoord0, v_color, v_layer1UV, v_layer2UV 2 | 3 | #include 4 | 5 | uniform vec4 GlintColor; 6 | uniform vec4 HudOpacity; 7 | uniform vec4 TintColor; 8 | uniform vec4 UVOffset; 9 | uniform vec4 UVRotation; 10 | uniform vec4 UVScale; 11 | 12 | SAMPLER2D_AUTOREG(s_MatTexture); 13 | SAMPLER2D_AUTOREG(s_GlintTexture); 14 | 15 | void main() { 16 | vec4 diffuse = texture2D(s_MatTexture, v_texcoord0); 17 | if (diffuse.a <= 0.00390625) { 18 | discard; 19 | } 20 | vec4 layer1 = texture2D(s_GlintTexture, fract(v_layer1UV)); 21 | vec4 layer2 = texture2D(s_GlintTexture, fract(v_layer2UV)); 22 | vec4 glint = layer1 + layer2; 23 | glint *= GlintColor; 24 | diffuse.rgb = glint.rgb; 25 | diffuse = diffuse * TintColor; 26 | diffuse.rgb *= diffuse.rgb; 27 | diffuse.rgb *= TintColor.a; 28 | diffuse = diffuse * HudOpacity.x; 29 | gl_FragColor = diffuse; 30 | } 31 | -------------------------------------------------------------------------------- /materials/UIGlint/src/UIGlint.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec4 a_color0 : COLOR0; 2 | vec3 a_position : POSITION; 3 | vec2 a_texcoord0 : TEXCOORD0; 4 | 5 | vec4 v_color : COLOR0; 6 | vec2 v_layer1UV : TEXCOORD1; 7 | vec2 v_layer2UV : TEXCOORD2; 8 | vec2 v_texcoord0 : TEXCOORD0; 9 | -------------------------------------------------------------------------------- /materials/UIGlint/src/UIGlint.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_texcoord0, a_color0 2 | $output v_texcoord0, v_color, v_layer1UV, v_layer2UV 3 | 4 | #include 5 | #include 6 | 7 | uniform vec4 GlintColor; 8 | uniform vec4 HudOpacity; 9 | uniform vec4 TintColor; 10 | uniform vec4 UVRotation; 11 | uniform vec4 UVScale; 12 | uniform vec4 UVOffset; 13 | 14 | void main() { 15 | v_texcoord0 = a_texcoord0; 16 | v_layer1UV = calculateLayerUV(a_texcoord0, UVOffset.x, UVRotation.x, UVScale.xy); 17 | v_layer2UV = calculateLayerUV(a_texcoord0, UVOffset.y, UVRotation.y, UVScale.xy); 18 | v_color = a_color0; 19 | gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0)); 20 | } 21 | -------------------------------------------------------------------------------- /materials/UISprite/src/UISprite.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_texcoord0, v_color0 2 | 3 | #include 4 | 5 | uniform vec4 TintColor; 6 | uniform vec4 HudOpacity; 7 | uniform vec4 ChangeColor; 8 | 9 | SAMPLER2D_AUTOREG(s_MatTexture); 10 | 11 | void main() { 12 | vec4 diffuse = texture2D(s_MatTexture, v_texcoord0); 13 | 14 | #ifdef MULTI_COLOR_TINT 15 | vec2 colorMask = diffuse.rg; 16 | diffuse.rgb = colorMask.rrr * v_color0.rgb; 17 | diffuse.rgb = mix(diffuse.rgb, colorMask.ggg * ChangeColor.rgb, ceil(colorMask.g)); 18 | #else 19 | diffuse.rgb = mix(diffuse.rgb, diffuse.rgb * v_color0.rgb, diffuse.a); 20 | #endif 21 | 22 | if (v_color0.a > 0.0) { 23 | diffuse.a = ceil(diffuse.a); 24 | } 25 | 26 | diffuse *= TintColor; 27 | diffuse.a = diffuse.a * HudOpacity.x; 28 | 29 | gl_FragColor = diffuse; 30 | } 31 | -------------------------------------------------------------------------------- /materials/UISprite/src/UISprite.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec3 a_position : POSITION; 2 | vec2 a_texcoord0 : TEXCOORD0; 3 | vec4 a_color0 : COLOR0; 4 | 5 | vec2 v_texcoord0 : TEXCOORD0; 6 | vec4 v_color0 : COLOR0; -------------------------------------------------------------------------------- /materials/UISprite/src/UISprite.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_texcoord0, a_color0 2 | $output v_texcoord0, v_color0 3 | 4 | #include 5 | 6 | void main() { 7 | v_texcoord0 = a_texcoord0; 8 | v_color0 = a_color0; 9 | gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0)); 10 | } 11 | -------------------------------------------------------------------------------- /materials/UIText/defines.json: -------------------------------------------------------------------------------- 1 | { 2 | "flag": { 3 | "FONT_TYPE": { 4 | "Bitmap": ["FONT_TYPE_BITMAP"], 5 | "BitmapSmooth": ["FONT_TYPE_BITMAP_SMOOTH"], 6 | "TrueType": ["FONT_TYPE_TRUE_TYPE"], 7 | "MSDF": ["FONT_TYPE_MSDF"] 8 | } 9 | } 10 | } -------------------------------------------------------------------------------- /materials/UIText/src/UIText.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_texcoord0, v_color0, v_linearClampBounds 2 | 3 | #include 4 | 5 | uniform vec4 TintColor; 6 | uniform vec4 HudOpacity; 7 | uniform vec4 OutlineCutoff; 8 | uniform vec4 GlyphCutoff; 9 | uniform vec4 GlyphSmoothRadius; 10 | uniform vec4 ShadowSmoothRadius; 11 | uniform vec4 ShadowColor; 12 | uniform vec4 OutlineColor; 13 | uniform vec4 ShadowOffset; 14 | uniform vec4 HalfTexelOffset; 15 | 16 | SAMPLER2D_AUTOREG(s_GlyphTexture); 17 | 18 | #ifndef FONT_TYPE_TRUE_TYPE 19 | bool NeedsLinearClamp() { 20 | #ifndef FONT_TYPE_MSDF 21 | return true; 22 | #else 23 | return GlyphSmoothRadius.x > 0.00095f; 24 | #endif 25 | } 26 | #endif 27 | 28 | float median(float a, float b, float c) { 29 | return max(min(a, b), min(max(a, b), c)); 30 | } 31 | 32 | void main() { 33 | vec2 texCoord = v_texcoord0; 34 | #ifndef FONT_TYPE_TRUE_TYPE 35 | if (NeedsLinearClamp()) { 36 | texCoord = min(max(v_texcoord0, v_linearClampBounds.xy), v_linearClampBounds.zw); 37 | } 38 | #endif 39 | 40 | vec4 glyphColor = texture2D(s_GlyphTexture, texCoord); 41 | #ifdef FONT_TYPE_BITMAP_SMOOTH 42 | const float center = 0.4; 43 | const float radius = 0.1; 44 | glyphColor = smoothstep(center - radius, center + radius, glyphColor); 45 | #endif 46 | 47 | #ifdef ALPHA_TEST 48 | if(glyphColor.a < 0.5) { 49 | discard; 50 | } 51 | #endif 52 | 53 | #ifdef FONT_TYPE_MSDF 54 | vec4 resultColor = v_color0; 55 | vec2 uv = v_texcoord0; 56 | float sampleDistance = median(glyphColor.r, glyphColor.g, glyphColor.b); 57 | 58 | float innerEdgeAlpha = smoothstep(max(0.0, GlyphCutoff.x - GlyphSmoothRadius.x), min(1.0, GlyphCutoff.x + GlyphSmoothRadius.x), sampleDistance); 59 | resultColor = mix(OutlineColor, resultColor, innerEdgeAlpha); 60 | 61 | float outerEdgeAlpha = smoothstep(max(0.0, OutlineCutoff.x - GlyphSmoothRadius.x), min(1.0, OutlineCutoff.x + GlyphSmoothRadius.x), sampleDistance); 62 | resultColor = vec4(resultColor.rgb, resultColor.a * outerEdgeAlpha); 63 | 64 | const float GlyphUvSize = 1.0 / 16.0; 65 | vec2 topLeft = floor(uv / GlyphUvSize) * GlyphUvSize; 66 | vec2 bottomRight = topLeft + vec2(GlyphUvSize, GlyphUvSize); 67 | 68 | vec4 shadowSample = texture2D(s_GlyphTexture, clamp(uv - ShadowOffset.xy, topLeft, bottomRight)); 69 | float shadowAlpha = smoothstep(max(0.0, OutlineCutoff.x - ShadowSmoothRadius.x), min(1.0, OutlineCutoff.x + ShadowSmoothRadius.x), shadowSample.a); 70 | 71 | vec4 diffuse = mix(vec4(ShadowColor.rgb, ShadowColor.a * shadowAlpha), resultColor, outerEdgeAlpha) * TintColor; 72 | #else 73 | vec4 diffuse = v_color0 * glyphColor * TintColor; 74 | #endif 75 | 76 | diffuse.a = diffuse.a * HudOpacity.x; 77 | gl_FragColor = diffuse; 78 | } 79 | -------------------------------------------------------------------------------- /materials/UIText/src/UIText.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec3 a_position : POSITION; 2 | vec2 a_texcoord0 : TEXCOORD0; 3 | vec4 a_color0 : COLOR0; 4 | 5 | vec2 v_texcoord0 : TEXCOORD0; 6 | vec4 v_color0 : COLOR0; 7 | vec4 v_linearClampBounds : TEXCOORD1; 8 | -------------------------------------------------------------------------------- /materials/UIText/src/UIText.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_color0, a_position, a_texcoord0 2 | $output v_texcoord0, v_color0, v_linearClampBounds 3 | 4 | #include 5 | 6 | uniform vec4 TintColor; 7 | uniform vec4 HudOpacity; 8 | uniform vec4 OutlineCutoff; 9 | uniform vec4 GlyphCutoff; 10 | uniform vec4 GlyphSmoothRadius; 11 | uniform vec4 ShadowSmoothRadius; 12 | uniform vec4 ShadowColor; 13 | uniform vec4 OutlineColor; 14 | uniform vec4 ShadowOffset; 15 | uniform vec4 HalfTexelOffset; 16 | 17 | #ifndef FONT_TYPE_TRUE_TYPE 18 | bool NeedsLinearClamp() { 19 | #ifndef FONT_TYPE_MSDF 20 | return true; 21 | #else 22 | return GlyphSmoothRadius.x > 0.00095f; 23 | #endif 24 | } 25 | #endif 26 | 27 | void main() { 28 | const float GLYPH_SIZE = 1.0 / 16.0; 29 | 30 | vec2 texCoord = a_texcoord0; 31 | #ifndef FONT_TYPE_TRUE_TYPE 32 | int corner = int(a_position.z); 33 | bool isRight = corner == 1 || corner == 2; 34 | bool isBottom = corner == 0 || corner == 1; 35 | texCoord.x += isRight ? GLYPH_SIZE : 0.0; 36 | texCoord.y += isBottom ? GLYPH_SIZE : 0.0; 37 | #endif 38 | 39 | vec4 linearClampBounds = vec4(0.0, 0.0, 1.0, 1.0); 40 | #ifndef FONT_TYPE_TRUE_TYPE 41 | if (NeedsLinearClamp()) { 42 | linearClampBounds.xy = a_texcoord0 + HalfTexelOffset.x; 43 | linearClampBounds.zw = a_texcoord0 + GLYPH_SIZE - HalfTexelOffset.x; 44 | } 45 | #endif 46 | 47 | v_texcoord0 = texCoord; 48 | v_linearClampBounds = linearClampBounds; 49 | v_color0 = a_color0; 50 | gl_Position = mul(u_modelViewProj, vec4(a_position.xy, 0.0, 1.0)); 51 | } 52 | -------------------------------------------------------------------------------- /materials/Upscaling/defines.json: -------------------------------------------------------------------------------- 1 | { 2 | "pass": { 3 | "TAAU": [""] 4 | } 5 | } -------------------------------------------------------------------------------- /materials/Upscaling/src/Upscaling.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_texcoord0 2 | 3 | #include 4 | 5 | uniform vec4 DisplayResolution; 6 | uniform vec4 RenderResolution; 7 | uniform vec4 RecipDisplayResolution; 8 | uniform vec4 RenderResolutionDivDisplayResolution; 9 | uniform vec4 DisplayResolutionDivRenderResolution; 10 | uniform vec4 SubPixelJitter; 11 | 12 | uniform mat4 PreviousViewProjectionMatrixUniform; 13 | uniform mat4 CurrentViewProjectionMatrixUniform; 14 | uniform vec4 CurrentWorldOrigin; 15 | uniform vec4 PreviousWorldOrigin; 16 | 17 | SAMPLER2D_AUTOREG(s_InputFinalColor); 18 | SAMPLER2D_AUTOREG(s_InputTAAHistory); 19 | SAMPLER2D_AUTOREG(s_InputBufferMotionVectors); 20 | 21 | vec2 computeMotionVectorForEnvironment(vec3 pixelWorldPos, mat4 currentViewProj, mat4 previousViewProj, vec3 currentWorldOrigin, vec3 previousWorldOrigin) { 22 | vec4 clipPos = mul(currentViewProj, vec4(pixelWorldPos, 0.0)); 23 | vec3 previousPixelWorldPosition = pixelWorldPos + currentWorldOrigin - previousWorldOrigin; 24 | vec4 prevClipPos = mul(previousViewProj, vec4(previousPixelWorldPosition, 0.0)); 25 | if (clipPos.w <= 0.0 || prevClipPos.w <= 0.0) { 26 | return vec2(0.0, 0.0); 27 | } 28 | vec2 ndcPos = clipPos.xy / clipPos.ww; 29 | vec2 prevNdcPos = prevClipPos.xy / prevClipPos.ww; 30 | vec2 currentToPreviousNdcXy = prevNdcPos - ndcPos; 31 | return currentToPreviousNdcXy * vec2(0.5f, -0.5f); 32 | } 33 | 34 | vec3 bicubicSampleCatmullRom(sampler2D tex, vec2 samplePos, vec2 recipTextureResolution) { 35 | vec2 tc = floor(samplePos - 0.5) + 0.5; 36 | vec2 f = clamp(samplePos - tc, 0.0, 1.0); 37 | vec2 f2 = f * f; 38 | vec2 f3 = f2 * f; 39 | vec2 w0 = f2 - 0.5 * (f3 + f); 40 | vec2 w1 = 1.5 * f3 - 2.5 * f2 + 1.0; 41 | vec2 w3 = 0.5 * (f3 - f2); 42 | vec2 w2 = 1.0 - w0 - w1 - w3; 43 | vec2 w12 = w1 + w2; 44 | vec2 tc0 = (tc - 1.0) * recipTextureResolution; 45 | vec2 tc12 = (tc + w2 / w12) * recipTextureResolution; 46 | vec2 tc3 = (tc + 2.0) * recipTextureResolution; 47 | vec3 result = 48 | texture2DLod(tex, vec2(tc0.x, tc0.y), 0.0).rgb * (w0.x * w0.y) + 49 | texture2DLod(tex, vec2(tc0.x, tc12.y), 0.0).rgb * (w0.x * w12.y) + 50 | texture2DLod(tex, vec2(tc0.x, tc3.y), 0.0).rgb * (w0.x * w3.y) + 51 | texture2DLod(tex, vec2(tc12.x, tc0.y), 0.0).rgb * (w12.x * w0.y) + 52 | texture2DLod(tex, vec2(tc12.x, tc12.y), 0.0).rgb * (w12.x * w12.y) + 53 | texture2DLod(tex, vec2(tc12.x, tc3.y), 0.0).rgb * (w12.x * w3.y) + 54 | texture2DLod(tex, vec2(tc3.x, tc0.y), 0.0).rgb * (w3.x * w0.y) + 55 | texture2DLod(tex, vec2(tc3.x, tc12.y), 0.0).rgb * (w3.x * w12.y) + 56 | texture2DLod(tex, vec2(tc3.x, tc3.y), 0.0).rgb * (w3.x * w3.y); 57 | return max(vec3(0.0, 0.0, 0.0), result); 58 | } 59 | 60 | float sampleWeight(vec2 delta, float scale) { 61 | float x = scale * dot(delta, delta); 62 | return clamp(1.0 - x, 0.05, 1.0); 63 | } 64 | 65 | void main() { 66 | #if !defined(FALLBACK) && (BGFX_SHADER_LANGUAGE_GLSL >= 310 || BGFX_SHADER_LANGUAGE_HLSL >= 500 || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_SPIRV || BGFX_SHADER_LANGUAGE_METAL) 67 | vec2 coords = DisplayResolution.xy * v_texcoord0; 68 | highp uint x = uint(coords.x); 69 | highp uint y = uint(coords.y); 70 | vec2 nearestRenderPos = vec2(float(x) + 0.5f, float(y) + 0.5f) * RenderResolutionDivDisplayResolution.x - SubPixelJitter.xy - 0.5f; 71 | ivec2 intRenderPos = ivec2(round(nearestRenderPos.x), round(nearestRenderPos.y)); 72 | vec4 currentColor = texelFetch(s_InputFinalColor, intRenderPos, 0).rgba; 73 | vec2 motionPixels = texelFetch(s_InputBufferMotionVectors, intRenderPos, 0).ba; 74 | #if !BGFX_SHADER_LANGUAGE_GLSL 75 | motionPixels.y *= -1.0; 76 | #endif 77 | vec3 c1 = currentColor.rgb; 78 | vec3 c2 = currentColor.rgb * currentColor.rgb; 79 | for (int i = -1; i <= 1; i++) { 80 | for (int j = -1; j <= 1; j++) { 81 | if (i == 0 && j == 0) 82 | continue; 83 | ivec2 p = intRenderPos + ivec2(i, j); 84 | vec3 c = texelFetch(s_InputFinalColor, p, 0).rgb; 85 | vec2 mv = texelFetch(s_InputBufferMotionVectors, p, 0).ba; 86 | c1 = c1 + c; 87 | c2 = c2 + c * c; 88 | } 89 | } 90 | motionPixels *= RenderResolution.xy; 91 | c1 = c1 / 9.0f; 92 | c2 = c2 / 9.0f; 93 | vec3 extent = sqrt(max(vec3(0.0, 0.0, 0.0), c2 - c1 * c1)); 94 | float motionWeight = smoothstep(0.0, 1.0f, sqrt(dot(motionPixels, motionPixels))); 95 | float bias = mix(4.0f, 1.0f, motionWeight); 96 | vec3 minValidColor = c1 - extent * bias; 97 | vec3 maxValidColor = c1 + extent * bias; 98 | vec2 posPreviousPixels = vec2(float(x) + 0.5f, float(y) + 0.5f) - motionPixels * DisplayResolutionDivRenderResolution.x; 99 | posPreviousPixels = clamp(posPreviousPixels, vec2(0, 0), DisplayResolution.xy - 1.0f); 100 | vec3 prevColor = bicubicSampleCatmullRom(s_InputTAAHistory, posPreviousPixels, RecipDisplayResolution.xy); 101 | prevColor = min(maxValidColor, max(minValidColor, prevColor)); 102 | float pixelWeight = max(motionWeight, sampleWeight(nearestRenderPos - vec2(intRenderPos), DisplayResolutionDivRenderResolution.x)) * 0.1f; 103 | vec3 finalColor = mix(prevColor, currentColor.rgb, pixelWeight); 104 | gl_FragColor = vec4(finalColor, 0.0); 105 | #else 106 | gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); 107 | #endif 108 | } 109 | -------------------------------------------------------------------------------- /materials/Upscaling/src/Upscaling.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec3 a_position : POSITION; 2 | vec2 a_texcoord0 : TEXCOORD0; 3 | 4 | vec2 v_texcoord0 : TEXCOORD0; 5 | -------------------------------------------------------------------------------- /materials/Upscaling/src/Upscaling.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_texcoord0 2 | $output v_texcoord0 3 | 4 | #include 5 | 6 | void main() { 7 | #if !defined(FALLBACK) 8 | vec4 pos = vec4(a_position, 1.0); 9 | pos.xy = pos.xy * 2.0 - 1.0; 10 | v_texcoord0 = a_texcoord0; 11 | gl_Position = pos; 12 | #else 13 | gl_Position = vec4(0.0, 0.0, 0.0, 0.0); 14 | #endif 15 | } 16 | -------------------------------------------------------------------------------- /materials/VolumeScattering/src/VolumeScattering.compute.sc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | uniform vec4 ClearValue; 4 | uniform vec4 VolumeNearFar; 5 | uniform vec4 VolumeDimensions; 6 | 7 | IMAGE2D_ARRAY_RO_AUTOREG(s_LightingBuffer, rgba16f); 8 | IMAGE2D_ARRAY_WR_AUTOREG(s_ScatteringBuffer, rgba16f); 9 | 10 | float linearToLogDepth(float linearDepth) { 11 | return log((exp(4.0) - 1.0) * linearDepth + 1.0) / 4.0; 12 | } 13 | 14 | float logToLinearDepth(float logDepth) { 15 | return (exp(4.0 * logDepth) - 1.0) / (exp(4.0) - 1.0); 16 | } 17 | 18 | void Scattering(uvec3 GlobalInvocationID) { 19 | int volumeWidth = int(VolumeDimensions.x); 20 | int volumeHeight = int(VolumeDimensions.y); 21 | int volumeDepth = int(VolumeDimensions.z); 22 | int x = int(GlobalInvocationID.x); 23 | int y = int(GlobalInvocationID.y); 24 | if (x >= volumeWidth || y >= volumeHeight) { 25 | return; 26 | } 27 | float prevW = -0.5 / VolumeDimensions.z; 28 | float prevWLinear = logToLinearDepth(prevW); 29 | float prevDepth = (1.0 - prevWLinear) * VolumeNearFar.x + prevWLinear * VolumeNearFar.y; 30 | vec4 accum = vec4(0.0, 0.0, 0.0, 1.0); 31 | for (int z = 0; z < volumeDepth; ++z) { 32 | float nextW = (float(z) + 0.5) / VolumeDimensions.z; 33 | float nextWLinear = logToLinearDepth(nextW); 34 | float nextDepth = (1.0 - nextWLinear) * VolumeNearFar.x + nextWLinear * VolumeNearFar.y; 35 | float stepSize = nextDepth - prevDepth; 36 | prevDepth = nextDepth; 37 | vec4 sourceExtinction = imageLoad(s_LightingBuffer, ivec3(x, y, z)); 38 | float transmittance = exp(-sourceExtinction.a * stepSize); 39 | float contribution = abs(sourceExtinction.a) > 1e-6 ? ((1.0 - transmittance) / sourceExtinction.a) : stepSize; 40 | accum.rgb += accum.a * contribution * sourceExtinction.rgb; 41 | accum.a *= transmittance; 42 | imageStore(s_ScatteringBuffer, ivec3(x, y, z), accum); 43 | } 44 | } 45 | 46 | NUM_THREADS(8, 8, 1) 47 | void main() { 48 | Scattering(gl_GlobalInvocationID); 49 | } 50 | -------------------------------------------------------------------------------- /materials/VolumeScattering/src/VolumeScattering.fragment.sc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() { 4 | gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); 5 | } 6 | -------------------------------------------------------------------------------- /materials/VolumeScattering/src/VolumeScattering.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec3 a_position : POSITION; 2 | -------------------------------------------------------------------------------- /materials/VolumeScattering/src/VolumeScattering.vertex.sc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() { 4 | gl_Position = vec4(0.0, 0.0, 0.0, 0.0); 5 | } 6 | -------------------------------------------------------------------------------- /materials/Weather/src/Weather.fragment.sc: -------------------------------------------------------------------------------- 1 | $input v_texcoord0, v_fog, v_occlusionHeight, v_occlusionUV 2 | 3 | #include 4 | #include 5 | 6 | uniform vec4 Dimensions; 7 | uniform vec4 ViewPosition; 8 | uniform vec4 UVOffsetAndScale; 9 | uniform vec4 OcclusionHeightOffset; 10 | uniform vec4 FogColor; 11 | uniform vec4 FogAndDistanceControl; 12 | uniform vec4 PositionForwardOffset; 13 | uniform vec4 PositionBaseOffset; 14 | uniform vec4 Velocity; 15 | 16 | SAMPLER2D(s_WeatherTexture, s_WeatherTexture_REG); 17 | SAMPLER2D(s_OcclusionTexture, s_OcclusionTexture_REG); 18 | SAMPLER2D(s_LightingTexture, s_LightingTexture_REG); 19 | 20 | float getOcclusionHeight(const vec4 occlusionTextureSample) { 21 | float height = occlusionTextureSample.g + (occlusionTextureSample.b * 255.0f) - (OcclusionHeightOffset.x / 255.0f); 22 | return height; 23 | } 24 | 25 | float getOcclusionLuminance(const vec4 occlusionTextureSample) { 26 | return occlusionTextureSample.r; 27 | } 28 | 29 | bool isOccluded(const vec2 occlusionUV, const float occlusionHeight, const float occlusionHeightThreshold) { 30 | #ifndef FLIP_OCCLUSION 31 | #define OCCLUSION_OPERATOR < 32 | #else 33 | #define OCCLUSION_OPERATOR > 34 | #endif 35 | 36 | #ifndef NO_OCCLUSION 37 | // clamp the uvs 38 | return ( occlusionUV.x >= 0.0 && occlusionUV.x <= 1.0 && 39 | occlusionUV.y >= 0.0 && occlusionUV.y <= 1.0 && 40 | occlusionHeight OCCLUSION_OPERATOR occlusionHeightThreshold); 41 | #else 42 | return false; 43 | #endif 44 | } 45 | 46 | void main() { 47 | vec4 diffuse = texture2D(s_WeatherTexture, v_texcoord0); 48 | vec4 occlusionLuminanceAndHeightThreshold = texture2D(s_OcclusionTexture, v_occlusionUV); 49 | 50 | float occlusionLuminance = getOcclusionLuminance(occlusionLuminanceAndHeightThreshold); 51 | float occlusionHeightThreshold = getOcclusionHeight(occlusionLuminanceAndHeightThreshold); 52 | 53 | if (isOccluded(v_occlusionUV, v_occlusionHeight, occlusionHeightThreshold)) { 54 | diffuse.a = 0.0; 55 | } else { 56 | float mixAmount = (v_occlusionHeight - occlusionHeightThreshold) * 25.0; 57 | float uvX = occlusionLuminance - (mixAmount * occlusionLuminance); 58 | vec2 lightingUV = vec2(uvX, 1.0); 59 | vec3 light = texture2D(s_LightingTexture, lightingUV).rgb; 60 | diffuse.rgb *= light; 61 | diffuse.rgb = applyFog(diffuse.rgb, v_fog.rgb, v_fog.a); 62 | } 63 | gl_FragColor = diffuse; 64 | } 65 | -------------------------------------------------------------------------------- /materials/Weather/src/Weather.varying.def.sc: -------------------------------------------------------------------------------- 1 | vec3 a_position : POSITION; 2 | vec4 a_color0 : COLOR0; 3 | vec2 a_texcoord0 : TEXCOORD0; 4 | 5 | vec2 v_texcoord0 : TEXCOORD0; 6 | vec4 v_fog : COLOR0; 7 | float v_occlusionHeight : TEXCOORD2; 8 | vec2 v_occlusionUV : TEXCOORD1; 9 | -------------------------------------------------------------------------------- /materials/Weather/src/Weather.vertex.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_color0, a_texcoord0 2 | $output v_texcoord0, v_fog, v_occlusionHeight, v_occlusionUV 3 | 4 | #include 5 | #include 6 | 7 | uniform vec4 FogColor; 8 | uniform vec4 FogAndDistanceControl; 9 | uniform vec4 Velocity; 10 | uniform vec4 Dimensions; 11 | uniform vec4 ViewPosition; 12 | uniform vec4 UVOffsetAndScale; 13 | uniform vec4 PositionForwardOffset; 14 | uniform vec4 PositionBaseOffset; 15 | 16 | #if BGFX_SHADER_LANGUAGE_GLSL 17 | #if BGFX_SHADER_LANGUAGE_GLSL < 130 18 | float trunc(float _x) { return float(int(_x)); } 19 | #endif // BGFX_SHADER_LANGUAGE_GLSL < 130 20 | float fmod(float _a, float _b) { return _a - _b * trunc(_a / _b); } 21 | vec2 fmod(vec2 _a, vec2 _b) { return vec2(fmod(_a.x, _b.x), fmod(_a.y, _b.y)); } 22 | vec3 fmod(vec3 _a, vec3 _b) { return vec3(fmod(_a.x, _b.x), fmod(_a.y, _b.y), fmod(_a.z, _b.z)); } 23 | vec4 fmod(vec4 _a, vec4 _b) { return vec4(fmod(_a.x, _b.x), fmod(_a.y, _b.y), fmod(_a.z, _b.z), fmod(_a.w, _b.w)); } 24 | #endif // BGFX_SHADER_LANGUAGE_GLSL 25 | 26 | void main() { 27 | float spriteSelector = a_color0.x * 255.0; 28 | 29 | vec2 texcoord0 = UVOffsetAndScale.xy + (a_texcoord0 * UVOffsetAndScale.zw); 30 | #if !NO_VARIETY 31 | texcoord0.x += spriteSelector * UVOffsetAndScale.z; 32 | #endif 33 | 34 | const float PARTICLE_BOX_DIMENSIONS = 30.0; 35 | const vec3 PARTICLE_BOX = vec3(PARTICLE_BOX_DIMENSIONS, PARTICLE_BOX_DIMENSIONS, PARTICLE_BOX_DIMENSIONS); 36 | 37 | // subtract the offset then fmod into (0.0f, PARTICLE_BOX) 38 | vec3 worldSpacePosition = fmod(a_position + PositionBaseOffset.xyz, PARTICLE_BOX); 39 | 40 | // centre box on origin 41 | worldSpacePosition -= PARTICLE_BOX * 0.5f; 42 | 43 | // push along view vector so box is positioned more infront of camera 44 | worldSpacePosition += PositionForwardOffset.xyz; 45 | 46 | // get world position 47 | vec3 worldSpacePositionBottom = worldSpacePosition; 48 | vec3 worldSpacePositionTop = worldSpacePositionBottom + (Velocity.xyz * Dimensions.y); 49 | 50 | // get projected positions of top and bottom of particle, and top of particle in previous frame 51 | vec4 screenSpacePositionBottom = mul(u_modelViewProj, vec4(worldSpacePositionBottom, 1.0)); 52 | vec4 screenSpacePositionTop = mul(u_modelViewProj, vec4(worldSpacePositionTop, 1.0)); 53 | 54 | // get 2d vector in screenspace between top and bottom of particle 55 | vec2 screenSpaceUpDirection = (screenSpacePositionTop.xy / screenSpacePositionTop.w) - (screenSpacePositionBottom.xy / screenSpacePositionBottom.w); 56 | 57 | // get 2d vector perpendicular to velocity 58 | vec2 screenSpaceRightDirection = normalize(vec2(-screenSpaceUpDirection.y, screenSpaceUpDirection.x)); 59 | 60 | // choose either the top or bottom projected position using uv.y 61 | vec4 position = mix(screenSpacePositionTop, screenSpacePositionBottom, a_texcoord0.y); 62 | 63 | // offset the position of each side of the particle using uv.x 64 | position.xy += (0.5 - a_texcoord0.x) * screenSpaceRightDirection * Dimensions.x; 65 | 66 | vec2 occlusionUV = worldSpacePosition.xz; 67 | occlusionUV += ViewPosition.xz; 68 | occlusionUV *= 1.0 / 64.0; // Scale by 1/TextureDimensions to get values between 69 | occlusionUV += 0.5; // Offset so that center of view is in the center of occlusion texture 70 | 71 | float occlusionHeight = worldSpacePosition.y; 72 | occlusionHeight += ViewPosition.y - 0.5; 73 | occlusionHeight *= 1.0 / 255.0; 74 | 75 | //fog 76 | float fogIntensity = calculateFogIntensity(position.z, FogAndDistanceControl.z, FogAndDistanceControl.x, FogAndDistanceControl.y); 77 | vec4 fog = vec4(FogColor.rgb, fogIntensity); 78 | 79 | v_texcoord0 = texcoord0; 80 | v_fog = fog; 81 | v_occlusionHeight = occlusionHeight; 82 | v_occlusionUV = occlusionUV; 83 | gl_Position = position; 84 | } 85 | -------------------------------------------------------------------------------- /setup_build_environment.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | set MBT_VERSION=0.8.2 4 | set DATA_VERSION=1.20.60 5 | 6 | set MBT_DIR=env\MaterialBinTool-%MBT_VERSION% 7 | set MBT=%MBT_DIR%\MaterialBinTool-%MBT_VERSION%-native-image.exe 8 | set SHADERC=%MBT_DIR%\shaderc.exe 9 | 10 | set DATA_DIR=data\%DATA_VERSION% 11 | 12 | cd %~dp0 13 | 14 | if not exist env ( 15 | mkdir env 16 | ) 17 | if not exist %MBT_DIR% ( 18 | mkdir %MBT_DIR% 19 | ) 20 | if not exist %MBT% ( 21 | echo Downloading MaterialBinTool-%MBT_VERSION% 22 | curl -L -o %MBT% https://github.com/ddf8196/MaterialBinTool/releases/download/v%MBT_VERSION%/MaterialBinTool-%MBT_VERSION%-native-image.exe 23 | ) 24 | if not exist %SHADERC% ( 25 | echo Downloading shaderc 26 | curl -L -o %SHADERC% https://github.com/ddf8196/MaterialBinTool/releases/download/v%MBT_VERSION%/shaderc.exe 27 | ) 28 | 29 | if not exist data ( 30 | echo Cloning RenderDragonData 31 | git clone --branch %DATA_VERSION% --single-branch --depth 1 --origin origin https://github.com/ddf8196/RenderDragonData.git data 32 | ) 33 | if not exist %DATA_DIR% ( 34 | echo Updating RenderDragonData 35 | pushd data 36 | git fetch origin %DATA_VERSION%:%DATA_VERSION% 37 | git checkout %DATA_VERSION% 38 | git checkout . 39 | git pull 40 | popd 41 | ) 42 | --------------------------------------------------------------------------------