Skip to content

Instantly share code, notes, and snippets.

@cabbibo
Created January 29, 2018 16:11
Show Gist options
  • Save cabbibo/ed8363ee6593b18750f7633650ed3a35 to your computer and use it in GitHub Desktop.
Save cabbibo/ed8363ee6593b18750f7633650ed3a35 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class pointBuffer : MonoBehaviour {
public sdfBoneBuffer Bones;
public ComputeShader physics;
public bool update;
public float hue;
public float pointSize;
public float speed;
public float noise;
public float lifetime;
public bool renderPoints;
public Material material;
public ComputeBuffer _buffer;
private float[] values;
private int _kernel;
private int numGroups;
private int numThreads;
public int numVerts;
public Transform CenterPoint;
struct Point{
public Vector3 pos;
public Vector3 targetPos;
public Vector3 vel;
public float id;
public Vector3 debug;
};
private int PointStructSize = 3+3+3+1+3;
public bool ready = false;
public void OnEnable(){
_buffer = new ComputeBuffer( numVerts , PointStructSize * sizeof(float) );
values = new float[ PointStructSize * numVerts ];
material = new Material( material);
int index = 0;
for( int i = 0; i < numVerts; i++ ){
values[ index++ ] = Random.Range( -1.01f , 1.01f);
values[ index++ ] = Random.Range( 0.01f , 2.01f);
values[ index++ ] = Random.Range( -1.01f , 1.01f);
values[ index++ ] = 0;
values[ index++ ] = 0;
values[ index++ ] = 0;
values[ index++ ] = 0;
values[ index++ ] = 0;
values[ index++ ] = 0;
values[ index++ ] = i;
values[ index++ ] = 1;
values[ index++ ] = 0;
values[ index++ ] = Random.Range(-.99f , -.01f);
}
_buffer.SetData( values );
WhenBufferReady();
}
void OnRenderObject(){
if( renderPoints == true ){
material.SetPass(0);
material.SetFloat("_Hue", hue);
material.SetFloat("_Size", pointSize);
material.SetBuffer("_vertBuffer", _buffer);
Graphics.DrawProcedural(MeshTopology.Triangles, numVerts*6);
}
}
public void Die(){
ready = false;
}
// Use this for initialization
void WhenBufferReady() {
_kernel = physics.FindKernel("CSMain");
numThreads = 128;
numGroups = (numVerts+(numThreads-1))/numThreads;
ready = true;
}
public void DispatchComputeShader(){
physics.SetInt( "_Reset" , 0 );
float dT = Time.deltaTime;
if( dT > .1f){ dT = 0; }
physics.SetFloat( "_DeltaTime" , dT );
physics.SetFloat( "_Time" , Time.time );
physics.SetFloat( "_Speed" , speed );
physics.SetFloat( "_Noise" , noise );
physics.SetFloat( "_Lifetime" , lifetime );
physics.SetBuffer( _kernel , "_vertBuffer" , _buffer );
physics.SetBuffer( _kernel , "_boneBuffer" , Bones._buffer );
physics.SetVector( "_CenterPoint" , CenterPoint.position );
physics.SetInt( "_NumBones" , Bones.numBones);
physics.SetInt( "_NumVerts" , numVerts );
physics.SetInt( "_NumGroups", numGroups );
physics.Dispatch( _kernel, numGroups,1,1 );
}
// Update is called once per frame
void FixedUpdate () {
if( update == true && ready == true ){ DispatchComputeShader(); }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment