imagination XD

realizing dream

[TUTORIAL] REALTIME Reflection on Unity Indie

9 Comments

I really amazed when I found this trick. I know that Unity INDIE could make a realtime reflection via CAMERA.RENDERCUBEMAP. However, there still has some limitations like not really slow, and only 64 pixels. If you set the update rate to 0.03, it’ll slow as hell (about 14 fps), and if you set the update rate to 1, it’s not slow (about 60 fps).

And this is the tutorial :

  1. Go make a new javascript. The code are in the below of this post.
  2. Go make a new Cubemap by clicking CREATE > CUBEMAP on project tab.
  3. I named it “CUBOMAPO_REF”;
  4. Assign this script on an object that you want to enable the reflection.
  5. Assign CUBOMAPO_REF or your cubemap to CUBEMAP properties in your Reflective Object.
  6. Assign YOUR CURRENT MATERIAL to CURRENT MATERIAL properties in your reflective object.
  7. meshos
  8. Adjust the update rate.
  9. DONE! Now look what have you done! This is my preview…. With 0.2 Update Rate about 40 fps.

Realtime Reflection

This is the code

#pragma strict

var cubemap : Cubemap;
var currentMaterial : Material;
var updateRate = 1.0;
private var renderFromPosition : Transform;
private var minz = -1.0;

function Start () {
    renderFromPosition = transform;
}

function Update () {
    if(Time.time - updateRate > minz){
        minz = Time.time - Time.deltaTime;
        RenderMe();
        currentMaterial.SetTexture("_Cube",cubemap);
        renderer.material = currentMaterial;
    }
}

function RenderMe(){
    var go = new GameObject( "CubemapCamera"+Random.seed, Camera );
        
    go.camera.backgroundColor = Color.black;
    go.camera.cullingMask = ~(1<<8);
    go.transform.position = renderFromPosition.position;
    if(renderFromPosition.renderer )go.transform.position = renderFromPosition.renderer.bounds.center;
    go.transform.rotation = Quaternion.identity;
    
    go.camera.RenderToCubemap( cubemap );

    DestroyImmediate( go );
}
Advertisements

Author: IMGVERTEX

Started making games since 13 year old. 3D CGI Weeaboo | VFX Otaku | Anime Researcher | Indie Game Dev | VR Chuunibyou

9 thoughts on “[TUTORIAL] REALTIME Reflection on Unity Indie

  1. this is so great, I find a long way to this tutorial, very thank you.

  2. Niesamowita ocena. Właśnie takiego podejścia szukałem.
    Prześlę tę stronę kolegom. Przyrzekam iż będę tu zaliczał koleje odwiedziny.

    Szczęścia w dalszym prowadzeniu.

  3. It freezes for half a second every time it updates.

    • Yeah, as I said, It’s very slow if you decrease the update rate. If you use mobile processor, this is not recommended.

      If you use mobile processor, set the Cubemap resolution to 32, and update rate 0.5

      In my computer (Core i3 @ 3.10GHz, 3GB DDR3, DA GT220), I set the Cubemap resolution to 256, and update rate 0.05 and I got 15 FPS.

  4. Pingback: [Shader] Reflective Transparent / Water-like Unity Indie | imagination XD

  5. Hi, it does’nt work. what is the material “Fencing_cha…” please ?
    Thanks

  6. Struggling to understand this as a total scripting n00b, where do I attach the camera so it renders to the cubemap?

FEED ME

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s