How do you get replacementTag to work

I’m trying unsuccessfully so far to use the replacementTag option when using a replacement shader. Unfortunately the help doesn’t give any examples. It says

However a tag is made up of a key and a value, I’ve tried supplying just the key and just the value and both but nothing seems to work.

This is my replacement shader line

camera.SetReplacementShader (renderSceneShader, "VisibleInDepth=On");

and this is my tag definition in the shader

Tags { "RenderType"="Opaque" "VisibleInDepth"="On"}

How is this suppose to work?

ok so i found an example that uses RenderType

So this works

camera.SetReplacementShader (renderSceneShader, "RenderType");

Therefore it looks like you supply the key not the value as it says in the help.

But if I change it to my own tag key “VisibleInDepth” it doesn’t work so it only seems to work with RenderType and not with your own tags. Is this a bug? or just a very annoying limitation?

1 Like

Is there any solution to this? I have the same problem… :wink:

And another question: If I render from a cam with replacement shader, are there the shadows of ALL objects included? I ask, because when I use a culling mask it is not.

Well I ended up using a work around, so I can;t shed any further light on it i’m afraid.

Ah, I think I understand it now…

Here is the shader from the FunkyGlowingThings Example:

Shader "Hidden/RenderFunkyThings" {
SubShader {
	Tags { "RenderType"="Funky" }
	Pass {
		Fog { Mode Off }		
		Color (1,1,1,1)
	}
}
SubShader {
	Tags { "RenderType"="Opaque" }
	Pass {
		Fog { Mode Off }
		Color (0,0,0,0)
	}
} 
}

As you can see there are two subshaders.
If you render the scene now with

camera.SetReplacementShader (renderSceneShader, "RenderType");

Unity will check for all existing shaders in the scene, if they have a RenderType-Tag with either funky or Opaque and replace this very subshader with the one from the replacement shader.

All shaders that DONT have a RenderType-tag set to funky or Opaque will be ignored.

Didn’t test it… but I will do now! :smile:

I see, so you are saying you it just matches the tags in the replacement shader with the tag in the existing shader and swaps the ones that match… that makes a lot of sense. If only the help had explained it that way… :roll:

I discovered strange behavior, like it was only ever doing the first SubShader.

The resolution was to add a light to the scene. My scene doesn’t use lights so I just set the light color to black.

Did you check the LightMode-Tags? Maybe it’s set to render only for Pixel and/or Vertex lights…

For example if there is something like

Tags { "LightMode" = "Pixel" }

try to change it to

 Tags { "LightMode" = "PixelOrNone" }

Have a look at the documentation here:

2 Likes

Oh my God DerWoDaSo you’re a life saver! I know it’s been like 5 years since this was posted. But still I have to say in about 5 hours of searching for how freaking replacement shaders work this was the only thing that explains it.

1 Like

I’m having the same issue with Unity always using the first SubShader regardless of what the tags are in it.
This is rather annoying as I wanna create a generic shader that has multiple SubShaders that responds to different Replacement Shaders. Basically this “bug” renders my idea useless.

Any Unity guys around to verify whether this is actually a bug or a wanted behaviour?


scene use shader with tag “rendertype=opaque”,the tank body material shader with tag"rendertype=tank",close replacement shader in camera.

setreplacementshader in camera,use a shader has two subshader:
subshader0 set tag “rendertype= tank” not use diffuse texture but only purecolor red.
subshader1 set tag"rendertype=opaque" same as standard shader.

get this picture
if remove subshader1, or set “RenderType” = “somethingelse” all scene like picture ,and no change

if change subshader1 not use diffuse texture but only purecolor red ,we got this

if change tank body shader set tag “rendertype1 = tank” set replacement shader subshader0 set tag “rendertype1=tank”,set camera replacementshader “rendertype1” ,only tank is render no else render.

hey guys i need that script which attach to camera and shader please

i am new in unity and need same output you did above, i need whole scene color with a simple shader with my camera.
which i can’t do because of nothing detail about replacement shader use.
i am not a programmer so i just need simple script which i drag to my 2nd camera and render the scene as another color and my 1st camera as original,but i cant’t do this
please guys anyone have the solution please help me please :frowning: