[A] ocean shader add light
This commit is contained in:
@@ -90,16 +90,18 @@ Material:
|
||||
- _ClearCoatSmoothness: 0
|
||||
- _Cull: 2
|
||||
- _Cutoff: 0.5
|
||||
- _DepthHorizontal: 1.39
|
||||
- _DepthVertical: 3.69
|
||||
- _DepthHorizontal: 1.34
|
||||
- _DepthVertical: 10.83
|
||||
- _DetailAlbedoMapScale: 1
|
||||
- _DetailNormalMapScale: 1
|
||||
- _DstBlend: 0
|
||||
- _DstBlendAlpha: 0
|
||||
- _EdgeFade: 8.34
|
||||
- _EnvironmentReflections: 1
|
||||
- _GlossMapScale: 0
|
||||
- _Glossiness: 0
|
||||
- _GlossyReflections: 0
|
||||
- _HorizonDistance: 21.27
|
||||
- _Metallic: 0
|
||||
- _OcclusionStrength: 1
|
||||
- _Parallax: 0.005
|
||||
@@ -112,23 +114,29 @@ Material:
|
||||
- _SrcBlend: 1
|
||||
- _SrcBlendAlpha: 1
|
||||
- _Surface: 0
|
||||
- _WaveCount: 2
|
||||
- _WaveDistance: 0.8
|
||||
- _WaveHeight: 0.25
|
||||
- _TranslucencyCurvatureMask: 0.584
|
||||
- _TranslucencyExp: 15.3
|
||||
- _TranslucencyReflectionMask: 0.747
|
||||
- _TranslucencyStrength: 1.03
|
||||
- _TranslucencyStrengthDirect: 0.07
|
||||
- _WaveCount: 3
|
||||
- _WaveDistance: 0.747
|
||||
- _WaveHeight: 0.46
|
||||
- _WaveNormalStr: 0.5
|
||||
- _WaveSpeed: 2
|
||||
- _WaveSteepness: 0.1
|
||||
- _WaveSpeed: 1
|
||||
- _WaveSteepness: 2.24
|
||||
- _WorkflowMode: 1
|
||||
- _ZWrite: 1
|
||||
m_Colors:
|
||||
- _BaseColor: {r: 0.024964403, g: 0.23726082, b: 0.4811321, a: 0.8862745}
|
||||
- _BaseColor: {r: 0.0133944545, g: 0.20659995, b: 0.4056604, a: 0.84705883}
|
||||
- _Color: {r: 1, g: 1, b: 1, a: 1}
|
||||
- _Direction: {r: 0, g: -1, b: 0, a: 0}
|
||||
- _Direction: {r: 1, 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}
|
||||
- _HorizonColor: {r: 0.09376113, g: 0.18046764, b: 0.46226418, a: 0.95686275}
|
||||
- _ShallowColor: {r: 0.07943218, g: 0.5314412, b: 0.8018868, a: 0.79607844}
|
||||
- _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}
|
||||
- _WaveDirection: {r: 1, g: 1, b: 1, a: 2}
|
||||
- _WaveFadeDistance: {r: 50, g: 81.68, b: 0, a: 0}
|
||||
m_BuildTextureStacks: []
|
||||
--- !u!114 &1635733923474585344
|
||||
MonoBehaviour:
|
||||
|
||||
@@ -21,11 +21,14 @@ struct WaterSurface
|
||||
|
||||
//Normal from the base geometry, in world-space
|
||||
float3 vertexNormal;
|
||||
float3 tangentNormal;
|
||||
float3 tangentWorldNormal;
|
||||
|
||||
float3 albedo;
|
||||
float3 offset;
|
||||
float alpha;
|
||||
float fog;
|
||||
float edgeFade;
|
||||
};
|
||||
|
||||
struct SceneDepth
|
||||
|
||||
@@ -4,6 +4,7 @@ struct SceneData
|
||||
float4 positionNDC; //Unnormalized
|
||||
float2 screenPos; //Normalized and no refraction
|
||||
float3 positionWS;
|
||||
float3 color;
|
||||
|
||||
float viewDepth;
|
||||
float verticalDepth;
|
||||
@@ -64,8 +65,24 @@ float4 ForwardPassFragment(Varyings input) : SV_Target
|
||||
water.viewDir = normalize(water.viewDelta);
|
||||
water.vertexNormal = normalize(input.normalWS.xyz);
|
||||
|
||||
//Normal
|
||||
water.tangentNormal = float3(0.5, 0.5, 1);
|
||||
water.tangentWorldNormal = normalWS;
|
||||
|
||||
//Tangent-space
|
||||
//water.tangentNormal = SampleNormals(uv, _NormalTiling, _NormalSubTiling, positionWS, TIME, _NormalSpeed, _NormalSubSpeed, water.slope, water.vFace);
|
||||
|
||||
//Based on wave normal, makes it easier to create blend between the smooth wave normals and high-frequency normal maps
|
||||
//water.tangentToWorldMatrix = half3x3(WorldTangent, WorldBiTangent, water.waveNormal);
|
||||
|
||||
//World-space
|
||||
//water.tangentWorldNormal = normalize(TransformTangentToWorld(water.tangentNormal, water.tangentToWorldMatrix));
|
||||
|
||||
half VdotN = 1.0 - saturate(dot(water.viewDir, normalWS));
|
||||
|
||||
half4 shadowMask = 1.0;
|
||||
float4 shadowCoords = float4(0, 0, 0, 0);
|
||||
Light mainLight = GetMainLight(shadowCoords, water.positionWS, shadowMask);
|
||||
|
||||
PopulateSceneData(scene, input, water);
|
||||
|
||||
@@ -75,5 +92,69 @@ float4 ForwardPassFragment(Varyings input) : SV_Target
|
||||
|
||||
//Albedo
|
||||
float4 baseColor = lerp(_ShallowColor, _BaseColor, water.fog);
|
||||
return baseColor;
|
||||
|
||||
water.fog *= baseColor.a;
|
||||
water.alpha = baseColor.a;
|
||||
|
||||
float fresnel = saturate(pow(VdotN, _HorizonDistance)) * _HorizonColor.a;
|
||||
|
||||
water.albedo = lerp(baseColor.rgb, _HorizonColor.rgb, fresnel);
|
||||
|
||||
water.edgeFade = saturate(scene.verticalDepth / (_EdgeFade * 0.01));
|
||||
|
||||
water.alpha *= water.edgeFade;
|
||||
|
||||
|
||||
/* ========
|
||||
// TRANSLUCENCY
|
||||
=========== */
|
||||
TranslucencyData translucencyData = (TranslucencyData)0;
|
||||
float scatteringMask = 1.0;
|
||||
scatteringMask = saturate(water.fog + water.edgeFade);
|
||||
//scatteringMask = saturate((water.fog + water.edgeFade) - (water.reflectionMask * water.vFace)) * water.shadowMask;
|
||||
//scatteringMask -= water.foam;
|
||||
//scatteringMask = saturate(scatteringMask);
|
||||
translucencyData.subsurfaceColor = _ShallowColor.rgb;
|
||||
//translucencyData.lightColor = mainLight.color;
|
||||
//translucencyData.lightDir = mainLight.direction;
|
||||
|
||||
//translucencyData.normal = normalWS;
|
||||
translucencyData.curvature = _TranslucencyCurvatureMask;
|
||||
translucencyData.mask = scatteringMask;
|
||||
translucencyData.strength = _TranslucencyStrength;
|
||||
translucencyData.strengthIncident = _TranslucencyStrengthDirect;
|
||||
//translucencyData.viewDir = water.viewDir;
|
||||
translucencyData.exponent = _TranslucencyExp;
|
||||
|
||||
|
||||
/* ========
|
||||
// UNITY SURFACE & INPUT DATA
|
||||
=========== */
|
||||
SurfaceData surfaceData = (SurfaceData)0;
|
||||
|
||||
surfaceData.albedo = water.albedo.rgb;
|
||||
surfaceData.specular = 0;// water.specular.rgb;
|
||||
surfaceData.metallic = 0;
|
||||
surfaceData.smoothness = 0;
|
||||
surfaceData.normalTS = water.tangentNormal;
|
||||
surfaceData.emission = 0; //To be populated with translucency+caustics
|
||||
surfaceData.occlusion = 1.0;
|
||||
surfaceData.alpha = water.alpha;
|
||||
|
||||
InputData inputData = (InputData)0;
|
||||
inputData.positionWS = positionWS;
|
||||
inputData.viewDirectionWS = water.viewDir;
|
||||
inputData.shadowCoord = shadowCoords;
|
||||
inputData.normalWS = water.tangentWorldNormal;
|
||||
inputData.fogCoord = 0;//InitializeInputDataFog(float4(positionWS, 1.0), input.fogFactorAndVertexLight.x);
|
||||
inputData.vertexLighting = 0;//input.fogFactorAndVertexLight.yzw;
|
||||
inputData.bakedGI = 0;
|
||||
inputData.shadowMask = 0;//shadowMask;
|
||||
|
||||
|
||||
float4 finalColor = float4(ApplyLighting(surfaceData, scene.color, mainLight, inputData, water, translucencyData), water.alpha);
|
||||
|
||||
|
||||
return finalColor;
|
||||
// return float4(water.albedo, water.alpha);
|
||||
}
|
||||
|
||||
@@ -7,8 +7,18 @@ CBUFFER_START(UnityPerMaterial)
|
||||
//Color + Transparency
|
||||
half4 _BaseColor;
|
||||
half4 _ShallowColor;
|
||||
float4 _HorizonColor;
|
||||
half _HorizonDistance;
|
||||
float _DepthVertical;
|
||||
float _DepthHorizontal;
|
||||
half _EdgeFade;
|
||||
|
||||
half _TranslucencyStrength;
|
||||
half _TranslucencyStrengthDirect;
|
||||
half _TranslucencyExp;
|
||||
half _TranslucencyCurvatureMask;
|
||||
|
||||
|
||||
|
||||
//Waves
|
||||
float _WaveSpeed;
|
||||
|
||||
45
Packages/zzwater/shaders/libs/Light.hlsl
Normal file
45
Packages/zzwater/shaders/libs/Light.hlsl
Normal file
@@ -0,0 +1,45 @@
|
||||
|
||||
struct TranslucencyData
|
||||
{
|
||||
float3 subsurfaceColor;
|
||||
float curvature;
|
||||
float mask; //Actually the 'thickness'
|
||||
float strength;
|
||||
float strengthIncident;
|
||||
float exponent;
|
||||
};
|
||||
|
||||
void ApplyTranslucency(float3 subsurfaceColor, float3 lightDir, float3 lightColor, float3 viewDir, float3 normal, float occlusion, float strength, float incidentStrength, float exponent, float offset, inout float3 emission)
|
||||
{
|
||||
//Coefficient describing how much the surface orientation is between the camera and the direction of/to the light
|
||||
half transmittance = saturate(dot(-viewDir, lightDir));
|
||||
//Exponentiate to tighten the falloff
|
||||
transmittance = saturate(pow(transmittance, exponent)) * strength;
|
||||
|
||||
half incident = saturate(dot(lightDir, normal)) * incidentStrength;
|
||||
|
||||
//Mask by normals facing away from the light (backfaces, in light-space)
|
||||
const half curvature = saturate(lerp(1.0, dot(normal, -lightDir), offset));
|
||||
transmittance *= curvature;
|
||||
|
||||
const float lightIntensity = lightColor.r * 0.3 + lightColor.g * 0.59 + lightColor.b * 0.11;
|
||||
|
||||
half attenuation = (transmittance + incident) * occlusion * lightIntensity;
|
||||
|
||||
emission += lerp(emission, subsurfaceColor, attenuation);
|
||||
}
|
||||
|
||||
|
||||
//Based on UniversalFragmentBlinnPhong (no BRDF)
|
||||
float3 ApplyLighting(inout SurfaceData surfaceData, inout float3 sceneColor, Light mainLight, InputData inputData, WaterSurface water, TranslucencyData translucencyData)
|
||||
{
|
||||
ApplyTranslucency(translucencyData.subsurfaceColor, mainLight.direction, mainLight.color, water.viewDir, water.vertexNormal, translucencyData.mask, translucencyData.strength, translucencyData.strengthIncident, translucencyData.exponent, translucencyData.curvature, surfaceData.emission.rgb );
|
||||
|
||||
half3 attenuatedLightColor = mainLight.color * (mainLight.distanceAttenuation * mainLight.shadowAttenuation);
|
||||
half3 diffuseColor = inputData.bakedGI + LightingLambert(attenuatedLightColor, mainLight.direction, water.vertexNormal);
|
||||
|
||||
float3 color = (surfaceData.albedo.rgb * diffuseColor) + surfaceData.emission.rgb + surfaceData.specular;
|
||||
|
||||
return color;
|
||||
}
|
||||
|
||||
7
Packages/zzwater/shaders/libs/Light.hlsl.meta
Normal file
7
Packages/zzwater/shaders/libs/Light.hlsl.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cf5941ae7f85e4b30bab818d47e3a79b
|
||||
ShaderIncludeImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -62,7 +62,7 @@ Varyings LitPassVertex(Attributes input)
|
||||
ndc.zw = positionCS.zw;
|
||||
output.positionNDC = ndc;
|
||||
|
||||
output.normalWS = float4(normalInput.normalWS, positionWS.x);
|
||||
output.normalWS = float4(waves.normal, positionWS.x);
|
||||
output.tangent = float4(normalInput.tangentWS, positionWS.y);
|
||||
output.bitangent = float4(normalInput.bitangentWS, positionWS.z);
|
||||
|
||||
|
||||
@@ -8,8 +8,18 @@ Shader "zzwater/ocean"
|
||||
//Color + Transparency
|
||||
[HDR]_BaseColor("Deep", Color) = (0, 0.44, 0.62, 1)
|
||||
[HDR]_ShallowColor("Shallow", Color) = (0.1, 0.9, 0.89, 0.02)
|
||||
[HDR]_HorizonColor("Horizon", Color) = (0.84, 1, 1, 0.15)
|
||||
_HorizonDistance("Horizon Distance", Range(0.01 , 32)) = 8
|
||||
_DepthVertical("View Depth", Range(0.01 , 16)) = 4
|
||||
_DepthHorizontal("Vertical Height Depth", Range(0.01 , 8)) = 1
|
||||
_EdgeFade("Edge Fade", Float) = 0.1
|
||||
|
||||
_TranslucencyStrength("Translucency Strength", Range(0 , 3)) = 1
|
||||
_TranslucencyStrengthDirect("Translucency Strength (Direct)", Range(0 , 0.5)) = 0.05
|
||||
_TranslucencyExp("Translucency Exponent", Range(1 , 32)) = 4
|
||||
_TranslucencyCurvatureMask("Translucency Curvature mask", Range(0, 1)) = 0.75
|
||||
_TranslucencyReflectionMask("Translucency Reflection mask", Range(0, 1)) = 1
|
||||
|
||||
|
||||
//waves
|
||||
_WaveSpeed("Speed", Float) = 2
|
||||
@@ -58,6 +68,7 @@ Shader "zzwater/ocean"
|
||||
#include "Packages/zzwater/shaders/libs/Input.hlsl"
|
||||
#include "Packages/zzwater/shaders/libs/Waves.hlsl"
|
||||
#include "Packages/zzwater/shaders/libs/Vertex.hlsl"
|
||||
#include "Packages/zzwater/shaders/libs/Light.hlsl"
|
||||
|
||||
#pragma vertex Vertex
|
||||
|
||||
|
||||
Reference in New Issue
Block a user