Search Unity

Question Problem with blend shader

Discussion in 'Shaders' started by unkownfire, Feb 6, 2023.

  1. unkownfire

    unkownfire

    Joined:
    Feb 18, 2017
    Posts:
    2
    So I'm trying to create a Texture2D dynamically, add it to a material with a shader, then make updates to that texture using SetPixel. The shader code is just supposed to lerp between each pixel between the main texture and the dynamically created mask texture. The goal is to be able to draw on sprites. For some reason, changing the pixels of the textures does nothing unless you change the first pixel at 0,0. Then it the whole sprite is lerped by the color of the pixel at 0,0. I'm trying to figure out if there is something wrong with my shader code or if it has something to do with how I'm creating the texture.

    Shader code
    Code (CGPROGRAM):
    1. Shader "Custom/Blend"
    2. {
    3.     Properties
    4.     {
    5.         _Color("Color", Color) = (1,1,1,1)
    6.         _MainTex("Texture", 2D) = "white" {}
    7.         _MaskTex("Texture", 2D) = "transparent" {}
    8.         _Blend("Blend Amount", Float) = 0.25
    9.     }
    10.     SubShader
    11.     {
    12.         Cull Off
    13.         Blend One OneMinusSrcAlpha
    14.         Pass
    15.         {
    16.             CGPROGRAM
    17.             #pragma enable_d3d11_debug_symbols
    18.             #pragma vertex vert
    19.             #pragma fragment frag
    20.             // Physically based Standard lighting model, and enable shadows on all light types
    21.             #include "UnityCG.cginc"
    22.             // Use shader model 3.0 target, to get nicer looking lighting
    23.             #pragma target 3.0
    24.  
    25.            
    26.             struct appdata
    27.             {
    28.                 float4 vertex : POSITION;
    29.                 float2 uv : TEXCOORD0;
    30.                 float4 color : COLOR0;
    31.             };
    32.  
    33.             struct v2f {
    34.                 float4 pos : SV_POSITION;
    35.                 float2 uv : TEXCOORD0;
    36.                 float2 uv2 : TEXCOORD1;
    37.             };
    38.  
    39.             float4 _Color;
    40.             sampler2D _MainTex;
    41.             sampler2D _MaskTex;
    42.             float4 _MainTex_ST;
    43.             float4 _MaskTex_ST;
    44.             float _Blend;
    45.             v2f vert(appdata v)
    46.             {
    47.                 v2f o;
    48.                 o.pos = UnityObjectToClipPos(v.vertex);
    49.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    50.                 o.uv2 = TRANSFORM_TEX(v.uv, _MaskTex);
    51.  
    52.                 return o;
    53.             }
    54.  
    55.            
    56.  
    57.             fixed4 frag(v2f i) : SV_Target
    58.             {
    59.                 fixed4 texture1 = tex2D(_MainTex, i.uv);
    60.                 fixed4 texture2 = tex2D(_MaskTex, i.uv);
    61.                 fixed4 col = lerp(texture1, texture2, _Blend.x);
    62.                 return col;
    63.             }
    64.  
    65.         ENDCG
    66.         }
    67.     }
    68.     FallBack "Diffuse"
    69. }
    Code where the texture is created.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Paintable : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     int TEXTURE_WIDTH;
    9.     [SerializeField]
    10.     int TEXTURE_HEIGHT;
    11.  
    12.  
    13.     public Texture2D maskTexture;
    14.  
    15.     int maskTextureID = Shader.PropertyToID("_MaskTexture");
    16.     Renderer rend;
    17.  
    18.     BitArray bitArray;
    19.  
    20.     public Renderer getRenderer() => rend;
    21.  
    22.     private void Start()
    23.     {
    24.        
    25.         bitArray = new BitArray(TEXTURE_WIDTH * TEXTURE_HEIGHT, false);
    26.  
    27.         maskTexture= new Texture2D(TEXTURE_WIDTH, TEXTURE_HEIGHT);
    28.         maskTexture.filterMode = FilterMode.Point;
    29.         maskTexture.wrapMode = TextureWrapMode.Clamp;
    30.        
    31.         rend = GetComponent<Renderer>();
    32.         rend.material.SetTexture(maskTextureID, maskTexture);
    33.  
    34.     }
    35. }