- Home /
Using compute shader, how to take every vertex of a mesh and make a cube spawn and move to each one?
Here is a script I currently have for object Emptsh to use a compute shader for making a point appear on a screen where every vertex on the BoyMesh mesh is.
Now I'm trying to, instead of make simple points appear on screen for each BoyMesh vertex position, spawn cubes from a different position and then have them fly over to each vertex position (one cube per vertex position). Any ideas?
using UnityEngine;
using System.Collections;
public class Emptsh : MonoBehaviour
{
public BoyMesh BoyMesh;
public Mesh meshdata;
public ComputeShader cshader;
public Material mat;
private int kernel;
private int num4pos;
private ComputeBuffer posbuffer;
private int num4vertex;
private ComputeBuffer vertexbuffer;
private void meshInfo()
{
Vector3[] vertics = meshdata.vertices;
int[] triangles = meshdata.triangles;
num4vertex = triangles.Length;
Vector3[] newVertics = new Vector3[num4vertex];
for (int i = 0; i < num4vertex; ++i)
{
newVertics[i] = vertics[triangles[i]];
}
vertexbuffer = new ComputeBuffer(num4vertex, 12);
vertexbuffer.SetData(newVertics);
}
void Start()
{
meshdata = BoyMesh.Mesh;
kernel = cshader.FindKernel("CSMain");
num4pos = 1; //this determines how many appear
//num4vertex = ;
meshInfo();
//float3
posbuffer = new ComputeBuffer(num4pos, 12);
}
private void BufferSet()
{
cshader.SetBuffer(kernel, "posbuffer", posbuffer);
mat.SetBuffer("posbuffer", posbuffer);
mat.SetBuffer("vertexbuffer", vertexbuffer);
}
private void OnRenderObject()
{
BufferSet();
//1
cshader.Dispatch(kernel, 1, 1, 1);
//2
mat.SetPass(0);
//3
Graphics.DrawProceduralNow(MeshTopology.Points, num4vertex, num4pos);
}
private void OnDestroy()
{
posbuffer.Release();
vertexbuffer.Release();
}
}
And then the shader code:
Shader "Unlit/cshader4"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
CGINCLUDE
#define time _Time.y
float3 Rotx(in float3 p,in float a){
float c,s; float3 q = p;
c = cos(a); s = sin(a);
q.y = c * p.y - s * q.z;
q.z = s * p.y + c * q.z;
return q;
}
float random(float id){
return frac(sin(id)*678.342231);
}
ENDCG
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass{
CGPROGRAM
#pragma target 5.0
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
StructuredBuffer<float3> vertexbuffer;
StructuredBuffer<float3> posbuffer;
struct vertIN{
uint vID : SV_VertexID;
uint ins : SV_InstanceID;
};
struct vertOUT{
float4 pos : SV_POSITION;
};
vertOUT vert(vertIN i){
vertOUT o = (vertOUT)0;
float4 position = float4(vertexbuffer[i.vID],1);
position.xyz = Rotx(position.xyz,time*(1+random(i.ins)));
position.xyz += posbuffer[i.ins];
o.pos = UnityObjectToClipPos(position);
return o;
}
fixed4 frag(vertOUT ou):SV_Target{
return 1;
}
ENDCG
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How does appdata_base work? 0 Answers
How to set float or float4 UV in Mesh. 1 Answer
Vertex position problem in vertex shader 0 Answers
Create a Wireframe Shader with Shader Forge 1 Answer
Array of Verticies 1 Answer