Created
November 22, 2020 13:59
-
-
Save herohiralal/44cd4e5d4910c73cb91db47bd03a2082 to your computer and use it in GitHub Desktop.
Interior Mapping shader written in HLSL to be used with URP.
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
Shader "HiraEngine/URP/InteriorMapping" | |
{ | |
Properties | |
{ | |
_Cubemap("Cubemap", CUBE) = "" {} | |
_Depth("Depth", Float) = 0 | |
} | |
SubShader | |
{ | |
Tags | |
{ | |
"RenderPipeline"="UniversalPipeline" | |
"RenderType"="Opaque" | |
"UniversalMaterialType" = "Unlit" | |
"Queue"="Geometry" | |
} | |
Pass | |
{ | |
Name "Pass" | |
// Render State | |
Cull Back | |
Blend One Zero | |
ZTest LEqual | |
ZWrite On | |
// -------------------------------------------------- | |
// Pass | |
HLSLPROGRAM | |
// Includes | |
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl" | |
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" | |
// Pragmas | |
#pragma vertex Vert | |
#pragma fragment Frag | |
// -------------------------------------------------- | |
// Structs | |
struct Attributes | |
{ | |
half3 PositionOs : POSITION; | |
half3 NormalOs : NORMAL; | |
half4 TangentOs : TANGENT; | |
half4 Uv0 : TEXCOORD0; | |
}; | |
struct VertexToFragment | |
{ | |
half4 PositionCs : SV_POSITION; | |
half3 NormalWs : TEXCOORD0; | |
half4 TangentWs : TEXCOORD1; | |
half4 Uv0 : TEXCOORD2; | |
half3 ViewDirectionWs : TEXCOORD3; | |
}; | |
// -------------------------------------------------- | |
// Graph | |
CBUFFER_START(UnityPerMaterial) | |
half4 _Cubemap_ST; | |
half _Depth; | |
CBUFFER_END | |
TEXTURECUBE(_Cubemap); | |
SAMPLER(sampler_Cubemap); | |
// -------------------------------------------------- | |
// Main | |
VertexToFragment Vert(Attributes Input) | |
{ | |
VertexToFragment Output; | |
half3 PositionWs = TransformObjectToWorld(Input.PositionOs); | |
Output.NormalWs = TransformObjectToWorldNormal(Input.NormalOs); | |
Output.TangentWs = half4(TransformObjectToWorldDir(Input.TangentOs.xyz), Input.TangentOs.w); | |
Output.PositionCs = TransformWorldToHClip(PositionWs); | |
Output.Uv0 = Input.Uv0; | |
Output.ViewDirectionWs = GetWorldSpaceViewDir(PositionWs); | |
return Output; | |
} | |
half4 Frag(VertexToFragment Input) : SV_TARGET | |
{ | |
half ReNormFactor = 1.0 / length(Input.NormalWs); | |
half CrossSign = (Input.TangentWs.w > 0.0 ? 1.0 : -1.0) * GetOddNegativeScale(); | |
half3 BiTangentOs = CrossSign * cross(Input.NormalWs.xyz, Input.TangentWs.xyz); | |
// Get Tangent Space View Direction | |
half3 NormalWs = ReNormFactor * Input.NormalWs.xyz; | |
half3 TangentWs = ReNormFactor * Input.TangentWs.xyz; | |
half3 BiTangentWs = ReNormFactor * BiTangentOs; | |
half3x3 TangentSpaceTransform = half3x3(TangentWs, BiTangentWs, NormalWs); | |
half3 ViewDirTs = mul(TangentSpaceTransform, Input.ViewDirectionWs); | |
// Get cubemap view direction | |
half3 Id = 1 / ViewDirTs; | |
half2 Pos = ((frac((Input.Uv0.xy * (_Cubemap_ST.xy * -1)) + _Cubemap_ST.zw) * 2) - 1).xy; | |
half3 Val = half3(Pos, _Depth * -1); | |
half3 K = abs(Id) - (Id * Val); | |
half KMin = min(min(K.x, K.y), K.z); | |
half3 CubemapViewDir = (ViewDirTs * KMin) + Val; | |
return SAMPLE_TEXTURECUBE(_Cubemap, sampler_Cubemap, CubemapViewDir); | |
} | |
ENDHLSL | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment