Skip to content

Commit 1668916

Browse files
authored
Added a method to handle renderers with multiple submeshes. (#200)
1 parent 7bca24e commit 1668916

6 files changed

Lines changed: 403 additions & 6 deletions

File tree

com.microsoft.mrtk.graphicstools.unity/Runtime/MeshOutline/MeshOutline.cs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class MeshOutline : BaseMeshOutline
1919
private const string vertexExtrusionKeyword = "_VERTEX_EXTRUSION";
2020
private const string vertexExtrusionSmoothNormalsKeyword = "_VERTEX_EXTRUSION_SMOOTH_NORMALS";
2121

22-
private Renderer meshRenderer = null;
22+
private Renderer baseRenderer = null;
2323
private int stencilReferenceID = Shader.PropertyToID("_StencilReference");
2424
private int vertexExtrusionValueID = Shader.PropertyToID("_VertexExtrusionValue");
2525
private Material[] defaultMaterials = null;
@@ -38,8 +38,8 @@ private void Awake()
3838
Debug.LogWarning($"{this.GetType()} is not supported on this type of renderer.");
3939
}
4040

41-
meshRenderer = GetComponent<Renderer>();
42-
defaultMaterials = meshRenderer.sharedMaterials;
41+
baseRenderer = GetComponent<Renderer>();
42+
defaultMaterials = baseRenderer.sharedMaterials;
4343
}
4444

4545
/// <summary>
@@ -55,7 +55,7 @@ private void OnEnable()
5555
/// </summary>
5656
private void OnDisable()
5757
{
58-
meshRenderer.materials = defaultMaterials;
58+
baseRenderer.materials = defaultMaterials;
5959
}
6060

6161
/// <summary>
@@ -77,7 +77,7 @@ public override void ApplyOutlineMaterial()
7777
{
7878
if (enabled == false ||
7979
gameObject.activeInHierarchy == false ||
80-
meshRenderer == null ||
80+
baseRenderer == null ||
8181
outlineMaterial == null)
8282
{
8383
return;
@@ -140,7 +140,9 @@ public override void ApplyOutlineMaterial()
140140

141141
materials[nextFreeIdx] = outlineMaterial;
142142

143-
meshRenderer.materials = materials;
143+
HandleMultipleSubMeshes();
144+
145+
baseRenderer.materials = materials;
144146
}
145147

146148
/// <summary>
@@ -238,5 +240,40 @@ private static int GetMaxRenderQueue(Material[] materials)
238240

239241
return max;
240242
}
243+
244+
/// <summary>
245+
/// Creates and adds a SubMeshDescriptor which covers the entire mesh.
246+
/// This ensures that the outline material is applied to the entire mesh.
247+
/// </summary>
248+
private void HandleMultipleSubMeshes()
249+
{
250+
Mesh mesh = null;
251+
252+
if (baseRenderer is SkinnedMeshRenderer skinnedMeshRenderer)
253+
{
254+
mesh = skinnedMeshRenderer.sharedMesh;
255+
}
256+
else if (baseRenderer is MeshRenderer meshRenderer)
257+
{
258+
mesh = meshRenderer.GetComponent<MeshFilter>().sharedMesh;
259+
}
260+
261+
if (mesh != null)
262+
{
263+
var meshSubMeshCount = mesh.subMeshCount;
264+
if (meshSubMeshCount > 1)
265+
{
266+
var subMeshes = new SubMeshDescriptor[meshSubMeshCount + 1];
267+
for (int i = 0; i < meshSubMeshCount; i++)
268+
{
269+
var subMesh = mesh.GetSubMesh(i);
270+
subMeshes[i] = subMesh;
271+
}
272+
var lastMesh = subMeshes[meshSubMeshCount - 1];
273+
subMeshes[meshSubMeshCount] = new SubMeshDescriptor(0, lastMesh.indexStart + lastMesh.indexCount);
274+
mesh.SetSubMeshes(subMeshes, MeshUpdateFlags.DontRecalculateBounds);
275+
}
276+
}
277+
}
241278
}
242279
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!21 &2100000
4+
Material:
5+
serializedVersion: 8
6+
m_ObjectHideFlags: 0
7+
m_CorrespondingSourceObject: {fileID: 0}
8+
m_PrefabInstance: {fileID: 0}
9+
m_PrefabAsset: {fileID: 0}
10+
m_Name: ScrewDriverOutline
11+
m_Shader: {fileID: 4800000, guid: c331f6c43a2ef0945864cb668f2653c9, type: 3}
12+
m_ValidKeywords:
13+
- _SPECULAR_HIGHLIGHTS
14+
- _USE_WORLD_SCALE
15+
- _VERTEX_EXTRUSION
16+
- _VERTEX_EXTRUSION_SMOOTH_NORMALS
17+
m_InvalidKeywords: []
18+
m_LightmapFlags: 4
19+
m_EnableInstancingVariants: 0
20+
m_DoubleSidedGI: 0
21+
m_CustomRenderQueue: 1999
22+
stringTagMap: {}
23+
disabledShaderPasses: []
24+
m_SavedProperties:
25+
serializedVersion: 3
26+
m_TexEnvs:
27+
- _ChannelMap:
28+
m_Texture: {fileID: 0}
29+
m_Scale: {x: 1, y: 1}
30+
m_Offset: {x: 0, y: 0}
31+
- _EmissiveMap:
32+
m_Texture: {fileID: 0}
33+
m_Scale: {x: 1, y: 1}
34+
m_Offset: {x: 0, y: 0}
35+
- _IridescentSpectrumMap:
36+
m_Texture: {fileID: 0}
37+
m_Scale: {x: 1, y: 1}
38+
m_Offset: {x: 0, y: 0}
39+
- _MainTex:
40+
m_Texture: {fileID: 0}
41+
m_Scale: {x: 1, y: 1}
42+
m_Offset: {x: 0, y: 0}
43+
- _NormalMap:
44+
m_Texture: {fileID: 0}
45+
m_Scale: {x: 1, y: 1}
46+
m_Offset: {x: 0, y: 0}
47+
m_Ints: []
48+
m_Floats:
49+
- _AlbedoAlphaMode: 0
50+
- _AlbedoAssignedAtRuntime: 0
51+
- _BlendOp: 0
52+
- _BlendedClippingWidth: 0
53+
- _BlurBorderIntensity: 0
54+
- _BlurMode: 0
55+
- _BlurTextureIntensity: 1
56+
- _BorderColorMode: 0
57+
- _BorderLight: 0
58+
- _BorderLightOpaque: 0
59+
- _BorderLightOpaqueAlpha: 1
60+
- _BorderLightReplacesAlbedo: 0
61+
- _BorderMinValue: 0.1
62+
- _BorderWidth: 0.1
63+
- _ClippingBorder: 0
64+
- _ClippingBorderWidth: 0.025
65+
- _ColorWriteMask: 15
66+
- _CullMode: 2
67+
- _CustomMode: 0
68+
- _Cutoff: 0.5
69+
- _DirectionalLight: 0
70+
- _DstBlend: 0
71+
- _DstBlendAlpha: 1
72+
- _EdgeSmoothingMode: 0
73+
- _EdgeSmoothingValue: 0.002
74+
- _EnableChannelMap: 0
75+
- _EnableEmission: 0
76+
- _EnableHoverColorOverride: 0
77+
- _EnableLocalSpaceTriplanarMapping: 0
78+
- _EnableNormalMap: 0
79+
- _EnableProximityLightColorOverride: 0
80+
- _EnableSSAA: 0
81+
- _EnableStencil: 0
82+
- _EnableTriplanarMapping: 0
83+
- _EnvironmentColorIntensity: 0.5
84+
- _EnvironmentColorThreshold: 1.5
85+
- _EnvironmentColoring: 0
86+
- _Fade: 1
87+
- _FadeBeginDistance: 0.85
88+
- _FadeCompleteDistance: 0.5
89+
- _FadeMinValue: 0
90+
- _FluentLightIntensity: 1
91+
- _GradientAngle: 180
92+
- _GradientMode: 0
93+
- _HoverLight: 0
94+
- _IndependentCorners: 0
95+
- _InnerGlow: 0
96+
- _InnerGlowPower: 4
97+
- _IridescenceAngle: -0.78
98+
- _IridescenceIntensity: 0.5
99+
- _IridescenceThreshold: 0.05
100+
- _Metallic: 0
101+
- _MipmapBias: -2
102+
- _Mode: 5
103+
- _NPR: 0
104+
- _NearLightFade: 0
105+
- _NearPlaneFade: 0
106+
- _NormalMapScale: 1
107+
- _ProximityLight: 0
108+
- _ProximityLightSubtractive: 0
109+
- _ProximityLightTwoSided: 0
110+
- _Reflections: 0
111+
- _RenderQueueOverride: -1
112+
- _RimLight: 0
113+
- _RimPower: 0.25
114+
- _RoundCornerMargin: 0.01
115+
- _RoundCornerRadius: 0.25
116+
- _RoundCorners: 0
117+
- _RoundCornersHideInterior: 0
118+
- _Smoothness: 0.5
119+
- _SpecularHighlights: 1
120+
- _SphericalHarmonics: 0
121+
- _SrcBlend: 1
122+
- _SrcBlendAlpha: 1
123+
- _StencilComparison: 0
124+
- _StencilOperation: 0
125+
- _StencilReadMask: 255
126+
- _StencilReference: 1
127+
- _StencilWriteMask: 255
128+
- _TriplanarMappingBlendSharpness: 4
129+
- _UseWorldScale: 1
130+
- _VertexColors: 0
131+
- _VertexExtrusion: 1
132+
- _VertexExtrusionConstantWidth: 0
133+
- _VertexExtrusionSmoothNormals: 1
134+
- _VertexExtrusionValue: 0.01
135+
- _ZOffsetFactor: 0
136+
- _ZOffsetUnits: 0
137+
- _ZTest: 4
138+
- _ZWrite: 0
139+
m_Colors:
140+
- _BlurBackgroundRect: {r: 0, g: 0, b: 1, a: 1}
141+
- _BorderColor: {r: 1, g: 1, b: 1, a: 0}
142+
- _ClippingBorderColor: {r: 1, g: 0.19999996, b: 0, a: 1}
143+
- _Color: {r: 0, g: 1, b: 0, a: 1}
144+
- _EmissiveColor: {r: 0, g: 0, b: 0, a: 1}
145+
- _EnvironmentColorX: {r: 1, g: 0, b: 0, a: 1}
146+
- _EnvironmentColorY: {r: 0, g: 1, b: 0, a: 1}
147+
- _EnvironmentColorZ: {r: 0, g: 0, b: 1, a: 1}
148+
- _GradientAlpha: {r: 1, g: 1, b: 1, a: 1}
149+
- _GradientAlphaTime: {r: 0, g: 0, b: 0, a: 0}
150+
- _GradientColor0: {r: 0.631373, g: 0.631373, b: 0.631373, a: 0}
151+
- _GradientColor1: {r: 1, g: 0.690196, b: 0.976471, a: 0.25}
152+
- _GradientColor2: {r: 0, g: 0.32999998, b: 0.88, a: 0.5}
153+
- _GradientColor3: {r: 0, g: 0.32999998, b: 0.88, a: 1}
154+
- _GradientColor4: {r: 1, g: 1, b: 1, a: 1}
155+
- _HoverColorOverride: {r: 1, g: 1, b: 1, a: 1}
156+
- _InnerGlowColor: {r: 1, g: 1, b: 1, a: 0.75}
157+
- _ProximityLightCenterColorOverride: {r: 1, g: 0, b: 0, a: 0}
158+
- _ProximityLightMiddleColorOverride: {r: 0, g: 1, b: 0, a: 0.5}
159+
- _ProximityLightOuterColorOverride: {r: 0, g: 0, b: 1, a: 1}
160+
- _RimColor: {r: 0.5, g: 0.5, b: 0.5, a: 1}
161+
- _RoundCornersRadius: {r: 0.5, g: 0.5, b: 0.5, a: 0.5}
162+
m_BuildTextureStacks: []

com.microsoft.mrtk.graphicstools.unity/Samples~/MeshOutlines/Materials/ScrewDriverOutline.mat.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.microsoft.mrtk.graphicstools.unity/Samples~/MeshOutlines/MeshOutlines.unity

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,7 @@ Animator:
779779
m_HasTransformHierarchy: 1
780780
m_AllowConstantClipSamplingOptimization: 1
781781
m_KeepAnimatorStateOnDisable: 0
782+
m_WriteDefaultValuesOnDisable: 0
782783
--- !u!1 &404787638
783784
GameObject:
784785
m_ObjectHideFlags: 0
@@ -2273,6 +2274,63 @@ Transform:
22732274
m_Father: {fileID: 122737516}
22742275
m_RootOrder: 0
22752276
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
2277+
--- !u!1001 &1099723155
2278+
PrefabInstance:
2279+
m_ObjectHideFlags: 0
2280+
serializedVersion: 2
2281+
m_Modification:
2282+
m_TransformParent: {fileID: 0}
2283+
m_Modifications:
2284+
- target: {fileID: -8679921383154817045, guid: a5abb9c67495cc044abc73dfa85ab8dc, type: 3}
2285+
propertyPath: m_RootOrder
2286+
value: 9
2287+
objectReference: {fileID: 0}
2288+
- target: {fileID: -8679921383154817045, guid: a5abb9c67495cc044abc73dfa85ab8dc, type: 3}
2289+
propertyPath: m_LocalPosition.x
2290+
value: -0.5722
2291+
objectReference: {fileID: 0}
2292+
- target: {fileID: -8679921383154817045, guid: a5abb9c67495cc044abc73dfa85ab8dc, type: 3}
2293+
propertyPath: m_LocalPosition.y
2294+
value: 0
2295+
objectReference: {fileID: 0}
2296+
- target: {fileID: -8679921383154817045, guid: a5abb9c67495cc044abc73dfa85ab8dc, type: 3}
2297+
propertyPath: m_LocalPosition.z
2298+
value: 0
2299+
objectReference: {fileID: 0}
2300+
- target: {fileID: -8679921383154817045, guid: a5abb9c67495cc044abc73dfa85ab8dc, type: 3}
2301+
propertyPath: m_LocalRotation.w
2302+
value: 1
2303+
objectReference: {fileID: 0}
2304+
- target: {fileID: -8679921383154817045, guid: a5abb9c67495cc044abc73dfa85ab8dc, type: 3}
2305+
propertyPath: m_LocalRotation.x
2306+
value: 0
2307+
objectReference: {fileID: 0}
2308+
- target: {fileID: -8679921383154817045, guid: a5abb9c67495cc044abc73dfa85ab8dc, type: 3}
2309+
propertyPath: m_LocalRotation.y
2310+
value: -0
2311+
objectReference: {fileID: 0}
2312+
- target: {fileID: -8679921383154817045, guid: a5abb9c67495cc044abc73dfa85ab8dc, type: 3}
2313+
propertyPath: m_LocalRotation.z
2314+
value: -0
2315+
objectReference: {fileID: 0}
2316+
- target: {fileID: -8679921383154817045, guid: a5abb9c67495cc044abc73dfa85ab8dc, type: 3}
2317+
propertyPath: m_LocalEulerAnglesHint.x
2318+
value: 0
2319+
objectReference: {fileID: 0}
2320+
- target: {fileID: -8679921383154817045, guid: a5abb9c67495cc044abc73dfa85ab8dc, type: 3}
2321+
propertyPath: m_LocalEulerAnglesHint.y
2322+
value: 0
2323+
objectReference: {fileID: 0}
2324+
- target: {fileID: -8679921383154817045, guid: a5abb9c67495cc044abc73dfa85ab8dc, type: 3}
2325+
propertyPath: m_LocalEulerAnglesHint.z
2326+
value: 0
2327+
objectReference: {fileID: 0}
2328+
- target: {fileID: 919132149155446097, guid: a5abb9c67495cc044abc73dfa85ab8dc, type: 3}
2329+
propertyPath: m_Name
2330+
value: ScrewDriver
2331+
objectReference: {fileID: 0}
2332+
m_RemovedComponents: []
2333+
m_SourcePrefab: {fileID: 100100000, guid: a5abb9c67495cc044abc73dfa85ab8dc, type: 3}
22762334
--- !u!1 &1111783013
22772335
GameObject:
22782336
m_ObjectHideFlags: 0
@@ -2838,6 +2896,7 @@ Animator:
28382896
m_HasTransformHierarchy: 1
28392897
m_AllowConstantClipSamplingOptimization: 1
28402898
m_KeepAnimatorStateOnDisable: 0
2899+
m_WriteDefaultValuesOnDisable: 0
28412900
--- !u!1 &1394696927
28422901
GameObject:
28432902
m_ObjectHideFlags: 0
@@ -3452,6 +3511,7 @@ Animator:
34523511
m_HasTransformHierarchy: 1
34533512
m_AllowConstantClipSamplingOptimization: 1
34543513
m_KeepAnimatorStateOnDisable: 0
3514+
m_WriteDefaultValuesOnDisable: 0
34553515
--- !u!1 &1748349411
34563516
GameObject:
34573517
m_ObjectHideFlags: 0
@@ -3546,6 +3606,30 @@ Transform:
35463606
m_Father: {fileID: 708487598}
35473607
m_RootOrder: 0
35483608
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
3609+
--- !u!1 &1826402465 stripped
3610+
GameObject:
3611+
m_CorrespondingSourceObject: {fileID: 919132149155446097, guid: a5abb9c67495cc044abc73dfa85ab8dc, type: 3}
3612+
m_PrefabInstance: {fileID: 1099723155}
3613+
m_PrefabAsset: {fileID: 0}
3614+
--- !u!114 &1826402471
3615+
MonoBehaviour:
3616+
m_ObjectHideFlags: 0
3617+
m_CorrespondingSourceObject: {fileID: 0}
3618+
m_PrefabInstance: {fileID: 0}
3619+
m_PrefabAsset: {fileID: 0}
3620+
m_GameObject: {fileID: 1826402465}
3621+
m_Enabled: 1
3622+
m_EditorHideFlags: 0
3623+
m_Script: {fileID: 11500000, guid: 90adb44e17279dc4db8f0fc595f1f52f, type: 3}
3624+
m_Name:
3625+
m_EditorClassIdentifier:
3626+
outlineMaterial: {fileID: 2100000, guid: c5b73fa7a335a7a4aa2eaee73d7a25d0, type: 2}
3627+
outlineWidth: 0.01
3628+
autoAssignRenderQueue: 1
3629+
useStencilOutline: 0
3630+
stencilWriteMaterial: {fileID: 2100000, guid: 8b3892ddedaaaed41a663f501f23b032, type: 2}
3631+
outlineOffset: 0
3632+
stencilReference: 1
35493633
--- !u!1 &1836611017
35503634
GameObject:
35513635
m_ObjectHideFlags: 0
Binary file not shown.

0 commit comments

Comments
 (0)