Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Graphics.DrawMeshInstancedIndirect with different colors ?

Discussion in 'General Graphics' started by Deytooh, Dec 8, 2018.

  1. Deytooh

    Deytooh

    Joined:
    Jan 14, 2015
    Posts:
    6
    Hello ! I am working on a GPU Instanced Particle System and I am wondering how I could apply different colors to the things that I am creating !

    Since the DrawMeshInstancedIndirect can get only one MaterialPropertyBlock, I tried another solution by doing a fixed4 StructuredBuffer and a CompteBuffer of colors and applying a color by instance ID, it didnt work (or maybe i did it wrong)

    I need your help !
     
  2. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,240
    A color per-instance in a compute buffer sounds good to me!

    The manual page for that api gives you an example of how to pass per instance data.

    https://docs.unity3d.com/ScriptReference/Graphics.DrawMeshInstancedIndirect.html

    I wouldn’t use fixed4 tho, what that is varies per platform. Use float4 or uint. Float4 requires no unpacking but may be a waste of memory/bandwidth, whereas a uint would be 4 bytes (each one 0-255 for rgba) and you would need to unpack into floats in the shader (eg (float)((myUint << 8) & 255) / 255 for the green channel, etc) it’s the equivalent of ColorRGBAf vs ColorRGBA32 in script.
     
    Last edited: Dec 8, 2018
  3. Deytooh

    Deytooh

    Joined:
    Jan 14, 2015
    Posts:
    6
    Ohhh really nice !! I tried to do it like in the documentation but it wasnt working, so I tried as you said with a float (to begin, and then if it works go with uint) and I did try again and it worked !! Thank you :) !!
     
    richardkettlewell likes this.
  4. FE-Games

    FE-Games

    Joined:
    Jul 1, 2020
    Posts:
    15
    What do you mean with uint?
    How can i use uInt for Color control in my shader script or must this also be inside a normal script?

    Iam using DrawMeshInstancedIndirect for drawing large amounts of cubes first, now my render threat is only by 0,2ms but the cpu is using 14,2ms or more, how can i reduce the cpu usage?

    Code (CSharp):
    1. Shader "Instanced/InstancedVertexShader" {
    2.     SubShader {
    3.         Tags { "RenderType"="Opaque" }
    4.         LOD 100
    5.  
    6.         Pass {
    7.             CGPROGRAM
    8.             #pragma vertex vert
    9.             #pragma fragment frag
    10.             #pragma multi_compile_instancing
    11.             #include "UnityCG.cginc"
    12.  
    13.             StructuredBuffer<float4> positionBuffer;
    14.             StructuredBuffer<float4> colorBuffer;
    15.            
    16.             struct v2f
    17.             {
    18.                 float4 vertex : SV_POSITION;
    19.                 float4 color : COLOR0;
    20.             };
    21.  
    22.             v2f vert(appdata_full v, uint instanceID : SV_InstanceID) {
    23.                
    24.                 float4 data = positionBuffer[instanceID];  
    25.                 float4 color = colorBuffer[instanceID];
    26.                 float3 localPosition = v.vertex.xyz * data.w;
    27.                 float3 worldPosition = data.xyz + localPosition;
    28.                 float3 worldNormal = v.normal;
    29.                                
    30.                 v2f o;
    31.                 o.vertex = mul(UNITY_MATRIX_VP, float4(worldPosition, 1.0f));
    32.                 o.color = color;
    33.                 return o;
    34.             }
    35.            
    36.             fixed4 frag (v2f i) : SV_Target
    37.             {
    38.                 return i.color;
    39.             }
    40.             ENDCG
    41.         }
    42.     }
    43. }
    thats my simple vertex shader, iam newly in scripting shaders if you see some optimize let me know.
    iam from germany so sorry for bad english.

    iam also find this:
    https://forum.unity.com/threads/loo...color-data-into-a-float-in-shader-lab.189728/

    its sounds interesting but i dont know much about the Decode process or something.

    i need to maximum optimize my game, because it should be for all plattforms and many players in a open world.