示例#1
0
 public void Dispose()
 {
     _fbx?.Dispose();
     _textures.Dispose();
     _indices.Dispose();
     _vertices.Dispose();
     _skeletons.Dispose();
 }
示例#2
0
            static UnsafeRawArray <T> Decode <T>(Reader reader, int arrayLength, int compressedSize) where T : unmanaged
            {
                const int deflateMetaDataSize = 2;

                reader.Int16(out var _);            // deflateMetaData (not be used)

                // read compressed data
                using var buf = new UnsafeRawArray <byte>(compressedSize - deflateMetaDataSize);
                reader.Read(buf.AsSpan());

                // decompress data
                var ptr = (byte *)buf.Ptr;

                using var ms = (_ms is null) ? (_ms = new MemoryStreamUM(ptr, buf.Length)) : _ms.RefreshInstance(ptr, buf.Length);
                using var ds = new DeflateStream(ms, CompressionMode.Decompress);
                var decoded = new UnsafeRawArray <T>(arrayLength);

                try {
                    ds.Read(MemoryMarshal.Cast <T, byte>(decoded.AsSpan()));
                }
                catch {
                    decoded.Dispose();
                    throw;
                }
                return(decoded);
            }
示例#3
0
        private static UnsafeRawArray <Components.Bone> GetBones(PMXObject pmx)
        {
            var pmxBones = pmx.BoneList.AsSpan();
            var bones    = new UnsafeRawArray <Components.Bone>(pmxBones.Length, false);

            try {
                for (int i = 0; i < pmxBones.Length; i++)
                {
                    var     parentBone = pmxBones[i].ParentBone >= 0 ? pmxBones[i].ParentBone : (int?)null;
                    Vector3 boneVec;
                    var     c = pmxBones[i].ConnectedBone;
                    if (c >= 0)
                    {
                        boneVec = pmxBones[c].Position.ToVector3() - pmxBones[i].Position.ToVector3();
                    }
                    else
                    {
                        boneVec = pmxBones[i].PositionOffset.ToVector3();
                    }

                    bones[i] = new Components.Bone();
                }
            }
            catch {
                bones.Dispose();
                throw;
            }
            return(bones);
        }
示例#4
0
        private static UnsafeRawArray <SkinnedVertex> GetVertices(PMXObject pmx)
        {
            // build vertices
            var pmxVertices = pmx.VertexList.AsSpan();
            var vertices    = new UnsafeRawArray <SkinnedVertex>(pmxVertices.Length, false);

            try {
                for (int i = 0; i < pmxVertices.Length; i++)
                {
                    vertices[i] = pmxVertices[i].ToRigVertex();
                }
            }
            catch {
                vertices.Dispose();
                throw;
            }
            return(vertices);
        }
示例#5
0
        public async UniTask LoadAsync <TBoneSpan>(TBoneSpan bones, FrameTimingPointList timingPoints, FrameTiming timing,
                                                   CancellationToken cancellationToken = default) where TBoneSpan : IReadOnlySpan <Bone>
        {
            if (IsBoneLoaded)
            {
                throw new InvalidOperationException("Already loaded");
            }
            if (timingPoints is null)
            {
                throw new ArgumentNullException(nameof(timingPoints));
            }
            timing.ThrowArgExceptionIfNotSpecified(nameof(timing));

            cancellationToken.ThrowIfCancellationRequested();
            await UniTask.SwitchToThreadPool();

            cancellationToken.ThrowIfCancellationRequested();
            try {
                InitializeSkeletonData(this, bones.AsReadOnlySpan());
                await timingPoints.TimingOf(timing).Next(cancellationToken);

                if (IsBoneLoaded)
                {
                    throw new InvalidOperationException("Already loaded");
                }
                _boneTranslationData.Load(_matrices !.AsSpan().MarshalCast <Matrix4, Color4>());
            }
            catch {
                _posMatrices.Dispose();
                _posInvMatrices.Dispose();
                _tree.Dispose();
                _matrices.Dispose();
                _translations.Dispose();
                throw;
            }

            ContextAssociatedMemorySafety.Register(this, Engine.GetValidCurrentContext());
            return;
        }
示例#6
0
        private unsafe static void ParseProperty(Reader reader, ref FbxProperty property)
        {
            reader.Byte(out var propertyType);
            switch (propertyType)
            {
            case INT16_PROPERTY: {
                reader.Int16(out var value);
                property.SetInt16(value);
                break;
            }

            case BOOL_PROPERTY: {
                reader.Byte(out var value);
                // two types format exist. (Oh my gosh !! Fuuuuuuuu*k !!)
                // blender             -> true/false = 0x01/0x00
                // Autodesk production -> true/false = 'Y'/'T' = 0x59/0x54
                property.SetBool(value != 0x00 && value != 0x54);
                break;
            }

            case INT32_PROPERTY: {
                reader.Int32(out var value);
                property.SetInt32(value);
                break;
            }

            case FLOAT_PROPERTY: {
                reader.Float(out var value);
                property.SetFloat(value);
                break;
            }

            case DOUBLE_PROPERTY: {
                reader.Double(out var value);
                property.SetDouble(value);
                break;
            }

            case INT64_PROPERTY: {
                reader.Int64(out var value);
                property.SetInt64(value);
                break;
            }

            case STRING_PROPERTY: {
                reader.Int32(out var len);
                RawStringMem str = default;
                try {
                    str = new RawStringMem(len);
                    reader.Read(str.AsSpan());
                    SanitizeString(str.AsSpan());
                    property.SetString((byte *)str.Ptr, str.ByteLength);
                }
                catch {
                    str.Dispose();
                    throw;
                }
                break;
            }

            case BOOL_ARRAY_PROPERTY: {
                reader.Int32(out var len);
                reader.UInt32(out var encoded);
                reader.UInt32(out var compressedSize);
                UnsafeRawArray <bool> array = default;               // Don't dispose
                try {
                    if (encoded != 0)
                    {
                        array = Decode <bool>(reader, len, (int)compressedSize);
                    }
                    else
                    {
                        array = new UnsafeRawArray <bool>((int)compressedSize);
                        reader.Read(MemoryMarshal.Cast <bool, byte>(array.AsSpan()));
                    }
                    property.SetBoolArray((bool *)array.Ptr, array.Length);
                }
                catch {
                    array.Dispose();
                    throw;
                }
                break;
            }

            case INT32_ARRAY_PROPERTY: {
                reader.Int32(out var len);
                reader.UInt32(out var encoded);
                reader.UInt32(out var compressedSize);
                UnsafeRawArray <int> array = default;               // Don't dispose
                try {
                    if (encoded != 0)
                    {
                        array = Decode <int>(reader, len, (int)compressedSize);
                    }
                    else
                    {
                        array = new UnsafeRawArray <int>((int)compressedSize / sizeof(int));
                        reader.Read(MemoryMarshal.Cast <int, byte>(array.AsSpan()));
                    }
                    property.SetInt32Array((int *)array.Ptr, array.Length);
                }
                catch {
                    array.Dispose();
                    throw;
                }
                break;
            }

            case FLOAT_ARRAY_PROPERTY: {
                reader.Int32(out var len);
                reader.UInt32(out var encoded);
                reader.UInt32(out var compressedSize);
                UnsafeRawArray <float> array = default;               // Don't dispose
                try {
                    if (encoded != 0)
                    {
                        array = Decode <float>(reader, len, (int)compressedSize);
                    }
                    else
                    {
                        array = new UnsafeRawArray <float>((int)compressedSize / sizeof(float));
                        reader.Read(MemoryMarshal.Cast <float, byte>(array.AsSpan()));
                    }
                    property.SetFloatArray((float *)array.Ptr, array.Length);
                }
                catch {
                    array.Dispose();
                    throw;
                }
                break;
            }

            case DOUBLE_ARRAY_PROPERTY: {
                reader.Int32(out var len);
                reader.UInt32(out var encoded);
                reader.UInt32(out var compressedSize);
                UnsafeRawArray <double> array = default;
                try {
                    if (encoded != 0)
                    {
                        array = Decode <double>(reader, len, (int)compressedSize);
                    }
                    else
                    {
                        array = new UnsafeRawArray <double>((int)compressedSize / sizeof(double));
                        reader.Read(MemoryMarshal.Cast <double, byte>(array.AsSpan()));
                    }
                    property.SetDoubleArray((double *)array.Ptr, array.Length);
                }
                catch {
                    array.Dispose();
                    throw;
                }
                break;
            }

            case INT64_ARRAY_PROPERTY: {
                reader.Int32(out var len);
                reader.UInt32(out var encoded);
                reader.UInt32(out var compressedSize);
                UnsafeRawArray <long> array = default;
                try {
                    if (encoded != 0)
                    {
                        array = Decode <long>(reader, len, (int)compressedSize);
                    }
                    else
                    {
                        array = new UnsafeRawArray <long>((int)compressedSize / sizeof(long));
                        reader.Read(MemoryMarshal.Cast <long, byte>(array.AsSpan()));
                    }
                    property.SetInt64Array((long *)array.Ptr, array.Length);
                }
                catch {
                    array.Dispose();
                    throw;
                }
                break;
            }

            case RAW_BINARY_PROPERTY: {
                reader.Int32(out var len);
                var buf = new UnsafeRawArray <byte>(len);
                try {
                    reader.Read(buf.AsSpan());
                    property.SetByteArray((byte *)buf.Ptr, buf.Length);
                }
                catch {
                    buf.Dispose();
                    throw;
                }
                break;
            }

            default: {
                Debug.WriteLine($"[Skip Unknow Type Property] Position : {reader.BaseStream.Position}, type : {propertyType}");
                break;
            }
            }