private IEnumerator ConvertLightmapTexture(Texture2D src, LightmapTextureImporterResult result) { var importerShader = GetImporterShader(); if (importerShader == null) { result.Result = null; yield break; } var importerMaterial = new Material(importerShader); var rt = RenderTexture.GetTemporary(src.width, src.height, 0, RenderTextureFormat.DefaultHDR, RenderTextureReadWrite.Linear); // Decode into Linear RenderTexture Graphics.Blit(src, rt, importerMaterial); yield return(null); // Create Linear Texture2D with settings var dst = new Texture2D(rt.width, rt.height, TextureFormat.RGBAHalf, mipCount: src.mipmapCount, linear: true); dst.name = $"{src.name}_decoded"; dst.mipMapBias = src.mipMapBias; dst.wrapMode = src.wrapMode; dst.wrapModeU = src.wrapModeU; dst.wrapModeV = src.wrapModeV; dst.wrapModeW = src.wrapModeW; dst.filterMode = src.filterMode; // Copy to Linear Texture2D var tmpActive = RenderTexture.active; RenderTexture.active = rt; dst.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0); dst.Apply(updateMipmaps: true, makeNoLongerReadable: true); RenderTexture.active = tmpActive; yield return(null); RenderTexture.ReleaseTemporary(rt); UnityEngine.Object.DestroyImmediate(importerMaterial); result.Result = dst; }
public IEnumerator GetOrConvertColorTextureCoroutine(int colorTextureGltfIndex, LightmapTextureImporterResult result) { var textureItem = _textureGetter.Invoke(colorTextureGltfIndex); if (textureItem.Converts.ContainsKey(ConvertedTexKey)) { result.Result = textureItem.Converts[ConvertedTexKey]; yield break; } yield return(ConvertLightmapTexture(textureItem.Texture, result)); if (result.Result == null) { yield break; } textureItem.Converts.Add("LightmapLinearTexture", result.Result); }