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:
But for some reason it only captures the "hair", this is the image saved:
I just want to save the front face of my model
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:
I just want to save the front face of my model
Last edited: