Start Learning Chapter 4th-Reflection of shaders
After reading the section 1, 2, to record. The reflection is mainly using the Cubemap cube map.
Meet CubeMap
The cube map, as the name says, has 6 pictures on a cube, so to speak.
Imagine that in a bright room, there is a surface is a mirror of the ball, then the surface of the ball reflects all the things in the room, is a large convex mirror.
This is to find a picture on the Internet, very intuitive expression of my meaning ...
Note in the title, the static cube map, why is called static, because this time using the cube map is produced in advance of the good picture, rather than dynamically generated.
What does that mean?
Take the scene in the picture above, if it is a static cube map, then when the ball is moving, the ball above the object will not be changed.
In real life words, the ball movement, the ball above the content should also be changed.
So here is the use of static cube map, is to learn the knowledge of the cube map, the following will learn the dynamic cube map, in the book is 4.6 this section.
Create CubeMap
First, create a cube map and right-click a new Cubemap in assets.
Build the scene and add a sphere as a camera container. Because you want to use the Camera's API to generate CubeMap.
Transfer from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn
Here are the scenes I built.
Write a Unity editor plugin below to generate CubeMap.
Using unityengine;using system.collections;using Unityeditor;public class Generatestaticcubemap:scriptablewizard {PU Blic Transform renderposition; Public Cubemap Cubemap; void Onizardupdate () {helpstring = ' Select Transform to render ' + ' from and cubemap to render into '; if (renderposition = null && cubemap! = null) {IsValid = true; } else {isValid = false; }} void Onwizardcreate () {//Add a camera to create the cubemap. Gameobject go = new Gameobject ("Cubemapcamera", typeof (Camera)); Go.transform.position = renderposition.position; Go.transform.rotation = quaternion.identity; Go.camera.RenderToCubemap (CUBEMAP); Destroyimmediate (GO); } [MenuItem ("Cookbookshaders/render Cubemap")] static void Rendercubemap () {Scriptablewizard.displaywizar D ("Render Cube", typeof (Generatestaticcubemap), "render"); }}
Because it is an editor tool, follow the rules of unity, put this code file in the folder named Editor, you can create a new one.
The core of this code file is
Go.camera.RenderToCubemap (CUBEMAP);
With the camera API, a Cubemap is generated.
Then a menu is added as a portal.
[MenuItem ("Cookbookshaders/render Cubemap")]
When Unity is compiled, the menu bar will appear with the menu we added ourselves.
Drag the Cubemap you just created into the sphere and drag it in.
Click Render to generate the Cubemap.
Using CubeMap
The cube map was created, and then it was ready to use.
Still creates a material, a Shader.
Transfer from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn
Shader Code
Shader "Cookbookshaders/cubemap" {Properties {_maintint ("Diffuse Tint", Color) = (1,1,1,1) _maintex ("Base (RGB)", 2D) = " White "{}_cubemap (" Cubemap ", CUBE) =" "" {}_reflectionamount ("Reflection Amount", Range (0.01,1)) =0.5}subshader {Tags {" Rendertype "=" Opaque "}lod 200cgprogram#pragma surface Surf Lambertfloat4 _maintint;sampler2d _maintex;samplercube _ Cubemap;float _reflectionamount;struct Input {float2 uv_maintex;float3 worldrefl;}; void Surf (Input in, InOut surfaceoutput o) {half4 c = tex2d (_maintex, In.uv_maintex) *_maintint;o. Emission=texcube (_CUBEMAP,IN.WORLDREFL). RGB *_reflectionamount;o. Albedo = C.rgb;o. Alpha = C.A;} ENDCG} FallBack "Diffuse"}
Use the cube map for the first time.
In the properties block, the usage type of the definition attribute is CUBE. The normal texture is 2D.
In Subshader, note the samplercube when defining variables. The normal texture is sampler2d.
In the Input struct, Unity's built-in variable WORLDREFL is used, which provides the world's reflection variables used in shaders.
Then in the surf function, use Texcube to extract the Texel from the CubeMap.
Texcube (_CUBEMAP,IN.WORLDREFL). RGB
Post-run effects
Sample Project Download:
Http://pan.baidu.com/s/1qYcNKxI
Unity Shaders and Effects Cookbook (4-1) (4-2) creation and use of static cube maps