Shader "Unlit/TestShader1"
{
Properties
{
_MainTex ("Object Texture", 2D) = "white" {}
_PosTex ("Position Texture", 2D) = "white" {}
_minScale("Min Value for Scale", Float) = -100.0
_maxScale("Max Value for Scale", Float) = 100.0
}
SubShader
{
Tags { "RenderType"="Opaque" }
Pass
{
CGPROGRAM
#pragma target 3.0
#pragma only_renderers d3d11
#pragma enable_d3d11_debug_symbols
#pragma vertex vertexShader
#pragma fragment fragmentShader
#include "UnityCG.cginc"
struct VS_IN
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION; // clipspace pos
float2 uv : TEXCOORD0; // uv coords
float3 wpos : TEXCOORD1; // World pos
};
sampler2D _MainTex;
sampler2D _PosTex;
float4 _MainTex_ST;
float4 _PosTex_ST;
float4 _PosTex_TexelSize;
float _minScale;
float _maxScale;
v2f vertexShader(VS_IN v)
{
v2f o;
o.wpos = mul(unity_ObjectToWorld, v.vertex).xyz; // World pos pixel
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); // Screen space pos pixel
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 fragmentShader (v2f i) : SV_Target
{
// i.vertex: Pixel position in screen space (pixel value)
// i.pos: Pixel position in world space
// _PosTex_TexelSize.zw is width, height of texture
float2 bgUV = TRANSFORM_TEX((i.vertex.xy / _PosTex_TexelSize.zw), _PosTex);
float fScaleRange = (_maxScale - _minScale);
float4 C4DPosInfo = tex2D(_PosTex, bgUV) * fScaleRange + _minScale;
float distC4D = distance(C4DPosInfo.rgb, float3(_WorldSpaceCameraPos.xyz));
float distNow = distance(i.wpos, float3(_WorldSpaceCameraPos.xyz));
float4 colObj = tex2D(_MainTex, i.uv);
if (distNow > distC4D) discard;
//if (distNow > distC4D) colObj = float4(0.0, 1.0, 1.0, 1.0); For debugging
return colObj;
}
ENDCG
}
}
}