目录索引
译文
此函数返回一个不大于其参数的整数值,即没有小数位数的标量或矢量,向下取整,例如1.97f的底数返回1;为什么?因为这个函数将一个数字的小数从总数中减去。
floor (1.56) = 1 it’s the same as 1.56f – 0.56f.
floor (0.34) = 0
floor (2.99) = 2
语法如下所示:
float floor (float n)
{
float fn;
fn = n - frac(n);
return fn;
}
float2 floor (float2 n);
float3 floor (float3 n);
float4 floor (float4 n);
![图片[1]-《Unity着色器圣经》4.1.4. | Floor function.-软件开发学习笔记](https://gamedevfan.cn/wp-content/uploads/2025/05/image-63-1024x430.jpeg)
与ceil函数相反,当使用实心颜色块(例如卡通着色器或重复图案)创建视觉效果时,floor非常有用。
举例来说,让我们了解实现卡通着色器的原理。从这个意义上讲,我们将生成一种新的着色器类型“Unlit shader”,我们将其称为USB_functions_FLOOR。
我们将首先在着色器中添加两个属性:第一个属性用于生成多个分割,第二个属性用于增加输出颜色中的gamma。
Shader "USB/USB_function_FLOOR"
{
Properties
{
_MainTex ("Texture", 2D) = "white"{}
[IntRange]_Sections ("Sections", Range(2, 10)) = 5
_Gamma ("Gamma", Range(0, 1)) = 0
}
SubShader { ... }
}
属性部分定义如上所示: _ sections变量定义了[IntRange]。与之前的过程一样,我们必须在Pass中包含用来连接Properties的全局变量,以便在ShaderLab和我们的程序之间实现直接通信。
Pass
{
CGPROGRAM
...
sampler2D _MainTex;
float4 _MainTex_ST;
float _Sections;
float _Gamma;
...
ENDCG
}
接下来,我们将进入片段着色器阶段,并声明一个新变量,该变量将根据UV的V坐标生成纯色块。
fixed4 frag (v2f i) : SV_Target
{
float fv = floor(i.uv.y);
fixed4 col = tex2D(_MainTex, i.uv);
return col;
}
在上一次操作中,变量“fv”已声明并初始化,只有一个维度。其值等于V的floor结果;因此,它等于“零”。这是因为UV坐标从0.0f开始,到1.0f结束,正如我们已经知道的,这个函数返回一个不大于其参数的整数。
我们可以通过指定一个四维向量作为输出来证实这一运算,其中前三个向量等于“fv”的值
fixed4 frag (v2f i) : SV_Target
{
float fv = floor(i.uv.y);
// fixed4 col = tex2D(_MainTex, i.uv);
// return col;
return float4(fv.xxx, 1);
}
![图片[2]-《Unity着色器圣经》4.1.4. | Floor function.-软件开发学习笔记](https://gamedevfan.cn/wp-content/uploads/2025/05/image-62-1024x392.jpeg)
要添加纯色块,我们可以简单地将运算乘以一定数量的sections,然后除以一个十进制值。
至于gamma,我们可以将属性直接添加到输出颜色中。
fixed4 frag (v2f i) : SV_Target
{
float fv = floor(i.uv.y * _Sections) * (_Sections / 100);
// fixed4 col = tex2D(_MainTex, i.uv);
// return col;
return float4(fv.xxx, 1) + _Gamma;
}
![图片[3]-《Unity着色器圣经》4.1.4. | Floor function.-软件开发学习笔记](https://gamedevfan.cn/wp-content/uploads/2025/05/image-61-1024x392.jpeg)
卡通着色器的实现原理是相同的,不同之处在于我们在计算中使用全局照明而不是V坐标。
在相应的章节, 会有自定义光照的详细介绍.
原文对照
This function returns an integer value not greater than its argument, i.e., a scalar or vector number without decimal places, rounded down, e.g., the floor of 1.97f returns 1; why? Because this function subtracts the decimals of a number from its total.
floor (1.56) = 1 it’s the same as 1.56f – 0.56f.
floor (0.34) = 0
floor (2.99) = 2
Its syntax is as follows:
float floor (float n)
{
float fn;
fn = n - frac(n);
return fn;
}
float2 floor (float2 n);
float3 floor (float3 n);
float4 floor (float4 n);
![图片[1]-《Unity着色器圣经》4.1.4. | Floor function.-软件开发学习笔记](https://gamedevfan.cn/wp-content/uploads/2025/05/image-63-1024x430.jpeg)
Contrary to the ceil function, the floor is quite useful when creating visual effects with solid blocks of color, e.g., toon shader or repeating patterns in general.
To exemplify, let’s understand the principle of implementing a toon shader. In this sense, we are going to generate a new shader type, “Unlit Shader,” which we will call USB_functions_ FLOOR.
We will start by adding two properties in our shader: we will use the first to generate multiple splits and the second to increase the gamma in the output color.
Shader "USB/USB_function_FLOOR"
{
Properties
{
_MainTex ("Texture", 2D) = "white"{}
[IntRange]_Sections ("Sections", Range(2, 10)) = 5
_Gamma ("Gamma", Range(0, 1)) = 0
}
SubShader { ... }
}
Since the sections must be aggregated uniformly, [IntRange] has been defined for the _ Sections variable. As in previous processes, we must include the global variables inside the Pass so that we will achieve direct communication between ShaderLab and our program.
Pass
{
CGPROGRAM
...
sampler2D _MainTex;
float4 _MainTex_ST;
float _Sections;
float _Gamma;
...
ENDCG
}
Next, we will go to the fragment shader stage and declare a new variable, which will generate solid color blocks according to the V coordinate of the UV.
fixed4 frag (v2f i) : SV_Target
{
float fv = floor(i.uv.y);
fixed4 col = tex2D(_MainTex, i.uv);
return col;
}
In the previous operation, the variable “fv” has been declared and initialized, with only one dimension. Its value is equal to the floor result of V; hence, it is equal to “zero.” it’s because the UV coordinates start at 0.0f and end at 1.0f, and as we already know, this function returns an integer not greater than its argument.
We can corroborate the operation by assigning a four-dimensional vector as output, where the first three are equal to the value of “fv.”
fixed4 frag (v2f i) : SV_Target
{
float fv = floor(i.uv.y);
// fixed4 col = tex2D(_MainTex, i.uv);
// return col;
return float4(fv.xxx, 1);
}
![图片[2]-《Unity着色器圣经》4.1.4. | Floor function.-软件开发学习笔记](https://gamedevfan.cn/wp-content/uploads/2025/05/image-62-1024x392.jpeg)
To add solid color blocks, we can simply multiply the operation by a certain number of sections and then divide by a decimal value.
As for gamma, we can add the property directly to the output color.
fixed4 frag (v2f i) : SV_Target
{
float fv = floor(i.uv.y * _Sections) * (_Sections / 100);
// fixed4 col = tex2D(_MainTex, i.uv);
// return col;
return float4(fv.xxx, 1) + _Gamma;
}
![图片[3]-《Unity着色器圣经》4.1.4. | Floor function.-软件开发学习笔记](https://gamedevfan.cn/wp-content/uploads/2025/05/image-61-1024x392.jpeg)
The implementation principle is the same for a toon shader, with the difference that we use global illumination in the calculation instead of the V-coordinate.
In the second chapter of this book, we will review the custom lighting implementation in detail.
暂无评论内容