Unity3d 相对方向的统一性
我正在尝试编写一个类似于Unity手册的着色器,该着色器通过Clip()函数丢弃几乎水平环中的像素来“切片”对象Unity3d 相对方向的统一性,unity3d,coordinates,shader,hlsl,cg,Unity3d,Coordinates,Shader,Hlsl,Cg,我正在尝试编写一个类似于Unity手册的着色器,该着色器通过Clip()函数丢弃几乎水平环中的像素来“切片”对象 Shader "Example/Slices" { Properties { _MainTex ("Texture", 2D) = "white" {} _BumpMap ("Bumpmap", 2D) = "bump" {} } SubShader { Tags { "RenderType" = "Opaque" }
Shader "Example/Slices" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_BumpMap ("Bumpmap", 2D) = "bump" {}
}
SubShader {
Tags { "RenderType" = "Opaque" }
Cull Off
CGPROGRAM
#pragma surface surf Lambert
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
float3 worldPos;
};
sampler2D _MainTex;
sampler2D _BumpMap;
void surf (Input IN, inout SurfaceOutput o) {
clip (frac((IN.worldPos.y+IN.worldPos.z*0.1) * 5) - 0.5);
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
}
ENDCG
}
Fallback "Diffuse"
}
我希望能够以任意角度进行切片,而不仅仅是水平线。我通过实验发现(因为我不熟悉着色器编码),worldPos Z上的乘数确实会改变切片的角度,因此我为其设置了一个属性变量:
clip (frac((IN.worldPos.y+IN.worldPos.z*_MYANGLEVARIABLE) * 5) - 0.5);
然而,这有两个问题。1) 值高达1.0时,线旋转最多45度,但超过该值时,线开始“扭曲”并卷积成各种图案,而不是整齐的线;2)仅当面朝向正X轴或负X轴时,此操作才有效。当面对Z时,直线不移动;当面对Y时,直线变大,但不旋转
将.worldPos.y中的更改为.worldPos.x中的会执行您可能期望的操作-类似的情况,但在Z而不是x中按预期工作
你知道怎么做吗
1) 实现任意角度?
2) 让他们不管朝哪个方向工作
我之所以使用worlPos,是因为我总是希望线条相对于对象而不是屏幕空间,但也许还有另一种方式?我的实际着色器是一个片段,而不是一个曲面着色器&我将worldPos从顶点传递到frag
非常感谢要获得任意角度,您可以使用法向量定义平面,并使用点积进行剪裁
Shader "Example/Slices" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_BumpMap ("Bumpmap", 2D) = "bump" {}
_PlaneNormal ("Plane Normal", Vector) = (0, 1, 0)
}
SubShader {
Tags { "RenderType" = "Opaque" }
Cull Off
CGPROGRAM
#pragma surface surf Lambert
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
float3 worldPos;
};
sampler2D _MainTex;
sampler2D _BumpMap;
float3 _PlaneNormal;
void surf (Input IN, inout SurfaceOutput o) {
float d = dot(_PlaneNormal, IN.worldPos);
clip (frac(d) - 0.5);
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
}
ENDCG
}
Fallback "Diffuse"
}
这里的法向量越长,切片的频率就越高
如果希望切片相对于对象,则需要使用除worldPos之外的一组坐标。这个答案可能会有帮助:要获得任意角度,可以使用法向量定义平面,并使用点积剪裁
Shader "Example/Slices" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_BumpMap ("Bumpmap", 2D) = "bump" {}
_PlaneNormal ("Plane Normal", Vector) = (0, 1, 0)
}
SubShader {
Tags { "RenderType" = "Opaque" }
Cull Off
CGPROGRAM
#pragma surface surf Lambert
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
float3 worldPos;
};
sampler2D _MainTex;
sampler2D _BumpMap;
float3 _PlaneNormal;
void surf (Input IN, inout SurfaceOutput o) {
float d = dot(_PlaneNormal, IN.worldPos);
clip (frac(d) - 0.5);
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
}
ENDCG
}
Fallback "Diffuse"
}
这里的法向量越长,切片的频率就越高
如果希望切片相对于对象,则需要使用除worldPos之外的一组坐标。这个答案可能会有帮助: