-
Notifications
You must be signed in to change notification settings - Fork 1
/
MyCameraRandomizerTag.cs
39 lines (33 loc) · 1.67 KB
/
MyCameraRandomizerTag.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
using UnityEngine;
using UnityEngine.Perception.Randomization.Parameters;
using UnityEngine.Perception.Randomization.Randomizers;
using UnityEngine.Perception.Randomization.Samplers;
// Add this Component to any GameObject that you would like to be randomized. This class must have an identical name to
// the .cs file it is defined in.
public class MyCameraRandomizerTag : RandomizerTag { }
[Serializable]
// This attribute adds a menu entry in the Unity Editor for this randomizer.
[AddRandomizerMenu("MyCameraRandomizer")]
public class MyCameraRandomizer : Randomizer
{
// Parameters controlling the camera randomization.
// The values of these parameters will be sampled during randomization.
public FloatParameter cameraRotateX = new() { value = new UniformSampler(0, 1) };
public FloatParameter cameraDistance = new() { value = new UniformSampler(0, 1) };
// Reference to the camera that will be manipulated.
public Camera myCamera;
// Run this every randomization iteration
protected override void OnIterationStart()
{
// Sample a random elevation and distance.
float elevation = cameraRotateX.Sample();
float distance = cameraDistance.Sample();
// Calculate the camera's new position in a spherical coordinate system.
float z = -distance * Mathf.Cos(elevation * Mathf.PI / 180);
float y = distance * Mathf.Sin(elevation * Mathf.PI / 180);
// Update the camera's rotation and position based on the sampled values.
myCamera.transform.rotation = Quaternion.Euler(elevation, 0, 0);
myCamera.transform.position = new Vector3(0, y, z);
}
}