Reprinted from the Wind Yu Chong Unity3d Tutorial College
IntroductionReflection Mapping, also called Environment Mapping. Corresponds to Texgen spheremap and Cubereflect. is the fastest reflect the algorithm of the surrounding environment first appeared to beSphere Mapping, and then beCube MappingReplace. Reflection Mapping than ray tracing (ray tracing) is much more efficient Reflection mapping premise is that 2 hypothesis (1) into the ray from the Infinite Distance (2) The object is convex, without its own mutual reflection
Sphere MappingOutdated technology, advantages: 1 High Efficiency 2 can also be used with the common mapping disadvantage: 1. Only the fish eye map, and in the perspective of the orthogonal camera is the best effect. Other situations, such as using a normal map, are generally effective in other locations. 2 wasted 4 corners of the map, texture values outside the circle area do not affect the result. Principle: Coding the colors in each direction in a flat texture image is equivalent to placing a polished, shiny sphere in the center of the environment, and then photographing it at a distance with a telephoto lens. The area that needs to be coded is a circular area covering the entire texture image, tangent to the top, bottom, left, and right edges of the texture image. Texture values outside this circular area do not affect the results because they are not used in the environment texture. Map: Put a polished perfect sphere in the center of the environment, take a picture with the camera of the fisheye lens, cut out a photograph along the top, bottom, left and right edge of the ball, then it is the decal. With the decal, in the camera coordinate system, according to I,n to derive R. The UV is then drawn by R. Even without the fish-eye map, there is a similar effect with a normal map. StickersFish Eye Map Normal map
Effect
Calculation method: 1 in the camera coordinate system according to I: into the amount and N: normal vector calculated r: Reflection Vector r = I-2 * N * DOT (n,i) 2 and then based on the fixed algorithm R calculated UV
Float2 R_to_uv (float3 R) { float interim = 2.0 * SQRT (r.x * r.x + r.y * r.y + (R.z + 1.0) * (R.z + 1.0)); Return Float2 (r.x/interim+0.5,r.y/interim+0.5); }
Fixed and Fragment Shader code
Shader "Custom/texgen_spheremap_fragmine" {Properties {_reflectivity ("reflectivity", Range (0,1)) = 0.5 _maintex ("Base", 2D) = "White" _environment ("Environment", 2D) = "White"} subshader {Pass { Cgprogram #pragma vertex vert #pragma fragment frag #include "Unitycg.cginc" Sampler2d _maintex; Sampler2d _environment; FLOAT4 _maintex_st; float _reflectivity; struct V2F {float4 pos:sv_position; FLOAT2 uv:texcoord0; FLOAT2 Uv2:texcoord1; } ; I: Into the amount of N: normal vector R: Reflection vector float3 reflect (float3 i,float3 N) {return i-2.0*n *dot (n,i ); }//Float2 R_to_uv (float3 R) {Float interim = 2.0 * SQRT (r.x * r.x + r.y * R.y + (R.z + 1.0) * (R.z + 1.0)); Return Float2 (r.x/interim+0.5,r.y/interim+0.5); } v2f Vert (Appdata_base v) {v2f o; O.pos = Mul (Unity_matrix_mvp,v.vertex); O.UV = Transform_tex (V.texcoord,_maintex); FLOAT3 poseyespace = Mul (Unity_matrix_mv,v.vertex). xyz; FLOAT3 I = POSEYESPACE-FLOAT3 (0,0,0); FLOAT3 N = Mul ((float3x3) unity_matrix_mv,v.normal); n = normalize (n); FLOAT3 R = Reflect (i,n); O.uv2 = R_to_uv (R); return o; } float4 Frag (v2f i): COLOR {float4 reflectivecolor = tex2d (_ENVIRONMENT,I.UV2); FLOAT4 Decalcolor = tex2d (_MAINTEX,I.UV); FLOAT4 OUTP = Lerp (decalcolor,reflectivecolor,_reflectivity); return OUTP; } ENDCG}}}
Cube MappingMainstream technology Advantages: a true reflection of the environment, not limited by the position angle. Cons: less efficient than sphere mappingprinciple andcalculation Method: 1 in the world coordinate system according to I: into the amount and N: normal vector calculated r: Reflection Vector r = I-2 * N * DOT (n,i) 2 again by R using texcube texture map to get color texcube (_ENVIRONMENT,I.R); Fixed and Fragment Shader code
Shader "Custom/texgen_cuber_fragmine" {Properties {_reflectivity ("reflectivity", Range (0,1)) = 0.5 _maintex ("Base", 2D) = "White" _environment ("Environment", Cube) = ' white '} subshader {Pass { Cgprogram #pragma vertex vert #pragma fragment frag #include "Unitycg.cginc" Sampler2d _maintex; Samplercube _environment; FLOAT4 _maintex_st; float _reflectivity; struct V2F {float4 pos:sv_position; FLOAT2 uv:texcoord0; FLOAT3 R:texcoord1; } ; FLOAT3 reflect (FLOAT3 i,float3 N) {return i-2.0*n *dot (n,i); } v2f Vert (Appdata_base v) {v2f o; O.pos = Mul (Unity_matrix_mvp,v.vertex); O.UV = Transform_tex (V.texcoord,_maintex); FLOAT3 PosW = Mul (_objeCt2world,v.vertex). xyz; FLOAT3 I = PosW-_worldspacecamerapos.xyz; FLOAT3 N = Mul ((float3x3) _object2world,v.normal); n = normalize (n); O.R = Reflect (i,n); return o; } float4 Frag (v2f i): COLOR {float4 reflectivecolor = Texcube (_ENVIRONMENT,I.R); FLOAT4 Decalcolor = tex2d (_MAINTEX,I.UV); FLOAT4 OUTP = Lerp (decalcolor,reflectivecolor,_reflectivity); return OUTP; } ENDCG}}}
Unity3d Tutorial Shader Chapter: 24th Reflection Mapping