0
\$\begingroup\$

I want to fading my paint over time like this:

but currently It's gray! what Is the problem?

Gray Problem

Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


// Collider & Renderer Required
[RequireComponent(typeof(Renderer))]
[RequireComponent(typeof(Collider))]
public class Paint : MonoBehaviour {
    public Material PaintMat; 

    private Texture mainTexture; // The texture used in the original material
    private RenderTexture renderTexture; // Work render texture (drawn here)

    // shader's propertyID
    private int mainTexturePropertyID;
    private int paintUVPropertyID;


    // Use this for initialization
    void Start()
    {

        // property id set
        mainTexturePropertyID = Shader.PropertyToID("_MainTex");
        paintUVPropertyID = Shader.PropertyToID("_PaintUV");

        // Get the main texture set for the material
        mainTexture = PaintMat.GetTexture(mainTexturePropertyID);

        // create a renderTexture 
        renderTexture = new RenderTexture(mainTexture.width, mainTexture.height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default);

        // Copy main texture to render texture
        Graphics.Blit(mainTexture, renderTexture);

        // set the main texture for the material with the render texture
        PaintMat.SetTexture(mainTexturePropertyID, renderTexture);
    }

    // I hope this is necessary
    void OnDestroy()
    {
        renderTexture.Release(); // render texture open
        PaintMat.SetTexture(mainTexturePropertyID, mainTexture); // Return to the original texture
    }


    void Update()
    {

        // Acquire the uv of the touched place
        if (Input.GetMouseButton(0))
        {
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            if (Physics.Raycast(ray, out hitInfo))
            {
                // Put that
                PaintOnUV(hitInfo.textureCoord);
            }
        }else{
            PaintOnUV(new Vector2(1000,1000));//Just Update
        }
    }

    private void PaintOnUV(Vector2 uv)
    {
        // Create buffer 
        var renderTextureBuffer = RenderTexture.GetTemporary(renderTexture.width, renderTexture.height);
        PaintMat.SetVector(paintUVPropertyID, uv);

        Graphics.Blit(renderTexture, renderTextureBuffer, PaintMat); //Copy the material rendered texture to buffer , You can not write directly
        Graphics.Blit(renderTextureBuffer, renderTexture); // Write back from buffer to render texture
        RenderTexture.ReleaseTemporary(renderTextureBuffer); // Release buffer
    }
}

Shader:

   Shader "Hidden/Paint"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _PaintUV("Hit UV Position", VECTOR) = (0, 0, 0, 0) // Position to  painting with brush
        _FadeTime("FadeTime",Float) = 50
    }
    SubShader
    {
Tags {"Queue"="Transparent" "IgnoreProjector"="true" "RenderType"="Transparent"}
ZWrite Off Blend SrcAlpha OneMinusSrcAlpha Cull Off

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

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

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

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            sampler2D _MainTex,_Temp;
            float4 _PaintUV;
            float _FadeTime;

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 temp = tex2D(_MainTex, i.uv);
                i.uv -= _PaintUV.xy;
                i.uv +=0.5;
                float paint = length(i.uv-0.5 );
                paint = step(paint,0.05);
                float mask = paint+temp;
                float percent = 1.0/500;//why Is gray? can't fade completely!
                percent = 1.0/1000;//not Fade!!
                mask-= percent;
                return  mask;
            }
            ENDCG
        }
    }
}

Also you can download and test this from my github.

\$\endgroup\$
4
  • \$\begingroup\$ What range of values does _FadeTime get? \$\endgroup\$
    – user35344
    Commented Apr 26, 2018 at 8:19
  • \$\begingroup\$ @Tyyppi_77 float percent range is between 0 - 1 so when _FadeTime = 500 , 1/500.0 = 0.002.I store the result In RenderTexture and return It again by reducing alpha of previous result,this cycle creates the fading effect over the time. \$\endgroup\$ Commented Apr 26, 2018 at 16:45
  • 3
    \$\begingroup\$ I don't think you should edit the question to ask something different after getting a solution to the original question. Another question should be asked instead. \$\endgroup\$
    – user35344
    Commented Apr 27, 2018 at 15:11
  • \$\begingroup\$ @Tyyppi_77 ok I will edit It to the previous time \$\endgroup\$ Commented Apr 27, 2018 at 16:08

1 Answer 1

2
\$\begingroup\$

frag is declared as returning a Vector (float4). What you are actually returning is a Scalar (float). This results in a Scalar-to-Vector conversion which is defined as:

Scalar-to-Vector

Such conversions are always valid. This cast works by copying the scalar to fill the vector.

(Emphasis mine).

This means that when your code returns 0.02 what it's actually returning is (0.2, 0.2, 0.2, 0.2).

Or...gray.

\$\endgroup\$
6
  • \$\begingroup\$ thanks I could fix It.there Is only one problem => how can I set time to fading over time? \$\endgroup\$ Commented Apr 27, 2018 at 5:28
  • \$\begingroup\$ It's already "fading over time." You're going to need to clarify. \$\endgroup\$ Commented Apr 27, 2018 at 5:30
  • \$\begingroup\$ I need Lerp(Paint,0,FadeTime) Paint to fading.Length of time to fade.to clarify you can see this shader and change the fadeTime 50 to fadeTime 500 In Buf A.I just want to increase the trail length over time. \$\endgroup\$ Commented Apr 27, 2018 at 5:33
  • \$\begingroup\$ I need to change trail length like this Gif 1 Gif 2.please help me \$\endgroup\$ Commented Apr 27, 2018 at 5:47
  • \$\begingroup\$ when I try to Increase trail length I have this problem Gray Problem float result = lerp(mask,0,0.01); return result; \$\endgroup\$ Commented Apr 27, 2018 at 12:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .