【发布时间】:2016-03-21 13:35:17
【问题描述】:
我目前正在开发一个角色可以在背景上移动的游戏。这个想法将是这个角色挖掘这个背景。我认为它应该由着色器完成,但我是它的初学者。 .
我可以想象如果字符离这个像素的位置太远,所以这个像素的 alpha 为 0。并且保持 alpha 为 0。如果 alpha 等于 0,你不会性格测试。
但是现在,我已经尝试使用精灵/漫反射着色器作为基础,但我找不到获取像素位置的方法。我在 surf 函数中尝试了一些东西,但没有真正的结果 surf 函数应该按像素执行一次吗?
在此先感谢您的帮助。
编辑 我已经尝试了一些方法来最终获得一个垂直片段着色器。正如我所说,我正在尝试用像素的距离计算 alpha。 现在我不知道我的错误在哪里,但也许有些人会更健谈。 顺便说一句,当我放置这个新着色器时,我的排序层刚刚爆炸。我试图关闭 Ztest 和 Zwrite 但它不起作用。因此,如果您有任何想法(但这不是主要问题)
Shader "Unlit/SimpleUnlitTexturedShader"
{
Properties
{
// we have removed support for texture tiling/offset,
// so make them not be displayed in material inspector
[NoScaleOffset] _MainTex("Texture", 2D) = "white" {}
_Position("Position", Vector) = (0,0,0,0)
}
SubShader
{
Pass
{
CGPROGRAM
// use "vert" function as the vertex shader
#pragma vertex vert
// use "frag" function as the pixel (fragment) shader
#pragma fragment frag
// vertex shader inputs
struct appdata
{
float4 vertex : POSITION; // vertex position
float2 uv : TEXCOORD0; // texture coordinate
};
// vertex shader outputs ("vertex to fragment")
struct v2f
{
float2 uv : TEXCOORD0; // texture coordinate
float4 vertex : SV_POSITION; // clip space position
float3 worldpos:WD_POSITION;
};
// vertex shader
v2f vert(appdata v)
{
v2f o;
// transform position to clip space
// (multiply with model*view*projection matrix)
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
// just pass the texture coordinate
o.uv = v.uv;
o.worldpos= mul(_Object2World,o.vertex).xyz;
return o;
}
// texture we will sample
sampler2D _MainTex;
float4 Position ;
// pixel shader; returns low precision ("fixed4" type)
// color ("SV_Target" semantic)
fixed4 frag(v2f i) : SV_Target
{
// sample texture and return it
fixed4 col = tex2D(_MainTex, i.uv);
col.a = step(2,distance(Position.xyz, i.worldpos));
return col;
}
ENDCG
}
}
}
【问题讨论】: