diff --git a/Assets/Scenes/Bloody Mary.unity b/Assets/Scenes/Bloody Mary.unity index 18426db..7b4cc49 100644 --- a/Assets/Scenes/Bloody Mary.unity +++ b/Assets/Scenes/Bloody Mary.unity @@ -416,11 +416,11 @@ PrefabInstance: m_Modifications: - target: {fileID: 320690117028550720, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} propertyPath: m_LocalPosition.x - value: -2.8340137 + value: -15.03 objectReference: {fileID: 0} - target: {fileID: 320690117028550720, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} propertyPath: m_LocalPosition.y - value: -3.6601481 + value: -8.88 objectReference: {fileID: 0} - target: {fileID: 320690117028550720, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} propertyPath: m_LocalPosition.z @@ -881,11 +881,11 @@ PrefabInstance: m_Modifications: - target: {fileID: 320690117028550720, guid: 30e0cc55a67f02d4f92b2677ec4b1511, type: 3} propertyPath: m_LocalPosition.x - value: -0.7082175 + value: -12.904203 objectReference: {fileID: 0} - target: {fileID: 320690117028550720, guid: 30e0cc55a67f02d4f92b2677ec4b1511, type: 3} propertyPath: m_LocalPosition.y - value: -0.44170734 + value: -5.661559 objectReference: {fileID: 0} - target: {fileID: 320690117028550720, guid: 30e0cc55a67f02d4f92b2677ec4b1511, type: 3} propertyPath: m_LocalPosition.z diff --git a/Assets/Scripts/BloodComputeShader.cs b/Assets/Scripts/BloodComputeShader.cs index 73bfad3..e6f348f 100644 --- a/Assets/Scripts/BloodComputeShader.cs +++ b/Assets/Scripts/BloodComputeShader.cs @@ -1,5 +1,6 @@ using System.Collections; using System.Collections.Generic; +using System.Linq; using ComputeShaderUtility; using UnityEngine; using UnityEngine.Rendering; @@ -12,9 +13,15 @@ public struct Droplet public uint airborne; } +public struct Cum +{ + public Vector3 dir; + public float power; +} + 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; @@ -142,7 +149,7 @@ public class BloodComputeShader : MonoBehaviour } bool putBuffer = false; - uint[] bufferData = new uint[] {0,0}; + uint[] bufferData = new uint[] { 0, 0 }; if (readbackRequest.done) { @@ -167,22 +174,24 @@ public class BloodComputeShader : MonoBehaviour } // 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; } - + RequestAsyncReadback(); } - if (putBuffer) { + if (putBuffer) + { numParticlesConsumedBuffer.SetData(bufferData); bloodCompute.SetBuffer(UpdateDustKernel, "numParticlesConsumed", numParticlesConsumedBuffer); - + } ComputeHelper.Dispatch(bloodCompute, numParticles, 1, 1, UpdateDustKernel); @@ -226,7 +235,6 @@ public class BloodComputeShader : MonoBehaviour uint i = bufferLookPointer; int found = 0; - Droplet[] particles = new Droplet[numParticles]; var as_Particles = freeBloodReadRequest.GetData().ToArray(); int length = as_Particles.Length; // particleBuffer.GetData(particles); @@ -260,13 +268,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 = Random.insideUnitSphere * power; pow.z = Mathf.Abs(pow.z); - bloodCompute.SetVector("initialVelocity",pow); + bloodCompute.SetVector("initialVelocity", pow); ComputeHelper.Dispatch(bloodCompute, amount, 1, 1, InitDustKernel); @@ -275,6 +283,80 @@ public class BloodComputeShader : MonoBehaviour yield return null; } + public void createBloodButBetter(Vector3 pos, int amount, float power, Cum[] dirs) + { + StartCoroutine(penisBlood2(pos, amount, power, dirs)); + } + + IEnumerator penisBlood2(Vector3 loc, int amount, float power, Cum[] dirs) + { + RequestAllBloodStates(); + + // Wait until we get the state of all the particles from the gpu + while (!freeBloodReadRequest.done) + { + yield return new WaitForEndOfFrame(); + } + + // Find N particles which are disabled + + uint i = bufferLookPointer; + int found = 0; + + var as_Particles = freeBloodReadRequest.GetData().ToArray(); + int length = as_Particles.Length; + // particleBuffer.GetData(particles); + + uint[] particleIndeces = new uint[amount]; + + // oof + while (i < numParticles) + { + if (as_Particles[i % (length - 1)].enabled == 0) + { // Found unused particle + particleIndeces[found] = i; + found++; + + if (found >= amount) + break; + } + + i++; + } + + bufferLookPointer = (uint)((bufferLookPointer + amount) % numParticles); + + // Debug.Log(string.Join(", ", particleIndeces)); + + + // Test for race conditions + // yield return new WaitForSeconds(1.0f); + + bloodCompute.SetVector("particleInitPos", loc); + + int particlesPerLoad = found / dirs.Length; + + for (int idx = 0; idx < dirs.Length; idx++) + { + Vector3 pow = dirs[idx].dir * power; + pow.z = Mathf.Abs(pow.z); + bloodCompute.SetVector("initialVelocity", pow); + + bloodCompute.SetFloat("particleVel", dirs[idx].power); + bloodCompute.SetInt("particlesToInitialize", particlesPerLoad); + + // send data to gpu + freeParticleBuffer.SetData(particleIndeces.Take(particlesPerLoad).ToArray()); + bloodCompute.SetBuffer(InitDustKernel, "freeParticles", freeParticleBuffer); + + ComputeHelper.Dispatch(bloodCompute, amount, 1, 1, InitDustKernel); + } + + activeParticles += found; + + yield return null; + } + void OnDestroy() { ComputeHelper.Release(particleBuffer, positionBuffer, argsBuffer, numParticlesConsumedBuffer, freeParticleBuffer); @@ -282,6 +364,13 @@ public class BloodComputeShader : MonoBehaviour public void createBloodTest(int amount) { - createBlood(Vector3.zero, amount, 10.0f); + // createBlood(Vector3.zero, amount, 10.0f); + Cum[] dirs = { + // Random.insideUnitSphere, + new Cum {dir= Vector3.zero, power= 10.0f}, + new Cum {dir= Random.insideUnitSphere, power= 2.5f} + }; + + createBloodButBetter(Vector3.zero, amount, 10.0f, dirs); } }