preface
An important step in texture mapping is texture sampling, which is the process of extracting the color of the corresponding position in the texture map according to the texture coordinates of the slice, as shown in the figure above
However, there is not always a one-to-one correspondence between the number of tiles being rendered and the number of pixels in the corresponding texture area
For example, in the red circle in the figure, a slice may cover multiple texture pixels or may cover less than one texture pixel. How to sample? In three, set the magFilter and minFilter properties of the texture to change the sampling method.
Bosses video reference link www.bilibili.com/video/BV1X7…
MagFilter properties
Usage scenarios
- A small texture stuck to a large space (e.g. a 16X16 texture mapped to a 32X32 pixel space) is equivalent to a texture enlargement
- The nearest element under perspective
In this case, a slice will cover less than one texture pixel (red circle).
The value of a settable property
THREE.NearestFilter
Nearest samplingTHREE.LinearFilter
Linear sampling default value
Attribute is introduced
NearestFilter nearest sampling method
Returns the value of the texture pixel closest to the specified texture coordinates (within the Manhattan distance).
The principle of
As shown in the figure, the range of S and T coordinates of the texture is 0-1. The texture itself is composed of discrete pixels. If each texture pixel is regarded as a small square, each pixel occupies a certain texture coordinate.
According to the texture coordinates of the slice, which pixel (small square) is calculated, and the color value of this pixel is directly taken as the sampling value of the nearest point sampling
The characteristics of
The computation is small, but it is easy to produce obvious serrations
LinearFilter linear sampling method
Returns the weighted average of the four texture elements closest to the specified texture coordinates
The principle of
Linear sampling takes into account 4 texture pixel values near texture coordinate points, and generally calculates the final sampling result according to area proportional weighting
The characteristics of
Because of the weighted average of pixels, it is excessively smooth
MinFilter zoom out filter
Usage scenarios
- A large texture stuck to a small space (e.g. a 32X32 texture mapped to a 16X16 pixel space) corresponds to texture reduction
- A distant slice in perspective
In this case, a slice will cover multiple texture pixels
The value of a settable property
THREE.NearestFilter
THREE.NearestMipmapNearestFilter
THREE.NearestMipmapLinearFilter
THREE.LinearFilter
THREE.LinearMipmapNearestFilter
THREE.LinearMipmapLinearFilter
The default value
NearestFilter, LinearFilter have been introduced above, the other four attributes are added to mipmap related operations, mipmap \ will be introduced later
NearestMipmapNearestFilter choice and are the most matching mipmap by the size of the texture pixels, and on the basis of NearestFilter value to generate texture. NearestMipmapLinearFilter choice and by the size of the texture pixels closest two mipmap, and on the basis of NearestFilter from each mipmap generated in texture and value. The final texture value is the weighted average of these two values. LinearMipmapNearestFilter choice and are the most matching mipmap by the size of the texture pixels, and with LinearFilter (closest to the pixel center weighted average of the four texture elements) as the standard values to generate texture.
LinearMipmapLinearFilter is the default. It selects the two MiPMaps closest to the size of the pixel being textured. LinearFilter is the standard to generate texture values from each MIPMap. The final texture value is the weighted average of these two values.
Mipmap introduction
The principle of
When minFilter is set to the LinearFilter, we find that the distance should be blurred, but the distant picture is clearer than the closer one.
Since they are all using the same resolution texture, the corresponding relationship in the distance is equivalent to the texture being reduced, which is naturally clearer than the texture amplified in the near place. However, if the texture is reduced too much, the distance will also produce sawtooth due to insufficient sampling rate.
One solution is to use lower-resolution textures at a distance and higher-resolution textures at a distance, which is the core idea of Mipmap
As shown in the figure above, when providing an original texture map with MiPMap, the system will automatically generate a series of texture maps from large to small when loading the texture. Each texture map is 1/2 the size of the previous one (1/4 of the area) until the size of the texture is reduced to 1X1
When the program runs, the rendering pipeline first calculates the levels, decides which texture maps to use, and then processes them according to the processing we choose, for example LinearMipmapLinearFilter selects the two texture maps closest to the level
The LinearMipmapLinearFilter is obviously blurred in the distance on the left, and the LinearFilter on the right
conclusion
The magFilter and minFilter attributes of the texture in Three have default values. The default values are generally reasonable and do not need to be modified. The principle is understood so that it can be better used in special cases