Skip to content

Instantly share code, notes, and snippets.

@joonjoonjoon
Last active June 6, 2023 07:19
Show Gist options
  • Save joonjoonjoon/122899f8ec135fa443140bdaa68e27bd to your computer and use it in GitHub Desktop.
Save joonjoonjoon/122899f8ec135fa443140bdaa68e27bd to your computer and use it in GitHub Desktop.
// Apply this to a Camera, and make a material with the shader.
public class CameraRenderWithMaterial : MonoBehaviour
{
public Material material;
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
Graphics.Blit(source, destination, material);
}
}
Shader "Joon/TopoMap"
{
Properties
{
_startDepth ("Start Depth", float) = 0
_scale ("Depth Scale", float) = 0
_lineHeight ("Line Height", float) = 0
_Gradient("", 2D) = "" {}
_colorLine ("Color Line", Color) = (0,0,0,0) // color
}
SubShader{
Tags{ "RenderType" = "Opaque" }
Pass{
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
float4 worldPos : NORMAL;
};
float _startDepth;
float _scale;
float _lineHeight;
sampler2D_float _CameraDepthTexture;
float2 _CameraDepthTexture_TexelSize;
float4 _colorLine;
sampler2D _Gradient;
half4 frag(v2f_img i) : SV_Target
{
float3 viewDir = UNITY_MATRIX_V[2].xyz;
viewDir = normalize(viewDir);
// sample points of the depth texture
float2 uv1 = i.uv + float2(0,_CameraDepthTexture_TexelSize.y); // BR
float2 uv2 = i.uv + float2(_CameraDepthTexture_TexelSize.x, 0); // TR
// Depth samples
float zs0 = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv);
float zs1 = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, uv1);
float zs2 = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, uv2);
// Convert to linear depth values.
float z0 = floor(zs0 / _lineHeight);
float z1 = floor(zs1 / _lineHeight);
float z2 = floor(zs2 / _lineHeight);
// check if any point lies in a different height slice
float z = abs(z0-z1+z0-z2);
if(abs(z) > 0) return _colorLine;
// else, return gradient sampled color
float v = ((z0 * _lineHeight) - _startDepth) * _scale;
return tex2D(_Gradient, float2(v, 0));
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment