Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Oct 31, 2011 at 08:25 PM by Julien-Lynge for the following reason:

Question is badly phrased and confusing.

avatar image
2
Question by Julien-Lynge · Oct 21, 2011 at 07:40 PM · shaderoffsetdepth-bufferz-fighting

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Oct 22, 2011 at 04:08 PM 0
Share

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.

1 Reply

  • Sort: 
avatar image
3
Best Answer

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

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Julien-Lynge · Oct 21, 2011 at 08:23 PM 1
Share

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

avatar image Bunny83 · Oct 22, 2011 at 12:21 AM 1
Share

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

avatar image Julien-Lynge · Oct 22, 2011 at 04:25 AM 1
Share

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.

avatar image Bunny83 · Oct 22, 2011 at 12:31 PM 0
Share

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

avatar image Julien-Lynge · Oct 31, 2011 at 08:24 PM 0
Share

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

Show more comments

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges