Trash maze MVP (#79)
Co-authored-by: Michal Pikulski <michal@foolhardyhorizons.com> Co-authored-by: Michal Pikulski <michal.a.pikulski@gmail.com> Reviewed-on: #79
This commit is contained in:
106
Assets/Shaders/TrashMaze/ObjectVisibilityProgressive.shader
Normal file
106
Assets/Shaders/TrashMaze/ObjectVisibilityProgressive.shader
Normal file
@@ -0,0 +1,106 @@
|
||||
Shader "TrashMaze/ObjectVisibilityProgressive"
|
||||
{
|
||||
Properties
|
||||
{
|
||||
_MainTex ("Normal Texture (Color)", 2D) = "white" {}
|
||||
_OutlineTex ("Outline Texture (White)", 2D) = "white" {}
|
||||
_RevealMask ("Reveal Mask", 2D) = "black" {}
|
||||
[PerRendererData] _IsInVision ("Is In Vision", Float) = 0
|
||||
}
|
||||
|
||||
SubShader
|
||||
{
|
||||
Tags
|
||||
{
|
||||
"Queue"="Transparent"
|
||||
"RenderType"="Transparent"
|
||||
"IgnoreProjector"="True"
|
||||
}
|
||||
|
||||
Blend SrcAlpha OneMinusSrcAlpha
|
||||
ZWrite Off
|
||||
Cull Off
|
||||
|
||||
Pass
|
||||
{
|
||||
HLSLPROGRAM
|
||||
#pragma vertex vert
|
||||
#pragma fragment frag
|
||||
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
|
||||
|
||||
struct Attributes
|
||||
{
|
||||
float4 positionOS : POSITION;
|
||||
float2 uv : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct Varyings
|
||||
{
|
||||
float2 uv : TEXCOORD0;
|
||||
float4 positionCS : SV_POSITION;
|
||||
float3 positionWS : TEXCOORD1;
|
||||
};
|
||||
|
||||
TEXTURE2D(_MainTex);
|
||||
SAMPLER(sampler_MainTex);
|
||||
TEXTURE2D(_OutlineTex);
|
||||
SAMPLER(sampler_OutlineTex);
|
||||
TEXTURE2D(_RevealMask);
|
||||
SAMPLER(sampler_RevealMask);
|
||||
float4 _MainTex_ST;
|
||||
|
||||
// Global shader properties (set by PulverController)
|
||||
float2 _PlayerWorldPos;
|
||||
float _VisionRadius;
|
||||
|
||||
Varyings vert(Attributes input)
|
||||
{
|
||||
Varyings output;
|
||||
VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
|
||||
output.positionCS = vertexInput.positionCS;
|
||||
output.positionWS = vertexInput.positionWS;
|
||||
output.uv = TRANSFORM_TEX(input.uv, _MainTex);
|
||||
return output;
|
||||
}
|
||||
|
||||
half4 frag(Varyings input) : SV_Target
|
||||
{
|
||||
// Sample reveal mask (0 = not revealed, 1 = revealed)
|
||||
float revealAmount = SAMPLE_TEXTURE2D(_RevealMask, sampler_RevealMask, input.uv).r;
|
||||
|
||||
// Binary decision: is this pixel revealed?
|
||||
bool isRevealed = revealAmount > 0.5;
|
||||
|
||||
// If pixel was never revealed, hide it completely
|
||||
if (!isRevealed)
|
||||
{
|
||||
return half4(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
// Calculate per-pixel distance to player in world space
|
||||
float2 pixelWorldPos = input.positionWS.xy;
|
||||
float distanceToPlayer = distance(pixelWorldPos, _PlayerWorldPos);
|
||||
|
||||
// Three-state logic per pixel:
|
||||
// 1. Never revealed -> hide (handled above)
|
||||
// 2. Revealed + currently in player vision radius -> show color
|
||||
// 3. Revealed + outside player vision radius -> show outline
|
||||
|
||||
if (distanceToPlayer < _VisionRadius)
|
||||
{
|
||||
// Pixel is revealed AND currently in vision - show color
|
||||
return SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.uv);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Pixel is revealed but NOT currently in vision - show outline
|
||||
return SAMPLE_TEXTURE2D(_OutlineTex, sampler_OutlineTex, input.uv);
|
||||
}
|
||||
}
|
||||
ENDHLSL
|
||||
}
|
||||
}
|
||||
|
||||
FallBack "Transparent/Diffuse"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user