浏览代码

Water update

/demo-work-customrenderer
André McGrail 5 年前
当前提交
97a0a4fb
共有 15 个文件被更改,包括 1144 次插入1273 次删除
  1. 940
      Assets/scenes/Island.unity
  2. 10
      Packages/com.verasl.water-system/Caustics.mat
  3. 2
      Packages/com.verasl.water-system/Scripts/BuoyantObject.cs
  4. 27
      Packages/com.verasl.water-system/Scripts/Data/WaterResources.asset
  5. 4
      Packages/com.verasl.water-system/Scripts/Data/WaterResources.cs
  6. 2
      Packages/com.verasl.water-system/Scripts/MainCameraAlign.cs
  7. 34
      Packages/com.verasl.water-system/Scripts/Rendering/PlanarReflections.cs
  8. 42
      Packages/com.verasl.water-system/Scripts/Water.cs
  9. 185
      Packages/com.verasl.water-system/SeaVisual.prefab
  10. 3
      Packages/com.verasl.water-system/Shaders/GerstnerWaves.hlsl
  11. 9
      Packages/com.verasl.water-system/Shaders/WaterCommon.hlsl
  12. 63
      Packages/com.verasl.water-system/Textures/WaterSurface_atlas.tif.meta
  13. 985
      Packages/com.verasl.water-system/Textures/WaterSurface_single.tif
  14. 102
      Packages/com.verasl.water-system/Shaders/Caustics.shader
  15. 9
      Packages/com.verasl.water-system/Shaders/Caustics.shader.meta

940
Assets/scenes/Island.unity
文件差异内容过多而无法显示
查看文件

10
Packages/com.verasl.water-system/Caustics.mat


m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Caustics
m_Shader: {fileID: -6465566751694194690, guid: 127e7bcb862931d40864f8215c168a82,
type: 3}
m_Shader: {fileID: 4800000, guid: 90939d2e4b62841d29c136c866715501, type: 3}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0

m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _CausticMap:
m_Texture: {fileID: 2800000, guid: f395b7d64f44848c6a3cba1f1173fe90, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}

- Vector1_F3303B3C: 0.5
- _AlphaClip: 0
- _Blend: 0
- _BlendDistance: 2
- _BumpScale: 1
- _Cull: 2
- _Cutoff: 0.5

- _OcclusionStrength: 1
- _QueueOffset: 0
- _ReceiveShadows: 1
- _Size: 0.25
- _WaterLevel: -0.625
- _WorkflowMode: 1
- _ZWrite: 1
m_Colors:

2
Packages/com.verasl.water-system/Scripts/BuoyantObject.cs


if (wp.y - voxelResolution < waterLevel)
{
float k = Mathf.Clamp01(waterLevel - (wp.y - voxelResolution)) / (voxelResolution * 2f);
float k = Mathf.Clamp01((waterLevel - (wp.y - voxelResolution)) / (voxelResolution * 2f));
submergedAmount += k / voxels.Length;//(math.clamp(waterLevel - (wp.y - voxelResolution), 0f, voxelResolution * 2f) / (voxelResolution * 2f)) / voxels.Count;

27
Packages/com.verasl.water-system/Scripts/Data/WaterResources.asset


defaultFoamRamp: {fileID: 2800000, guid: 4098762bce32f44b9b7c56531d1a988e, type: 3}
defaultFoamMap: {fileID: 2800000, guid: ca439406e806ecf44a9586510be9477d, type: 3}
defaultSurfaceMap: {fileID: 2800000, guid: f395b7d64f44848c6a3cba1f1173fe90, type: 3}
defaultSeaMaterial: {fileID: 2100000, guid: 4849d4d15a9cb42eb9533ae935e2630b, type: 2}
defaultWaterMeshes:
- {fileID: 4300036, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}
- {fileID: 4300050, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}
- {fileID: 4300038, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}
- {fileID: 4300048, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}
- {fileID: 4300040, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}
- {fileID: 4300046, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}
- {fileID: 4300042, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}
- {fileID: 4300044, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}
- {fileID: 4300026, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}
- {fileID: 4300024, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}
- {fileID: 4300022, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}
- {fileID: 4300020, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}
- {fileID: 4300018, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}
- {fileID: 4300014, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}
- {fileID: 4300012, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}
- {fileID: 4300006, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}
- {fileID: 4300004, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}
- {fileID: 4300034, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}
- {fileID: 4300032, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}
- {fileID: 4300030, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}
- {fileID: 4300028, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}
- {fileID: 4300010, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}
- {fileID: 4300016, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}
- {fileID: 4300008, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}
- {fileID: 4300002, guid: 074aa8ca109924e18baf19f3e26665b6, type: 3}

4
Packages/com.verasl.water-system/Scripts/Data/WaterResources.cs


public Texture2D defaultFoamRamp; // a default foam ramp for the basic foam setting
public Texture2D defaultFoamMap; // a default foam texture map
public Texture2D defaultSurfaceMap; // a default normal/caustic map
}
public Material defaultSeaMaterial;
public Mesh[] defaultWaterMeshes;
}
}

2
Packages/com.verasl.water-system/Scripts/MainCameraAlign.cs


public float quantizeValue = 6.25f;
public float forwards = 10f;
public float yOffset = -1f;
public float yOffset = -0.25f;
private void OnEnable()
{

34
Packages/com.verasl.water-system/Scripts/Rendering/PlanarReflections.cs


if (dest == null)
return;
dest.CopyFrom(src);
dest.useOcclusionCulling = false;
dest.useOcclusionCulling = true;
UniversalAdditionalCameraData camData;
if (dest.gameObject.TryGetComponent(out camData))
{
camData.renderShadows = m_settings.m_Shadows; // turn off shadows for the reflection camera
}
m_ReflectionCamera = CreateMirrorObjects(realCamera);
m_ReflectionCamera = CreateMirrorObjects();
// find out the reflection plane: position and normal in world space
Vector3 pos = Vector3.zero;

return new Vector4(cnormal.x, cnormal.y, cnormal.z, -Vector3.Dot(cpos, cnormal));
}
private Camera CreateMirrorObjects(Camera currentCamera)
private Camera CreateMirrorObjects()
GameObject go =
new GameObject($"Planar Refl Camera id{GetInstanceID().ToString()} for {currentCamera.GetInstanceID().ToString()}",
typeof(Camera));
UniversalAdditionalCameraData lwrpCamData =
go.AddComponent(typeof(UniversalAdditionalCameraData)) as UniversalAdditionalCameraData;
//UniversalAdditionalCameraData lwrpCamDataCurrent = currentCamera.GetComponent<UniversalAdditionalCameraData>();
lwrpCamData.renderShadows = m_settings.m_Shadows; // turn off shadows for the reflection camera
lwrpCamData.requiresColorOption = CameraOverrideOption.Off;
lwrpCamData.requiresDepthOption = CameraOverrideOption.Off;
GameObject go = new GameObject("Planar Reflections",typeof(Camera));
var cameraData = go.AddComponent(typeof(UniversalAdditionalCameraData)) as UniversalAdditionalCameraData;
cameraData.requiresColorOption = CameraOverrideOption.Off;
cameraData.requiresDepthOption = CameraOverrideOption.Off;
cameraData.SetRenderer(1);
//reflectionCamera.targetTexture = m_ReflectionTexture;
reflectionCamera.allowMSAA = currentCamera.allowMSAA;
reflectionCamera.allowHDR = currentCamera.allowHDR;
go.hideFlags = HideFlags.HideAndDontSave;
return reflectionCamera;

ScriptableRenderContext context,
Camera camera)
{
if (camera.cameraType == CameraType.Reflection)
if (camera.cameraType == CameraType.Reflection || camera.cameraType == CameraType.Preview)
return;
GL.invertCulling = true;

QualitySettings.maximumLODLevel = 0;
QualitySettings.maximumLODLevel = 1;
QualitySettings.lodBias = bias * 2f;
UpdateReflectionCamera(camera);

if (m_ReflectionTexture == null)
{
bool useHDR10 = true;// SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.RGB111110Float);
bool useHDR10 = true;
RenderTextureFormat hdrFormat =
useHDR10 ? RenderTextureFormat.RGB111110Float : RenderTextureFormat.DefaultHDR;
m_ReflectionTexture = RenderTexture.GetTemporary(res.x, res.y, 16,

42
Packages/com.verasl.water-system/Scripts/Water.cs


using UnityEngine.Profiling;
using UnityEngine.Rendering;
using UnityEngine.Rendering.LWRP;
using UnityEngine.Rendering.Universal;
using Lightmapping = UnityEngine.Experimental.GlobalIllumination.Lightmapping;
namespace WaterSystem
{

return _Instance;
}
}
// Script references
private MainCameraAlign camAlign;
private PlanarReflections planarReflections;
private bool useComputeBuffer;
public bool computeOverride;

useComputeBuffer = false;
Init();
RenderPipelineManager.beginCameraRendering += BeginCameraRendering;
if(resources == null)
{
resources = Resources.Load("WaterResources") as WaterResources;
}
}
private void OnDisable() {

float roll = cam.transform.localEulerAngles.z;
Shader.SetGlobalFloat("_CameraRoll", roll);
Shader.SetGlobalMatrix("_InvViewProjection", (GL.GetGPUProjectionMatrix(cam.projectionMatrix, false) * cam.worldToCameraMatrix).inverse);
foreach (var mesh in resources.defaultWaterMeshes)
{
Graphics.DrawMesh(mesh, transform.localToWorldMatrix, resources.defaultSeaMaterial, gameObject.layer, cam);
}
}
private void SafeDestroy(Object o)

{
Shader.SetGlobalTexture("_WaterDepthMap", _bakedDepthTex);
}
//CaptureDepthMap();
if (!gameObject.TryGetComponent(out camAlign))
{
camAlign = gameObject.AddComponent<MainCameraAlign>();
camAlign.hideFlags = HideFlags.HideAndDontSave | HideFlags.HideInInspector;
}
if (!gameObject.TryGetComponent(out planarReflections))
{
planarReflections = gameObject.AddComponent<PlanarReflections>();
planarReflections.hideFlags = HideFlags.HideAndDontSave | HideFlags.HideInInspector;
}
planarReflections.m_settings = settingsData.planarSettings;
planarReflections.enabled = settingsData.refType == ReflectionType.PlanarReflection;
if(resources == null)
{
resources = Resources.Load("WaterResources") as WaterResources;
}
void Update()
{
waterTime = Time.time;
Shader.SetGlobalFloat("_GlobalTime", waterTime);
private void LateUpdate() {
if(Application.isPlaying)
GerstnerWavesJobs.UpdateHeights();
}
public void FragWaveNormals(bool toggle)

185
Packages/com.verasl.water-system/SeaVisual.prefab
文件差异内容过多而无法显示
查看文件

3
Packages/com.verasl.water-system/Shaders/GerstnerWaves.hlsl


#define GERSTNER_WAVES_INCLUDED
uniform uint _WaveCount; // how many waves, set via the water component
float _GlobalTime; // global scene time
struct Wave
{

half dir = dot(windDir, pos - (omniPos * omni)); // calculate a gradient along the wind direction
////////////////////////////position output calculations/////////////////////////
half calc = dir * w + -_GlobalTime * wSpeed; // the wave calculation
half calc = dir * w + -_Time.y * wSpeed; // the wave calculation
half cosCalc = cos(calc); // cosine version(used for horizontal undulation)
half sinCalc = sin(calc); // sin version(used for vertical undulation)

9
Packages/com.verasl.water-system/Shaders/WaterCommon.hlsl


{
input.normal = float3(0, 1, 0);
input.uv.zw = input.posWS.xz;
input.fogFactorNoise.y = ((noise((input.posWS.xz * 0.5) + _GlobalTime) + noise((input.posWS.xz * 1) + _GlobalTime)) * 0.25 - 0.5) + 1;
input.fogFactorNoise.y = ((noise((input.posWS.xz * 0.5) + _Time.y) + noise((input.posWS.xz * 1) + _Time.y)) * 0.25 - 0.5) + 1;
half4 screenUV = ComputeScreenPos(TransformWorldToHClip(input.posWS));
screenUV.xyz /= screenUV.w;

half4 waterFX = SAMPLE_TEXTURE2D(_WaterFXMap, sampler_ScreenTextures_linear_clamp, IN.preWaveSP.xy);
half animT = frac(_GlobalTime) * 16; // amination value for caustics(16 frames)
half t = _Time.x;
half2 detailBump = SAMPLE_TEXTURE2D(_SurfaceMap, sampler_SurfaceMap, IN.uv.zw * 0.25h + t + (IN.fogFactorNoise.y * 0.1)).xy; // TODO - check perf
half2 detailBump = SAMPLE_TEXTURE2D(_SurfaceMap, sampler_SurfaceMap, IN.uv.zw * 0.25h + _Time.y * 0.1h + (IN.fogFactorNoise.y * 0.1)).xy; // TODO - check perf
IN.normal += (half3(detailBump.x, 0.5h, detailBump.y) * 2 - 1) * _BumpScale;
IN.normal += half3(1-waterFX.y, 0.5h, 1-waterFX.z) - 0.5;

Light mainLight = GetMainLight();
// Foam
float2 foamMapUV = (IN.uv.zw * 0.1) + (detailBump.xy * 0.0025) + half2(IN.fogFactorNoise.y * 0.1, (1-IN.fogFactorNoise.y) * 0.1) + _GlobalTime * 0.05;
float2 foamMapUV = (IN.uv.zw * 0.1) + (detailBump.xy * 0.0025) + half2(IN.fogFactorNoise.y * 0.1, (1-IN.fogFactorNoise.y) * 0.1) + _Time.y * 0.05;
half3 foamMap = SAMPLE_TEXTURE2D(_FoamMap, sampler_FoamMap, foamMapUV).rgb; //r=thick, g=medium, b=light
half shoreMask = pow(((1-depth.y + 9) * 0.1), 6);
half foamMask = (IN.additionalData.z);

63
Packages/com.verasl.water-system/Textures/WaterSurface_atlas.tif.meta


fileFormatVersion: 2
guid: e5587917cd9a44d8a98a83b31e3f69c3
TextureImporter:
fileIDToRecycleName: {}
internalIDToNameTable: []
serializedVersion: 7
serializedVersion: 10
mipMapMode: 0
mipMapMode: 1
enableMipMap: 1
sRGBTexture: 1
linearTexture: 0

maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: -1
aniso: -1
filterMode: 2
aniso: 4
mipBias: -100
wrapU: -1
wrapV: -1

compressionQualitySet: 0
textureFormatSet: 0
platformSettings:
- serializedVersion: 2
- serializedVersion: 3
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0

allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: iPhone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Android
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: WebGL
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet:
serializedVersion: 2
sprites: []

spriteID:
internalID: 0
secondaryTextures: []
spritePackingTag:
pSDRemoveMatte: 0
pSDShowRemoveMatteOption: 0

985
Packages/com.verasl.water-system/Textures/WaterSurface_single.tif
文件差异内容过多而无法显示
查看文件

102
Packages/com.verasl.water-system/Shaders/Caustics.shader


Shader "Hidden/BoatAttack/Caustics"
{
Properties
{
//Vector1_F3303B3C("Speed", Float) = 0.5
_Size("Size", Float) = 0.5
[NoScaleOffset]_CausticMap("Caustics", 2D) = "white" {}
_WaterLevel("WaterLevel", Float) = -0.25
_BlendDistance("BlendDistance", Float) = 0.1
//Vector1_CD857B77("CausticsRGB Split", Float) = 2
}
SubShader
{
// No culling or depth
Cull Off
ZWrite Off
ZTest Always
Pass
{
Blend DstColor Zero, One Zero
HLSLPROGRAM
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#pragma vertex vert
#pragma fragment frag
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float2 uv : TEXCOORD0;
float4 positionOS : SV_POSITION;
};
TEXTURE2D(_CameraDepthTexture); SAMPLER(sampler_CameraDepthTexture);
TEXTURE2D(_CausticMap); SAMPLER(sampler_CausticMap);
half _Size;
half _WaterLevel;
half _BlendDistance;
float3 ReconstructWorldPos(half2 screenPos, float depth)
{
// World Pos reconstriction
float4 raw = mul(UNITY_MATRIX_I_VP, float4(screenPos * 2 - 1, depth, 1));
float3 worldPos = raw.rgb / raw.a;
return worldPos;
}
float2 CausticUVs(float2 rawUV, float2 offset)
{
//anim
float2 uv = rawUV * _Size + float2(_Time.y, _Time.x) * 0.1;
return uv + offset * 0.25;
}
Varyings vert (Attributes input)
{
Varyings output = (Varyings)0;
UNITY_SETUP_INSTANCE_ID(input);
UNITY_TRANSFER_INSTANCE_ID(input, output);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
output.positionOS = vertexInput.positionCS;
output.uv = input.uv;
return output;
}
sampler2D _MainTex;
real4 frag (Varyings input) : SV_Target
{
real depth = SAMPLE_DEPTH_TEXTURE( _CameraDepthTexture, sampler_CameraDepthTexture, input.uv);
float3 worldPos = ReconstructWorldPos(input.uv, depth);
float waveOffset = SAMPLE_TEXTURE2D(_CausticMap, sampler_CausticMap, worldPos.xz * 0.025 + _Time.x * 0.25).w - 0.5;
float2 causticUV = CausticUVs(worldPos.xz, waveOffset);
float3 caustics = SAMPLE_TEXTURE2D(_CausticMap, sampler_CausticMap, causticUV).bbb;
half upperMask = saturate(-worldPos.y + _WaterLevel);
half lowerMask = saturate((worldPos.y - _WaterLevel) / _BlendDistance + _BlendDistance);
caustics *= min(upperMask, lowerMask) * 1.5;
//return half4(waveOffset.xx, 0, 1);
return half4(caustics + 1, 1);
}
ENDHLSL
}
}
}

9
Packages/com.verasl.water-system/Shaders/Caustics.shader.meta


fileFormatVersion: 2
guid: 90939d2e4b62841d29c136c866715501
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存