In general my goal is to make the material shader and the outline effect to work on any gameobject.
I have in the Hierarchy a Sphere Cube Cylinder all this I added the material and shader and they get this light blue color and the Outline script that attach to them is working and making the effect.
Now I added to the Hierarchy also the unity AirCraft from the Vehicles package.
Again I added to the AirCraft the same material and shader and also added mesh renderer component but the color is still gray and when running the game the outline effect is not doing anything on the planet.
This screenshot is before running the game:
This screenshot is when the game is running:
And this is the shader:
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Outlined/Silhouetted Diffuse" {
Properties {
_Color ("Main Color", Color) = (.5,.5,.5,1)
_OutlineColor ("Outline Color", Color) = (0,0,0,1)
_Outline ("Outline width", Range (0.0, 0.1)) = .005
_MainTex ("Base (RGB)", 2D) = "white" { }
}
SubShader {
Tags { "Queue" = "Transparent" }
Pass {
Name "OUTLINE"
Tags { "LightMode" = "Always" }
Cull Off
ZWrite Off
ZTest Always
ColorMask RGB // alpha not used
// you can choose what kind of blending mode you want for the outline
Blend SrcAlpha OneMinusSrcAlpha // Normal
//Blend One One // Additive
//Blend One OneMinusDstColor // Soft Additive
//Blend DstColor Zero // Multiplicative
//Blend DstColor SrcColor // 2x Multiplicative
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 color : COLOR;
};
struct v2f {
float4 pos : POSITION;
float4 color : COLOR;
};
uniform float _Outline;
uniform float4 _OutlineColor;
v2f vert(appdata v) {
v2f o;
v.vertex.xyz += v.color * _Outline;
o.pos = UnityObjectToClipPos(v.vertex);
o.color = _OutlineColor;
return o;
}
half4 frag(v2f i) :COLOR {
return i.color;
}
ENDCG
}
Pass {
Name "BASE"
ZWrite On
ZTest LEqual
Blend SrcAlpha OneMinusSrcAlpha
Material {
Diffuse [_Color]
Ambient [_Color]
}
Lighting On
SetTexture [_MainTex] {
ConstantColor [_Color]
Combine texture * constant
}
SetTexture [_MainTex] {
Combine previous * primary DOUBLE
}
}
}
SubShader {
Tags { "Queue" = "Transparent" }
Pass {
Name "OUTLINE"
Tags { "LightMode" = "Always" }
Cull Front
ZWrite Off
ZTest Always
ColorMask RGB
// you can choose what kind of blending mode you want for the outline
Blend SrcAlpha OneMinusSrcAlpha // Normal
//Blend One One // Additive
//Blend One OneMinusDstColor // Soft Additive
//Blend DstColor Zero // Multiplicative
//Blend DstColor SrcColor // 2x Multiplicative
CGPROGRAM
#pragma vertex vert
#pragma exclude_renderers gles xbox360 ps3
ENDCG
SetTexture [_MainTex] { combine primary }
}
Pass {
Name "BASE"
ZWrite On
ZTest LEqual
Blend SrcAlpha OneMinusSrcAlpha
Material {
Diffuse [_Color]
Ambient [_Color]
}
Lighting On
SetTexture [_MainTex] {
ConstantColor [_Color]
Combine texture * constant
}
SetTexture [_MainTex] {
Combine previous * primary DOUBLE
}
}
}
Fallback "Diffuse"
}
This script is attached to the object MeshProcessor:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class AverageMeshNormals : MonoBehaviour {
public MeshFilter[] meshSources;
private static readonly Vector3 zeroVec = Vector3.zero;
// Use this for initialization
void Start() {
foreach (MeshFilter meshSource in meshSources) {
Vector3[] verts = meshSource.mesh.vertices;
Vector3[] normals = meshSource.mesh.normals;
VertInfo[] vertInfo = new VertInfo[verts.Length];
for (int i = 0; i < verts.Length; i++) {
vertInfo[i] = new VertInfo() {
vert = verts[i],
origIndex = i,
normal = normals[i]
};
}
var groups = vertInfo.GroupBy(x => x.vert);
VertInfo[] processedVertInfo = new VertInfo[vertInfo.Length];
int index = 0;
foreach (IGrouping<Vector3, VertInfo> group in groups) {
Vector3 avgNormal = zeroVec;
foreach (VertInfo item in group) {
avgNormal += item.normal;
}
avgNormal = avgNormal / group.Count();
foreach (VertInfo item in group) {
processedVertInfo[index] = new VertInfo() {
vert = item.vert,
origIndex = item.origIndex,
normal = item.normal,
averagedNormal = avgNormal
};
index++;
}
}
Color[] colors = new Color[verts.Length];
for (int i = 0; i < processedVertInfo.Length; i++) {
VertInfo info = processedVertInfo[i];
int origIndex = info.origIndex;
Vector3 normal = info.averagedNormal;
Color normColor = new Color(normal.x, normal.y, normal.z, 1);
colors[origIndex] = normColor;
}
meshSource.mesh.colors = colors;
}
}
private struct VertInfo {
public Vector3 vert;
public int origIndex;
public Vector3 normal;
public Vector3 averagedNormal;
}
// Update is called once per frame
void Update () {
}
}

