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 00george · May 04, 2020 at 09:55 AM · shadergraphicsproceduralmarching cubes

Graphics.DrawProceduralIndirect isnt rendering anything

Hi, I am running marching cubes on compute shaders and then was constructing meshes on the CPU which works fine.

I have now tried to use

 Graphics.DrawProceduralIndirect

to draw the meshes straight from the GPU however nothing is rendering with this new approach. I was wondering if anyone could figure out what is wrong?

The compute shader:

 // Each #kernel tells which function to compile; you can have many kernels
 #pragma kernel March
 #include "MarchTables.compute"
  
 float isoLevel = 0;
 int width = 16;
 float resolution = 1;
  
  
 struct Triangle
 {
     float3 v[3];
 };
  
 float3 interpolateVertices(float3 v1, float3 v2, float value1, float value2)
 {
     float mu = (isoLevel - value1) / (value2 - value1);
  
     return float3(v1.xyz + mu * (v2.xyz - v1.xyz));
 }
  
 int flattenedIndex(int x, int y, int z)
 {
     return (x * (width + 1) * (width + 1)) + (y * (width + 1)) + z;
 }
  
  
 AppendStructuredBuffer<Triangle> triangleBuffer;
 RWStructuredBuffer<float> densityBuffer;
  
 //int height;
 //int width;
 //float isoLevel;
  
 [numthreads(8, 8, 8)]
 void March(int3 id : SV_DispatchThreadID)
 {
     if (id.x >= (width) || id.y >= (width) || id.z >= (width))
     {
         return;
     }
  
  
     float cubeCorners[8] =
     {
         densityBuffer[flattenedIndex(id.x, id.y, id.z + 1)],
         densityBuffer[flattenedIndex(id.x + 1, id.y, id.z + 1)],
         densityBuffer[flattenedIndex(id.x + 1, id.y, id.z)],
         densityBuffer[flattenedIndex(id.x, id.y, id.z)],
         densityBuffer[flattenedIndex(id.x, id.y + 1, id.z + 1)],
         densityBuffer[flattenedIndex(id.x + 1, id.y + 1, id.z + 1)],
         densityBuffer[flattenedIndex(id.x + 1, id.y + 1, id.z)],
         densityBuffer[flattenedIndex(id.x, id.y + 1, id.z)],
     };
  
     int cubeIndex = 0;
     if (cubeCorners[0] > isoLevel)
         cubeIndex |= 1;
     if (cubeCorners[1] > isoLevel)
         cubeIndex |= 2;
     if (cubeCorners[2] > isoLevel)
         cubeIndex |= 4;
     if (cubeCorners[3] > isoLevel)
         cubeIndex |= 8;
     if (cubeCorners[4] > isoLevel)
         cubeIndex |= 16;
     if (cubeCorners[5] > isoLevel)
         cubeIndex |= 32;
     if (cubeCorners[6] > isoLevel)
         cubeIndex |= 64;
     if (cubeCorners[7] > isoLevel)
         cubeIndex |= 128;
  
     if (cubeIndex == 0 || cubeIndex == 255)
     {
         return;
     }
  
     // Create triangles for current cube configuration
     for (int i = 0; triangulation[cubeIndex][i] != -1; i += 3)
     {
         // Get indices of corner points A and B for each of the three edges
         // of the cube that need to be joined to form the triangle.
         int a0 = cornerIndexAFromEdge[triangulation[cubeIndex][i]];
         int b0 = cornerIndexBFromEdge[triangulation[cubeIndex][i]];
  
         int a1 = cornerIndexAFromEdge[triangulation[cubeIndex][i + 1]];
         int b1 = cornerIndexBFromEdge[triangulation[cubeIndex][i + 1]];
  
         int a2 = cornerIndexAFromEdge[triangulation[cubeIndex][i + 2]];
         int b2 = cornerIndexBFromEdge[triangulation[cubeIndex][i + 2]];
  
         Triangle tri;
         tri.v[0] = interpolateVertices(float3(((id * resolution) + (vertexPositions[a0] * resolution))), float3(((id * resolution) + (vertexPositions[b0] * resolution))), cubeCorners[a0], cubeCorners[b0]);
         tri.v[1] = interpolateVertices(float3(((id * resolution) + (vertexPositions[a1] * resolution))), float3(((id * resolution) + (vertexPositions[b1] * resolution))), cubeCorners[a1], cubeCorners[b1]);
         tri.v[2] = interpolateVertices(float3(((id * resolution) + (vertexPositions[a2] * resolution))), float3(((id * resolution) + (vertexPositions[b2] * resolution))), cubeCorners[a2], cubeCorners[b2]);
         triangleBuffer.Append(tri);
     }
 }


The shader:

 // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  
 Shader "Unlit/TerrainShader"
 {
     Properties
     {
     }
         SubShader
     {
         Tags { "RenderType" = "Opaque" }
         LOD 100
  
         Pass
         {
             CGPROGRAM
             #pragma enable_d3d11_debug_symbols
             #pragma target 5.0
             #pragma vertex vert
             #pragma fragment frag
             // make fog work
             #pragma multi_compile_fog
  
             #include "UnityCG.cginc"
  
  
             struct Triangle {
                 float3 v[3];
  
             };
  
             uniform StructuredBuffer<Triangle> triangles;
             uniform float4x4 model;
  
             struct appdata
             {
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD0;
             };
  
             struct v2f
             {
                 float4 pos: SV_POSITION;
             };
  
             v2f vert (uint id : SV_VertexID)
             {
                 uint pid = id / 3;
                 uint vid = id % 3;
              
                 float3 pos = triangles[pid].v[vid];
  
                 v2f o;
                 o.pos = mul(UNITY_MATRIX_VP,mul(model,pos));
                 return o;
             }
  
             float4 frag(v2f i) : SV_Target
             {
                 return float4(1,0.5,0,1);
             }
             ENDCG
         }
     }
 }


The C# code:

 public void GeneratePlanetOnGPU()
     {
         argBuffer = new ComputeBuffer(4, sizeof(int), ComputeBufferType.IndirectArguments);
         int[] args = new int[] { 0, 1, 0, 0 };
         argBuffer.SetData(args);
  
         int threadGroups = (width / 8);
         int kernelHandle = shader.FindKernel("March");
         if (!useComputeDensity)
         {
             densityMap = new float[(width + 1) * (width + 1) * (width + 1)];
             densityBuffer = new ComputeBuffer((width + 1) * (width + 1) * (width + 1), sizeof(float));
             densityBuffer.SetData(densityMap);
             shader.SetBuffer(kernelHandle, "densityBuffer", densityBuffer);
         }
         else
         {
             ComputeBuffer db = dg.Generate(radius, width, resolution, new float[] { planetCentre.x, planetCentre.y, planetCentre.z }, new float[] { worldPos.x, worldPos.y, worldPos.z }, octaves, minHeight, strength);
             shader.SetBuffer(kernelHandle, "densityBuffer", db);
         }
         triangleCountBuffer = new ComputeBuffer(1, sizeof(int), ComputeBufferType.Raw);
         triangleBuffer = new ComputeBuffer((width) * (width) * (width) * 5, sizeof(float) * 3 * 3, ComputeBufferType.Append);
         triangleBuffer.SetCounterValue(0);
         shader.SetBuffer(kernelHandle, "triangleBuffer", triangleBuffer);
         shader.SetInt("width", width);
         shader.SetFloat("resolution", resolution);
  
         shader.Dispatch(kernelHandle, threadGroups, threadGroups, threadGroups);
  
         ComputeBuffer.CopyCount(triangleBuffer, argBuffer, 0);
         argBuffer.GetData(args);
         int tris = args[0];
         args[0] *= 3;
         argBuffer.SetData(args);
  
         float size = 16 * resolution;
         Vector3 centre = worldPos + (Vector3.one * (size / 2));
  
         b = new Bounds(centre, new Vector3(size, size, size));
         setup = true;
  
     }
  
  
     private void OnRenderObject()
     {
         if (setup)
         {
             mat.SetPass(0);
             mat.SetBuffer("triangles", triangleBuffer);
             mat.SetMatrix("model", transform.localToWorldMatrix);
             Graphics.DrawProceduralIndirect(mat,b,MeshTopology.Triangles,argBuffer);
         }
  
     }


The only difference between this and the CPU method is how the meshes are constructed. The gameobject the script is running on has a mesh filter and mesh renderer but I am unclear if it needs this. I assign the material in the inspector. Also, the gameobject is a prefab that is instantiated at runtime.

There are lots of these prefabs each running this C# script.

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 Egad_McDad · May 05, 2020 at 04:22 AM 0
Share

I have successfully used Graphics.DrawProcedural in the past and, while I'm not 100% sure, I think you also need to call Graphics.ClearRandomWriteTargets(); prior to setting the buffer on the material and calling DrawProceduralIndirect

0 Replies

· Add your reply
  • Sort: 

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

212 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

Related Questions

Procedural Shader Cost/Draw Calls 1 Answer

How to access another sprite's texture from a 2D shader 0 Answers

(Shader Graph) Output resolution/ setting resolution of the main preview window 0 Answers

How to see both sides of transparrent mesh? 1 Answer

Merging, Transitioning, or Blending from one texture to the other based upon Height Maps 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