- Home /
Question is badly phrased and confusing.
Dynamically alter shader parameters?
Is there any way to modify shader params on a frame-by-frame basis when those parameters are within a subshader but not within a pass?
I have a world mesh with a 'clouds' texture that's on a separate mesh just over the world. I was getting z-fighting so I added an offset (simple enough), which works great until you start to zoom in and the z-fighting returns - this is because, as you move closer to the camera's near-clip plane, the depth layers become smaller and the offset just isn't doing as much. I could fix this by having a script dynamically set the Offset as a function of the mesh-to-camera distance, but I don't know how.
I'm trying to do something like this:
Shader "Clouds"
{
Properties
{
_OFFSET("Vert Offset",Float) = -3
}
SubShader {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
Offset [_OFFSET], [_OFFSET]
Pass {
etc...
Forgive me if my code is way off - I'm rather new to shaders still, and it's hard to find a lot of documentation / examples / tutorials.
Note from below: To clarify: actually setting the value from a script isn't the issue. The issue is: how do I use a variable to set the Offset? It only seems to accept actual numbers - trying to set "Offset [_OFFSET], [_OFFSET]" throws a compilation error.
In a regular Unity shader (like a surface or frag shader, but not ShaderLab only,) you just write float _OFFSET;
down below (same spelling -- it links them.) The shaders using _$$anonymous$$ainTex have an example: defined as Property up above (so Inspector can change it) and declared as variable down below.
So, in general, it is definitely doable.
Answer by jahroy · Oct 21, 2011 at 08:19 PM
I'm probably even newer to shaders than you, but have you looked into the Material.SetFloat() function?
I don't know much about shaders, but was able to manipulate the alpha value in one of ours using this function (and others like it)...
To clarify: actually setting the value from another script isn't the issue. The issue is: how do I use a variable to set the Offset? It only seems to accept actual numbers - trying to set "Offset variable, variable" throws a compilation error.
Thanks for the thought, though :)
I'm not sure if you really have read the documentation page of SetFloat because it actually explains that quite detailed.
If you have trouble with the shader lab syntax (but it looks right) this might help.
To set your _OFFSET parameter just use:
renderer.material.SetFloat("_OFFSET", myOffset);
where myOffset is a float variable in your script
Again, the problem is not with getting the variable into the shader from a script, or defining the variable within the properties. I've got all that working 100%. The problem is that I don't know how to apply the variable within the Tags{} but not within a Pass{} or cg program. Trying to set
Offset [_OFFSET], [_OFFSET]
throws a syntax error.
If I were to set a variable within the pass or cg program or within a subprogram, that is, as you've pointed out, trivially easy.
Well, i'm not a shader expert but i don't see a reason why Offset [_OFFSET], [_OFFSET]
should not work. Note: you can not put it into the Tags{} block it should be like the Tags block within the subshader block. Does it work when you use it inside a Pass? All examples i've found always use it inside a pass but i guess it should also work within a subshader.
btw. you really should rephrase your question and title. The question title says you want to change a shader parameter per frame but it seems the only problem you really have is the shader syntax. Writing a shader and using a shader are two different things. The whole part about setting a shader property can be left out since shader properties are there to be set from outside and if you don't have trouble with it, don't mention it. Focus on your problem.
Even your first sentence is very confusing. Shader parameters can never be inside a pass or subshader. They always have to be declared at top in the Properies block. The Offset
command is not a parameter.
The actual compilation error would help i guess ;)
Bunny, Thank you much for trying to help me with this shader question. I obviously phrased the question very badly, and wasn't getting across what I was looking for. I'm going to delete this question and try again from scratch.
-Julien
Follow this Question
Related Questions
Mesh z-fighting to itself 1 Answer
How to get Terrain to respect shader offset 0 Answers
Get the world position from Depth buffer to main thread 1 Answer
Material offset of standart shader - animate? 0 Answers
Read depth buffer on the cpu 1 Answer