Tanoda
MeshRendererConversion.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.Collections.Generic;
10using UnityEngine;
11using UnityEditor;
12using Leap.Unity.Query;
13
15
16 public static class MeshRendererConversion {
17 private const string CONTEXT_PATH = "CONTEXT/MeshRenderer/Convert To Leap Graphic Mesh";
18
19 [MenuItem(CONTEXT_PATH)]
20 public static void convert(MenuCommand command) {
21 var graphicRenderer = (command.context as MeshRenderer).GetComponentInParent<LeapGraphicRenderer>();
22
23 if (graphicRenderer.groups.Count == 0) {
24 graphicRenderer.editor.CreateGroup(typeof(LeapBakedRenderer));
25 }
26
27 var group = graphicRenderer.groups[0];
28
29 var graphics = new List<LeapMeshGraphic>();
30 var meshRenderers = (command.context as MeshRenderer).GetComponentsInChildren<MeshRenderer>();
31 foreach (var meshRenderer in meshRenderers) {
32 var material = meshRenderer.sharedMaterial;
33 if (material == null) continue;
34
35 var shader = material.shader;
36 if (shader == null) continue;
37
38 var filter = meshRenderer.GetComponent<MeshFilter>();
39 if (filter == null) continue;
40
41 var mesh = filter.sharedMesh;
42 if (mesh == null) continue;
43
44 int propCount = ShaderUtil.GetPropertyCount(shader);
45 for (int i = 0; i < propCount; i++) {
46 if (ShaderUtil.GetPropertyType(shader, i) == ShaderUtil.ShaderPropertyType.TexEnv) {
47 string propName = ShaderUtil.GetPropertyName(shader, i);
48
49 if (material.GetTexture(propName) == null) continue;
50
51 var feature = group.features.Query().
52 OfType<LeapTextureFeature>().
53 FirstOrDefault(f => f.propertyName == propName);
54
55 if (feature == null) {
56 feature = group.editor.AddFeature(typeof(LeapTextureFeature)) as LeapTextureFeature;
57 feature.channel = UnityEngine.Rendering.UVChannelFlags.UV0;
58 feature.propertyName = propName;
59 }
60 }
61 }
62
63 var graphic = meshRenderer.gameObject.AddComponent<LeapMeshGraphic>();
64 Undo.RegisterCreatedObjectUndo(graphic, "Create Leap Mesh Graphic");
65
66 group.TryAddGraphic(graphic);
67 graphics.Add(graphic);
68 }
69
70 foreach (var graphic in graphics) {
71 var meshRenderer = graphic.GetComponent<MeshRenderer>();
72 var meshFilter = graphic.GetComponent<MeshFilter>();
73 var material = meshRenderer.sharedMaterial;
74
75 graphic.SetMesh(meshFilter.sharedMesh);
76
77 foreach (var dataObj in graphic.featureData) {
78 var textureData = dataObj as LeapTextureData;
79 if (textureData == null) {
80 continue;
81 }
82
83 var feature = textureData.feature as LeapTextureFeature;
84 if (!material.HasProperty(feature.propertyName)) {
85 continue;
86 }
87
88 Texture2D tex2d = material.GetTexture(feature.propertyName) as Texture2D;
89 if (tex2d == null) {
90 continue;
91 }
92
93 textureData.texture = tex2d;
94 }
95
96 Undo.DestroyObjectImmediate(meshRenderer);
97 Undo.DestroyObjectImmediate(meshFilter);
98 }
99
100 group.renderer.editor.ScheduleRebuild();
101 }
102
103 [MenuItem(CONTEXT_PATH, validate = true)]
104 public static bool convertValidate(MenuCommand command) {
105 var graphicRenderer = (command.context as MeshRenderer).GetComponentInParent<LeapGraphicRenderer>();
106 return graphicRenderer != null;
107 }
108 }
109}