程序流程
使用内置函数说明:
vec2 floor (vec2 x): Returns a value equal to the nearest integer that
is less than or equal to x.
vec2 fract (vec2 x): Returns x – floor (x).
vec2 step (vec2 edge, vec2 x): Returns 0 if x <= edge; otherwise, it returns 1.0.
vec3 mix (vec3 x, vec3 y, vec3 a): Returns x * (1.0 – a) + y * a, i.e., the linear
blend of x and y using the floating-point value a.
The value for a is not restricted to the range [0,1].
得到当前片断在整个砖块图案中的位置。(单位长度1 = 一个砖块的尺寸)
position = MCposition / BrickSize;
每隔一行,偏移半个砖块宽度。产生交错感。
if (fract(position.y * 0.5) > 0.5)
position.x += 0.5;
得到当前片断在砖块图的一个图元中的相对位置。
position = fract(position);
判断当前片断处于砖块位置还是灰泥位置,处于砖块位置时为1,处于灰泥位置时为0。
useBrick = step(position, BrickPct);
根据当前片断的位置,得到当前片断的颜色值。
color = mix(MortarColor, BrickColor, useBrick.x * useBrick.y);
在颜色中加入光照效果。
color *= LightIntensity;
将计算好的颜色赋给当前片断。
gl_FragColor = vec4 (color, 1.0);
vec2 floor (vec2 x): Returns a value equal to the nearest integer that
is less than or equal to x.
vec2 fract (vec2 x): Returns x – floor (x).
vec2 step (vec2 edge, vec2 x): Returns 0 if x <= edge; otherwise, it returns 1.0.
vec3 mix (vec3 x, vec3 y, vec3 a): Returns x * (1.0 – a) + y * a, i.e., the linear
blend of x and y using the floating-point value a.
The value for a is not restricted to the range [0,1].
得到当前片断在整个砖块图案中的位置。(单位长度1 = 一个砖块的尺寸)
position = MCposition / BrickSize;
每隔一行,偏移半个砖块宽度。产生交错感。
if (fract(position.y * 0.5) > 0.5)
position.x += 0.5;
得到当前片断在砖块图的一个图元中的相对位置。
position = fract(position);
判断当前片断处于砖块位置还是灰泥位置,处于砖块位置时为1,处于灰泥位置时为0。
useBrick = step(position, BrickPct);
根据当前片断的位置,得到当前片断的颜色值。
color = mix(MortarColor, BrickColor, useBrick.x * useBrick.y);
在颜色中加入光照效果。
color *= LightIntensity;
将计算好的颜色赋给当前片断。
gl_FragColor = vec4 (color, 1.0);
程序清单
uniform vec3 BrickColor, MortarColor;
uniform vec2 BrickSize;
uniform vec2 BrickPct;
varying vec2 MCposition;
varying float LightIntensity;
void main(void){
vec3 color;
vec2 position, useBrick;
position = MCposition / BrickSize;
if (fract(position.y * 0.5) > 0.5)
position.x += 0.5;
position = fract(position);
useBrick = step(position, BrickPct);
color = mix(MortarColor, BrickColor, useBrick.x * useBrick.y);
color *= LightIntensity;
gl_FragColor = vec4 (color, 1.0);
}
uniform vec2 BrickSize;
uniform vec2 BrickPct;
varying vec2 MCposition;
varying float LightIntensity;
void main(void){
vec3 color;
vec2 position, useBrick;
position = MCposition / BrickSize;
if (fract(position.y * 0.5) > 0.5)
position.x += 0.5;
position = fract(position);
useBrick = step(position, BrickPct);
color = mix(MortarColor, BrickColor, useBrick.x * useBrick.y);
color *= LightIntensity;
gl_FragColor = vec4 (color, 1.0);
}