UnityGLTF icon indicating copy to clipboard operation
UnityGLTF copied to clipboard

Why ExportImages() in SaveGLB and how to write to internalBuffer

Open JochnGst opened this issue 8 years ago • 6 comments

Hi, I am trieing to export several models as GLB in view them in the Mixed-Reality-Viewer. Now I saw that in the validator all images described as external reference.

And I think the benifit of GLB is that all is included in this one binary file.

JochnGst avatar May 22 '18 11:05 JochnGst

GLTFSceneExporter.zip

I have added a few Chances to the GLTFSceneExporter.cs

first of all I added a little Methode to all ExportAccessor Funktions to adjust the align to 4-byte.

The secound and bigger chance is that I have implementet that the SaveGLB Methode save images into the Buffer.

I have Testet a few files with this Methode and the Validator give always 0 errors. And now the Mixed-Reality-Viewer kann also display the result glb from the exporter.

PS:

I found a little error in the Image.cs file \GLTFSerialization\GLTFSerialization\Schema\Image.cs

In the Serialize Methode the BufferView object is given to the writer, but right is to give BufferView.Id like by all other Serialize Methodes.

    public override void Serialize(JsonWriter writer)
    {
        writer.WriteStartObject();

        if (Uri != null)
        {
            writer.WritePropertyName("uri");
            writer.WriteValue(Uri);
        }

        if (MimeType != null)
        {
            writer.WritePropertyName("mimeType");
            writer.WriteValue(Newtonsoft.Json.Linq.JValue.CreateString(MimeType).ToString());
        }

        if (BufferView != null)
        {
            writer.WritePropertyName("bufferView");
            writer.WriteValue(BufferView.Id);
        }

        base.Serialize(writer);

        writer.WriteEndObject();
    }`

JochnGst avatar May 23 '18 11:05 JochnGst

@Neuxxx: I think you should open a pull request. So it is easy to see which changes you made and where the bugs have been.

janholo avatar Jul 09 '18 13:07 janholo

@Neuxxx Your solution worked nicely for me. Please do open a pull request, this should be an official part of the library.

yasirkula avatar Jul 13 '18 13:07 yasirkula

In order to make some changes in line with the official export

“ExportImageInternalBuffer” methed Add parameters “TextureMapType texturMapType” private ImageId ExportImageInternalBuffer(UnityEngine.Texture texture, TextureMapType texturMapType) {

	if (texture == null)
	{
		throw new Exception("texture can not be NULL.");
	}

	var image = new GLTFImage();
	image.MimeType = "image/png";
    
	var byteOffset = _bufferWriter.BaseStream.Position;

	{//
		var destRenderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);
		GL.sRGBWrite = true;
        switch (texturMapType)
        {
            case TextureMapType.MetallicGloss:
                Graphics.Blit(texture, destRenderTexture, _metalGlossChannelSwapMaterial);
                break;
            case TextureMapType.Bump:
                Graphics.Blit(texture, destRenderTexture, _normalChannelMaterial);
                break;
            default:
                Graphics.Blit(texture, destRenderTexture);
                break;
        }

        //Graphics.Blit(texture, destRenderTexture);

		var exportTexture = new Texture2D(texture.width, texture.height, TextureFormat.ARGB32, false);
		exportTexture.ReadPixels(new Rect(0, 0, destRenderTexture.width, destRenderTexture.height), 0, 0);
		exportTexture.Apply();

		var pngImageData = exportTexture.EncodeToPNG();
		_bufferWriter.Write(pngImageData);

		destRenderTexture.Release();
		GL.sRGBWrite = false;
		if (Application.isEditor)
		{
			UnityEngine.Object.DestroyImmediate(exportTexture);
		}
		else
		{
			UnityEngine.Object.Destroy(exportTexture);
		}
	}

	var byteLength = _bufferWriter.BaseStream.Position - byteOffset;

	byteLength = AppendToBufferMultiplyOf4(byteOffset, byteLength);

	image.BufferView = ExportBufferView((uint)byteOffset, (uint)byteLength);


	var id = new ImageId
	{
		Id = _root.Images.Count,
		Root = _root
	};
	_root.Images.Add(image);

	return id;
}

kknsy avatar Dec 14 '18 08:12 kknsy

@kknsy, could you make a pull request for this?

ericob avatar Jan 04 '19 22:01 ericob

Cleaning up old issues. Looks like this was fixed a while back.

AdamMitchell-ms avatar Jun 25 '19 21:06 AdamMitchell-ms