- Home /
Special shader question. How to draw half an object with one shader and the other half with another?
I think this is the right question? Here's the back story: I was playing a mod for a game called Garry's Mod. One of the game mechanics involved producing resources such as oil. To produce the oil you'd put down an oil derrick and let it produce the oil. Your oil barrel spawns in front of the derrick and starts out in wireframe. As it gets produces it slowly fills with the actual real texture of the model. For example, if you're oil barrel was 50% done producing, the top half would be wireframe and the bottom half would be the actual oil barrel texture.
How exactly would you do something like this in unity. I'm not sure how they did it in the source engine but if they did it with lua in there, I'm guessing there's some way that its possible to do in unity.
Answer by Owen-Reynolds · Apr 07, 2013 at 02:08 AM
You'd draw it twice, using different shaders. Probably just draw a different "wireframe" model for one of them, then a "bottom half" for the real, which covers the wireframe.
For the "real" part, skip the top using a shader parameter, maybe HT. The program would set it and the shader would say something like if(o.vert.y>HT) discard;
. Since the shader can't normally access the model coords, you'd have to copy it like in the "Custom data computed per-vertex" surf shader example. It's not super-easy -- you have to type some shader lines -- but dosn't use advanced math or anything.
You could also just use one shader, and the if(y>HT)
test would put a grey texture on the unbuilt part (but would obviously be the same model.)
I'm unfamiliar with HT? Is it something I can set per mesh or object? I can't be changing a shader parameter directly because then it would change it on all objects that might be using this shader (so the shader would have to take in some sort of per object data).
The other thing, I'm not sure if I can do this with discarding a vertex? Consider I have a barrel with a circle of vertices at the top and the bottom, discarding vertices would always end up in the bottom ring being the vertices that pass the y height test. The effect as I recall was also very smooth as in it scrolled up the height of the barrel. I looked for a video but sadly could not find any.
Sorry, HT would be a uniform shader parameter you make up, like _Color is now. That's what I meant by you have to set it in the vertex program, since it's made-up. Shader parms are on the material instance, so, (again like _Color,) are set for that one gameObject (`material.setFloat("HT",2.3f);`)
Discard is a pixel command. Right, you can't discard a vertex. And a line like if(IN.vert.y>3) IN.vert.y=3;
usually gives terrible-looking results.
You're saying that it's set on the material instance, wont it change the value in the material directly (so any object with that material will have it changed)? I didnt think it created a copy of the material for each object.
Your answer
![](https://koobas.hobune.stream/wayback/20220613095827im_/https://answers.unity.com/themes/thub/images/avi.jpg)