Blending 2 textures only once (using render to texture)

Hello, I am new to shader.
This is my problem, I have some base texture for my object and some mask texture, which in every color channel will be used for set some color (for user to choose color for every part of object)

The idea is, use this texture info and blended with the base one, taking the base one (grey scaled) as moderator.

First at all is the above possible by shader? (I soupouse yes, but just in case I ask ;-)).

Then we only want to to this only once, after the user select this color parts, because there is no need to use the shader anymore.

So, the idea is to use render to texture , where target texture is the final texture to set to the object.
Is this approach correct?, can I accomplish this by shaders?

Finally, I noob question, but…, my object has some material that has some build in shader, and I would like always to use this shader.
But when user select color, I should apply this other shader/render to texture to later use this blended texture in he “common shader”.

How do I apply a Shader of this type “only once”, I mean, always from script of course, I create RenderToTexture, and then… how to run the shader?

I simple c# pseudo-code for this last question is really appreciate.

Thanks.

Well I have some advance, although for some reason is working partialy.

I have the Masked tint using textures channels working, the fallowing code is the shader:

Shader "FX/MaskedtintChannels" {

Properties {
	_ColorR ("Tint ColorR", Color) = (1,1,1,1)
	_ColorG ("Tint ColorG", Color) = (1,1,1,1)
	_ColorB ("Tint ColorB", Color) = (1,1,1,1)
	_ColorA ("Tint ColorA", Color) = (1,1,1,1)
   _MainTex ("Base (RGB)", 2D) = "white" {}
	_TexMask ("TextureMask ", 2D) = ""
	
}

SubShader {
	
   Pass {
   
	
	CGPROGRAM
	#pragma fragment frag
	#include "UnityCG.cginc"

	sampler2D _TexMask;
	sampler2D _MainTex;
	struct v2f {
		float4 pos : POSITION;
		float4 uv : TEXCOORD0;
	};
	uniform float4 _ColorR;
	uniform float4 _ColorG;
	uniform float4 _ColorB;
	uniform float4 _ColorA;
	half4 frag (v2f i) : COLOR
	{
	   half4 color = tex2D(_TexMask, i.uv.xy);
	   half4 base = tex2D(_MainTex, i.uv.xy);
	   	   					
	   half4 parc = half4((color.r*_ColorR.r + color.g*_ColorG.r+ color.b*_ColorB.r)
				   ,color.r*_ColorR.g + color.g*_ColorG.g+ color.b*_ColorB.g
				   ,color.r*_ColorR.b + color.g*_ColorG.b+ color.b*_ColorB.b
				   ,color.r*_ColorR.a + color.g*_ColorG.a+ color.b*_ColorB.a)*base;
		return parc;	   
	}
	ENDCG	
      } // pass
   } // subshader
    
} // shader

So, this work perfect for RGB channels but not for alpha, anyone have any ideas what ccould it be?

Beasides I have another problem, there are parts in all mask channels that are no tint, they are just to be used for the base texture (“base” in the shader) just like it comes.
I don’t know how to achive this last one, what I think I need is to every black pixel, add the base pixel for that zone.

Any ideas how to achieve this.
thanks.

Well, some other advance, I have blended “correctly” between the mask color channels and the base texture.

In the above code, I add this second pass

Pass {
Blend OneMinusDstAlpha SrcColor
SetTexture [_MainTex] {}

}
As I am new to shader I have not idea conceptually why I should use another pass to accomplish, but it works.

¿Any ideas why I can’t make in the first pass (after the CG code?)
Is to slow performance have more than one pass?

About the alpha channel, I continue to having the same problem!
Now I am focusing in a render to texture, so the shader is only use “while user select colors” and no all over the game.

Thanks.

Is there a specific reason why are you trying to do this in a shader? To me it seems a lot easier to use GetPixels on your two source textures, do the blending in script and then use SetPixels to write the blend to a resulting texture. I realize that that will be tricky when the source textures have different resolutions, but when they don’t, it’s probably an easier implementation than the shader approach.

Actually I am new to shader, and looking in the forum for something similar like masked tint , I have found only shader approach. (Take a look at anything similar in the forum or unityAnswers.com and you will find shader and more shader)
Besides aren’t shader int this case faster?

I am trying also your approach… let you know how it works.

By the way, if anyone have ad idea why this is not working for the alpha channel is really appreciated.

Thanks.

Well, I understand how it does not work for the alpha channel, because I never added it in each sum :-P.

But, working the alpha channel now is breaking the final blend.
Let you know how this works and the suggested by getpixel in script approach.

Well I have also implemented the script getPixels() way.
And it workds, however, for some strange reason, using this technic, I can not see my object from far ¿?, from near it isn’t any problem.
With shader this problem does not arise.

What could it be?

Anyway the shader code, it is simple and can be useful for anyone looking for do a masking tint with texture channels.
I will up it up to the wiki soon.

It sounds like you aren’t updating mipmaps. Are you using Texture2D.Apply()? By default, that should update them based on the data in mipmap level 0.

Yes you were correct :-).