在 Unity 中使用 ShaderLab 以椭圆形状裁剪区域
Clip the region in Ellipse shape using ShaderLab in Unity
我正在尝试在 Unity 中使用着色器绘制椭圆体
按照步骤
1. 我在一些互联网教程的帮助下编写了一个自定义着色器
2.在材质上添加了着色器
3. 将该材质应用于对象
Shader "Custom/HoleMaker" {
Properties{
_Position("Position", Vector) = (0,0,0,0)
_Radius("Radius", Range(0,1)) = 0.01
_Color("Color", Color) = (1,1,1,1)
_CutawayColor("Cutaway Color", Color) = (1,1,1,1)
}
SubShader{
Tags { "RenderType" = "Opaque"}
LOD 200
Cull Front
CGPROGRAMtypes
#pragma surface surf MonoColor fullforwardshadows vertex:vert
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
fixed4 _CutawayColor;
float4 _Position;
float _Radius;
struct Input
{
float2 uv_MainTex;
float3 worldPos;
};
void vert(inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
v.normal *= -1;
}
half4 LightingMonoColor(SurfaceOutput s, half3 lightDir, half atten) {
return _CutawayColor;
}
void surf(Input IN, inout SurfaceOutput o)
{
//spherical clipping
float dist = distance(IN.worldPos.xyz, _Position.xyz);
if (dist < _Radius)
clip(-1);
o.Albedo = _CutawayColor.rgb;
}
ENDCG
}
FallBack "Diffuse"
}
此代码可用于制作圆,但当我尝试使用函数 clip() 制作椭圆体时,它不起作用。根据文档, clip(float4 x) 也可用于裁剪表面。变量 _Radius 在世界 Space 中形成了一个完美的球体,但是当我尝试使用 _Radius.x 和 _Radius.y 时,代码不起作用。请参考随附的图片。
_Radius.x
和 _Radius.y
破坏着色器,因为 _Radius
是 float
。它没有 x
或 y
成员。它怎么可能有 _Radius.y
的值?
相反,考虑添加一个 Scale
属性,然后将世界位置与 _Position
之间的差异缩放该数量,然后再将差异的大小与 [=20= 进行比较]:
Shader "Custom/HoleMaker" {
Properties{
_Position("Position", Vector) = (0,0,0,0)
_Scale("Scale", Vector) = (1,1,1,0)
_Radius("Radius", Range(0,1)) = 0.01
_Color("Color", Color) = (1,1,1,1)
_CutawayColor("Cutaway Color", Color) = (1,1,1,1)
}
SubShader{
Tags { "RenderType" = "Opaque"}
LOD 200
Cull Front
CGPROGRAMtypes
#pragma surface surf MonoColor fullforwardshadows vertex:vert
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
fixed4 _CutawayColor;
float4 _Position;
float4 _Scale;
float _Radius;
struct Input
{
float2 uv_MainTex;
float3 worldPos;
};
void vert(inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
v.normal *= -1;
}
half4 LightingMonoColor(SurfaceOutput s, half3 lightDir, half atten) {
return _CutawayColor;
}
void surf(Input IN, inout SurfaceOutput o)
{
//spherical clipping
float dist = length(_Scale.xyz * IN.worldPos.xyz - _Position.xyz);
if (dist < _Radius)
clip(-1);
o.Albedo = _CutawayColor.rgb;
}
ENDCG
}
FallBack "Diffuse"
}
我正在尝试在 Unity 中使用着色器绘制椭圆体
按照步骤
1. 我在一些互联网教程的帮助下编写了一个自定义着色器
2.在材质上添加了着色器
3. 将该材质应用于对象
Shader "Custom/HoleMaker" {
Properties{
_Position("Position", Vector) = (0,0,0,0)
_Radius("Radius", Range(0,1)) = 0.01
_Color("Color", Color) = (1,1,1,1)
_CutawayColor("Cutaway Color", Color) = (1,1,1,1)
}
SubShader{
Tags { "RenderType" = "Opaque"}
LOD 200
Cull Front
CGPROGRAMtypes
#pragma surface surf MonoColor fullforwardshadows vertex:vert
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
fixed4 _CutawayColor;
float4 _Position;
float _Radius;
struct Input
{
float2 uv_MainTex;
float3 worldPos;
};
void vert(inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
v.normal *= -1;
}
half4 LightingMonoColor(SurfaceOutput s, half3 lightDir, half atten) {
return _CutawayColor;
}
void surf(Input IN, inout SurfaceOutput o)
{
//spherical clipping
float dist = distance(IN.worldPos.xyz, _Position.xyz);
if (dist < _Radius)
clip(-1);
o.Albedo = _CutawayColor.rgb;
}
ENDCG
}
FallBack "Diffuse"
}
此代码可用于制作圆,但当我尝试使用函数 clip() 制作椭圆体时,它不起作用。根据文档, clip(float4 x) 也可用于裁剪表面。变量 _Radius 在世界 Space 中形成了一个完美的球体,但是当我尝试使用 _Radius.x 和 _Radius.y 时,代码不起作用。请参考随附的图片。
_Radius.x
和 _Radius.y
破坏着色器,因为 _Radius
是 float
。它没有 x
或 y
成员。它怎么可能有 _Radius.y
的值?
相反,考虑添加一个 Scale
属性,然后将世界位置与 _Position
之间的差异缩放该数量,然后再将差异的大小与 [=20= 进行比较]:
Shader "Custom/HoleMaker" {
Properties{
_Position("Position", Vector) = (0,0,0,0)
_Scale("Scale", Vector) = (1,1,1,0)
_Radius("Radius", Range(0,1)) = 0.01
_Color("Color", Color) = (1,1,1,1)
_CutawayColor("Cutaway Color", Color) = (1,1,1,1)
}
SubShader{
Tags { "RenderType" = "Opaque"}
LOD 200
Cull Front
CGPROGRAMtypes
#pragma surface surf MonoColor fullforwardshadows vertex:vert
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
fixed4 _CutawayColor;
float4 _Position;
float4 _Scale;
float _Radius;
struct Input
{
float2 uv_MainTex;
float3 worldPos;
};
void vert(inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
v.normal *= -1;
}
half4 LightingMonoColor(SurfaceOutput s, half3 lightDir, half atten) {
return _CutawayColor;
}
void surf(Input IN, inout SurfaceOutput o)
{
//spherical clipping
float dist = length(_Scale.xyz * IN.worldPos.xyz - _Position.xyz);
if (dist < _Radius)
clip(-1);
o.Albedo = _CutawayColor.rgb;
}
ENDCG
}
FallBack "Diffuse"
}