"Unity Shaders" Learning Note--surfaceshader (ix) Cubemap

Source: Internet
Author: User

"Unity Shaders" Learning Note--surfaceshader (ix) Cubemap
    1. If you want to learn unity Shader from scratch, then you can take a look at this series of articles to get started, you just need a little bit of programming concept.

    2. The level is limited, inevitably has the fallacy, hoped to point out.

The methods for making CubeMap are described in the previous section. This section tells you how to use it.

Simple Cubemap

Let's take a look at the simplest cubemap.

Shader"Custom/simplereflection"{Properties {_maintint ("Diffuse Tint", Color) = (1,1,1,1) _maintex ("Base (RGB)", 2D) ="White"{} _cubemap ("CubeMap", CUBE) =""{} _reflamount ("Reflection Amount",Range(0.01, 1)) = 0.5} subshader {Tags {"Rendertype"="Opaque"} LOD Cgprogram #pragma surface surf Lambert sampler2d _maintex;        Samplercube _cubemap;        FLOAT4 _maintint;        float _reflamount; structInput{FLOAT2 Uv_maintex;        FLOAT3 WORLDREFL;        }; void Surf (Input inch, InOut surfaceoutput o) {half4 c = tex2d (_maintex,inch. Uv_maintex) * _MAINTINT; O.emission = Texcube (_cubemap,inch. WORLDREFL). RGB * _REFLAMOUNT;            O.albedo = C.rgb; O.Alpha= C.A; } ENDCG} FallBack"Diffuse"}

The Texcube () function is actually used to sample the cubemap.

The second parameter should pass in the UV coordinates, but the actual pass-through is the world reflection vector. This is unity that calculates the UV coordinates for us.
CubeMap is made up of six maps that make up a six-face like a sky box, and how do the six maps map to the ball, making the ball look like a reflection of the surrounding environment?
Think of CubeMap as a cube, wrapped in small balls, emitting a ray from the center of the ball, passing through the surface of the sphere and the surface of the cube, where the ray and the ball and cube each have an intersection, and the point on the ball corresponds to the texture of the point on the cube. So how do you calculate the UV coordinates of each point? One of the rays emitted from the center of the ball is the normal, and there is a clear fact that the normals are facing the polygon of the cube pointing to the axis with the most normal coordinate component. That is, the normals of the coordinates (1,1,3) face the polygon of the cube that the z-axis is pointing toward. This will find a point corresponding to the face of the normal. So how to calculate the UV coordinates? The other two smaller coordinate values of the normals are related to the UV coordinates. As an example of a special point, the coordinates of the ball's vertex normals should be (0,0,1), corresponding to the midpoint of the top of the cube, and the UV coordinates should be (0.5,0.5). The method of calculating the UV coordinates is (x/zx0.5+0.5,y/zx0.5+0.5), the special point is used, the answer is correct. The principle is a bit difficult to explain, equivalent to the x, Y coordinate projection to the corresponding surface. Multiply 0.5 plus 0.5 to change the range of normal coordinates from [ -1,1] to [0,1].
In unity, we don't have to calculate for ourselves, we can retrieve cubemap directly using the built-in variable WORLDREFL.

Normal Cubemap
Shader "Custom/normalmappedreflection" {Properties {_maintint ("Diffuse Tint", Color) = (1,1,1,1) _  Maintex ("Base (RGB)", 2D) = "White" {} _normalmap ("Normal Map", 2D) = "Bump" {} _cubemap ("Cubemap", CUBE) = "" {} _reflamount ("Reflection Amount", Range (0,1)) = 0.5} subshader {Tags {"Rendertype" =        "Opaque"} LOD cgprogram #pragma surface surf Lambert samplercube _cubemap;        Sampler2d _maintex;        Sampler2d _normalmap;        FLOAT4 _maintint;        float _reflamount;            struct Input {float2 Uv_maintex;            FLOAT2 Uv_normalmap;            FLOAT3 WORLDREFL;        Internal_data};                        void Surf (Input in, InOut surfaceoutput o) {half4 c = tex2d (_maintex, In.uv_maintex);            FLOAT3 normals = Unpacknormal (tex2d (_normalmap, In.uv_normalmap)). RGB;                O.normal = normals;        O.emission = Texcube (_cubemap, Worldreflectionvector (in, O.normal)). RGB * _REFLAMOUNT;            O.albedo = C.rgb * _MAINTINT;        O.alpha = C.A; } ENDCG} FallBack "Diffuse"}

It just adds the normal map.
Because the normal information is changed, the world reflection vector of the incoming Texcube () function is recalculated. FLOAT3 Worldrefl;internal_data variables are used when the original normal information is not used, such as using the normal map, the original normal information is not used. To calculate the world reflection vectors based on the new normal information, use the Worldreflectionvector () function.

Dynamic Cubic Chart System

Sometimes the objects in the game go from one environment to another and need to change the cubemap so that the reflected effect is true.
There are two ways to replace the cubemap, one is to replace the cubemap in real time, the effect is most real, but to sacrifice performance; the second is to change the cubemap when the object goes to another environment, that's the way I'm going to talk.
The method is very simple, is in C # with SetTexture method of dynamic replacement cubemap.

[Executeineditmode] Public classSwapcubemaps:monobehaviour { PublicCubemap Cubea; PublicCubemap Cubeb; PublicTransform PosA; PublicTransform PosB;PrivateMaterial Curmat;PrivateCubemap Curcube;// Use this for initialization     void Start ()      {        }//Update is called once per frame     void Update ()      {Curmat = renderer.sharedmaterial;if(Curmat)            {curcube = Checkprobedistance (); Curmat.settexture ("_cubemap", Curcube); }    } Private Cubemap checkprobedistance()     {floatDista = Vector3.distance (transform.position, posa.position);floatDISTB = Vector3.distance (transform.position, posb.position);if(Dista < DISTB) {returnCubea; }Else if(Distb < Dista) {returnCubeb; }Else{returnCubea; }            } void Ondrawgizmos()     {gizmos.color = Color.green;if(PosA) {Gizmos.drawwiresphere (posa.position,0.5f); }if(PosB) {Gizmos.drawwiresphere (posb.position,0.5f); }    }}
    1. [Executeineditmode] is to make the script in the editor state can also be executed, so that you do not have to click Play debugging, more convenient.

    2. Ondrawgizmos () is to draw some visual things in the scene to facilitate debugging.

    3. Checkprobedistance () uses distance to determine the distance between the object and point A and point B, depending on the distance, which type of cubemap is returned.

    4. Sets the cubemap of the material in update ().

CubeMap effect


Cubemap

I have the texture of a metal. The effect is like this.


Normalcubemap

This is a normal map added.

"Unity Shaders" Learning Note--surfaceshader (ix) Cubemap

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.