Ejemplo n.º 1
0
        unsafe void UnsafeRead(ReadOnlySpan <byte> all)
        {
            var _uint32 = sizeof(UInt32);

            var header = all.Slice(0, sizeof(sbyte) * 80);

            var triangles = all.Slice(sizeof(sbyte) * 80, sizeof(uint));

            var text = Utf8ReadOnlySpanHelper.GetString(header);

            if (!Utf8Parser.TryParse(triangles, out UInt32 value, out var _))
            {
                throw new Exception("Can't read float");
            }
        }
Ejemplo n.º 2
0
        unsafe void UnsafeRead(ReadOnlySpan <byte> all)
        {
            var groupname   = "noname";
            var current     = GeometryCache.CreatePart(groupname);
            var floats      = new float[3];
            var vertices    = new OBJVertex[4];
            var triangleFan = new OBJVertex[6];

            while (!all.IsEmpty)
            {
                var endLine         = all.IndexOf(LFChars);
                int separatorLenght = LFChars.Length;
                if (endLine == -1)
                {
                    endLine         = all.Length;
                    separatorLenght = 0;
                }

                if (endLine == 0)  //has only CR_LF, ignore then
                {
                    all = all.Slice(separatorLenght);
                    continue;
                }

                int cr = 0;
                if (all[endLine - 1] == CRChars)  //check 'CR LF'
                {
                    cr = 1;
                }
                var line = all.Slice(0, endLine - cr);//ignore CR_LF or LF

                all = all.Slice(endLine + separatorLenght);

                if (line.StartsWith(commentChars) || IsWhiteSpace(line))
                {
                    continue;
                }
                //lenth 2 is default for OBJ format keys, just use it as a const
                var part = line.Slice(2, line.Length - 2).Trim(space);
                if (line.StartsWith(groupChars))
                {
                    var names = Utf8ReadOnlySpanHelper.GetString(part).Trim().SplitOnWhitespace();
                    groupname = string.Join(" ", names);//clean up group name from extra space
                    current   = GeometryCache.CreatePart(groupname);
                }
                else if (line.StartsWith(textureChars))
                {
                    //vt u v w
                    // u is the value for the horizontal direction of the texture.
                    // v is an optional argument.
                    // v is the value for the vertical direction of the texture.The default is 0.
                    // w is an optional argument.
                    // w is a value for the depth of the texture.The default is 0.

                    SplitVertex(ref part, floats, 2);
                    var v = new Vector2(floats[0], 1 - floats[1]); // '1 - ' specific fot OBJ format
                    current.AddTextureCoor(ref v);
                }
                else if (line.StartsWith(normalChars))
                {
                    SplitVertex(ref part, floats);
                    var v = new Vector3(floats[0], floats[1], floats[2]);
                    current.AddNormal(ref v);
                }
                else if (line.StartsWith(vectorChars))
                {
                    SplitVertex(ref part, floats);
                    var v = new Vector3(floats[0], floats[1], floats[2]);
                    current.AddPosition(ref v);
                    if (!part.IsEmpty)  //format 'v' line with colors
                                        //example: v -2.503583 6.779097 -5.350025 0.0 128.0 0.0
                    {
                        SplitVertex(ref part, floats);
                        v = new Vector3(floats[0], floats[1], floats[2]);
                        current.AddColor(ref v);
                    }
                    if (!part.IsEmpty)
                    {
                        throw new NotSupportedException("Unexpected vertex format.");
                    }
                }
                else if (line.StartsWith(faceChars))
                {
                    var topo = SplitFace(part, vertices);
                    current.AddVertices(vertices, topo);
                }
                else if (line.StartsWith(materialChars))
                {
                    //mtllib filename.mat
                    var end      = line.IndexOf(space);
                    var filename = line.Slice(end + 1, line.Length - end - 1);
                    LoadMaterial(filename);
                }
            }
        }