Пример #1
0
    private bool ParseObjLine(ref string objline)
    {
        linePart = objline.Trim().Split(linePartSplitChar);
        switch (linePart[0])
        {
        case O:
            //buffer.AddObject(linePart[1].Trim()); We skip object seperation, to reduce object count.
            //Importing large SketchupUp generated OBJ files results in an enormous amount of objects, making WebGL builds explode.
            break;

        case MTLLIB:
            mtllib = line.Substring(linePart[0].Length + 1).Trim();
            break;

        case USEMTL:
            buffer.AddSubMeshGroup(linePart[1].Trim());
            break;

        case V:
            buffer.PushVertex(new Vector3(cf(linePart[1]), cf(linePart[2]), cf(linePart[3])));
            break;

        case VT:
            buffer.PushUV(new Vector2(cf(linePart[1]), cf(linePart[2])));
            break;

        case VN:
            buffer.PushNormal(new Vector3(cf(linePart[1]), cf(linePart[2]), cf(linePart[3])));
            break;

        case F:
            var faces = new FaceIndices[linePart.Length - 1];
            GetFaceIndices(faces, linePart);
            if (linePart.Length == 4)
            {
                //tris
                buffer.PushFace(faces[0]);
                buffer.PushFace(faces[1]);
                buffer.PushFace(faces[2]);
            }
            else if (linePart.Length == 5)
            {
                //quad
                buffer.PushFace(faces[0]);
                buffer.PushFace(faces[1]);
                buffer.PushFace(faces[3]);
                buffer.PushFace(faces[3]);
                buffer.PushFace(faces[1]);
                buffer.PushFace(faces[2]);
            }
            else
            {
                Debug.LogWarning("face vertex count :" + (linePart.Length - 1) + " larger than 4. Ngons not supported.");
                return(false);                        //Return failure. Not triangulated.
            }
            break;
        }
        return(true);
    }
    private void SetGeometryData(string data)
    {
        string[] lines = data.Split("\n".ToCharArray());

        for (int i = 0; i < lines.Length; i++)
        {
            string l = lines[i];
            l = l.Replace("  ", " |").Replace("| ", "").Replace("|", "");
            if (l.IndexOf("#") != -1)
            {
                l = l.Substring(0, l.IndexOf("#"));
            }
            string[] p = l.Split(" ".ToCharArray());
            switch (p[0])
            {
            case O:
                //buffer.PushObject(p[1].Trim());
                break;

            case G:
                buffer.PushGroup(p[1].Trim());
                break;

            case V:
                buffer.PushVertex(new Vector3(cf(p[1]), cf(p[2]), cf(p[3])));
                break;

            case VT:
                buffer.PushUV(new Vector2(cf(p[1]), cf(p[2])));
                break;

            case VN:
                buffer.PushNormal(new Vector3(cf(p[1]), cf(p[2]), cf(p[3])));
                break;

            case F:
                for (int j = 1; j < p.Length; j++)
                {
                    string[]    c  = p[j].Trim().Split("/".ToCharArray());
                    FaceIndices fi = new FaceIndices();
                    if (c[0] != "")
                    {
                        fi.vi = ci(c[0]) - 1;
                    }
                    if (c.Length > 1 && c[1] != "")
                    {
                        fi.vu = ci(c[1]) - 1;
                    }
                    if (c.Length > 2 && c[2] != "")
                    {
                        fi.vn = ci(c[2]) - 1;
                    }
                    if (c[0] != "")
                    {
                        buffer.PushFace(fi);
                    }
                }
                break;

            case MTL:
                mtllib = p[1].Trim();
                break;

            case UML:
                buffer.PushMaterialName(p[1].Trim());
                break;
            }
        }

        //buffer.Trace();
    }
Пример #3
0
    private void SetGeometryData(string data)
    {
        string[] lines            = data.Split("\n".ToCharArray());
        Regex    regexWhitespaces = new Regex(@"\s+");
        bool     isFirstInGroup   = true;
        bool     isFaceIndexPlus  = true;

        for (int i = 0; i < lines.Length; i++)
        {
            string l = lines[i].Trim();

            if (l.IndexOf("#") != -1)              // comment line
            {
                continue;
            }
            string[] p = regexWhitespaces.Split(l);
            switch (p[0])
            {
            case O:
                buffer.PushObject(p[1].Trim());
                isFirstInGroup = true;
                break;

            case G:
                string groupName = null;
                if (p.Length >= 2)
                {
                    groupName = p[1].Trim();
                }
                isFirstInGroup = true;
                buffer.PushGroup(groupName);
                break;

            case V:
                buffer.PushVertex(new Vector3(cf(p[1]), cf(p[2]), cf(p[3])));
                break;

            case VT:
                buffer.PushUV(new Vector2(cf(p[1]), cf(p[2])));
                break;

            case VN:
                buffer.PushNormal(new Vector3(cf(p[1]), cf(p[2]), cf(p[3])));
                break;

            case F:
                FaceIndices[] faces = new FaceIndices[p.Length - 1];
                if (isFirstInGroup)
                {
                    isFirstInGroup = false;
                    string[] c = p[1].Trim().Split("/".ToCharArray());
                    isFaceIndexPlus = (ci(c[0]) >= 0);
                }
                GetFaceIndicesByOneFaceLine(faces, p, isFaceIndexPlus);
                if (p.Length == 4)
                {
                    buffer.PushFace(faces[0]);
                    buffer.PushFace(faces[1]);
                    buffer.PushFace(faces[2]);
                }
                else if (p.Length == 5)
                {
                    buffer.PushFace(faces[0]);
                    buffer.PushFace(faces[1]);
                    buffer.PushFace(faces[3]);
                    buffer.PushFace(faces[3]);
                    buffer.PushFace(faces[1]);
                    buffer.PushFace(faces[2]);
                }
                else
                {
                    //Debug.LogWarning("face vertex count :"+(p.Length-1)+" larger than 4:");
                }
                break;

            case MTL:
                mtllib = l.Substring(p[0].Length + 1).Trim();
                break;

            case UML:
                buffer.PushMaterialName(p[1].Trim());
                break;
            }
        }

        // buffer.Trace();
    }
Пример #4
0
        static private void ProcessOBJLine(GeometryBuffer gBuffer, string line)
        {
            string l      = line;
            int    iSchrp = l.IndexOf("#");

            if (iSchrp != -1)
            {
                l = l.Substring(0, iSchrp);
            }
            l = l.Trim().Replace("  ", " ");
            string[] p = l.Split(" ".ToCharArray());

            switch (p[0])
            {
            case O:
                gBuffer.PushObject(p[1].Trim());
                break;

            case G:
                gBuffer.PushGroup(p[1].Trim());
                break;

            case V:
                gBuffer.PushVertex(new Vector3(Cf(p[1]), Cf(p[2]), Cf(p[3])));
                break;

            case VT:
                gBuffer.PushUV(new Vector2(Cf(p[1]), Cf(p[2])));
                break;

            case VN:
                gBuffer.PushNormal(new Vector3(Cf(p[1]), Cf(p[2]), Cf(p[3])));
                break;

            case F:
                string[]    c;
                FaceIndices fi;
                if (p.Length - 1 == 3)
                {
                    //For Triangles
                    for (int j = 0; j < 3; j++)
                    {
                        c     = p[rightTrisWay[j]].Trim().Split("/".ToCharArray());
                        fi    = new FaceIndices();
                        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;
                        }
                        gBuffer.PushFace(fi);
                    }
                }
                else
                {
                    //For Quads
                    for (int j = 0; j < 6; j++)
                    {
                        c     = p[rightQuadsWay[j]].Trim().Split("/".ToCharArray());
                        fi    = new FaceIndices();
                        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;
                        }
                        gBuffer.PushFace(fi);
                    }
                }
                break;

            default:
                break;
            }
        }
Пример #5
0
    private void SetGeometryData(string data)
    {
        string[] lines = data.Split("\n".ToCharArray());

        for (int i = 0; i < lines.Length; i++)
        {
            string l = lines[i];

            if (l.IndexOf("#") != -1)
            {
                l = l.Substring(0, l.IndexOf("#"));
            }
            string[] p = l.Split(" ".ToCharArray());

            switch (p[0])
            {
            case O:
                buffer.PushObject(p[1].Trim());
                break;

            case G:
                buffer.PushGroup(p[1].Trim());
                break;

            case V:
                //Unity has a left-handed coordinates system while Molecular OBJs are right-handed
                //So we have to negate the X coordinates
                buffer.PushVertex(new Vector3(-cf(p[1]), cf(p[2]), cf(p[3])));
                compteurvertice++;
                break;

            case VT:
                buffer.PushUV(new Vector2(cf(p[1]), cf(p[2])));
                break;

            case VN:
                //Unity has a left-handed coordinates system while Molecular OBJs are right-handed
                //So we have to negate the X coordinates
                //Here it affects only light ! The winding reverse is in GeometryBuffer::PopulateMeshes
                Vector3 norm = new Vector3(-cf(p[1]), cf(p[2]), cf(p[3]));
                norm.Normalize();
                buffer.PushNormal(norm);
                break;

            case F:
                for (int j = 1; j < p.Length; j++)
                {
                    string[] c = p[j].Trim().Split("/".ToCharArray());
//						Debug.Log("" +p[j]);
                    FaceIndices fi = new FaceIndices();
                    fi.vi = ci(c[0]) - 1;
                    if (c.Length > 1 && c[1] != "")
                    {
                        fi.vu = ci(c[1]) - 1;
                    }
                    if (c.Length > 2 && c[2] != "")
                    {
                        fi.vn = ci(c[2]) - 1;
                    }
//						Debug.Log("vi "+fi.vi+" vu "+fi.vu+ " vn "+fi.vn);
                    buffer.PushFace(fi);
                }
                break;

            case MTL:
                if (mtl_reader != null)
                {
                    mtllib = p[1].Trim();
                }
                break;

            case UML:
                if (mtl_reader != null)
                {
                    buffer.PushMaterialName(p[1].Trim());
                }
                break;
            }
        }

        // buffer.Trace();
    }
Пример #6
0
    private IEnumerator SetGeometryData(string data)
    {
        yield return(0);        // play nice by not hogging the main thread

        string[] lines = data.Split("\n".ToCharArray());

        bool isFirstInGroup  = true;
        bool isFaceIndexPlus = true;

        for (int i = 0; i < lines.Length; i++)
        {
            string l = lines[i].Trim();

            if (l.Length > 0 && l[0] == '#')              // comment line
            {
                continue;
            }
            string[] p = l.Replace("  ", " ").Split(' ');

            switch (p[0])
            {
            case O:
                buffer.PushObject(p[1].Trim());
                isFirstInGroup = true;
                break;

            case G:
                string groupName = null;
                if (p.Length >= 2)
                {
                    groupName = p[1].Trim();
                }
                isFirstInGroup = true;
                buffer.PushGroup(groupName);
                break;

            case V:
                buffer.PushVertex(new Vector3(cf(p[1]), cf(p[2]), cf(p[3])));
                break;

            case VT:
                buffer.PushUV(new Vector2(cf(p[1]), cf(p[2])));
                break;

            case VN:
                buffer.PushNormal(new Vector3(cf(p[1]), cf(p[2]), cf(p[3])));
                break;

            case F:
                FaceIndices[] faces = new FaceIndices[p.Length - 1];
                if (isFirstInGroup)
                {
                    isFirstInGroup = false;
                    string[] c = p[1].Trim().Split("/".ToCharArray());
                    isFaceIndexPlus = (ci(c[0]) >= 0);
                }
                GetFaceIndicesByOneFaceLine(faces, p, isFaceIndexPlus);
                if (p.Length == 4)
                {
                    buffer.PushFace(faces[0]);
                    buffer.PushFace(faces[1]);
                    buffer.PushFace(faces[2]);
                }
                else if (p.Length == 5)
                {
                    buffer.PushFace(faces[0]);
                    buffer.PushFace(faces[1]);
                    buffer.PushFace(faces[3]);
                    buffer.PushFace(faces[3]);
                    buffer.PushFace(faces[1]);
                    buffer.PushFace(faces[2]);
                }
                else
                {
                    //Debug.LogWarning("face vertex count :"+(p.Length-1)+" larger than 4:");
                }
                break;

            case MTL:
                mtllib = l.Substring(p[0].Length + 1).Trim();
                break;

            case UML:
                buffer.PushMaterialName(p[1].Trim());
                break;
            }
            if (i % 7000 == 0)
            {
                yield return(0);                           // play nice with main thread while parsing large objs
            }
        }
        // buffer.Trace();
    }
Пример #7
0
        public static void SetGeometryData(string data, GeometryBuffer buffer)
        {
            var lines = data.Split("\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            for (var i = 0; i < lines.Length; i++)
            {
                var l = lines[i].Trim();

                if (l.IndexOf("#") != -1)
                    l = l.Substring(0, l.IndexOf("#"));
                var p = l.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                if (p.Length > 1)
                {
                    switch (p[0])
                    {
                        case O:
                            buffer.PushObject(p[1].Trim());
                            break;
                        case G:
                            buffer.PushGroup(p[1].Trim());
                            break;
                        case V:
                            buffer.PushVertex(new Vector3(
                                Cf(p[1]),
                                Cf(p[2]),
                                Cf(p[3]))
                                );
                            break;
                        case Vt:
                            buffer.PushUv(new Vector2(Cf(p[1]), Cf(p[2])));
                            break;
                        case Vn:
                            buffer.PushNormal(new Vector3(Cf(p[1]), Cf(p[2]), Cf(p[3])));
                            break;
                        case F:
                            for (var j = 1; j < p.Length; j++)
                            {
                                var c = p[j].Trim().Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                                var fi = new FaceIndices();
                                fi.Vi = Ci(c[0]) - 1;
                                if (c.Length > 1)
                                {
                                    fi.Vu = Ci(c[1]) - 1;
                                }
                                if (c.Length > 2)
                                {
                                    fi.Vn = Ci(c[2]) - 1;
                                }
                                buffer.PushFace(fi);
                            }
                            break;
                        case Mtl:
                            // mtllib = p[1].Trim();
                            break;
                        case Uml:
                            buffer.PushMaterialName(p[1].Trim());
                            break;
                    }
                }
            }
            // buffer.Trace();
        }
Пример #8
0
    ///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);
        }
    }