Created
July 26, 2017 01:34
-
-
Save cabbibo/74633a205ff24c045f6e67b305cb2bf0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class cloudPhysicsBuffer : MonoBehaviour { | |
public ARKitComputeCloud cloud; | |
public Material mat; | |
public Material lineMat; | |
public Text debugText; | |
public GameObject target; | |
public int numFish; | |
private float[] values; | |
public ComputeBuffer _buffer; | |
public RodMaker rods; | |
public float[] floatValues; | |
public float[] finalValues; | |
private ComputeBuffer _floatBuffer; | |
private ComputeBuffer _gatherBuffer; | |
public ComputeShader physics; | |
public ComputeShader gather; | |
struct Vert{ | |
public float used; | |
public float justChanged; | |
public Vector3 pos; | |
public Vector3 vel; | |
public Vector3 target; | |
public Vector3[] oPos; | |
public Vector3 debug; | |
}; | |
private int vertStructSize = 1+1+3+3+3+(3*8)+3; | |
private int points; | |
private int numGroups; | |
// Use this for initialization | |
void OnEnable() { | |
points = numFish;///cloud.maxPointsToShow; | |
values = new float[ points * vertStructSize ]; | |
_buffer = new ComputeBuffer( points , vertStructSize * sizeof( float )); | |
int numParticles = points; | |
int numThreads = 128; | |
numGroups = (numParticles+(numThreads-1))/numThreads; | |
floatValues = new float[numGroups * 4]; | |
finalValues = new float[4]; | |
_floatBuffer = new ComputeBuffer(4*numGroups, sizeof(float)); | |
_gatherBuffer = new ComputeBuffer(4, sizeof(float)); | |
Debug.Log( numGroups ); | |
//_floatBuffer.SetData( floatValues); | |
//_gatherBuffer.SetData( finalValues); | |
//_buffer.SetData( values ); | |
} | |
public void OnDisable(){ | |
_buffer.Release(); | |
_gatherBuffer.Release(); | |
_floatBuffer.Release(); | |
} | |
public void DispatchComputeShader(){ | |
physics.SetFloat( "_DeltaTime" , Time.deltaTime ); | |
physics.SetFloat( "_Time" , Time.time ); | |
physics.SetBuffer( 0 , "vertBuffer" , _buffer ); | |
physics.SetBuffer( 0 , "cloudBuffer" , cloud._buffer ); | |
physics.SetBuffer( 0 , "rodBuffer" , rods._buffer ); | |
physics.SetBuffer( 0 , "outBuffer" , _floatBuffer ); | |
physics.SetInt( "_MaxRods" , rods.MaxRods ); | |
physics.SetInt( "_NumVerts" , points ); | |
physics.SetInt( "_NumCloudPoints" , cloud.maxPointsToShow ); | |
physics.SetInt( "_NumGroups", numGroups ); | |
physics.SetVector( "_Target", target.transform.position ); | |
physics.SetVector( "_CameraPos", Camera.main.transform.position ); | |
// Debug.Log( target.transform.position); | |
physics.Dispatch( 0 , numGroups , 1 , 1 ); | |
_floatBuffer.GetData( floatValues ); | |
float closest = 10001; | |
float id = 10000; | |
gather.SetBuffer( 0 , "floatBuffer" , _floatBuffer ); | |
gather.SetBuffer( 0 , "gatherBuffer" , _gatherBuffer ); | |
gather.Dispatch( 0, 1 , 1 , 1 ); | |
_gatherBuffer.GetData(finalValues); | |
//Debug.Log( finalValues[1] ); | |
if( finalValues[0] != 0 ){ Debug.Log( finalValues[0] ); } | |
if( finalValues[1] != 0 ){ Debug.Log( finalValues[1] ); } | |
debugText.text = finalValues[0] + " : " + finalValues[1]; | |
} | |
void OnRenderObject(){ | |
//print("ss"); | |
mat.SetPass(0); | |
mat.SetBuffer( "_vertBuffer", _buffer ); | |
//Graphics.DrawProcedural(MeshTopology.Points, vBuf.vertCount ); | |
Graphics.DrawProcedural(MeshTopology.Triangles, points * 6 ); | |
//print("ss"); | |
lineMat.SetPass(0); | |
lineMat.SetBuffer( "_vertBuffer", _buffer ); | |
//Graphics.DrawProcedural(MeshTopology.Points, vBuf.vertCount ); | |
Graphics.DrawProcedural(MeshTopology.Triangles, points * 6 * 7 ); | |
} | |
// Update is called once per frame | |
void FixedUpdate () { | |
DispatchComputeShader(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment