将顶点着色器更改发送到曲面着色器

时间:2018-05-24 15:04:12

标签: unity3d shader

我尝试使用着色器像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位置,因为我认为如果我实现了那么应用了#34;曲线变化"这将是微不足道的,但如果你认为这不会那么容易,如果你警告我,我将不胜感激。

2 个答案:

答案 0 :(得分:2)

编辑:

这是团结2018.1的一个例子 Color

float4 worldPosition = mul(unity_ObjectToWorld, v.vertex); 
// get world space position of vertex
half2 wpToCam = _WorldSpaceCameraPos.xz - worldPosition.xz; 
// get vector to camera and dismiss vertical component
half distance = dot(wpToCam, wpToCam); 
// distance squared from vertex to the camera, this power gives the curvature
worldPosition.y -= distance * _Curvature; 
// offset vertical position by factor and square of distance.
// the default 0.01 would lower the position by 1cm at 1m distance, 1m at 10m and 100m at 100m
v.vertex = mul(unity_WorldToObject, worldPosition); 
// reproject position into object space

https://gist.github.com/bricevdm/caaace3cce9a87e081602ffd08dee1ad

您正在混合常规CG着色器和Surface着色器。来自github的样本是前者。 Surface着色器中的v.vertex预计位于对象空间中,与CG着色器中的float4 pos : SV_POSITION不同,后者应位于其最终 - 剪辑/屏幕空间中 - 位置。

解决方案是将转换反转回对象空间。在您的情况下,您需要从相机中显示反投影矩阵。

替换v.vertex = mul (UNITY_MATRIX_P, vPos); 使用此矩阵:screen capture from unity editor。由于您使用UNITY_MATRIX_MV将顶点从对象转换为相机空间,因此您需要先使用unity_WorldToObject(或更好地将两者结合在cpu上)反转到世界,然后再反转到对象空间。

但是使用已经提供的矩阵计算世界空间中的曲率实际上要容易得多:

https://docs.unity3d.com/ScriptReference/Camera-cameraToWorldMatrix.html

  

unity_ObjectToWorld当前模型矩阵。

     

unity_WorldToObject当前世界矩阵的逆。

答案 1 :(得分:0)

这是因为appdata_full struct中的位置没有值:

struct appdata_full {
    float4 vertex : POSITION;
    float4 tangent : TANGENT;
    float3 normal : NORMAL;
    float4 texcoord : TEXCOORD0;
    float4 texcoord1 : TEXCOORD1;
    fixed4 color : COLOR;
#if defined(SHADER_API_XBOX360)
    half4 texcoord2 : TEXCOORD2;
    half4 texcoord3 : TEXCOORD3;
    half4 texcoord4 : TEXCOORD4;
    half4 texcoord5 : TEXCOORD5;
#endif
};

而不是v.position使用v.vertex这样:

void vert (inout appdata_full v, out Input o){
            UNITY_INITIALIZE_OUTPUT(Input,o);
            v.vertex += 10;
}

这里是曲线着色器的表面版本:

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, out Input o){
            UNITY_INITIALIZE_OUTPUT(Input,o);
            float4 vPos = mul (UNITY_MATRIX_MV, v.vertex);
            float zOff = vPos.z/_Dist;
            vPos += _QOffset*zOff*zOff;
            v.vertex = mul (UNITY_MATRIX_P, vPos);
            v.texcoord = v.texcoord;
}

 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"
 }