Advertisement
Guest User

Untitled

a guest
Dec 25th, 2012
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.71 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Threading;
  4.  
  5. public class PartController : MonoBehaviour {
  6.    
  7.    
  8.     public Texture2D particleTexture;
  9.    
  10.     public Shader shader;
  11.    
  12.     private Material mat;
  13.    
  14.     private ComputeBuffer cbDrawArgs;
  15.     private ComputeBuffer cbPoints;
  16.    
  17.     public ComputeShader cs;
  18.    
  19.     public int particleCount = 1000;
  20.     public Transform rootLocation;
  21.    
  22.     private float[] x_p;
  23.     private float[] y_p;
  24.     private float[] z_p;
  25.    
  26.     private Vector3[] particleLocations;
  27.     private Vector3[] particleColors;
  28.     private Vector3[] particleTargets;
  29.    
  30.     private ComputeBuffer particleBuffer;
  31.     private ComputeBuffer particleColorBuffer;
  32.     private ComputeBuffer targetBuffer;
  33.    
  34.     private bool useCS = true;
  35.    
  36.     private bool rendering = false;
  37.    
  38.     Thread csThread;
  39.    
  40.     // Setup the shaders and such
  41.     void Start()
  42.     {
  43.         // Setup the base args buffer
  44.         particleLocations = new Vector3[particleCount];
  45.         particleColors = new Vector3[particleCount];
  46.         particleTargets = new Vector3[particleCount];
  47.        
  48.         for (int i = 0; i < particleCount; i++)
  49.         {
  50.             // Generate a random location
  51.             Vector3 newLoc = rootLocation.position + Random.insideUnitSphere;
  52.             particleLocations[i] = newLoc;
  53.             particleTargets[i] = newLoc;
  54.             particleColors[i] = new Vector3(Random.value,Random.value,Random.value);
  55.         }
  56.        
  57.         targetBuffer = new ComputeBuffer(particleCount, 12);
  58.         targetBuffer.SetData (particleTargets);
  59.        
  60.         particleColorBuffer = new ComputeBuffer(particleCount, 12);
  61.         particleColorBuffer.SetData (particleColors);
  62.        
  63.         particleBuffer = new ComputeBuffer(particleCount, 12);
  64.         particleBuffer.SetData(particleLocations);
  65.        
  66.         // Setup the material based on the shader
  67.         mat = new Material(shader);
  68.         mat.hideFlags = HideFlags.HideAndDontSave;
  69.        
  70.         mat.SetTexture ("_Sprite", particleTexture);
  71.         Debug.Log ("Finshed setting up");
  72.     }
  73.    
  74.     private void ReleaseResources()
  75.     {
  76.         csThread.Abort ();
  77.  
  78.         cbDrawArgs.Release ();
  79.         cbPoints.Release ();
  80.         Object.DestroyImmediate (mat);
  81.     }
  82.    
  83.     void OnDisable()
  84.     {
  85.         ReleaseResources ();
  86.     }
  87.    
  88.    
  89.     void Update()
  90.     {
  91.         float T = Time.timeSinceLevelLoad;
  92.        
  93.         if (useCS){
  94.             if (useCS){
  95.                 // Set time to the compute shader
  96.                 cs.SetFloat ("Time",T);
  97.                 cs.SetVector ("CoreLoc", rootLocation.position);
  98.            
  99.                 // Have the compute shader process all the particle locations
  100.                 cs.SetBuffer (cs.FindKernel ("CSMain"), "colBuffer", particleColorBuffer);
  101.                 cs.SetBuffer (cs.FindKernel ("CSMain"), "posBuffer", particleBuffer);  
  102.                 cs.SetBuffer (cs.FindKernel ("CSMain"), "tarBuffer", targetBuffer);
  103.                 // Dispatch the compute shader
  104.                 cs.Dispatch (cs.FindKernel ("CSMain"), particleCount, 1, 1);
  105.                 // Reconstruct the locations and colors
  106.             }
  107.         }
  108.         else{
  109.             // Do some modifications on the cpu
  110.  
  111.             for (int i = 0; i < particleCount; i++)
  112.             {
  113.                 particleColors[i].x = Mathf.Cos (T);
  114.                 particleColors[i].y = Mathf.Sin (T);
  115.                 particleLocations[i] += Random.insideUnitSphere / 3;
  116.             }
  117.            
  118.            
  119.             // Set the buffers
  120.             particleColorBuffer.SetData (particleColors);
  121.             particleBuffer.SetData (particleLocations);
  122.         }
  123.     }
  124.    
  125.     void OnRenderImage(RenderTexture src, RenderTexture dst)
  126.     {
  127.         rendering = true;
  128.         // The meat of the controller, this is where we can actually render our particles
  129.         mat.SetTexture("_Sprite", particleTexture);
  130.        
  131.         // Blit the sceene without any interaction
  132.         Graphics.Blit( src, dst);
  133.        
  134.        
  135.         // Set the buffers
  136.         //particleBuffer.SetData(particleLocations);
  137.         mat.SetBuffer("particleBuffer", particleBuffer);
  138.         mat.SetBuffer ("particleColor", particleColorBuffer);
  139.         mat.SetPass (0);
  140.         // Draw the particles into the scene
  141.         Graphics.DrawProcedural(MeshTopology.LineStrip, particleCount);
  142.         rendering = false;
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement