Ejemplo n.º 1
0
        /// <summary>
        /// Get number of components of glTF attribute type.
        /// </summary>
        /// <param name="type">glTF attribute type</param>
        /// <returns>Number of components</returns>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public static int GetAccessorAttributeTypeLength(GLTFAccessorAttributeType type)
        {
            switch (type)
            {
            case GLTFAccessorAttributeType.SCALAR:
                return(1);

            case GLTFAccessorAttributeType.VEC2:
                return(2);

            case GLTFAccessorAttributeType.VEC3:
                return(3);

            case GLTFAccessorAttributeType.VEC4:
            case GLTFAccessorAttributeType.MAT2:
                return(4);

            case GLTFAccessorAttributeType.MAT3:
                return(9);

            case GLTFAccessorAttributeType.MAT4:
                return(16);

            default:
                throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }
        }
Ejemplo n.º 2
0
        public static int GetAccessorAttriuteTypeLength(GLTFAccessorAttributeType type)
        {
            switch (type)
            {
            case GLTFAccessorAttributeType.SCALAR:
                return(1);

            case GLTFAccessorAttributeType.VEC2:
                return(2);

            case GLTFAccessorAttributeType.VEC3:
                return(3);

            case GLTFAccessorAttributeType.VEC4:
            case GLTFAccessorAttributeType.MAT2:
                return(4);

            case GLTFAccessorAttributeType.MAT3:
                return(9);

            case GLTFAccessorAttributeType.MAT4:
                return(16);

            default:
                Debug.LogError("Unknown GLTFAccessorAttributeType");
                return(0);
            }
        }
Ejemplo n.º 3
0
        public Accessor(Accessor accessor, GLTFRoot gltfRoot) : base(accessor, gltfRoot)
        {
            if (accessor == null)
            {
                return;
            }

            if (accessor.BufferView != null)
            {
                BufferView = new BufferViewId(accessor.BufferView, gltfRoot);
            }

            ByteOffset    = accessor.ByteOffset;
            ComponentType = accessor.ComponentType;
            Normalized    = accessor.Normalized;
            Count         = accessor.Count;
            Type          = accessor.Type;

            if (accessor.Max != null)
            {
                Max = accessor.Max.ToList();
            }

            if (accessor.Min != null)
            {
                Min = accessor.Min.ToList();
            }

            if (accessor.Sparse != null)
            {
                Sparse = new AccessorSparse(accessor.Sparse, gltfRoot);
            }
        }
Ejemplo n.º 4
0
        static Type GetAccessorTye(GLTFAccessorAttributeType accessorAttributeType)
        {
            switch (accessorAttributeType)
            {
            case GLTFAccessorAttributeType.SCALAR:
                return(typeof(float));

            case GLTFAccessorAttributeType.VEC2:
                return(typeof(Vector2));

            case GLTFAccessorAttributeType.VEC3:
                return(typeof(Vector3));

            case GLTFAccessorAttributeType.VEC4:
            case GLTFAccessorAttributeType.MAT2:
                return(typeof(Vector4));

            case GLTFAccessorAttributeType.MAT3:
                return(typeof(Matrix4x4));

            case GLTFAccessorAttributeType.MAT4:
            default:
                Debug.LogError("Unknown/Unsupported GLTFAccessorAttributeType");
                return(typeof(float));
            }
        }
Ejemplo n.º 5
0
        private static void TestAccessor(Accessor accessor, GLTFAccessorAttributeType type, int count, GLTFComponentType componentType, int bufferViewId, List <float> max, List <float> min)
        {
            Assert.AreEqual(type, accessor.Type);
            Assert.AreEqual(count, accessor.Count);
            Assert.AreEqual(componentType, accessor.ComponentType);
            Assert.AreEqual(bufferViewId, accessor.BufferView.Id);
            Assert.AreEqual(min.Count, accessor.Min.Count);
            Assert.AreEqual(max.Count, accessor.Max.Count);

            for (int i = 0; i < max.Count; ++i)
            {
                Assert.AreEqual(max[i], accessor.Max[i], .000001f);
            }

            for (int i = 0; i < min.Count; ++i)
            {
                Assert.AreEqual(min[i], accessor.Min[i], .000001f);
            }
        }
Ejemplo n.º 6
0
        public static Accessor PackToBufferFloatArray(
            MemoryStream stream, float[] data,
            GLTFAccessorAttributeType attributeType,
            GLTFComponentType componentType
            )
        {
            var accessor = new Accessor();

            accessor.ByteOffset    = (int)stream.Length;
            accessor.ComponentType = componentType;
            accessor.Type          = attributeType;

            int elementSize = attributeType == GLTFAccessorAttributeType.VEC2 ? 2 : attributeType == GLTFAccessorAttributeType.VEC3 ? 3 : attributeType == GLTFAccessorAttributeType.VEC4 ? 4 : 1;

            // no need to calc max and min for animation
            var bytes = GetBytes(data, componentType);

            stream.Write(bytes, 0, bytes.Length);
            accessor.Count = data.Length / elementSize;

            return(accessor);
        }
Ejemplo n.º 7
0
        private static byte[] GetDataToBuffer <DataType>(
            DataType value, GLTFComponentType componentType,
            ref double[] max, ref double[] min, ref GLTFAccessorAttributeType type
            )
        {
            float[] array    = null;
            int[]   intArray = null;
            int     size     = 0;

            /**
             * @todo: support int uint short byte ushort...
             */
            if (typeof(DataType) == typeof(float))
            {
                size = 1;
                type = GLTFAccessorAttributeType.SCALAR;
                var v = (float)Convert.ChangeType(value, typeof(float));
                array = new float[] { v };
            }
            else if (typeof(DataType) == typeof(int))
            {
                size = 1;
                type = GLTFAccessorAttributeType.SCALAR;
                var v = (int)Convert.ChangeType(value, typeof(int));
                intArray = new int[] { v };
            }
            else if (typeof(DataType) == typeof(Vector2))
            {
                size = 2;
                type = GLTFAccessorAttributeType.VEC2;
                var v = (Vector2)Convert.ChangeType(value, typeof(Vector2));
                array = new float[] { v.x, v.y };
            }
            else if (typeof(DataType) == typeof(Vector3))
            {
                size = 3;
                type = GLTFAccessorAttributeType.VEC3;
                var v = (Vector3)Convert.ChangeType(value, typeof(Vector3));
                array = new float[] { v.x, v.y, v.z };
            }
            else if (typeof(DataType) == typeof(Vector4))
            {
                size = 4;
                type = GLTFAccessorAttributeType.VEC4;
                var v = (Vector4)Convert.ChangeType(value, typeof(Vector4));
                array = new float[] { v.x, v.y, v.z, v.w };
            }
            else if (typeof(DataType) == typeof(Color))
            {
                size = 4;
                type = GLTFAccessorAttributeType.VEC4;
                var v = (Color)Convert.ChangeType(value, typeof(Color));
                array = new float[] { v.r, v.g, v.b, v.a };
            }
            else if (typeof(DataType) == typeof(BoneWeight))
            {
                size = 4;
                type = GLTFAccessorAttributeType.VEC4;
                var v = (BoneWeight)Convert.ChangeType(value, typeof(BoneWeight));
                intArray = new int[] { v.boneIndex0, v.boneIndex1, v.boneIndex2, v.boneIndex3 };
            }
            else if (typeof(DataType) == typeof(Matrix4x4))
            {
                size = 16;
                type = GLTFAccessorAttributeType.MAT4;
                var v = (Matrix4x4)Convert.ChangeType(value, typeof(Matrix4x4));
                array = new float[] {
                    v.m00, v.m10, v.m20, v.m30,
                    v.m01, v.m11, v.m21, v.m31,
                    v.m02, v.m12, v.m22, v.m32,
                    v.m03, v.m13, v.m23, v.m33
                };
            }
            else
            {
                Utils.ThrowExcption("Only support packing float, int, Vector2, Vector3, Vector4, BoneWeight, Matrix4 and Color now !");
            }

            if (max == null)
            {
                max = new double[size];
                min = new double[size];

                for (int i = 0; i < size; i += 1)
                {
                    var v = intArray == null ? array[i] : intArray[i];
                    max[i] = v;
                    min[i] = v;
                }
            }

            for (int i = 0; i < size; i += 1)
            {
                var v = intArray == null ? array[i] : intArray[i];
                max[i] = max[i] > v ? max[i] : v;
                min[i] = min[i] < v ? min[i] : v;
            }

            if (intArray != null)
            {
                return(GetBytes(intArray, componentType));
            }

            return(GetBytes(array, componentType));
        }
Ejemplo n.º 8
0
        unsafe JobHandle?GetColors32Job(
            void *input,
            GLTFComponentType inputType,
            GLTFAccessorAttributeType attributeType,
            int inputByteStride,
            NativeArray <float4> output
            )
        {
            Profiler.BeginSample("PrepareColors32");
            JobHandle?jobHandle = null;

            if (attributeType == GLTFAccessorAttributeType.VEC3)
            {
                switch (inputType)
                {
                case GLTFComponentType.UnsignedByte:
                {
                    var job = new Jobs.ConvertColorsRGBUInt8ToRGBAFloatJob {
                        input           = (byte *)input,
                        inputByteStride = inputByteStride > 0 ? inputByteStride : 3,
                        result          = output
                    };
                    jobHandle = job.Schedule(output.Length, GltfImport.DefaultBatchCount);
                }
                break;

                case GLTFComponentType.Float:
                {
                    var job = new Jobs.ConvertColorsRGBFloatToRGBAFloatJob {
                        input           = (byte *)input,
                        inputByteStride = inputByteStride > 0 ? inputByteStride : 12,
                        result          = (float4 *)output.GetUnsafePtr()
                    };
                    jobHandle = job.Schedule(output.Length, GltfImport.DefaultBatchCount);
                }
                break;

                case GLTFComponentType.UnsignedShort:
                {
                    var job = new Jobs.ConvertColorsRGBUInt16ToRGBAFloatJob {
                        input           = (System.UInt16 *)input,
                        inputByteStride = inputByteStride > 0 ? inputByteStride : 6,
                        result          = output
                    };
                    jobHandle = job.Schedule(output.Length, GltfImport.DefaultBatchCount);
                }
                break;

                default:
                    logger?.Error(LogCode.ColorFormatUnsupported, attributeType.ToString());
                    break;
                }
            }
            else if (attributeType == GLTFAccessorAttributeType.VEC4)
            {
                switch (inputType)
                {
                case GLTFComponentType.UnsignedByte:
                {
                    var job = new Jobs.ConvertColorsRGBAUInt8ToRGBAFloatJob {
                        input           = (byte *)input,
                        inputByteStride = inputByteStride > 0 ? inputByteStride : 4,
                        result          = output
                    };
                    jobHandle = job.Schedule(output.Length, GltfImport.DefaultBatchCount);
                }
                break;

                case GLTFComponentType.Float:
                {
                    if (inputByteStride == 16 || inputByteStride <= 0)
                    {
                        var job = new Jobs.MemCopyJob {
                            bufferSize = output.Length * 16,
                            input      = input,
                            result     = output.GetUnsafeReadOnlyPtr()
                        };
                        jobHandle = job.Schedule();
                    }
                    else
                    {
                        var job = new Jobs.ConvertColorsRGBAFloatToRGBAFloatJob {
                            input           = (byte *)input,
                            inputByteStride = inputByteStride,
                            result          = (float4 *)output.GetUnsafePtr()
                        };
#if UNITY_JOBS
                        jobHandle = job.ScheduleBatch(output.Length, GltfImport.DefaultBatchCount);
#else
                        jobHandle = job.Schedule(output.Length, GltfImport.DefaultBatchCount);
#endif
                    }
                }
                break;

                case GLTFComponentType.UnsignedShort:
                {
                    var job = new Jobs.ConvertColorsRGBAUInt16ToRGBAFloatJob {
                        input           = (System.UInt16 *)input,
                        inputByteStride = inputByteStride > 0 ? inputByteStride : 8,
                        result          = (float4 *)output.GetUnsafePtr()
                    };
#if UNITY_JOBS
                    jobHandle = job.ScheduleBatch(output.Length, GltfImport.DefaultBatchCount);
#else
                    jobHandle = job.Schedule(output.Length, GltfImport.DefaultBatchCount);
#endif
                }
                break;

                default:
                    logger?.Error(LogCode.ColorFormatUnsupported, attributeType.ToString());
                    break;
                }
            }
            else
            {
                logger?.Error(LogCode.TypeUnsupported, "color accessor", inputType.ToString());
            }
            Profiler.EndSample();
            return(jobHandle);
        }
Ejemplo n.º 9
0
        unsafe JobHandle?GetColors32Job(
            void *input,
            GLTFComponentType inputType,
            GLTFAccessorAttributeType attributeType,
            int inputByteStride,
            NativeArray <Color> output
            )
        {
            Profiler.BeginSample("PrepareColors32");
            JobHandle?jobHandle = null;

            if (attributeType == GLTFAccessorAttributeType.VEC3)
            {
                switch (inputType)
                {
                case GLTFComponentType.UnsignedByte:
                {
                    var job = new Jobs.GetColorsVec3UInt8Job {
                        input           = (byte *)input,
                        inputByteStride = inputByteStride > 0 ? inputByteStride : 3,
                        result          = output
                    };
                    jobHandle = job.Schedule(output.Length, GLTFast.DefaultBatchCount);
                }
                break;

                case GLTFComponentType.Float:
                {
                    var job = new Jobs.GetColorsVec3FloatJob {
                        input           = (float *)input,
                        inputByteStride = inputByteStride > 0 ? inputByteStride : 12,
                        result          = output
                    };
                    jobHandle = job.Schedule(output.Length, GLTFast.DefaultBatchCount);
                }
                break;

                case GLTFComponentType.UnsignedShort:
                {
                    var job = new Jobs.GetColorsVec3UInt16Job {
                        input           = (System.UInt16 *)input,
                        inputByteStride = inputByteStride > 0 ? inputByteStride : 6,
                        result          = output
                    };
                    jobHandle = job.Schedule(output.Length, GLTFast.DefaultBatchCount);
                }
                break;

                default:
                    Debug.LogErrorFormat(GLTFast.ErrorUnsupportedColorFormat, attributeType);
                    break;
                }
            }
            else if (attributeType == GLTFAccessorAttributeType.VEC4)
            {
                switch (inputType)
                {
                case GLTFComponentType.UnsignedByte:
                {
                    var job = new Jobs.GetColorsVec4UInt8Job {
                        input           = (byte *)input,
                        inputByteStride = inputByteStride > 0 ? inputByteStride : 4,
                        result          = output
                    };
                    jobHandle = job.Schedule(output.Length, GLTFast.DefaultBatchCount);
                }
                break;

                case GLTFComponentType.Float:
                {
                    var job = new Jobs.MemCopyJob();
                    job.bufferSize = output.Length * 16;
                    job.input      = input;
                    job.result     = NativeArrayUnsafeUtility.GetUnsafeReadOnlyPtr(output);
                    jobHandle      = job.Schedule();
                }
                break;

                case GLTFComponentType.UnsignedShort:
                {
                    var job = new Jobs.GetColorsVec4UInt16Job {
                        input           = (System.UInt16 *)input,
                        inputByteStride = inputByteStride > 0 ? inputByteStride : 8,
                        result          = output
                    };
                    jobHandle = job.Schedule(output.Length, GLTFast.DefaultBatchCount);
                }
                break;

                default:
                    Debug.LogErrorFormat(GLTFast.ErrorUnsupportedColorFormat, attributeType);
                    break;
                }
            }
            else
            {
                Debug.LogErrorFormat(GLTFast.ErrorUnsupportedType, "color accessor", inputType);
            }
            Profiler.EndSample();
            return(jobHandle);
        }