private void oneshot() { LPRayCastHit[] hits = caster.RaycastWorld(); if (hits.Length > 0) { hits = hits.OrderBy(d => d.Fraction).ToArray(); IntPtr partsysptr = lpman.ParticleSystems[ParticleSystemImIn].GetPtr(); IntPtr particlesPointer = LPAPIParticleSystems.GetParticlesInShape(partsysptr , shape, hits[0].Position.x, hits[0].Position.y , transform.rotation.eulerAngles.z); int[] particlesArray = new int[1]; Marshal.Copy(particlesPointer, particlesArray, 0, 1); int foundNum = particlesArray[0]; if (foundNum > 0) { int[] Indices = new int[foundNum + 1]; Marshal.Copy(particlesPointer, Indices, 0, foundNum + 1); LPAPIParticles.SetSelectedParticleFlags(partsysptr, Indices, DebrisMaterial.GetInt()); LPAPIParticles.SetSelectedParticleUserData(partsysptr, Indices, 2); LPAPIParticles.ExplodeSelectedParticles(partsysptr, Indices, hits[0].Position.x, hits[0].Position.y, ExplosionStrenght); } } }
/// <summary> /// Returns a List of all Particle-Particle contacts in this particle system at the time it is called</summary> public List <LPSystemPartPartContact> GetParticleContacts() { List <LPSystemPartPartContact> contactslist = new List <LPSystemPartPartContact>(); IntPtr partpartcontactsPtr = LPAPIParticleSystems.GetParticleSystemContacts(PartSysPtr); int[] contactsArray = new int[1]; Marshal.Copy(partpartcontactsPtr, contactsArray, 0, 1); int contsNum = contactsArray[0]; int lenght = (contsNum * 4) + 1; if (contsNum > 0) { int[] contactdata = new int[lenght]; Marshal.Copy(partpartcontactsPtr, contactdata, 0, lenght); for (int i = 0; i < contsNum; i++) { contactslist.Add(new LPSystemPartPartContact() { ParticleAIndex = contactdata[(i * 4) + 1] , ParticleBIndex = contactdata[(i * 4) + 2] , ParticleAUserData = contactdata[(i * 4) + 3] , ParticleBUserData = contactdata[(i * 4) + 4] }); } } return(contactslist); }
/// <summary> /// Returns a List of all Particle-Fixture contacts in this particle system at the time it is called</summary> public List <LPSystemFixPartContact> GetBodyContacts() { List <LPSystemFixPartContact> contactslist = new List <LPSystemFixPartContact>(); IntPtr fixpartcontactsPtr = LPAPIParticleSystems.GetParticleSystemBodyContacts(PartSysPtr); float[] contactsArray = new float[1]; Marshal.Copy(fixpartcontactsPtr, contactsArray, 0, 1); int am = 7; int contsNum = (int)contactsArray[0]; int lenght = (contsNum * am) + 1; if (contsNum > 0) { float[] contactdata = new float[lenght]; Marshal.Copy(fixpartcontactsPtr, contactdata, 0, lenght); for (int i = 0; i < contsNum; i++) { contactslist.Add(new LPSystemFixPartContact() { ParticleIndex = (int)contactdata[(i * am) + 1] , ParticleUserData = (int)contactdata[(i * am) + 2] , BodyIndex = (int)contactdata[(i * am) + 3] , FixtureIndex = (int)contactdata[(i * am) + 4] , Normal = new Vector3(contactdata[(i * am) + 5] , contactdata[(i * am) + 6]) , Weight = contactdata[(i * am) + 7] }); } } return(contactslist); }
// Update is called once per frame void Update() { if (LPAPIParticleSystems.GetNumberOfParticles(sys.GetPtr()) <= 0) { return; } SetPositionToGroupCenteroid(); MoveGroupToMousePosition(); }
IEnumerator SplitGroup() { // Splitting a group can be costly so do not perform this operation each frame while (LPAPIParticleSystems.GetNumberOfParticles(sys.GetPtr()) > 0) { LPAPIParticleGroups.SplitParticleGroup(FindObjectOfType <LPManager>().ParticleSystems[0].GetPtr(), FindObjectOfType <LPParticleGroup>().GetPtr()); IntPtr largestGroup = sys.GetLargestGroupDataFromPlugin(); GetComponent <LPParticleGroup>().SetThingPtr(largestGroup); yield return(new WaitForSeconds(1f)); } }
/// <summary> /// Creates the particle system in the physics simulation and creates any existing particle groups</summary> public void Initialise(IntPtr world, bool debug, int index) { lpMan = FindObjectOfType <LPManager>(); if (Particles != null) { Debug.Log(Particles.Count); } Index = index; //PartSysPtr = LPAPIParticleSystems.CreateParticleSystem(world, ParticleRadius, Damping, GravityScale, Index); PartSysPtr = LPAPIParticleSystems.CreateParticleSystem2(world, ParticleRadius, Damping, GravityScale, Index, SurfaceTensionNormalStrenght, SurfaceTensionPressureStrenght, ViscousStrenght); LPAPIParticleSystems.SetDestructionByAge(PartSysPtr, DestroyByAgeAllowed); if (ParticleAmountLimit > 0) { LPAPIParticleSystems.SetMaxParticleCount(PartSysPtr, ParticleAmountLimit); } LPAPIParticleSystems.SetParticleSystemIndex(PartSysPtr, Index); if (debug) { Debug.Log("Particle System Created at: 0x" + PartSysPtr.ToInt64()); } foreach (LPParticleGroup _shape in GameObject.FindObjectsOfType <LPParticleGroup>()) { if (Index == _shape.ParticleSystemImIn && _shape.SpawnOnPlay) { _shape.Initialise(this); } } if (debug) { Debug.Log(LPAPIParticleSystems.GetNumberOfParticles(PartSysPtr) + " Particles created"); } Drawers = GetComponentsInChildren <LPDrawParticleSystem>(); for (int i = 0; i < Drawers.Length; i++) { Drawers[i].Initialise(this); } particlesCountArray = new float[1]; Particles = new List <LPParticle>(); particleData = new float[0]; }
/// <summary> /// Returns an array of the indices of every particle in this particle system within the shape passed to it /// Note that the 1st element of the array is the lenght of the array</summary> public int[] GetParticlesInShape(IntPtr shape, Transform tran) { IntPtr particlesPointer = LPAPIParticleSystems.GetParticlesInShape(PartSysPtr , shape, tran.position.x, tran.position.y , tran.rotation.eulerAngles.z); int[] particlesArray = new int[1]; Marshal.Copy(particlesPointer, particlesArray, 0, 1); int partsNum = particlesArray[0]; int[] particlePositions = new int[partsNum + 1]; if (partsNum > 0) { Marshal.Copy(particlesPointer, particlePositions, 0, partsNum + 1); } return(particlePositions); }
private void oneshot2(Vector3 pos) { IntPtr partsysptr = lpman.ParticleSystems[ParticleSystemImIn].GetPtr(); IntPtr particlesPointer = LPAPIParticleSystems.GetParticlesInShape(partsysptr , shape, pos.x, pos.y , transform.rotation.eulerAngles.z); int[] particlesArray = new int[1]; Marshal.Copy(particlesPointer, particlesArray, 0, 1); int foundNum = particlesArray[0]; if (foundNum > 0) { int[] Indices = new int[foundNum + 1]; Marshal.Copy(particlesPointer, Indices, 0, foundNum + 1); LPAPIParticles.SetSelectedParticleFlags(partsysptr, Indices, DebrisMaterial.GetInt()); LPAPIParticles.SetSelectedParticleUserData(partsysptr, Indices, 2); LPAPIParticles.ExplodeSelectedParticles(partsysptr, Indices, pos.x, pos.y, ExplosionStrenght); } }
public LPParticleGroup[] GetGroupDataFromPlugin() { int groupCount = LPAPIParticleSystems.GetParticleGroupCount(PartSysPtr); Groups = new LPParticleGroup[groupCount]; if (groupCount > 0) { IntPtr groupsPointer = LPAPIParticleSystems.GetParticleGroupPointers(PartSysPtr); IntPtr[] groupThingPtrs = new IntPtr[groupCount]; Marshal.Copy(groupsPointer, groupThingPtrs, 0, groupCount); for (int i = 0; i < groupCount; i++) { Groups[i] = new LPParticleGroup(); Groups[i].SetThingPtr(groupThingPtrs[i]); //Debug.Log("Group " + i + " has " + Groups[i].GetParticleCount()); } } return(Groups); }
public void Splode() { IntPtr partsysptr = lpman.ParticleSystems[ParticleSystemImIn].GetPtr(); IntPtr particlesPointer = LPAPIParticleSystems.GetParticlesInShape(partsysptr , shape, transform.position.x, transform.position.y , transform.rotation.eulerAngles.z); int[] particlesArray = new int[1]; Marshal.Copy(particlesPointer, particlesArray, 0, 1); int foundNum = particlesArray[0]; if (foundNum > 0) { int[] Indices = new int[foundNum + 1]; Marshal.Copy(particlesPointer, Indices, 0, foundNum + 1); LPAPIParticles.ExplodeSelectedParticles(partsysptr, Indices, transform.position.x, transform.position.y, ExplosionStrenght); } GetComponent <LPBody>().Delete(); Destroy(gameObject); }
void Splode(Vector3 splodepos) { Vector3 pos = Camera.main.ScreenToWorldPoint(splodepos); IntPtr partsysptr = lpman.ParticleSystems[ParticleSystemImIn].GetPtr(); IntPtr worldPtr = lpman.GetPtr(); IntPtr particlesPointer = LPAPIParticleSystems.GetParticlesInShape(worldPtr, partsysptr , shape, pos.x, pos.y , 0f); int[] particlesArray = new int[1]; Marshal.Copy(particlesPointer, particlesArray, 0, 1); int foundNum = particlesArray[0]; if (foundNum > 0) { int[] Indices = new int[foundNum + 1]; Marshal.Copy(particlesPointer, Indices, 0, foundNum + 1); LPAPIParticles.ExplodeSelectedParticles(partsysptr, Indices, pos.x, pos.y, ExplosionStrenght); } }
void FixedUpdate() { LPAPIParticleSystems.DestroyParticlesInShape(sys.GetPtr(), Shape, transform.position.x, transform.position.y , Mathf.Deg2Rad * transform.rotation.eulerAngles.z, false); }
public IntPtr GetLargestGroupDataFromPlugin() { return(LPAPIParticleSystems.GetParticleGroupPointerForLargestGroup(PartSysPtr)); }
/// <summary> /// Update the chosen particle data from the simulation and store it in this class</summary> public void UpdateData() { IntPtr particlesPointer = LPAPIParticleSystems.GetParticlesDetails(PartSysPtr, GetPositions, GetColors, GetLifeTimes , GetWeights, GetVelocities, GetUserData); Marshal.Copy(particlesPointer, particlesCountArray, 0, 1); int partsNum = (int)particlesCountArray[0]; // Allocate or remove particles // Removal is done so that the count will be correct. Reusable pooled objects would be better if performance is a problem if (Particles.Count < partsNum) { while (Particles.Count < partsNum) { Particles.Add(new LPParticle()); } } else if (partsNum == 0) { Particles.RemoveRange(0, Particles.Count); } else if (Particles.Count > partsNum) { Particles.RemoveRange(partsNum - 1, Particles.Count - partsNum - 1); } if (partsNum > 0) { int datanum = 0; if (GetPositions) { datanum += 2; } if (GetColors) { datanum += 4; } if (GetLifeTimes) { datanum += 1; } if (GetWeights) { datanum += 1; } if (GetVelocities) { datanum += 2; } if (GetUserData) { datanum += 1; } int numberOfCoordinates = (partsNum * datanum) + 1; if (particleData.Length < numberOfCoordinates) { particleData = new float[(int)(numberOfCoordinates * 1.5f)]; } Marshal.Copy(particlesPointer, particleData, 0, numberOfCoordinates); int curpos = 1; if (GetPositions) { for (int i = 0; i < partsNum; i++) { Particles[i].Position = new Vector3(particleData[curpos + (i * 2)], particleData[curpos + (i * 2) + 1]); } curpos += partsNum * 2; } if (GetColors) { for (int i = 0; i < partsNum; i++) { Particles[i]._Color = new Color(particleData[curpos + (i * 4)] / 255f, particleData[curpos + (i * 4) + 1] / 255f , particleData[curpos + (i * 4) + 2] / 255f, particleData[curpos + (i * 4) + 3] / 255f); } curpos += partsNum * 4; } if (GetLifeTimes) { for (int i = 0; i < partsNum; i++) { Particles[i].LifeTime = particleData[curpos + i]; } curpos += partsNum; } if (GetWeights) { for (int i = 0; i < partsNum; i++) { Particles[i].Weight = particleData[curpos + i]; } curpos += partsNum; } if (GetVelocities) { for (int i = 0; i < partsNum; i++) { Particles[i].Velocity = new Vector3(particleData[curpos + (i * 2)], particleData[curpos + (i * 2) + 1]); } curpos += partsNum * 2; } if (GetUserData) { for (int i = 0; i < partsNum; i++) { Particles[i].UserData = (int)particleData[curpos + i]; } } if (GetGroupData) { GetGroupDataFromPlugin(); } } }
/// <summary> /// Update the chosen particle data from the simulation and store it in this class</summary> public void UpdateData() { IntPtr particlesPointer = LPAPIParticleSystems.GetParticlesDetails(PartSysPtr, GetPositions, GetColors, GetLifeTimes , GetWeights, GetVelocities, GetUserData); float[] particlesArray = new float[1]; Marshal.Copy(particlesPointer, particlesArray, 0, 1); int partsNum = (int)particlesArray[0]; Particles = new LPParticle[partsNum]; if (partsNum > 0) { int datanum = 0; if (GetPositions) { datanum += 2; } if (GetColors) { datanum += 4; } if (GetLifeTimes) { datanum += 1; } if (GetWeights) { datanum += 1; } if (GetVelocities) { datanum += 2; } if (GetUserData) { datanum += 1; } int numberOfCoordinates = (partsNum * datanum) + 1; float[] pd = new float[numberOfCoordinates]; Marshal.Copy(particlesPointer, pd, 0, numberOfCoordinates); for (int i = 0; i < partsNum; i++) { Particles[i] = new LPParticle(); } int curpos = 1; if (GetPositions) { for (int i = 0; i < partsNum; i++) { Particles[i].Position = new Vector3(pd[curpos + (i * 2)], pd[curpos + (i * 2) + 1]); } curpos += partsNum * 2; } if (GetColors) { for (int i = 0; i < partsNum; i++) { Particles[i]._Color = new Color(pd[curpos + (i * 4)] / 255f, pd[curpos + (i * 4) + 1] / 255f , pd[curpos + (i * 4) + 2] / 255f, pd[curpos + (i * 4) + 3] / 255f); } curpos += partsNum * 4; } if (GetLifeTimes) { for (int i = 0; i < partsNum; i++) { Particles[i].LifeTime = pd[curpos + i]; } curpos += partsNum; } if (GetWeights) { for (int i = 0; i < partsNum; i++) { Particles[i].Weight = pd[curpos + i]; } curpos += partsNum; } if (GetVelocities) { for (int i = 0; i < partsNum; i++) { Particles[i].Velocity = new Vector3(pd[curpos + (i * 2)], pd[curpos + (i * 2) + 1]); } curpos += partsNum * 2; } if (GetUserData) { for (int i = 0; i < partsNum; i++) { Particles[i].UserData = (int)pd[curpos + i]; } } } }
/// <summary> /// Sets up the physics world and the bodies, particle systems and joints in it. /// This is called on Awake() so you can implement your code on Start() and the liquidfun stuff will be ready</summary> void Awake() { //Always turn off debug messages in a build (Debug.Log writes to a log file and costs performance) if (!Application.isEditor && !Debug.isDebugBuild) { DebugMessages = false; } //Check for duplicate LPmanagers if (GameObject.FindObjectsOfType <LPManager>().Length > 1) { Debug.LogError("There is more than one LiquidFunManager in your scene. There can be only one!"); } else { //Create contact listener c# object ContactListener = new LPContactListener(); //Create World worldPtr = LPAPIWorld.CreateWorld(Gravity.x, Gravity.y); if (DebugMessages) { Debug.Log("World Created at: 0x" + worldPtr.ToInt64()); } //Initialise Contact Listener if (UseContactListener) { ContactListener.Initialise(worldPtr); } //Create bodies LPBody[] bodies = GameObject.FindObjectsOfType <LPBody>(); foreach (LPBody bod in bodies) { if (bod.SpawnOnPlay) { bod.Initialise(this); } } if (DebugMessages) { Debug.Log(bodies.Length + " Bodies created"); } //Create Joints foreach (LPJoint joint in GameObject.FindObjectsOfType <LPJoint>()) { if (joint.GetType() != typeof(LPJointGear)) { if (joint.SpawnOnPlay) { joint.Initialise(this); } } } foreach (LPJoint joint in GameObject.FindObjectsOfType <LPJointGear>()) { if (joint.SpawnOnPlay) { joint.Initialise(this); } } //Create Particles ParticleSystems = GameObject.FindObjectsOfType <LPParticleSystem>(); for (int i = 0; i < ParticleSystems.Length; i++) { ParticleSystems[i].Initialise(worldPtr, DebugMessages, i); } //Set or Determine recommended number of particle iterations if (OverrideParticleIterations) { m_particleIterations = ParticleIterationsOverride; } else { m_particleIterations = LPAPIParticleSystems.GetParticleIterations(Gravity.magnitude, ParticleSystems[0].ParticleRadius, TimeStep); if (DebugMessages) { Debug.Log("Recommended number of particle iterations is " + m_particleIterations.ToString()); } } } }