private void ProcessLightmapsUnpacked() { ExportTextureFormat format = room.TextureFormat; if (room.ExportOnlyHtml) { foreach (var lightPair in lightmapped) { int id = lightPair.Key; List <RoomObject> toRender = lightPair.Value; for (int i = 0; i < toRender.Count; i++) { RoomObject rObj = toRender[i]; string imgId = rObj.id + "_Baked"; UpdateTextureFormat(format, room.GetTexture(imgId), ""); } } return; } string lightMapsFolder = UnityUtil.GetLightmapsFolder(); Shader lightMapShader = Shader.Find("Hidden/LMapUnpacked"); JanusUtil.AssertShader(lightMapShader); Material lightMap = new Material(lightMapShader); lightMap.SetPass(0); lightMap.SetFloat("_RelFStops", room.LightmapRelFStops); lightMap.SetFloat("_IsLinear", PlayerSettings.colorSpace == ColorSpace.Linear ? 1 : 0); // export lightmaps int lmap = 0; foreach (var lightPair in lightmapped) { int id = lightPair.Key; List <RoomObject> toRender = lightPair.Value; // get the path to the lightmap file string lightMapFile = Path.Combine(lightMapsFolder, "Lightmap-" + id + "_comp_light.exr"); Texture2D texture = AssetDatabase.LoadAssetAtPath <Texture2D>(lightMapFile); if (texture == null) { continue; } lightMap.SetTexture("_LightMapTex", texture); for (int i = 0; i < toRender.Count; i++) { RoomObject rObj = toRender[i]; GameObject obj = rObj.UnityObj; MeshRenderer renderer = obj.GetComponent <MeshRenderer>(); MeshFilter filter = obj.GetComponent <MeshFilter>(); Mesh mesh = filter.sharedMesh; Transform trans = obj.transform; Matrix4x4 world = Matrix4x4.TRS(trans.position, trans.rotation, trans.lossyScale); Vector4 scaleOffset = renderer.lightmapScaleOffset; float width = (1 - scaleOffset.z) * scaleOffset.x; float height = (1 - scaleOffset.w) * scaleOffset.y; float size = Math.Max(width, height); int lightMapSize = (int)(room.LightmapMaxResolution * size); lightMapSize = (int)Math.Pow(2, Math.Ceiling(Math.Log(lightMapSize) / Math.Log(2))); lightMapSize = Math.Min(room.LightmapMaxResolution, Math.Max(lightMapSize, 16)); RenderTexture renderTexture = RenderTexture.GetTemporary(lightMapSize, lightMapSize, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB); Graphics.SetRenderTarget(renderTexture); GL.Clear(true, true, new Color(0, 0, 0, 0)); // clear to transparent Material[] mats = renderer.sharedMaterials; lightMap.SetVector("_LightMapUV", renderer.lightmapScaleOffset); for (int j = 0; j < mats.Length; j++) { //Material mat = mats[j]; lightMap.SetPass(0); Graphics.DrawMeshNow(mesh, world, j); } // This is the only way to access data from a RenderTexture Texture2D tex = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32, false, false); tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0); string imgId = rObj.id + "_Baked"; tex.name = imgId; tex.Apply(); // send the data back to the GPU so we can draw it on the preview area Graphics.SetRenderTarget(null); RenderTexture.ReleaseTemporary(renderTexture); lmap++; // save the texture file ExportTexture(tex, format, room.GetTexture(rObj.image_id), false); UObject.DestroyImmediate(tex); } } UObject.DestroyImmediate(lightMap); }
private void ProcessLightmapsBaked() { ExportTextureFormat format = room.TextureFormat; if (room.ExportOnlyHtml) { foreach (var lightPair in lightmapped) { int id = lightPair.Key; List <RoomObject> toRender = lightPair.Value; for (int i = 0; i < toRender.Count; i++) { RoomObject rObj = toRender[i]; string imgId = rObj.id + "_Baked"; UpdateTextureFormat(format, room.GetTexture(imgId), ""); } } return; } string lightMapsFolder = UnityUtil.GetLightmapsFolder(); // only load shader now, so if the user is not exporting lightmaps // he doesn't need to have it on his project folder Shader lightMapShader = Shader.Find("Hidden/LMapBaked"); JanusUtil.AssertShader(lightMapShader); Material lightMap = new Material(lightMapShader); lightMap.SetPass(0); lightMap.SetFloat("_RelFStops", room.LightmapRelFStops); lightMap.SetFloat("_IsLinear", PlayerSettings.colorSpace == ColorSpace.Linear ? 1 : 0); // export lightmaps int lmap = 0; foreach (var lightPair in lightmapped) { int id = lightPair.Key; List <RoomObject> toRender = lightPair.Value; // get the path to the lightmap file string lightMapFile = Path.Combine(lightMapsFolder, "Lightmap-" + id + "_comp_light.exr"); Texture2D texture = AssetDatabase.LoadAssetAtPath <Texture2D>(lightMapFile); if (texture == null) { continue; } lightMap.SetTexture("_LightMapTex", texture); for (int i = 0; i < toRender.Count; i++) { RoomObject rObj = toRender[i]; GameObject obj = rObj.UnityObj; MeshRenderer renderer = obj.GetComponent <MeshRenderer>(); MeshFilter filter = obj.GetComponent <MeshFilter>(); Mesh mesh = filter.sharedMesh; Transform trans = obj.transform; Matrix4x4 world = Matrix4x4.TRS(trans.position, trans.rotation, trans.lossyScale); Vector4 scaleOffset = renderer.lightmapScaleOffset; float width = (1 - scaleOffset.z) * scaleOffset.x; float height = (1 - scaleOffset.w) * scaleOffset.y; float size = Math.Max(width, height); // guarantee were not scaling stuff up int maxLmapRes = Math.Min(room.LightmapMaxResolution, texture.width); int lightMapSize = (int)(maxLmapRes * size); lightMapSize = (int)Math.Pow(2, Math.Ceiling(Math.Log(lightMapSize) / Math.Log(2))); lightMapSize = Math.Min(maxLmapRes, Math.Max(lightMapSize, 16)); RenderTexture renderTexture = RenderTexture.GetTemporary(lightMapSize, lightMapSize, 0, RenderTextureFormat.ARGB32); Graphics.SetRenderTarget(renderTexture); GL.Clear(true, true, new Color(0, 0, 0, 0)); // clear to transparent Material[] mats = renderer.sharedMaterials; for (int j = 0; j < mats.Length; j++) { Material mat = mats[j]; // clear to default lightMap.SetTexture("_MainTex", EditorGUIUtility.whiteTexture); lightMap.SetColor("_Color", Color.white); // uvs lightMap.SetVector("_LightMapUV", renderer.lightmapScaleOffset); Shader shader = mat.shader; int props = ShaderUtil.GetPropertyCount(shader); for (int k = 0; k < props; k++) { string name = ShaderUtil.GetPropertyName(shader, k); ShaderUtil.ShaderPropertyType propType = ShaderUtil.GetPropertyType(shader, k); if (propType == ShaderUtil.ShaderPropertyType.TexEnv) { if (JanusGlobals.SemanticsMainTex.Contains(name.ToLower())) { // main texture texture Texture matTex = mat.GetTexture(name); if (matTex) { lightMap.SetTexture("_MainTex", matTex); } } } else if (propType == ShaderUtil.ShaderPropertyType.Color) { if (JanusGlobals.SemanticsColor.Contains(name.ToLower())) { lightMap.SetColor("_Color", mat.GetColor(name)); } } } lightMap.SetPass(0); Graphics.DrawMeshNow(mesh, world, j); } // This is the only way to access data from a RenderTexture Texture2D tex = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32, false, false); tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0); string imgId = rObj.id + "_Baked"; tex.name = imgId; Graphics.SetRenderTarget(null); RenderTexture.ReleaseTemporary(renderTexture); lmap++; // save the texture file ExportTexture(tex, format, room.GetTexture(rObj.image_id), false); UObject.DestroyImmediate(tex); } } UObject.DestroyImmediate(lightMap); }
private void ProcessLightmapsPacked() { ExportTextureFormat format = room.TextureFormat; if (room.ExportOnlyHtml) { foreach (var lightPair in lightmapped) { int id = lightPair.Key; string imgId = "Lightmap" + id; UpdateTextureFormat(format, room.GetTexture(imgId), ""); } return; } string lightMapsFolder = UnityUtil.GetLightmapsFolder(); Shader exposureShader = Shader.Find("Hidden/ExposureShader"); JanusUtil.AssertShader(exposureShader); Material exposureMat = new Material(exposureShader); exposureMat.SetPass(0); exposureMat.SetFloat("_RelFStops", room.LightmapRelFStops); exposureMat.SetFloat("_IsLinear", PlayerSettings.colorSpace == ColorSpace.Linear ? 1 : 0); foreach (var lightPair in lightmapped) { int id = lightPair.Key; AssetImage lmapImage = room.TryGetTexture("Lightmap" + id); if (lmapImage == null) { continue; } // get the path to the lightmap file string lightMapFile = Path.Combine(lightMapsFolder, "Lightmap-" + id + "_comp_light.exr"); Texture2D texture = AssetDatabase.LoadAssetAtPath <Texture2D>(lightMapFile); if (texture == null) { continue; } exposureMat.SetTexture("_InputTex", texture); // We need to access unity_Lightmap_HDR to decode the lightmap, // but we can't, so we have to render everything to a custom RenderTexture! Texture2D decTex = new Texture2D(texture.width, texture.height); decTex.name = "Lightmap" + id; //texturesExported.Add(decTex); RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height); Graphics.SetRenderTarget(renderTexture); GL.Clear(true, true, new Color(0, 0, 0, 0)); // clear to transparent exposureMat.SetPass(0); Graphics.DrawMeshNow(JanusResources.PlaneMesh, Matrix4x4.identity); decTex.ReadPixels(new Rect(0, 0, decTex.width, decTex.height), 0, 0); Graphics.SetRenderTarget(null); RenderTexture.ReleaseTemporary(renderTexture); // save the lightmap file ExportTexture(decTex, format, lmapImage, false); UObject.DestroyImmediate(decTex); } UObject.DestroyImmediate(exposureMat); }