Unity Shaders and Effects Cookbook (4-6) Stunning real-time reflection dynamic cubic chart System

Source: Internet
Author: User

Yesterday when shopping to see the peace bird inside put a metal Mickey Mouse, so the occupational disease committed, has been thinking about the color of metal is what, this reflection how to write, think not come out ...

Today just see the dynamic Reflection Cubic chart System This section, after reading feel very awkward, because the book is introduced in advance casing generate cubemap way instead of real-time. Then to the official documents to find real-time reflection of the code, did a more fancy scene, after the run to eat a surprise, real-time reflection is so shocking.


Chapter fourth, section II, describes the creation of CubeMap and then learns how to use it.

Unity Shaders and Effects Cookbook (4-1) (4-2) creation and use of static cube maps

I also said, is static cubemap, that is, this cubemap is collected from a place, will not move with the location of the dynamic acquisition of the surrounding environment to produce reflection.

This section learns real-time dynamic cubic graphs.

Book 4.6 This section is about the simple dynamic cubic chart system, which means that several points in a room are collected, and then several cubemap are generated.

Then judge the position of the object, close to the acquisition point when the selection of the current collection point of the cubemap.

I drew a picture.


1, 2, 3, 4 is the acquisition point, in these 4 points were created Cubemap1, CUBEMAP2, CUBEMAP3, CUBEMAP4.

The middle is a man.

When the person walks to the location near the acquisition point 3, the CUBEMAP3 is used.

Transfer from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn

This is dynamic, but not real-time.

I'm still looking for real-time reflection on the web, as in the unity documentation.

Http://docs.unity3d.com/ScriptReference/Camera.RenderToCubemap.html

The document is JS, but it doesn't affect reading, because the code is simple.

I translated it into C #.


Take a look first.




Compared to the static cubic graph of 4.1 learning, this section learns real-time cubic graphs, just modifying the cubic diagram to generate in real time.

Remember that cube graph generation is the API that uses the camera

Camera. Rendertocubemap (CUBEMAP);

After each character move, call this function to update the CUBEMAP, it achieves the real-time purpose.


Previously we created Cubemap in Assets because the creation and use were not done together.

This time, because it is real time, you do not have to create cubemap in Assets, and then save it with a variable and update it in Lateupdate.



Set up a good scene, I am here for a better look on the smear made a terrain. Add a sphere to do the reflection, the material with the first section of the can, copy one. Then the Cubemap in the material is removed, because now we are generating it in real time.

Hang the following script on the Sphere

Using unityengine;using System.collections;public class Realtimereflection:monobehaviour {Camera reflectioncamera; Rendertexture cubemap;//Use this for initializationvoid Start () {Gameobject go = new Gameobject ("Reflectio        N Camera ", typeof (camera));        Reflectioncamera = Go.camera;        Go.hideflags = Hideflags.hideanddontsave;        Go.transform.position = transform.position;        Go.transform.rotation = quaternion.identity;        Reflectioncamera.farclipplane = 100;        reflectioncamera.enabled = false;        CubeMap = new Rendertexture (128, 128, 16);        Cubemap.iscubemap = true;        Cubemap.hideflags = Hideflags.hideanddontsave;        Renderer.sharedMaterial.SetTexture ("_cubemap", Cubemap);        ReflectionCamera.transform.position = transform.position;    Reflectioncamera.rendertocubemap (CubeMap, 63);} void Rendercubemap () {}//Update is called once per framevoid update () {} void Lateupdate () {REFL EctioncaMera.transform.position = transform.position;    Reflectioncamera.rendertocubemap (CubeMap, 63); }}


The main thing is the Rendertocubemap in Lateupdate.


Facemask refers to which faces need to be rendered. 63 is 111111, you need to render and write 1.

Official documents

Http://docs.unity3d.com/ScriptReference/Camera.RenderToCubemap.html

transfer from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn

The order of the 6 1 is: Right and left up and down

Official documents

Http://docs.unity3d.com/ScriptReference/CubemapFace.html

Basically the script above, the script to control the move is as follows

Using unityengine;using System.collections;public class Movecontroller:monobehaviour {[Serializefield] Transform cameratrans;//Use this for initializationvoid Start () {} void Ongui () {if (GUI). RepeatButton (New Rect (), "Forward") {transform.localposition + = new Vector3 (0, 0, 10 *        Time.deltatime); } if (GUI. RepeatButton (New Rect (+, (), Max, Max), "back") {transform.localposition + = new Vector3 (0, 0, -10 * T        Ime.deltatime); } if (GUI. RepeatButton (New Rect, Max, Max, Max), "left") {transform.localposition + = new Vector3 ( -10 * time.de                    Ltatime, 0, 0); } if (GUI. RepeatButton (New Rect (+,-), "right")) {transform.localposition + = new Vector3 (Ten * time.de        Ltatime, 0, 0); } if (GUI. RepeatButton (New Rect (650, Max, Max), "Up")) {transform.localposition + = new Vector3 (0, Ten * Time.delTatime, 0); } if (GUI. RepeatButton (New Rect (650, (), Max, Max), "Down")) {transform.localposition + = new Vector3 (0        , -10 * time.deltatime, 0); }}//update is called once per framevoid update () {if (Input.getkey (KEYCODE.W)) {trans        Form.localposition + = new Vector3 (0, 0, ten * time.deltatime); } if (Input.getkey (Keycode.a)) {transform.localposition + = new Vector3 ( -10 * time.deltatime,0, 0        ); } if (Input.getkey (KEYCODE.S)) {transform.localposition + = new Vector3 (0, 0, -10 * time.deltatim        e); } if (Input.getkey (KEYCODE.D)) {transform.localposition + = new Vector3 (Ten * time.deltatime, 0, 0        ); } if (Input.getkey (KEYCODE.Q)) {transform.localposition + = new Vector3 (0, -10 * time.deltatime,        0); } if (Input.getkey (KEYCODE.E)) {transform.localposition + = nEW Vector3 (0, ten * time.deltatime, 0);    }} void Lateupdate () {cameratrans.localposition = transform.localposition + new Vector3 (0f, 1.0f, -2f); }}

I also put on the phone the charm Blue metal test the next, very card.

On the phone



But the effect is really shocking.


Project Package Download:

Http://pan.baidu.com/s/1dFqb7NV


APK Download:

Http://pan.baidu.com/s/1gf2iJcv


Unity Shaders and Effects Cookbook (4-6) Stunning real-time reflection dynamic cubic chart System

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.