private void CalculateScaleOffset(scaleoffset so, bool above) { so.MasterOffsetx = 0.0f - ((so.maxvxpos / 2.0f) + (so.minvxpos / 2.0f)); so.MasterOffsety = 0.0f - ((so.maxvypos / 2.0f) + (so.minvypos / 2.0f)); so.MasterOffsetz = 0.0f - ((so.maxvzpos / 2.0f) + (so.minvzpos / 2.0f)); //if (above) { //UPrestingonzero // so.MainOffset=new Vector3(so.MasterOffsetx,so.MasterOffsety+((so.maxvypos-so.minvypos)/2.0f),so.MasterOffsetz); //} else { //UpCentred so.MainOffset = new Vector3(so.MasterOffsetx, so.MasterOffsety, so.MasterOffsetz); //} so.minvpos = so.minvypos < so.minvxpos?so.minvypos:so.minvxpos; so.minvpos = so.minvzpos < so.minvpos?so.minvzpos:so.minvpos; so.maxvpos = so.maxvypos > so.maxvxpos?so.maxvypos:so.maxvxpos; so.maxvpos = so.maxvzpos > so.maxvpos?so.maxvzpos:so.maxvpos; float ep = 0.0F; ep = (0 - so.minvpos) > so.maxvpos?(0 - so.minvpos):so.maxvpos; /*if (ep!=0.0f) { * if (ep>objmaxsize) so.MasterScale=objmaxsize/ep; * if (ep<objminsize) so.MasterScale=objminsize/ep; * } else { * so.MasterScale=1.0f; * }*/ so.MasterScale = MasterScale; }
//Set ScaleOffset data for object or master group private void pushscaleoffset(scaleoffset so, Vector3 v) { if (!so.initval) { so.minvxpos = v.x; so.maxvxpos = v.x; so.minvypos = v.y; so.maxvypos = v.y; so.minvzpos = v.z; so.maxvzpos = v.z; so.initval = true; } else { so.minvxpos = v.x < so.minvxpos?v.x:so.minvxpos; so.maxvxpos = v.x > so.maxvxpos?v.x:so.maxvxpos; so.minvypos = v.y < so.minvypos?v.y:so.minvypos; so.maxvypos = v.y > so.maxvypos?v.y:so.maxvypos; so.minvzpos = v.z < so.minvzpos?v.z:so.minvzpos; so.maxvzpos = v.z > so.maxvzpos?v.z:so.maxvzpos; } }
///Assemble private void Build() { Dictionary <string, Material> materials = new Dictionary <string, Material>(); if (hasMaterials) { Material m = new Material(Shader.Find("Diffuse")); m.SetColor("_Color", new Color(0.5f, 0.5f, 0.5f, 1.0f)); materials.Add("_spc_default", m); foreach (MaterialData md in materialData) { if (!materials.ContainsKey(md.name)) { materials.Add(md.name, GetMaterial(md)); } } } else { Material m = new Material(Shader.Find("Diffuse")); m.SetColor("_Color", new Color(0.5f, 0.5f, 0.5f, 1.0f)); materials.Add("_spc_default", m); } GameObject[] ms = new GameObject[buffer.numObjects]; if (buffer.numObjects == 1) { if (!treatasoneobject) //incase of one geometry but instantiated { GameObject go = new GameObject(); go.transform.parent = gameObject.transform; go.AddComponent(typeof(MeshFilter)); go.AddComponent(typeof(MeshRenderer)); go.name = "_spc_rename_"; ms[0] = go; } else { gameObject.AddComponent(typeof(MeshFilter)); gameObject.AddComponent(typeof(MeshRenderer)); ms[0] = gameObject; } } else if (buffer.numObjects > 1) { for (int i = 0; i < buffer.numObjects; i++) { GameObject go = new GameObject(); go.transform.parent = gameObject.transform; go.AddComponent(typeof(MeshFilter)); go.AddComponent(typeof(MeshRenderer)); ms[i] = go; } } buffer.PopulateMeshes(ms, materials); if (!treatasoneobject) { foreach (GameObject go in ms) { scaleoffset so = objso.Find(delegate(scaleoffset item) { return(item.name == go.name); }); if (so != null) { go.transform.localPosition = ((Vector3.zero - so.MainOffset) * mainso.MasterScale) - ((Vector3.zero - mainso.MainOffset) * mainso.MasterScale); } } } }
///Geometry private void SetGeometryData(string data) { data = data.Replace("\r\n", "\n"); string[] lines = data.Split("\n".ToCharArray()); Vector3 v; mainso = new scaleoffset(); for (int i = 0; i < lines.Length; i++) { string l = lines[i]; l = Regex.Replace(l, @"# object", "o"); //tomekkie ALTERATION if (l.IndexOf("#") != -1) { l = l.Substring(0, l.IndexOf("#")); } l = Regex.Replace(l, @"\s+", " "); l = l.Trim(); string[] p = l.Split(" ".ToCharArray()); switch (p[0]) { case O: if (!EnforceSingleObj) { if (currentso != null) { currentso.vlast = vcount; objso.Add(currentso); } buffer.PushObject(p[1].Trim()); currentso = new scaleoffset(); currentso.vfirst = vcount; currentso.name = p[1].Trim(); } break; case G: buffer.PushGroup(p[1].Trim()); break; case V: if (p.Length >= 3) { v = new Vector3(cf(p[1]), cf(p[2]), 0 - cf(p[3])); buffer.PushVertex(v); //Any 0- Flipping should match normals vcount++; pushscaleoffset(mainso, v); if (currentso != null) { pushscaleoffset(currentso, v); } } break; case VT: if (p.Length >= 2) { buffer.PushUV(new Vector2(cf(p[1]), cf(p[2]))); } break; case VN: if (p.Length >= 3) { buffer.PushNormal(new Vector3(cf(p[1]), cf(p[2]), 0 - cf(p[3]))); //Any 0- Flipping should match vertex } break; case F: if (p.Length >= 4) { if (p.Length <= 5) //is triangle or quad { string[] c; for (int j = 0; j < p.Length - 3; j++) //get all possible triangles from line { FaceIndices fi = new FaceIndices(); //1 vert c = p[1].Trim().Split("/".ToCharArray()); if (c.Length > 0 && c[0] != string.Empty) { fi.vi = ci(c[0]) - 1; } if (c.Length > 1 && c[1] != string.Empty) { fi.vu = ci(c[1]) - 1; } if (c.Length > 2 && c[2] != string.Empty) { fi.vn = ci(c[2]) - 1; } buffer.PushFace(fi); for (int k = 0; k < 2; k++) //2nd and 3rd vert { fi = new FaceIndices(); int no = 3 - k + j; // To invert faces replace with : int no=2+k+j; c = p[no].Trim().Split("/".ToCharArray()); if (c.Length > 0 && c[0] != string.Empty) { fi.vi = ci(c[0]) - 1; } if (c.Length > 1 && c[1] != string.Empty) { fi.vu = ci(c[1]) - 1; } if (c.Length > 2 && c[2] != string.Empty) { fi.vn = ci(c[2]) - 1; } buffer.PushFace(fi); } } } else //is poly try triangulate, see TriPoly script { TriPoly triangulation; triangulation = new TriPoly(); Vector3[] pointlist = new Vector3[p.Length - 1]; //Vector3[] normallist=new Vector3[p.Length-1]; string[] c; for (int j = 1; j < p.Length; j++) //go through each faceindex in poly list and pull relevant vertice from geometrybuffer add to vector[] { c = p[j].Trim().Split("/".ToCharArray()); if (c.Length > 0 && c[0] != string.Empty) { pointlist[j - 1] = buffer.vertices[ci(c[0]) - 1]; } //if (c.Length > 2 && c[2] != string.Empty) {normallist[j-1] = buffer.normals[ci(c[2])-1];} } int[] indices; //if (normallist!=null) { // indices=triangulation.Patch(pointlist, normallist); //} else { indices = triangulation.Patch(pointlist); //, normallist //} if (indices.Length > 2) { for (int j = 0; j < indices.Length; ++j) //may need to reverse this? { FaceIndices fi = new FaceIndices(); c = p[indices[j] + 1].Trim().Split("/".ToCharArray()); if (c.Length > 0 && c[0] != string.Empty) { fi.vi = ci(c[0]) - 1; } if (c.Length > 1 && c[1] != string.Empty) { fi.vu = ci(c[1]) - 1; } if (c.Length > 2 && c[2] != string.Empty) { fi.vn = ci(c[2]) - 1; } buffer.PushFace(fi); } } } } break; case MTL: mtllib = p[1].Trim(); break; case UML: buffer.PushMaterialGroup(p[1].Trim()); //hello break; } } if (currentso != null) { currentso.vlast = vcount; objso.Add(currentso); } }