Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 /
avatar image
0
Question by Gergo4222 · Dec 17, 2021 at 09:54 PM · shaderrenderinginstancing

URP Shader Graphs with instanced indirect - InstanceID node seems to give 0

Hello!
I'm trying to render meshes with shader graph shaders from a custom render pass using CommandBuffer.DrawMeshInstancedIndirect. I'm passing my positions to the shader via a StructuredBuffer defined in a custom function node. I index the buffer with the instance id and return the position, that gets added to the object space vertex position. This works great, but it renders all of my objects on the same position (the first one in the buffer).
I checked the draw command with renderdoc and it was indexing the buffer for every instance with 0 instead of the instance id.
I was able to make it work without shader graphs, using SV_InstanceID, but i would really love to get it working with shader graphs.
I also got multiple objects to render with DrawMeshInstanced, using shader graphs, but then i'm not able to use compute buffers to provide the count and I also have to make a long array of matrices. However the instance id worked that time.
I attached some pictures to hopefully make it more understandable. Any ideas what I'm doing wrong? (I selected instance 24 in the renderdoc picture)

alt text

alt text

shadergraph-functionnode.png (94.8 kB)
renderdoc.png (158.9 kB)
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
0
Best Answer

Answer by Gergo4222 · Dec 18, 2021 at 07:08 PM

I managed to solve it using this code: https://gist.github.com/Cyanilux/4046e7bf3725b8f64761bf6cf54a16eb
I was missing this:

  #pragma instancing_options procedural:setup
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 Llockham-Industries · Mar 11 at 11:18 PM

Hey @Gergo4222 , trying to achieve something similar.

How does one use #pragma instancing_options procedural:setup?

I continue to get the following error after following the comments in the link. "UNITY_INSTANCING_PROCEDURAL_FUNC must be defined."

Starting out super simple, & still struggling. Here is what I have thus far -

 #ifndef EXPANSE_INCLUDED
 #define EXPANSE_INCLUDED
 
 StructuredBuffer<float4> _PositionBuffer;
 
 #pragma instancing_options procedural:vertInstancingSetup
 
 void GetPosition_float(float instanceID, out float4 position)
 {
     position = _PositionBuffer[instanceID];
 }
 #endif


& the barebones shader - alt text

Any help appreciated, cheers.


setup.png (83.4 kB)
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 Gergo4222 · Mar 12 at 04:45 PM 0
Share

Hi @Llockham-Industries !

The error is because pragmas must be defined directly in the shader graph, so no external files are allowed for that. You have to type them into a custom function node with the type set to string like this: alt text

Also, you should probably set the built in unity_ObjectToWorld and unity_WorldToObject transformation matrices instead of adding to the vertex positions. This way you can set not only the position, but the rotation and scale as well. Here is how i did that:

 #include "Assets/InstancedTerrain/AdditionalHLSL/MatrixOperations.hlsl"
 
 #ifndef INSTANCEDTERRAIN_INCLUDED
 #define INSTANCEDTERRAIN_INCLUDED
 
 StructuredBuffer<float4x4> _TEMatrices;
 StructuredBuffer<uint> _CullingResultIdxs;
 
 void setMatrices(inout float4x4 objectToWorld, inout float4x4 worldToObject)
 {
     float4x4 matr = _TEMatrices[_CullingResultIdxs[unity_InstanceID]];
 
     objectToWorld = mul(objectToWorld, matr);
     // Inverse transform matrix
     worldToObject = inverse(objectToWorld);
 }
 
 void setupInstancing()
 {
     setMatrices(unity_ObjectToWorld, unity_WorldToObject);
 }
 
 void FNodeDummy_float(in float3 IN, out float3 OUT)
 {
     OUT = IN;
 }
 #endif


pragmacfn.png (52.6 kB)

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

210 People are following this question.

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

Related Questions

How do I enable DOTS Instancing on a material/shadergraph 1 Answer

GPU Instancing in HDRP 0 Answers

OpenGL ES 2.0 support breaks simple shader 0 Answers

how to do a simple multi-pass rendering? 0 Answers

Forward Rendering for Terrain, overriding the shader? 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