- Home /
Script rendering Mesh to Image with transparent background
Hi,
I have a dynamically generated but static (after generation) mesh. The mesh has a material with an unlit shader using the alpha channel. Is it possible to render this mesh to an image with transparent background? The process should happen offscreen.
Answer by guggisberg8 · Apr 09, 2021 at 11:48 AM
I found the answer:
Here:
https://docs.unity3d.com/Manual/class-RenderTexture.html https://answers.unity.com/questions/22954/how-to-save-a-picture-take-screenshot-from-a-camer.html
Create a render texture
Attach render texture to a camera, which should only capture the object / mesh
Set Camera to only render a specific layer
On capture (eg k press) move camera infront of specific game object and move game object to cameras specific render layer.
Capture image given code below / answer links.
(If unsused) dispose of Destroy(screenShot )
private void Start() { captureCamera.enabled = false; } void LateUpdate() { capture |= Input.GetKeyDown("k"); if (capture) { captureCamera.Render(); RenderTexture.active = captureCamera.targetTexture; Texture2D screenShot = new Texture2D(screenShotWidth, screenShotHeight, TextureFormat.RGBA32, false); screenShot.ReadPixels(new Rect(0, 0, screenShotWidth, screenShotHeight), 0, 0); RenderTexture.active = null; screenShot.Apply(); byte[] bytes = screenShot.EncodeToPNG(); System.IO.File.WriteAllBytes(ScreenShotName(screenShotWidth, screenShotHeight), bytes); Debug.Log(string.Format(ScreenShotName(screenShotWidth, screenShotHeight))); capture = false; } }
Your answer
Follow this Question
Related Questions
Performance drop with transparent objecs overlying big meshes 1 Answer
Procedural model turns black up close 0 Answers
Stencil buffer and transparency 0 Answers
Is it possible to render a mesh from code? 2 Answers
Static batching changes vertex colors? 0 Answers