Created
March 12, 2017 07:15
-
-
Save edom18/179e54ec304e52cd88c56f7ae6bdd334 to your computer and use it in GitHub Desktop.
[Unity] 深度バッファから法線を復元しようとしてうまくいかなかった話 ref: http://qiita.com/edo_m18/items/86029fd6c18a52ad4f3d
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 "Custom/DepthNormal" { | |
SubShader { | |
Tags { "RenderType"="Transparent" "Queue"="Transparent+10" } | |
LOD 200 | |
Pass | |
{ | |
CGPROGRAM | |
#include "UnityCG.cginc" | |
#pragma vertex vert | |
#pragma fragment frag | |
#pragma target 3.0 | |
uniform sampler2D _CameraDepthTexture; | |
uniform float4x4 _InverseProjection; | |
struct v2f | |
{ | |
float4 pos : SV_POSITION; | |
float2 uv : TEXCOORD0; | |
}; | |
v2f vert(appdata_img v) | |
{ | |
v2f o = (v2f)0; | |
o.pos = mul(UNITY_MATRIX_MVP, v.vertex); | |
o.uv = v.texcoord.xy; | |
return o; | |
} | |
float4 frag(v2f i) : SV_Target | |
{ | |
float ep = 0.001; | |
float2 xy1 = float2(i.uv.x + ep, i.uv.y + ep); | |
float2 xy2 = float2(i.uv.x - ep, i.uv.y + ep); | |
float2 xy3 = float2(i.uv.x - ep, i.uv.y - ep); | |
float2 xy4 = float2(i.uv.x + ep, i.uv.y - ep); | |
float z1 = tex2D(_CameraDepthTexture, xy1); | |
float z2 = tex2D(_CameraDepthTexture, xy2); | |
float z3 = tex2D(_CameraDepthTexture, xy3); | |
float z4 = tex2D(_CameraDepthTexture, xy4); | |
float4 pPos1 = float4(xy1 * 2 - 1, z1, 1.0); | |
float4 p1 = mul(_InverseProjection, pPos1); | |
p1.xyz /= p1.w; | |
float4 pPos2 = float4(xy2 * 2 - 1, z2, 1.0); | |
float4 p2 = mul(_InverseProjection, pPos2); | |
p2.xyz /= p2.w; | |
float4 pPos3 = float4(xy3 * 2 - 1, z3, 1.0); | |
float4 p3 = mul(_InverseProjection, pPos3); | |
p3.xyz /= p3.w; | |
float4 pPos4 = float4(xy4 * 2 - 1, z4, 1.0); | |
float4 p4 = mul(_InverseProjection, pPos4); | |
p4.xyz /= p4.w; | |
float3 v1 = normalize(p3.xyz - p1.xyz); | |
float3 v2 = normalize(p4.xyz - p2.xyz); | |
float3 n = normalize(cross(v1, v2)); | |
return half4(n, 1); | |
} | |
ENDCG | |
} | |
} | |
FallBack "Diffuse" | |
} |
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
float ep = 0.001; | |
float2 xy1 = float2(i.uv.x + ep, i.uv.y + ep); | |
float2 xy2 = float2(i.uv.x - ep, i.uv.y + ep); | |
float2 xy3 = float2(i.uv.x - ep, i.uv.y - ep); | |
float2 xy4 = float2(i.uv.x + ep, i.uv.y - ep); | |
float z1 = tex2D(_CameraDepthTexture, xy1); | |
float z2 = tex2D(_CameraDepthTexture, xy2); | |
float z3 = tex2D(_CameraDepthTexture, xy3); | |
float z4 = tex2D(_CameraDepthTexture, xy4); |
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
float4 pPos1 = float4(xy1 * 2 - 1, z1, 1.0); |
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
float4 p1 = mul(_InverseProjection, pPos1); | |
p1.xyz /= p1.w; |
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
float3 v1 = normalize(p3.xyz - p1.xyz); | |
float3 v2 = normalize(p4.xyz - p2.xyz); | |
float3 n = normalize(cross(v1, v2)); |
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
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
[ExecuteInEditMode] | |
public class DepthEnabler : MonoBehaviour | |
{ | |
public Material _material; | |
void Start() | |
{ | |
Camera.main.depthTextureMode |= DepthTextureMode.Depth; | |
} | |
private void OnRenderImage(RenderTexture source, RenderTexture destination) | |
{ | |
Matrix4x4 mat = Camera.main.projectionMatrix; | |
_material.SetMatrix("_InverseProjection", mat.inverse); | |
Graphics.Blit(source, destination, _material); | |
} | |
} |
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
Camera.main.depthTextureMode |= DepthTextureMode.Depth; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment