Multiply shader troubles

Hey guys :slight_smile:

Ok so I really need you peoples help. I’ve been using my googling capabilities to the fullest but I haven’t found a good solution to my problem yet (or at least one I can understand). For the game I’m working on we need a shader which can do something similiar to the Multiply function in Photoshop, with the option to change transparency on the ‘detail’ texture. We want to have a creature which can have it’s normal texture, and when it gets sicker we want to raise the transparency on the detail texture to gradually make it appear sicker and sicker. We are using 2d Toolkit and will use this shader on animated sprites if that makes any difference. So what we need in our shader is a _MainTex for the regular texture, a _DetailTex for the texture we want to “Multiply” with the regular and some way to adjust transparency on the _DetailTex (however, this is not our main concern here, just getting Multiply to work would be great). Here’s an example picture of what we want:

http://imageshack.us/photo/my-images/834/multiplyexample.jpg/

Here’s what we got so far. I thought the lerp function would do the ‘multiplying’ for us by it’s use of _DetailMap _DiffuseMap, however with this code it seems we are only taking the mask from the _MainTex and then putting _DetailTex in that mask.

We are really in a jam about this and any help at all is greatly appreciated :slight_smile:

Shader "Custom/DiffuseDetailCuttoff" 
{
Properties {
	_Color ("Main Color", Color) = (1,1,1,1)
	_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
	_DetailTex ("Detail Texture", 2D) = "white" {}
	_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
	_DetailTiling ("Detail Tiling", Float) = 1.0
	_DetailFade ("Defail Fade", Range(0, 1)) = 1.0
}

SubShader {
	Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
	LOD 200
	
CGPROGRAM
#pragma surface surf Lambert alphatest:_Cutoff

sampler2D _MainTex;
sampler2D _DetailTex;
float _DetailTiling;
float _DetailFade;
fixed4 _Color;

struct Input {
	float2 uv_MainTex;
};

void surf (Input IN, inout SurfaceOutput o) {
	fixed4 DiffuseMap = tex2D(_MainTex, IN.uv_MainTex) * _Color;
	fixed4 DetailMap = tex2D(_DetailTex, IN.uv_MainTex * _DetailTiling);
	o.Albedo = lerp(DiffuseMap.rgb, DetailMap.rgb, _DetailFade);
	o.Alpha = DiffuseMap.a;
}
ENDCG
}

Fallback "Transparent/Cutout/VertexLit"
}

1235121--52333--$Multiply-Example.jpg

Hi

I would do something like :

o.Albedo = DiffuseMap.rgb * (1 - _DetailFade) + DiffuseMap.rgb * DetailMap.rgb * _DetailFade;

So if your _DetailFade is at 0 it display the diffusemap, and if it’s greater than 0 it will lerp between pure diffusemap + the product of diffusemap and detailmap. Finally, it _DetailFade is to 1, it display only the product

Might be easier with

o.Albedo = DiffuseMap.rgb * lerp(DetailMap.rgb, fixed3(1,1,1), _DetailFade);

Giving…
_DetailFade at 0 = full detail map.
_DetailFade at 1 = no detail map.

Basically blends the detail map between itself and white, based on detail fade value. As multiplying a colour with white means that you don’t change it (as you’re simply multiplying by 1).

If you want the opposite effect (no detail at 0, full detail at 1), swap the position of DetailMap.rgb and fixed3(1,1,1) in the code I posted.

You guys are the most awesome people in the world :smile: Thanks! We have our beta in less than a week and I’ve been stuck with shader problems this entire week.

I do have some more problems with it though, since we want to use the shader with sprites (taken from the 2D Toolkit) there’s no meshes on them and the shader can’t function with them. I tried taking the sprites emtpy mesh and running RecalculateNormals() but that gave trouble to the animations. Is there any way to convert the shader to only handle the pixels without taking into account the geometrical shapes? Maybe changing the _DiffuseMap _DetailMap so that it doesn’t take in UV maps (would that be enough)?

I’m not sure I understand what you mean…

Sprites are geometric shapes - they’re just flat quads, generally.

I’m sorry, I’m pretty new at shaders and such, but if I understand correctly, most of Unity’s shaders are meant to work with 3D objects right? Whereas what I want is just multiplication between two textures and have no need for any geometrical calculations which may have to do with lightning and such. I just thought maybe you could trim that away from the shader somehow and maybe have it working better with ‘easier’ objects like sprites. But thats just me thinking out loud. Right now when I use the shader with my sprites they’re only black which I thought could have something to do with the texture not being stretched right on the ‘geometry’.