Exemplo n.º 1
0
        public float[] AsFloatArray(ref NumericArray contents, byte[] bufferData)
        {
            if (contents.AsFloats != null)
            {
                return(contents.AsFloats);
            }

            if (Type != GLTFAccessorAttributeType.SCALAR)
            {
                return(null);
            }

            var arr             = new float[Count];
            var totalByteOffset = BufferView.Value.ByteOffset + ByteOffset;

            int   componentSize;
            float maxValue;
            Func <byte[], int, int>   getDiscreteElement;
            Func <byte[], int, float> getContinuousElement;

            GetTypeDetails(ComponentType, out componentSize, out maxValue, out getDiscreteElement, out getContinuousElement);

            var stride = BufferView.Value.ByteStride > 0 ? BufferView.Value.ByteStride : componentSize;

            for (var idx = 0; idx < Count; idx++)
            {
                arr[idx] = getContinuousElement(bufferData, totalByteOffset + idx * stride);
            }

            contents.AsFloats = arr;
            return(arr);
        }
Exemplo n.º 2
0
        public Color[] AsColorArray(ref NumericArray contents, byte[] bufferData)
        {
            if (contents.AsColors != null)
            {
                return(contents.AsColors);
            }

            if (Type != GLTFAccessorAttributeType.VEC3 && Type != GLTFAccessorAttributeType.VEC4)
            {
                return(null);
            }

            var arr             = new Color[Count];
            var totalByteOffset = BufferView.Value.ByteOffset + ByteOffset;

            int   componentSize;
            float maxValue;
            Func <byte[], int, int>   getDiscreteElement;
            Func <byte[], int, float> getContinuousElement;

            GetTypeDetails(ComponentType, out componentSize, out maxValue, out getDiscreteElement, out getContinuousElement);

            var stride = BufferView.Value.ByteStride > 0 ? BufferView.Value.ByteStride : componentSize * (Type == GLTFAccessorAttributeType.VEC3 ? 3 : 4);

            for (var idx = 0; idx < Count; idx++)
            {
                if (ComponentType == GLTFComponentType.Float)
                {
                    arr[idx].R = getContinuousElement(bufferData, totalByteOffset + idx * stride + componentSize * 0);
                    arr[idx].G = getContinuousElement(bufferData, totalByteOffset + idx * stride + componentSize * 1);
                    arr[idx].B = getContinuousElement(bufferData, totalByteOffset + idx * stride + componentSize * 2);
                    if (Type == GLTFAccessorAttributeType.VEC4)
                    {
                        arr[idx].A = getContinuousElement(bufferData, totalByteOffset + idx * stride + componentSize * 3);
                    }
                    else
                    {
                        arr[idx].A = 1;
                    }
                }
                else
                {
                    arr[idx].R = getDiscreteElement(bufferData, totalByteOffset + idx * stride + componentSize * 0) / maxValue;
                    arr[idx].G = getDiscreteElement(bufferData, totalByteOffset + idx * stride + componentSize * 1) / maxValue;
                    arr[idx].B = getDiscreteElement(bufferData, totalByteOffset + idx * stride + componentSize * 2) / maxValue;
                    if (Type == GLTFAccessorAttributeType.VEC4)
                    {
                        arr[idx].A = getDiscreteElement(bufferData, totalByteOffset + idx * stride + componentSize * 3) / maxValue;
                    }
                    else
                    {
                        arr[idx].A = 1;
                    }
                }
            }

            contents.AsColors = arr;
            return(arr);
        }
Exemplo n.º 3
0
        public Matrix4x4[] AsMatrixArray(ref NumericArray contents, byte[] bufferData)
        {
            if (contents.AsMatrix4x4s != null)
            {
                return(contents.AsMatrix4x4s);
            }

            if (Type != GLTFAccessorAttributeType.MAT4)
            {
                return(null);
            }

            var arr             = new Matrix4x4[Count];
            var totalByteOffset = BufferView.Value.ByteOffset + ByteOffset;

            int   componentSize;
            float maxValue;
            Func <byte[], int, int>   getDiscreteElement;
            Func <byte[], int, float> getContinuousElement;

            GetTypeDetails(ComponentType, out componentSize, out maxValue, out getDiscreteElement, out getContinuousElement);

            var stride = BufferView.Value.ByteStride > 0 ? BufferView.Value.ByteStride : componentSize * 16;

            for (var i = 0; i < Count; i++)
            {
                int startElement = totalByteOffset + i * stride;
                if (ComponentType == GLTFComponentType.Float)
                {
                    arr[i] = new Matrix4x4(
                        getContinuousElement(bufferData, startElement + componentSize * 0),
                        getContinuousElement(bufferData, startElement + componentSize * 4),
                        getContinuousElement(bufferData, startElement + componentSize * 8),
                        getContinuousElement(bufferData, startElement + componentSize * 12),

                        getContinuousElement(bufferData, startElement + componentSize * 1),
                        getContinuousElement(bufferData, startElement + componentSize * 5),
                        getContinuousElement(bufferData, startElement + componentSize * 9),
                        getContinuousElement(bufferData, startElement + componentSize * 13),

                        getContinuousElement(bufferData, startElement + componentSize * 2),
                        getContinuousElement(bufferData, startElement + componentSize * 6),
                        getContinuousElement(bufferData, startElement + componentSize * 10),
                        getContinuousElement(bufferData, startElement + componentSize * 14),

                        getContinuousElement(bufferData, startElement + componentSize * 3),
                        getContinuousElement(bufferData, startElement + componentSize * 7),
                        getContinuousElement(bufferData, startElement + componentSize * 11),
                        getContinuousElement(bufferData, startElement + componentSize * 15)
                        );
                }
            }

            contents.AsMatrix4x4s = arr;
            return(arr);
        }
Exemplo n.º 4
0
        public uint[] AsTriangles(ref NumericArray contents, byte[] bufferViewData, int offset)
        {
            if (contents.AsTriangles != null)
            {
                return(contents.AsTriangles);
            }

            contents.AsTriangles = AsUIntArray(ref contents, bufferViewData, offset);

            return(contents.AsTriangles);
        }
Exemplo n.º 5
0
        public Vector4[] AsTangentArray(ref NumericArray contents, byte[] bufferViewData, int offset)
        {
            if (contents.AsTangents != null)
            {
                return(contents.AsTangents);
            }

            contents.AsTangents = AsVector4Array(ref contents, bufferViewData, offset);

            return(contents.AsTangents);
        }
Exemplo n.º 6
0
        public Vector3[] AsNormalArray(ref NumericArray contents, byte[] bufferViewData, int offset)
        {
            if (contents.AsNormals != null)
            {
                return(contents.AsNormals);
            }

            contents.AsNormals = AsVector3Array(ref contents, bufferViewData, offset);

            return(contents.AsNormals);
        }
Exemplo n.º 7
0
        public Vector2[] AsTexcoordArray(ref NumericArray contents, byte[] bufferViewData, int offset)
        {
            if (contents.AsTexcoords != null)
            {
                return(contents.AsTexcoords);
            }

            contents.AsTexcoords = AsVector2Array(ref contents, bufferViewData, offset);

            return(contents.AsTexcoords);
        }
Exemplo n.º 8
0
        public Vector3[] AsVertexArray(ref NumericArray contents, byte[] bufferViewData, uint offset)
        {
            if (contents.AsVertices != null)
            {
                return(contents.AsVertices);
            }

            contents.AsVertices = AsVector3Array(ref contents, bufferViewData, offset);

            return(contents.AsVertices);
        }
Exemplo n.º 9
0
        public Vector4[] AsVector4Array(ref NumericArray contents, byte[] bufferViewData, int offset, bool normalizeIntValues = true)
        {
            if (contents.AsVec4s != null)
            {
                return(contents.AsVec4s);
            }

            if (Type != GLTFAccessorAttributeType.VEC4)
            {
                return(null);
            }

            if (ComponentType == GLTFComponentType.UnsignedInt)
            {
                return(null);
            }

            var arr             = new Vector4[Count];
            var totalByteOffset = ByteOffset + offset;

            int   componentSize;
            float maxValue;

            GetTypeDetails(ComponentType, out componentSize, out maxValue);

            var stride = BufferView.Value.ByteStride > 0 ? BufferView.Value.ByteStride : componentSize * 4;

            if (normalizeIntValues)
            {
                maxValue = 1;
            }

            for (var idx = 0; idx < Count; idx++)
            {
                if (ComponentType == GLTFComponentType.Float)
                {
                    arr[idx].X = GetFloatElement(bufferViewData, totalByteOffset + idx * stride + componentSize * 0);
                    arr[idx].Y = GetFloatElement(bufferViewData, totalByteOffset + idx * stride + componentSize * 1);
                    arr[idx].Z = GetFloatElement(bufferViewData, totalByteOffset + idx * stride + componentSize * 2);
                    arr[idx].W = GetFloatElement(bufferViewData, totalByteOffset + idx * stride + componentSize * 3);
                }
                else
                {
                    arr[idx].X = GetDiscreteElement(bufferViewData, totalByteOffset + idx * stride + componentSize * 0, ComponentType) / maxValue;
                    arr[idx].Y = GetDiscreteElement(bufferViewData, totalByteOffset + idx * stride + componentSize * 1, ComponentType) / maxValue;
                    arr[idx].Z = GetDiscreteElement(bufferViewData, totalByteOffset + idx * stride + componentSize * 2, ComponentType) / maxValue;
                    arr[idx].W = GetDiscreteElement(bufferViewData, totalByteOffset + idx * stride + componentSize * 3, ComponentType) / maxValue;
                }
            }

            contents.AsVec4s = arr;
            return(arr);
        }
Exemplo n.º 10
0
        public Matrix4x4[] AsMatrix4x4Array(ref NumericArray contents, byte[] bufferViewData, uint offset, bool normalizeIntValues = true)
        {
            if (contents.AsMatrix4x4s != null)
            {
                return(contents.AsMatrix4x4s);
            }

            if (Type != GLTFAccessorAttributeType.MAT4)
            {
                return(null);
            }

            Matrix4x4[] arr             = new Matrix4x4[Count];
            uint        totalByteOffset = ByteOffset + offset;

            uint  componentSize;
            float maxValue;

            GetTypeDetails(ComponentType, out componentSize, out maxValue);

            if (normalizeIntValues)
            {
                maxValue = 1;
            }

            uint stride = (uint)(BufferView.Value.ByteStride > 0 ? BufferView.Value.ByteStride : componentSize * 16);

            for (uint idx = 0; idx < Count; idx++)
            {
                arr[idx] = Matrix4x4.identity;

                if (ComponentType == GLTFComponentType.Float)
                {
                    for (uint i = 0; i < 16; i++)
                    {
                        float value = GetFloatElement(bufferViewData, totalByteOffset + idx * stride + componentSize * i);
                        arr[idx][(int)i] = value;
                    }
                }
                else
                {
                    for (uint i = 0; i < 16; i++)
                    {
                        float value = GetDiscreteElement(bufferViewData, totalByteOffset + idx * stride + componentSize * i, ComponentType) / maxValue;
                        arr[idx][(int)i] = value;
                    }
                }
            }

            contents.AsMatrix4x4s = arr;
            return(arr);
        }
Exemplo n.º 11
0
        public Vector3[] AsVector3Array(ref NumericArray contents, byte[] bufferData, bool normalizeIntValues = true)
        {
            if (contents.AsVec3s != null)
            {
                return(contents.AsVec3s);
            }

            if (Type != GLTFAccessorAttributeType.VEC3)
            {
                return(null);
            }

            var arr             = new Vector3[Count];
            var totalByteOffset = BufferView.Value.ByteOffset + ByteOffset;

            int   componentSize;
            float maxValue;
            Func <byte[], int, int>   getDiscreteElement;
            Func <byte[], int, float> getContinuousElement;

            GetTypeDetails(ComponentType, out componentSize, out maxValue, out getDiscreteElement, out getContinuousElement);

            var stride = BufferView.Value.ByteStride > 0 ? BufferView.Value.ByteStride : componentSize * 3;

            if (normalizeIntValues)
            {
                maxValue = 1;
            }

            for (var idx = 0; idx < Count; idx++)
            {
                if (ComponentType == GLTFComponentType.Float)
                {
                    arr[idx].X = getContinuousElement(bufferData, totalByteOffset + idx * stride + componentSize * 0);
                    arr[idx].Y = getContinuousElement(bufferData, totalByteOffset + idx * stride + componentSize * 1);
                    arr[idx].Z = getContinuousElement(bufferData, totalByteOffset + idx * stride + componentSize * 2);
                }
                else
                {
                    arr[idx].X = getDiscreteElement(bufferData, totalByteOffset + idx * stride + componentSize * 0) / maxValue;
                    arr[idx].Y = getDiscreteElement(bufferData, totalByteOffset + idx * stride + componentSize * 1) / maxValue;
                    arr[idx].Z = getDiscreteElement(bufferData, totalByteOffset + idx * stride + componentSize * 2) / maxValue;
                }
            }

            contents.AsVec3s = arr;
            return(arr);
        }
Exemplo n.º 12
0
        public Vector4[] AsTangentArray(ref NumericArray contents, byte[] bufferViewData, int offset)
        {
            if (contents.AsTangents != null)
            {
                return(contents.AsTangents);
            }

            var arr = AsVector4Array(ref contents, bufferViewData, offset);

            for (var i = 0; i < arr.Length; i++)
            {
                arr[i].W *= -1;
            }

            contents.AsTangents = arr;

            return(arr);
        }
Exemplo n.º 13
0
        public Vector3[] AsVertexArray(ref NumericArray contents, byte[] bufferViewData, int offset)
        {
            if (contents.AsVertices != null)
            {
                return(contents.AsVertices);
            }

            var arr = AsVector3Array(ref contents, bufferViewData, offset);

            for (var i = 0; i < arr.Length; i++)
            {
                arr[i].Z *= -1;
            }

            contents.AsVertices = arr;

            return(arr);
        }
Exemplo n.º 14
0
        public Vector2[] AsTexcoordArray(ref NumericArray contents, byte[] bufferViewData, int offset)
        {
            if (contents.AsTexcoords != null)
            {
                return(contents.AsTexcoords);
            }

            var arr = AsVector2Array(ref contents, bufferViewData, offset);

            for (var i = 0; i < arr.Length; i++)
            {
                arr[i].Y = 1 - arr[i].Y;
            }

            contents.AsTexcoords = arr;

            return(arr);
        }
Exemplo n.º 15
0
        public Vector2[] AsTexcoordArray(ref NumericArray contents, byte[] bufferData)
        {
            if (contents.AsTexcoords != null)
            {
                return(contents.AsTexcoords);
            }

            var arr = AsVector2Array(ref contents, bufferData);

            for (var i = 0; i < arr.Length; i++)
            {
                arr[i].Y *= -1;
            }

            contents.AsTexcoords = arr;
            contents.AsVec2s     = null;

            return(arr);
        }
Exemplo n.º 16
0
        public Vector3[] AsNormalArray(ref NumericArray contents, byte[] bufferData)
        {
            if (contents.AsNormals != null)
            {
                return(contents.AsNormals);
            }

            var arr = AsVector3Array(ref contents, bufferData);

            for (var i = 0; i < arr.Length; i++)
            {
                arr[i].Z *= -1;
            }

            contents.AsNormals = arr;
            contents.AsVec3s   = null;

            return(arr);
        }
Exemplo n.º 17
0
        public uint[] AsTriangles(ref NumericArray contents, byte[] bufferViewData, int offset)
        {
            if (contents.AsTriangles != null)
            {
                return(contents.AsTriangles);
            }

            var arr = AsUIntArray(ref contents, bufferViewData, offset);

            for (var i = 0; i < arr.Length; i += 3)
            {
                var temp = arr[i];
                arr[i]     = arr[i + 2];
                arr[i + 2] = temp;
            }

            contents.AsTriangles = arr;

            return(arr);
        }
Exemplo n.º 18
0
        public int[] AsTriangles(ref NumericArray contents, byte[] bufferData)
        {
            if (contents.AsTriangles != null)
            {
                return(contents.AsTriangles);
            }

            var arr = AsIntArray(ref contents, bufferData);

            for (var i = 0; i < arr.Length; i += 3)
            {
                var temp = arr[i];
                arr[i]     = arr[i + 2];
                arr[i + 2] = temp;
            }

            contents.AsTriangles = arr;
            contents.AsInts      = null;

            return(arr);
        }
Exemplo n.º 19
0
        public uint[] AsUIntArray(ref NumericArray contents, byte[] bufferViewData, uint offset)
        {
            if (contents.AsUInts != null)
            {
                return(contents.AsUInts);
            }

            if (Type != GLTFAccessorAttributeType.SCALAR)
            {
                return(null);
            }

            var arr             = new uint[Count];
            var totalByteOffset = ByteOffset + offset;

            uint  componentSize;
            float maxValue;

            GetTypeDetails(ComponentType, out componentSize, out maxValue);

            uint stride = BufferView.Value.ByteStride > 0 ? BufferView.Value.ByteStride : componentSize;

            var GetUnsignedDiscreteElement = GetUnsignedFuncForType(ComponentType);

            for (uint idx = 0; idx < Count; idx++)
            {
                if (ComponentType == GLTFComponentType.Float)
                {
                    arr[idx] = (uint)System.Math.Floor(GetFloatElement(bufferViewData, totalByteOffset + idx * stride));
                }
                else
                {
                    arr[idx] = GetUnsignedDiscreteElement(bufferViewData, totalByteOffset + idx * stride);
                }
            }

            contents.AsUInts = arr;
            return(arr);
        }
Exemplo n.º 20
0
        public Color[] AsColorArray(ref NumericArray contents, byte[] bufferViewData, int offset)
        {
            if (contents.AsColors != null)
            {
                return(contents.AsColors);
            }

            if (Type != GLTFAccessorAttributeType.VEC3 && Type != GLTFAccessorAttributeType.VEC4)
            {
                return(null);
            }

            if (ComponentType == GLTFComponentType.UnsignedInt)
            {
                return(null);
            }

            var arr             = new Color[Count];
            var totalByteOffset = ByteOffset + offset;

            int   componentSize;
            float maxValue;

            GetTypeDetails(ComponentType, out componentSize, out maxValue);

            var stride = BufferView.Value.ByteStride > 0 ? BufferView.Value.ByteStride : componentSize * (Type == GLTFAccessorAttributeType.VEC3 ? 3 : 4);

            for (var idx = 0; idx < Count; idx++)
            {
                if (ComponentType == GLTFComponentType.Float)
                {
                    arr[idx].R = GetFloatElement(bufferViewData, totalByteOffset + idx * stride + componentSize * 0);
                    arr[idx].G = GetFloatElement(bufferViewData, totalByteOffset + idx * stride + componentSize * 1);
                    arr[idx].B = GetFloatElement(bufferViewData, totalByteOffset + idx * stride + componentSize * 2);
                    if (Type == GLTFAccessorAttributeType.VEC4)
                    {
                        arr[idx].A = GetFloatElement(bufferViewData, totalByteOffset + idx * stride + componentSize * 3);
                    }
                    else
                    {
                        arr[idx].A = 1;
                    }
                }
                else
                {
                    // todo: can be optimized to get these in a block...
                    arr[idx].R = GetDiscreteElement(bufferViewData, totalByteOffset + idx * stride + componentSize * 0, ComponentType) / maxValue;
                    arr[idx].G = GetDiscreteElement(bufferViewData, totalByteOffset + idx * stride + componentSize * 1, ComponentType) / maxValue;
                    arr[idx].B = GetDiscreteElement(bufferViewData, totalByteOffset + idx * stride + componentSize * 2, ComponentType) / maxValue;
                    if (Type == GLTFAccessorAttributeType.VEC4)
                    {
                        arr[idx].A = GetDiscreteElement(bufferViewData, totalByteOffset + idx * stride + componentSize * 3, ComponentType) / maxValue;
                    }
                    else
                    {
                        arr[idx].A = 1;
                    }
                }
            }

            contents.AsColors = arr;
            return(arr);
        }