-
Notifications
You must be signed in to change notification settings - Fork 15
/
Internal-DepthNormalsTexture.shader
87 lines (71 loc) · 2.98 KB
/
Internal-DepthNormalsTexture.shader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
Shader "RoadOfShader/1.7-NormalTex/Internal-DepthNormalsTexture"
{
SubShader
{
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
Pass
{
Name "Depth-Normal"
HLSLPROGRAM
// Required to compile gles 2.0 with standard SRP library
// All shaders must be compiled with HLSLcc and currently only gles is not using HLSLcc by default
#pragma prefer_hlslcc gles
#pragma exclude_renderers d3d11_9x
#pragma target 2.0
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#pragma vertex vert
#pragma fragment frag
struct Attributes
{
float4 positionOS: POSITION;
float3 normalOS: NORMAL;
};
struct Varyings
{
float4 positionCS: SV_POSITION;
float4 normal_depth: TEXCOORD0;
};
// Encoding/decoding view space normals into 2D 0..1 vector
float2 EncodeViewNormalStereo(float3 n)
{
float kScale = 1.7777;
float2 enc;
enc = n.xy / (n.z + 1);
enc /= kScale;
enc = enc * 0.5 + 0.5;
return enc;
}
// Encoding/decoding [0..1) floats into 8 bit/channel RG. Note that 1.0 will not be encoded properly.
float2 EncodeFloatRG(float v)
{
float2 kEncodeMul = float2(1.0, 255.0);
float kEncodeBit = 1.0 / 255.0;
float2 enc = kEncodeMul * v;
enc = frac(enc);
enc.x -= enc.y * kEncodeBit;
return enc;
}
float4 EncodeDepthNormal(float depth, float3 normal)
{
float4 enc;
enc.xy = EncodeViewNormalStereo(normal);
enc.zw = EncodeFloatRG(depth);
return enc;
}
Varyings vert(Attributes input)
{
Varyings output = (Varyings)0;
VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
output.positionCS = vertexInput.positionCS;
output.normal_depth.xyz = mul((float3x3)UNITY_MATRIX_IT_MV, input.normalOS);
output.normal_depth.w = - (vertexInput.positionVS.z * _ProjectionParams.w);
return output;
}
half4 frag(Varyings input): SV_Target
{
return EncodeDepthNormal(input.normal_depth.w, input.normal_depth.xyz);
}
ENDHLSL
}
}
}