static BytesWithMime GetBytesWithMime(Texture texture, glTFTextureTypes textureType) { #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", }); } if (path.Extension == ".jpg") { return(new BytesWithMime { Bytes = System.IO.File.ReadAllBytes(path.FullPath), Mime = "image/jpeg", }); } } #endif return(new BytesWithMime { Bytes = TextureItem.CopyTexture(texture, TextureIO.GetColorSpace(textureType), null).EncodeToPNG(), Mime = "image/png", }); }
public static int ExportTexture(glTF gltf, int bufferIndex, Texture texture, glTFTextureTypes textureType) { var bytesWithMime = GetBytesWithMime(texture, textureType);; // add view var view = gltf.buffers[bufferIndex].Append(bytesWithMime.Bytes, glBufferTarget.NONE); var viewIndex = gltf.AddBufferView(view); // add image var imageIndex = gltf.images.Count; gltf.images.Add(new glTFImage { name = texture.name, bufferView = viewIndex, mimeType = bytesWithMime.Mime, }); // add sampler var samplerIndex = gltf.samplers.Count; var sampler = TextureSamplerUtil.Export(texture); gltf.samplers.Add(sampler); // add texture gltf.textures.Add(new glTFTexture { sampler = samplerIndex, source = imageIndex, }); return(imageIndex); }
/// <summary> /// Texture の export index を得る /// </summary> /// <param name="src"></param> /// <param name="textureType"></param> /// <returns></returns> public int GetTextureIndex(Texture src, glTFTextureTypes textureType) { if (src == null) { return(-1); } return(m_exportMap[new ExportKey(src, textureType)]); }
public ExportKey(Texture src, glTFTextureTypes type) { if (src == null) { throw new ArgumentNullException(); } Src = src; TextureType = type; }
public virtual (Byte[] bytes, string mine) GetBytesWithMime(Texture texture, glTFTextureTypes textureType) { #if UNITY_EDITOR var path = UnityPath.FromAsset(texture); if (path.IsUnderAssetsFolder) { var textureImporter = AssetImporter.GetAtPath(path.Value) as TextureImporter; var getSizeMethod = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance); if (textureImporter != null && getSizeMethod != null) { var args = new object[2] { 0, 0 }; getSizeMethod.Invoke(textureImporter, args); var originalWidth = (int)args[0]; var originalHeight = (int)args[1]; var originalSize = Mathf.Max(originalWidth, originalHeight); var requiredMaxSize = textureImporter.maxTextureSize; // Resized exporting if MaxSize setting value is smaller than original image size. if (originalSize > requiredMaxSize) { return ( TextureConverter.CopyTexture(texture, GetColorSpace(textureType), null).EncodeToPNG(), "image/png" ); } } if (path.Extension == ".png") { return ( System.IO.File.ReadAllBytes(path.FullPath), "image/png" ); } if (path.Extension == ".jpg") { return ( System.IO.File.ReadAllBytes(path.FullPath), "image/jpeg" ); } } #endif return ( TextureConverter.CopyTexture(texture, TextureIO.GetColorSpace(textureType), null).EncodeToPNG(), "image/png" ); }
public static Texture2D Convert(Texture2D texture, glTFTextureTypes textureType, ColorConversion colorConversion, Material convertMaterial) { var copyTexture = TextureItem.CopyTexture(texture, TextureIO.GetColorSpace(textureType), convertMaterial); if (colorConversion != null) { copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => colorConversion(x)).ToArray()); copyTexture.Apply(); } copyTexture.name = texture.name; return(copyTexture); }
public static RenderTextureReadWrite GetColorSpace(this glTFTextureTypes textureType) { switch (textureType) { case glTFTextureTypes.SRGB: return(RenderTextureReadWrite.sRGB); case glTFTextureTypes.OcclusionMetallicRoughness: case glTFTextureTypes.Normal: return(RenderTextureReadWrite.Linear); default: throw new NotImplementedException(); } }
public static bool TryGetglTFTextureType(this glTF glTf, int textureIndex, out glTFTextureTypes textureType) { foreach (var material in glTf.materials) { var textureInfo = material.GetTextures().FirstOrDefault(x => (x != null) && x.index == textureIndex); if (textureInfo != null) { textureType = textureInfo.TextureType; return(true); } } // textureIndex is not used by Material. textureType = default; return(false); }
public static RenderTextureReadWrite GetColorSpace(glTFTextureTypes textureType) { switch (textureType) { case glTFTextureTypes.Metallic: case glTFTextureTypes.Normal: case glTFTextureTypes.Occlusion: return(RenderTextureReadWrite.Linear); case glTFTextureTypes.BaseColor: case glTFTextureTypes.Emissive: return(RenderTextureReadWrite.sRGB); default: return(RenderTextureReadWrite.sRGB); } }
public static Texture2D CopyTexture(Texture src, glTFTextureTypes textureType, Material material) { Texture2D dst = null; RenderTextureReadWrite colorSpace = textureType.GetColorSpace(); var renderTexture = new RenderTexture(src.width, src.height, 0, RenderTextureFormat.ARGB32, colorSpace); using (var scope = new ColorSpaceScope(colorSpace)) { if (material != null) { Graphics.Blit(src, renderTexture, material); } else { Graphics.Blit(src, renderTexture); } } dst = new Texture2D(src.width, src.height, TextureFormat.ARGB32, false, colorSpace == RenderTextureReadWrite.Linear); dst.ReadPixels(new Rect(0, 0, src.width, src.height), 0, 0); dst.name = src.name; dst.anisoLevel = src.anisoLevel; dst.filterMode = src.filterMode; dst.mipMapBias = src.mipMapBias; dst.wrapMode = src.wrapMode; #if UNITY_2017_1_OR_NEWER dst.wrapModeU = src.wrapModeU; dst.wrapModeV = src.wrapModeV; dst.wrapModeW = src.wrapModeW; #endif dst.Apply(); RenderTexture.active = null; if (Application.isEditor) { GameObject.DestroyImmediate(renderTexture); } else { GameObject.Destroy(renderTexture); } return(dst); }
static BytesWithMime GetBytesWithMime(Texture texture, glTFTextureTypes textureType) { /* #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 */ return(new BytesWithMime { Bytes = TextureItem.CopyTexture(texture, TextureIO.GetColorSpace(textureType), null).EncodeToPNG(), Mime = "image/png", }); }
public TextureExportItem(Texture texture, glTFTextureTypes textureType) { Texture = texture; TextureType = textureType; }