【发布时间】:2018-06-06 08:16:05
【问题描述】:
我正在尝试做一个着色器来像Subway Surfer 那样弯曲世界。
我找到了一个GitHub repo,有人在其中推了一个很酷的近似值。
这是代码:
Shader "Custom/Curved" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_QOffset ("Offset", Vector) = (0,0,0,0)
_Dist ("Distance", Float) = 100.0
}
SubShader {
Tags { "RenderType"="Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _QOffset;
float _Dist;
struct v2f {
float4 pos : SV_POSITION;
float4 uv : TEXCOORD0;
};
v2f vert (appdata_base v)
{
v2f o;
float4 vPos = mul (UNITY_MATRIX_MV, v.vertex);
float zOff = vPos.z/_Dist;
vPos += _QOffset*zOff*zOff;
o.pos = mul (UNITY_MATRIX_P, vPos);
o.uv = v.texcoord;
return o;
}
half4 frag (v2f i) : COLOR
{
half4 col = tex2D(_MainTex, i.uv.xy);
return col;
}
ENDCG
}
}
FallBack "Diffuse"
}
关键是现在我想将新的顶点位置发送到表面着色器,以便能够为其他顶点提供照明。
我读到我必须删除片段着色器,但我仍然遇到无法将新信息发送到表面着色器的问题。
这是我的代码:
Shader "Custom/Curve" {
Properties{
_Color("Color", Color) = (1,1,1,1)
_MainTex("Albedo (RGB)", 2D) = "white" {}
_Glossiness("Smoothness", Range(0,1)) = 0.5
_Metallic("Metallic", Range(0,1)) = 0.0
_QOffset("Offset", Vector) = (0,0,0,0)
_Dist("Distance", Float) = 100.0
}
SubShader{
Tags{ "RenderType" = "Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows vertex:vert addshadow
#pragma target 3.0
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
float4 _QOffset;
float _Dist;
void vert(inout appdata_full v)
{
v.position.x += 10;
}
void surf(Input IN, inout SurfaceOutputStandard o) {
// Albedo comes from a texture tinted by color
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
正如您现在在顶点着色器中看到的那样,我只是想在顶点的 X 位置添加一些单位,因为我认为如果我实现了这一点,那么“曲线变化”将是微不足道的,但如果你认为它如果您警告我,我将不胜感激。
【问题讨论】: