Skip to content

Commit 8cfaf9f

Browse files
authored
Added selected object highlight (#4)
* Create Outline.shader * Update TransformGizmo.cs
1 parent 7cf3ad8 commit 8cfaf9f

File tree

2 files changed

+119
-27
lines changed

2 files changed

+119
-27
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//Taken and modified from github.com/Shrimpey/Outlined-Diffuse-Shader-Fixed/blob/master/CustomOutline.shader
2+
3+
Shader "Custom/Outline" {
4+
Properties {
5+
_OutlineColor ("Outline Color", Color) = (1,.5,0,1)
6+
_Outline ("Outline width", Range (0, 1)) = .1
7+
}
8+
9+
CGINCLUDE
10+
#include "UnityCG.cginc"
11+
12+
struct appdata {
13+
float4 vertex : POSITION;
14+
float3 normal : NORMAL;
15+
};
16+
17+
struct v2f {
18+
float4 pos : POSITION;
19+
float4 color : COLOR;
20+
};
21+
22+
uniform float _Outline;
23+
uniform float4 _OutlineColor;
24+
25+
v2f vert(appdata v) {
26+
// just make a copy of incoming vertex data but scaled according to normal direction
27+
v2f o;
28+
29+
v.vertex *= ( 1 + _Outline);
30+
31+
o.pos = UnityObjectToClipPos(v.vertex);
32+
33+
o.color = _OutlineColor;
34+
return o;
35+
}
36+
ENDCG
37+
38+
SubShader {
39+
Tags { "DisableBatching" = "True" }
40+
Pass {
41+
Name "OUTLINE"
42+
Tags {"LightMode" = "Always" }
43+
Cull Front
44+
ZWrite On
45+
ColorMask RGB
46+
Blend SrcAlpha OneMinusSrcAlpha
47+
48+
CGPROGRAM
49+
#pragma vertex vert
50+
#pragma fragment frag
51+
half4 frag(v2f i) :COLOR { return i.color; }
52+
ENDCG
53+
}
54+
}
55+
56+
Fallback "Diffuse"
57+
}

Assets/RuntimeGizmo/TransformGizmo.cs

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,20 @@ public class TransformGizmo : MonoBehaviour
5757
Camera myCamera;
5858

5959
static Material lineMaterial;
60+
static Material outlineMaterial;
61+
static string outlineMaterialName = "TransformGizmoOutlineMaterial";
6062

6163
void Awake()
6264
{
6365
myCamera = GetComponent<Camera>();
6466
SetMaterial();
6567
}
6668

69+
void OnDisable()
70+
{
71+
SetTarget(null); //Just so things gets cleaned up, such as removing any materials we placed on objects.
72+
}
73+
6774
void Update()
6875
{
6976
SetSpaceAndType();
@@ -303,14 +310,65 @@ void GetTarget()
303310
RaycastHit hitInfo;
304311
if(Physics.Raycast(myCamera.ScreenPointToRay(Input.mousePosition), out hitInfo))
305312
{
306-
target = hitInfo.transform;
307-
SetPivotPoint();
313+
SetTarget(hitInfo.transform);
308314
}else{
309-
target = null;
315+
SetTarget(null);
310316
}
311317
}
312318
}
313319

320+
void SetTarget(Transform newTarget)
321+
{
322+
if(target != null)
323+
{
324+
SetHighlighTarget(false);
325+
}
326+
327+
target = newTarget;
328+
329+
if(target != null)
330+
{
331+
SetHighlighTarget(true);
332+
SetPivotPoint();
333+
}
334+
}
335+
336+
List<Renderer> renderersBuffer = new List<Renderer>();
337+
List<Material> materialsBuffer = new List<Material>();
338+
void SetHighlighTarget(bool isHighlighted)
339+
{
340+
if(target != null)
341+
{
342+
renderersBuffer.Clear();
343+
target.GetComponentsInChildren<Renderer>(true, renderersBuffer);
344+
345+
for(int i = 0; i < renderersBuffer.Count; i++)
346+
{
347+
Renderer render = renderersBuffer[i];
348+
materialsBuffer.Clear();
349+
materialsBuffer.AddRange(render.sharedMaterials);
350+
351+
if(isHighlighted)
352+
{
353+
if(!materialsBuffer.Contains(outlineMaterial))
354+
{
355+
materialsBuffer.Add(outlineMaterial);
356+
render.materials = materialsBuffer.ToArray();
357+
}
358+
}else{
359+
if(materialsBuffer.Contains(outlineMaterial))
360+
{
361+
materialsBuffer.Remove(outlineMaterial);
362+
render.materials = materialsBuffer.ToArray();
363+
}
364+
}
365+
}
366+
367+
renderersBuffer.Clear();
368+
materialsBuffer.Clear();
369+
}
370+
}
371+
314372
void SetPivotPoint()
315373
{
316374
if(target != null)
@@ -677,30 +735,7 @@ void SetMaterial()
677735
if(lineMaterial == null)
678736
{
679737
lineMaterial = new Material(Shader.Find("Custom/Lines"));
680-
#region Shader code
681-
/*
682-
Shader "Custom/Lines"
683-
{
684-
SubShader
685-
{
686-
Pass
687-
{
688-
Blend SrcAlpha OneMinusSrcAlpha
689-
ZWrite Off
690-
ZTest Always
691-
Cull Off
692-
Fog { Mode Off }
693-
694-
BindChannels
695-
{
696-
Bind "vertex", vertex
697-
Bind "color", color
698-
}
699-
}
700-
}
701-
}
702-
*/
703-
#endregion
738+
outlineMaterial = new Material(Shader.Find("Custom/Outline"));
704739
}
705740
}
706741

0 commit comments

Comments
 (0)