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 Id = entry.SaveTexture(map, false, maxSize: ExporterSettings.Lighting.reflectionSize, flipY: false); textureId = Id.Id; cacheId = Id.GetHashCode(); light.specType = "2D"; } 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); }
public override void Serialize(ExporterEntry entry, Dictionary <string, Extension> extensions, UnityEngine.Object component = null) { if (!ExporterSettings.Lighting.reflection) { return; } var mat = component as UnityEngine.Material; if (RenderSettings.ambientMode != UnityEngine.Rendering.AmbientMode.Skybox) { 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)extensions["KHR_lights_punctual"]; } var extension = new Sein_imageBasedLightingExtension(); Cubemap specMap = null; //ReflectionProbe float specIntensity = RenderSettings.reflectionIntensity; if (RenderSettings.defaultReflectionMode == UnityEngine.Rendering.DefaultReflectionMode.Custom) { specMap = RenderSettings.customReflection; } else { //todo: support skybox cubemap } if (_cache.ContainsKey(specMap)) { extension.iblIndex = _cache[specMap]; AddExtension(extensions, extension); return; } var light = new Sein_imageBasedLightingExtension.Light(); var coefficients = new float[9][]; UnityEngine.Rendering.SphericalHarmonicsL2 shs; LightProbes.GetInterpolatedProbe(new UnityEngine.Vector3(), null, out shs); float diffuseIntensity = RenderSettings.ambientIntensity; 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."); } light.shCoefficients = coefficients; light.diffuseIntensity = diffuseIntensity; light.brdfLUT = entry.SaveTexture(brdfLUT, false).Id; light.specIntensity = specIntensity; light.specMapFaces = new int[6]; string origAssetPath = AssetDatabase.GetAssetPath(specMap); string ext = Path.GetExtension(origAssetPath); var blurredSpecMap = GetSpecularCubeMap(specMap); for (var i = 0; i < 6; i += 1) { var tex2d = new Texture2D(blurredSpecMap.width, blurredSpecMap.height, TextureFormat.RGBAHalf, false); UnityEngine.Color[] colors = null; switch (i) { case 0: colors = blurredSpecMap.GetPixels(CubemapFace.PositiveX); break; case 1: colors = blurredSpecMap.GetPixels(CubemapFace.NegativeX); break; case 2: colors = blurredSpecMap.GetPixels(CubemapFace.PositiveY); break; case 3: colors = blurredSpecMap.GetPixels(CubemapFace.NegativeY); break; case 4: colors = blurredSpecMap.GetPixels(CubemapFace.PositiveZ); break; case 5: colors = blurredSpecMap.GetPixels(CubemapFace.NegativeZ); break; } tex2d.SetPixels(colors); tex2d.Apply(); light.specMapFaces[i] = entry.SaveImageHDR(tex2d, ExporterSettings.Lighting.reflectionType, ExporterSettings.Lighting.reflectionSize, origAssetPath.Replace(ext, "-" + i + ext)).Id; } DeleteTempMap(blurredSpecMap); globalExtension.lights.Add(light); extension.iblIndex = globalExtension.lights.Count - 1; if (mat.HasProperty("envReflection")) { extension.iblType = mat.GetInt("envReflection"); } else { extension.iblType = 2; } AddExtension(extensions, extension); }