-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisualMeshCloner.cs
More file actions
108 lines (93 loc) · 4.14 KB
/
Copy pathVisualMeshCloner.cs
File metadata and controls
108 lines (93 loc) · 4.14 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
using UnityEngine;
namespace ProxyNote
{
internal static class VisualMeshCloner
{
internal static GameObject CloneRenderHierarchy(Transform sourceRoot, string name)
{
if (sourceRoot == null)
{
return null;
}
GameObject root = new GameObject(name);
root.layer = sourceRoot.gameObject.layer;
CopyTransformChildrenAndRenderers(sourceRoot, root.transform, copyTransform: false);
if (root.GetComponentsInChildren<Renderer>(includeInactive: true).Length == 0)
{
Object.Destroy(root);
return null;
}
return root;
}
private static void CopyTransformChildrenAndRenderers(
Transform source,
Transform destination,
bool copyTransform)
{
if (copyTransform)
{
destination.localPosition = source.localPosition;
destination.localRotation = source.localRotation;
destination.localScale = source.localScale;
}
destination.gameObject.layer = source.gameObject.layer;
CopyMeshRenderer(source.gameObject, destination.gameObject);
CopySkinnedMeshRenderer(source.gameObject, destination.gameObject);
for (int index = 0; index < source.childCount; index++)
{
Transform sourceChild = source.GetChild(index);
if (sourceChild.name == "NoteCutGuide")
{
continue;
}
GameObject destinationChild = new GameObject(sourceChild.name);
destinationChild.SetActive(sourceChild.gameObject.activeSelf);
destinationChild.transform.SetParent(destination, false);
CopyTransformChildrenAndRenderers(
sourceChild,
destinationChild.transform,
copyTransform: true);
}
}
private static void CopyMeshRenderer(GameObject source, GameObject destination)
{
MeshRenderer sourceRenderer = source.GetComponent<MeshRenderer>();
MeshFilter sourceFilter = source.GetComponent<MeshFilter>();
if (sourceRenderer == null || sourceFilter == null)
{
return;
}
MeshFilter destinationFilter = destination.AddComponent<MeshFilter>();
destinationFilter.sharedMesh = sourceFilter.sharedMesh;
MeshRenderer destinationRenderer = destination.AddComponent<MeshRenderer>();
CopyRendererState(sourceRenderer, destinationRenderer);
}
private static void CopySkinnedMeshRenderer(GameObject source, GameObject destination)
{
SkinnedMeshRenderer sourceRenderer = source.GetComponent<SkinnedMeshRenderer>();
if (sourceRenderer == null)
{
return;
}
SkinnedMeshRenderer destinationRenderer = destination.AddComponent<SkinnedMeshRenderer>();
destinationRenderer.sharedMesh = sourceRenderer.sharedMesh;
destinationRenderer.localBounds = sourceRenderer.localBounds;
CopyRendererState(sourceRenderer, destinationRenderer);
}
private static void CopyRendererState(Renderer source, Renderer destination)
{
destination.sharedMaterials = source.sharedMaterials;
destination.enabled = source.enabled;
destination.shadowCastingMode = source.shadowCastingMode;
destination.receiveShadows = source.receiveShadows;
destination.lightProbeUsage = source.lightProbeUsage;
destination.reflectionProbeUsage = source.reflectionProbeUsage;
destination.probeAnchor = source.probeAnchor;
destination.sortingLayerID = source.sortingLayerID;
destination.sortingOrder = source.sortingOrder;
MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock();
source.GetPropertyBlock(propertyBlock);
destination.SetPropertyBlock(propertyBlock);
}
}
}