Nullable <SimData.Particle> FindParticle(ComputeBuffer particleBuffer, int numParticles, int id) { Nullable <SimData.Particle> target = null; foundParticles.SetCounterValue(0); findParticles.SetBuffer(0, "particles", particleBuffer); findParticles.SetBuffer(0, "foundParticles", foundParticles); findParticles.SetInt("particleId", id); findParticles.SetInt("count", numParticles); findParticles.Dispatch(0, particleBuffer.count / 8, 1, 1); int[] args = new int[] { 0, 1, 0, 0, 0 }; foundParticlesArgs.SetData(args); ComputeBuffer.CopyCount(foundParticles, foundParticlesArgs, 0); foundParticlesArgs.GetData(args); //Debug.Log(string.Format("Found {0} particles with id {1}", args[0], id)); if (args[0] > 0) { var found = new SimData.Particle[1]; foundParticles.GetData(found); target = found[0]; } return(target); }
void Update() { if (Input.GetButtonDown("Fire1") && !EventSystem.current.IsPointerOverGameObject()) { Ray ray = camera.ScreenPointToRay(Input.mousePosition); targetParticleRenderer = null; targetParticle = null; int targetParticleIdx = -1; float targetParticleDist = float.MaxValue; var particleRenderers = GameObject.FindObjectsOfType <Particles>(); foreach (var particleRenderer in particleRenderers) { if (!particleRenderer.gameObject.activeSelf || particleRenderer.frameData.Frame == null) { continue; } SimData.SimFrame frame = (SimData.SimFrame)particleRenderer.frameData.Frame; unsafe { for (var i = 0; i < frame.particles.Length; i++) { var p = UnsafeUtility.ReadArrayElement <SimData.Particle>(frame.particles.GetUnsafeReadOnlyPtr(), i); if ( (p.flags & SimData.PARTICLE_FLAG_ACTIVE) < 1 || p.pos.x < particleVisibleRangeXSlider.LowValue || p.pos.x > particleVisibleRangeXSlider.HighValue || p.pos.y < particleVisibleRangeYSlider.LowValue || p.pos.y > particleVisibleRangeYSlider.HighValue || p.pos.z < particleVisibleRangeZSlider.LowValue || p.pos.z > particleVisibleRangeZSlider.HighValue ) { continue; } var pPos = p.pos.UnityVector(); var particlePos = particleRenderer.transform.position + pPos * particleRenderer.DrawScale; //Debug.Log("particlePos " + p.id + ", " + particlePos.ToString("F4") + ", r " + p.radius * 10); if (IntersectRaySphere(ray, particlePos, p.radius * particleRenderer.DrawScale)) { var particleDist = (ray.origin - particlePos).magnitude; if (particleDist < targetParticleDist) { targetParticleRenderer = particleRenderer; targetParticleIdx = i; targetParticle = p; targetParticleDist = particleDist; } } } //fixed (SimData.Particle* ps = particleRenderer.frameData.Frame.particles.GetUnsafeReadOnlyPtr()) //{ // Debug.Log("ps " + m[0] + ", " + m[1] + ", " + m[2] + ", " + m[3]); //} } } if (targetParticle != null) { SimData.Particle tp = (SimData.Particle)targetParticle; //LogParticleState(tp.id); foreach (var particleRenderer in particleRenderers) { particleRenderer.TargetParticleId = tp.id; } } else { foreach (var particleRenderer in particleRenderers) { particleRenderer.TargetParticleId = -1; } } } ResolveTargetParticle(); //if (Input.GetKeyDown(KeyCode.X)) //{ // if (targetParticle != null) // { // SimData.Particle tp = (SimData.Particle)targetParticle; // LogParticleState(tp.id); // } //} }