-
-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shader for rendering alpha stuck to ocean surface
- Loading branch information
Showing
11 changed files
with
374 additions
and
5 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/unity/Assets/Crest-Examples/Materials/OceanAlpha_CrestLogo.mat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] |
8 changes: 8 additions & 0 deletions
8
src/unity/Assets/Crest-Examples/Materials/OceanAlpha_CrestLogo.mat.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/unity/Assets/Crest-Examples/Scripts/RenderAlphaOnSurface.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/unity/Assets/Crest-Examples/Scripts/RenderAlphaOnSurface.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
92 changes: 92 additions & 0 deletions
92
src/unity/Assets/Crest-Examples/Shaders/OceanSurfaceAlpha.shader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/unity/Assets/Crest-Examples/Shaders/OceanSurfaceAlpha.shader.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file added
BIN
+313 KB
src/unity/Assets/Crest-Examples/Textures/crest-oceanrender-logomark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.