public void SaveGLTFandBin(string path, string fileName) { var binFile = File.Create(Path.Combine(path, fileName + ".bin")); _bufferWriter = new BinaryWriter(binFile); _root.Scene = ExportScene(fileName, _rootTransforms); _buffer.Uri = fileName + ".bin"; _buffer.ByteLength = (int)_bufferWriter.BaseStream.Length; var gltfFile = File.CreateText(Path.Combine(path, fileName + ".gltf")); var writer = new JsonTextWriter(gltfFile); _root.Serialize(writer); gltfFile.Close(); binFile.Close(); foreach (var image in _images) { Debug.Log(image.name); var renderTexture = RenderTexture.GetTemporary(image.width, image.height); Graphics.Blit(image, renderTexture); RenderTexture.active = renderTexture; var exportTexture = new Texture2D(image.width, image.height); exportTexture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0); exportTexture.Apply(); File.WriteAllBytes(Path.Combine(path, image.name + ".png"), exportTexture.EncodeToPNG()); } }
/// <summary> /// Turns a glTF file w/ structure into a GLB. Does not currently copy binary data /// </summary> /// <param name="root">The glTF root to turn into a GLBObject</param> /// <param name="glbOutStream">Output stream to write the GLB to</param> /// <param name="loader">Loader for loading external components from GLTFRoot. The loader will receive uris and return the stream to the resource</param> /// <returns>A constructed GLBObject</returns> private static GLBObject ConstructFromGLTF(GLTFRoot root, Stream glbOutStream, Func <string, Stream> loader) { if (root == null) { throw new ArgumentNullException(nameof(root)); } if (glbOutStream == null) { throw new ArgumentNullException(nameof(glbOutStream)); } MemoryStream gltfJsonStream = new MemoryStream(); using (StreamWriter sw = new StreamWriter(gltfJsonStream)) { root.Serialize(sw, true); sw.Flush(); long proposedLength = gltfJsonStream.Length + GLTFParser.HEADER_SIZE + GLTFParser.CHUNK_HEADER_SIZE; if (gltfJsonStream.Length > uint.MaxValue) { throw new ArgumentException("Serialized root cannot exceed uint.maxvalue", nameof(root)); } uint proposedLengthAsUint = (uint)proposedLength; glbOutStream.SetLength(proposedLengthAsUint); GLBObject glbObject = new GLBObject { Header = new GLBHeader { FileLength = proposedLengthAsUint, Version = 2 }, Root = root, Stream = glbOutStream, JsonChunkInfo = new ChunkInfo { Length = (uint)gltfJsonStream.Length, StartPosition = GLTFParser.HEADER_SIZE, Type = ChunkFormat.JSON } }; // write header WriteHeader(glbOutStream, glbObject.Header, glbObject.StreamStartPosition); // write chunk header WriteChunkHeader(glbOutStream, glbObject.JsonChunkInfo); gltfJsonStream.Position = 0; gltfJsonStream.CopyTo(glbOutStream); // todo: implement getting binary data for loader return(glbObject); } }