public ImageId SaveImageHDR(Texture2D texture, EHDRTextureType type, int maxSize = -1, string path = null) { if (_texture2d2ImageID.ContainsKey(texture)) { return _texture2d2ImageID[texture]; } string format = ".png"; string[] pathes; if (path == null) { pathes = GetTextureOutPath(texture, format); } else { pathes = ExporterUtils.GetAssetOutPath(path, format); } var exportPath = pathes[0]; var pathInGlTF = pathes[1]; var newtex = GLTFTextureUtils.HDR2RGBD(texture); if (maxSize > 0 && (newtex.width > maxSize || newtex.height > maxSize)) { TextureScale.Bilinear(newtex, maxSize, maxSize); } byte[] content = newtex.EncodeToPNG(); return GenerateImage(content, exportPath, pathInGlTF); }
public void registerImageFromData(byte[] imageData, int imageID, string imageName = "") { Texture2D texture = new Texture2D(4, 4); texture.name = imageName; texture.LoadImage(imageData); GL.sRGBWrite = true; saveTexture(GLTFTextureUtils.flipTexture(texture), imageID); }
public Texture2D saveTexture(Texture2D texture, int index = -1, string imageName = "") { string basename = GLTFUtils.cleanName(texture.name + (index >= 0 ? "_" + index.ToString() : "") + ".png"); // Extension will be overridden string fullPath = Path.Combine(_importTexturesDirectory, basename); // Write texture string newAssetPath = GLTFTextureUtils.writeTextureOnDisk(texture, fullPath, true); // Reload as asset string projectPath = GLTFUtils.getPathProjectFromAbsolute(newAssetPath); Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(projectPath, typeof(Texture2D)); _parsedImages.Add(tex); return(tex); }
private Texture2D TextureFlipY(Texture2D texture, Func<Color, Color> convertColor = null, Action<Texture2D> processTexture = null) { Texture2D newTex; TextureImporter im = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(texture)) as TextureImporter; if (convertColor == null && (im == null || im.textureType != TextureImporterType.NormalMap)) { // use gpu to speed up newTex = GLTFTextureUtils.flipTexture(texture); } else { int height = texture.height; int width = texture.width; Color[] newTextureColors = new Color[height * width]; ExporterUtils.DoActionForTexture(ref texture, tex => { Color[] textureColors = tex.GetPixels(); for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { var c = textureColors[(height - i - 1) * width + j]; newTextureColors[i * width + j] = convertColor != null ? convertColor(c) : c; } } } ); newTex = new Texture2D(width, height); newTex.SetPixels(newTextureColors); newTex.Apply(); } if (processTexture != null) { processTexture(newTex); } return newTex; }
private static Texture2D CreateOcclusionMetallicRoughnessTexture( ref Texture2D metallic, ref Texture2D roughness, ref Texture2D occlusion ) { string texName = metallic.name + "_orm"; var textureM = metallic; var textureR = roughness; var textureAO = occlusion; string id = ""; if (metallic) { id += metallic.GetInstanceID(); } if (roughness) { id += roughness.GetInstanceID(); } if (occlusion) { id += occlusion.GetInstanceID(); } if (ExporterEntry.composedTextures.ContainsKey(id)) { return(ExporterEntry.composedTextures[id]); } var newTex = GLTFTextureUtils.packOcclusionMetalRough(metallic, roughness, occlusion, texName); newTex.filterMode = metallic.filterMode; newTex.wrapMode = metallic.wrapMode; ExporterEntry.composedTextures.Add(id, newTex); return(newTex); }
private static Texture2D SplitRoughnessTexture(Texture2D texture) { int width = texture.width; int height = texture.height; string assetPath = AssetDatabase.GetAssetPath(texture); TextureImporter im = AssetImporter.GetAtPath(assetPath) as TextureImporter; im.isReadable = true; im.SaveAndReimport(); var iColor = texture.GetPixels(); im.isReadable = false; im.SaveAndReimport(); // Let's consider that the three textures have the same resolution Color[] colors = new Color[width * height]; for (int i = 0; i < colors.Length; i += 1) { float a = 1 - iColor[i].a; colors[i] = new Color(a, a, a); } var res = new Texture2D(width, height); res.SetPixels(colors); string basename = Path.GetFileNameWithoutExtension(assetPath) + "_rg"; string fullPath = Path.GetFullPath(Path.GetDirectoryName(assetPath)) + "/" + basename + ".png"; string newAssetPath = GLTFTextureUtils.writeTextureOnDisk(res, fullPath, true); string projectPath = GLTFUtils.getPathProjectFromAbsolute(newAssetPath); Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(projectPath, typeof(Texture2D)); return(tex); }
public static string buildImageName(Texture2D image) { string extension = GLTFTextureUtils.useJPGTexture(image) ? ".jpg" : ".png"; return(image.GetInstanceID().ToString().Replace("-", "") + "_" + image.name + extension); }
public override void Serialize(ExporterEntry entry, Dictionary <string, Extension> extensions, UnityEngine.Object component = null, object options = null) { var mat = component as UnityEngine.Material; var hasReflection = ExporterSettings.Lighting.reflection && mat.GetInt("envReflection") != (int)SeinPBRShaderGUI.EnvReflection.Off; var hasLighting = RenderSettings.ambientMode == UnityEngine.Rendering.AmbientMode.Skybox || RenderSettings.ambientMode == UnityEngine.Rendering.AmbientMode.Trilight; if (!hasReflection && !hasLighting) { return; } if (entry.root.Extensions == null) { entry.root.Extensions = new Dictionary <string, Extension>(); } Sein_imageBasedLightingExtension globalExtension; if (!entry.root.Extensions.ContainsKey(ExtensionName)) { globalExtension = new Sein_imageBasedLightingExtension(); globalExtension.isGlobal = true; AddExtension(entry.root.Extensions, globalExtension); } else { globalExtension = (Sein_imageBasedLightingExtension)entry.root.Extensions[ExtensionName]; } var extension = new Sein_imageBasedLightingExtension(); if (hasLighting && !hasReflection && _onlyLightingId.ContainsKey(entry)) { extension.iblIndex = _onlyLightingId[entry]; extension.iblType = 1; AddExtension(extensions, extension); return; } var light = new Sein_imageBasedLightingExtension.Light { specMap = -1 }; var coefficients = new float[9][]; UnityEngine.Rendering.SphericalHarmonicsL2 shs; LightProbes.GetInterpolatedProbe(new UnityEngine.Vector3(), null, out shs); float diffuseIntensity = 1; if (shs != null) { for (var c = 0; c < 9; c += 1) { coefficients[c] = new float[3]; for (var b = 0; b < 3; b += 1) { coefficients[c][b] = shs[b, c]; } } } else { Debug.LogWarning("There is no baked light probe."); } SHToRightHand(coefficients); light.shCoefficients = coefficients; light.diffuseIntensity = diffuseIntensity; if (hasLighting && !hasReflection) { if (!_onlyLightingId.ContainsKey(entry)) { globalExtension.lights.Add(light); _onlyLightingId.Add(entry, globalExtension.lights.Count - 1); } extension.iblIndex = _onlyLightingId[entry]; extension.iblType = 1; AddExtension(extensions, extension); return; } var isCustomCubMap = RenderSettings.defaultReflectionMode == UnityEngine.Rendering.DefaultReflectionMode.Custom; //ReflectionProbe float specIntensity = RenderSettings.reflectionIntensity; int textureId = -1; int cacheId = 0; light.specType = "Cube"; if (isCustomCubMap) { var Id = entry.SaveCubeTexture(RenderSettings.customReflection, maxSize: ExporterSettings.Lighting.reflectionSize); cacheId = Id.GetHashCode(); textureId = Id.Id; } else { var skybox = RenderSettings.skybox; if (skybox == null) { Debug.LogWarning("Use skybox as relfection source, but skybox is not defined, ignore... Check 'http://seinjs.com/cn/tutorial/artist/reflection'"); return; } if (skybox.shader.name == "Skybox/Cubemap") { var cubemap = skybox.GetTexture("_Tex") as Cubemap; var Id = entry.SaveCubeTexture(cubemap, maxSize: ExporterSettings.Lighting.reflectionSize); cacheId = Id.GetHashCode(); textureId = Id.Id; } else if (skybox.shader.name == "Skybox/6 Sided") { var texes = new Texture2D[] { skybox.GetTexture("_RightTex") as Texture2D, skybox.GetTexture("_LeftTex") as Texture2D, skybox.GetTexture("_UpTex") as Texture2D, skybox.GetTexture("_DownTex") as Texture2D, skybox.GetTexture("_FrontTex") as Texture2D, skybox.GetTexture("_BackTex") as Texture2D }; var Id = entry.SaveCubeTexture(texes, maxSize: ExporterSettings.Lighting.reflectionSize); cacheId = Id.GetHashCode(); textureId = Id.Id; } else if (skybox.shader.name == "Skybox/Panoramic") { var map = skybox.GetTexture("_MainTex") as Texture2D; var fp = AssetDatabase.GetAssetPath(map); fp = fp.Replace(Path.GetExtension(fp), "-mipmaps" + Path.GetExtension(fp)); var Id = entry.SaveTexture(GLTFTextureUtils.generateMipmaps(map), false, maxSize: ExporterSettings.Lighting.reflectionSize, flipY: false, path: fp); textureId = Id.Id; cacheId = Id.GetHashCode(); light.specType = "2D"; light.specIncludeMipmaps = true; } else { Utils.ThrowExcption("Only support 'Skybox/Cubemap', 'Skybox/6 Side', 'Skybox/Panormic'"); } } if (_cache.ContainsKey(entry) && _cache[entry].ContainsKey(cacheId)) { extension.iblIndex = _cache[entry][cacheId]; AddExtension(extensions, extension); return; } light.specIntensity = specIntensity; light.specMap = textureId; light.brdfLUT = entry.SaveTexture(brdfLUT, false).Id; globalExtension.lights.Add(light); if (!_cache.ContainsKey(entry)) { _cache[entry] = new Dictionary <int, int>(); } _cache[entry].Add(cacheId, globalExtension.lights.Count - 1); extension.iblIndex = _cache[entry][cacheId]; extension.iblType = 2; AddExtension(extensions, extension); }