升级到Unity 5.5后轮廓着色器破损
问题描述:
着色器在iPad Air上的统一性5.4上工作正常,但在升级到统一5.5后,它突破了轮廓,但alpha仍在工作。升级到Unity 5.5后轮廓着色器破损
此着色器对象支持纹理和α-
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"
}
此着色器轮廓,2通,上述着色器第一次拉伸,然后绘制轮廓和剔除前面。
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以防止缩放比例太大。
+0
对于内置着色器变量,请参阅此处。 https://docs.unity3d.com/Manual/SL-UnityShaderVariables.html 对于宏下载UnityShader并查看UnityCG.cginc –
不是100%确定,但更改日志提到:'Shaders:Per-rendertarget blend modes。新的着色语法:“Blend N Src Dst”,“BlendOp N Op”,“ColorMask Mask N”,其中N是渲染目标索引(0..7)。此功能适用于可能影响着色器的DX11/12,GLCore,Metal,PS4。 –
雅我听说团结5.5更改日志,但经过后,我不知道它是否影响上面的着色器。 –
考虑到上面引用的更改提到混合的语法更改,您似乎多次使用它我会假设它。再说一遍,我在着色器中并不是英雄。 –