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
2
Question by Jens_Restemeier · Mar 17, 2016 at 03:02 PM · shadersmaterialspass

How do I find the index of a shader pass

I am not sure if I'm missing something obvious, but how do I resolve a shader pass name into an index I can use in Material.SetPass or CommandBuffer.DrawMesh*? The best I found is Material.passCount to get the number of passes. Do I need to manually store the correct pass index for each shader, or is there a Shader.PropertyToID equivalent for passes?

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 Jens_Restemeier · Jul 22, 2018 at 10:36 AM

So, to answer my own question from years ago: There is now a function https://docs.unity3d.com/ScriptReference/Material.GetPassName.html

Though I guess the Unity Renderer bases its pass selection on pass tags. So to pick the deferred pass you'll need to look for the tag "LIGHTMODE" "DEFERRED".

I found a lot of information is available through serialisation on the shader object, that doesn't seem to be covered by official API. Some of it is available through https://docs.unity3d.com/ScriptReference/ShaderUtil.GetShaderData.html

I am not sure at what point these values got added, but for example 2017.3 seems to have parsed shader info:

             SerializedObject so = new SerializedObject (shader);
             SerializedProperty subShaders = so.FindProperty ("m_ParsedForm.m_SubShaders");
             for (int k = 0; k < subShaders.arraySize; k++) {
                 Debug.Log ("SubShader: " + k);
                 SerializedProperty subShader = subShaders.GetArrayElementAtIndex (k);
                 SerializedProperty passes = subShader.FindPropertyRelative ("m_Passes");
                 for (int i = 0; i < passes.arraySize; i++) {
                     Debug.Log ("Pass: " + i);
                     SerializedProperty pass = passes.GetArrayElementAtIndex (i);
 
                     SerializedProperty name = pass.FindPropertyRelative("m_State.m_Name");
                     Debug.Log("Name: " + name.stringValue);
 
                     SerializedProperty tags = pass.FindPropertyRelative ("m_State.m_Tags.tags");
                     for (int j = 0; j < tags.arraySize; j++) {
                         SerializedProperty tag = tags.GetArrayElementAtIndex (j);
                         SerializedProperty first = tag.FindPropertyRelative ("first");
                         SerializedProperty second = tag.FindPropertyRelative ("second");
                         Debug.Log (first.stringValue + ":" + second.stringValue);
                     }
                 }
             }

Earlier versions of Unity, for example I looked at 5.4, only allow access to shader sourcecode, so getting name and pass info requires a custom parser. Sigh.

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
0

Answer by Bunny83 · Mar 17, 2016 at 04:52 PM

The shader pass name is quite irrelevant and i don't think there's any method to convert a pass name to an index. I also don't see the point for that. A shader executes it's passes in the order they appear inside the shader. The passes belong to that shader. So if you manually want to render something with a shader that has multiple passes you simply iterate through them starting at index 0 up to (Material.passCount - 1). If you use passes to combine different shaders into one, you use the pass wrong.

Comment
Add comment · Show 1 · 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 Jens_Restemeier · Mar 17, 2016 at 05:29 PM 1
Share

Have a look at Unity's standard shader. Obviously I don't want to execute all passes, but only the pass I'm building the command buffer for. I guess I'll have to hard-code the pass indices and make sure all shaders have the same pass setup.

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

Does the Web Player support PBR? 0 Answers

Lightweight shader doesn't properly apply to blender imported GameObjects 1 Answer

Using different UV for multiple model copies 2 Answers

Is there an any way to use batching with UV tiling? 1 Answer

Lit, Simple Lit and Complex Lit Shaders are not Avaiable 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