Exemple #1
0
        public static BytesWithMime GetBytesWithMime(this Texture texture, GLTFTextureType textureType, int mimeType = 0, int quality = 75, int width = 0, int height = 0)
        {
            var tex   = texture.CopyTexture(textureType.GetColorSpace(), null, width, height);
            var bytes = mimeType == 0 ? tex.EncodeToPNG() : tex.EncodeToJPG(quality);

            if (Application.isEditor)
            {
                GameObject.DestroyImmediate(tex);
            }
            else
            {
                GameObject.Destroy(tex);
            }
            return(new BytesWithMime
            {
                bytes = bytes,
                mime = mimeType == 0 ? "image/png" : "image/jpeg"
            });
            // #if UNITY_EDITOR
            //             var path = UnityPath.FromAsset(texture);
            //             if (path.isUnderAssetsFolder)
            //             {
            //                 if (path.extension == ".png")
            //                 {
            //                     return new BytesWithMime
            //                     {
            //                         bytes = System.IO.File.ReadAllBytes(path.fullPath),
            //                         mime = "image/png"
            //                     };
            //                 }
            //             }
            // #endif
        }
Exemple #2
0
        public static Texture2D Convert(Texture2D texture, GLTFTextureType textureType, ColorConversion colorConversion, Material convertMaterial)
        {
            var copyTexture = texture.CopyTexture(textureType.GetColorSpace(), convertMaterial);

            if (colorConversion != null)
            {
                copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => colorConversion(x)).ToArray());
                copyTexture.Apply();
            }
            copyTexture.name = texture.name;
            return(copyTexture);
        }
Exemple #3
0
        public static RenderTextureReadWrite GetColorSpace(this GLTFTextureType textureType)
        {
            switch (textureType)
            {
            case GLTFTextureType.Metallic:
            case GLTFTextureType.Normal:
            case GLTFTextureType.Occlusion:
                return(RenderTextureReadWrite.Linear);

            case GLTFTextureType.BaseColor:
            case GLTFTextureType.Emissive:
                return(RenderTextureReadWrite.sRGB);

            default:
                return(RenderTextureReadWrite.sRGB);
            }
        }
Exemple #4
0
        public static int ExportTexture(this GLTFRoot gltf, int bufferIndex, Texture texture, GLTFTextureType textureType, int mimeType = 0)
        {
            //var bytesWithMime = GetBytesWithMime(texture, textureType); ;

            // add view
            //var view = gltf.buffers[bufferIndex].Append(bytesWithMime.bytes, GLTFBufferTarget.NONE);
            //var viewIndex = gltf.AddBufferView(view);

            // add image
            var imageIndex = gltf.images.Count;
            var image      = new GLTFImage
            {
                name     = texture.name,
                mimeType = mimeType == 0 ? "image/png" : "image/jpeg"
            };

            //bufferView = viewIndex,
            image.uri = texture.name.ToLower() + image.GetExt().ToLower();
            gltf.images.Add(image);

            // add sampler
            var samplerIndex = gltf.samplers.Count;
            var sampler      = texture.ToTextureSampler();

            gltf.samplers.Add(sampler);

            // add texture
            gltf.textures.Add(new GLTFTexture
            {
                sampler = samplerIndex,
                source  = imageIndex,
            });

            return(imageIndex);
        }
Exemple #5
0
 public TextureExportItem(Texture texture, GLTFTextureType textureType)
 {
     Texture     = texture;
     TextureType = textureType;
 }