From a70473e7e8ea31b477d7252d5cd039d8920a2af3 Mon Sep 17 00:00:00 2001 From: SpoodyTheOne Date: Sun, 4 Feb 2024 09:29:52 +0100 Subject: [PATCH] Bloodmanager now has seperate values for blood mopped for both players --- Assets/Scripts/BloodComputeShader.cs | 50 +++-- .../ComputeShaders/BloodCompute.compute | 201 +++++++++--------- 2 files changed, 131 insertions(+), 120 deletions(-) diff --git a/Assets/Scripts/BloodComputeShader.cs b/Assets/Scripts/BloodComputeShader.cs index d4d0946..3dba8e6 100644 --- a/Assets/Scripts/BloodComputeShader.cs +++ b/Assets/Scripts/BloodComputeShader.cs @@ -15,7 +15,7 @@ public struct Droplet public class BloodComputeShader : MonoBehaviour { - public static BloodComputeShader Instance { get; private set; } + public static BloodComputeShader Instance { get; private set; } public int numParticles = 1000; public ComputeShader bloodCompute; @@ -28,6 +28,9 @@ public class BloodComputeShader : MonoBehaviour public int activeParticles = 0; public long score = 0; + public int mop1Clean = 0; + public int mop2Clean = 0; + public float squeakVolume = 0.0f; public AudioSource squeakPlayer; @@ -104,8 +107,8 @@ public class BloodComputeShader : MonoBehaviour argsBuffer = new ComputeBuffer(1, 5 * sizeof(uint), ComputeBufferType.IndirectArguments); argsBuffer.SetData(args); - ComputeHelper.CreateStructuredBuffer(ref numParticlesConsumedBuffer, 2); - numParticlesConsumedBuffer.SetData(new uint[] { 0, 0 }); + ComputeHelper.CreateStructuredBuffer(ref numParticlesConsumedBuffer, 3); + numParticlesConsumedBuffer.SetData(new uint[] { 0, 0, 0 }); bloodCompute.SetBuffer(UpdateDustKernel, "numParticlesConsumed", numParticlesConsumedBuffer); // Initialize with empty data @@ -145,50 +148,58 @@ public class BloodComputeShader : MonoBehaviour } bool putBuffer = false; - uint[] bufferData = new uint[] {0,0}; + uint[] bufferData = {0,0,0}; if (readbackRequest.done) { - bufferData[0] = readbackRequest.GetData()[0]; - bufferData[1] = readbackRequest.GetData()[1]; + bufferData = readbackRequest.GetData().ToArray(); // Blood cleaned - if (bufferData[0] > 0) + if (bufferData[0] > 0 || bufferData[1] > 0) { // Debug.Log("Cleaned " + bufferData[0]); - activeParticles -= (int)bufferData[0]; + uint totalBloodCleaned = bufferData[0] + bufferData[1]; + + activeParticles -= (int)totalBloodCleaned; score += bufferData[0]; - float mappedRumble = Convert.ToSingle(bufferData[0]).Remap(0, RumbleAmount, 0, 0.1f); + // this doesnt exist but ok + // float mappedRumble = Convert.ToSingle(bufferData[0]).Remap(0, RumbleAmount, 0, 0.1f); //RumbleManager.StartRumble(-1, 0, mappedRumble, 0.1f); squeakVolume += 0.1f; + mop1Clean += (int)bufferData[0]; + mop2Clean += (int)bufferData[1]; + // Reset counter putBuffer = true; bufferData[0] = 0; + bufferData[1] = 0; } // Blood hitting the floor - if (bufferData[1] > 0) { - splatterVolume += bufferData[1]/25.0f; - + if (bufferData[1] > 0) + { + splatterVolume += bufferData[1] / 25.0f; + // Debug.Log("splat x" + bufferData[1]); putBuffer = true; - bufferData[1] = 0; + bufferData[2] = 0; } - + RequestAsyncReadback(); } - if (putBuffer) { + if (putBuffer) + { numParticlesConsumedBuffer.SetData(bufferData); bloodCompute.SetBuffer(UpdateDustKernel, "numParticlesConsumed", numParticlesConsumedBuffer); - + } ComputeHelper.Dispatch(bloodCompute, numParticles, 1, 1, UpdateDustKernel); @@ -206,6 +217,9 @@ public class BloodComputeShader : MonoBehaviour if (squeakVolume < 0.001) squeakVolume = 0; + + mop1Clean = (int)(mop1Clean * 0.8f); + mop2Clean = (int)(mop2Clean * 0.8f); } void FixedUpdate() @@ -266,13 +280,13 @@ public class BloodComputeShader : MonoBehaviour // Test for race conditions // yield return new WaitForSeconds(1.0f); - bloodCompute.SetFloat("particleVel", power/4.0f); + bloodCompute.SetFloat("particleVel", power / 4.0f); bloodCompute.SetVector("particleInitPos", loc); bloodCompute.SetInt("particlesToInitialize", found); Vector3 pow = UnityEngine.Random.insideUnitSphere * power; pow.z = Mathf.Abs(pow.z); - bloodCompute.SetVector("initialVelocity",pow); + bloodCompute.SetVector("initialVelocity", pow); ComputeHelper.Dispatch(bloodCompute, amount, 1, 1, InitDustKernel); diff --git a/Assets/Scripts/ComputeShaders/BloodCompute.compute b/Assets/Scripts/ComputeShaders/BloodCompute.compute index b6dd556..6fcefd1 100644 --- a/Assets/Scripts/ComputeShaders/BloodCompute.compute +++ b/Assets/Scripts/ComputeShaders/BloodCompute.compute @@ -2,10 +2,10 @@ #pragma kernel UpdateDust struct Particle { - float3 position; - float3 velocity; + float3 position; + float3 velocity; uint enabled; - uint airborne; + uint airborne; }; RWStructuredBuffer positions; @@ -28,115 +28,112 @@ float gravity; float particleVel; // Hash function www.cs.ubc.ca/~rbridson/docs/schechter-sca08-turbulence.pdf -uint hash(uint state) -{ - state ^= 2747636419u; - state *= 2654435769u; - state ^= state >> 16; - state *= 2654435769u; - state ^= state >> 16; - state *= 2654435769u; - return state; +uint hash(uint state) { + state ^= 2747636419u; + state *= 2654435769u; + state ^= state >> 16; + state *= 2654435769u; + state ^= state >> 16; + state *= 2654435769u; + return state; } -float scaleToRange01(uint state) -{ - return state / 4294967295.0; +float scaleToRange01(uint state) { return state / 4294967295.0; } + +[numthreads(64, 1, 1)] void InitDust(uint3 id + : SV_DispatchThreadID) { + if (id.x > particlesToInitialize) { + return; + } + + uint i = freeParticles[id.x]; + + // if (particles[id.x].enabled != 0 || numParticlesInitialized[0] >= + // numParticles ) { + while (i <= numParticles) { + if (i == numParticles) { + return; + } + + if (particles[i].enabled == 0) { + break; + } + + i++; + } + + uint randState = i; + + randState = hash(randState); + float dv = scaleToRange01(randState) * 3.14f * 2.0f; + float dx = cos(dv); + float dy = sin(dv); + randState = hash(randState); + float dz = scaleToRange01(randState); + + float3 nvel = float3(dx, dy, dz) * particleVel; + + randState = hash(randState); + nvel.xy *= scaleToRange01(randState); + + randState = hash(randState); + float3 iv = initialVelocity * scaleToRange01(randState); + + particles[i].velocity = iv + nvel; + + particles[i].position = particleInitPos; + particles[i].position.z = 0.01; + particles[i].enabled = 1; + particles[i].airborne = 1; + positions[i] = float4(particles[i].position, 1 * size); + // particles[i].position = positions[i].xyz; + + // InterlockedAdd(numParticlesInitialized[0],1); } - -[numthreads(64,1,1)] -void InitDust (uint3 id : SV_DispatchThreadID) -{ - if (id.x > particlesToInitialize) { - return; - } - - uint i = freeParticles[id.x]; - - // if (particles[id.x].enabled != 0 || numParticlesInitialized[0] >= numParticles ) { - while (i <= numParticles) { - if (i == numParticles) { - return; - } - - if (particles[i].enabled == 0) { - break; - } - - i++; - } - - uint randState = i; - - randState = hash(randState); - float dv = scaleToRange01(randState) * 3.14f*2.0f; - float dx = cos(dv); - float dy = sin(dv); - randState = hash(randState); - float dz = scaleToRange01(randState); - - float3 nvel = float3(dx, dy, dz) * particleVel; - - randState = hash(randState); - nvel.xy *= scaleToRange01(randState); - - randState = hash(randState); - float3 iv = initialVelocity * scaleToRange01(randState); - - particles[i].velocity = iv + nvel; - - particles[i].position = particleInitPos; - particles[i].position.z = 0.01; - particles[i].enabled = 1; - particles[i].airborne = 1; - positions[i] = float4(particles[i].position, 1 * size); - // particles[i].position = positions[i].xyz; - - // InterlockedAdd(numParticlesInitialized[0],1); -} - -[numthreads(64,1,1)] -void UpdateDust (uint3 id : SV_DispatchThreadID) -{ - uint i = id.x; + [numthreads(64, 1, 1)] void UpdateDust(uint3 id + : SV_DispatchThreadID) { + uint i = id.x; if (particles[i].enabled == 0) { - return; - } - - float3 pos = particles[i].position; + return; + } - if (particles[i].position.z > 0) { - particles[i].velocity.z -= gravity * deltaTime; - } else { - particles[i].velocity -= particles[i].velocity * deltaTime * 15; - - if (particles[i].airborne == 1) { - particles[i].airborne = 0; + float3 pos = particles[i].position; - // Increase splattered particles - InterlockedAdd(numParticlesConsumed[1],1); - } + if (particles[i].position.z > 0) { + particles[i].velocity.z -= gravity * deltaTime; + } else { + particles[i].velocity -= particles[i].velocity * deltaTime * 15; - float3 offset1 = mop1Pos - pos; - offset1.z = 0; - float dist1 = dot(offset1, offset1); + if (particles[i].airborne == 1) { + particles[i].airborne = 0; - float3 offset2 = mop2Pos - pos; - offset2.z = 0; - float dist2 = dot(offset2, offset2); + // Increase splattered particles + InterlockedAdd(numParticlesConsumed[2], 1); + } - float dist = min(dist1, dist2); - - if (dist < CleanRadius) { - particles[i].enabled = 0; - // Increase cleaned particles - InterlockedAdd(numParticlesConsumed[0],1); - } - } + float3 offset1 = mop1Pos - pos; + offset1.z = 0; + float dist1 = dot(offset1, offset1); - particles[i].position += particles[i].velocity * deltaTime; - positions[i] = float4(particles[i].position, particles[i].enabled * size); + float3 offset2 = mop2Pos - pos; + offset2.z = 0; + float dist2 = dot(offset2, offset2); + + float dist = min(dist1, dist2); + + if (dist < CleanRadius) { + particles[i].enabled = 0; + // Increase cleaned particles + if (dist1 < dist2) { // Mop1 is the one that cleaned + InterlockedAdd(numParticlesConsumed[0], 1); + } else { + InterlockedAdd(numParticlesConsumed[1], 1); + } + } + } + + particles[i].position += particles[i].velocity * deltaTime; + positions[i] = float4(particles[i].position, particles[i].enabled * size); } -