
This tutorial covers the rendering ofenvironment maps as backgrounds with the help of cube maps.
It is based onSection “Reflecting Surfaces”. If you haven't read that tutorial, this would be a very good time to read it.
As explained inSection “Reflecting Surfaces”, a skybox can be thought of as an infinitely large, textured box that surrounds a scene. Sometimes, skyboxes (or skydomes) are implemented by sufficiently large textured models, which approximate an infinitely large box (or dome). However,Section “Reflecting Surfaces” introduced the concept of a cube map, which actually represents an infinitely large box; thus, we don't need the approximation of a box or a dome of limited size. Instead, we can render any screen-filling model (it doesn't matter whether it is a box, a dome, or an apple tree as long as it covers the whole background), compute the view vector from the camera to the rasterized surface point in the vertex shader (as we did inSection “Reflecting Surfaces”) and then perform a lookup in the cube map with this view vector (instead of the reflected view vector inSection “Reflecting Surfaces”) in the fragment shader:
float4frag(vertexOutputinput):COLOR{returntexCUBE(_Cube,input.viewDir);}
For best performance we should, of course, render a model with only a few vertices and each pixel should be rasterized only once. Thus, rendering the inside of a cube that surrounds the camera (or the whole scene) is fine.
The shader should be attached to a material, which should be attached to a cube that surrounds the camera. In the shader code, we deactivate writing to the depth buffer withZWrite Off such that no objects are occluded by the skybox. (See the description of the depth test inSection “Per-Fragment Operations”.) Front-face culling is activated withCull Front such that only the “inside” of the cube is rasterized. (SeeSection “Cutaways”.) The lineTags { "Queue" = "Background" } instructs Unity to render this pass before other objects are rendered.
Shader"Cg shader for skybox"{Properties{_Cube("Environment Map",Cube)=""{}}SubShader{Tags{"Queue"="Background"}Pass{ZWriteOffCullFrontCGPROGRAM#pragma vertex vert#pragma fragment frag#include"UnityCG.cginc"// User-specified uniformsuniformsamplerCUBE_Cube;structvertexInput{float4vertex:POSITION;};structvertexOutput{float4pos:SV_POSITION;float3viewDir:TEXCOORD1;};vertexOutputvert(vertexInputinput){vertexOutputoutput;float4x4modelMatrix=unity_ObjectToWorld;output.viewDir=mul(modelMatrix,input.vertex).xyz-_WorldSpaceCameraPos;output.pos=UnityObjectToClipPos(input.vertex);returnoutput;}float4frag(vertexOutputinput):COLOR{returntexCUBE(_Cube,input.viewDir);}ENDCG}}}
The shader above illustrates how to render a skybox by rendering a cube around the camera with a specific shader. This is a very general approach. Unity, however, has its own skybox system that doesn't require any game object: you just specify the material with the skybox shader in the main menuWindow > Rendering > Lighting Settings > Scene > Skybox Material and Unity takes care of the rest. Unfortunately, we cannot use our shader for this system since we have to do the lookup in the cube texture at the position that is specified by the vertex texture coordinates. This is actually easier than computing the view direction. Here is the code:
Shader"Cg shader for Unity-specific skybox"{Properties{_Cube("Environment Map",Cube)="white"{}}SubShader{Tags{"Queue"="Background"}Pass{ZWriteOffCullOffCGPROGRAM#pragma vertex vert#pragma fragment frag// User-specified uniformssamplerCUBE_Cube;structvertexInput{float4vertex:POSITION;float3texcoord:TEXCOORD0;};structvertexOutput{float4vertex:SV_POSITION;float3texcoord:TEXCOORD0;};vertexOutputvert(vertexInputinput){vertexOutputoutput;output.vertex=UnityObjectToClipPos(input.vertex);output.texcoord=input.texcoord;returnoutput;}fixed4frag(vertexOutputinput):COLOR{returntexCUBE(_Cube,input.texcoord);}ENDCG}}}
As mentioned above, you should create a material with this shader and drag the material toWindow > Rendering > Lighting Settings > Scene > Skybox Material. There is no need to attach the material to any game object.
Congratulations, you have reached the end of another tutorial! We have seen:
If you still want to know more