Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by salvolannister · May 12, 2020 at 10:28 AM · shaderrenderingmaterialoutline

QuickOutline: how can I change the shader to work with objects with two materials?

Hello folks,

I'm trying to create an outline effect that works also in VR and I am using the QuickOutline asset from the asset store with Unity Version 2018.4.8f1.

The problem is that I have some model that has more than one material and that makes the outline appear only one part of it.

Do you know why and how can I solve it? alt text

I found out that the same thing happens when using different shader found online on objects that have more than one material and that probably it is due to the fact that I need to change something in the #pragma fragment function, but I don't know exactly how.

The quick Outline use the script outline.cs to add at run time to material with two different shaders that are responsible to create the outline:

The Script: // // 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;
     }
   }
 }
 

OutlineFill.shader

 //
 //  OutlineFill.shader
 //  QuickOutline
 //
 //  Created by Chris Nolet on 2/21/18.
 //  Copyright © 2018 Chris Nolet. All rights reserved.
 //
 
 Shader "Custom/Outline Fill" {
   Properties {
     [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("ZTest", Float) = 0
 
     _OutlineColor("Outline Color", Color) = (1, 1, 1, 1)
     _OutlineWidth("Outline Width", Range(0, 10)) = 2
   }
 
   SubShader {  // render things once
     Tags {
       "Queue" = "Transparent+110"
       "RenderType" = "Transparent"
       "DisableBatching" = "True"
     }
 
     Pass {
       Name "Fill"
       Cull Off
       ZTest [_ZTest]
       ZWrite Off
       Blend SrcAlpha OneMinusSrcAlpha
       ColorMask RGB
 
       Stencil {
         Ref 1
         Comp NotEqual
       }
 
       CGPROGRAM // Allows to talk with two different program languages the graphic card and unity one
       #include "UnityCG.cginc"
 
       #pragma vertex vert  // tells how to build the object
       #pragma fragment frag  // tells which color it should be 
 
       struct appdata {
         float4 vertex : POSITION;
         float3 normal : NORMAL;
         float3 smoothNormal : TEXCOORD3;
         UNITY_VERTEX_INPUT_INSTANCE_ID
       };
 
       struct v2f {  // vertex to fragment
         float4 position : SV_POSITION;
         fixed4 color : COLOR;
         UNITY_VERTEX_OUTPUT_STEREO
       };
 
       //enables to connect with the  properties we defined at the beginning
       uniform fixed4 _OutlineColor;
       uniform float _OutlineWidth;
 
       v2f vert(appdata input) { // defintion of the pragma vert function
         v2f output;
 
         UNITY_SETUP_INSTANCE_ID(input);
         UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
 
         float3 normal = any(input.smoothNormal) ? input.smoothNormal : input.normal;
         float3 viewPosition = UnityObjectToViewPos(input.vertex);
         float3 viewNormal = normalize(mul((float3x3)UNITY_MATRIX_IT_MV, normal));
 
         output.position = UnityViewToClipPos(viewPosition + viewNormal * -viewPosition.z * _OutlineWidth / 1000.0);
         output.color = _OutlineColor;
 
         return output;
       }
 
       fixed4 frag(v2f input) : SV_Target {
         return input.color;
       }
       ENDCG
     }
   }
 }
 

OutlineMask.shader

 Shader "Custom/Outline Mask" {
   Properties {
     [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("ZTest", Float) = 0
   }
 
   SubShader {
     Tags {
       "Queue" = "Transparent+100"
       "RenderType" = "Transparent"
     }
 
     Pass {
       Name "Mask"
       Cull Off
       ZTest [_ZTest]
       ZWrite Off
       ColorMask 0
 
       Stencil {
         Ref 1
         Pass Replace
       }
     }
   }
 }
 

EDIT:
This is how it looks like the element in the editor, I have 4 materials but just one mesh Screenshot of the editor

Thank you for the help

outline-example.png (26.7 kB)
outline-material.png (27.0 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image highpockets · May 04, 2020 at 05:54 AM 0
Share

So, you are using several different materials on these objects and each material has to have a shader, so if you do not have the outline shader on each material of the model, you won’t see the effect of it. Is the outline shader on each material??

avatar image salvolannister highpockets · May 11, 2020 at 06:39 AM 0
Share

there are four material two of the object and two of the shader plus a script. I can't put the shader on all of them because I need to use the different textures

3 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by z0code0z · Jun 03, 2020 at 12:58 AM

I FOUND THE SOLUTION!

After hours of going through some information and other outline solutions, simply adding the following code in the Awake function of the Outline.cs (at the very top) will fix it, I am not totally sure how well some of the features work but the outline works perfectly from my tests.

         foreach (var skinnedMeshRenderer in GetComponentsInChildren<SkinnedMeshRenderer>())
         {
             if (skinnedMeshRenderer.sharedMesh.subMeshCount > 1)
             {
                 skinnedMeshRenderer.sharedMesh.subMeshCount = skinnedMeshRenderer.sharedMesh.subMeshCount + 1;
                 skinnedMeshRenderer.sharedMesh.SetTriangles(skinnedMeshRenderer.sharedMesh.triangles, skinnedMeshRenderer.sharedMesh.subMeshCount - 1);
             }
 
         }
 
         foreach (var meshFilter in GetComponentsInChildren<MeshFilter>())
         {
             if (meshFilter.sharedMesh.subMeshCount > 1)
             {
                 meshFilter.sharedMesh.subMeshCount = meshFilter.sharedMesh.subMeshCount + 1;
                 meshFilter.sharedMesh.SetTriangles(meshFilter.sharedMesh.triangles, meshFilter.sharedMesh.subMeshCount - 1);
             }
         }
 

Disclaimer, the second portion (involving meshfilter) was not tested but it should work the same as with the skinnedMeshRenderer

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Aspharon · Jan 14, 2021 at 08:32 PM 0
Share

One small correction, the meshFilter[i].shared$$anonymous$$esh.sub$$anonymous$$eshCount = meshFilter[i].shared$$anonymous$$esh.sub$$anonymous$$eshCount + 1; line isn't needed, and will in fact cause a crash in a build.

avatar image awesome_suri · Jul 09, 2021 at 01:27 PM 0
Share

No, the line Aspharon mentioned IS needed, so you don't break the mesh materials. What's breaking the game is the fact that you're working with the asset directly. So every time the scene reloads or another scene containing the same asset loads, this asset gets more and more submeshes. To fix this, use .mesh instead of .sharedMesh. This way, a new instance of the mesh is created for every object that you can independently adjust.

avatar image Sarahnade · Jan 03 at 09:49 PM 0
Share

Thank you SO much for this solution. Saved me so much time and headache. Probably would have ended up cutting the outlines from my game. Bless you @z0code0z

avatar image chris-nolet · Mar 07 at 11:01 PM 0
Share

This functionality is now supported with the latest update to Quick Outline. Please note that the solution above has a memory leak, when running in the editor.

avatar image
0

Answer by Pangamini · May 11, 2020 at 08:02 AM

Yeah I think that's still bugged. You have to merge submeshes (and materials) into a single mesh, or separate submeshes (a mesh with multiple materials) into actual separate meshes with separate renderers

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image salvolannister · May 12, 2020 at 06:23 AM 0
Share

Thank you for the reply, I added a picture of how it looks like the element in the editor. Could you explain me where I should make those modification?

avatar image Pangamini salvolannister · May 12, 2020 at 08:22 AM 0
Share

in your modelling software

avatar image salvolannister · May 13, 2020 at 06:50 AM 0
Share

I need two Uv maps, so I can't merge the materials. What I can do is to have to separate objects for the base and the pillar...but in this way I would have outlined also the bottom of the pillar which I don't want to happen.

avatar image
0

Answer by jon_underwood · Jun 20, 2020 at 04:46 PM

I also used the code provided by @z0codez above.

I found I also needed to use closed volume objects (no open faces at the back). Set import settings on Tangents to none.

Hope this is helpful for somebody.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

223 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why does the frame debugger say node uses different shader when it isn't? 0 Answers

Object outline component 0 Answers

Using Color.Lerp with Lightweight Render Pipeline 1 Answer

How can i get my quad to only render my texture without stretching it? 1 Answer

Outline Shader HELP 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges