- Home /
Need a bit of help implementing something into a code
So I needed an object outline/highlight and I found a good one on the asset store, however by default the outline is always showing on all objects the script is attached to. I would like to be able to make it show the outline either with mouse hovering over the object or with holding a down a button (or both).
Here's the script. I tried to implement it but since I'm still learning C# I couldn't quite figure it out.
//
// Outline.cs
// QuickOutline
//
// Created by Chris Nolet on 3/30/18.
// Copyright © 2018 Chris Nolet. All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
[DisallowMultipleComponent]
public class Outline : MonoBehaviour {
private static HashSet<Mesh> registeredMeshes = new HashSet<Mesh>();
public enum Mode {
OutlineAll,
OutlineVisible,
OutlineHidden,
OutlineAndSilhouette,
SilhouetteOnly
}
public Mode OutlineMode {
get { return outlineMode; }
set {
outlineMode = value;
needsUpdate = true;
}
}
public Color OutlineColor {
get { return outlineColor; }
set {
outlineColor = value;
needsUpdate = true;
}
}
public float OutlineWidth {
get { return outlineWidth; }
set {
outlineWidth = value;
needsUpdate = true;
}
}
[Serializable]
private class ListVector3 {
public List<Vector3> data;
}
[SerializeField]
private Mode outlineMode;
[SerializeField]
private Color outlineColor = Color.white;
[SerializeField, Range(0f, 10f)]
private float outlineWidth = 2f;
[Header("Optional")]
[SerializeField, Tooltip("Precompute enabled: Per-vertex calculations are performed in the editor and serialized with the object. "
+ "Precompute disabled: Per-vertex calculations are performed at runtime in Awake(). This may cause a pause for large meshes.")]
private bool precomputeOutline;
[SerializeField, HideInInspector]
private List<Mesh> bakeKeys = new List<Mesh>();
[SerializeField, HideInInspector]
private List<ListVector3> bakeValues = new List<ListVector3>();
private Renderer[] renderers;
private Material outlineMaskMaterial;
private Material outlineFillMaterial;
private bool needsUpdate;
void Awake() {
// Cache renderers
renderers = GetComponentsInChildren<Renderer>();
// Instantiate outline materials
outlineMaskMaterial = Instantiate(Resources.Load<Material>(@"Materials/OutlineMask"));
outlineFillMaterial = Instantiate(Resources.Load<Material>(@"Materials/OutlineFill"));
outlineMaskMaterial.name = "OutlineMask (Instance)";
outlineFillMaterial.name = "OutlineFill (Instance)";
// Retrieve or generate smooth normals
LoadSmoothNormals();
// Apply material properties immediately
needsUpdate = true;
}
void OnEnable() {
foreach (var renderer in renderers) {
// Append outline shaders
var materials = renderer.sharedMaterials.ToList();
materials.Add(outlineMaskMaterial);
materials.Add(outlineFillMaterial);
renderer.materials = materials.ToArray();
}
}
void OnValidate() {
// Update material properties
needsUpdate = true;
// Clear cache when baking is disabled or corrupted
if (!precomputeOutline && bakeKeys.Count != 0 || bakeKeys.Count != bakeValues.Count) {
bakeKeys.Clear();
bakeValues.Clear();
}
// Generate smooth normals when baking is enabled
if (precomputeOutline && bakeKeys.Count == 0) {
Bake();
}
}
void Update() {
if (needsUpdate) {
needsUpdate = false;
UpdateMaterialProperties();
}
}
void OnDisable() {
foreach (var renderer in renderers) {
// Remove outline shaders
var materials = renderer.sharedMaterials.ToList();
materials.Remove(outlineMaskMaterial);
materials.Remove(outlineFillMaterial);
renderer.materials = materials.ToArray();
}
}
void OnDestroy() {
// Destroy material instances
Destroy(outlineMaskMaterial);
Destroy(outlineFillMaterial);
}
void Bake() {
// Generate smooth normals for each mesh
var bakedMeshes = new HashSet<Mesh>();
foreach (var meshFilter in GetComponentsInChildren<MeshFilter>()) {
// Skip duplicates
if (!bakedMeshes.Add(meshFilter.sharedMesh)) {
continue;
}
// Serialize smooth normals
var smoothNormals = SmoothNormals(meshFilter.sharedMesh);
bakeKeys.Add(meshFilter.sharedMesh);
bakeValues.Add(new ListVector3() { data = smoothNormals });
}
}
void LoadSmoothNormals() {
// Retrieve or generate smooth normals
foreach (var meshFilter in GetComponentsInChildren<MeshFilter>()) {
// Skip if smooth normals have already been adopted
if (!registeredMeshes.Add(meshFilter.sharedMesh)) {
continue;
}
// Retrieve or generate smooth normals
var index = bakeKeys.IndexOf(meshFilter.sharedMesh);
var smoothNormals = (index >= 0) ? bakeValues[index].data : SmoothNormals(meshFilter.sharedMesh);
// Store smooth normals in UV3
meshFilter.sharedMesh.SetUVs(3, smoothNormals);
}
// Clear UV3 on skinned mesh renderers
foreach (var skinnedMeshRenderer in GetComponentsInChildren<SkinnedMeshRenderer>()) {
if (registeredMeshes.Add(skinnedMeshRenderer.sharedMesh)) {
skinnedMeshRenderer.sharedMesh.uv4 = new Vector2[skinnedMeshRenderer.sharedMesh.vertexCount];
}
}
}
List<Vector3> SmoothNormals(Mesh mesh) {
// Group vertices by location
var groups = mesh.vertices.Select((vertex, index) => new KeyValuePair<Vector3, int>(vertex, index)).GroupBy(pair => pair.Key);
// Copy normals to a new list
var smoothNormals = new List<Vector3>(mesh.normals);
// Average normals for grouped vertices
foreach (var group in groups) {
// Skip single vertices
if (group.Count() == 1) {
continue;
}
// Calculate the average normal
var smoothNormal = Vector3.zero;
foreach (var pair in group) {
smoothNormal += mesh.normals[pair.Value];
}
smoothNormal.Normalize();
// Assign smooth normal to each vertex
foreach (var pair in group) {
smoothNormals[pair.Value] = smoothNormal;
}
}
return smoothNormals;
}
void UpdateMaterialProperties() {
// Apply properties according to mode
outlineFillMaterial.SetColor("_OutlineColor", outlineColor);
switch (outlineMode) {
case Mode.OutlineAll:
outlineMaskMaterial.SetFloat("_ZTest", (float)UnityEngine.Rendering.CompareFunction.Always);
outlineFillMaterial.SetFloat("_ZTest", (float)UnityEngine.Rendering.CompareFunction.Always);
outlineFillMaterial.SetFloat("_OutlineWidth", outlineWidth);
break;
case Mode.OutlineVisible:
outlineMaskMaterial.SetFloat("_ZTest", (float)UnityEngine.Rendering.CompareFunction.Always);
outlineFillMaterial.SetFloat("_ZTest", (float)UnityEngine.Rendering.CompareFunction.LessEqual);
outlineFillMaterial.SetFloat("_OutlineWidth", outlineWidth);
break;
case Mode.OutlineHidden:
outlineMaskMaterial.SetFloat("_ZTest", (float)UnityEngine.Rendering.CompareFunction.Always);
outlineFillMaterial.SetFloat("_ZTest", (float)UnityEngine.Rendering.CompareFunction.Greater);
outlineFillMaterial.SetFloat("_OutlineWidth", outlineWidth);
break;
case Mode.OutlineAndSilhouette:
outlineMaskMaterial.SetFloat("_ZTest", (float)UnityEngine.Rendering.CompareFunction.LessEqual);
outlineFillMaterial.SetFloat("_ZTest", (float)UnityEngine.Rendering.CompareFunction.Always);
outlineFillMaterial.SetFloat("_OutlineWidth", outlineWidth);
break;
case Mode.SilhouetteOnly:
outlineMaskMaterial.SetFloat("_ZTest", (float)UnityEngine.Rendering.CompareFunction.LessEqual);
outlineFillMaterial.SetFloat("_ZTest", (float)UnityEngine.Rendering.CompareFunction.Greater);
outlineFillMaterial.SetFloat("_OutlineWidth", 0);
break;
}
}
}
Answer by Sonic4080 · Mar 24, 2019 at 01:15 PM
That Script has the property OutlineMode which can be set to the five Modes spezified in the enum in lines 20-24. So what you need is a second Script (with a reference to the instance of this Script) in which you Check for User-Input and set the Mode accordingly.
To read the User-Input you should look at methodes like Input.GetKeyDown and OnMouseOver. Both methodes are used very often, and therefor they have a lot of tutorials and examples.
I hope that answers your question. If it doesn't please write a comment or something where your specify your problem and/or further question to my answer
Hey thanks for the answer man! I'm gonna try this and report back.
Your answer
Follow this Question
Related Questions
Sorting a list based of a variable 1 Answer
How can i List objects by name but also in small text or big text or any kind ? 1 Answer
How can i make an entrance and exit in this maze ? 1 Answer
How can I rotate all the objects ? One of them is never rotating 0 Answers
how to add x value in y seconds 1 Answer