[A] color [M] NDC position insted screen pos
This commit is contained in:
@@ -20,8 +20,8 @@ MonoBehaviour:
|
||||
- {fileID: 11400000, guid: 707360a9c581a4bd7aa53bfeb1429f71, type: 2}
|
||||
m_DefaultRendererIndex: 0
|
||||
m_RequireDepthTexture: 1
|
||||
m_RequireOpaqueTexture: 0
|
||||
m_OpaqueDownsampling: 1
|
||||
m_RequireOpaqueTexture: 1
|
||||
m_OpaqueDownsampling: 0
|
||||
m_SupportsTerrainHoles: 1
|
||||
m_SupportsHDR: 0
|
||||
m_HDRColorBufferPrecision: 0
|
||||
|
||||
@@ -90,6 +90,8 @@ Material:
|
||||
- _ClearCoatSmoothness: 0
|
||||
- _Cull: 2
|
||||
- _Cutoff: 0.5
|
||||
- _DepthHorizontal: 1.39
|
||||
- _DepthVertical: 3.69
|
||||
- _DetailAlbedoMapScale: 1
|
||||
- _DetailNormalMapScale: 1
|
||||
- _DstBlend: 0
|
||||
@@ -119,10 +121,11 @@ Material:
|
||||
- _WorkflowMode: 1
|
||||
- _ZWrite: 1
|
||||
m_Colors:
|
||||
- _BaseColor: {r: 1, g: 1, b: 1, a: 1}
|
||||
- _BaseColor: {r: 0.024964403, g: 0.23726082, b: 0.4811321, a: 0.8862745}
|
||||
- _Color: {r: 1, g: 1, b: 1, a: 1}
|
||||
- _Direction: {r: 0, g: -1, b: 0, a: 0}
|
||||
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
|
||||
- _ShallowColor: {r: 0.06430225, g: 0.8018868, b: 0.79103994, a: 0.27450982}
|
||||
- _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1}
|
||||
- _WaveDirection: {r: 1, g: 1, b: 1, a: 1}
|
||||
- _WaveFadeDistance: {r: 150, g: 300, b: 0, a: 0}
|
||||
|
||||
@@ -25,6 +25,7 @@ struct WaterSurface
|
||||
float3 albedo;
|
||||
float3 offset;
|
||||
float alpha;
|
||||
float fog;
|
||||
};
|
||||
|
||||
struct SceneDepth
|
||||
@@ -40,15 +41,16 @@ struct SceneDepth
|
||||
#define DEPTH_SCALAR (FAR_CLIP - NEAR_CLIP)
|
||||
|
||||
//Return depth based on the used technique (buffer, vertex color, baked texture)
|
||||
SceneDepth SampleDepth(float4 screenPos)
|
||||
SceneDepth SampleDepth(float4 positionNDC)
|
||||
{
|
||||
SceneDepth depth = (SceneDepth)0;
|
||||
|
||||
screenPos.xyz /= screenPos.w;
|
||||
positionNDC.xy /= positionNDC.w;
|
||||
|
||||
depth.raw = SampleSceneDepth(screenPos.xy);
|
||||
depth.eye = LinearEyeDepth(depth.raw, _ZBufferParams);
|
||||
depth.linear01 = Linear01Depth(depth.raw, _ZBufferParams) * DEPTH_SCALAR;
|
||||
float raw = SampleSceneDepth(positionNDC.xy);
|
||||
depth.raw = raw;
|
||||
depth.eye = LinearEyeDepth(raw, _ZBufferParams);
|
||||
depth.linear01 = Linear01Depth(raw, _ZBufferParams) * DEPTH_SCALAR;
|
||||
|
||||
return depth;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
|
||||
struct SceneData
|
||||
{
|
||||
float4 positionSS; //Unnormalized
|
||||
float4 positionNDC; //Unnormalized
|
||||
float2 screenPos; //Normalized and no refraction
|
||||
float3 positionWS;
|
||||
|
||||
@@ -9,18 +9,18 @@ struct SceneData
|
||||
float verticalDepth;
|
||||
};
|
||||
|
||||
void PopulateSceneData(inout SceneData scene, in Varyings input, in WaterSurface water, in float3 camPos)
|
||||
void PopulateSceneData(inout SceneData scene, in Varyings input, in WaterSurface water)
|
||||
{
|
||||
scene.positionSS = input.screenPos;
|
||||
scene.screenPos = input.screenPos.xy / input.screenPos.w;
|
||||
scene.positionNDC = input.positionNDC;
|
||||
scene.screenPos = input.positionNDC.xy / input.positionNDC.w;
|
||||
|
||||
//Default for disabled depth texture
|
||||
scene.viewDepth = 1;
|
||||
scene.verticalDepth = 1;
|
||||
|
||||
//Screen depth Projection to world position
|
||||
SceneDepth depth = SampleDepth(scene.positionSS);
|
||||
float3 projWorldPos = depth.eye * (water.viewDir / input.screenPos.w) - camPos;
|
||||
SceneDepth depth = SampleDepth(input.positionNDC);
|
||||
float3 projWorldPos = depth.eye * (water.viewDelta / input.positionNDC.w) - _WorldSpaceCameraPos;
|
||||
scene.positionWS = -projWorldPos;
|
||||
|
||||
const float sceneDepth = depth.eye;
|
||||
@@ -33,12 +33,24 @@ void PopulateSceneData(inout SceneData scene, in Varyings input, in WaterSurface
|
||||
//scene.verticalDepth = DepthDistance(water.positionWS, scene.positionWS, water.waveNormal * normalSign);
|
||||
}
|
||||
|
||||
float GetWaterDensity(SceneData scene, float heightScalar, float viewDepthScalar)
|
||||
{
|
||||
//Best default value, otherwise water just turns invisible (infinitely shallow)
|
||||
float density = 1.0;
|
||||
|
||||
float viewDepth = scene.viewDepth;
|
||||
float verticalDepth = scene.verticalDepth;
|
||||
|
||||
float depthAttenuation = 1.0 - exp(-viewDepth * viewDepthScalar * 0.1);
|
||||
float heightAttenuation = saturate(verticalDepth * heightScalar);
|
||||
|
||||
return max(depthAttenuation, heightAttenuation);
|
||||
}
|
||||
|
||||
float4 ForwardPassFragment(Varyings input) : SV_Target
|
||||
{
|
||||
WaterSurface water = (WaterSurface)0;
|
||||
SceneData scene = (SceneData)0;
|
||||
float3 camPos = GetCurrentViewPosition();
|
||||
|
||||
|
||||
float3 normalWS = normalize(input.normalWS.xyz);
|
||||
float3 WorldTangent = input.tangent.xyz;
|
||||
@@ -48,14 +60,20 @@ float4 ForwardPassFragment(Varyings input) : SV_Target
|
||||
|
||||
water.alpha = 1.0;
|
||||
water.positionWS = positionWS;
|
||||
water.viewDelta = camPos - scene.positionWS;
|
||||
water.viewDelta = _WorldSpaceCameraPos - positionWS;
|
||||
water.viewDir = normalize(water.viewDelta);
|
||||
water.vertexNormal = normalize(input.normalWS.xyz);
|
||||
|
||||
half VdotN = 1.0 - saturate(dot(water.viewDir, normalWS));
|
||||
|
||||
|
||||
PopulateSceneData(scene, input, water, camPos);
|
||||
PopulateSceneData(scene, input, water);
|
||||
|
||||
return float4(scene.viewDepth.xxx, 1);
|
||||
//return float4(scene.verticalDepth.xxx, 1);
|
||||
|
||||
water.fog = GetWaterDensity(scene, _DepthHorizontal, _DepthVertical);
|
||||
|
||||
//Albedo
|
||||
float4 baseColor = lerp(_ShallowColor, _BaseColor, water.fog);
|
||||
return baseColor;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,12 @@ CBUFFER_START(UnityPerMaterial)
|
||||
float2 _Direction;
|
||||
float _Speed;
|
||||
|
||||
//Color + Transparency
|
||||
half4 _BaseColor;
|
||||
half4 _ShallowColor;
|
||||
float _DepthVertical;
|
||||
float _DepthHorizontal;
|
||||
|
||||
//Waves
|
||||
float _WaveSpeed;
|
||||
half _WaveHeight;
|
||||
|
||||
@@ -22,7 +22,7 @@ struct Varyings
|
||||
//wPos.z in w-component
|
||||
float4 bitangent : TEXCOORD4;
|
||||
|
||||
float4 screenPos : TEXCOORD5;
|
||||
float4 positionNDC : TEXCOORD5;
|
||||
|
||||
float4 positionCS : SV_POSITION;
|
||||
|
||||
@@ -53,9 +53,14 @@ Varyings LitPassVertex(Attributes input)
|
||||
//Apply vertex displacements
|
||||
positionWS += waves.position.xyz;
|
||||
|
||||
output.positionCS = TransformWorldToHClip(positionWS);
|
||||
float4 positionCS = TransformWorldToHClip(positionWS);
|
||||
|
||||
output.screenPos = ComputeScreenPos(output.positionCS);
|
||||
output.positionCS = positionCS;
|
||||
|
||||
float4 ndc = positionCS * 0.5f;
|
||||
ndc.xy = float2(ndc.x, ndc.y * _ProjectionParams.x) + ndc.w;
|
||||
ndc.zw = positionCS.zw;
|
||||
output.positionNDC = ndc;
|
||||
|
||||
output.normalWS = float4(normalInput.normalWS, positionWS.x);
|
||||
output.tangent = float4(normalInput.tangentWS, positionWS.y);
|
||||
|
||||
@@ -5,6 +5,12 @@ Shader "zzwater/ocean"
|
||||
_Direction("Animation direction", Vector) = (0,-1,0,0)
|
||||
_Speed("Animation Speed", Float) = 1
|
||||
|
||||
//Color + Transparency
|
||||
[HDR]_BaseColor("Deep", Color) = (0, 0.44, 0.62, 1)
|
||||
[HDR]_ShallowColor("Shallow", Color) = (0.1, 0.9, 0.89, 0.02)
|
||||
_DepthVertical("View Depth", Range(0.01 , 16)) = 4
|
||||
_DepthHorizontal("Vertical Height Depth", Range(0.01 , 8)) = 1
|
||||
|
||||
//waves
|
||||
_WaveSpeed("Speed", Float) = 2
|
||||
_WaveHeight("Height", Range(0 , 10)) = 0.25
|
||||
|
||||
Reference in New Issue
Block a user