Пример #1
0
        private static int GetDiscreteElement(byte[] bufferViewData, int offset, GLTFComponentType type)
        {
            switch (type)
            {
            case GLTFComponentType.Byte:
            {
                return(GetByteElement(bufferViewData, offset));
            }

            case GLTFComponentType.UnsignedByte:
            {
                return(GetUByteElement(bufferViewData, offset));
            }

            case GLTFComponentType.Short:
            {
                return(GetShortElement(bufferViewData, offset));
            }

            case GLTFComponentType.UnsignedShort:
            {
                return(GetUShortElement(bufferViewData, offset));
            }

            default:
            {
                throw new Exception("Unsupported type passed in: " + type);
            }
            }
        }
Пример #2
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);
            }
        }
Пример #3
0
        private static Func <byte[], uint, uint> GetUnsignedFuncForType(GLTFComponentType type)
        {
            switch (type)
            {
            case GLTFComponentType.Byte:
            {
                return(GetByteElementAsUInt);
            }

            case GLTFComponentType.UnsignedByte:
            {
                return(GetUByteElementAsUInt);
            }

            case GLTFComponentType.Short:
            {
                return(GetShortElementAsUInt);
            }

            case GLTFComponentType.UnsignedShort:
            {
                return(GetUShortElementAsUInt);
            }

            case GLTFComponentType.UnsignedInt:
            {
                return(GetUIntElement);
            }

            default:
            {
                throw new Exception("Unsupported type passed in: " + type);
            }
            }
        }
Пример #4
0
        static Type GetAccessorComponentType(GLTFComponentType componentType)
        {
            switch (componentType)
            {
            case GLTFComponentType.Byte:
                return(typeof(byte));

            case GLTFComponentType.Float:
                return(typeof(float));

            case GLTFComponentType.Short:
                return(typeof(System.Int16));

            case GLTFComponentType.UnsignedByte:
                return(typeof(byte));

            case GLTFComponentType.UnsignedInt:
                return(typeof(int));

            case GLTFComponentType.UnsignedShort:
                return(typeof(System.UInt16));

            default:
                Debug.LogError("Unknown GLTFComponentType");
                return(null);
            }
        }
        public static Accessor PackToBuffer <DataType>(
            byte[] buffer, DataType[] data,
            GLTFComponentType componentType,
            int offset, int stride,
            Func <DataType[], int, DataType> getValueByIndex = null
            )
        {
            var accessor = new Accessor();

            accessor.ByteOffset    = offset;
            accessor.ComponentType = componentType;

            double[] max = null;
            double[] min = null;

            for (int i = 0; i < data.Length; i += 1)
            {
                var bytes      = GetDataToBuffer(getValueByIndex == null ? data[i] : getValueByIndex(data, i), componentType, ref max, ref min, ref accessor.Type);
                int byteOffset = offset + stride * i;

                bytes.CopyTo(buffer, byteOffset);
            }

            accessor.Count = data.Length;
            accessor.Max   = max.ToList();
            accessor.Min   = min.ToList();

            return(accessor);
        }
Пример #6
0
        public static unsafe JobHandle?GetVector3sSparseJob(
            void *indexBuffer,
            void *valueBuffer,
            int sparseCount,
            GLTFComponentType indexType,
            GLTFComponentType valueType,
            float3 *output,
            int outputByteStride,
            ref JobHandle?dependsOn,
            bool normalized = false
            )
        {
            JobHandle?jobHandle;

            Profiler.BeginSample("GetVector3sSparseJob");
            var job = new ConvertVector3SparseJob {
                indexBuffer      = (ushort *)indexBuffer,
                indexConverter   = CachedFunction.GetIndexConverter(indexType),
                inputByteStride  = 3 * Accessor.GetComponentTypeSize(valueType),
                input            = valueBuffer,
                valueConverter   = CachedFunction.GetPositionConverter(valueType, normalized),
                outputByteStride = outputByteStride,
                result           = output,
            };

            jobHandle = job.Schedule(
                sparseCount,
                GltfImport.DefaultBatchCount,
                dependsOn: dependsOn.HasValue ? dependsOn.Value : default(JobHandle)
                );
            Profiler.EndSample();
            return(jobHandle);
        }
        public static Accessor PackToBuffer <DataType>(
            MemoryStream stream, DataType[] data,
            GLTFComponentType componentType,
            Func <DataType[], int, DataType> getValueByIndex = null
            )
        {
            var accessor = new Accessor();

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

            double[] max = null;
            double[] min = null;

            for (int i = 0; i < data.Length; i += 1)
            {
                var bytes = GetDataToBuffer(getValueByIndex == null ? data[i] : getValueByIndex(data, i), componentType, ref max, ref min, ref accessor.Type);

                stream.Write(bytes, 0, bytes.Length);
            }

            accessor.Count = data.Length;
            accessor.Max   = max.ToList();
            accessor.Min   = min.ToList();

            return(accessor);
        }
        public AccessorSparseIndices(AccessorSparseIndices accessorSparseIndices, GLTFRoot gltfRoot) : base(accessorSparseIndices)
        {
            if (accessorSparseIndices == null)
            {
                return;
            }

            BufferView    = new BufferViewId(accessorSparseIndices.BufferView, gltfRoot);
            ByteOffset    = accessorSparseIndices.ByteOffset;
            ComponentType = accessorSparseIndices.ComponentType;
        }
Пример #9
0
        protected unsafe JobHandle?GetTangentsJob(
            void *input,
            int count,
            GLTFComponentType inputType,
            int inputByteStride,
            Vector4 *output,
            int outputByteStride,
            bool normalized = false
            )
        {
            Profiler.BeginSample("GetTangentsJob");
            JobHandle?jobHandle;

            switch (inputType)
            {
            case GLTFComponentType.Float:
                var jobTangentI = new Jobs.GetTangentsInterleavedJob();
                jobTangentI.inputByteStride  = inputByteStride > 0 ? inputByteStride : 16;
                jobTangentI.input            = (byte *)input;
                jobTangentI.outputByteStride = outputByteStride;
                jobTangentI.result           = output;
                jobHandle = jobTangentI.Schedule(count, GLTFast.DefaultBatchCount);
                break;

            case GLTFComponentType.Short:
                var jobTangent = new Jobs.GetTangentsInt16NormalizedInterleavedJob();
                jobTangent.inputByteStride = inputByteStride > 0 ? inputByteStride : 8;;
                Assert.IsTrue(normalized);
                jobTangent.input            = (System.Int16 *)input;
                jobTangent.outputByteStride = outputByteStride;
                jobTangent.result           = output;
                jobHandle = jobTangent.Schedule(count, GLTFast.DefaultBatchCount);
                break;

            case GLTFComponentType.Byte:
                var jobTangentByte = new Jobs.GetVector4sInt8NormalizedInterleavedJob();
                jobTangentByte.inputByteStride = inputByteStride > 0 ? inputByteStride : 4;
                Assert.IsTrue(normalized);
                jobTangentByte.input            = (sbyte *)input;
                jobTangentByte.outputByteStride = outputByteStride;
                jobTangentByte.result           = output;
                jobHandle = jobTangentByte.Schedule(count, GLTFast.DefaultBatchCount);
                break;

            default:
                Debug.LogErrorFormat(GLTFast.ErrorUnsupportedType, "Tangent", inputType);
                jobHandle = null;
                break;
            }

            Profiler.EndSample();
            return(jobHandle);
        }
Пример #10
0
        private static void GetTypeDetails(GLTFComponentType type, out int componentSize, out float maxValue,
                                           out Func <byte[], int, int> discreteFunc, out Func <byte[], int, float> continuousFunc)
        {
            componentSize  = 1;
            maxValue       = byte.MaxValue;
            discreteFunc   = GetUByteElement;
            continuousFunc = GetFloatElement;

            switch (type)
            {
            case GLTFComponentType.Byte:
                discreteFunc  = GetByteElement;
                componentSize = sizeof(sbyte);
                maxValue      = sbyte.MaxValue;
                break;

            case GLTFComponentType.UnsignedByte:
                discreteFunc  = GetUByteElement;
                componentSize = sizeof(byte);
                maxValue      = byte.MaxValue;
                break;

            case GLTFComponentType.Short:
                discreteFunc  = GetShortElement;
                componentSize = sizeof(short);
                maxValue      = short.MaxValue;
                break;

            case GLTFComponentType.UnsignedShort:
                discreteFunc  = GetUShortElement;
                componentSize = sizeof(ushort);
                maxValue      = ushort.MaxValue;
                break;

            case GLTFComponentType.UnsignedInt:
                discreteFunc  = GetUIntElement;
                componentSize = sizeof(uint);
                maxValue      = uint.MaxValue;
                break;

            case GLTFComponentType.Float:
                continuousFunc = GetFloatElement;
                componentSize  = sizeof(float);
                maxValue       = float.MaxValue;
                break;

            default:
                throw new Exception("Unsupported component type.");
            }
        }
Пример #11
0
        private static void GetTypeDetails(
            GLTFComponentType type,
            out int componentSize,
            out float maxValue)
        {
            componentSize = 1;
            maxValue      = byte.MaxValue;

            switch (type)
            {
            case GLTFComponentType.Byte:
                componentSize = sizeof(sbyte);
                maxValue      = sbyte.MaxValue;
                break;

            case GLTFComponentType.UnsignedByte:
                componentSize = sizeof(byte);
                maxValue      = byte.MaxValue;
                break;

            case GLTFComponentType.Short:
                componentSize = sizeof(short);
                maxValue      = short.MaxValue;
                break;

            case GLTFComponentType.UnsignedShort:
                componentSize = sizeof(ushort);
                maxValue      = ushort.MaxValue;
                break;

            case GLTFComponentType.UnsignedInt:
                componentSize = sizeof(uint);
                maxValue      = uint.MaxValue;
                break;

            case GLTFComponentType.Float:
                componentSize = sizeof(float);
                maxValue      = float.MaxValue;
                break;

            default:
                throw new Exception("Unsupported component type.");
            }
        }
Пример #12
0
        static unsafe JobHandle?GetJointsJob(
            void *input,
            int count,
            GLTFComponentType inputType,
            int inputByteStride,
            uint4 *output,
            int outputByteStride,
            ICodeLogger logger
            )
        {
            Profiler.BeginSample("GetJointsJob");
            JobHandle?jobHandle;

            switch (inputType)
            {
            case GLTFComponentType.UnsignedByte:
                var jointsUInt8Job = new Jobs.ConvertBoneJointsUInt8ToUInt32Job();
                jointsUInt8Job.inputByteStride  = inputByteStride > 0 ? inputByteStride : 4;
                jointsUInt8Job.input            = (byte *)input;
                jointsUInt8Job.outputByteStride = outputByteStride;
                jointsUInt8Job.result           = output;
                jobHandle = jointsUInt8Job.Schedule(count, GltfImport.DefaultBatchCount);
                break;

            case GLTFComponentType.UnsignedShort:
                var jointsUInt16Job = new Jobs.ConvertBoneJointsUInt16ToUInt32Job();
                jointsUInt16Job.inputByteStride  = inputByteStride > 0 ? inputByteStride : 8;
                jointsUInt16Job.input            = (byte *)input;
                jointsUInt16Job.outputByteStride = outputByteStride;
                jointsUInt16Job.result           = output;
                jobHandle = jointsUInt16Job.Schedule(count, GltfImport.DefaultBatchCount);
                break;

            default:
                logger?.Error(LogCode.TypeUnsupported, "Joints", inputType.ToString());
                jobHandle = null;
                break;
            }

            Profiler.EndSample();
            return(jobHandle);
        }
Пример #13
0
        /// <summary>
        /// Provides size of components by type
        /// </summary>
        /// <param name="componentType">glTF component type</param>
        /// <returns>Component size in bytes</returns>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public static int GetComponentTypeSize(GLTFComponentType componentType)
        {
            switch (componentType)
            {
            case GLTFComponentType.Byte:
            case GLTFComponentType.UnsignedByte:
                return(1);

            case GLTFComponentType.Short:
            case GLTFComponentType.UnsignedShort:
                return(2);

            case GLTFComponentType.Float:
            case GLTFComponentType.UnsignedInt:
                return(4);

            default:
                throw new ArgumentOutOfRangeException(nameof(type), componentType, null);
            }
        }
Пример #14
0
        static unsafe JobHandle?GetJointsJob(
            void *input,
            int count,
            GLTFComponentType inputType,
            int inputByteStride,
            uint *output,
            int outputByteStride
            )
        {
            Profiler.BeginSample("GetJointsJob");
            JobHandle?jobHandle;

            switch (inputType)
            {
            case GLTFComponentType.UnsignedByte:
                var jointsUInt8Job = new Jobs.GetJointsUInt8Job();
                jointsUInt8Job.inputByteStride  = inputByteStride > 0 ? inputByteStride : 4;
                jointsUInt8Job.input            = (byte *)input;
                jointsUInt8Job.outputByteStride = outputByteStride;
                jointsUInt8Job.result           = output;
                jobHandle = jointsUInt8Job.Schedule(count, GLTFast.DefaultBatchCount);
                break;

            case GLTFComponentType.UnsignedShort:
                var jointsUInt16Job = new Jobs.GetJointsUInt16Job();
                jointsUInt16Job.inputByteStride  = inputByteStride > 0 ? inputByteStride : 8;
                jointsUInt16Job.input            = (byte *)input;
                jointsUInt16Job.outputByteStride = outputByteStride;
                jointsUInt16Job.result           = output;
                jobHandle = jointsUInt16Job.Schedule(count, GLTFast.DefaultBatchCount);
                break;

            default:
                Debug.LogErrorFormat(GLTFast.ErrorUnsupportedType, "Joints", inputType);
                jobHandle = null;
                break;
            }

            Profiler.EndSample();
            return(jobHandle);
        }
Пример #15
0
        IEnumerable <int> GetIndices(GLTFBufferView view, int count, int byteOffset, GLTFComponentType componentType)
        {
            switch (componentType)
            {
            case GLTFComponentType.UNSIGNED_BYTE:
            {
                return(GetAttrib <Byte>(count, byteOffset, view).Select(x => (int)(x)));
            }

            case GLTFComponentType.UNSIGNED_SHORT:
            {
                return(GetAttrib <UInt16>(count, byteOffset, view).Select(x => (int)(x)));
            }

            case GLTFComponentType.UNSIGNED_INT:
            {
                return(GetAttrib <UInt32>(count, byteOffset, view).Select(x => (int)(x)));
            }
            }
            throw new NotImplementedException("GetIndices: unknown componenttype: " + componentType);
        }
Пример #16
0
        public static int GetAccessorComponentTypeLength(GLTFComponentType componentType)
        {
            switch (componentType)
            {
            case GLTFComponentType.Byte:
            case GLTFComponentType.UnsignedByte:
                return(1);

            case GLTFComponentType.Short:
            case GLTFComponentType.UnsignedShort:
                return(2);

            case GLTFComponentType.Float:
            case GLTFComponentType.UnsignedInt:
                return(4);

            default:
                Debug.LogError("Unknown GLTFComponentType");
                return(0);
            }
        }
Пример #17
0
        protected unsafe JobHandle?GetJointsJob(
            void *input,
            int count,
            GLTFComponentType inputType,
            int inputByteStride,
            uint *output,
            int outputByteStride,
            bool normalized = false
            )
        {
            Profiler.BeginSample("GetJointsJob");
            JobHandle?jobHandle;

            switch (inputType)
            {
            // TODO: Complete
            // case GLTFComponentType.Float:
            //     break;
            case GLTFComponentType.UnsignedShort:
                var jobTangent = new Jobs.GetJointsUInt16Job();
                jobTangent.inputByteStride = inputByteStride > 0 ? inputByteStride : 8;
                // Assert.IsTrue(normalized);
                jobTangent.input            = (byte *)input;
                jobTangent.outputByteStride = outputByteStride;
                jobTangent.result           = output;
                jobHandle = jobTangent.Schedule(count, GLTFast.DefaultBatchCount);
                break;

            // TODO: Complete
            // case GLTFComponentType.UnsignedByte:
            //     break;
            default:
                Debug.LogErrorFormat(GLTFast.ErrorUnsupportedType, "Tangent", inputType);
                jobHandle = null;
                break;
            }

            Profiler.EndSample();
            return(jobHandle);
        }
Пример #18
0
        private static byte[] GetBytes(int[] array, GLTFComponentType componentType)
        {
            int   size   = 0;
            Array dArray = null;

            switch (componentType)
            {
            case GLTFComponentType.Byte:
            case GLTFComponentType.UnsignedByte:
                size   = 1;
                dArray = Array.ConvertAll(array, item => (byte)item);
                break;

            case GLTFComponentType.Short:
                size   = 2;
                dArray = Array.ConvertAll(array, item => (short)item);
                break;

            case GLTFComponentType.UnsignedShort:
                size   = 2;
                dArray = Array.ConvertAll(array, item => (ushort)item);
                break;

            case GLTFComponentType.Float:
                size   = 4;
                dArray = array;
                break;

            case GLTFComponentType.UnsignedInt:
                size   = 4;
                dArray = Array.ConvertAll(array, item => (uint)item);
                break;
            }

            var bytes = new byte[size * array.Length];

            System.Buffer.BlockCopy(dArray, 0, bytes, 0, bytes.Length);

            return(bytes);
        }
Пример #19
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);
        }
Пример #20
0
        protected unsafe JobHandle?GetWeightsJob(
            void *input,
            int count,
            GLTFComponentType inputType,
            int inputByteStride,
            Vector4 *output,
            int outputByteStride,
            bool normalized = false
            )
        {
            Profiler.BeginSample("GetWeightsJob");
            JobHandle?jobHandle;

            switch (inputType)
            {
            case GLTFComponentType.Float:
                var jobTangentI = new Jobs.GetVector4sInterleavedJob();
                jobTangentI.inputByteStride  = inputByteStride > 0 ? inputByteStride : 16;
                jobTangentI.input            = (byte *)input;
                jobTangentI.outputByteStride = outputByteStride;
                jobTangentI.result           = output;
                jobHandle = jobTangentI.Schedule(count, GltfImport.DefaultBatchCount);
                break;

            // TODO: Complete those cases
            // case GLTFComponentType.UnsignedShort:
            //     break;
            // case GLTFComponentType.UnsignedByte:
            //     break;
            default:
                logger?.Error(LogCode.TypeUnsupported, "Weights", inputType.ToString());
                jobHandle = null;
                break;
            }

            Profiler.EndSample();
            return(jobHandle);
        }
Пример #21
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);
        }
Пример #22
0
        unsafe JobHandle?GetUvsJob(
            void *input,
            int count,
            GLTFComponentType inputType,
            int inputByteStride,
            float2 *output,
            int outputByteStride,
            bool normalized = false
            )
        {
            Profiler.BeginSample("PrepareUVs");
            JobHandle?jobHandle = null;

            switch (inputType)
            {
            case GLTFComponentType.Float:
            {
                var jobUv = new Jobs.ConvertUVsFloatToFloatInterleavedJob {
                    inputByteStride  = (inputByteStride > 0) ? inputByteStride : 8,
                    input            = (byte *)input,
                    outputByteStride = outputByteStride,
                    result           = output
                };
#if UNITY_JOBS
                jobHandle = jobUv.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                jobHandle = jobUv.Schedule(count, GltfImport.DefaultBatchCount);
#endif
            }
            break;

            case GLTFComponentType.UnsignedByte:
                if (normalized)
                {
                    var jobUv = new Jobs.ConvertUVsUInt8ToFloatInterleavedNormalizedJob {
                        inputByteStride  = (inputByteStride > 0) ? inputByteStride : 2,
                        input            = (byte *)input,
                        outputByteStride = outputByteStride,
                        result           = output
                    };
                    jobHandle = jobUv.Schedule(count, GltfImport.DefaultBatchCount);
                }
                else
                {
                    var jobUv = new Jobs.ConvertUVsUInt8ToFloatInterleavedJob {
                        inputByteStride  = (inputByteStride > 0) ? inputByteStride : 2,
                        input            = (byte *)input,
                        outputByteStride = outputByteStride,
                        result           = output
                    };
#if UNITY_JOBS
                    jobHandle = jobUv.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                    jobHandle = jobUv.Schedule(count, GltfImport.DefaultBatchCount);
#endif
                }
                break;

            case GLTFComponentType.UnsignedShort:
                if (normalized)
                {
                    var jobUv = new Jobs.ConvertUVsUInt16ToFloatInterleavedNormalizedJob {
                        inputByteStride  = (inputByteStride > 0) ? inputByteStride : 4,
                        input            = (byte *)input,
                        outputByteStride = outputByteStride,
                        result           = output
                    };
                    jobHandle = jobUv.Schedule(count, GltfImport.DefaultBatchCount);
                }
                else
                {
                    var jobUv = new Jobs.ConvertUVsUInt16ToFloatInterleavedJob {
                        inputByteStride  = (inputByteStride > 0) ? inputByteStride : 4,
                        input            = (byte *)input,
                        outputByteStride = outputByteStride,
                        result           = output
                    };
#if UNITY_JOBS
                    jobHandle = jobUv.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                    jobHandle = jobUv.Schedule(count, GltfImport.DefaultBatchCount);
#endif
                }
                break;

            case GLTFComponentType.Short:
                if (normalized)
                {
                    var job = new Jobs.ConvertUVsInt16ToFloatInterleavedNormalizedJob {
                        inputByteStride  = inputByteStride > 0 ? inputByteStride : 4,
                        input            = (System.Int16 *)input,
                        outputByteStride = outputByteStride,
                        result           = output
                    };
#if UNITY_JOBS
                    jobHandle = job.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                    jobHandle = job.Schedule(count, GltfImport.DefaultBatchCount);
#endif
                }
                else
                {
                    var job = new Jobs.ConvertUVsInt16ToFloatInterleavedJob {
                        inputByteStride  = inputByteStride > 0 ? inputByteStride : 4,
                        input            = (System.Int16 *)input,
                        outputByteStride = outputByteStride,
                        result           = output
                    };
#if UNITY_JOBS
                    jobHandle = job.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                    jobHandle = job.Schedule(count, GltfImport.DefaultBatchCount);
#endif
                }
                break;

            case GLTFComponentType.Byte:
                var byteStride = inputByteStride > 0 ? inputByteStride : 2;
                if (normalized)
                {
                    var jobInt8 = new Jobs.ConvertUVsInt8ToFloatInterleavedNormalizedJob {
                        inputByteStride  = inputByteStride > 0 ? inputByteStride : 2,
                        input            = (sbyte *)input,
                        outputByteStride = outputByteStride,
                        result           = output
                    };
#if UNITY_JOBS
                    jobHandle = jobInt8.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                    jobHandle = jobInt8.Schedule(count, GltfImport.DefaultBatchCount);
#endif
                }
                else
                {
                    var jobInt8 = new Jobs.ConvertUVsInt8ToFloatInterleavedJob {
                        inputByteStride  = inputByteStride > 0 ? inputByteStride : 2,
                        input            = (sbyte *)input,
                        outputByteStride = outputByteStride,
                        result           = output
                    };
#if UNITY_JOBS
                    jobHandle = jobInt8.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                    jobHandle = jobInt8.Schedule(count, GltfImport.DefaultBatchCount);
#endif
                }
                break;

            default:
                logger?.Error(LogCode.TypeUnsupported, "UV", inputType.ToString());
                break;
            }
            Profiler.EndSample();
            return(jobHandle);
        }
Пример #23
0
        protected unsafe JobHandle?GetVector3sJob(
            void *input,
            int count,
            GLTFComponentType inputType,
            int inputByteStride,
            Vector3 *output,
            int outputByteStride,
            bool normalized = false
            )
        {
            JobHandle?jobHandle;

            Profiler.BeginSample("GetVector3sJob");
            if (inputType == GLTFComponentType.Float)
            {
                var job = new Jobs.GetVector3sInterleavedJob();
                job.inputByteStride  = (inputByteStride > 0) ? inputByteStride : 12;
                job.input            = (byte *)input;
                job.outputByteStride = outputByteStride;
                job.result           = output;
                jobHandle            = job.Schedule(count, GLTFast.DefaultBatchCount);
            }
            else
            if (inputType == GLTFComponentType.UnsignedShort)
            {
                if (normalized)
                {
                    var job = new Jobs.GetUInt16PositionsInterleavedNormalizedJob();
                    job.inputByteStride  = (inputByteStride > 0) ? inputByteStride : 6;
                    job.input            = (byte *)input;
                    job.outputByteStride = outputByteStride;
                    job.result           = output;
                    jobHandle            = job.Schedule(count, GLTFast.DefaultBatchCount);
                }
                else
                {
                    var job = new Jobs.GetUInt16PositionsInterleavedJob();
                    job.inputByteStride  = (inputByteStride > 0) ? inputByteStride : 6;
                    job.input            = (byte *)input;
                    job.outputByteStride = outputByteStride;
                    job.result           = output;
                    jobHandle            = job.Schedule(count, GLTFast.DefaultBatchCount);
                }
            }
            else
            if (inputType == GLTFComponentType.Short)
            {
                // TODO: test. did not have test files
                if (normalized)
                {
                    var job = new Jobs.GetVector3FromInt16InterleavedNormalizedJob();
                    job.inputByteStride  = (inputByteStride > 0) ? inputByteStride : 6;
                    job.input            = (byte *)input;
                    job.outputByteStride = outputByteStride;
                    job.result           = output;
                    jobHandle            = job.Schedule(count, GLTFast.DefaultBatchCount);
                }
                else
                {
                    var job = new Jobs.GetVector3FromInt16InterleavedJob();
                    job.inputByteStride  = (inputByteStride > 0) ? inputByteStride : 6;
                    job.input            = (byte *)input;
                    job.outputByteStride = outputByteStride;
                    job.result           = output;
                    jobHandle            = job.Schedule(count, GLTFast.DefaultBatchCount);
                }
            }
            else
            if (inputType == GLTFComponentType.Byte)
            {
                // TODO: test positions. did not have test files
                if (normalized)
                {
                    var job = new Jobs.GetVector3FromSByteInterleavedNormalizedJob();
                    job.Setup((inputByteStride > 0) ? inputByteStride : 3, (sbyte *)input, outputByteStride, output);
                    jobHandle = job.Schedule(count, GLTFast.DefaultBatchCount);
                }
                else
                {
                    var job = new Jobs.GetVector3FromSByteInterleavedJob();
                    job.Setup((inputByteStride > 0) ? inputByteStride : 3, (sbyte *)input, outputByteStride, output);
                    jobHandle = job.Schedule(count, GLTFast.DefaultBatchCount);
                }
            }
            else
            if (inputType == GLTFComponentType.UnsignedByte)
            {
                // TODO: test. did not have test files
                if (normalized)
                {
                    var job = new Jobs.GetVector3FromByteInterleavedNormalizedJob();
                    job.Setup((inputByteStride > 0) ? inputByteStride : 3, (byte *)input, outputByteStride, output);
                    jobHandle = job.Schedule(count, GLTFast.DefaultBatchCount);
                }
                else
                {
                    var job = new Jobs.GetVector3FromByteInterleavedJob();
                    job.Setup((inputByteStride > 0) ? inputByteStride : 3, (byte *)input, outputByteStride, output);
                    jobHandle = job.Schedule(count, GLTFast.DefaultBatchCount);
                }
            }
            else
            {
                Debug.LogError("Unknown componentType");
                jobHandle = null;
            }
            Profiler.EndSample();
            return(jobHandle);
        }
Пример #24
0
        /// <summary>
        /// Schedules a job that converts input data into float3 arrays.
        /// </summary>
        /// <param name="input">Points at the input data in memory</param>
        /// <param name="count">Attribute quantity</param>
        /// <param name="inputType">Input data type</param>
        /// <param name="inputByteStride">Input byte stride</param>
        /// <param name="output">Points at the destination buffer in memory</param>
        /// <param name="outputByteStride">Ouput byte stride</param>
        /// <param name="normalized">If true, integer values have to be normalized</param>
        /// <param name="ensureUnitLength">If true, normalized values will be scaled to have unit length again (only if <see cref="normalized"/>is true)</param>
        /// <returns></returns>
        public static unsafe JobHandle?GetVector3sJob(
            void *input,
            int count,
            GLTFComponentType inputType,
            int inputByteStride,
            float3 *output,
            int outputByteStride,
            bool normalized       = false,
            bool ensureUnitLength = true
            )
        {
            JobHandle?jobHandle;

            Profiler.BeginSample("GetVector3sJob");
            if (inputType == GLTFComponentType.Float)
            {
                var job = new ConvertVector3FloatToFloatInterleavedJob {
                    inputByteStride  = (inputByteStride > 0) ? inputByteStride : 12,
                    input            = (byte *)input,
                    outputByteStride = outputByteStride,
                    result           = output
                };
#if UNITY_JOBS
                jobHandle = job.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                jobHandle = job.Schedule(count, GltfImport.DefaultBatchCount);
#endif
            }
            else
            if (inputType == GLTFComponentType.UnsignedShort)
            {
                if (normalized)
                {
                    var job = new ConvertPositionsUInt16ToFloatInterleavedNormalizedJob {
                        inputByteStride  = (inputByteStride > 0) ? inputByteStride : 6,
                        input            = (byte *)input,
                        outputByteStride = outputByteStride,
                        result           = output
                    };
#if UNITY_JOBS
                    jobHandle = job.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                    jobHandle = job.Schedule(count, GltfImport.DefaultBatchCount);
#endif
                }
                else
                {
                    var job = new ConvertPositionsUInt16ToFloatInterleavedJob {
                        inputByteStride  = (inputByteStride > 0) ? inputByteStride : 6,
                        input            = (byte *)input,
                        outputByteStride = outputByteStride,
                        result           = output
                    };
#if UNITY_JOBS
                    jobHandle = job.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                    jobHandle = job.Schedule(count, GltfImport.DefaultBatchCount);
#endif
                }
            }
            else
            if (inputType == GLTFComponentType.Short)
            {
                if (normalized)
                {
                    if (ensureUnitLength)
                    {
                        // TODO: test. did not have test files
                        var job = new ConvertNormalsInt16ToFloatInterleavedNormalizedJob {
                            inputByteStride  = (inputByteStride > 0) ? inputByteStride : 6,
                            input            = (byte *)input,
                            outputByteStride = outputByteStride,
                            result           = output
                        };
#if UNITY_JOBS
                        jobHandle = job.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                        jobHandle = job.Schedule(count, GltfImport.DefaultBatchCount);
#endif
                    }
                    else
                    {
                        var job = new ConvertVector3Int16ToFloatInterleavedNormalizedJob {
                            inputByteStride  = (inputByteStride > 0) ? inputByteStride : 6,
                            input            = (byte *)input,
                            outputByteStride = outputByteStride,
                            result           = output
                        };
#if UNITY_JOBS
                        jobHandle = job.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                        jobHandle = job.Schedule(count, GltfImport.DefaultBatchCount);
#endif
                    }
                }
                else
                {
                    var job = new ConvertPositionsInt16ToFloatInterleavedJob {
                        inputByteStride  = (inputByteStride > 0) ? inputByteStride : 6,
                        input            = (byte *)input,
                        outputByteStride = outputByteStride,
                        result           = output
                    };
#if UNITY_JOBS
                    jobHandle = job.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                    jobHandle = job.Schedule(count, GltfImport.DefaultBatchCount);
#endif
                }
            }
            else
            if (inputType == GLTFComponentType.Byte)
            {
                if (normalized)
                {
                    if (ensureUnitLength)
                    {
                        var job = new ConvertNormalsInt8ToFloatInterleavedNormalizedJob {
                            input            = (sbyte *)input,
                            inputByteStride  = (inputByteStride > 0) ? inputByteStride : 3,
                            outputByteStride = outputByteStride,
                            result           = output
                        };
    #if UNITY_JOBS
                        jobHandle = job.ScheduleBatch(count, GltfImport.DefaultBatchCount);
    #else
                        jobHandle = job.Schedule(count, GltfImport.DefaultBatchCount);
    #endif
                    }
                    else
                    {
                        var job = new ConvertVector3Int8ToFloatInterleavedNormalizedJob()
                        {
                            input            = (sbyte *)input,
                            inputByteStride  = (inputByteStride > 0) ? inputByteStride : 3,
                            outputByteStride = outputByteStride,
                            result           = output
                        };
#if UNITY_JOBS
                        jobHandle = job.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                        jobHandle = job.Schedule(count, GltfImport.DefaultBatchCount);
#endif
                    }
                }
                else
                {
                    // TODO: test positions. did not have test files
                    var job = new ConvertPositionsInt8ToFloatInterleavedJob {
                        inputByteStride  = inputByteStride > 0 ? inputByteStride : 3,
                        input            = (sbyte *)input,
                        outputByteStride = outputByteStride,
                        result           = output
                    };
#if UNITY_JOBS
                    jobHandle = job.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                    jobHandle = job.Schedule(count, GltfImport.DefaultBatchCount);
#endif
                }
            }
            else
            if (inputType == GLTFComponentType.UnsignedByte)
            {
                // TODO: test. did not have test files
                if (normalized)
                {
                    var job = new ConvertPositionsUInt8ToFloatInterleavedNormalizedJob {
                        input            = (byte *)input,
                        inputByteStride  = (inputByteStride > 0) ? inputByteStride : 3,
                        outputByteStride = outputByteStride,
                        result           = output
                    };
#if UNITY_JOBS
                    jobHandle = job.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                    jobHandle = job.Schedule(count, GltfImport.DefaultBatchCount);
#endif
                }
                else
                {
                    var job = new ConvertPositionsUInt8ToFloatInterleavedJob {
                        input            = (byte *)input,
                        inputByteStride  = (inputByteStride > 0) ? inputByteStride : 3,
                        outputByteStride = outputByteStride,
                        result           = output
                    };
#if UNITY_JOBS
                    jobHandle = job.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                    jobHandle = job.Schedule(count, GltfImport.DefaultBatchCount);
#endif
                }
            }
            else
            {
                Debug.LogError("Unknown componentType");
                jobHandle = null;
            }
            Profiler.EndSample();
            return(jobHandle);
        }
Пример #25
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);
            }
        }
Пример #26
0
        unsafe JobHandle?GetUvsJob(
            void *input,
            int count,
            GLTFComponentType inputType,
            int inputByteStride,
            Vector2 *output,
            int outputByteStride,
            bool normalized = false
            )
        {
            Profiler.BeginSample("PrepareUVs");
            JobHandle?jobHandle = null;

            switch (inputType)
            {
            case GLTFComponentType.Float:
            {
                var jobUv = new Jobs.GetVector2sInterleavedJob {
                    inputByteStride  = (inputByteStride > 0) ? inputByteStride : 8,
                    input            = (byte *)input,
                    outputByteStride = outputByteStride,
                    result           = output
                };
                jobHandle = jobUv.Schedule(count, GLTFast.DefaultBatchCount);
            }
            break;

            case GLTFComponentType.UnsignedByte:
                if (normalized)
                {
                    var jobUv = new Jobs.GetUVsUInt8InterleavedNormalizedJob {
                        inputByteStride  = (inputByteStride > 0) ? inputByteStride : 2,
                        input            = (byte *)input,
                        outputByteStride = outputByteStride,
                        result           = output
                    };
                    jobHandle = jobUv.Schedule(count, GLTFast.DefaultBatchCount);
                }
                else
                {
                    var jobUv = new Jobs.GetUVsUInt8InterleavedJob {
                        inputByteStride  = (inputByteStride > 0) ? inputByteStride : 2,
                        input            = (byte *)input,
                        outputByteStride = outputByteStride,
                        result           = output
                    };
                    jobHandle = jobUv.Schedule(count, GLTFast.DefaultBatchCount);
                }
                break;

            case GLTFComponentType.UnsignedShort:
                if (normalized)
                {
                    var jobUv = new Jobs.GetUVsUInt16InterleavedNormalizedJob {
                        inputByteStride  = (inputByteStride > 0) ? inputByteStride : 4,
                        input            = (byte *)input,
                        outputByteStride = outputByteStride,
                        result           = output
                    };
                    jobHandle = jobUv.Schedule(count, GLTFast.DefaultBatchCount);
                }
                else
                {
                    var jobUv = new Jobs.GetUVsUInt16InterleavedJob {
                        inputByteStride  = (inputByteStride > 0) ? inputByteStride : 4,
                        input            = (byte *)input,
                        outputByteStride = outputByteStride,
                        result           = output
                    };
                    jobHandle = jobUv.Schedule(count, GLTFast.DefaultBatchCount);
                }
                break;

            case GLTFComponentType.Short:
                if (normalized)
                {
                    var job = new Jobs.GetUVsInt16InterleavedNormalizedJob {
                        inputByteStride  = inputByteStride > 0 ? inputByteStride : 4,
                        input            = (System.Int16 *)input,
                        outputByteStride = outputByteStride,
                        result           = output
                    };
                    jobHandle = job.Schedule(count, GLTFast.DefaultBatchCount);
                }
                else
                {
                    var job = new Jobs.GetUVsInt16InterleavedJob {
                        inputByteStride  = inputByteStride > 0 ? inputByteStride : 4,
                        input            = (System.Int16 *)input,
                        outputByteStride = outputByteStride,
                        result           = output
                    };
                    jobHandle = job.Schedule(count, GLTFast.DefaultBatchCount);
                }
                break;

            case GLTFComponentType.Byte:
                var byteStride = inputByteStride > 0 ? inputByteStride : 2;
                if (normalized)
                {
                    var jobInt8 = new Jobs.GetUVsInt8InterleavedNormalizedJob {
                        inputByteStride  = inputByteStride > 0 ? inputByteStride : 2,
                        input            = (sbyte *)input,
                        outputByteStride = outputByteStride,
                        result           = output
                    };
                    jobHandle = jobInt8.Schedule(count, GLTFast.DefaultBatchCount);
                }
                else
                {
                    var jobInt8 = new Jobs.GetUVsInt8InterleavedJob {
                        inputByteStride  = inputByteStride > 0 ? inputByteStride : 2,
                        input            = (sbyte *)input,
                        outputByteStride = outputByteStride,
                        result           = output
                    };
                    jobHandle = jobInt8.Schedule(count, GLTFast.DefaultBatchCount);
                }
                break;

            default:
                Debug.LogErrorFormat(GLTFast.ErrorUnsupportedType, "UV", inputType);
                break;
            }
            Profiler.EndSample();
            return(jobHandle);
        }
Пример #27
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);
        }
Пример #28
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));
        }
Пример #29
0
        protected unsafe JobHandle?GetTangentsJob(
            void *input,
            int count,
            GLTFComponentType inputType,
            int inputByteStride,
            float4 *output,
            int outputByteStride,
            bool normalized = false
            )
        {
            Profiler.BeginSample("GetTangentsJob");
            JobHandle?jobHandle;

            switch (inputType)
            {
            case GLTFComponentType.Float: {
                var jobTangent = new ConvertTangentsFloatToFloatInterleavedJob {
                    inputByteStride  = inputByteStride > 0 ? inputByteStride : 16,
                    input            = (byte *)input,
                    outputByteStride = outputByteStride,
                    result           = output
                };
#if UNITY_JOBS
                jobHandle = jobTangent.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                jobHandle = jobTangent.Schedule(count, GltfImport.DefaultBatchCount);
#endif
                break;
            }

            case GLTFComponentType.Short: {
                Assert.IsTrue(normalized);
                var jobTangent = new ConvertTangentsInt16ToFloatInterleavedNormalizedJob {
                    inputByteStride  = inputByteStride > 0 ? inputByteStride : 8,
                    input            = (short *)input,
                    outputByteStride = outputByteStride,
                    result           = output
                };
#if UNITY_JOBS
                jobHandle = jobTangent.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                jobHandle = jobTangent.Schedule(count, GltfImport.DefaultBatchCount);
#endif
                break;
            }

            case GLTFComponentType.Byte: {
                Assert.IsTrue(normalized);
                var jobTangent = new ConvertTangentsInt8ToFloatInterleavedNormalizedJob {
                    inputByteStride  = inputByteStride > 0 ? inputByteStride : 4,
                    input            = (sbyte *)input,
                    outputByteStride = outputByteStride,
                    result           = output
                };
#if UNITY_JOBS
                jobHandle = jobTangent.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                jobHandle = jobTangent.Schedule(count, GltfImport.DefaultBatchCount);
#endif
                break;
            }

            default:
                logger?.Error(LogCode.TypeUnsupported, "Tangent", inputType.ToString());
                jobHandle = null;
                break;
            }

            Profiler.EndSample();
            return(jobHandle);
        }
Пример #30
0
        protected unsafe JobHandle?GetWeightsJob(
            void *input,
            int count,
            GLTFComponentType inputType,
            int inputByteStride,
            float4 *output,
            int outputByteStride,
            bool normalized = false
            )
        {
            Profiler.BeginSample("GetWeightsJob");
            JobHandle?jobHandle;

            switch (inputType)
            {
            case GLTFComponentType.Float:
                var jobTangentI = new Jobs.ConvertBoneWeightsFloatToFloatInterleavedJob();
                jobTangentI.inputByteStride  = inputByteStride > 0 ? inputByteStride : 16;
                jobTangentI.input            = (byte *)input;
                jobTangentI.outputByteStride = outputByteStride;
                jobTangentI.result           = output;
#if UNITY_JOBS
                jobHandle = jobTangentI.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                jobHandle = jobTangentI.Schedule(count, GltfImport.DefaultBatchCount);
#endif
                break;

            case GLTFComponentType.UnsignedShort: {
                var job = new Jobs.ConvertBoneWeightsUInt16ToFloatInterleavedJob {
                    inputByteStride  = inputByteStride > 0 ? inputByteStride : 8,
                    input            = (byte *)input,
                    outputByteStride = outputByteStride,
                    result           = output
                };
#if UNITY_JOBS
                jobHandle = job.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                jobHandle = job.Schedule(count, GltfImport.DefaultBatchCount);
#endif
                break;
            }

            case GLTFComponentType.UnsignedByte: {
                var job = new Jobs.ConvertBoneWeightsUInt8ToFloatInterleavedJob {
                    inputByteStride  = inputByteStride > 0 ? inputByteStride : 4,
                    input            = (byte *)input,
                    outputByteStride = outputByteStride,
                    result           = output
                };
#if UNITY_JOBS
                jobHandle = job.ScheduleBatch(count, GltfImport.DefaultBatchCount);
#else
                jobHandle = job.Schedule(count, GltfImport.DefaultBatchCount);
#endif
                break;
            }

            default:
                logger?.Error(LogCode.TypeUnsupported, "Weights", inputType.ToString());
                jobHandle = null;
                break;
            }

            Profiler.EndSample();
            return(jobHandle);
        }