Anda dapat menggunakan ruang dunia untuk memudar karakter Anda.
Ruang objek (atau sistem koordinat objek) khusus untuk setiap objek game; namun, semua objek game ditransformasikan menjadi satu sistem koordinat yang umum - ruang dunia.
Jika objek game dimasukkan langsung ke ruang dunia, transformasi objek-ke-dunia ditentukan oleh komponen Transform objek game.
https://en.wikibooks.org/wiki/Cg_Programming/Unity/Shading_in_World_Space
Memudar oleh ruang dunia
Shader "Smkgames/worldSpaceFade" {
Properties{
_Size("Size",Vector) = (2,2,0,0)
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
struct vertexInput {
float4 vertex : POSITION;
};
struct vertexOutput {
float4 pos : SV_POSITION;
float4 position_in_world_space : TEXCOORD0;
};
vertexOutput vert(vertexInput input)
{
vertexOutput output;
output.pos = UnityObjectToClipPos(input.vertex);
output.position_in_world_space =
mul(unity_ObjectToWorld, input.vertex);
return output;
}
float2 _Size;
float4 frag(vertexOutput input) : COLOR
{
float3 world = input.position_in_world_space;
float4 equation = pow(world.x/_Size.x,8) + pow(world.z/_Size.y,8);
return smoothstep(1,0,equation);
}
ENDCG
}
}
}
Permukaan Shader
Setelah memahami ruang dunia kita dapat menggunakannya di permukaan shader kami:
Shader "Smkgames/worldSpaceFade" {
Properties {
_Size("Size",Vector) = (2,2,0,0)
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
Pass {
ZWrite On
ColorMask 0
}
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows alpha:fade
#pragma target 3.0
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
float3 worldPos: TEXCOORD2;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
float2 _Size;
void vert (inout appdata_full v, out Input o){
UNITY_INITIALIZE_OUTPUT(Input,o);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
}
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o) {
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
float4 equation = pow(IN.worldPos.x/_Size.x,8) + pow(IN.worldPos.z/_Size.y,8);
o.Albedo = c.rgb;
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = smoothstep(1,0,equation);
}
ENDCG
}
FallBack "Diffuse"
}