Unity Advanced Textures

cube texture

In graphics, cube textures are an implementation of environment mapping. Environment mapping can simulate the environment around an object, and objects using environment mapping can appear to reflect the surrounding environment as if they were plated with a layer of metal.

Different from the previous texture, the cube texture contains a total of 6 images, these images correspond to the 6 faces of a cube, and the name of the cube texture comes from this. Each face of the cube represents the image viewed along the axis in world space (up, down, left, right, front, and back). For cube texture sampling we need to provide a 3D texture coordinate that represents a 3D orientation in our world space. This direction vector starts from the center of the cube, and as it extends outward, it intersects one of the cube's 6 textures, and the sampled result is calculated from this intersection.

The advantage of using cube textures is that it is simple and fast to implement, and the results are also cost-effective. But it has some drawbacks, such as when new objects, light sources, are introduced into the scene. Or when the object is displaced, we need to regenerate the texture of the cube. In addition, cube textures can also reflect the environment, but not the object itself that uses the cube texture.

Cube textures have many applications in real-time rendering, the most common being skyboxes and environment mapping.

sky box

The skybox is a method used in the play to simulate Beijing. The name skybox has two messages: it is used to simulate the sky, and it is a box. When we use a skybox in a scene, the whole scene is enclosed within a cube. The technique used for each face of this cube is the cube texture mapping technique.

Unity is very simple to use skyboxes. We just need to create a Skybox material and assign it to the scene-related settings.

Create a cube texture for environment reflections

Aside from skyboxes, cube textures are most commonly used for environment mapping. In this way, we can simulate the material of metallic texture.

There are three ways to create a cube texture for environment mapping: the first method is to create it directly from some special layout textures; the second method is to manually create a Cubemap resource and assign 6 images to it; the third method The method is generated by the script.

If we use the first method, we need to provide a texture with a special layout, and then we only need to set the Texture Type of the texture to Cubemap, and Unity will take care of the next thing automatically.

The second method is that we first need to create a Cubemap in the project resources, and then drag and drop 6 textures into his panel.

The first two methods require us to provide the image of the prepared cube texture, and the cube texture they get is often shared by the colored objects in the scene. But in an ideal situation, I dream of generating different cube textures according to the different positions of the objects in the scene. At this time, we can use scripts to create them in Unity. This is achieved by using the Camera.renderToCubemap function provided by Unity.

reflection

Objects that use reflections appear to be metallized. To imitate the effect of reflection is very simple, we only need to calculate the reflection direction by the direction of the incident light and the surface inversion, and then use the reflection direction to sample the cube texture.

refraction

The physical principle of refraction is more complicated than that of reflection. We have been exposed to the definition of refraction in junior high school physics; when light is incident from one medium obliquely into another medium, the propagation direction generally changes. When given the angle of incidence, we can use Snell's law to calculate the angle of reflection. When light enters medium 2 obliquely from medium 1 along the direction of the angle θ1 with the surface normal, we can use the following formula to calculate the angle θ2 between the refracted ray and the normal

 

n1, n2 are the refractive indices of the two media, respectively. The refractive index is an important physical constant. For example, the refractive index of vacuum is 1, while the refractive index of glass is generally 1.5.

Usually, when we get the refraction direction, we directly use it to sample the cube texture, but this is against the laws of physics. For a transparent object, a more accurate way of simulating it requires calculating two refractions, one when light enters its interior and one when it exits. However, it is more complicated to simulate the second refraction inversion in real-time rendering, and the effect obtained by only simulating once is visually "pretty good".

 

Fresnel reflection

In real-time rendering, we usually use the Fresnel reflection to control the degree of reflection based on the viewing direction. In layman's terms, Phesnel reflection describes an optical phenomenon, that is, when light hits the surface of an object, part of it is reflected, and part of it enters the object and is refracted or scattered. There is a certain ratio between the reflected light and the incident light. This ratio relationship can be calculated by the Pheinel equation. A frequently used example is when you stand on the edge of a lake and look up directly at the water at your feet, you will find that the water is almost transparent, and you can directly see small fish and stones at the bottom; but when you look up far away When you are on the water surface, you will find that you can hardly see the underwater scene, and you can only see the environment reflected by the water surface. This is the so-called Phineel effect, which is a very important specular reflection factor in physically based rendering.

The real-world Phineer equation is very complex, but in real-time rendering, we usually use some approximate equations to calculate. One of the well-known approximate equations is the Schlick-Finel approximation equation:

 

where f0 is a reflection coefficient that controls the strength of the Fresnel reflection, v is the viewing direction, and n is the surface normal. Another widely used equation is the Empricial Fresnel approximation equation.

 

Using the Fresnel approximation equations above, we can simulate reflected light intensity and refracted lighting at the boundary. Variation between diffuse light intensities. In many material renderings such as car paint and water surface, we often use Phineel reflection to simulate a more realistic reflection effect.

render texture

The result of a camera's rendering is output to the color buffer and displayed on our screen. Modern GPUs allow us to render the entire 3D scene into an intermediate buffer, the render target texture, instead of the traditional framebuffer or backbuffer. Related to this is multiple render targets, a technique that refers to the fact that the GPU allows us to render the scene to multiple render target textures at the same time, rather than needing to render the complete scene separately for each render target texture. Deferred rendering is one application that uses multiple render targets.

Unity defines a special texture type for render targets - render textures. There are usually two ways to use rendering textures in Unity: one way is to create a rendering texture in the Project directory, and then set the rendering target of a camera to the rendering texture, so that the rendering result of the camera will be real-time. Updates to render textures without showing on screen. Using this method, we can also choose texture properties such as the resolution of the rendered texture, filtering mode, etc. Another way is to use the GrabPass command or the OnRenderImage function to get the current screen image during screen post-processing. Unity will put this screen image into a rendering texture equal to the screen resolution. Next, we can use the custom Pass They are treated as ordinary textures, so as to achieve various screen effects. We will learn how to implement these two methods in Unity in turn.

Procedural textures

Procedural textures refer to those images that are generated by a computer. We usually use some specific algorithms to create personalized patterns or very realistic natural elements. The advantage of using procedural textures is that you can use various parameters to control the appearance of the texture , and these properties are not only those color properties, but can even be completely different types of pattern properties, which allows us to get richer animations and visual effects.

Guess you like

Origin blog.csdn.net/f402455894/article/details/123269472