Last active
May 28, 2022 20:14
-
-
Save partybusiness/b7ec339d16d3f3b7e2b202ac4e5d8686 to your computer and use it in GitHub Desktop.
Depth Cube
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
Shader "Unlit/BorderedDepthCube" | |
{ | |
Properties | |
{ | |
_Color("Colour", Color) = (1,1,1,1) | |
_BorderWidth("Border Width", float) = 0.03 | |
_BorderStrength("Border Strength", float) = 0.3 | |
} | |
SubShader | |
{ | |
Tags { | |
"RenderType"="Transparent" | |
"Queue" = "Transparent" | |
"DisableBatching" = "True" | |
} | |
LOD 100 | |
Blend SrcAlpha OneMinusSrcAlpha | |
Cull Back | |
ZWrite Off | |
CGINCLUDE | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
float2 uv : TEXCOORD0; | |
float4 normal : NORMAL; | |
float4 tangent : TANGENT; | |
}; | |
struct v2f | |
{ | |
float4 vertex : SV_POSITION; | |
float2 uv : TEXCOORD0; | |
float3 viewDir : TEXCOORD1; | |
}; | |
float4 _Color; | |
float _BorderWidth; | |
float _BorderStrength; | |
ENDCG | |
Pass | |
{ | |
CGPROGRAM | |
v2f vert (appdata v) | |
{ | |
v2f o; | |
o.vertex = UnityObjectToClipPos(v.vertex); | |
float3 binormal = cross(v.normal, v.tangent.xyz) * v.tangent.w; | |
float3 viewDirInObjectCoords = mul( | |
unity_WorldToObject, float4(_WorldSpaceCameraPos, 1.0)).xyz | |
- v.vertex.xyz; | |
float3x3 localSurface2ScaledObjectT = | |
float3x3(v.tangent.xyz, binormal, v.normal.xyz); | |
float3 viewDirT = mul(localSurface2ScaledObjectT, viewDirInObjectCoords); | |
o.viewDir = viewDirT; | |
o.uv = v.uv; | |
return o; | |
} | |
fixed4 frag (v2f i) : SV_Target | |
{ | |
fixed3 viewDir = normalize(i.viewDir); | |
float distToGround = max(length((i.uv.y / i.viewDir.y)* i.viewDir), (i.viewDir.y<0)*3); | |
float distToRoof = max(length((-(1.0f - i.uv.y) / i.viewDir.y)* i.viewDir), (i.viewDir.y>0)*3); | |
float distToLeft = max(length(((i.uv.x ) / i.viewDir.x) * i.viewDir), (i.viewDir.x<0)*3); | |
float distToRight = max(length((-(1.0f - i.uv.x ) / i.viewDir.x) * i.viewDir), (i.viewDir.x>0)*3); | |
float distToBack = length(1 / i.viewDir.z * i.viewDir); | |
float minDist = min(distToBack, min( min(distToGround, distToRoof), min(distToLeft, distToRight))); | |
float2 twoBorders = 1 - abs(i.uv - 0.5) * 2; | |
float border = (1-smoothstep(0, _BorderWidth, min(twoBorders.x,twoBorders.y))); | |
border = smoothstep(0,minDist, _BorderWidth) * _BorderStrength; | |
minDist += border; | |
return fixed4(_Color.rgb, _Color.a * clamp(0,1,minDist)); | |
} | |
ENDCG | |
} | |
} | |
FallBack "Particles/AlphaBlended" | |
} |
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
Shader "Unlit/DepthCube" | |
{ | |
Properties | |
{ | |
_Color("Colour", Color) = (1,1,1,1) | |
} | |
SubShader | |
{ | |
Tags { | |
"RenderType"="Transparent" | |
"Queue" = "Transparent" | |
"DisableBatching" = "True" | |
} | |
LOD 100 | |
Blend SrcAlpha OneMinusSrcAlpha | |
Cull Back | |
ZWrite Off | |
CGINCLUDE | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
float2 uv : TEXCOORD0; | |
float4 normal : NORMAL; | |
float4 tangent : TANGENT; | |
}; | |
struct v2f | |
{ | |
float4 vertex : SV_POSITION; | |
float2 uv : TEXCOORD0; | |
float3 viewDir : TEXCOORD1; | |
}; | |
float4 _Color; | |
ENDCG | |
Pass | |
{ | |
CGPROGRAM | |
v2f vert (appdata v) | |
{ | |
v2f o; | |
o.vertex = UnityObjectToClipPos(v.vertex); | |
float3 binormal = cross(v.normal, v.tangent.xyz) * v.tangent.w; | |
float3 viewDirInObjectCoords = mul( | |
unity_WorldToObject, float4(_WorldSpaceCameraPos, 1.0)).xyz | |
- v.vertex.xyz; | |
float3x3 localSurface2ScaledObjectT = | |
float3x3(v.tangent.xyz, binormal, v.normal.xyz); | |
float3 viewDirT = mul(localSurface2ScaledObjectT, viewDirInObjectCoords); | |
o.viewDir = viewDirT; | |
o.uv = v.uv; | |
return o; | |
} | |
fixed4 frag (v2f i) : SV_Target | |
{ | |
fixed3 viewDir = normalize(i.viewDir); | |
float distToGround = max(length((i.uv.y / i.viewDir.y)* i.viewDir), (i.viewDir.y<0)*3); | |
float distToRoof = max(length((-(1.0f - i.uv.y) / i.viewDir.y)* i.viewDir), (i.viewDir.y>0)*3); | |
float distToLeft = max(length(((i.uv.x ) / i.viewDir.x) * i.viewDir), (i.viewDir.x<0)*3); | |
float distToRight = max(length((-(1.0f - i.uv.x ) / i.viewDir.x) * i.viewDir), (i.viewDir.x>0)*3); | |
float distToBack = length(1 / i.viewDir.z * i.viewDir); | |
float minDist = min(distToBack, min( min(distToGround, distToRoof), min(distToLeft, distToRight))); | |
return fixed4(_Color.rgb, _Color.a * clamp(0,1,minDist)); | |
} | |
ENDCG | |
} | |
} | |
FallBack "Particles/AlphaBlended" | |
} |
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
Shader "Unlit/IconDepthCube" | |
{ | |
Properties | |
{ | |
_Color("Colour", Color) = (1,1,1,1) | |
_MainTex("Icon Texture", 2D) = "white" {} | |
_IconDepth("Icon Depth", float) = 0.5 | |
} | |
SubShader | |
{ | |
Tags { | |
"RenderType"="Transparent" | |
"Queue" = "Transparent" | |
"DisableBatching" = "True" | |
} | |
LOD 100 | |
Blend SrcAlpha OneMinusSrcAlpha | |
Cull Back | |
ZWrite Off | |
CGINCLUDE | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
float2 uv : TEXCOORD0; | |
float4 normal : NORMAL; | |
float4 tangent : TANGENT; | |
}; | |
struct v2f | |
{ | |
float4 vertex : SV_POSITION; | |
float2 uv : TEXCOORD0; | |
float3 viewDir : TEXCOORD1; | |
}; | |
float4 _Color; | |
sampler2D _MainTex; | |
float4 _MainTex_ST; | |
float _IconDepth; | |
ENDCG | |
Pass | |
{ | |
CGPROGRAM | |
v2f vert (appdata v) | |
{ | |
v2f o; | |
o.vertex = UnityObjectToClipPos(v.vertex); | |
float3 binormal = cross(v.normal, v.tangent.xyz) * v.tangent.w; | |
float3 viewDirInObjectCoords = mul( | |
unity_WorldToObject, float4(_WorldSpaceCameraPos, 1.0)).xyz | |
- v.vertex.xyz; | |
float3x3 localSurface2ScaledObjectT = | |
float3x3(v.tangent.xyz, binormal, v.normal.xyz); | |
float3 viewDirT = mul(localSurface2ScaledObjectT, viewDirInObjectCoords); | |
o.viewDir = viewDirT; | |
o.uv = v.uv; | |
return o; | |
} | |
fixed4 frag (v2f i) : SV_Target | |
{ | |
fixed3 viewDir = normalize(i.viewDir); | |
float distToGround = max(length((i.uv.y / i.viewDir.y)* i.viewDir), (i.viewDir.y<0)*3); | |
float distToRoof = max(length((-(1.0f - i.uv.y) / i.viewDir.y)* i.viewDir), (i.viewDir.y>0)*3); | |
float distToLeft = max(length(((i.uv.x ) / i.viewDir.x) * i.viewDir), (i.viewDir.x<0)*3); | |
float distToRight = max(length((-(1.0f - i.uv.x ) / i.viewDir.x) * i.viewDir), (i.viewDir.x>0)*3); | |
float distToBack = length(1 / i.viewDir.z * i.viewDir); | |
float4 centreTexture = tex2D(_MainTex, clamp(i.uv - i.viewDir.xy/i.viewDir.z * _IconDepth, 0,1)); | |
float minDist = min(distToBack, min( min(distToGround, distToRoof), min(distToLeft, distToRight))); | |
minDist = lerp(minDist, centreTexture.r, centreTexture.a); | |
return fixed4(_Color.rgb, _Color.a * minDist); | |
} | |
ENDCG | |
} | |
} | |
FallBack "Particles/AlphaBlended" | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's only sure to work on cubes and flat surfaces. It assumes a 0 to 1 uv map represents the face of a cube and calcs the distance to the other five faces. If you have something intersect the surface it will look wrong because it will still show the depth of the back of the cube rather than the depth of the intersecting object.
If you scale it up or down, the distance to the other side of the cube scales appropriately, but the opacity is always the same for one cube's length, no matter how large that cube is. So a large cube will seem like a lower density.
Maybe you could do a cool gem thing with clever manipulation of the normals.