Example #1
0
        public Vertex[] GetVertices(VRTF vrtf, long offset, int count, float[] uvscales)
        {
            long   streamOffset = offset;
            Stream s            = new MemoryStream(mBuffer);

            s.Seek(streamOffset, SeekOrigin.Begin);

            VRTF.ElementLayout position = vrtf.Layouts
                                          .FirstOrDefault(x => x.Usage == VRTF.ElementUsage.Position);
            VRTF.ElementLayout normal = vrtf.Layouts
                                        .FirstOrDefault(x => x.Usage == VRTF.ElementUsage.Normal);
            VRTF.ElementLayout[] uv = vrtf.Layouts
                                      .Where(x => x.Usage == VRTF.ElementUsage.UV)
                                      .ToArray();
            VRTF.ElementLayout blendIndices = vrtf.Layouts
                                              .FirstOrDefault(x => x.Usage == VRTF.ElementUsage.BlendIndex);
            VRTF.ElementLayout blendWeights = vrtf.Layouts
                                              .FirstOrDefault(x => x.Usage == VRTF.ElementUsage.BlendWeight);
            VRTF.ElementLayout tangents = vrtf.Layouts
                                          .FirstOrDefault(x => x.Usage == VRTF.ElementUsage.Tangent);
            VRTF.ElementLayout color = vrtf.Layouts
                                       .FirstOrDefault(x => x.Usage == VRTF.ElementUsage.Colour);

            Vertex[] verts = new Vertex[count];
            if (uvscales == null)
            {
                uvscales = new float[3];
            }
            for (int i = 0; i < count; i++)
            {
                Vertex v    = new Vertex();
                byte[] data = new byte[vrtf.Stride];
                s.Read(data, 0, vrtf.Stride);
                if (position != null)
                {
                    float[] posPoints = new float[VRTF.FloatCountFromFormat(position.Format)];
                    ReadFloatData(data, position, ref posPoints);
                    v.Position = posPoints;
                }
                if (normal != null)
                {
                    float[] normPoints = new float[VRTF.FloatCountFromFormat(normal.Format)];
                    ReadFloatData(data, normal, ref normPoints);
                    v.Normal = normPoints;
                }
                v.UV = new float[uv.Length][];
                for (int j = 0; j < uv.Length; j++)
                {
                    VRTF.ElementLayout u        = uv[j];
                    float[]            uvPoints = new float[VRTF.FloatCountFromFormat(u.Format)];
                    float scale = j < uvscales.Length && uvscales[j] != 0 ? uvscales[j] : uvscales[0];
                    ReadUVData(data, u, ref uvPoints, scale);
                    v.UV[j] = uvPoints;
                }
                if (blendIndices != null)
                {
                    byte[] blendIPoints = new byte[VRTF.ByteSizeFromFormat(blendIndices.Format)];
                    Array.Copy(data, blendIndices.Offset, blendIPoints, 0, blendIPoints.Length);
                    v.BlendIndices = blendIPoints;
                }
                if (blendWeights != null)
                {
                    float[] blendWPoints = new float[VRTF.FloatCountFromFormat(blendWeights.Format)];
                    ReadFloatData(data, blendWeights, ref blendWPoints);
                    v.BlendWeights = blendWPoints;
                }
                if (tangents != null)
                {
                    float[] tangentPoints = new float[VRTF.FloatCountFromFormat(tangents.Format)];
                    ReadFloatData(data, tangents, ref tangentPoints);
                    v.Tangents = tangentPoints;
                }
                if (color != null)
                {
                    float[] colorPoints = new float[VRTF.FloatCountFromFormat(color.Format)];
                    ReadFloatData(data, color, ref colorPoints);
                    v.Color = colorPoints;
                }
                verts[i] = v;
            }
            return(verts);
        }