Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Johannes 2 · Apr 08, 2011 at 04:54 PM · shaderterrainruntimereplace

Changing Replacement Shaders at Runtime

Hi!

We are using a replacement shader that enables normal maps on terrain by simply putting a replacement shader into the Resources folder. The Shader code just starts with

Shader "Hidden/TerrainEngine/Splatmap/Lightmap-FirstPass" {

and thus overrides the standard terrain shader without further need to do something in code.

Unfortunately, this shader performs bad on integrated graphic chips, since on those vertex programs are calculated on the CPU. So for lower quality levels, we want to replace this shader at runtime with the default terrain shader (that also can be found as source code on the Unity homepage).

So now we have two Terrain shaders in the Resources folder, and I tried to set a specific one by calling:

var terrainShader = Resources.Load("Shaders/TerrainSimpleReplacement", typeof(Shader)) as Shader;
Camera.mainCamera.RenderWithShader(terrainShader, "Opaque");

The "Opaque" tag was chosen because the Subshader code looks like this:

SubShader {
    Tags {
        "SplatCount" = "4"
        "Queue" = "Geometry-100"
        "RenderType" = "Opaque"
    }

But this does not work, and there are no errors logged either. It seems that the first (or last) shader found in the Resources folder on startup is used and the code above has no effect.

Is there any way to change the shader used for terrain at runtime?

Thanks & Best Regards, Johannes

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Jaroslav-Stehlik · Dec 26, 2012 at 07:33 PM

The solution is:

var terrainShader = Resources.Load("Shaders/TerrainSimpleReplacement", typeof(Shader)) as Shader; Camera.mainCamera.RenderWithShader(terrainShader, "RenderType");

in the second attribute when you try to pass tag value you have to pass tag type instead of the actual value. Unity will find the correct SubShaders by the corresponding tag value in the replacementShader it self.

I had the same problem, I think this is not well documented or explained in Unity Scripting Reference...

Comment
Add comment · 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
1

Answer by taoa · Apr 13, 2011 at 12:35 PM

You can do that by using the 'Fallback' feature of Unity shaders.

If the shader doesn't run at all on integrated chips, then the Fallback will tell Unity what replacement shader to use instead. Unfortunately, you might be 'unlucky enough' to have your shader still run on those chips but very poorly, ending up with crap results. In this particular case, a solution would be to use the LOD feature in combination of Fallback.

Check Shader Labs in the docs for more info on this but basically, you can assign a LOD value to all your shaders (including the overloaded terrain shaders), and in one of your scripts, once you've somehow detected that you're running on an integrated chip, turn down the value of

Shader.globalMaximumLOD

to a value you've decided on, and then all the shaders that have a LOD value superior to it will be instead replaced by their Fallback alternative.

This LOD feature is very useful for if you want to give to the users a choice in the level of quality in rendering.

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

How to make trees in a terrain transparent in runtime? 0 Answers

Procedural heightmap shader - Normals and tangents 0 Answers

Force Unity Terrain to be flat-shaded? 1 Answer

How to generate a smooth spherical world? 1 Answer

Using custom vertex shader for terrain? 0 Answers


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