//spheres we use for visualisation instances = new Transform[SphereAmount]; for (int i = 0; i < SphereAmount; i++) { instances[i] = Instantiate(Prefab, transform).transform; }
1 2 3
//in update method for (int i = 0; i < instances.Length; i++) instances[i].localPosition = output[i];
[numthreads(64,1,1)] voidSpheres(uint3 id : SV_DispatchThreadID) { //generate 2 orthogonal vectors float3 baseDir = normalize(rand1dTo3d(id.x) - 0.5) * (rand1dTo1d(id.x)*0.9+0.1); float3 orthogonal = normalize(cross(baseDir, rand1dTo3d(id.x + 7.1393) - 0.5)) * (rand1dTo1d(id.x+3.7443)*0.9+0.1); //scale the time and give it a random offset float scaledTime = Time * 2 + rand1dTo1d(id.x) * 712.131234; //calculate a vector based on vectors float3 dir = baseDir * sin(scaledTime) + orthogonal * cos(scaledTime); Result[id.x] = dir * 20; }
[numthreads(64,1,1)] voidSpheres(uint3 id : SV_DispatchThreadID) { //generate 2 orthogonal vectors float3 baseDir = normalize(rand1dTo3d(id.x) - 0.5) * (rand1dTo1d(id.x)*0.9+0.1); float3 orthogonal = normalize(cross(baseDir, rand1dTo3d(id.x + 7.1393) - 0.5)) * (rand1dTo1d(id.x+3.7443)*0.9+0.1); //scale the time and give it a random offset float scaledTime = Time * 2 + rand1dTo1d(id.x) * 712.131234; //calculate a vector based on vectors float3 dir = baseDir * sin(scaledTime) + orthogonal * cos(scaledTime); Result[id.x] = dir * 20; }
ComputeBuffer resultBuffer; int kernel; uint threadGroupSize; Vector3[] output;
Transform[] instances;
voidStart() { //program we're executing kernel = Shader.FindKernel("Spheres"); Shader.GetKernelThreadGroupSizes(kernel, out threadGroupSize, out _, out _);
//buffer on the gpu in the ram resultBuffer = new ComputeBuffer(SphereAmount, sizeof(float) * 3); output = new Vector3[SphereAmount];
//spheres we use for visualisation instances = new Transform[SphereAmount]; for (int i = 0; i < SphereAmount; i++) { instances[i] = Instantiate(Prefab, transform).transform; } }