Beispiel #1
0
        private static PerVertexTangentType ExtractTangents(IMgtfPerVertexDataLocator locator,
                                                            MgtfAccessor[] accessors)
        {
            if (locator.Tangent.HasValue)
            {
                var selected = accessors[locator.Tangent.Value];

                var result = PerVertexTangentType.None;
                if (selected.ElementType == MgtfElementType.Float && selected.NoOfComponents == 4)
                {
                    result = PerVertexTangentType.Float4;
                }
                else if (selected.ElementType == MgtfElementType.Half && selected.NoOfComponents == 4)
                {
                    result = PerVertexTangentType.Half4;
                }
                else
                {
                    throw new NotSupportedException(
                              string.Format(
                                  "Format not support : {0}",
                                  selected.ElementType)
                              );
                }
                return(result);
            }
            else
            {
                return(PerVertexTangentType.None);
            }
        }
Beispiel #2
0
        private static PerVertexPositionType ExtractPosition(
            IMgtfPerVertexDataLocator locator,
            MgtfAccessor[] accessors)
        {
            if (locator.Position.HasValue)
            {
                var selected = accessors[locator.Position.Value];

                bool isInvalid = true;
                var  result    = PerVertexPositionType.None;
                if (selected.ElementType == MgtfElementType.Float)
                {
                    if (selected.NoOfComponents == 3)
                    {
                        result    = PerVertexPositionType.Float3;
                        isInvalid = false;
                    }
                    else if (selected.NoOfComponents == 2)
                    {
                        result    = PerVertexPositionType.Float2;
                        isInvalid = false;
                    }
                }
                else if (selected.ElementType == MgtfElementType.Half)
                {
                    if (selected.NoOfComponents == 3)
                    {
                        result    = PerVertexPositionType.Half3;
                        isInvalid = false;
                    }
                    else if (selected.NoOfComponents == 2)
                    {
                        result    = PerVertexPositionType.Half2;
                        isInvalid = false;
                    }
                }

                if (isInvalid)
                {
                    throw new NotSupportedException(
                              string.Format(
                                  "Format not support : {0}",
                                  selected.ElementType)
                              );
                }

                return(result);
            }
            else
            {
                return(PerVertexPositionType.None);
            }
        }
Beispiel #3
0
 public static PerVertexDefinition Extract(IMgtfPerVertexDataLocator locator, MgtfAccessor[] accessors)
 {
     return(new PerVertexDefinition
     {
         IndexType = ExtractIndexType(locator.Indices, accessors),
         Position = ExtractPosition(locator, accessors),
         Normal = ExtractNormals(locator, accessors),
         Tangent = ExtractTangents(locator, accessors),
         TexCoords0 = ExtractTexCoords(locator.TexCoords0, accessors),
         TexCoords1 = ExtractTexCoords(locator.TexCoords1, accessors),
         Weights0 = ExtractWeights(locator.Weights0, accessors),
         Weights1 = ExtractWeights(locator.Weights1, accessors),
         Color0 = ExtractColor(locator.Color0, accessors),
         Color1 = ExtractColor(locator.Color1, accessors),
         Joints0 = ExtractJoints(locator.Joints0, accessors),
         Joints1 = ExtractJoints(locator.Joints1, accessors),
     });
 }
Beispiel #4
0
        private static int ExtractVertices(uint vertexCount,
                                           MgStorageBlockAllocationRequest request,
                                           MgtfAccessor[] accessors,
                                           MgtfBufferView[] bufferViews,
                                           IMgtfPerVertexDataLocator locator,
                                           out GltfInterleavedOperation[] vertexCopies)
        {
            var totalSize    = 0UL;
            var vertexFields = new int?[]
            {
                locator.Position,
                locator.Normal,
                locator.Tangent,
                locator.TexCoords0,
                locator.TexCoords1,
                locator.Color0,
                locator.Color1,
                locator.Joints0,
                locator.Joints1,
                locator.Weights0,
                locator.Weights1,
            };

            var DEFAULT_PADDING_BYTE_STRIDE = new uint[]
            {
                12U,
                12U,
                16U,
                8U,
                8U,
                16U,
                16U,
                4U,
                4U,
                16U,
                16U,
            };

            var copyOps = new List <GltfInterleavedOperation>();

            var vertexBufferStride = 0U;

            for (var i = 0; i < vertexFields.Length; i += 1)
            {
                var field = vertexFields[i];
                if (field.HasValue)
                {
                    var selected = accessors[field.Value];
                    var op       = GenerateCopyOperation(selected, bufferViews);
                    copyOps.Add(op);
                    vertexBufferStride += op.ByteStride;
                    totalSize          += selected.TotalByteSize;
                }
                else
                {
                    vertexBufferStride += DEFAULT_PADDING_BYTE_STRIDE[i];
                    totalSize          += vertexCount * DEFAULT_PADDING_BYTE_STRIDE[i];
                }
            }

            foreach (var op in copyOps)
            {
                op.DstStride = vertexBufferStride;
            }

            var vertexInfo = new MgStorageBlockAllocationInfo
            {
                MemoryPropertyFlags = MgMemoryPropertyFlagBits.HOST_COHERENT_BIT,
                Usage           = MgBufferUsageFlagBits.VERTEX_BUFFER_BIT,
                ElementByteSize = vertexBufferStride,
                Size            = totalSize,
            };

            vertexCopies = copyOps.ToArray();
            return(request.Insert(vertexInfo));
        }