【问题标题】:Shader to create hole in sprite Unity 2d着色器在精灵 Unity 2d 中创建孔
【发布时间】: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
    }
    }
}

【问题讨论】:

    标签: unity3d 2d shader sprite


    【解决方案1】:

    在几何体中“挖洞”的效果可以使用顶点着色器来实现。这是可能的,因为顶点着色器允许您使用 _Object2World 矩阵确定对象像素的 3d 世界位置。如果我不在手机上,我会给出一个代码示例,但我会尝试描述它。如果你取顶点位置,应用一个 _Object2World 矩阵,它将返回一个 3d 世界位置。这是一个简单的例子: float3 worldPos = mul (_Object2World, v.vertex).xyz; 在着色器的片段部分,您可以根据 worldPos 和任何其他位置之间的距离设置透明度。如果这仍然难以理解,我深表歉意,我现在正尝试在手机上执行此操作。如果有人想详细说明我所说的或提供代码示例,那就太好了。否则,您可以在此处研究顶点着色器: http://docs.unity3d.com/Manual/SL-VertexFragmentShaderExamples.html

    希望这会有所帮助!

    【讨论】:

    • 所以,我目前正在使用一些图像效果着色器。之前,我正在研究表面着色器,我真的不知道是否有区别。但是现在,我尝试在着色器v2f vert(appdata v) { v2f o; o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); o.uv = v.uv; return o; } sampler2D _MainTex; fixed4 frag(v2f i) : SV_Target { fixed4 col; col.rgb = tex2D(_MainTex, i.uv).rgb; // just invert the colors return col; } 上显示我的纹理,但我的精灵仍然是白色的。我的下一步应该是在 vert 函数中创建 WorldPos 并在下一个函数中使用它
    猜你喜欢
    • 1970-01-01
    • 2016-05-18
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多