Search Unity

Question Particles don't render via CommandBuffer

Discussion in 'High Definition Render Pipeline' started by Lynxed, Apr 5, 2020.

  1. Lynxed

    Lynxed

    Joined:
    Dec 9, 2012
    Posts:
    121
    I'm trying to render particles with a CommandBuffer.DrawRenderer() (HRDP) and particles don't update even with "Always Simulate". I'm emitting them via ParticleSystem.Emit(). But they are not actually updated and drawn. They appear only when i switch to Scene Camera. Then Frame Debugger starts to show my commandBuffer, and last Scene-view state is drawn by it, but particles they stop updating the moment i switch to Game. The version is 2019.3.7f1
    Those are Billboard with View alignment and Always Simulate:



    Am i doing something wrong?
     
  2. Lynxed

    Lynxed

    Joined:
    Dec 9, 2012
    Posts:
    121
    I "fixed" it by adding useless copy of my main camera and assigning it the same layer as the particles. Then manually rendering it with cam.Render() into a useless renderTexture just to trigger particle updates.
    Then i do my commandBuffer.drawRenderer(particles) and it works fine (if you can call it that)... in 2019.3.7f1.

    But if i "upgrade" to 2019.3.8f1, it does not execute the command buffer again even with this workaround. This is very frustrating.

    Can anyone help me understand, why particle render via CommandBuffer.DrawRenderer does not work as intended?
     
  3. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Lynxed likes this.
  4. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,266
    I've had the same issue in URP. DrawRenders is fairly broken since it was introduced. For example, trying to draw a disabled trail renderer flat out crashes Unity.

    Like you said, particles only render correctly when the scene view camera renders. I got around this by baking the particle system to a mesh, and drawing that instead. This is arguably a little slower, but the only thing I found works.

    Code (CSharp):
    1. if (!bakedMesh) bakedMesh = new Mesh();
    2. m_ParticleRenderer.BakeMesh(bakedMesh, renderingData.cameraData.camera);
    3.  
    4. bool localSpace = b.particleSystem.main.simulationSpace == ParticleSystemSimulationSpace.Local;
    5. cmd.DrawMesh(bakedMesh, localSpace ? m_ParticleRenderer.localToWorldMatrix : Matrix4x4.identity, material, submeshIndex, passIndex, props);
     
    Lynxed likes this.
  5. Lynxed

    Lynxed

    Joined:
    Dec 9, 2012
    Posts:
    121
    I hope to find time to make a project for a bug report.
    @richardkettlewell, please help?

    Thanks, i'll try the BakeMesh approach. I want to use trails with my CommandBuffer system too. What if trails won't be disabled, but will be in a layer, that is not rendered by main cameras (only a dummy one)?
     
  6. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,266
    Not sure if that'll work as well. In my case, trails only needed to be rendered to a specific camera using DrawRenderers. Leaving them enabled and setting the forceRenderingOff property on the TrailRenderer made them invisible to everything else, which worked for me.

    But I still had to bake trails to meshes since the scene view camera influences how they're rendered for some reason.This was in URP, but I assume this is the case for HDRP as well.
    Code (CSharp):
    1. m_TrailRenderer.BakeMesh(bakedMesh, renderingData.cameraData.camera, false);
    2.  
    3. cmd.DrawMesh(bakedMesh, Matrix4x4.identity, TrailMaterial, 0, 0, props);
     
    Lynxed likes this.
  7. ruojixu

    ruojixu

    Joined:
    Jan 19, 2021
    Posts:
    1
    Actually, this is not a bug. when rendering particles, the camera is required, but in the srp when using commandbuffer DrawRenderer rendering particles, the camera may be null. So the particle renderer is not working.
    The solution is simple, add Camera.SetupCurrent(camera) just before commandbuffer rendering particles.
     
    EvViolet and customphase like this.
  8. adam_unity450

    adam_unity450

    Joined:
    Aug 3, 2020
    Posts:
    13
    While the undocumented function Camera.SetupCurrent makes the particle system render, it seems to break the projection for VR rendering and the image is distorted.
     
    customphase likes this.
  9. bfoddy

    bfoddy

    Joined:
    Mar 27, 2012
    Posts:
    85
    Unity sent word that this bug (yes it was a bug) is now fixed in 2022.2.0a3