Ejemplo n.º 1
0
        private static byte[] TryLoadBase64BinaryBufferUnchecked(Schema.Buffer buffer, string prefix)
        {
            if (buffer.Uri is null || !buffer.Uri.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
            {
                return(null);
            }

            string content = buffer.Uri.Substring(prefix.Length);

            return(Convert.FromBase64String(content));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets a binary buffer referenced by a specific <code>Schema.Buffer</code>
        /// </summary>
        /// <param name="model">The <code>Schema.Gltf</code> model containing the <code>Schema.Buffer</code></param>
        /// <param name="bufferIndex">The index of the buffer</param>
        /// <param name="externalReferenceSolver">An user provided lambda function to resolve external assets</param>
        /// <returns>Byte array of the buffer</returns>
        /// <remarks>
        /// Binary buffers can be stored in three different ways:
        /// - As stand alone files.
        /// - As a binary chunk within a glb file.
        /// - Encoded to Base64 within the JSON.
        ///
        /// The external reference solver funcion is called when the buffer is stored in an external file,
        /// or when the buffer is in the glb binary chunk, in which case, the Argument of the function will be Null.
        ///
        /// The Lambda function must return the byte array of the requested file or buffer.
        /// </remarks>
        public static byte[] LoadBinaryBuffer(this Gltf model, int bufferIndex, Func <string, byte[]> externalReferenceSolver)
        {
            Schema.Buffer buffer = model.Buffers[bufferIndex];

            byte[] bufferData = LoadBinaryBufferUnchecked(buffer, externalReferenceSolver);

            // As per https://github.com/KhronosGroup/glTF/issues/1026
            // Due to buffer padding, buffer length can be equal or larger than expected length by only 3 bytes
            if (bufferData.Length < buffer.ByteLength || (bufferData.Length - buffer.ByteLength) > 3)
            {
                throw new InvalidDataException($"The buffer length is {bufferData.Length}, expected {buffer.ByteLength}");
            }

            return(bufferData);
        }
Ejemplo n.º 3
0
 private static byte[] LoadBinaryBufferUnchecked(Schema.Buffer buffer, Func <string, byte[]> externalReferenceSolver)
 {
     return(TryLoadBase64BinaryBufferUnchecked(buffer, EMBEDDEDGLTFBUFFER)
            ?? TryLoadBase64BinaryBufferUnchecked(buffer, EMBEDDEDOCTETSTREAM)
            ?? externalReferenceSolver(buffer?.Uri));
 }
Ejemplo n.º 4
0
        internal byte[] LoadDataUri(Schema.Buffer buff)
        {
            int idxComa = buff.Uri.IndexOf(",", 5, StringComparison.Ordinal);

            return(Convert.FromBase64String(buff.Uri.Substring(idxComa + 1)));
        }