x


[Closed] 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.

more ▼

asked Oct 21, 2011 at 07:40 PM

Julien.Lynge gravatar image

Julien.Lynge
10.2k 87 84 123

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 _MainTex 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.

Oct 22, 2011 at 04:08 PM Owen Reynolds
(comments are locked)
10|3000 characters needed characters left

The question has been closed Oct 31, 2011 at 08:25 PM by Julien.Lynge for the following reason:

Question is badly phrased and confusing.


1 answer: sort voted first

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)...

more ▼

answered Oct 21, 2011 at 08:19 PM

jahroy gravatar image

jahroy
3.7k 36 43 73

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 :)

Oct 21, 2011 at 08:23 PM Julien.Lynge

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

Oct 22, 2011 at 12:21 AM Bunny83

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.

Oct 22, 2011 at 04:25 AM Julien.Lynge

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 ;)

Oct 22, 2011 at 12:31 PM Bunny83

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

Oct 31, 2011 at 08:24 PM Julien.Lynge
(comments are locked)
10|3000 characters needed characters left

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2945
x186
x42
x14

asked: Oct 21, 2011 at 07:40 PM

Seen: 13346 times

Last Updated: Feb 20, 2013 at 02:46 AM