着色器在iPad Air上的Unity 5.4上工作正常,但在升级到unity 5.5之后它打破了轮廓但是alpha仍在工作。
此对象支持纹理着色器和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"
}
此轮廓线用于轮廓,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"
}
答案 0 :(得分:0)
我通过将api渲染更改为OpenGL ES 3
而不是Metal
来解决此问题,但当然现在暂时修复它。
答案 1 :(得分:0)
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以防止缩放轮廓,如果它太过分了。