• Hi Guest!

    We are extremely excited to announce the release of our first Beta for VaM2, the next generation of Virt-A-Mate which is currently in development.
    To participate in the Beta, a subscription to the Entertainer or Creator Tier is required. Once subscribed, download instructions can be found here.

    Click here for information and guides regarding the VaM2 beta. Join our Discord server for more announcements and community discussion about VaM2.

How to take a snapshot?

henshin

Member
Joined
Dec 29, 2023
Messages
155
Reactions
22
I want to take a picture of the face of my person and save it as a .png file
Is there any recommendation (or plugin to see the code) to do this?

EDIT: this is what I have:

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using MVR.FileManagementSecure;

namespace HenshinNamespace
{
    public class CaptureFace : MVRScript
    {
        private Camera customCamera;

        public override void Init()
        {
            pluginLabelJSON.val = "CaptureFace TEST 1.0";

            CreateCustomCamera();
            SuperController.LogMessage("CreateCustomCamera OK");

        }


        // Runs once when plugin loads - after Init()
        protected void Start()
        {
            // show a message
            SuperController.LogMessage(pluginLabelJSON.val + " Loaded");
        }

        // A Unity thing - runs every physics cycle
        public void FixedUpdate()
        {
            // put code here
        }


        private void CreateCustomCamera()
        {
            if (containingAtom == null)
            {
                SuperController.LogMessage("containingAtom null");
                return;
            }

            var headControl = containingAtom.GetStorableByID("headControl")?.transform;
            if (headControl == null)
            {
                SuperController.LogMessage("headControl null");
                return;
            }

            GameObject cameraObject = new GameObject("CustomCaptureCamera");
            customCamera = cameraObject.AddComponent<Camera>();


            Vector3 cameraPosition = headControl.position + headControl.up * 1f - headControl.right * 1f;
            cameraObject.transform.position = cameraPosition;
            cameraObject.transform.LookAt(headControl.position - headControl.up * 0.5f);

            customCamera.fieldOfView = 35;
        }


        public void CaptureFrontFace(int width = 1024, int height = 1024, string filename = "FaceCapture.png")
        {

            Camera faceCamera = customCamera;

            if (faceCamera == null)
            {
                Debug.LogError("Face camera is not assigned!");
                return;
            }

            RenderTexture rt = new RenderTexture(width, height, 24);
            faceCamera.targetTexture = rt;

            RenderTexture previousRT = RenderTexture.active;
            RenderTexture.active = rt;
            faceCamera.Render();

            Texture2D screenshot = new Texture2D(width, height, TextureFormat.RGB24, false);
            screenshot.ReadPixels(new Rect(0, 0, width, height), 0, 0);
            screenshot.Apply();

            RenderTexture.active = previousRT;
            faceCamera.targetTexture = null;
            Destroy(rt);

            string savePath = "C:/VAM/Saves/";

            if (!FileManagerSecure.DirectoryExists(savePath)){
                FileManagerSecure.CreateDirectory(savePath);
            }

            string fullPath = savePath + filename;
            FileManagerSecure.WriteAllBytes(fullPath, screenshot.EncodeToPNG());

            Debug.Log("Image saved at: " + fullPath);
        }

        public void CaptureFaceScreenshot()
        {
            if (customCamera == null)
            {
                SuperController.LogMessage("La cámara personalizada no está configurada.");
                return;
            }

            Camera mainCamera = Camera.main;
            if (mainCamera != null)
            {
                mainCamera.enabled = false;
            }
            customCamera.enabled = true;

            CaptureFrontFace(512, 512, "TestFaceCapture.png");

            if (mainCamera != null)
            {
                mainCamera.enabled = true;
            }
            customCamera.enabled = false;

            SuperController.LogMessage("Captura de pantalla realizada.");
        }

        // Unity thing - runs every rendered frame
        public void Update()
        {


            // Capturar la pantalla al presionar la tecla "C"
            if (Input.GetKeyDown(KeyCode.C))
            {
                CaptureFaceScreenshot();
            }


        }


    }
}


But for some reason it only captures the "hair", this is the image saved:

1732415509832.png


I just want to save the front face of my model :(
 
Last edited:
Or we need all those things from SuperShot to take a simple screenshot?
SuperShot does the screenshot with higher resolution and then uses the GPU to downscale it before saving the file. That results is MUCH better image quality. If you don't need/want that, you could just trigger VaMs build in screenshot mechanism instead.

The reason why your method doesn't work might be that you explicitly call Render() on the camera. Both SuperShot as well as VaM's build in tool just enable the camera and then wait for the next frame. I vaguely remember it has something to do with some effect magic VaM is doing internally. Sry, SuperShot is a 5 year old plugin but this point, don't remember the details why that was needed 🤷‍♂️

Another thing you could do is just use SuperShot. It has trigger support, you could just trigger it from your plugin.
 
Back
Top Bottom