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 Glurth · Jan 27, 2017 at 07:01 PM · meshdrawcommandbuffer

How to properly draw a mesh using CommandBuffer.DrawMesh?

I’ve seen the examples provided by Unity, but I must be missing something because I’m not getting the results I expect.

In my initial test, I’m simply trying to draw a non-moving, user defined mesh, with a user defined material. (New scene, new project). Unfortunately, while I can get the mesh to show up on camera, it looks like it has no lighting on it, as it’s completely black. (Lighting not applied is confirmed; tested using unlit shader materials, which DO show up properly.) alt text

The main question is: how do I fix this script to allow the material to show up properly, with lighting?

(Note on script: it is necessary to add (but not configure) a renderer component, in order for OnWillRenderObject to be executed. I used a meshRenderer, without a meshfilter. Can this be avoided, if I want to move the mesh?)

 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Rendering;
 [ExecuteInEditMode]
 public class AllCameraCommandRender : MonoBehaviour
 {
     
     List<int> assignedCameraGUIDs;
     public Mesh meshToRender;
     public Material matierialForMesh;
     CommandBuffer cb;
     private void Start()
     {
         if (assignedCameraGUIDs == null) assignedCameraGUIDs = new List<int>();
         else assignedCameraGUIDs.Clear();
     }
     public void OnWillRenderObject()
     {
         if (assignedCameraGUIDs == null) assignedCameraGUIDs = new List<int>();
         Camera cam= Camera.current;
         int id = cam.gameObject.GetInstanceID();
         if (!assignedCameraGUIDs.Contains(id))
         {
             if (cb == null)
             {
                 Matrix4x4 M = transform.localToWorldMatrix;
                 //Matrix4x4 V = cam.worldToCameraMatrix;
                 //Matrix4x4 P = GL.GetGPUProjectionMatrix(cam.projectionMatrix, false);
                 //Matrix4x4 I = Matrix4x4.identity;
                 cb = new CommandBuffer();
                // cb.SetRenderTarget(BuiltinRenderTextureType.CameraTarget);
                 cb.DrawMesh(meshToRender, M, matierialForMesh);
             }                                                                              
             cam.AddCommandBuffer(CameraEvent.BeforeForwardOpaque, cb);
            // cam.AddCommandBuffer(CameraEvent.BeforeLighting, cb);
             assignedCameraGUIDs.Add(id);
         }
     }
 }

I was also experimenting with changing the CameraEvent: I tried to use BeforeLighting, like the decals example does, but this change prevented the object from being visible at all. Why is that? (I saw a post in the forum indicating that CameraEvent might be using the matrix differently, so I experimented with some variants: none, except the transform.localToWorldMatrix, seemed to work- but thats why they are in the code above.)

I also tried using the SetRenderTarget command, but this seemed to have no effect. I assume that if I make no changes to the target, the default render target will be the BuiltinRenderTextureType.CameraTarget. Is that correct?

commandbufferdrawnmesh.png (10.3 kB)
Comment
Add comment · Show 2
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 relativegames · Oct 28, 2018 at 09:04 PM 0
Share

This is due to commandBuffer activating the shader's forwardpass for a deferred camera. I actually need the right shader but I don't know how to active it in a Draw$$anonymous$$eshInstancedIndirect :/

avatar image JotaRata · Feb 11, 2021 at 09:05 PM 0
Share

I would create the CommandBuffer at the Start() or Awake() method and then use cb.Clear() to assign new commands so you have more performance

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by SgtOkiDoki · May 06, 2020 at 12:49 PM

I have found a solution.

It's a Unity issue and they provide a solution around this. (Took me a while to find the solution)

---What is the problem--- Unity doesn't assign the 'SH coefficients' to the target material and that leads to no ambient lighting.

---How to solve it--- Calculate it in CPU once -> Assing it to 'MaterialPropertyBlock', -> Assing that to the material.

Github link for the solution example https://github.com/keijiro/LightProbeUtility

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 hexagonius · Oct 28, 2018 at 09:36 PM

Funny, according to this link you just do:

 cb.DrawMesh(meshToRender, M, matierialForMesh, 0, 0);
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 Glurth · Oct 30, 2018 at 09:27 PM 0
Share

Thanks for finding that hexagonius, I just gave up on this, so missed that post six months after my Q was posted ;) This DOES light the object, but if using a transparent material, it places it behind everything else (edit: fixed by changing event to AfterForwardAlpha). Also, still unsure how to cast/receive shadows.

Why am I supposed to draw only shader pass "0" rather than all shader passes (like the default parameter of -1 is supposed to)?

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

69 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

Related Questions

Using CommandBuffer how to draw Directional Light shadows in native plugin? 0 Answers

Procedural Mesh 1 Answer

Combining Mesh changes Draw Order? 3 Answers

Draw prefab in OnPostRender (hud effect) 0 Answers

Transparent grid shows odd irregularities when displayed. 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