升级到 Unity 5.5 后轮廓着色器中断
Outline shader break after upgrading to Unity 5.5
着色器在 iPad Air 上的 unity 5.4 上运行良好,但在升级到 unity 5.5 后它破坏了轮廓,但 alpha 仍在运行。
This shader for object support texture and alpha
Shader "TFTM/Outline/Basic-Alpha" {
Properties {
_Color ("Main Color", Color) = (.5,.5,.5,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Transparent" }
Pass {
Name "BASE"
Blend SrcAlpha OneMinusSrcAlpha
//Blend DstColor SrcColor
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _Color;
struct appdata {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : POSITION;
float2 texcoord : TEXCOORD0;
};
v2f vert (appdata v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
float4 frag (v2f i) : COLOR
{
float4 col = _Color * tex2D(_MainTex, i.texcoord);
return float4(2.0f * col.rgb, col.a);
}
ENDCG
}
}
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Transparent"}
Pass {
Name "BASE"
SetTexture [_MainTex] {
constantColor [_Color]
Combine texture * constant
}
}
}
Fallback "VertexLit"
}
This shader for outline, 2 pass , first draw above shader and then draw outline and cull the front.
Shader "TFTM/Outline/Basic Outline-Alpha" {
Properties {
_Color ("Main Color", Color) = (.5,.5,.5,1)
_OutlineColor ("Outline Color", Color) = (0,0,0,1)
_Outline ("Outline width", Range (0, 0.02)) = 0
_MainTex ("Base (RGB)", 2D) = "white" { }
}
CGINCLUDE
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : POSITION;
float4 color : COLOR;
};
uniform float _Outline;
uniform float4 _OutlineColor;
v2f vert(appdata v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
float3 norm = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
float2 offset = TransformViewToProjection(norm.xy);
o.pos.xy += offset * o.pos.z * _Outline;
o.color = _OutlineColor;
return o;
}
ENDCG
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Transparent" }
UsePass "TFTM/Outline/Basic-Alpha/BASE"
Pass {
Name "OUTLINE"
Tags { "LightMode" = "Always" }
Cull Front
ZWrite On
ColorMask RGB
Blend DstColor SrcColor // 2x Multiplicative
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
half4 frag(v2f i) :COLOR { return i.color ; }
ENDCG
}
}
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Transparent"}
UsePass "TFTM/Outline/Basic-Alpha/BASE"
Pass {
Name "OUTLINE"
Tags { "LightMode" = "Always" }
Cull Front
ZWrite On
ColorMask RGB
Blend DstColor SrcColor // 2x Multiplicative
CGPROGRAM
#pragma vertex vert
#pragma exclude_renderers shaderonly
ENDCG
SetTexture [_MainTex] { combine primary }
}
}
Fallback "TFTM/Outline/Basic-Alpha"
}
我通过将 api 渲染更改为 OpenGL ES 3
而不是 Metal
来修复它,当然这只是目前的临时修复。
o.pos.xy += offset * o.pos.z * _Outline;
o.pos.z 因平台而异。 https://docs.unity3d.com/Manual/SL-PlatformDifferences.html
因此,您不能使用它来缩放轮廓。
o.pos.z 的目的是无论距离如何都保持轮廓大小不变。
现在,我们想要的是基于相机 z 轴的比例值。
float eye_depth;
// This depth is based on camera's z axis
COMPUTE_EYEDEPTH(eye_depth);
// Divided by near plane. This is similar triangle math.
float eye_depth_scale = eye_depth/_ProjectionParams.y;
o.pos.xy += offset * eye_depth_scale * _Outline;
您可能需要夹紧 eye_depth 以防止缩放轮廓太远。
着色器在 iPad Air 上的 unity 5.4 上运行良好,但在升级到 unity 5.5 后它破坏了轮廓,但 alpha 仍在运行。
This shader for object support texture and alpha
Shader "TFTM/Outline/Basic-Alpha" {
Properties {
_Color ("Main Color", Color) = (.5,.5,.5,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Transparent" }
Pass {
Name "BASE"
Blend SrcAlpha OneMinusSrcAlpha
//Blend DstColor SrcColor
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _Color;
struct appdata {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : POSITION;
float2 texcoord : TEXCOORD0;
};
v2f vert (appdata v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
float4 frag (v2f i) : COLOR
{
float4 col = _Color * tex2D(_MainTex, i.texcoord);
return float4(2.0f * col.rgb, col.a);
}
ENDCG
}
}
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Transparent"}
Pass {
Name "BASE"
SetTexture [_MainTex] {
constantColor [_Color]
Combine texture * constant
}
}
}
Fallback "VertexLit"
}
This shader for outline, 2 pass , first draw above shader and then draw outline and cull the front.
Shader "TFTM/Outline/Basic Outline-Alpha" {
Properties {
_Color ("Main Color", Color) = (.5,.5,.5,1)
_OutlineColor ("Outline Color", Color) = (0,0,0,1)
_Outline ("Outline width", Range (0, 0.02)) = 0
_MainTex ("Base (RGB)", 2D) = "white" { }
}
CGINCLUDE
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : POSITION;
float4 color : COLOR;
};
uniform float _Outline;
uniform float4 _OutlineColor;
v2f vert(appdata v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
float3 norm = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
float2 offset = TransformViewToProjection(norm.xy);
o.pos.xy += offset * o.pos.z * _Outline;
o.color = _OutlineColor;
return o;
}
ENDCG
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Transparent" }
UsePass "TFTM/Outline/Basic-Alpha/BASE"
Pass {
Name "OUTLINE"
Tags { "LightMode" = "Always" }
Cull Front
ZWrite On
ColorMask RGB
Blend DstColor SrcColor // 2x Multiplicative
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
half4 frag(v2f i) :COLOR { return i.color ; }
ENDCG
}
}
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Transparent"}
UsePass "TFTM/Outline/Basic-Alpha/BASE"
Pass {
Name "OUTLINE"
Tags { "LightMode" = "Always" }
Cull Front
ZWrite On
ColorMask RGB
Blend DstColor SrcColor // 2x Multiplicative
CGPROGRAM
#pragma vertex vert
#pragma exclude_renderers shaderonly
ENDCG
SetTexture [_MainTex] { combine primary }
}
}
Fallback "TFTM/Outline/Basic-Alpha"
}
我通过将 api 渲染更改为 OpenGL ES 3
而不是 Metal
来修复它,当然这只是目前的临时修复。
o.pos.xy += offset * o.pos.z * _Outline;
o.pos.z 因平台而异。 https://docs.unity3d.com/Manual/SL-PlatformDifferences.html
因此,您不能使用它来缩放轮廓。
o.pos.z 的目的是无论距离如何都保持轮廓大小不变。
现在,我们想要的是基于相机 z 轴的比例值。
float eye_depth;
// This depth is based on camera's z axis
COMPUTE_EYEDEPTH(eye_depth);
// Divided by near plane. This is similar triangle math.
float eye_depth_scale = eye_depth/_ProjectionParams.y;
o.pos.xy += offset * eye_depth_scale * _Outline;
您可能需要夹紧 eye_depth 以防止缩放轮廓太远。