Skip to content

Commit

Permalink
Shader for rendering alpha stuck to ocean surface
Browse files Browse the repository at this point in the history
  • Loading branch information
huwb committed Jun 17, 2018
1 parent e52675d commit 1728555
Show file tree
Hide file tree
Showing 11 changed files with 374 additions and 5 deletions.
27 changes: 27 additions & 0 deletions src/unity/Assets/Crest-Examples/Materials/OceanAlpha_CrestLogo.mat
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: OceanAlpha_CrestLogo
m_Shader: {fileID: 4800000, guid: 2d1af474b068d704284d3480b6d80201, type: 3}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _MainTex:
m_Texture: {fileID: 2800000, guid: d254fdcebc9d1ce45a2d81cb7928a699, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _Alpha: 0.5
m_Colors: []

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 85 additions & 4 deletions src/unity/Assets/Crest-Examples/Scenes/main.unity
Original file line number Diff line number Diff line change
Expand Up @@ -460,13 +460,93 @@ Prefab:
propertyPath: m_RootOrder
value: 1
objectReference: {fileID: 0}
- target: {fileID: 11417796, guid: 8454e29c2a033974fb559f5f8eff1918, type: 2}
propertyPath: _baseVertDensity
value: 64
objectReference: {fileID: 0}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: 8454e29c2a033974fb559f5f8eff1918, type: 2}
m_IsPrefabParent: 0
--- !u!1 &1325202379
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 1325202383}
- component: {fileID: 1325202382}
- component: {fileID: 1325202381}
- component: {fileID: 1325202380}
m_Layer: 0
m_Name: CrestLogo
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1325202380
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1325202379}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c6727809da6522a4a851af2b1e732088, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!23 &1325202381
MeshRenderer:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1325202379}
m_Enabled: 1
m_CastShadows: 0
m_ReceiveShadows: 0
m_DynamicOccludee: 0
m_MotionVectors: 2
m_LightProbeUsage: 0
m_ReflectionProbeUsage: 0
m_RenderingLayerMask: 4294967295
m_Materials:
- {fileID: 2100000, guid: c2bfe9cdbddfb4a4aba12602cfa9c8ab, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 0
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
--- !u!33 &1325202382
MeshFilter:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1325202379}
m_Mesh: {fileID: 10209, guid: 0000000000000000e000000000000000, type: 0}
--- !u!4 &1325202383
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1325202379}
m_LocalRotation: {x: -0, y: -0.7044484, z: -0, w: 0.70975524}
m_LocalPosition: {x: 102.7, y: 0, z: 33.9}
m_LocalScale: {x: 0.5, y: 0.5, z: 0.5}
m_Children: []
m_Father: {fileID: 1566317005}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: -89.57, z: 0}
--- !u!1001 &1380102997
Prefab:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -568,6 +648,7 @@ Transform:
m_Children:
- {fileID: 1589415981}
- {fileID: 1591410378}
- {fileID: 1325202383}
m_Father: {fileID: 0}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
Expand Down
37 changes: 37 additions & 0 deletions src/unity/Assets/Crest-Examples/Scripts/RenderAlphaOnSurface.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using UnityEngine;
using Crest;

public class RenderAlphaOnSurface : MonoBehaviour
{
Material _mat;

void Start()
{
_mat = GetComponent<Renderer>().material;
}

private void LateUpdate()
{
// find which lod this object is overlapping
var rect = new Rect(transform.position.x, transform.position.z, 0f, 0f);
var idx = WaveDataCam.SuggestCollisionLOD(rect);

if (idx > -1)
{
var pwm = new PropertyWrapperMaterial(_mat);
var wdcs = OceanRenderer.Instance.Builder._shapeWDCs;
wdcs[idx].ApplyMaterialParams(0, pwm);
int idx1 = Mathf.Min(idx + 1, wdcs.Length - 1);
wdcs[idx1].ApplyMaterialParams(1, pwm);

// blend LOD 0 shape in/out to avoid pop, if the ocean might scale up later (it is smaller than its maximum scale)
bool needToBlendOutShape = idx == 0 && OceanRenderer.Instance.ScaleCouldIncrease;
float meshScaleLerp = needToBlendOutShape ? OceanRenderer.Instance.ViewerAltitudeLevelAlpha : 0f;

// blend furthest normals scale in/out to avoid pop, if scale could reduce
bool needToBlendOutNormals = idx == wdcs.Length - 1 && OceanRenderer.Instance.ScaleCouldDecrease;
float farNormalsWeight = needToBlendOutNormals ? OceanRenderer.Instance.ViewerAltitudeLevelAlpha : 1f;
pwm.SetVector("_InstanceData", new Vector4(meshScaleLerp, farNormalsWeight, idx));
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/unity/Assets/Crest-Examples/Shaders.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions src/unity/Assets/Crest-Examples/Shaders/OceanSurfaceAlpha.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
Shader "Ocean/Ocean Surface Alpha"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Alpha("Alpha Multiplier", Range(0.0, 1.0)) = 1.0
}
SubShader
{
Tags { "RenderType"="Transparent" "Queue"="Transparent" }
LOD 100

Pass
{
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
// Depth offset to stop intersection with water. "Factor" and "Units". typical seems to be (-1,-1). (-0.5,0) gives
// pretty good results for me when alpha geometry is fairly well matched but fails when alpha geo is too low res.
// the ludicrously large value below seems to work in most of my tests.
Offset 0, -1000000

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog

#include "UnityCG.cginc"
#include "../../Crest/Shaders/OceanLODData.cginc"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};

struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};

sampler2D _MainTex;
float4 _MainTex_ST;
half _Alpha;

// MeshScaleLerp, FarNormalsWeight, LODIndex (debug), unused
uniform float4 _InstanceData;

v2f vert (appdata v)
{
v2f o;

// move to world
float3 worldPos = mul(unity_ObjectToWorld, v.vertex);

// vertex snapping and lod transition
float lodAlpha = ComputeLodAlpha_FromLODs(worldPos, _InstanceData.x);

// sample shape textures - always lerp between 2 scales, so sample two textures
half3 n = half3(0., 1., 0.);
half det = 0., signedOceanDepth = 0.;
// sample weights. params.z allows shape to be faded out (used on last lod to support pop-less scale transitions)
float wt_0 = (1. - lodAlpha) * _WD_Params_0.z;
float wt_1 = (1. - wt_0) * _WD_Params_1.z;
// sample displacement textures, add results to current world pos / normal / foam
const float2 wxz = worldPos.xz;
SampleDisplacements(_WD_Sampler_0, _WD_OceanDepth_Sampler_0, _WD_Pos_Scale_0.xy, _WD_Params_0.y, _WD_Params_0.w, _WD_Params_0.x, wxz, wt_0, worldPos, n, det, signedOceanDepth);
SampleDisplacements(_WD_Sampler_1, _WD_OceanDepth_Sampler_1, _WD_Pos_Scale_1.xy, _WD_Params_1.y, _WD_Params_1.w, _WD_Params_1.x, wxz, wt_1, worldPos, n, det, signedOceanDepth);

// view-projection
o.vertex = mul(UNITY_MATRIX_VP, float4(worldPos, 1.));

o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}

fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);

UNITY_APPLY_FOG(i.fogCoord, col);

col.a *= _Alpha;

return col;
}
ENDCG
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 1728555

Please sign in to comment.