SceneUtils.cs
5.27 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.XR.ARFoundation;
namespace UnityEditor.XR.ARFoundation
{
internal static class SceneUtils
{
static readonly string k_DebugFaceMaterial = "Packages/com.unity.xr.arfoundation/Materials/DebugFace.mat";
static readonly string k_DebugPlaneMaterial = "Packages/com.unity.xr.arfoundation/Materials/DebugPlane.mat";
static readonly string k_ParticleMaterial = "Default-Particle.mat";
static readonly string k_LineMaterial = "Default-Line.mat";
static readonly Color k_ParticleColor = new Color(253f / 255f, 184f / 255f, 19f / 255f);
static readonly float k_ParticleSize = 0.02f;
[MenuItem("GameObject/XR/AR Session Origin", false, 10)]
static void CreateARSessionOrigin()
{
var originGo = ObjectFactory.CreateGameObject("AR Session Origin", typeof(ARSessionOrigin));
var cameraGo = ObjectFactory.CreateGameObject("AR Camera",
typeof(Camera),
typeof(ARPoseDriver),
typeof(ARCameraManager),
typeof(ARCameraBackground));
Undo.SetTransformParent(cameraGo.transform, originGo.transform, "Parent camera to session origin");
var camera = cameraGo.GetComponent<Camera>();
// Enforce local transform as identity for new ARSessionOrigins
camera.transform.localPosition = Vector3.zero;
camera.transform.localRotation = Quaternion.identity;
camera.clearFlags = CameraClearFlags.Color;
camera.backgroundColor = Color.black;
camera.nearClipPlane = 0.1f;
camera.farClipPlane = 20f;
var origin = originGo.GetComponent<ARSessionOrigin>();
origin.camera = camera;
}
[MenuItem("GameObject/XR/AR Session", false, 10)]
static void CreateARSession()
{
ObjectFactory.CreateGameObject("AR Session", typeof(ARSession), typeof(ARInputManager));
}
[MenuItem("GameObject/XR/AR Default Point Cloud", false, 10)]
static void CreateARPointCloudVisualizer()
{
var go = ObjectFactory.CreateGameObject("AR Default Point Cloud",
typeof(ARPointCloudParticleVisualizer));
var particleSystem = go.GetComponent<ParticleSystem>();
UnityEditorInternal.ComponentUtility.MoveComponentDown(particleSystem);
UnityEditorInternal.ComponentUtility.MoveComponentDown(particleSystem);
var main = particleSystem.main;
main.loop = false;
main.startSize = k_ParticleSize;
main.startColor = k_ParticleColor;
main.scalingMode = ParticleSystemScalingMode.Hierarchy;
main.playOnAwake = false;
var emission = particleSystem.emission;
emission.enabled = false;
var shape = particleSystem.shape;
shape.enabled = false;
var renderer = particleSystem.GetComponent<Renderer>();
renderer.material = AssetDatabase.GetBuiltinExtraResource<Material>(k_ParticleMaterial);
}
[MenuItem("GameObject/XR/AR Default Plane", false, 10)]
static void CreateARPlaneVisualizer()
{
var go = ObjectFactory.CreateGameObject("AR Default Plane",
typeof(ARPlaneMeshVisualizer), typeof(MeshCollider), typeof(MeshFilter),
typeof(MeshRenderer), typeof(LineRenderer));
SetupMeshRenderer(go.GetComponent<MeshRenderer>(), k_DebugPlaneMaterial);
SetupLineRenderer(go.GetComponent<LineRenderer>());
}
[MenuItem("GameObject/XR/AR Default Face", false, 10)]
static void CreateARFaceVisualizer()
{
var go = ObjectFactory.CreateGameObject("AR Default Face",
typeof(ARFaceMeshVisualizer), typeof(MeshCollider), typeof(MeshFilter),
typeof(MeshRenderer));
var meshRenderer = go.GetComponent<MeshRenderer>();
SetupMeshRenderer(meshRenderer, k_DebugFaceMaterial);
//self shadowing doesn't look good on the default face
meshRenderer.receiveShadows = false;
meshRenderer.shadowCastingMode = ShadowCastingMode.Off;
}
static void SetupLineRenderer(LineRenderer lineRenderer)
{
var materials = new Material[1];
materials[0] = AssetDatabase.GetBuiltinExtraResource<Material>(k_LineMaterial);
lineRenderer.materials = materials;
lineRenderer.loop = true;
var curve = new AnimationCurve();
curve.AddKey(0f, 0.005f);
lineRenderer.widthCurve = curve;
lineRenderer.startColor = Color.black;
lineRenderer.endColor = Color.black;
lineRenderer.numCornerVertices = 4;
lineRenderer.numCapVertices = 4;
lineRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
lineRenderer.receiveShadows = false;
lineRenderer.useWorldSpace = false;
}
static void SetupMeshRenderer(MeshRenderer meshRenderer, string materialName)
{
var material = AssetDatabase.LoadAssetAtPath<Material>(materialName);
meshRenderer.materials = new Material[] { material };
}
}
}