Last active
March 12, 2023 00:51
-
-
Save negipoyoc/71c894eccb0cae442a6007408edc700f to your computer and use it in GitHub Desktop.
AI学習用データの作成向けに、1つのポーズに対して複数の角度から撮影するためのスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.IO; | |
using Negipoyoc.SS; | |
using UnityEngine; | |
/// <summary> | |
/// ポーズそれぞれに対して分割数を指定して撮影するスクリプト | |
/// </summary> | |
public class RollCamera : MonoBehaviour | |
{ | |
/// <summary> | |
/// 基準となる高さ | |
/// </summary> | |
private float y = 0.825f; | |
/// <summary> | |
/// カメラとキャラクターの距離 | |
/// </summary> | |
private float zRadius = 5; | |
/// <summary> | |
/// 開始角度 | |
/// </summary> | |
private float start = -40; | |
/// <summary> | |
/// 終了角度 | |
/// </summary> | |
private float goal = 40; | |
/// <summary> | |
/// 現在の角度のキャッシュ | |
/// </summary> | |
private float _degreeCache; | |
/// <summary> | |
/// 1つのポーズに対して角度をで撮影するか? | |
/// | |
/// </summary> | |
[SerializeField] private int step = 4; | |
/// <summary> | |
/// 撮影する対象の位置(キャラクターの胸のあたりの位置が良いので、(0,1.2f,0)など) | |
/// </summary> | |
[SerializeField] private Vector3 targetPosition; | |
/// <summary> | |
/// スクショの保存場所 | |
/// </summary> | |
[SerializeField] private string saveDir; | |
/// <summary> | |
/// キャラクターのポーズ用AnimationClip | |
/// </summary> | |
[SerializeField] private AnimationClip[] clips; | |
/// <summary> | |
/// キャラクターのAnimator | |
/// </summary> | |
[SerializeField] private Animator animator; | |
private void Start() | |
{ | |
_degreeCache = start; | |
StartCoroutine(RotatePhoto()); | |
} | |
private IEnumerator RotatePhoto() | |
{ | |
var fileNameIndex = 0; | |
foreach (var clip in clips) | |
{ | |
Debug.Log($"Will play:{clip.name}"); | |
animator.Play(clip.name); | |
yield return new WaitForSeconds(1f); | |
for (var i = 0; i < step; i++) | |
{ | |
_degreeCache = Mathf.Lerp(start, goal, (float) i / (step - 1)); | |
var theta = Mathf.Deg2Rad * (_degreeCache + 90); | |
var x = Mathf.Cos(theta); | |
var z = Mathf.Sin(theta); | |
transform.position = new Vector3(zRadius * x, y + 1f, zRadius * z); | |
transform.LookAt(targetPosition); | |
yield return CaptureWithAlpha(saveDir, fileNameIndex.ToString("0000")); | |
yield return new WaitForSeconds(0.3f); | |
fileNameIndex++; | |
} | |
} | |
} | |
private IEnumerator CaptureWithAlpha(string folderPath, string fileName) | |
{ | |
yield return new WaitForEndOfFrame(); | |
var tex = ScreenCapture.CaptureScreenshotAsTexture(); | |
var width = tex.width; | |
var height = tex.height; | |
var texAlpha = new Texture2D(width, height, TextureFormat.ARGB32, false); | |
// Read screen contents into the texture | |
texAlpha.ReadPixels(new Rect(0, 0, width, height), 0, 0); | |
texAlpha.Apply(); | |
// Encode texture into PNG | |
var bytes = texAlpha.EncodeToPNG(); | |
Destroy(tex); | |
File.WriteAllBytes(folderPath + "/" + fileName + ".png", bytes); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment