Unity Shader Paint(未完成以后有时间再研究)

Shader "Unlit/PaintShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _BrushTex ("Brush Texture", 2D) = "white" {}
        _Pos("Pos",Vector)=(960,540,0,0)
        _Size("Size",float)=5
    }
    SubShader
    {
        Tags {"Queue"="Transparent" "IngnoreProjector"="True" "RenderType"="Transparent"  }
        ZWrite Off
        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float4 uv : TEXCOORD0;
            };

            struct v2f
            {
                float4 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            sampler2D _BrushTex;
            float4 _BrushTex_ST;
            Vector _Pos;
            float _Size;
            
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex);
                o.uv.zw = TRANSFORM_TEX(v.uv, _BrushTex);
                return o;
            }

            
            fixed4 frag (v2f i) : SV_Target
            {
                
                fixed4 col ;
                fixed4 mainCol  = tex2D(_MainTex, i.uv.xy);
                fixed4 brushCol;
                
                if(i.vertex.x >= _Pos.x-_Size && i.vertex.x <= _Pos.x+_Size && i.vertex.y >= _Pos.y-_Size && i.vertex.y <= _Pos.y+_Size){
                    // 这个地方这样做为啥不行呢  懵逼树下只有我
                    //if(mainCol.a >0)  
                    mainCol   = fixed4(0,0,0,1);
                }
                return mainCol;
            }
            ENDCG
        }
    }
}
using UnityEngine;
using UnityEngine.UI;

public class MyPaint : MonoBehaviour
{
   public Material material;
   Vector4 myPos;
   private RenderTexture _renderTex;
   RawImage _rawImage;
    void Start()
    {
        _rawImage = GetComponent<RawImage>();
        myPos= Vector4.zero;
        _renderTex = RenderTexture.GetTemporary(1920, 1080, 24);
        Graphics.Blit(_rawImage.texture,_renderTex);
        _rawImage.texture = _renderTex;
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButton(0)){
            // Debug.Log(Input.mousePosition);
            myPos.x= Input.mousePosition.x;
            myPos.y= Input.mousePosition.y;
            material.SetVector("_Pos",myPos);
            Graphics.Blit(_renderTex,_renderTex,material);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_39097425/article/details/112463004