Hi! I want to blend a texture and color using a shader.
This is my script:
using UnityEngine;
using System.Collections;
using UnityEditor;
publicclassTest:EditorWindow{
ComputeShader cs;
float[ ] flAr =newfloat[1];
float[ ] arrayToProcess =newfloat[1];
Texture2D texture;
Texture2D textureDr;
RenderTexture tex;
Color[ ] pix;
Color col =Color.white;
[MenuItem(“Window/My Window”)]
static void Init()
{Test editor =(Test)EditorWindow.GetWindow(typeof(Test));
editor.Show();
editor.texture =Resources.Load(“Rust2”)asTexture2D;
editor.tex =newRenderTexture(editor.texture.width, editor.texture.height,0);
editor.tex.enableRandomWrite =true;
editor.tex.format =RenderTextureFormat.ARGB32;
editor.tex.Create();
editor.cs =Resources.Load(“test”)asComputeShader;
}
voidOnGUI(){
col =EditorGUILayout.ColorField(“new color”,col);
cs.SetFloats(“_Color”,newfloat[ ]{ col.r/255, col.g /255, col.b /255,col.a/255});
cs.SetTexture(0,“texCopy”, tex);
cs.SetTexture(0,“tex”, texture);
cs.Dispatch(0, tex.width /8, tex.height /8,1);int s =512;
GUI.DrawTexture(newRect(0,0, s, s), tex);
}
}
and this is shader code:
#pragma kernel CSMain
RWTexture2D texCopy;
Texture2D tex;
float4_Color;
SamplerState_LinearClamp;
SamplerState_LinearRepeat;
SamplerState_PointClamp;
SamplerState_PointRepeat;
[numthreads(8,8,1)]
void CSMain(uint2 id : SV_DispatchThreadID)
{
float w, h;
texCopy.GetDimensions(w, h);
float2 uv =float2(id.x/w, id.y/h);
float4 t = tex.SampleLevel(_LinearClamp, uv,0);
t.r=t.r*_Color.r;
t.g=t.g*_Color.g;
t.b=t.b*_Color.b;
t.a=t.a*_Color.a;
texCopy[id]= t;
}
I send the texture to the shader, copy it to another texture and draw it in the editor window.
if I write so in the shader:
t.r=t.r*_Color.r;
t.g=t.g*_Color.g;
t.b=t.b*_Color.b;
t.a=t.a*_Color.a;
texCopy[id]= t;
textture is empty.
if this:
texCopy[id]= t;
the texture is drawn. But i want to draw the texture with the color selected in the editor. Please tell me how to do it.