Files
Lumina-GI/Runtime/Scripts/SEGI/Resources/SEGI.shader
T
2025-07-20 19:03:25 +08:00

1476 lines
53 KiB
Plaintext

Shader "Hidden/SEGI" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_MainTexARRAY("Base (RGB)", 2DArray) = "white" {}
_MainTexSKY("SKY (RGB)", 2DArray) = "white" {}
//HDRP2
DepthPyramidScale("Depth Pyramid Scale", Float) = (1, 0.67, 0, 0)
}
HLSLINCLUDE//CGINCLUDE
//HDRP2
//#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/API/D3D11.hlsl"
//v1.1
//#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/TextureXR.hlsl"
//#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/ScreenSpaceLighting/ShaderVariablesScreenSpaceLighting.hlsl"
//#define SAMPLER(samplerName) SamplerState samplerName
#define LOAD_TEXTURE2D_ARRAY(textureName, unCoord2, index) textureName.Load(int4(unCoord2, index, 0))
#define TEXTURE2D_ARRAY(textureName) Texture2DArray textureName
//#define TEXTURE2D(textureName) Texture2D textureName
//#define LOAD_TEXTURE2D(textureName, texelSize, icoord2) textureName.Load(int3(icoord2, 0))
//#define LOAD_TEXTURE2D_ARRAY_LOD(textureName, unCoord2, index, lod) textureName.Load(int4(unCoord2, index, lod))
// #define SLICE_ARRAY_INDEX 0
// #define SAMPLER(samplerName) SamplerState samplerName
// #define LOAD_TEXTURE2D_ARRAY(textureName, unCoord2, index) textureName.Load(int4(unCoord2, index, 0))
// #define TEXTURE2D_ARRAY(textureName) Texture2DArray textureName
// #define SAMPLE_TEXTURE2D_X_LOD(textureName, samplerName, coord2, lod) SAMPLE_TEXTURE2D_ARRAY_LOD(textureName, samplerName, coord2, SLICE_ARRAY_INDEX, lod)
// #define LOAD_TEXTURE2D_X_LOD(textureName, unCoord2, lod) LOAD_TEXTURE2D_ARRAY_LOD(textureName, unCoord2, SLICE_ARRAY_INDEX, lod)
//
#include "UnityCG.cginc"
#include "SEGI.cginc"
#pragma target 5.0
//
//HDRP2
//float4 DepthPyramidScale;//HDRP2
//HDRP 2019.3
//https://forum.unity.com/threads/has-depthpyramidtexture-been-removed-from-hdrp-in-unity-2019-3.851236/#post-5615971
//UNITY_DECLARE_TEX2DARRAY(_CameraDepthTexture);
//TEXTURE2D_ARRAY(_CameraDepthTexture);
//SAMPLER(sampler_CameraDepthTexture); //v0.4a HDRP 10.2
///TEXTURE2D_X(_DepthPyramidTexture);
//SAMPLER(sampler_DepthPyramidTexture);
//TEXTURE2D_ARRAY(_DepthTexture);
//SAMPLER(sampler_DepthTexture);
// //////////////// CUSTOM DEPTH COPY
// //#pragma vertex Vert
// //#pragma target 4.5
// //#pragma only_renderers d3d11 playstation xboxone xboxseries vulkan metal switch
// //#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/CustomPass/CustomPassCommon.hlsl"
// // Note: To sample camera depth in HDRP we provide these utils functions because the way we store the depth mips can change
//// Currently it's an atlas and it's layout can be found at ComputePackedMipChainInfo in HDUtils.cs
// float LoadCameraDepth(uint2 pixelCoords)
// {
// return 0;// LOAD_TEXTURE2D_X_LOD(_CameraDepthTexture, pixelCoords, 0).r;
// }
// struct PositionInputs
// {
// float3 positionWS; // World space position (could be camera-relative)
// float2 positionNDC; // Normalized screen coordinates within the viewport : [0, 1) (with the half-pixel offset)
// uint2 positionSS; // Screen space pixel coordinates : [0, NumPixels)
// uint2 tileCoord; // Screen tile coordinates : [0, NumTiles)
// float deviceDepth; // Depth from the depth buffer : [0, 1] (typically reversed)
// float linearDepth; // View space Z coordinate : [Near, Far]
// };
// float4 GetFullScreenTriangleVertexPosition(uint vertexID, float z = UNITY_NEAR_CLIP_VALUE)
// {
// // note: the triangle vertex position coordinates are x2 so the returned UV coordinates are in range -1, 1 on the screen.
// float2 uv = float2((vertexID << 1) & 2, vertexID & 2);
// return float4(uv * 2.0 - 1.0, z, 1.0);
// }
// struct Attributes
// {
// uint vertexID : SV_VertexID;
// UNITY_VERTEX_INPUT_INSTANCE_ID
// };
// struct Varyings
// {
// float4 positionCS : SV_POSITION;
// UNITY_VERTEX_OUTPUT_STEREO
// };
// Varyings Vert(Attributes input)
// {
// Varyings output;
// UNITY_SETUP_INSTANCE_ID(input);
// UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
// output.positionCS = GetFullScreenTriangleVertexPosition(input.vertexID, UNITY_RAW_FAR_CLIP_VALUE);
// return output;
// }
// float4 _Scale;
// //float4 _ScreenSize;
// float4x4 _InvViewProjMatrix;
//#define UNITY_MATRIX_I_VP _InvViewProjMatrix
// float4 SampleBuffer(PositionInputs posInput);
// // ----------------------------------------------------------------------------
//// Depth encoding/decoding
//// ----------------------------------------------------------------------------
//
//// Z buffer to linear 0..1 depth (0 at near plane, 1 at far plane).
//// Does NOT correctly handle oblique view frustums.
//// Does NOT work with orthographic projection.
//// zBufferParam = { (f-n)/n, 1, (f-n)/n*f, 1/f }
//float Linear01DepthFromNear(float depth, float4 zBufferParam)
//{
// return 1.0 / (zBufferParam.x + zBufferParam.y / depth);
//}
//
//// Z buffer to linear 0..1 depth (0 at camera position, 1 at far plane).
//// Does NOT work with orthographic projections.
//// Does NOT correctly handle oblique view frustums.
//// zBufferParam = { (f-n)/n, 1, (f-n)/n*f, 1/f }
//float Linear01Depth(float depth, float4 zBufferParam)
//{
// return 1.0 / (zBufferParam.x * depth + zBufferParam.y);
//}
//
//// Z buffer to linear depth.
//// Does NOT correctly handle oblique view frustums.
//// Does NOT work with orthographic projection.
//// zBufferParam = { (f-n)/n, 1, (f-n)/n*f, 1/f }
//float LinearEyeDepth(float depth, float4 zBufferParam)
//{
// return 1.0 / (zBufferParam.z * depth + zBufferParam.w);
//}
//
//// Z buffer to linear depth.
//// Correctly handles oblique view frustums.
//// Does NOT work with orthographic projection.
//// Ref: An Efficient Depth Linearization Method for Oblique View Frustums, Eq. 6.
//float LinearEyeDepth(float2 positionNDC, float deviceDepth, float4 invProjParam)
//{
// float4 positionCS = float4(positionNDC * 2.0 - 1.0, deviceDepth, 1.0);
// float viewSpaceZ = rcp(dot(positionCS, invProjParam));
//
// // If the matrix is right-handed, we have to flip the Z axis to get a positive value.
// return abs(viewSpaceZ);
//}
//
//// Z buffer to linear depth.
//// Works in all cases.
//// Typically, this is the cheapest variant, provided you've already computed 'positionWS'.
//// Assumes that the 'positionWS' is in front of the camera.
//float LinearEyeDepth(float3 positionWS, float4x4 viewMatrix)
//{
// float viewSpaceZ = mul(viewMatrix, float4(positionWS, 1.0)).z;
//
// // If the matrix is right-handed, we have to flip the Z axis to get a positive value.
// return abs(viewSpaceZ);
//}
// // ----------------------------------------------------------------------------
//// Space transformations
//// ----------------------------------------------------------------------------
//
// static const float3x3 k_identity3x3 = { 1, 0, 0,
// 0, 1, 0,
// 0, 0, 1 };
//
// static const float4x4 k_identity4x4 = { 1, 0, 0, 0,
// 0, 1, 0, 0,
// 0, 0, 1, 0,
// 0, 0, 0, 1 };
//
// float4 ComputeClipSpacePosition(float2 positionNDC, float deviceDepth)
// {
// float4 positionCS = float4(positionNDC * 2.0 - 1.0, deviceDepth, 1.0);
//
//#if UNITY_UV_STARTS_AT_TOP
// // Our world space, view space, screen space and NDC space are Y-up.
// // Our clip space is flipped upside-down due to poor legacy Unity design.
// // The flip is baked into the projection matrix, so we only have to flip
// // manually when going from CS to NDC and back.
// positionCS.y = -positionCS.y;
//#endif
//
// return positionCS;
// }
// // Use case examples:
//// (position = positionCS) => (clipSpaceTransform = use default)
//// (position = positionVS) => (clipSpaceTransform = UNITY_MATRIX_P)
//// (position = positionWS) => (clipSpaceTransform = UNITY_MATRIX_VP)
// float4 ComputeClipSpacePosition(float3 position, float4x4 clipSpaceTransform = k_identity4x4)
// {
// return mul(clipSpaceTransform, float4(position, 1.0));
// }
//
// // The returned Z value is the depth buffer value (and NOT linear view space Z value).
// // Use case examples:
// // (position = positionCS) => (clipSpaceTransform = use default)
// // (position = positionVS) => (clipSpaceTransform = UNITY_MATRIX_P)
// // (position = positionWS) => (clipSpaceTransform = UNITY_MATRIX_VP)
// float3 ComputeNormalizedDeviceCoordinatesWithZ(float3 position, float4x4 clipSpaceTransform = k_identity4x4)
// {
// float4 positionCS = ComputeClipSpacePosition(position, clipSpaceTransform);
//
//#if UNITY_UV_STARTS_AT_TOP
// // Our world space, view space, screen space and NDC space are Y-up.
// // Our clip space is flipped upside-down due to poor legacy Unity design.
// // The flip is baked into the projection matrix, so we only have to flip
// // manually when going from CS to NDC and back.
// positionCS.y = -positionCS.y;
//#endif
//
// positionCS *= rcp(positionCS.w);
// positionCS.xy = positionCS.xy * 0.5 + 0.5;
//
// return positionCS.xyz;
// }
//
// // Use case examples:
// // (position = positionCS) => (clipSpaceTransform = use default)
// // (position = positionVS) => (clipSpaceTransform = UNITY_MATRIX_P)
// // (position = positionWS) => (clipSpaceTransform = UNITY_MATRIX_VP)
// float2 ComputeNormalizedDeviceCoordinates(float3 position, float4x4 clipSpaceTransform = k_identity4x4)
// {
// return ComputeNormalizedDeviceCoordinatesWithZ(position, clipSpaceTransform).xy;
// }
//
// float3 ComputeViewSpacePosition(float2 positionNDC, float deviceDepth, float4x4 invProjMatrix)
// {
// float4 positionCS = ComputeClipSpacePosition(positionNDC, deviceDepth);
// float4 positionVS = mul(invProjMatrix, positionCS);
// // The view space uses a right-handed coordinate system.
// positionVS.z = -positionVS.z;
// return positionVS.xyz / positionVS.w;
// }
//
// float3 ComputeWorldSpacePosition(float2 positionNDC, float deviceDepth, float4x4 invViewProjMatrix)
// {
// float4 positionCS = ComputeClipSpacePosition(positionNDC, deviceDepth);
// float4 hpositionWS = mul(invViewProjMatrix, positionCS);
// return hpositionWS.xyz / hpositionWS.w;
// }
//
// float3 ComputeWorldSpacePosition(float4 positionCS, float4x4 invViewProjMatrix)
// {
// float4 hpositionWS = mul(invViewProjMatrix, positionCS);
// return hpositionWS.xyz / hpositionWS.w;
// }
// // This function is use to provide an easy way to sample into a screen texture, either from a pixel or a compute shaders.
//// This allow to easily share code.
//// If a compute shader call this function positionSS is an integer usually calculate like: uint2 positionSS = groupId.xy * BLOCK_SIZE + groupThreadId.xy
//// else it is current unormalized screen coordinate like return by SV_Position
// PositionInputs GetPositionInput(float2 positionSS, float2 invScreenSize, uint2 tileCoord) // Specify explicit tile coordinates so that we can easily make it lane invariant for compute evaluation.
// {
// PositionInputs posInput;
// ZERO_INITIALIZE(PositionInputs, posInput);
//
// posInput.positionNDC = positionSS;
//#if defined(SHADER_STAGE_COMPUTE) || defined(SHADER_STAGE_RAY_TRACING)
// // In case of compute shader an extra half offset is added to the screenPos to shift the integer position to pixel center.
// posInput.positionNDC.xy += float2(0.5, 0.5);
//#endif
// posInput.positionNDC *= invScreenSize;
// posInput.positionSS = uint2(positionSS);
// posInput.tileCoord = tileCoord;
//
// return posInput;
// }
// PositionInputs GetPositionInput(float2 positionSS, float2 invScreenSize)
// {
// return GetPositionInput(positionSS, invScreenSize, uint2(0, 0));
// }
// // For Raytracing only
// // This function does not initialize deviceDepth and linearDepth
// PositionInputs GetPositionInput(float2 positionSS, float2 invScreenSize, float3 positionWS)
// {
// PositionInputs posInput = GetPositionInput(positionSS, invScreenSize, uint2(0, 0));
// posInput.positionWS = positionWS;
//
// return posInput;
// }
// // From forward
// // deviceDepth and linearDepth come directly from .zw of SV_Position
// PositionInputs GetPositionInput(float2 positionSS, float2 invScreenSize, float deviceDepth, float linearDepth, float3 positionWS, uint2 tileCoord)
// {
// PositionInputs posInput = GetPositionInput(positionSS, invScreenSize, tileCoord);
// posInput.positionWS = positionWS;
// posInput.deviceDepth = deviceDepth;
// posInput.linearDepth = linearDepth;
//
// return posInput;
// }
// PositionInputs GetPositionInput(float2 positionSS, float2 invScreenSize, float deviceDepth, float linearDepth, float3 positionWS)
// {
// return GetPositionInput(positionSS, invScreenSize, deviceDepth, linearDepth, positionWS, uint2(0, 0));
// }
// // From deferred or compute shader
// // depth must be the depth from the raw depth buffer. This allow to handle all kind of depth automatically with the inverse view projection matrix.
// // For information. In Unity Depth is always in range 0..1 (even on OpenGL) but can be reversed.
// PositionInputs GetPositionInput(float2 positionSS, float2 invScreenSize, float deviceDepth,
// float4x4 invViewProjMatrix, float4x4 viewMatrix,
// uint2 tileCoord)
// {
// PositionInputs posInput = GetPositionInput(positionSS, invScreenSize, tileCoord);
// posInput.positionWS = ComputeWorldSpacePosition(posInput.positionNDC, deviceDepth, invViewProjMatrix);
// posInput.deviceDepth = deviceDepth;
// posInput.linearDepth = LinearEyeDepth(posInput.positionWS, viewMatrix);
//
// return posInput;
// }
// PositionInputs GetPositionInput(float2 positionSS, float2 invScreenSize, float deviceDepth,
// float4x4 invViewProjMatrix, float4x4 viewMatrix)
// {
// return GetPositionInput(positionSS, invScreenSize, deviceDepth, invViewProjMatrix, viewMatrix, uint2(0, 0));
// }
// float4 FullScreenPass(Varyings varyings) : SV_Target
// {
// UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(varyings);
// float depth = LoadCameraDepth(varyings.positionCS.xy);
// PositionInputs posInput = GetPositionInput(varyings.positionCS.xy, _ScreenSize.zw, depth, UNITY_MATRIX_I_VP, UNITY_MATRIX_V);
//
// return SampleBuffer(posInput);
// }
struct v2f
{
float4 pos : SV_POSITION;
float4 uv : TEXCOORD0;
#if UNITY_UV_STARTS_AT_TOP
half4 uv2 : TEXCOORD1;
#endif
};
v2f vert(appdata_img v)
{
v2f o;
o.pos = UnityObjectToClipPos (v.vertex);
o.uv = float4(v.texcoord.xy, 1, 1);
#if UNITY_UV_STARTS_AT_TOP
o.uv2 = float4(v.texcoord.xy, 1, 1);
if (_MainTex_TexelSize.y < 0.0)
o.uv.y = 1.0 - o.uv.y;
#endif
return o;
}
//HDRP
// Generates a triangle in homogeneous clip space, s.t.
// v0 = (-1, -1, 1), v1 = (3, -1, 1), v2 = (-1, 3, 1).
float2 GetFullScreenTriangleTexCoord(uint vertexID)
{
#if UNITY_UV_STARTS_AT_TOP
return float2((vertexID << 1) & 2, 1.0 - (vertexID & 2));
#else
return float2((vertexID << 1) & 2, vertexID & 2);
#endif
}
float4 GetFullScreenTriangleVertexPositionA(uint vertexID, float z = UNITY_NEAR_CLIP_VALUE)
{
// note: the triangle vertex position coordinates are x2 so the returned UV coordinates are in range -1, 1 on the screen.
float2 uv = float2((vertexID << 1) & 2, vertexID & 2);
return float4(uv * 2.0 - 1.0, z, 1.0);
}
struct VaryingsA
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;//float2 texcoord : TEXCOORD0;
/*#if UNITY_UV_STARTS_AT_TOP
half4 uv2 : TEXCOORD1;
#endif*/
UNITY_VERTEX_OUTPUT_STEREO
};
struct AttributesA
{
uint vertexID : SV_VertexID;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
VaryingsA vertHD(AttributesA input)
{
VaryingsA output;
UNITY_SETUP_INSTANCE_ID(input);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
output.positionCS = GetFullScreenTriangleVertexPositionA(input.vertexID);
output.uv = GetFullScreenTriangleTexCoord(input.vertexID);//output.texcoord = GetFullScreenTriangleTexCoord(input.vertexID);
/*#if UNITY_UV_STARTS_AT_TOP
o.uv2 = = GetFullScreenTriangleTexCoord(input.vertexID);
if (_MainTex_TexelSize.y < 0.0)
o.uv.y = 1.0 - o.uv.y;
#endif*/
return output;
}
#define PI 3.147159265
ENDHLSL
//ENDCG
SubShader
{
ZTest Off
Cull Off
ZWrite Off
Fog { Mode off }
Pass //0
{
HLSLPROGRAM//CGPROGRAM
#pragma vertex vert//vert
#pragma fragment frag
float4x4 CameraToWorld;
TEXTURE2D_ARRAY(_CameraGBufferTexture2);//sampler2D _CameraGBufferTexture2;//v1.0i
//v0.4
sampler2D _GBuffer0;
sampler2D _GBuffer1;
sampler2D _GBuffer2;
sampler2D _GBuffer3;
int FrameSwitch;
int HalfResolution;
sampler3D SEGIVolumeTexture1;
sampler2D NoiseTexture;
//HDRP
sampler2D currentNormals;
TEXTURE2D_ARRAY(_GBufferTexture0);
TEXTURE2D_ARRAY(_GBufferTexture1);
TEXTURE2D_ARRAY(_GBufferTexture2);
TEXTURE2D_ARRAY(_GBufferTexture3);
TEXTURE2D_ARRAY(_GBufferTexture4);
TEXTURE2D_ARRAY(_GBufferTexture5);
//TEXTURE2D_ARRAY(_GBuffer0);
//HDRP
float4 CameraPosition;
float4 ReflectControl;
//v2.1.34 - COLOR - DITHER - v0.7
float4 ditherControl;// = float4(0, 1, 1, 1); //enable Dirther - Trace Directions Divider - Trece Steps Devider - Reflections Steps Divider
//v1.2
float smoothNormals;
float4 frag(v2f input) : SV_Target
{
#if UNITY_UV_STARTS_AT_TOP
float2 coord = input.uv.xy; //float2 coord = input.uv2.xy;
#else
float2 coord = input.uv.xy;
#endif
//coord.y = 1 - coord.y;
//Get view space position and view vector
float4 viewSpacePosition = GetViewSpacePosition(coord);
float3 viewVector = normalize(viewSpacePosition.xyz);
//float depth = GetDepthTexture(coord);
//return float4(depth, depth, depth, 1)*depth*depth*depth*depth*depth*depth*depth*depth*depth*depth*depth*depth;
//return float4(viewSpacePosition);
//Get voxel space position
float4 voxelSpacePosition = mul(CameraToWorld, viewSpacePosition);
voxelSpacePosition = mul(SEGIWorldToVoxel, voxelSpacePosition);
voxelSpacePosition = mul(SEGIVoxelProjection, voxelSpacePosition);
//voxelSpacePosition.xyz += 0.8*0.1*float3(0,0,5);
//voxelSpacePosition.xyz += -0.1*_WorldSpaceCameraPos.xyz*0.1*float3(5, 5, 5);
// voxelSpacePosition.xyz -= 0.05*_WorldSpaceCameraPos.xyz;
// voxelSpacePosition.xyz -= 0.15*_WorldSpaceCameraPos.xyz;
// voxelSpacePosition.xyz -= 0.05*ReflectControl.x*_WorldSpaceCameraPos.xyz;
voxelSpacePosition.xyz = voxelSpacePosition.xyz * 0.5 + 0.5;
//voxelSpacePosition.xyz += _WorldSpaceCameraPos.xyz;//// HDRP Camera Relative
//voxelSpacePosition.xyz -= 0.05*ReflectControl.x*_WorldSpaceCameraPos.xyz;
//Prepare for cone trace
float2 dither = rand(coord + (float)FrameSwitch * 0.011734);
//v0.4
//float3 worldNormal = pow((tex2D(_CameraDepthNormalsTexture, coord).rgb * 2.0 - 1.0),1); //normalize(tex2D(_CameraGBufferTexture2, coord).rgb * 2.0 - 1.0);
// float3 worldNormalA = pow((tex2D(_CameraDepthNormalsTexture, coord).rgb* 2.0 - 1.0 ), 1);
// float3 worldNormalA1 = pow((tex2D(_CameraDepthNormalsTexture, coord).rgb), 1);
// float3 worldNormalB = pow((tex2D(_GBuffer2, coord).rgb * 2.0 - 1.0), 1);
//float depth = LinearEyeDepth(tex2D(_CameraDepthTexture, coord).x);
float depthA = 1;
/* if (worldNormalA1.r == 0 && worldNormalA1.g == 0 && worldNormalA1.b == 0) {
depthA = 0;
}*/
//HDRP
// float4 testOut = LOAD_TEXTURE2D_ARRAY(_GBufferTexture0, coord, 0); //v0.1 - LOAD_TEXTURE2D_ARRAY(_GBufferTexture2, coord, 0); //v0.1
//v1.2
float3 worldNormalA11 = tex2D(currentNormals, coord).rgb;// (LOAD_TEXTURE2D_ARRAY(_CameraGBufferTexture2, coord, 0).rgb * 1); //v1.0i
//v1.2
if (smoothNormals != 1){//if (ditherControl.x != 0) {
worldNormalA11 = tex2D(currentNormals, coord).rgb + (LOAD_TEXTURE2D_ARRAY(_CameraGBufferTexture2, coord, 0).rgb * 2.0 - 1.0);
}
if (worldNormalA11.r == 0 && worldNormalA11.g == 0 && worldNormalA11.b == 0) {
depthA = 0;
}
//HDRP
//float4 viewSpacePosition = GetViewSpacePosition(coord);
//float3 viewVector = normalize(viewSpacePosition.xyz);
// float4 worldViewVector = mul(CameraToWorld, float4(viewVector.xyz, 0.0));
// float4 voxelCameraPosition = mul(SEGIWorldToVoxel, float4(CameraPosition.xyz, 1.0));
// voxelCameraPosition = mul(SEGIVoxelProjection, voxelCameraPosition);
//voxelCameraPosition.xyz -= 0.05*_WorldSpaceCameraPos.xyz;
// voxelCameraPosition.xyz = voxelCameraPosition.xyz * 0.5 + 0.5;
// float4 resultA = VisualConeTrace4(voxelCameraPosition.xyz, worldViewVector.xyz, depthA);
//return resultA;
//v1.2
float3 worldNormal = worldNormalA11 * depthA;// normalize(tex2D(_CameraGBufferTexture2, coord).rgb * 2.0 - 1.0)*worldNormalB*(depthA)+worldNormalA;// pow((tex2D(_GBuffer2, coord).rgb * 2.0 - 1.0), 1) * worldNormalA;
//return float4(depth, depth, depth, depth);
//v2.1.34 - COLOR - v0.7
//float3 fakeGI = saturate(dot(worldNormal, float3(0, 1, 0)) * 0.5 + 0.5) * SEGISkyColor.rgb * 5.0;
//float4 ditherControl = float4(1, 1, 1, 1);
//if (mod(voxelSpacePosition.z, 0.01) < 0.002 && mod(voxelSpacePosition.z, 0.01) > 0.001) {
if (ditherControl.x != 0 && dither.x > 0.001 * ditherControl.x) {
//if (mod(input.uv.x, 0.1) == 0.004) {
//discard;
//float3 worldNormalB = pow((tex2D(_GBuffer2, coord).rgb * 2.0 - 1.0), 1);
//return float4(fakeGI *1, 1.0);
TraceDirections = TraceDirections / ditherControl.y;// *saturate(dither.x);
TraceSteps = TraceSteps / ditherControl.z;
}
float3 voxelOrigin = voxelSpacePosition.xyz + worldNormal.xyz * 0.003 * ConeTraceBias * 1.25 / SEGIVoxelScaleFactor;
//voxelSpacePosition.xyz + worldNormal.xyz * 0.003 * ConeTraceBias * 1.25 / SEGIVoxelScaleFactor;
//return float4(worldNormal, 1);
float3 gi = float3(0.0, 0.0, 0.0);
float4 traceResult = float4(0,0,0,0);
const float phi = 1.618033988;
const float gAngle = phi * PI * 1.0;
//Get blue noise
float2 noiseCoord = (input.uv.xy * _MainTex_TexelSize.zw) / (64.0).xx;
float blueNoise = tex2Dlod(NoiseTexture, float4(noiseCoord, 0.0, 0.0)).x;
//return float4(blueNoise, blueNoise, blueNoise);
//Trace GI cones
int numSamples = TraceDirections;
for (int i = 0; i < numSamples; i++)
{
float fi = (float)i + blueNoise * StochasticSampling;
float fiN = fi / numSamples;
float longitude = gAngle * fi;
float latitude = asin(fiN * 2.0 - 1.0);
float3 kernel;
kernel.x = cos(latitude) * cos(longitude);
kernel.z = cos(latitude) * sin(longitude);
kernel.y = sin(latitude);
kernel = normalize(kernel + worldNormal.xyz * 1.0);
//traceResult += ConeTraceA(voxelOrigin.xyz, kernel.xyz, worldNormal.xyz, coord, dither.y, TraceSteps, ConeSize, 1.0, 1.0);
traceResult += ConeTraceA(voxelOrigin.xyz, kernel.xyz, worldNormal.xyz, coord, dither.y, TraceSteps, ConeSize, 1.0, 1.0);
}
traceResult /= numSamples;
if (SEGISkyColor.a == 0) {
gi = traceResult.rgb * 80.0; //HDRP
// gi = traceResult.rgb *traceResult.rgb *traceResult.rgb * 11360.0;
float fadeout = saturate((distance(voxelSpacePosition.xyz, float3(0.5, 0.5, 0.5)) - 0.5f) * 5.0);
float3 fakeGI = saturate(dot(worldNormal, float3(0, 1, 0)) * 0.5 + 0.5) * SEGISkyColor.rgb * 5.0;
}
else {
gi = traceResult.rgb * 220.0*SEGISkyColor.a; //HDRP
float fadeout = saturate((distance(voxelSpacePosition.xyz, float3(0.5, 0.5, 0.5)) - 0.5f) * 5.0);
float3 fakeGI = saturate(dot(worldNormal, float3(0, 1, 0)) * 0.5 + 0.5) * SEGISkyColor.rgb * 5.0;
gi.rgb = lerp(gi.rgb, fakeGI, fadeout + 0.99);
gi *= 0.75 + (float)HalfResolution * 0.25;
//v1.0j
if (fadeout + 0.99 > 1) {
gi = saturate(gi);
}
}
// gi.rgb = lerp(gi.rgb, fakeGI, fadeout);
// gi *= 0.75 + (float)HalfResolution * 0.25;
//HDRP
//currentNormals
//float3 worldNormalA11 = pow((tex2D(currentNormals, coord).rgb), 1);
//return float4(worldNormalA11, 1);
//return float4(1,0,0, 1.0);
//return float4(gi + gi*resultA.rgb, 1.0);
//return float4(gi + gi *resultA*depthA, 1.0);
//return float4(gi + gi * resultA*2, 1.0);
return float4(gi, 1.0);
}
ENDHLSL//ENDCG
}
Pass //1 Bilateral Blur
{
HLSLPROGRAM//CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float2 Kernel;
float DepthTolerance;
sampler2D DepthNormalsLow;
sampler2D _CameraGBufferTexture2;
sampler2D DepthLow;
int SourceScale;
//v0.4
sampler2D _GBuffer0;
sampler2D _GBuffer1;
sampler2D _GBuffer2;
sampler2D _GBuffer3;
//v1.2
float smoothNormals;
float4 frag(v2f input) : COLOR0
{
float4 blurred = float4(0.0, 0.0, 0.0, 0.0);
float validWeights = 0.0;
//HDRP2
//v1.5
//float depth = LinearEyeDepth(tex2D(_CameraDepthTexture, input.uv.xy).x);
float depth = LinearEyeDepth(GetDepthTexture(input.uv.xy).x);
//float depth = LinearEyeDepth(LOAD_TEXTURE2D_X_LOD(_CameraDepthTexture, float2(input.uv.x, input.uv.y), 0).x);
//uint2 positionSS = input.uv* float2(DepthPyramidScale.x, DepthPyramidScale.y) + float2(DepthPyramidScale.z, DepthPyramidScale.w);// * _ScreenSize.xy;
//float depth = LinearEyeDepth(LOAD_TEXTURE2D(_CameraDepthTexture, 0, positionSS).x);
//float depth = LOAD_TEXTURE2D_X_LOD(_CameraDepthTexture, positionSS, 0).r;
//v0.4
//float3 worldNormal = pow((tex2D(_CameraDepthNormalsTexture, coord).rgb * 2.0 - 1.0),1); //normalize(tex2D(_CameraGBufferTexture2, coord).rgb * 2.0 - 1.0);
float3 worldNormalA = pow((tex2D(_CameraDepthNormalsTexture, input.uv.xy).rgb* 2.0 - 1.0), 1);
float3 worldNormalA1 = pow((tex2D(_CameraDepthNormalsTexture, input.uv.xy).rgb), 1);
float3 worldNormalB = pow((tex2D(_GBuffer2, input.uv.xy).rgb * 2.0 - 1.0), 1);
//float depth = LinearEyeDepth(tex2D(_CameraDepthTexture, coord).x);
float depthA = 1;
if (worldNormalA1.r == 0 && worldNormalA1.g == 0 && worldNormalA1.b == 0) {
depthA = 0;
}
/////////float3 worldNormal = normalize(tex2D(_CameraGBufferTexture2, coord).rgb * 2.0 - 1.0)*worldNormalB*(depthA)+worldNormalA;//
//half3 normal = normalize(tex2D(_CameraGBufferTexture2, input.uv.xy).rgb * 2.0 - 1.0);
//half3 normal = normalize(tex2D(_CameraGBufferTexture2, input.uv.xy).rgb * 0.2 - 0.1 + worldNormalA)*(worldNormalB*(depthA)+0.88);
//v1.2
half3 normal = tex2D(_CameraGBufferTexture2, input.uv.xy).rgb * depthA;
//v1.2
if (smoothNormals != 1){//if (ditherControl.x != 0) {
normal = normalize(tex2D(_CameraGBufferTexture2, input.uv.xy).rgb * 0.2 - 0.1)*(worldNormalB*(depthA + worldNormalA) + 0.88);
}
float thresh = 0.012; //v1.2b was 0.26
float3 viewPosition = GetViewSpacePosition(input.uv.xy).xyz;
float3 viewVector = normalize(viewPosition);
float NdotV = 1.0 / (saturate(dot(-viewVector, normal.xyz)) + 0.1);
thresh *= 1.0 + NdotV * 2.0;
for (int i = -4; i <= 4; i++)
{
float2 offs = Kernel.xy * (i) * _MainTex_TexelSize.xy * 1.0;
//HDRP2
//float sampleDepth = LinearEyeDepth(tex2Dlod(_CameraDepthTexture, float4(input.uv.xy + offs.xy * 1, 0, 0)).x);
float sampleDepth = LinearEyeDepth(GetDepthTexture(input.uv.xy + offs.xy).x);
//float sampleDepth = LinearEyeDepth(LOAD_TEXTURE2D_X_LOD(_CameraDepthTexture, float2(input.uv.xy + offs.xy * 1), 0).x);
//uint2 positionSS = float2(input.uv.xy + offs.xy * 1)* float2(DepthPyramidScale.x, DepthPyramidScale.y) + float2(DepthPyramidScale.z, DepthPyramidScale.w);// * _ScreenSize.xy;
//float sampleDepth = LinearEyeDepth(LOAD_TEXTURE2D(_CameraDepthTexture, 0, positionSS).x);
//float sampleDepth = LOAD_TEXTURE2D_X_LOD(_CameraDepthTexture, positionSS, 0).r;
//v1.2
half3 sampleNormal = tex2Dlod(_CameraGBufferTexture2, float4(input.uv.xy + offs.xy * 1, 0, 0)).rgb;//
//v1.2
if (smoothNormals != 1) {
sampleNormal = normalize(tex2Dlod(_CameraGBufferTexture2, float4(input.uv.xy + offs.xy * 1, 0, 0)).rgb * 2.0 - 1.0);
}
float weight = saturate(1.0 - abs(depth - sampleDepth) / thresh);
weight *= pow(saturate(dot(sampleNormal, normal)), 4.0); //v1.2b was 24
float4 blurSample = tex2Dlod(_MainTex, float4(input.uv.xy + offs.xy, 0, 0)).rgba;
blurred += blurSample * weight;
validWeights += weight;
}
blurred /= validWeights + 0.001;
return blurred;
}
ENDHLSL//ENDCG
}
Pass //2 Blend with scene
{
HLSLPROGRAM//CGPROGRAM
#pragma vertex vert //vertHD //v1.1
#pragma fragment frag
sampler2D _CameraGBufferTexture2;
sampler2D _CameraGBufferTexture1;
sampler2D GITexture;
sampler2D Reflections;
//v0.4
//sampler2D _GBuffer0;
sampler2D _GBuffer1;
//sampler2D _GBuffer2;
sampler2D _GBuffer3;
float4x4 CameraToWorld;
int DoReflections;
//v0.4
float contrastA;
float4 ReflectControl;
int HalfResolution;
//HDRP
sampler2D currentNormals;
//sampler2D _MainTexSKY;
UNITY_DECLARE_TEX2DARRAY(_MainTexSKY);
//uniform half4 _MainTex_TexelSize;
TEXTURE2D_ARRAY(_GBufferTexture0);
TEXTURE2D_ARRAY(_GBufferTexture1);
TEXTURE2D_ARRAY(_GBufferTexture2);
TEXTURE2D_ARRAY(_GBufferTexture3);
TEXTURE2D_ARRAY(_GBufferTexture4);
TEXTURE2D_ARRAY(_GBufferTexture5);
TEXTURE2D_ARRAY(_GBufferTexture6);
TEXTURE2D_ARRAY(_GBufferTexture7);
float4 frag(v2f input) : COLOR0
{
#if UNITY_UV_STARTS_AT_TOP
float2 coord = input.uv.xy;
#else
float2 coord = input.uv.xy;
#endif
//v0.4
float4 albedoSEGI = tex2D(_GBuffer3, input.uv.xy);
float4 albedoSEGIA = tex2D(_GBuffer1, input.uv.xy);
//albedoSEGI = LOAD_TEXTURE2D_ARRAY(_GBufferTexture7, coord, 0);
//albedoSEGIA = LOAD_TEXTURE2D_ARRAY(_GBufferTexture2, coord, 0);
//float4 albedoTex = (1-contrastA)*albedoSEGI +(contrastA * albedoSEGIA*albedoSEGI);// tex2D(_CameraGBufferTexture0, input.uv.xy);
float4 albedoTex = (1 - contrastA)*albedoSEGI*albedoSEGI + (contrastA *albedoSEGIA*albedoSEGI);//v1.2f
float4 albedoSEGIB = LOAD_TEXTURE2D_ARRAY(_GBufferTexture0, coord*_ScreenParams.xy, 0);//HDRP//v1.2f
albedoTex = albedoTex + albedoTex * albedoSEGIB;//v1.2f
float3 albedo = albedoTex.rgb;
float3 gi = tex2D(GITexture, input.uv.xy).rgb;
//HDRP
//float3 scene = tex2D(_MainTexSKY, input.uv.xy).rgb;
float3 scene = UNITY_SAMPLE_TEX2DARRAY(_MainTexSKY, input.uv).rgb;
float3 reflections = tex2D(Reflections, input.uv.xy).rgb;
//float3 result = scene + gi * albedoTex.a * albedoTex.rgb;
float3 result = scene + albedo * gi * albedoTex.a * albedoTex.rgb * 10;//v1.2f
if (DoReflections > 0)
{
//v0.4
//float3 worldNormal = pow((tex2D(_CameraDepthNormalsTexture, coord).rgb * 2.0 - 1.0),1); //normalize(tex2D(_CameraGBufferTexture2, coord).rgb * 2.0 - 1.0);
// float3 worldNormalA = pow((tex2D(_CameraDepthNormalsTexture, input.uv.xy).rgb* 2.0 - 1.0), 1);
// float3 worldNormalA1 = pow((tex2D(_CameraDepthNormalsTexture, input.uv.xy).rgb), 1);
// float3 worldNormalB = pow((tex2D(_GBuffer2, input.uv.xy).rgb * 2.0 - 1.0), 1);
//float depth = LinearEyeDepth(tex2D(_CameraDepthTexture, coord).x);
float depthA = 1;
// if (worldNormalA1.r == 0 && worldNormalA1.g == 0 && worldNormalA1.b == 0) {
// depthA = 0;
// }
/////////float3 worldNormal = normalize(tex2D(_CameraGBufferTexture2, coord).rgb * 2.0 - 1.0)*worldNormalB*(depthA)+worldNormalA;//
//HDRP
//float4 testOut = LOAD_TEXTURE2D_ARRAY(_GBufferTexture2, coord, 0);
float3 worldNormalA11 = pow((tex2D(currentNormals, coord).rgb), 1);
if (worldNormalA11.r == 0 && worldNormalA11.g == 0 && worldNormalA11.b == 0) {
depthA = 0;
}
float4 viewSpacePosition = GetViewSpacePosition(coord);
float3 viewVector = normalize(viewSpacePosition.xyz);
float4 worldViewVector = mul(CameraToWorld, float4(viewVector.xyz, 0.0));
//worldViewVector.xyz -= 1*_WorldSpaceCameraPos.xyz;//// HDRP Camera Relative 2
//worldViewVector.xyz -= 0.05*_WorldSpaceCameraPos.xyz;
//v0.4
//float4 spec = tex2D(_CameraGBufferTexture1, coord);
//float4 spec = tex2D(_GBuffer0, coord)* ReflectControl.y*(depthA)+ tex2D(_GBuffer1, coord)* ReflectControl.z*(depthA); //tex2D(_GBuffer1, coord); //v0.7
float4 spec = LOAD_TEXTURE2D_ARRAY(_GBufferTexture1, coord, 0);//HDRP
float smoothness = spec.a;
float3 specularColor = spec.rgb * ReflectControl.x;
//v0.4
//float3 worldNormal = normalize(tex2D(_CameraGBufferTexture2, coord).rgb * 2.0 - 1.0)*worldNormalB*(depthA)+worldNormalA;//normalize(tex2D(_CameraGBufferTexture2, coord).rgb * 2.0 - 1.0);
float3 worldNormal = worldNormalA11;// worldNormalB*(depthA)+worldNormalA1; //HDRP
float3 reflectionKernel = reflect(worldViewVector.xyz, worldNormal);
float3 fresnel = pow(saturate(dot(worldViewVector.xyz, reflectionKernel.xyz)) * (smoothness * 0.5 + 0.5), 5.0);
fresnel = lerp(fresnel, (1.0).xxx, specularColor.rgb);
fresnel *= saturate(smoothness * 4.0);
//result = lerp(result, reflections, fresnel);
//result = lerp(result, reflections, 0.2 + ReflectControl.w*0.01); // HDRP
result = lerp(result, reflections, ReflectControl.y*(0.2 + ReflectControl.w*0.01) + (1-ReflectControl.y)*(fresnel)); // HDRP
}
return float4(result, 1.0);
}
ENDHLSL//ENDCG
}
Pass //3 Temporal blend (with unity motion vectors)
{
HLSLPROGRAM//CGPROGRAM
#pragma vertex vert //vertHD //v1.1
#pragma fragment frag
sampler2D GITexture;
sampler2D PreviousDepth;
sampler2D CurrentDepth;
sampler2D PreviousLocalWorldPos;
//HDRP
//https://www.reddit.com/r/Unity3D/comments/cfob58/error_assigning_texarray_texture_to_2d_texture/
//sampler2D _MainTexARRAY;//HDRP
UNITY_DECLARE_TEX2DARRAY(_MainTexARRAY);
float4 _MainTexARRAY_ST;
uniform half4 _MainTexARRAY_TexelSize;
float4 CameraPosition;
float4 CameraPositionPrev;
float4x4 ProjectionPrev;
float4x4 ProjectionPrevInverse;
float4x4 WorldToCameraPrev;
float4x4 CameraToWorldPrev;
float4x4 CameraToWorld;
float DeltaTime;
float BlendWeight;
float4 frag(v2f input) : COLOR0
{
//float3 gi = tex2Dlod(_MainTexARRAY, float4(input.uv.xy,0,0)).rgb;//tex2D(_MainTex, input.uv.xy).rgb;
//fixed4 t = UNITY_SAMPLE_TEX2DARRAY(_MainTex, i.uv);
float3 gi = UNITY_SAMPLE_TEX2DARRAY(_MainTexARRAY, input.uv.xyy).rgb;//
float depth = GetDepthTexture(input.uv.xy);
float4 currentPos = float4(input.uv.x * 2.0 - 1.0, input.uv.y * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
float4 fragpos = mul(ProjectionMatrixInverse, currentPos);
fragpos = mul(CameraToWorld, fragpos);
fragpos /= fragpos.w;
float4 thisWorldPosition = fragpos;
float2 motionVectors = tex2Dlod(_CameraMotionVectorsTexture, float4(input.uv.xy, 0.0, 0.0)).xy;
float2 reprojCoord = input.uv.xy - motionVectors.xy;
float prevDepth = (tex2Dlod(PreviousDepth, float4(reprojCoord + _MainTex_TexelSize.xy * 0.0, 0.0, 0.0)).x);
#if defined(UNITY_REVERSED_Z)
prevDepth = 1.0 - prevDepth;
#endif
float4 previousWorldPosition = mul(ProjectionPrevInverse, float4(reprojCoord.xy * 2.0 - 1.0, prevDepth * 2.0 - 1.0, 1.0));
previousWorldPosition = mul(CameraToWorldPrev, previousWorldPosition);
previousWorldPosition /= previousWorldPosition.w;
float blendWeight = BlendWeight;
float posSimilarity = saturate(1.0 - distance(previousWorldPosition.xyz, thisWorldPosition.xyz) * 1.0);
blendWeight = lerp(1.0, blendWeight, posSimilarity);
float3 minPrev = float3(10000, 10000, 10000);
float3 maxPrev = float3(0, 0, 0);
/*float3 s0 = tex2Dlod(_MainTex, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(0.5, 0.5), 0, 0)).rgb;*/
float3 s0 = UNITY_SAMPLE_TEX2DARRAY(_MainTexARRAY, float4(input.uv.xy + _MainTexARRAY_TexelSize.xy * float2(0.5, 0.5), 0, 0)).rgb;
minPrev = s0;
maxPrev = s0;
//s0 = tex2Dlod(_MainTex, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(0.5, -0.5), 0, 0)).rgb;
s0 = UNITY_SAMPLE_TEX2DARRAY(_MainTexARRAY, float4(input.uv.xy + _MainTexARRAY_TexelSize.xy * float2(0.5, -0.5), 0, 0)).rgb;
minPrev = min(minPrev, s0);
maxPrev = max(maxPrev, s0);
//s0 = tex2Dlod(_MainTex, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(-0.5, 0.5), 0, 0)).rgb;
s0 = UNITY_SAMPLE_TEX2DARRAY(_MainTexARRAY, float4(input.uv.xy + _MainTexARRAY_TexelSize.xy * float2(-0.5, 0.5), 0, 0)).rgb;
minPrev = min(minPrev, s0);
maxPrev = max(maxPrev, s0);
//s0 = tex2Dlod(_MainTex, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(-0.5, -0.5), 0, 0)).rgb;
s0 = UNITY_SAMPLE_TEX2DARRAY(_MainTexARRAY, float4(input.uv.xy + _MainTexARRAY_TexelSize.xy * float2(-0.5, -0.5), 0, 0)).rgb;
minPrev = min(minPrev, s0);
maxPrev = max(maxPrev, s0);
float3 prevGI = tex2Dlod(PreviousGITexture, float4(reprojCoord, 0.0, 0.0)).rgb;
prevGI = lerp(prevGI, clamp(prevGI, minPrev, maxPrev), 0.25);
gi = lerp(prevGI, gi, float3(blendWeight, blendWeight, blendWeight));
float3 result = gi;
return float4(result, 1.0);
}
ENDHLSL//ENDCG
}
Pass //4 Specular/reflections trace
{
ZTest Always
HLSLPROGRAM//CGPROGRAM
#pragma vertex vert //vertHD //v1.1
#pragma fragment frag
float4x4 CameraToWorld;
sampler2D _CameraGBufferTexture1;
sampler2D _CameraGBufferTexture2;
//v0.4
sampler2D _GBuffer0;
sampler2D _GBuffer1;
sampler2D _GBuffer2;
sampler2D _GBuffer3;
sampler3D SEGIVolumeTexture1;
int FrameSwitch;
//HDRP
sampler2D currentNormals;
TEXTURE2D_ARRAY(_GBufferTexture0);
TEXTURE2D_ARRAY(_GBufferTexture1);
TEXTURE2D_ARRAY(_GBufferTexture2);
TEXTURE2D_ARRAY(_GBufferTexture3);
TEXTURE2D_ARRAY(_GBufferTexture4);
TEXTURE2D_ARRAY(_GBufferTexture5);
TEXTURE2D_ARRAY(_GBufferTexture6);
TEXTURE2D_ARRAY(_GBufferTexture7);
float4 ReflectControl;
//v2.1.34 - COLOR - DITHER - v0.7
float4 ditherControl;// = float4(0, 1, 1, 1); //enable Dirther - Trace Directions Divider - Trece Steps Devider - Reflections Steps Divider
float4 frag(v2f input) : SV_Target
{
#if UNITY_UV_STARTS_AT_TOP
float2 coord = input.uv.xy;
#else
float2 coord = input.uv.xy;
#endif
//float4 spec = tex2D(_GBuffer0, coord);
//float4 spec = tex2D(_CameraGBufferTexture1, coord);
float4 spec = LOAD_TEXTURE2D_ARRAY(_GBufferTexture1, coord*_ScreenParams.xy,0);// *float2(1119,614), 0); //HDRP !!! _ScreenSize.xy
float4 viewSpacePosition = GetViewSpacePosition(coord);
float3 viewVector = normalize(viewSpacePosition.xyz);
float4 worldViewVector = mul(CameraToWorld, float4(viewVector.xyz, 0.0));
float4 voxelSpacePosition = mul(CameraToWorld, viewSpacePosition);
float3 worldPosition = voxelSpacePosition.xyz;
voxelSpacePosition = mul(SEGIWorldToVoxel, voxelSpacePosition);
voxelSpacePosition = mul(SEGIVoxelProjection, voxelSpacePosition);
//voxelSpacePosition.xyz -= 1*_WorldSpaceCameraPos.xyz;//// HDRP Camera Relative 2
// voxelSpacePosition.xyz -= 0.05*_WorldSpaceCameraPos.xyz;
voxelSpacePosition.xyz = voxelSpacePosition.xyz * 0.5 + 0.5;
//v0.4
//float3 worldNormal = pow((tex2D(_CameraDepthNormalsTexture, coord).rgb * 2.0 - 1.0),1); //normalize(tex2D(_CameraGBufferTexture2, coord).rgb * 2.0 - 1.0);
//float3 worldNormalA = pow((tex2D(_CameraDepthNormalsTexture, input.uv.xy).rgb* 2.0 - 1.0), 1);
float3 worldNormalA1 = pow((tex2D(_CameraDepthNormalsTexture, input.uv.xy).rgb), 1);
//float3 worldNormalB = pow((tex2D(_GBuffer2, input.uv.xy).rgb * 2.0 - 1.0), 1);
//float depth = LinearEyeDepth(tex2D(_CameraDepthTexture, coord).x);
float depthA = 1;
if (worldNormalA1.r == 0 && worldNormalA1.g == 0 && worldNormalA1.b == 0) {
depthA = 0;
}
/////////float3 worldNormal = normalize(tex2D(_CameraGBufferTexture2, coord).rgb * 2.0 - 1.0)*worldNormalB*(depthA)+worldNormalA;//
//v0.4
//float3 worldNormal = normalize(tex2D(_CameraGBufferTexture2, coord).rgb * 2.0 - 1.0)*worldNormalB*(depthA)+worldNormalA;
float3 worldNormalA11 = pow((tex2D(currentNormals, coord).rgb), 1);
float3 worldNormal = worldNormalA11 * 1;// LOAD_TEXTURE2D_ARRAY(_GBufferTexture2, coord, 0); //HDRP2
// worldNormal = normalize(tex2D(_CameraGBufferTexture2, coord).rgb * 2.0 - 1.0);
float3 voxelOrigin = voxelSpacePosition.xyz + worldNormal.xyz * 0.006 * ConeTraceBias * 1.25 / SEGIVoxelScaleFactor;
float2 dither = rand(coord + (float)FrameSwitch * 0.11734);
//v2.1.34 - COLOR - v0.7
if (ditherControl.x != 0 && dither.x > 0.001 * ditherControl.x) {
ReflectionSteps = ReflectionSteps / ditherControl.w; //float2 dither = rand(coord + (float)FrameSwitch * 0.011734);
}
float smoothness = spec.a * 0.5;
float3 specularColor = spec.rgb;
float4 reflection = (0.0).xxxx;
float3 reflectionKernel = reflect(worldViewVector.xyz, worldNormal);
//float3 fresnel = pow(saturate(dot(worldViewVector.xyz, reflectionKernel.xyz)) * (smoothness * 0.5 + 0.5), 5.0);
//fresnel = lerp(fresnel, (1.0).xxx, specularColor.rgb);
voxelOrigin += worldNormal.xyz * 0.002 * 1.25 / SEGIVoxelScaleFactor;
reflection = SpecularConeTrace(voxelOrigin.xyz, reflectionKernel.xyz, worldNormal.xyz, smoothness, coord, dither.x, ReflectControl.z);
float3 skyReflection = (reflection.a * 1.0 * SEGISkyColor);
reflection.rgb = reflection.rgb * 0.7 + skyReflection.rgb * 2.4015 * SkyReflectionIntensity;
return float4(reflection.rgb, 1.0);
}
ENDHLSL//ENDCG
}
Pass //5 Get camera depth texture
{
HLSLPROGRAM//CGPROGRAM
#pragma vertex vert //vertHD //v1.1
#pragma fragment frag
float4 frag(v2f input) : COLOR0
{
float2 coord = input.uv.xy;
//HDRP2
//v1.5
//float4 tex = tex2D(_CameraDepthTexture, coord);
float4 tex = GetDepthTexture(coord);
return tex;
//float4 depth = LOAD_TEXTURE2D_X_LOD(_CameraDepthTexture, float2(coord.x, coord.y), 0);
//return depth;
//float2 positionSS = coord * _ScreenSize.zw;// float2(DepthPyramidScale.x, DepthPyramidScale.y) + float2(DepthPyramidScale.z, DepthPyramidScale.w);// *_ScreenSize.xy;
//float2 positionSS = coord*float2(DepthPyramidScale.x, DepthPyramidScale.y) + float2(DepthPyramidScale.z, DepthPyramidScale.w);
//float4 depth = LinearEyeDepth(LOAD_TEXTURE2D(_CameraDepthTexture, 0, positionSS));
//float4 depth = LinearEyeDepth(LOAD_TEXTURE2D_X_LOD(_CameraDepthTexture, positionSS, 0));
//return depth;// float4(0.5, 1, 1, 1);
}
ENDHLSL//ENDCG
}
Pass //6 Get camera normals texture
{
HLSLPROGRAM//CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4 frag(v2f input) : COLOR0
{
float2 coord = input.uv.xy;
float4 tex = tex2D(_CameraDepthNormalsTexture, coord);
return tex;
}
ENDHLSL//ENDCG
}
Pass //7 Visualize GI
{
HLSLPROGRAM//CGPROGRAM
#pragma vertex vert
#pragma fragment frag
sampler2D GITexture;
float4 frag(v2f input) : COLOR0
{
float4 albedoTex = tex2D(_CameraGBufferTexture0, input.uv.xy);
float3 albedo = albedoTex.rgb;
float3 gi = tex2D(GITexture, input.uv.xy).rgb;
return float4(gi, 1.0);
}
ENDHLSL//ENDCG
}
Pass //8 Write black
{
HLSLPROGRAM//CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4 frag(v2f input) : COLOR0
{
return float4(0.0, 0.0, 0.0, 1.0);
}
ENDHLSL//ENDCG
}
Pass //9 Visualize slice of GI Volume (CURRENTLY UNUSED)
{
HLSLPROGRAM//CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float LayerToVisualize;
int MipLevelToVisualize;
sampler3D SEGIVolumeTexture1;
float4 frag(v2f input) : COLOR0
{
return float4(tex3D(SEGIVolumeTexture1, float3(input.uv.xy, LayerToVisualize)).rgb, 1.0);
}
ENDHLSL//ENDCG
}
Pass //10 Visualize voxels (trace through GI volumes)
{
ZTest Always
HLSLPROGRAM//CGPROGRAM
#pragma vertex vert //vertHD //v1.1//vert //HDRP
#pragma fragment frag
float4x4 CameraToWorld;
//sampler2D _GBufferTexture0;// _GBuffer0;//outGBuffer0;//_GBufferTexture0;
//sampler2D _GBufferTexture1;
//sampler2D _GBufferTexture2;
//sampler2D _GBufferTexture3;
/* #define LOAD_TEXTURE2D_ARRAY(textureName, unCoord2, index) textureName.Load(int4(unCoord2, index, 0))
#define TEXTURE2D_ARRAY(textureName) Texture2DArray textureName*/
TEXTURE2D_ARRAY(_GBufferTexture0);
TEXTURE2D_ARRAY(_GBufferTexture1);
TEXTURE2D_ARRAY(_GBufferTexture2);
TEXTURE2D_ARRAY(_GBufferTexture3);
TEXTURE2D_ARRAY(_GBuffer0);
//sampler2D _CameraGBufferTexture2;
float4 CameraPosition;
float4 frag(v2f input) : SV_Target
{
#if UNITY_UV_STARTS_AT_TOP
float2 coord = input.uv.xy; //float2 coord = input.uv2.xy;
#else
float2 coord = input.uv.xy;
#endif
float4 viewSpacePosition = GetViewSpacePosition(coord);
float3 viewVector = normalize(viewSpacePosition.xyz);
float4 worldViewVector = mul(CameraToWorld, float4(viewVector.xyz, 0.0));
float4 voxelCameraPosition = mul(SEGIWorldToVoxel, float4(CameraPosition.xyz, 1.0));
voxelCameraPosition = mul(SEGIVoxelProjection, voxelCameraPosition);
//voxelCameraPosition.xyz -= 0.05*_WorldSpaceCameraPos.xyz;
voxelCameraPosition.xyz = voxelCameraPosition.xyz * 0.5 + 0.5;
float4 result = VisualConeTrace(voxelCameraPosition.xyz, worldViewVector.xyz);
//https://www.reddit.com/r/Unity3D/comments/cfob58/error_assigning_texarray_texture_to_2d_texture/
//return float4(1,0,0, 1.0);
//https://xibanya.github.io/URPShaderViewer/Library/Core/API/D3D11.html
//#define LOAD_TEXTURE2D_ARRAY(textureName, unCoord2, index) textureName.Load(int4(unCoord2, index, 0))
//#define LOAD_TEXTURE2D_ARRAY_MSAA(textureName, unCoord2, index, sampleIndex) textureName.Load(int3(unCoord2, index), sampleIndex)
//#define LOAD_TEXTURE2D_ARRAY_LOD(textureName, unCoord2, index, lod) textureName.Load(int4(unCoord2, index, lod))
//LOAD_TEXTURE2D_ARRAY(textureName, unCoord2, SLICE_ARRAY_INDEX)
//https://github.com/Unity-Technologies/Graphics/blob/6fdc7996098aa184495c0a3c0da10135bfe18340/Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl
float4 testOut = LOAD_TEXTURE2D_ARRAY(_GBufferTexture1, coord,0);
//return testOut;//tex2D(_GBufferTexture1, coord);
return float4(result.rgb, 1.0);
}
ENDHLSL//ENDCG
}
Pass //11 Bilateral upsample
{
HLSLPROGRAM//CGPROGRAM
#pragma vertex vert //vertHD //v1.1
#pragma fragment frag
float2 Kernel;
float DepthTolerance;
sampler2D DepthNormalsLow;
sampler2D DepthLow;
int SourceScale;
sampler2D CurrentDepth;
sampler2D CurrentNormal;
float4 frag(v2f input) : COLOR0
{
float4 blurred = float4(0.0, 0.0, 0.0, 0.0);
float4 blurredDumb = float4(0.0, 0.0, 0.0, 0.0);
float validWeights = 0.0;
//v1.5
//float depth = LinearEyeDepth(tex2D(_CameraDepthTexture, input.uv.xy).x);
float depth = LinearEyeDepth(GetDepthTexture(input.uv.xy).x);
//float depth = LinearEyeDepth(LOAD_TEXTURE2D_X_LOD(_CameraDepthTexture, float2(input.uv.x, input.uv.y), 0).x);
//uint2 positionSS = input.uv* float2(DepthPyramidScale.x, DepthPyramidScale.y) + float2(DepthPyramidScale.z, DepthPyramidScale.w);// * _ScreenSize.xy;
//float depth = LinearEyeDepth(LOAD_TEXTURE2D(_CameraDepthTexture, 0, positionSS).x);
//float depth = LOAD_TEXTURE2D_X_LOD(_CameraDepthTexture, positionSS, 0).r;
half3 normal = DecodeViewNormalStereo(tex2D(_CameraDepthNormalsTexture, input.uv.xy));
float thresh = 0.26;
float3 viewPosition = GetViewSpacePosition(input.uv.xy).xyz;
float3 viewVector = normalize(viewPosition);
float NdotV = 1.0 / (saturate(dot(-viewVector, normal.xyz)) + 0.1);
thresh *= 1.0 + NdotV * 2.0;
float4 sample00 = tex2Dlod(_MainTex, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(0.0, 0.0) * 1.0, 0.0, 0.0));
float4 sample10 = tex2Dlod(_MainTex, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(1.0, 0.0) * 1.0, 0.0, 0.0));
float4 sample11 = tex2Dlod(_MainTex, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(1.0, 1.0) * 1.0, 0.0, 0.0));
float4 sample01 = tex2Dlod(_MainTex, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(0.0, 1.0) * 1.0, 0.0, 0.0));
float4 depthSamples = float4(0,0,0,0);
depthSamples.x = LinearEyeDepth(tex2Dlod(CurrentDepth, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(0.0, 0.0), 0, 0)).x);
depthSamples.y = LinearEyeDepth(tex2Dlod(CurrentDepth, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(1.0, 0.0), 0, 0)).x);
depthSamples.z = LinearEyeDepth(tex2Dlod(CurrentDepth, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(1.0, 1.0), 0, 0)).x);
depthSamples.w = LinearEyeDepth(tex2Dlod(CurrentDepth, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(0.0, 1.0), 0, 0)).x);
half3 normal00 = DecodeViewNormalStereo(tex2D(CurrentNormal, input.uv.xy + _MainTex_TexelSize.xy * float2(0.0, 0.0)));
half3 normal10 = DecodeViewNormalStereo(tex2D(CurrentNormal, input.uv.xy + _MainTex_TexelSize.xy * float2(1.0, 0.0)));
half3 normal11 = DecodeViewNormalStereo(tex2D(CurrentNormal, input.uv.xy + _MainTex_TexelSize.xy * float2(1.0, 1.0)));
half3 normal01 = DecodeViewNormalStereo(tex2D(CurrentNormal, input.uv.xy + _MainTex_TexelSize.xy * float2(0.0, 1.0)));
float4 depthWeights = saturate(1.0 - abs(depthSamples - depth.xxxx) / thresh);
float4 normalWeights = float4(0,0,0,0);
normalWeights.x = pow(saturate(dot(normal00, normal)), 24.0);
normalWeights.y = pow(saturate(dot(normal10, normal)), 24.0);
normalWeights.z = pow(saturate(dot(normal11, normal)), 24.0);
normalWeights.w = pow(saturate(dot(normal01, normal)), 24.0);
float4 weights = depthWeights * normalWeights;
float weightSum = dot(weights, float4(1.0, 1.0, 1.0, 1.0));
if (weightSum < 0.01)
{
weightSum = 4.0;
weights = (1.0).xxxx;
}
weights /= weightSum;
float2 fractCoord = frac(input.uv.xy * _MainTex_TexelSize.zw * 1.0);
float4 filteredX0 = lerp(sample00 * weights.x, sample10 * weights.y, fractCoord.x);
float4 filteredX1 = lerp(sample01 * weights.w, sample11 * weights.z, fractCoord.x);
float4 filtered = lerp(filteredX0, filteredX1, fractCoord.y);
return filtered * 3.0;
return blurred;
}
ENDHLSL//ENDCG
}
Pass //12 Temporal blending without motion vectors (for legacy support)
{
HLSLPROGRAM//CGPROGRAM
#pragma vertex vert
#pragma fragment frag
sampler2D GITexture;
sampler2D PreviousDepth;
sampler2D CurrentDepth;
sampler2D PreviousLocalWorldPos;
float4 CameraPosition;
float4 CameraPositionPrev;
float4x4 ProjectionPrev;
float4x4 ProjectionPrevInverse;
float4x4 WorldToCameraPrev;
float4x4 CameraToWorldPrev;
float4x4 CameraToWorld;
float DeltaTime;
float BlendWeight;
float4 frag(v2f input) : COLOR0
{
float3 gi = tex2D(_MainTex, input.uv.xy).rgb;
float2 depthLookupCoord = round(input.uv.xy * _MainTex_TexelSize.zw) * _MainTex_TexelSize.xy;
depthLookupCoord = input.uv.xy;
//float depth = tex2Dlod(_CameraDepthTexture, float4(depthLookupCoord, 0.0, 0.0)).x;
float depth = GetDepthTexture(depthLookupCoord);
float4 currentPos = float4(input.uv.x * 2.0 - 1.0, input.uv.y * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
float4 fragpos = mul(ProjectionMatrixInverse, currentPos);
float4 thisViewPos = fragpos;
fragpos = mul(CameraToWorld, fragpos);
fragpos /= fragpos.w;
float4 thisWorldPosition = fragpos;
fragpos.xyz += CameraPosition.xyz * DeltaTime;
float4 prevPos = fragpos;
prevPos.xyz -= CameraPositionPrev.xyz * DeltaTime;
prevPos = mul(WorldToCameraPrev, prevPos);
prevPos = mul(ProjectionPrev, prevPos);
prevPos /= prevPos.w;
float2 diff = currentPos.xy - prevPos.xy;
float2 reprojCoord = input.uv.xy - diff.xy * 0.5;
float2 previousTexcoord = input.uv.xy + diff.xy * 0.5;
float blendWeight = BlendWeight;
float prevDepth = (tex2Dlod(PreviousDepth, float4(reprojCoord + _MainTex_TexelSize.xy * 0.0, 0.0, 0.0)).x);
float4 previousWorldPosition = mul(ProjectionPrevInverse, float4(reprojCoord.xy * 2.0 - 1.0, prevDepth * 2.0 - 1.0, 1.0));
previousWorldPosition = mul(CameraToWorldPrev, previousWorldPosition);
previousWorldPosition /= previousWorldPosition.w;
if (distance(previousWorldPosition.xyz, thisWorldPosition.xyz) > 0.1 || reprojCoord.x > 1.0 || reprojCoord.x < 0.0 || reprojCoord.y > 1.0 || reprojCoord.y < 0.0)
{
blendWeight = 1.0;
}
float3 prevGI = tex2D(PreviousGITexture, reprojCoord).rgb;
gi = lerp(prevGI, gi, float3(blendWeight, blendWeight, blendWeight));
float3 result = gi;
return float4(result, 1.0);
}
ENDHLSL//ENDCG
}
//// PASS 13 NEW DEPTH HDRP
/*Pass
{
ZWrite Off
ZTest Always
Blend Off
Cull Off
HLSLPROGRAM
#pragma fragment FullScreenPass
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/NormalBuffer.hlsl"
float4 SampleBuffer(PositionInputs posInput)
{
return float4(posInput.linearDepth, 0, 0, 1);
}
ENDHLSL
}*/
/////END PASS13
}
Fallback off
}