Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Gentatsu · Nov 01, 2016 at 09:25 PM · shadersstencilbuffer

Add to existing shader at runtime

I'd like to add a small snippet that simply references the stencil buffer. I'm trying to mask a volume, so I used the stencil buffer with a normal shader. Unfortunately, the model I'm working on has things in it that doesn't just use the standard shader, so when I replace them all with the new one, they break.

I tried adding the separate shader as another material, but it just didn't work.

So now, I'd like to add that little snippet into the existing shaders. Any way of doing this, or is there a better way?

Thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tanoshimi · Nov 01, 2016 at 10:26 PM

Shaders must be compiled - you can't alter them at runtime. You can however make multiple shader variants with conditional features activated via #pragma shader_feature: https://docs.unity3d.com/Manual/SL-MultipleProgramVariants.html

Comment
Add comment · Show 3 · 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 Gentatsu · Nov 02, 2016 at 09:06 AM 0
Share

You can alter and re-compile at runtime, no? Do they not get compiled at runtime, anyway? I've done it before in OpenGL (in C), where I add parts to the shader at runtime.

It's less that I want to have different versions for the shaders, it's more that I want to simply add a few lines to every existing shader currently running under a GameObject. So all their children's shaders will have the following snippet:

 Stencil 
 {
     Ref 1
     Comp Equal
 }

I don't want to add this manually to each of the shaders that they're using, simply because there are so many, and I'd have to go through them to find out what they are.

avatar image monotoan Gentatsu · Jan 23, 2017 at 11:07 PM 0
Share

I'm looking for an answer to the exact same question. Any luck?

avatar image tanoshimi monotoan · Jan 24, 2017 at 06:55 AM 0
Share

As I said previously, you can switch shaders assigned to a material at runtime or enable/disable a keyword that turns on some conditional feature, but you can't arbitrarily insert a line into a shader (just as you can't insert a line into the Update() function of a component).

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to clear the stencil buffer? 1 Answer

Using stencil buffer as form of zwrite? 0 Answers

Problem with transparency and stencil shader 1 Answer

Shader that renders pixel with highest alpha value? 0 Answers

Multi-shader Stencil Buffer not working as expected,Multi-shader Stencil not working as expected 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