Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
4 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
1
Question by mpa7ster · Feb 24 at 02:54 AM · graphicsinstancingdrawmesh

How to move from Graphics.DrawMeshInstanced to Graphics.RenderMeshInstanced

I am trying to convert a working Graphics.DrawMeshInstanced call to Graphics.RenderMeshInstanced.


I was hoping to eventually switch it to use a NativeArray that I populate from a burst Job, but I can't get the RenderMeshInstanced call to work at all.


Anybody know the trick to it?


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 mpa7ster · Feb 24 at 02:55 AM 0
Share

It doesn't throw errors, but nothing is rendering or showing in the frame debugger.

1 Reply

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

Answer by andrew-lukasik · Mar 21 at 07:12 PM


These 2 scripts results in exactly the same outcome while one is using Graphics.DrawMeshInstanced and the other Graphics.RenderMeshInstanced


alt text


LetsDrawMeshInstanced.cs

 using UnityEngine;
 
 [ExecuteAlways]
 public class LetsDrawMeshInstanced : MonoBehaviour
 {
     [SerializeField] Mesh _mesh = null;
     [SerializeField] Material _material = null;
     Matrix4x4[] _matrices = new Matrix4x4[100];
     void Update ()
     {
         if( _mesh!=null && _material!=null )
         {
             UpdateMatrices();
             Graphics.DrawMeshInstanced( _mesh , 0 , _material , _matrices );
         }
     }
     void UpdateMatrices ()
     {
         Vector3 origin = transform.position;
         float time = Time.time;
         const float tau = Mathf.PI * 2f;
         int numMatrices = _matrices.Length;
         for( int i=0 ; i<numMatrices ; i++ )
         {
             float theta = (float)i / (float)(numMatrices-1) * tau;
             _matrices[i] = Matrix4x4.TRS(
                 origin + new Vector3( 0 , Mathf.Sin(theta) , Mathf.Cos(theta) )*10f ,
                 Quaternion.Euler( new Vector3(333f,10f,333f)*time ) ,
                 Vector3.one
             );
         }
     }
 }



LetsRenderMeshInstanced.cs

 using UnityEngine;
 using Unity.Collections;
 using Unity.Jobs;
 using Unity.Mathematics;
 using BurstCompile = Unity.Burst.BurstCompileAttribute;
 
 [ExecuteAlways]
 public class LetsRenderMeshInstanced : MonoBehaviour
 {
     [SerializeField] Mesh _mesh = null;
     [SerializeField] Material _material = null;
     NativeArray<float4x4> _matrices;
     public JobHandle Dependency;
     void OnEnable ()
     {
         _matrices = new NativeArray<float4x4>( 100 , Allocator.Persistent );
     }
     void OnDisable ()
     {
         Dependency.Complete();
         if( _matrices.IsCreated ) _matrices.Dispose();
     }
     void Update ()
     {
         if( _mesh!=null && _material!=null )
         {
             // complete previous job
             Dependency.Complete();
 
             // render job results
             RenderParams rp = new RenderParams( _material );
             Graphics.RenderMeshInstanced( rp , _mesh , 0 , _matrices.Reinterpret<Matrix4x4>() );
 
             // schedule new job:
             var job = new UpdateMatrices{
                 Origin            = transform.position ,
                 Time            = Time.time ,
                 NumMatrices        = _matrices.Length ,
                 Matrices        = _matrices ,
             };
             Dependency = job.Schedule( _matrices.Length , 32 , Dependency );
         }
     }
 
     [BurstCompile]
     struct UpdateMatrices : IJobParallelFor
     {
         public float3 Origin;
         public float Time;
         public int NumMatrices;
         [WriteOnly] public NativeArray<float4x4> Matrices;
         void IJobParallelFor.Execute ( int i )
         {
             const float tau = math.PI * 2f;
             float theta = (float)i / (float)(NumMatrices-1) * tau;
             Matrices[i] = float4x4.TRS(
                 Origin + new float3( 0 , math.sin(theta) , math.cos(theta) )*10f ,
                 quaternion.Euler( math.radians(new float3(333f,10f,333f)*Time) ) ,
                 new float3(1,1,1)
             );
         }
     }
         
 }



y5htj9io4hr9hhgrhiheuhp34hggj0w94340223465yohjidf.gif (444.1 kB)
Comment
Add comment · Show 4 · 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 mpa7ster · Mar 22 at 03:36 PM 0
Share

Ah, so I tried Built-In just now like you, and it's working. I tried the same with URP and still no luck. I will dig around some more, but thank you for this and for including the job system example. I included a simplified version of LetsRenderMeshInstanced that works in Built-In but not URP for me.

 using UnityEngine;
 
 [ExecuteAlways]
 public class LetsRenderMeshInstanced : MonoBehaviour
 {
     [SerializeField] Mesh _mesh = null;
     [SerializeField] Material _material = null;
     Matrix4x4[] _matrices = new Matrix4x4[100];
     private RenderParams _renderParams;
     
     void Update()
     {
         if (_mesh != null && _material != null)
         {
             if (_renderParams.material != _material)
             {
                 _renderParams.material = _material;
             }
 
             UpdateMatrices();
             Graphics.RenderMeshInstanced(_renderParams, _mesh, 0, _matrices);
         }
     }
 
     void UpdateMatrices()
     {
         Vector3 origin = transform.position;
         float time = Time.time;
         const float tau = Mathf.PI * 2f;
         int numMatrices = _matrices.Length;
         for (int i = 0; i < numMatrices; i++)
         {
             float theta = (float)i / (float)(numMatrices - 1) * tau;
             _matrices[i] = Matrix4x4.TRS(
                 origin + new Vector3(0, Mathf.Sin(theta), Mathf.Cos(theta)) * 10f,
                 Quaternion.Euler(new Vector3(333f, 10f, 333f) * time),
                 Vector3.one
             );
         }
     }
 }

avatar image andrew-lukasik mpa7ster · Mar 22 at 05:54 PM 1
Share
 if( _renderParams.material!=_material ) _renderParams = new RenderParams(_material);

should resolve the issue

avatar image mpa7ster andrew-lukasik · Mar 22 at 08:01 PM 0
Share

It seems that the constructor accepting the material is setting some other necessary property values, especially...

renderingLayerMask = GraphicsSettings.defaultRenderingLayerMask

avatar image mpa7ster · Mar 22 at 07:40 PM 0
Share

Yep that fixed it and seems to have fixed my original project as well. Thanks!

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

235 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 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

Will this script that does drawmeshinstancedprocedural actually work? 0 Answers

Will Camera.RenderWithShader work with Graphics.DrawMesh (and DrawMeshInstanced)? 0 Answers

How to attach a mesh created with Graphics.DrawMesh to a GameObject? 1 Answer

How to access full Shuriken simulation mesh for use with DrawMeshNow? 0 Answers

Graphics.DrawMeshInstanced Does Not Write to _CameraDepthNormalsTexture 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