Created
October 3, 2016 20:53
-
-
Save Wikzo/0712fad567e3296eed58d64ffd908ac3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// via http://williamchyr.com/2013/11/unity-shaders-depth-and-normal-textures/ | |
Shader "Custom/DepthGrayscale" { | |
SubShader{ | |
Tags{ "RenderType" = "Opaque" } | |
Pass{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
sampler2D _CameraDepthTexture; | |
struct v2f | |
{ | |
float4 pos : SV_POSITION; | |
float4 scrPos:TEXCOORD1; | |
}; | |
//Vertex Shader | |
v2f vert(appdata_base v) | |
{ | |
v2f o; | |
o.pos = mul(UNITY_MATRIX_MVP, v.vertex); | |
o.scrPos = ComputeScreenPos(o.pos); | |
//for some reason, the y position of the depth texture comes out inverted | |
//o.scrPos.y = 1 - o.scrPos.y; | |
return o; | |
} | |
//Fragment Shader | |
half4 frag(v2f i) : COLOR{ | |
float depthValue = Linear01Depth(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.scrPos)).r); | |
half4 depth; | |
depth.r = depthValue; | |
depth.g = depthValue; | |
depth.b = depthValue; | |
depth.a = 1; | |
return depth; | |
} | |
ENDCG | |
} | |
} | |
FallBack "Diffuse" | |
} |
This file contains hidden or 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
using UnityEngine; | |
using System.Collections; | |
// ATTACHED ON THE CAMERA | |
[ExecuteInEditMode] | |
public class RenderTextureTest : MonoBehaviour | |
{ | |
// https://docs.unity3d.com/ScriptReference/Texture2D.ReadPixels.html | |
public Material mat; | |
public Texture2D tex; | |
public RenderTexture ShadowRenderTexture; | |
private Camera _camera; | |
void Start() | |
{ | |
_camera = GetComponent<Camera>(); | |
_camera.depthTextureMode = DepthTextureMode.Depth; | |
ShadowRenderTexture = _camera.targetTexture; | |
} | |
void OnRenderImage(RenderTexture source, RenderTexture destination) | |
{ | |
Graphics.Blit(source, destination, mat); | |
//mat is the material which contains the shader | |
//we are passing the destination RenderTexture to | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
works in URP ?