Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
edited body
Source Link
War
  • 1.7k
  • 1
  • 18
  • 33
using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour
{
   public ComputeShader Generator;
   public MeshTopology Topology;

   void OnEnable()
   {
      var computedMeshPoints = ComputeMesh();
      CreateMeshFrom(computedMeshPoints);
   }

   private Vector3[] ComputeMesh()
   {
      var size = (32*32) * 4; // 4 points added for each x,z pos
      var buffer = new ComputeBuffer(size, 12, ComputeBufferType.Append);
      Generator.SetBuffer(0, "vertexBuffer", buffer);
      Generator.Dispatch(0, 1, 01, 01);
      var results = new Vector3[size];
      buffer.GetData(results);
      buffer.Dispose();
      return results;
   }

   private void CreateMeshFrom(Vector3[] generatedPoints)
   {
      var filter = GetComponent<MeshFilter>();
      var renderer = GetComponent<MeshRenderer>();

      if (generatedPoints.Length > 0)
      {
         var mesh = new Mesh { vertices = generatedPoints };
         var colors = new Color[generatedPoints.Length];
         var indices = new int[generatedPoints.Length];

         //TODO: build this different based on topology of the mesh being generated
         for (int i = 0; i < indices.Length; i++)
         {
            indices[i] = i;
            colors[i] = Color.blue;
         }

         mesh.SetIndices(indices, Topology, 0);
         mesh.colors = colors;

         mesh.RecalculateNormals();
         mesh.Optimize();
         mesh.RecalculateBounds();
         filter.sharedMesh = mesh;
      }
      else
      {
         filter.sharedMesh = null;
      }
   }
}
using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour
{
   public ComputeShader Generator;
   public MeshTopology Topology;

   void OnEnable()
   {
      var computedMeshPoints = ComputeMesh();
      CreateMeshFrom(computedMeshPoints);
   }

   private Vector3[] ComputeMesh()
   {
      var size = (32*32) * 4; // 4 points added for each x,z pos
      var buffer = new ComputeBuffer(size, 12, ComputeBufferType.Append);
      Generator.SetBuffer(0, "vertexBuffer", buffer);
      Generator.Dispatch(0, 1, 0, 0);
      var results = new Vector3[size];
      buffer.GetData(results);
      buffer.Dispose();
      return results;
   }

   private void CreateMeshFrom(Vector3[] generatedPoints)
   {
      var filter = GetComponent<MeshFilter>();
      var renderer = GetComponent<MeshRenderer>();

      if (generatedPoints.Length > 0)
      {
         var mesh = new Mesh { vertices = generatedPoints };
         var colors = new Color[generatedPoints.Length];
         var indices = new int[generatedPoints.Length];

         //TODO: build this different based on topology of the mesh being generated
         for (int i = 0; i < indices.Length; i++)
         {
            indices[i] = i;
            colors[i] = Color.blue;
         }

         mesh.SetIndices(indices, Topology, 0);
         mesh.colors = colors;

         mesh.RecalculateNormals();
         mesh.Optimize();
         mesh.RecalculateBounds();
         filter.sharedMesh = mesh;
      }
      else
      {
         filter.sharedMesh = null;
      }
   }
}
using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour
{
   public ComputeShader Generator;
   public MeshTopology Topology;

   void OnEnable()
   {
      var computedMeshPoints = ComputeMesh();
      CreateMeshFrom(computedMeshPoints);
   }

   private Vector3[] ComputeMesh()
   {
      var size = (32*32) * 4; // 4 points added for each x,z pos
      var buffer = new ComputeBuffer(size, 12, ComputeBufferType.Append);
      Generator.SetBuffer(0, "vertexBuffer", buffer);
      Generator.Dispatch(0, 1, 1, 1);
      var results = new Vector3[size];
      buffer.GetData(results);
      buffer.Dispose();
      return results;
   }

   private void CreateMeshFrom(Vector3[] generatedPoints)
   {
      var filter = GetComponent<MeshFilter>();
      var renderer = GetComponent<MeshRenderer>();

      if (generatedPoints.Length > 0)
      {
         var mesh = new Mesh { vertices = generatedPoints };
         var colors = new Color[generatedPoints.Length];
         var indices = new int[generatedPoints.Length];

         //TODO: build this different based on topology of the mesh being generated
         for (int i = 0; i < indices.Length; i++)
         {
            indices[i] = i;
            colors[i] = Color.blue;
         }

         mesh.SetIndices(indices, Topology, 0);
         mesh.colors = colors;

         mesh.RecalculateNormals();
         mesh.Optimize();
         mesh.RecalculateBounds();
         filter.sharedMesh = mesh;
      }
      else
      {
         filter.sharedMesh = null;
      }
   }
}

Like many others I figured I would try and make the most of the monster processing power of the GPU but i'mI'm having trouble getting the basics in place ...

CPU code ...:

GPU code ...:

Can anyone explain why when I call "buffer.GetData(results);" on the cpuCPU after the compute dispatch call my buffer is full of Vector3(0,0,0), imI'm not expecting any y values yet but I would expect a bunch of thread indexes in the x,z values for the Vector3 array.

I'm not getting any errors in any of this code which suggests it's syntatically correct syntax-wise but maybe the issue is a logical bug.

Also: yes iYes, I know imI'm generating 4,000 Vector3's and then basically round tripping them but. However, the purpose of this code is purely to learn how round tripping works between CPU and GPU in unityUnity.

Like many others I figured I would try and make the most of the monster processing power of the GPU but i'm having trouble getting the basics in place ...

CPU code ...

GPU code ...

Can anyone explain why when I call "buffer.GetData(results);" on the cpu after the compute dispatch call my buffer is full of Vector3(0,0,0), im not expecting any y values yet but I would expect a bunch of thread indexes in the x,z values for the Vector3 array.

I'm not getting any errors in any of this code which suggests it's syntatically correct but maybe the issue is a logical bug.

Also: yes i know im generating 4,000 Vector3's and then basically round tripping them but the purpose of this code is purely to learn how round tripping works between CPU and GPU in unity.

Like many others I figured I would try and make the most of the monster processing power of the GPU but I'm having trouble getting the basics in place.

CPU code:

GPU code:

Can anyone explain why when I call "buffer.GetData(results);" on the CPU after the compute dispatch call my buffer is full of Vector3(0,0,0), I'm not expecting any y values yet but I would expect a bunch of thread indexes in the x,z values for the Vector3 array.

I'm not getting any errors in any of this code which suggests it's correct syntax-wise but maybe the issue is a logical bug.

Also: Yes, I know I'm generating 4,000 Vector3's and then basically round tripping them. However, the purpose of this code is purely to learn how round tripping works between CPU and GPU in Unity.

edited tags
Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345
Source Link
War
  • 1.7k
  • 1
  • 18
  • 33
Loading