public void LoadFumeFrame(MegaFlow flow, string filename) { if ( flow.frames.Count > 0 ) { if ( !EditorUtility.DisplayDialog("Add to or Replace", "Add new frame to existing frames", "Add", "Replace") ) flow.DestroyFrames(); } flow.AddFrame(MegaFlowFumeFX.LoadFrame(filename)); }
public void LoadFGASeq(MegaFlow flow, string filename, int first, int last, int step) { if ( flow.frames.Count > 0 ) { if ( !EditorUtility.DisplayDialog("Add to or Replace", "Add new frames to existing frames, or Replace All", "Add", "Replace") ) flow.DestroyFrames(); } for ( int i = first; i <= last; i += step ) { float a = (float)(i + 1 - first) / (last - first); if ( !EditorUtility.DisplayCancelableProgressBar("Loading Fluid Frames", "Frame " + i, a) ) flow.AddFrame(MegaFlowFGA.LoadFrame(filename, i, flow.namesplit, flow.decform)); else break; } EditorUtility.ClearProgressBar(); }
public void CreateFlow() { int gx = gridx; int gy = gridy; int gz = gridz; if (square) { // find major axis int axis = 0; if (Mathf.Abs(size.x) > Mathf.Abs(size.y)) { if (Mathf.Abs(size.x) > Mathf.Abs(size.z)) { axis = 0; } else { axis = 2; } } else { if (Mathf.Abs(size.y) > Mathf.Abs(size.z)) { axis = 1; } else { axis = 2; } } float csize = size[axis] / gridx; cellsize = new Vector3(csize, csize, csize); } else { cellsize.x = size.x / gridx; cellsize.y = size.y / gridy; cellsize.z = size.z / gridz; } gx = (int)(size.x / cellsize.x); gy = (int)(size.y / cellsize.y); gz = (int)(size.z / cellsize.z); cells = new Vector3[gx * gy * gz]; for (int i = 0; i < cells.Length; i++) { cells[i] = startval; } Vector3 pos = Vector3.zero; Vector3 half = cellsize * 0.5f; Vector3 tan = Vector3.zero; int kn = 0; float alpha = 0.0f; List <MegaFlowContrib> contrib = new List <MegaFlowContrib>(); for (int z = 0; z < gz; z++) { pos.z = (z * cellsize.z) + half.z; for (int y = 0; y < gy; y++) { pos.y = (y * cellsize.y) + half.y; for (int x = 0; x < gx; x++) { pos.x = (x * cellsize.x) + half.x; contrib.Clear(); float nearest = float.MaxValue; Vector3 neardelta = Vector3.zero; for (int i = 0; i < splines.Count; i++) { MegaFlowSpline fs = splines[i]; if (fs.include) { Vector3 np = fs.shape.FindNearestPointWorldShape(pos, 5, ref kn, ref tan, ref alpha); Vector3 delta = np - pos; float dist = delta.magnitude; if (dist < nearest) { nearest = dist; neardelta = delta; } if (dist < fs.falloffdist) { MegaFlowContrib con = new MegaFlowContrib(); con.src = fs; con.dist = dist; con.vel = tan * fs.velocity; con.delta = delta.normalized; contrib.Add(con); } } } if (contrib.Count > 0) { float tweight = 0.0f; for (int c = 0; c < contrib.Count; c++) { tweight += contrib[c].src.weight; } Vector3 vel = cells[(x * gz * gy) + (z * gy) + y]; //Vector3.zero; for (int c = 0; c < contrib.Count; c++) { float a = contrib[c].dist / contrib[c].src.falloffdist; float lerp = contrib[c].src.falloffcrv.Evaluate(a); switch (contrib[c].src.mode) { case MegaFlowMode.Attract: vel += Vector3.Lerp(contrib[c].delta, contrib[c].vel, lerp) * velocity * (contrib[c].src.weight / tweight); break; case MegaFlowMode.Repulse: vel += Vector3.Lerp(-contrib[c].delta, contrib[c].vel, lerp) * velocity * (contrib[c].src.weight / tweight); break; case MegaFlowMode.Flow: vel += Vector3.Lerp(Vector3.zero, contrib[c].vel, lerp) * velocity * (contrib[c].src.weight / tweight); break; } } cells[(x * gz * gy) + (z * gy) + y] = vel; } else { cells[(x * gz * gy) + (z * gy) + y] = neardelta.normalized * velocity; } } } } if (flow) { MegaFlowFrame newf = ScriptableObject.CreateInstance <MegaFlowFrame>(); newf.gridDim2[0] = gx; newf.gridDim2[1] = gy; newf.gridDim2[2] = gz; newf.size = size; newf.gsize = newf.size; // griddim should have a name change newf.spacing.x = newf.size.x / newf.gridDim2[0]; newf.spacing.y = newf.size.y / newf.gridDim2[1]; newf.spacing.z = newf.size.z / newf.gridDim2[2]; newf.oos.x = 1.0f / newf.spacing.x; newf.oos.y = 1.0f / newf.spacing.y; newf.oos.z = 1.0f / newf.spacing.z; newf.vel.AddRange(cells); flow.AddFrame(newf); } }
public void CreateFlow(MegaFlowCreateFromSplines mod, MegaFlow flow) { int gx = mod.gridx; int gy = mod.gridy; int gz = mod.gridz; if (mod.square) { int axis = 0; if (Mathf.Abs(mod.size.x) > Mathf.Abs(mod.size.y)) { if (Mathf.Abs(mod.size.x) > Mathf.Abs(mod.size.z)) { axis = 0; } else { axis = 2; } } else { if (Mathf.Abs(mod.size.y) > Mathf.Abs(mod.size.z)) { axis = 1; } else { axis = 2; } } float csize = mod.size[axis] / mod.gridx; mod.cellsize = new Vector3(csize, csize, csize); } else { mod.cellsize.x = mod.size.x / mod.gridx; mod.cellsize.y = mod.size.y / mod.gridy; mod.cellsize.z = mod.size.z / mod.gridz; } gx = (int)(mod.size.x / mod.cellsize.x); gy = (int)(mod.size.y / mod.cellsize.y); gz = (int)(mod.size.z / mod.cellsize.z); Vector3[] cells = new Vector3[gx * gy * gz]; for (int i = 0; i < cells.Length; i++) { cells[i] = mod.startval; } Vector3 pos = Vector3.zero; Vector3 half = mod.cellsize * 0.5f; Vector3 tan = Vector3.zero; int kn = 0; float alpha = 0.0f; List <MegaFlowContrib> contrib = new List <MegaFlowContrib>(); Matrix4x4 offtm = Matrix4x4.TRS(-mod.size * 0.5f, Quaternion.identity, Vector3.one); Matrix4x4 tm = mod.transform.localToWorldMatrix * offtm; if (mod.texture) { Vector3 p; Color[] cols = mod.texture.GetPixels(); for (int z = 0; z < gz; z++) { int tz = (int)(((float)z / (float)gz) * mod.texture.depth); // * f.size.z; for (int y = 0; y < gy; y++) { int ty = (int)(((float)y / (float)gy) * mod.texture.height); // * f.size.y; for (int x = 0; x < gx; x++) { int tx = (int)(((float)x / (float)gx) * mod.texture.width); // * f.size.x; Color cvel = cols[(tz * mod.texture.width * mod.texture.height) + (ty * mod.texture.width) + tx]; p.x = (cvel.r - 0.5f) * 2.0f; p.y = (cvel.g - 0.5f) * 2.0f; p.z = (cvel.b - 0.5f) * 2.0f; cells[(x * gz * gy) + (z * gy) + y] = p * mod.texturescale; } } } } for (int z = 0; z < gz; z++) { pos.z = (z * mod.cellsize.z) + half.z; EditorUtility.DisplayProgressBar("Building Vector Field", "Building", (float)z / (float)gz); for (int y = 0; y < gy; y++) { pos.y = (y * mod.cellsize.y) + half.y; for (int x = 0; x < gx; x++) { pos.x = (x * mod.cellsize.x) + half.x; contrib.Clear(); float nearest = float.MaxValue; Vector3 neardelta = Vector3.zero; for (int i = 0; i < mod.splines.Count; i++) { MegaFlowSpline fs = mod.splines[i]; if (fs.include) { Vector3 wp = tm.MultiplyPoint3x4(pos); Vector3 np = fs.shape.FindNearestPointWorld(wp, 5, ref kn, ref tan, ref alpha); Vector3 delta = np - wp; float dist = delta.magnitude; float fdist = fs.distcrv.Evaluate(alpha) * fs.falloffdist; if (dist < nearest) { nearest = dist; neardelta = delta; } if (dist < fdist) { MegaFlowContrib con = new MegaFlowContrib(); con.src = fs; con.dist = dist; con.vel = (tan - np).normalized * fs.velocity; con.delta = delta.normalized; con.alpha = alpha; con.fdist = fdist; contrib.Add(con); } } } if (contrib.Count > 0) { float tweight = 0.0f; for (int c = 0; c < contrib.Count; c++) { tweight += contrib[c].src.weight; } Vector3 vel = cells[(x * gz * gy) + (z * gy) + y]; //Vector3.zero; for (int c = 0; c < contrib.Count; c++) { float a = contrib[c].dist / contrib[c].fdist; float lerp = contrib[c].src.falloffcrv.Evaluate(a); float v = mod.velocity * contrib[c].src.velcrv.Evaluate(contrib[c].alpha); switch (contrib[c].src.mode) { case MegaFlowMode.Attract: vel += Vector3.Lerp(contrib[c].delta, contrib[c].vel, lerp) * v * (contrib[c].src.weight / tweight); break; case MegaFlowMode.Repulse: vel += Vector3.Lerp(-contrib[c].delta, contrib[c].vel, lerp) * v * (contrib[c].src.weight / tweight); break; case MegaFlowMode.Flow: vel += Vector3.Lerp(Vector3.zero, contrib[c].vel, lerp) * v * (contrib[c].src.weight / tweight); break; } } cells[(x * gz * gy) + (z * gy) + y] = vel; } else { Vector3 vl = cells[(x * gz * gy) + (z * gy) + y]; //Vector3.zero; switch (mod.emtyspacemode) { case MegaFlowMode.Attract: vl = (neardelta.normalized * mod.velocity) + mod.startval; break; case MegaFlowMode.Repulse: vl = (-neardelta.normalized * mod.velocity) + mod.startval; break; case MegaFlowMode.Flow: break; } cells[(x * gz * gy) + (z * gy) + y] = vl; } } } } EditorUtility.ClearProgressBar(); for (int i = 0; i < mod.modifiers.Count; i++) { EditorUtility.DisplayProgressBar("Adding Modifiers", "Building", (float)i / (float)mod.modifiers.Count); MegaFlowModifier fmod = mod.modifiers[i]; if (fmod.include && fmod.obj) { Ray ray = new Ray(Vector3.zero, Vector3.zero); for (int z = 0; z < gz; z++) { pos.z = (z * mod.cellsize.z) + half.z; for (int y = 0; y < gy; y++) { pos.y = (y * mod.cellsize.y) + half.y; for (int x = 0; x < gx; x++) { pos.x = (x * mod.cellsize.x) + half.x; Vector3 wpos = tm.MultiplyPoint3x4(pos); Vector3 origin = wpos; origin.y += 1000.0f; ray.origin = origin; ray.direction = Vector3.down; RaycastHit hit; if (fmod.obj.Raycast(ray, out hit, 1000.0f)) { ray.direction = Vector3.up; origin.y -= 2000.0f; ray.origin = origin; if (fmod.obj.Raycast(ray, out hit, 1000.0f)) { switch (fmod.type) { case MegaFlowModType.VelChange: Vector3 vel = cells[(x * gz * gy) + (z * gy) + y]; vel *= fmod.amount; cells[(x * gz * gy) + (z * gy) + y] = vel; break; } } } } } } } } EditorUtility.ClearProgressBar(); if (flow) { MegaFlowFrame newf = ScriptableObject.CreateInstance <MegaFlowFrame>(); newf.gridDim2[0] = gx; newf.gridDim2[1] = gy; newf.gridDim2[2] = gz; newf.size = mod.size; newf.gsize = newf.size; newf.spacing.x = newf.size.x / newf.gridDim2[0]; newf.spacing.y = newf.size.y / newf.gridDim2[1]; newf.spacing.z = newf.size.z / newf.gridDim2[2]; newf.oos.x = 1.0f / newf.spacing.x; newf.oos.y = 1.0f / newf.spacing.y; newf.oos.z = 1.0f / newf.spacing.z; newf.vel.AddRange(cells); flow.AddFrame(newf); } }
public void CreateFlow(MegaFlowCreateFromSplines mod, MegaFlow flow) { int gx = mod.gridx; int gy = mod.gridy; int gz = mod.gridz; if ( mod.square ) { int axis = 0; if ( Mathf.Abs(mod.size.x) > Mathf.Abs(mod.size.y) ) { if ( Mathf.Abs(mod.size.x) > Mathf.Abs(mod.size.z) ) axis = 0; else axis = 2; } else { if ( Mathf.Abs(mod.size.y) > Mathf.Abs(mod.size.z) ) axis = 1; else axis = 2; } float csize = mod.size[axis] / mod.gridx; mod.cellsize = new Vector3(csize, csize, csize); } else { mod.cellsize.x = mod.size.x / mod.gridx; mod.cellsize.y = mod.size.y / mod.gridy; mod.cellsize.z = mod.size.z / mod.gridz; } gx = (int)(mod.size.x / mod.cellsize.x); gy = (int)(mod.size.y / mod.cellsize.y); gz = (int)(mod.size.z / mod.cellsize.z); Vector3[] cells = new Vector3[gx * gy * gz]; for ( int i = 0; i < cells.Length; i++ ) cells[i] = mod.startval; Vector3 pos = Vector3.zero; Vector3 half = mod.cellsize * 0.5f; Vector3 tan = Vector3.zero; int kn = 0; float alpha = 0.0f; List<MegaFlowContrib> contrib = new List<MegaFlowContrib>(); Matrix4x4 offtm = Matrix4x4.TRS(-mod.size * 0.5f, Quaternion.identity, Vector3.one); Matrix4x4 tm = mod.transform.localToWorldMatrix * offtm; if ( mod.texture ) { Vector3 p; Color[] cols = mod.texture.GetPixels(); for ( int z = 0; z < gz; z++ ) { int tz = (int)(((float)z / (float)gz) * mod.texture.depth); // * f.size.z; for ( int y = 0; y < gy; y++ ) { int ty = (int)(((float)y / (float)gy) * mod.texture.height); // * f.size.y; for ( int x = 0; x < gx; x++ ) { int tx = (int)(((float)x / (float)gx) * mod.texture.width); // * f.size.x; Color cvel = cols[(tz * mod.texture.width * mod.texture.height) + (ty * mod.texture.width) + tx]; p.x = (cvel.r - 0.5f) * 2.0f; p.y = (cvel.g - 0.5f) * 2.0f; p.z = (cvel.b - 0.5f) * 2.0f; cells[(x * gz * gy) + (z * gy) + y] = p * mod.texturescale; } } } } for ( int z = 0; z < gz; z++ ) { pos.z = (z * mod.cellsize.z) + half.z; EditorUtility.DisplayProgressBar("Building Vector Field", "Building", (float)z / (float)gz); for ( int y = 0; y < gy; y++ ) { pos.y = (y * mod.cellsize.y) + half.y; for ( int x = 0; x < gx; x++ ) { pos.x = (x * mod.cellsize.x) + half.x; contrib.Clear(); float nearest = float.MaxValue; Vector3 neardelta = Vector3.zero; for ( int i = 0; i < mod.splines.Count; i++ ) { MegaFlowSpline fs = mod.splines[i]; if ( fs.include ) { Vector3 wp = tm.MultiplyPoint3x4(pos); Vector3 np = fs.shape.FindNearestPointWorld(wp, 5, ref kn, ref tan, ref alpha); Vector3 delta = np - wp; float dist = delta.magnitude; float fdist = fs.distcrv.Evaluate(alpha) * fs.falloffdist; if ( dist < nearest ) { nearest = dist; neardelta = delta; } if ( dist < fdist ) { MegaFlowContrib con = new MegaFlowContrib(); con.src = fs; con.dist = dist; con.vel = (tan - np).normalized * fs.velocity; con.delta = delta.normalized; con.alpha = alpha; con.fdist = fdist; contrib.Add(con); } } } if ( contrib.Count > 0 ) { float tweight = 0.0f; for ( int c = 0; c < contrib.Count; c++ ) tweight += contrib[c].src.weight; Vector3 vel = cells[(x * gz * gy) + (z * gy) + y]; //Vector3.zero; for ( int c = 0; c < contrib.Count; c++ ) { float a = contrib[c].dist / contrib[c].fdist; float lerp = contrib[c].src.falloffcrv.Evaluate(a); float v = mod.velocity * contrib[c].src.velcrv.Evaluate(contrib[c].alpha); switch ( contrib[c].src.mode ) { case MegaFlowMode.Attract: vel += Vector3.Lerp(contrib[c].delta, contrib[c].vel, lerp) * v * (contrib[c].src.weight / tweight); break; case MegaFlowMode.Repulse: vel += Vector3.Lerp(-contrib[c].delta, contrib[c].vel, lerp) * v * (contrib[c].src.weight / tweight); break; case MegaFlowMode.Flow: vel += Vector3.Lerp(Vector3.zero, contrib[c].vel, lerp) * v * (contrib[c].src.weight / tweight); break; } } cells[(x * gz * gy) + (z * gy) + y] = vel; } else { Vector3 vl = cells[(x * gz * gy) + (z * gy) + y]; //Vector3.zero; switch ( mod.emtyspacemode ) { case MegaFlowMode.Attract: vl = (neardelta.normalized * mod.velocity) + mod.startval; break; case MegaFlowMode.Repulse: vl = (-neardelta.normalized * mod.velocity) + mod.startval; break; case MegaFlowMode.Flow: break; } cells[(x * gz * gy) + (z * gy) + y] = vl; } } } } EditorUtility.ClearProgressBar(); for ( int i = 0; i < mod.modifiers.Count; i++ ) { EditorUtility.DisplayProgressBar("Adding Modifiers", "Building", (float)i / (float)mod.modifiers.Count); MegaFlowModifier fmod = mod.modifiers[i]; if ( fmod.include && fmod.obj ) { Ray ray = new Ray(Vector3.zero, Vector3.zero); for ( int z = 0; z < gz; z++ ) { pos.z = (z * mod.cellsize.z) + half.z; for ( int y = 0; y < gy; y++ ) { pos.y = (y * mod.cellsize.y) + half.y; for ( int x = 0; x < gx; x++ ) { pos.x = (x * mod.cellsize.x) + half.x; Vector3 wpos = tm.MultiplyPoint3x4(pos); Vector3 origin = wpos; origin.y += 1000.0f; ray.origin = origin; ray.direction = Vector3.down; RaycastHit hit; if ( fmod.obj.Raycast(ray, out hit, 1000.0f) ) { ray.direction = Vector3.up; origin.y -= 2000.0f; ray.origin = origin; if ( fmod.obj.Raycast(ray, out hit, 1000.0f) ) { switch ( fmod.type ) { case MegaFlowModType.VelChange: Vector3 vel = cells[(x * gz * gy) + (z * gy) + y]; vel *= fmod.amount; cells[(x * gz * gy) + (z * gy) + y] = vel; break; } } } } } } } } EditorUtility.ClearProgressBar(); if ( flow ) { MegaFlowFrame newf = ScriptableObject.CreateInstance<MegaFlowFrame>(); newf.gridDim2[0] = gx; newf.gridDim2[1] = gy; newf.gridDim2[2] = gz; newf.size = mod.size; newf.gsize = newf.size; newf.spacing.x = newf.size.x / newf.gridDim2[0]; newf.spacing.y = newf.size.y / newf.gridDim2[1]; newf.spacing.z = newf.size.z / newf.gridDim2[2]; newf.oos.x = 1.0f / newf.spacing.x; newf.oos.y = 1.0f / newf.spacing.y; newf.oos.z = 1.0f / newf.spacing.z; newf.vel.AddRange(cells); flow.AddFrame(newf); } }