public int CalcMemory() { int mem = 0; int skip = 1; if (optimized) { for (int i = 0; i < optparticles.Count; i++) { MegaCacheParticleHistoryOpt ph = optparticles[i]; mem += ph.pos.Length; mem += ph.vels.Length; mem += ph.rots.Length; mem += ph.scale.Length; mem += ph.spin.Length; } } else { for (int i = 0; i < particles.Count; i++) { MegaCacheParticleHistory ph = particles[i]; mem += ph.positions.Count * 12; mem += ph.vels.Count * 12; mem += ph.rots.Count * 12; mem += ph.scale.Count * 4; mem += ph.spin.Count * 4; } } return(mem / skip); }
public static void LoadPDASequence(MegaCacheParticle mod, string filename, int start, int end) { char[] namesplit = { '.' }; string[] fname = filename.Split(namesplit); List <int> dellist = new List <int>(); MegaCacheParticleImage img = ScriptableObject.CreateInstance <MegaCacheParticleImage>(); bool remove = false; //img.frames = 0; for (int i = start; i < end; i++) { if (i == end - 1) { remove = true; } string name = fname[0] + "." + i + ".pda"; //Debug.Log("name " + name); if (File.Exists(name)) { LoadPDAFile(mod, img, name, remove); //img.frames++; } } //Debug.Log("particles " + img.particles.Count); for (int i = img.particles.Count - 1; i >= 0; i--) { MegaCacheParticleHistory ph = img.particles[i]; //Debug.Log("c " + ph.positions.Count); if (ph.positions.Count <= 1) { img.particles.RemoveAt(i); } } if (mod.removealive) { for (int i = dellist.Count - 1; i >= 0; i--) { if (dellist[i] < img.particles.Count) { img.particles.RemoveAt(dellist[i]); } } } //img.maxparticles = img.particles.Count; mod.image = img; //Debug.Log("particles " + img.particles.Count); }
void OnDrawGizmosSelected() { if (showpaths && image) { Gizmos.color = Color.red; Gizmos.matrix = transform.localToWorldMatrix; Color col = showcolor; float len = image.frames / fps; float t = 0.0f; if (time < 0.0f) { time = 0.0f; } switch (loopmode) { case MegaCacheRepeatMode.Loop: t = Mathf.Repeat(time, len); break; case MegaCacheRepeatMode.Clamp: t = Mathf.Clamp(time, 0.0f, len); break; case MegaCacheRepeatMode.PingPong: t = Mathf.PingPong(time, len); break; } float alpha = t / len; float fn = alpha * (image.frames - 1); framenum = (int)fn; if (image.optimized) { MegaCacheParticleHistoryOpt ph = image.optparticles[framenum]; for (int j = 0; j < ph.count; j += showparticlestep) { Vector3 lpos = DecodeV3(ph.pos, j * 6, ph.posmin, ph.possize) * scaleall * emitscale; float scl = DecodeFloat(ph.scale, j, ph.scalemin, ph.scalesize); Gizmos.color = col; Gizmos.DrawSphere(lpos, scl * scaleall * sizescale); } } else { MegaCacheParticleHistory ph = image.particles[framenum]; for (int j = 0; j < ph.positions.Count; j += showparticlestep) { Vector3 lpos = ph.positions[j] * scaleall * emitscale; Gizmos.color = col; Gizmos.DrawSphere(lpos, ph.scale[j] * scaleall * sizescale); } } Gizmos.matrix = Matrix4x4.identity; } }
void UpdateParticles(float dt) { if (dt > 0.01f) { dt = 0.01f; } if (particle && image && image.particles.Count > 0) { //int count = particle.particleCount; particles = particle.particles; int ix = 0; Matrix4x4 tm = transform.localToWorldMatrix; time += Time.deltaTime * speed; float len = image.frames / fps; float t = 0.0f; switch (loopmode) { case MegaCacheRepeatMode.Loop: t = Mathf.Repeat(time, len); break; case MegaCacheRepeatMode.Clamp: t = Mathf.Clamp(time, 0.0f, len); break; case MegaCacheRepeatMode.PingPong: t = Mathf.PingPong(time, len); break; } float alpha = t / len; float fn = alpha * (image.frames - 1); framenum = (int)fn; MegaCacheParticleHistory ph = image.particles[framenum]; for (int i = 0; i < ph.positions.Count; i++) { particles[ix].position = tm.MultiplyPoint3x4(ph.positions[i] * scaleall); particles[ix].energy = alpha * len; particles[ix].startEnergy = len; particles[ix].size = ph.scale[i] * scaleall * sizescale; particles[ix].rotation = ph.rots[i][(int)axis]; ix++; } particle.particles = particles; } }
void LoadPDAPlayback(MegaCacheParticle mod, string filename, int start, int end) { char[] namesplit = { '.' }; string[] fname = filename.Split(namesplit); //List<int> dellist = new List<int>(); MegaCacheParticleImage img = ScriptableObject.CreateInstance <MegaCacheParticleImage>(); bool remove = false; img.frames = 0; for (int i = start; i < end; i++) { if (i == end - 1) { remove = true; } string name = fname[0] + "." + i + ".pda"; if (File.Exists(name)) { LoadPDAFile(mod, img, name, remove); } } img.maxparticles = 0; for (int i = img.particles.Count - 1; i >= 0; i--) { MegaCacheParticleHistory ph = img.particles[i]; if (ph.positions.Count > img.maxparticles) { img.maxparticles = ph.positions.Count; } } img.frames = img.particles.Count; mod.image = img; //Debug.Log("maxp " + img.maxparticles); }
void UpdateParticles(float dt) { if (dt > 0.01f) { dt = 0.01f; } if (particle && image && image.particles.Count > 0) { InitStates(); if (useemit) { emittime += dt * emitrate; int ecount = (int)emittime; if (ecount > 0) { particle.Emit(ecount); emittime -= 1.0f; } } int count = particle.particleCount; particles = particle.particles; int ix = 0; Matrix4x4 tm = transform.localToWorldMatrix; removeparticles.Clear(); float pscl = scaleall * sizescale * particle.minSize; for (int i = 0; i < activeparticles.Count; i++) { MegaCacheParticleHistory ph = image.particles[activeparticles[i]]; MegaCacheParticleState ps = states[activeparticles[i]]; ps.time += Time.deltaTime * speed * ps.locspeed; if (ps.time >= ph.life || ps.time < 0.0f) { removeparticles.Add(i); } else { float alpha = ps.time / ph.life; float fn = alpha * (ph.positions.Count - 1); framenum = (int)fn; float subalpha = fn - framenum; Vector3 lpos = Vector3.Lerp(ph.positions[framenum], ph.positions[framenum + 1], subalpha) * scaleall * ps.locscale; particles[ix].position = ps.tm.MultiplyPoint3x4(lpos); particles[ix].energy = ph.life - ps.time; particles[ix].startEnergy = ph.life; particles[ix].size = Mathf.Lerp(ph.scale[framenum], ph.scale[framenum + 1], subalpha) * pscl * ps.locscale; //particles[ix].rotation = Mathf.Lerp(ph.rots[framenum][(int)axis], ph.rots[framenum + 1][(int)axis], subalpha); particles[ix].rotation = Mathf.Lerp(ph.rots[framenum][(int)axis], ph.rots[framenum + 1][(int)axis], subalpha); ix++; } } for (int i = removeparticles.Count - 1; i >= 0; i--) { activeparticles.RemoveAt(removeparticles[i]); } if (activeparticles.Count < image.particles.Count) { if (count > activeparticles.Count) { int emit = count - activeparticles.Count; for (int i = 0; i < emit; i++) { MegaCacheParticleHistory ph = image.particles[particleindex]; MegaCacheParticleState ps = states[particleindex]; int px = 0; if (emitspeed * speed >= 0.0f) { ps.time = 0.0f; particles[ix].energy = ph.life; } else { ps.time = ph.life; particles[ix].energy = 0.0f; px = ph.positions.Count - 1; } ps.locscale = emitscale; ps.locspeed = emitspeed; ps.tm = tm; activeparticles.Add(particleindex++); particles[ix].position = tm.MultiplyPoint3x4(ph.positions[px] * scaleall); particles[ix].startEnergy = ph.life; //particles[ix].rotation = ph.rots[px][(int)axis] * Mathf.Rad2Deg; particles[ix].rotation = ph.rots[i][(int)axis]; // * Mathf.Rad2Deg; particles[ix].size = 0.0f; if (particleindex >= image.particles.Count) { particleindex = 0; } ix++; } } } else { //Debug.Log("No available particles"); } particle.particles = particles; } }
public static void LoadPDAFile(MegaCacheParticle mod, MegaCacheParticleImage img, string filename, bool remove) { StreamReader stream = File.OpenText(filename); string entireText = stream.ReadToEnd(); stream.Close(); char[] splitIdentifier = { ' ' }; //StringReader reader = new StringReader(entireText); offset = 0; //reader.ReadLine(); // ATTRIBUTES ReadLine(entireText); //string[] attribs = reader.ReadLine().Split(splitIdentifier, System.StringSplitOptions.RemoveEmptyEntries); string[] attribs = ReadLine(entireText).Split(splitIdentifier, System.StringSplitOptions.RemoveEmptyEntries); //reader.ReadLine(); // TYPES ReadLine(entireText); //string[] types = reader.ReadLine().Split(splitIdentifier, System.StringSplitOptions.RemoveEmptyEntries); // actual values string[] types = ReadLine(entireText).Split(splitIdentifier, System.StringSplitOptions.RemoveEmptyEntries); // actual values int[] attriboff = new int[attribs.Length]; int off = 0; for (int i = 0; i < types.Length; i++) { attriboff[i] = off; switch (types[i]) { case "V": off += 3; break; case "I": off += 1; break; case "R": off += 1; break; default: Debug.Log("Unknown Type " + types[i]); off += 1; break; } } //string[] vals = reader.ReadLine().Split(splitIdentifier, 2); string[] vals = ReadLine(entireText).Split(splitIdentifier, 2); int numParticles = int.Parse(vals[1]); //reader.ReadLine(); // BEGIN DATA ReadLine(entireText); for (int j = 0; j < numParticles; j++) { Vector3 pos = Vector3.zero; Vector3 rot = Vector3.zero; Vector3 vel = Vector3.zero; int id = 0; float life = 0.0f; float scale = 1.0f; float spin = 0.0f; //string ps = reader.ReadLine(); string ps = ReadLine(entireText); string[] values = ps.Split(splitIdentifier, 50); for (int a = 0; a < attribs.Length; a++) { int of = attriboff[a]; switch (attribs[a]) { case "position": pos.x = float.Parse(values[of]); pos.y = float.Parse(values[of + 1]); pos.z = float.Parse(values[of + 2]); break; case "velocity": vel.x = float.Parse(values[of]); vel.y = float.Parse(values[of + 1]); vel.z = float.Parse(values[of + 2]); break; case "id": id = int.Parse(values[of]); break; case "lifespanPP": life = (float)double.Parse(values[of]); break; //case "age": //age = float.Parse(values[of]); //break; case "radiusPP": scale = float.Parse(values[of]); break; default: break; } } MegaCacheParticleHistory ph; if (id >= img.particles.Count) { ph = new MegaCacheParticleHistory(); img.particles.Add(ph); Debug.Log("add particle " + img.particles.Count); } else { ph = img.particles[id]; } if (mod.vel) { ph.vels.Add(vel * mod.importscale); } if (mod.rot) { ph.rots.Add(rot); } if (mod.scale) { ph.scale.Add(scale * mod.importscale); } if (mod.spin) { ph.spin.Add(spin); } ph.life = life; ph.positions.Add(pos * mod.importscale); } }
public static void SaveCacheFile(MegaCacheParticle mod, string filename) { // save cache file FileStream fs = new FileStream(filename, FileMode.Create); if (fs != null) { BinaryWriter bw = new BinaryWriter(fs); if (bw != null) { MegaCacheParticleImage img = mod.image; int version = 1; bw.Write(version); bw.Write((int)img.particles.Count); bw.Write(mod.optimize); bw.Write(mod.saverot); bw.Write(mod.savevel); bw.Write(mod.savescale); bw.Write(mod.savespin); bw.Write(img.frames); bw.Write(img.maxparticles); for (int i = 0; i < img.particles.Count; i++) { MegaCacheParticleHistory ph = img.particles[i]; bw.Write(ph.positions.Count); bw.Write(ph.life); bw.Write(ph.id); if (mod.optimize) { Bounds bounds = MegaCacheUtils.GetBounds(ph.positions); bw.Write(bounds.min.x); bw.Write(bounds.min.y); bw.Write(bounds.min.z); bw.Write(bounds.size.x); bw.Write(bounds.size.y); bw.Write(bounds.size.z); for (int v = 0; v < ph.positions.Count; v++) { Vector3 pos = ph.positions[v]; short sb = (short)(((pos.x - bounds.min.x) / bounds.size.x) * 65535.0f); bw.Write(sb); sb = (short)(((pos.y - bounds.min.y) / bounds.size.y) * 65535.0f); bw.Write(sb); sb = (short)(((pos.z - bounds.min.z) / bounds.size.z) * 65535.0f); bw.Write(sb); } } else { for (int v = 0; v < ph.positions.Count; v++) { Vector3 pos = ph.positions[v]; bw.Write(pos.x); bw.Write(pos.y); bw.Write(pos.z); } } if (mod.savevel) { Bounds bounds = MegaCacheUtils.GetBounds(ph.vels); bw.Write(bounds.min.x); bw.Write(bounds.min.y); bw.Write(bounds.min.z); bw.Write(bounds.size.x); bw.Write(bounds.size.y); bw.Write(bounds.size.z); if (mod.optimize) { for (int v = 0; v < ph.vels.Count; v++) { Vector3 pos = ph.vels[v]; byte sb = (byte)(((pos.x - bounds.min.x) / bounds.size.x) * 255.0f); bw.Write(sb); sb = (byte)(((pos.y - bounds.min.y) / bounds.size.y) * 255.0f); bw.Write(sb); sb = (byte)(((pos.z - bounds.min.z) / bounds.size.z) * 255.0f); bw.Write(sb); } } else { for (int v = 0; v < ph.vels.Count; v++) { Vector3 pos = ph.vels[v]; bw.Write(pos.x); bw.Write(pos.y); bw.Write(pos.z); } } } if (mod.savescale) { if (mod.optimize) { Bounds bounds = MegaCacheUtils.GetBounds(ph.scale); bw.Write(bounds.min.x); bw.Write(bounds.size.x); for (int v = 0; v < ph.scale.Count; v++) { float scl = ph.scale[v]; byte sb = (byte)(((scl - bounds.min.x) / bounds.size.x) * 255.0f); bw.Write(sb); } } else { for (int v = 0; v < ph.scale.Count; v++) { bw.Write(ph.scale[v]); } } } if (mod.saverot) { if (mod.optimize) { Bounds bounds = MegaCacheUtils.GetBounds(ph.rots); bw.Write(bounds.min.x); bw.Write(bounds.min.y); bw.Write(bounds.min.z); bw.Write(bounds.size.x); bw.Write(bounds.size.y); bw.Write(bounds.size.z); for (int v = 0; v < ph.rots.Count; v++) { Vector3 scl = ph.rots[v]; byte sb = (byte)(((scl.x - bounds.min.x) / bounds.size.x) * 255.0f); bw.Write(sb); sb = (byte)(((scl.y - bounds.min.y) / bounds.size.y) * 255.0f); bw.Write(sb); sb = (byte)(((scl.z - bounds.min.z) / bounds.size.z) * 255.0f); bw.Write(sb); } } else { for (int v = 0; v < ph.rots.Count; v++) { Vector3 rot = ph.rots[v]; bw.Write(rot.x); bw.Write(rot.y); bw.Write(rot.z); } } } if (mod.savespin) { if (mod.optimize) { Bounds bounds = MegaCacheUtils.GetBounds(ph.spin); bw.Write(bounds.min.x); bw.Write(bounds.size.x); for (int v = 0; v < ph.spin.Count; v++) { float scl = ph.spin[v]; byte sb = (byte)(((scl - bounds.min.x) / bounds.size.x) * 255.0f); bw.Write(sb); } } else { for (int v = 0; v < ph.scale.Count; v++) { bw.Write(ph.scale[v]); } } } } bw.Close(); } fs.Close(); } }
public static void LoadFilePlayBack(MegaCacheParticle mod, string filename) { StreamReader stream = File.OpenText(filename); string entireText = stream.ReadToEnd(); stream.Close(); char[] splitIdentifier = { ' ' }; StringReader reader = new StringReader(entireText); int.Parse(reader.ReadLine()); // max int frames = int.Parse(reader.ReadLine()); MegaCacheParticleImage img; if (mod.image == null) { img = ScriptableObject.CreateInstance <MegaCacheParticleImage>(); } else { } int skip = 1; int scount = 0; int maxp = 0; for (int i = 0; i < frames; i++) { MegaCacheParticleHistory ph = new MegaCacheParticleHistory(); img.particles.Add(ph); int p = int.Parse(reader.ReadLine()); Vector3 pos = Vector3.zero; Vector3 rot = Vector3.zero; Vector3 vel = Vector3.zero; for (int j = 0; j < p; j++) { string ps = reader.ReadLine(); string[] brokenString = ps.Split(splitIdentifier, 50); //int id = int.Parse(brokenString[0]) - 1; if (scount == 0) { pos.x = float.Parse(brokenString[1]); pos.y = float.Parse(brokenString[2]); pos.z = float.Parse(brokenString[3]); float life = float.Parse(brokenString[12]); // / 30.0f; //float age = life - (float.Parse(brokenString[13])); // / 30.0f; if (mod.vel) { vel.x = float.Parse(brokenString[4]); vel.y = float.Parse(brokenString[5]); vel.z = float.Parse(brokenString[6]); ph.vels.Add(vel * mod.importscale); } if (mod.rot) { rot.x = float.Parse(brokenString[7]); rot.y = float.Parse(brokenString[8]); rot.z = float.Parse(brokenString[9]); ph.rots.Add(rot); } if (mod.scale) { float scale = float.Parse(brokenString[11]); ph.scale.Add(scale * mod.importscale); } if (mod.spin) { float spin = float.Parse(brokenString[10]); ph.spin.Add(spin); } ph.life = life; //ph.age.Add(age); ph.positions.Add(pos * mod.importscale); } if (ph.positions.Count > maxp) { maxp = ph.positions.Count; } } scount++; if (scount == skip) { scount = 0; } } for (int i = img.particles.Count - 1; i >= 0; i--) { MegaCacheParticleHistory ph = img.particles[i]; if (ph.positions.Count <= 1) { img.particles.RemoveAt(i); } } img.frames = frames; img.maxparticles = maxp; mod.image = img; }
public static void LoadFile(MegaCacheParticle mod, string filename) { //if ( mod.mode == MegaCacheParticleMode.Playback ) //{ //LoadFilePlayBack(mod, filename); //return; //} offset = 0; StreamReader stream = File.OpenText(filename); string entireText = stream.ReadToEnd(); stream.Close(); char[] splitIdentifier = { ' ' }; //StringReader reader = new StringReader(entireText); //int.Parse(reader.ReadLine()); // max int.Parse(ReadLine(entireText)); // max //int frames = int.Parse(reader.ReadLine()); int frames = int.Parse(ReadLine(entireText)); MegaCacheParticleImage img = ScriptableObject.CreateInstance <MegaCacheParticleImage>(); bool remove = false; List <int> dellist = new List <int>(); int skip = 1; int scount = 0; bool zerocheck = true; int sub = 0; for (int i = 0; i < frames; i++) { if (i == frames - 1) { remove = true; } //int p = int.Parse(reader.ReadLine()); int p = int.Parse(ReadLine(entireText)); Vector3 pos = Vector3.zero; Vector3 rot = Vector3.zero; Vector3 vel = Vector3.zero; for (int j = 0; j < p; j++) { //string ps = reader.ReadLine(); string ps = ReadLine(entireText); ps = ps.Replace(',', '.'); string[] brokenString = ps.Split(splitIdentifier, 50); int id = int.Parse(brokenString[0]); // - 1; if (zerocheck) { zerocheck = false; if (id == 0) { sub = 0; } else { sub = 1; } } id = id - sub; if (remove) { // Anything still active in the last frame cant be used dellist.Add(id); } else { if (scount == 0) { MegaCacheParticleHistory ph; if (id >= img.particles.Count) { ph = new MegaCacheParticleHistory(); img.particles.Add(ph); } else { ph = img.particles[id]; } pos.x = float.Parse(brokenString[1]); pos.y = float.Parse(brokenString[2]); pos.z = float.Parse(brokenString[3]); if (mod.yupimport) { pos = AdjustYUp(pos); } float life = float.Parse(brokenString[12]); // / 30.0f; //float age = life - (float.Parse(brokenString[13])); // / 30.0f; if (mod.vel) { vel.x = float.Parse(brokenString[4]); vel.y = float.Parse(brokenString[5]); vel.z = float.Parse(brokenString[6]); if (mod.yupimport) { vel = AdjustYUp(vel); } ph.vels.Add(vel * mod.importscale); } if (mod.rot) { rot.x = float.Parse(brokenString[7]) * Mathf.Rad2Deg; rot.y = float.Parse(brokenString[8]) * Mathf.Rad2Deg; rot.z = float.Parse(brokenString[9]) * Mathf.Rad2Deg; ph.rots.Add(rot); } if (mod.scale) { float scale = float.Parse(brokenString[11]); ph.scale.Add(scale * mod.importscale); } if (mod.spin) { float spin = float.Parse(brokenString[10]) * Mathf.Rad2Deg; ph.spin.Add(spin); } ph.life = life; //ph.age.Add(age); ph.positions.Add(pos * mod.importscale); } } } scount++; if (scount == skip) { scount = 0; } } for (int i = img.particles.Count - 1; i >= 0; i--) { MegaCacheParticleHistory ph = img.particles[i]; if (ph.positions.Count <= 1) { img.particles.RemoveAt(i); } } if (mod.removealive) { for (int i = dellist.Count - 1; i >= 0; i--) { if (dellist[i] < img.particles.Count) { img.particles.RemoveAt(dellist[i]); } } } mod.image = img; }
void OnDrawGizmosSelected() { int sfn = framenum; if (showpaths && image) { Gizmos.color = Color.red; Gizmos.matrix = transform.localToWorldMatrix; if (image.optimized) { for (int i = showstart; i < image.optparticles.Count; i += showparticlestep) { Vector3 lastpos = Vector3.zero; Color col = showcolor; MegaCacheParticleHistoryOpt ph = image.optparticles[i]; for (int j = 0; j < ph.count; j += showposstep) { float alpha = (float)j / (float)ph.count; float fn = alpha * (ph.count - 1); framenum = (int)fn; Vector3 lpos = DecodeV3(ph.pos, framenum * 6, ph.posmin, ph.possize) * scaleall * emitscale; if (j > 0) { col.a = 1.0f - alpha; Gizmos.color = col; Gizmos.DrawLine(lastpos, lpos); } lastpos = lpos; } } } else { for (int i = showstart; i < image.particles.Count; i += showparticlestep) { Vector3 lastpos = Vector3.zero; Color col = showcolor; MegaCacheParticleHistory ph = image.particles[i]; for (int j = 0; j < ph.positions.Count; j += showposstep) { float alpha = (float)j / (float)ph.positions.Count; float fn = alpha * (ph.positions.Count - 1); framenum = (int)fn; Vector3 lpos = ph.positions[framenum] * scaleall * emitscale; if (j > 0) { col.a = 1.0f - alpha; Gizmos.color = col; Gizmos.DrawLine(lastpos, lpos); } lastpos = lpos; } } } Gizmos.matrix = Matrix4x4.identity; } framenum = sfn; }
void UpdateParticles(float dt) { if (dt > 0.01f) { dt = 0.01f; } if (particle && image && image.particles.Count > 0) { particle.GetParticles(particles); int ix = 0; Matrix4x4 tm = transform.localToWorldMatrix; time += Time.deltaTime * speed; float len = image.frames / fps; float t = 0.0f; switch (loopmode) { case MegaCacheRepeatMode.Loop: t = Mathf.Repeat(time, len); break; case MegaCacheRepeatMode.Clamp: t = Mathf.Clamp(time, 0.0f, len); break; case MegaCacheRepeatMode.PingPong: t = Mathf.PingPong(time, len); break; } float alpha = t / len; float fn = alpha * (image.frames - 1); framenum = (int)fn; Debug.Log("fn " + framenum); MegaCacheParticleHistory ph = image.particles[framenum]; Debug.Log("fn " + framenum + " count " + ph.positions.Count); for (int i = 0; i < ph.positions.Count; i++) { particles[ix].position = tm.MultiplyPoint3x4(ph.positions[i] * scaleall); #if UNITY_2017 || UNITY_2018 particles[ix].remainingLifetime = alpha * len; #else particles[ix].lifetime = alpha * len; #endif particles[ix].startLifetime = len; #if UNITY_5_3 || UNITY_5_4 || UNITY_5_5 || UNITY_5_6 || UNITY_2017 || UNITY_2018 particles[ix].startSize = ph.scale[i] * scaleall * sizescale; #else particles[ix].size = ph.scale[i] * scaleall * sizescale; #endif //particles[ix].rotation = ph.rots[i][(int)axis] * Mathf.Rad2Deg; #if UNITY_5_4 || UNITY_5_5 || UNITY_5_6 || UNITY_2017 || UNITY_2018 particles[ix].rotation3D = ph.rots[i]; #else particles[ix].rotation = ph.rots[i][(int)axis]; // * Mathf.Rad2Deg; #endif ix++; } particle.SetParticles(particles, ix); } }
public static void LoadFile(MegaCacheParticle mod, string filename) { //if ( mod.mode == MegaCacheParticleMode.Playback ) //{ //LoadFilePlayBack(mod, filename); //return; //} StreamReader stream = File.OpenText(filename); string entireText = stream.ReadToEnd(); stream.Close(); char[] splitIdentifier = { ' ' }; StringReader reader = new StringReader(entireText); int.Parse(reader.ReadLine()); // max int frames = int.Parse(reader.ReadLine()); MegaCacheParticleImage img = ScriptableObject.CreateInstance<MegaCacheParticleImage>(); bool remove = false; List<int> dellist = new List<int>(); int skip = 1; int scount = 0; for ( int i = 0; i < frames; i++ ) { if ( i == frames - 1 ) remove = true; int p = int.Parse(reader.ReadLine()); Vector3 pos = Vector3.zero; Vector3 rot = Vector3.zero; Vector3 vel = Vector3.zero; for ( int j = 0; j < p; j++ ) { string ps = reader.ReadLine(); string[] brokenString = ps.Split(splitIdentifier, 50); int id = int.Parse(brokenString[0]) - 1; if ( remove ) { // Anything still active in the last frame cant be used dellist.Add(id); } else { if ( scount == 0 ) { MegaCacheParticleHistory ph; if ( id >= img.particles.Count ) { ph = new MegaCacheParticleHistory(); img.particles.Add(ph); } else ph = img.particles[id]; pos.x = float.Parse(brokenString[1]); pos.y = float.Parse(brokenString[2]); pos.z = float.Parse(brokenString[3]); float life = float.Parse(brokenString[12]); // / 30.0f; //float age = life - (float.Parse(brokenString[13])); // / 30.0f; if ( mod.vel ) { vel.x = float.Parse(brokenString[4]); vel.y = float.Parse(brokenString[5]); vel.z = float.Parse(brokenString[6]); ph.vels.Add(vel * mod.importscale); } if ( mod.rot ) { rot.x = float.Parse(brokenString[7]); rot.y = float.Parse(brokenString[8]); rot.z = float.Parse(brokenString[9]); ph.rots.Add(rot); } if ( mod.scale ) { float scale = float.Parse(brokenString[11]); ph.scale.Add(scale * mod.importscale); } if ( mod.spin ) { float spin = float.Parse(brokenString[10]); ph.spin.Add(spin); } ph.life = life; //ph.age.Add(age); ph.positions.Add(pos * mod.importscale); } } } scount++; if ( scount == skip ) { scount = 0; } } for ( int i = img.particles.Count - 1; i >= 0; i-- ) { MegaCacheParticleHistory ph = img.particles[i]; if ( ph.positions.Count <= 1 ) { img.particles.RemoveAt(i); } } if ( mod.removealive ) { for ( int i = dellist.Count - 1; i >= 0; i-- ) { if ( dellist[i] < img.particles.Count ) img.particles.RemoveAt(dellist[i]); } } mod.image = img; }
public static void LoadFilePlayBack(MegaCacheParticle mod, string filename) { StreamReader stream = File.OpenText(filename); string entireText = stream.ReadToEnd(); stream.Close(); char[] splitIdentifier = { ' ' }; StringReader reader = new StringReader(entireText); int.Parse(reader.ReadLine()); // max int frames = int.Parse(reader.ReadLine()); MegaCacheParticleImage img = ScriptableObject.CreateInstance<MegaCacheParticleImage>(); int skip = 1; int scount = 0; int maxp = 0; for ( int i = 0; i < frames; i++ ) { MegaCacheParticleHistory ph = new MegaCacheParticleHistory(); img.particles.Add(ph); int p = int.Parse(reader.ReadLine()); Vector3 pos = Vector3.zero; Vector3 rot = Vector3.zero; Vector3 vel = Vector3.zero; for ( int j = 0; j < p; j++ ) { string ps = reader.ReadLine(); string[] brokenString = ps.Split(splitIdentifier, 50); //int id = int.Parse(brokenString[0]) - 1; if ( scount == 0 ) { pos.x = float.Parse(brokenString[1]); pos.y = float.Parse(brokenString[2]); pos.z = float.Parse(brokenString[3]); float life = float.Parse(brokenString[12]); // / 30.0f; //float age = life - (float.Parse(brokenString[13])); // / 30.0f; if ( mod.vel ) { vel.x = float.Parse(brokenString[4]); vel.y = float.Parse(brokenString[5]); vel.z = float.Parse(brokenString[6]); ph.vels.Add(vel * mod.importscale); } if ( mod.rot ) { rot.x = float.Parse(brokenString[7]); rot.y = float.Parse(brokenString[8]); rot.z = float.Parse(brokenString[9]); ph.rots.Add(rot); } if ( mod.scale ) { float scale = float.Parse(brokenString[11]); ph.scale.Add(scale * mod.importscale); } if ( mod.spin ) { float spin = float.Parse(brokenString[10]); ph.spin.Add(spin); } ph.life = life; //ph.age.Add(age); ph.positions.Add(pos * mod.importscale); } if ( ph.positions.Count > maxp ) maxp = ph.positions.Count; } scount++; if ( scount == skip ) scount = 0; } for ( int i = img.particles.Count - 1; i >= 0; i-- ) { MegaCacheParticleHistory ph = img.particles[i]; if ( ph.positions.Count <= 1 ) { img.particles.RemoveAt(i); } } img.frames = frames; img.maxparticles = maxp; mod.image = img; }
public void ReadData(BinaryReader br) { int version = br.ReadInt32(); if (version < 2) { int pcount = br.ReadInt32(); bool optimize = br.ReadBoolean(); bool hasrot = br.ReadBoolean(); bool hasvel = br.ReadBoolean(); bool hasscale = br.ReadBoolean(); bool hasspin = br.ReadBoolean(); if (version == 1) { frames = br.ReadInt32(); maxparticles = br.ReadInt32(); } optimized = optimize; if (optimize) { //Debug.Log("v " + version); //Debug.Log("frames " + frames); //Debug.Log("max " + maxparticles); for (int i = 0; i < pcount; i++) { int count = br.ReadInt32(); MegaCacheParticleHistoryOpt ph = new MegaCacheParticleHistoryOpt(); ph.count = count; ph.life = br.ReadSingle(); ph.id = br.ReadInt32(); // load pos ph.posmin.x = br.ReadSingle(); ph.posmin.y = br.ReadSingle(); ph.posmin.z = br.ReadSingle(); ph.possize.x = br.ReadSingle(); ph.possize.y = br.ReadSingle(); ph.possize.z = br.ReadSingle(); ph.possize *= 1.0f / 65535.0f; ph.pos = br.ReadBytes(count * 6); if (hasvel) { ph.velmin.x = br.ReadSingle(); ph.velmin.y = br.ReadSingle(); ph.velmin.z = br.ReadSingle(); ph.velsize.x = br.ReadSingle(); ph.velsize.y = br.ReadSingle(); ph.velsize.z = br.ReadSingle(); ph.vels = br.ReadBytes(count * 3); } if (hasscale) { ph.scalemin = br.ReadSingle(); ph.scalesize = br.ReadSingle(); ph.scalesize *= 1.0f / 255.0f; ph.scale = br.ReadBytes(count); } if (hasrot) { ph.rotmin.x = br.ReadSingle(); ph.rotmin.y = br.ReadSingle(); ph.rotmin.z = br.ReadSingle(); ph.rotsize.x = br.ReadSingle(); ph.rotsize.y = br.ReadSingle(); ph.rotsize.z = br.ReadSingle(); ph.rotsize *= 1.0f / 255.0f; ph.rots = br.ReadBytes(count * 3); } if (hasspin) { ph.spinmin = br.ReadSingle(); ph.spinsize = br.ReadSingle(); ph.spinsize *= 1.0f / 255.0f; ph.spin = br.ReadBytes(count); } optparticles.Add(ph); } } else { for (int i = 0; i < pcount; i++) { int count = br.ReadInt32(); MegaCacheParticleHistory ph = new MegaCacheParticleHistory(); Vector3 p3 = Vector3.zero; // load pos for (int v = 0; v < count; v++) { p3.x = br.ReadSingle(); p3.y = br.ReadSingle(); p3.z = br.ReadSingle(); ph.positions.Add(p3); } if (hasvel) { for (int v = 0; v < count; v++) { p3.x = br.ReadSingle(); p3.y = br.ReadSingle(); p3.z = br.ReadSingle(); ph.vels.Add(p3); } } if (hasscale) { for (int v = 0; v < count; v++) { ph.scale.Add(br.ReadSingle()); } } if (hasrot) { for (int v = 0; v < count; v++) { p3.x = br.ReadSingle(); p3.y = br.ReadSingle(); p3.z = br.ReadSingle(); ph.rots.Add(p3); } } if (hasspin) { for (int v = 0; v < count; v++) { ph.spin.Add(br.ReadSingle()); } } particles.Add(ph); } } } }
public static void LoadFilePlayBack(MegaCacheParticle mod, string filename) { MegaCacheParticle.offset = 0; StreamReader stream = File.OpenText(filename); string entireText = stream.ReadToEnd(); stream.Close(); char[] splitIdentifier = { ' ' }; //StringReader reader = new StringReader(entireText); //int.Parse(reader.ReadLine()); // max int.Parse(MegaCacheParticle.ReadLine(entireText)); // max //int frames = int.Parse(reader.ReadLine()); int frames = int.Parse(MegaCacheParticle.ReadLine(entireText)); MegaCacheParticleImage img; if (mod.image == null) { img = ScriptableObject.CreateInstance <MegaCacheParticleImage>(); } else { if (EditorUtility.DisplayDialog("Add to or Replace", "Particles already loaded do you want to Replace?", "Yes", "No")) { img = ScriptableObject.CreateInstance <MegaCacheParticleImage>(); } else { img = mod.image; } } int skip = 1; int scount = 0; int maxp = 0; for (int i = 0; i < frames; i++) { MegaCacheParticleHistory ph = new MegaCacheParticleHistory(); img.particles.Add(ph); //int p = int.Parse(reader.ReadLine()); int p = int.Parse(MegaCacheParticle.ReadLine(entireText)); Vector3 pos = Vector3.zero; Vector3 rot = Vector3.zero; Vector3 vel = Vector3.zero; EditorUtility.DisplayProgressBar("Particle Import", "Importing frame " + i + " off " + frames, ((float)i / (float)frames)); for (int j = 0; j < p; j++) { //string ps = reader.ReadLine(); string ps = MegaCacheParticle.ReadLine(entireText); ps = ps.Replace(',', '.'); string[] brokenString = ps.Split(splitIdentifier, 50); //int id = int.Parse(brokenString[0]) - 1; if (scount == 0) { pos.x = float.Parse(brokenString[1]); if (mod.maxfile) { pos.x = -pos.x; } pos.y = float.Parse(brokenString[2]); pos.z = float.Parse(brokenString[3]); float life = float.Parse(brokenString[12]); // / 30.0f; //float age = life - (float.Parse(brokenString[13])); // / 30.0f; if (mod.vel) { vel.x = float.Parse(brokenString[4]); if (mod.maxfile) { vel.x = -vel.x; } vel.y = float.Parse(brokenString[5]); vel.z = float.Parse(brokenString[6]); ph.vels.Add(vel * mod.importscale); } if (mod.rot) { if (mod.maxfile) { rot.x = float.Parse(brokenString[7]) * Mathf.Rad2Deg; rot.z = float.Parse(brokenString[8]) * Mathf.Rad2Deg; rot.y = float.Parse(brokenString[9]) * Mathf.Rad2Deg; } else { rot.x = float.Parse(brokenString[7]) * Mathf.Rad2Deg; rot.y = float.Parse(brokenString[8]) * Mathf.Rad2Deg; rot.z = float.Parse(brokenString[9]) * Mathf.Rad2Deg; } ph.rots.Add(rot); } if (mod.scale) { float scale = float.Parse(brokenString[11]); ph.scale.Add(scale * mod.importscale); } if (mod.spin) { float spin = float.Parse(brokenString[10]) * Mathf.Rad2Deg; ph.spin.Add(spin); } ph.life = life; //ph.age.Add(age); ph.positions.Add(pos * mod.importscale); } if (ph.positions.Count > maxp) { maxp = ph.positions.Count; } } scount++; if (scount == skip) { scount = 0; } } EditorUtility.ClearProgressBar(); for (int i = img.particles.Count - 1; i >= 0; i--) { MegaCacheParticleHistory ph = img.particles[i]; if (ph.positions.Count < 1) { img.particles.RemoveAt(i); frames--; } } img.frames = frames; img.maxparticles = maxp; mod.image = img; }