Tanoda
LeapDynamicRenderer.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using System.Collections.Generic;
11using UnityEngine;
12using UnityEngine.Assertions;
13using Leap.Unity.Space;
14
16
17 [LeapGraphicTag("Dynamic")]
18 [Serializable]
20 public const string DEFAULT_SHADER = "LeapMotion/GraphicRenderer/Unlit/Dynamic Transparent";
21
22 #region PRIVATE VARIABLES
23
24 //Curved space
25 private const string CURVED_PARAMETERS = LeapGraphicRenderer.PROPERTY_PREFIX + "Curved_GraphicParameters";
26 private List<Matrix4x4> _curved_worldToAnchor = new List<Matrix4x4>();
27 private List<Vector4> _curved_graphicParameters = new List<Vector4>();
28 #endregion
29
30 public void OnAddRemoveGraphics(List<int> dirtyIndexes) {
31 MeshCache.Clear();
32
33 while (_meshes.Count > group.graphics.Count) {
35 }
36
37 while (_meshes.Count < group.graphics.Count) {
38 beginMesh();
42 base.buildGraphic();
44 }
45
46 foreach (var dirtyIndex in dirtyIndexes) {
47 beginMesh(_meshes[dirtyIndex]);
49 _generation.graphicIndex = dirtyIndex;
50 _generation.graphicId = dirtyIndex;
51 base.buildGraphic();
52 finishMesh();
53 _generation.mesh = null;
54 }
55
57 }
58
59 public override SupportInfo GetSpaceSupportInfo(LeapSpace space) {
60 if (space == null ||
61 space is LeapCylindricalSpace ||
62 space is LeapSphericalSpace) {
63 return SupportInfo.FullSupport();
64 } else {
65 return SupportInfo.Error("Dynamic Renderer does not support " + space.GetType().Name);
66 }
67 }
68
69 public override void OnUpdateRenderer() {
70 using (new ProfilerSample("Update Dynamic Renderer")) {
71 base.OnUpdateRenderer();
72
73 using (new ProfilerSample("Check For Dirty Graphics")) {
74 for (int i = 0; i < group.graphics.Count; i++) {
75 var graphic = group.graphics[i];
76 if (graphic.isRepresentationDirty || _meshes[i] == null) {
77 MeshCache.Clear();
78
83 base.buildGraphic();
84 finishMesh();
86 _generation.mesh = null;
87
88 graphic.isRepresentationDirty = false;
89 }
90 }
91 }
92
93 if (renderer.space == null) {
94 using (new ProfilerSample("Draw Meshes")) {
95 Assert.AreEqual(group.graphics.Count, _meshes.Count);
96 for (int i = 0; i < group.graphics.Count; i++) {
97 if (!group.graphics[i].isActiveAndEnabled) {
98 continue;
99 }
100
101 drawMesh(_meshes[i], group.graphics[i].transform.localToWorldMatrix);
102 }
103 }
104 } else if (renderer.space is LeapRadialSpace) {
105 var curvedSpace = renderer.space as LeapRadialSpace;
106 using (new ProfilerSample("Build Material Data And Draw Meshes")) {
107 _curved_worldToAnchor.Clear();
108 _curved_graphicParameters.Clear();
109 for (int i = 0; i < _meshes.Count; i++) {
110 var graphic = group.graphics[i];
111 if (!graphic.isActiveAndEnabled) {
112 _curved_graphicParameters.Add(Vector4.zero);
113 _curved_worldToAnchor.Add(Matrix4x4.identity);
114 continue;
115 }
116
117 var transformer = graphic.anchor.transformer;
118
119 Vector3 localPos = renderer.transform.InverseTransformPoint(graphic.transform.position);
120
121 Matrix4x4 mainTransform = renderer.transform.localToWorldMatrix * transformer.GetTransformationMatrix(localPos);
122 Matrix4x4 deform = renderer.transform.worldToLocalMatrix * Matrix4x4.TRS(renderer.transform.position - graphic.transform.position, Quaternion.identity, Vector3.one) * graphic.transform.localToWorldMatrix;
123 Matrix4x4 total = mainTransform * deform;
124
125 _curved_graphicParameters.Add((transformer as IRadialTransformer).GetVectorRepresentation(graphic.transform));
126 _curved_worldToAnchor.Add(mainTransform.inverse);
127
128 //Safe to do this before we upload material data
129 //meshes are drawn at end of frame anyway!
130 drawMesh(_meshes[i], total);
131 }
132 }
133
134 using (new ProfilerSample("Upload Material Data")) {
135 _material.SetFloat(SpaceProperties.RADIAL_SPACE_RADIUS, curvedSpace.radius);
136 _material.SetMatrixArraySafe("_GraphicRendererCurved_WorldToAnchor", _curved_worldToAnchor);
137 _material.SetMatrix("_GraphicRenderer_LocalToWorld", renderer.transform.localToWorldMatrix);
138 _material.SetVectorArraySafe("_GraphicRendererCurved_GraphicParameters", _curved_graphicParameters);
139 }
140 }
141 }
142 }
143
144#if UNITY_EDITOR
145 public override void OnEnableRendererEditor() {
146 base.OnEnableRendererEditor();
147
148 _shader = Shader.Find(DEFAULT_SHADER);
149 }
150#endif
151
152 protected override void prepareMaterial() {
153 if (_shader == null) {
154 _shader = Shader.Find(DEFAULT_SHADER);
155 }
156
157 base.prepareMaterial();
158
159 if (renderer.space != null) {
161 _material.EnableKeyword(SpaceProperties.CYLINDRICAL_FEATURE);
162 } else if (renderer.space is LeapSphericalSpace) {
163 _material.EnableKeyword(SpaceProperties.SPHERICAL_FEATURE);
164 }
165 }
166 }
167
168 protected override void buildGraphic() {
169 //Always start a new mesh for each graphic
171 beginMesh();
172 }
173
174 base.buildGraphic();
175
176 finishAndAddMesh(deleteEmptyMeshes: false);
177 }
178
179 protected override bool doesRequireVertInfo() {
180 return true;
181 }
182
183 protected override Vector3 graphicVertToMeshVert(Vector3 vertex) {
184 return vertex;
185 }
186
187 protected override void graphicVertNormalToMeshVertNormal(Vector3 vertex,
188 Vector3 normal,
189 out Vector3 meshVert,
190 out Vector3 meshNormal) {
191 meshVert = vertex;
192 meshNormal = normal;
193 }
194
195 protected override Vector3 blendShapeDelta(Vector3 shapeVert, Vector3 originalVert) {
196 return shapeVert - originalVert;
197 }
198 }
199}
override Vector3 blendShapeDelta(Vector3 shapeVert, Vector3 originalVert)
override void graphicVertNormalToMeshVertNormal(Vector3 vertex, Vector3 normal, out Vector3 meshVert, out Vector3 meshNormal)
override void OnUpdateRenderer()
Called from LateUpdate during runtime. Use this to update the renderer using any changes made to duri...
override SupportInfo GetSpaceSupportInfo(LeapSpace space)
override Vector3 graphicVertToMeshVert(Vector3 vertex)
void OnAddRemoveGraphics(List< int > dirtyIndexes)
Must be implemented by a renderer to report that it is able to support adding and removing graphics a...
List< LeapGraphic > graphics
Returns the list of graphics attached to this group. This getter returns a regular mutable list for s...
LeapSpace space
Returns the leap space that is currently attached to this graphic renderer.
This class is a base class for all graphics that can be represented by a mesh object.
virtual void finishMesh(bool deleteEmptyMeshes=true)
virtual void finishAndAddMesh(bool deleteEmptyMeshes=true)
void drawMesh(Mesh mesh, Matrix4x4 transform)
virtual void beginMesh(Mesh mesh=null)
LeapGraphicGroup group
Gets the group this rendering method is attached to.
LeapGraphicRenderer renderer
Gets the renderer this rendering method is attached to.
The support info class provides a very basic way to notify that something is fully supported,...
Definition: SupportInfo.cs:21
static SupportInfo Error(string message)
Helper getter to return a struct that signifies no support with an error message.
Definition: SupportInfo.cs:42
static SupportInfo FullSupport()
Helper getter to return a struct that signifies full support.
Definition: SupportInfo.cs:28
A utility struct for ease of use when you want to wrap a piece of code in a Profiler....