public static void Run() { try { // ExStart:RenderSceneIntoCubemapwithsixfaces //load the scene Scene scene = new Scene(RunExamples.GetDataFilePath("VirtualCity.glb")); //create a camera for capturing the cube map Camera cam = new Camera(ProjectionType.Perspective) { NearPlane = 0.1, FarPlane = 200, RotationMode = RotationMode.FixedDirection }; scene.RootNode.CreateChildNode(cam).Transform.Translation = new Vector3(5, 6, 0); //create two lights to illuminate the scene scene.RootNode.CreateChildNode(new Light() { LightType = LightType.Point }).Transform.Translation = new Vector3(-10, 7, -10); scene.RootNode.CreateChildNode(new Light() { Color = new Vector3(Color.CadetBlue) }).Transform.Translation = new Vector3(49, 0, 49); //create a renderer using (var renderer = Renderer.CreateRenderer()) { //create a cube map render target with depth texture, depth is required when rendering a scene. IRenderTexture rt = renderer.RenderFactory.CreateCubeRenderTexture(new RenderParameters(false), 512, 512); //a viewport is required on the render target rt.CreateViewport(cam, RelativeRectangle.FromScale(0, 0, 1, 1)); renderer.Render(rt); //now lets get the cubemap texture ITextureCubemap cubemap = rt.Targets[0] as ITextureCubemap; //we can directly save each face to disk by specifing the file name CubeFaceData <string> fileNames = new CubeFaceData <string>() { Right = RunExamples.GetOutputFilePath("right.png"), Left = RunExamples.GetOutputFilePath("left.png"), Back = RunExamples.GetOutputFilePath("back.png"), Front = RunExamples.GetOutputFilePath("front.png"), Bottom = RunExamples.GetOutputFilePath("bottom.png"), Top = RunExamples.GetOutputFilePath("top.png") }; //and call Save method cubemap.Save(fileNames, ImageFormat.Png); //or we just need to use the render result in memory, we can save it to CubeFaceData<Bitmap> //CubeFaceData<Bitmap> bitmaps = new CubeFaceData<Bitmap>(); //cubemap.Save(bitmaps); //bitmaps.Back.Save("back.bmp", ImageFormat.Bmp); } // ExEnd:RenderSceneIntoCubemapwithsixfaces } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private Texture RenderCubemapToTexture(Cubemap cubemap, int faceSize, Color clearColor, TextureFormat textureFormat, bool exportFaces, string assetPathPrefix, bool alphaIsTransparency) { CubeFaceData[] faces = { new CubeFaceData(CubemapFace.NegativeX, new Vector2(0, faceSize)), new CubeFaceData(CubemapFace.PositiveX, new Vector2(faceSize * 2, faceSize)), new CubeFaceData(CubemapFace.PositiveY, new Vector2(faceSize, faceSize * 2)), new CubeFaceData(CubemapFace.NegativeY, new Vector2(faceSize, 0)), new CubeFaceData(CubemapFace.PositiveZ, new Vector2(faceSize, faceSize)), new CubeFaceData(CubemapFace.NegativeZ, new Vector2(faceSize * 3, faceSize)) }; RenderTexture oldRt = RenderTexture.active; Texture2D faceTex = new Texture2D(faceSize, faceSize, TextureFormat.RGBA32, false); RenderTexture faceRenderTex = new RenderTexture(faceSize, faceSize, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB); faceRenderTex.Create(); RenderTexture flatCubeTex = new RenderTexture(faceSize * 4, faceSize * 3, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB); flatCubeTex.Create(); Material flipMat = new Material(Shader.Find("Funly/Sky Studio/Utility/Flip Image")); Material blitMat = new Material(Shader.Find("Unlit/Transparent")); RenderTexture.active = flatCubeTex; GL.PushMatrix(); GL.LoadPixelMatrix(0, flatCubeTex.width, flatCubeTex.height, 0); GL.Clear(true, true, new Color(0.0f, 0.0f, 0.0f, 0.0f)); GL.PopMatrix(); RenderTexture.active = null; for (int i = 0; i < faces.Length; i++) { CubeFaceData data = faces[i]; // Pull a face texture out of the cubemap. Color[] pixels = cubemap.GetPixels(data.face); faceTex.SetPixels(pixels); faceTex.Apply(); // Flip the image. RenderTexture.active = faceRenderTex; GL.PushMatrix(); GL.LoadPixelMatrix(0, faceRenderTex.width, flatCubeTex.height, 0); Graphics.Blit(faceTex, faceRenderTex, flipMat, 0); GL.PopMatrix(); if (exportFaces) { string path = assetPathPrefix + data.face.ToString() + ".png"; SkyEditorUtility.WriteTextureToFile(faceRenderTex, path, textureFormat); RenderTexture.active = null; AssetDatabase.ImportAsset(path); } RenderTexture.active = null; // Target our cubemap, and render the flipped face into it. RenderTexture.active = flatCubeTex; GL.PushMatrix(); GL.LoadPixelMatrix(0, flatCubeTex.width, flatCubeTex.height, 0); Graphics.DrawTexture(new Rect(data.offset.x, data.offset.y, faceSize, faceSize), faceRenderTex, blitMat); // Write the final texture on last face. if (i == faces.Length - 1) { string path = assetPathPrefix + ".png"; Debug.Log("Exporting cubemap to compressed texture at path: " + path); SkyEditorUtility.WriteTextureToFile(flatCubeTex, path, textureFormat); RenderTexture.active = null; AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate); // Adjust texture settings. TextureImporter importer = TextureImporter.GetAtPath(path) as TextureImporter; importer.textureShape = TextureImporterShape.TextureCube; importer.alphaIsTransparency = alphaIsTransparency; AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate); } GL.PopMatrix(); RenderTexture.active = null; } RenderTexture.active = oldRt; return(flatCubeTex); }