Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Realtime Reflection, works with Unity Indie

Discussion in 'General Discussion' started by vertgreenheart, Oct 7, 2013.

  1. vertgreenheart

    vertgreenheart

    Joined:
    May 6, 2011
    Posts:
    61
    I recently just made a research about Realtime Reflection on Unity Indie. 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.jpg
    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.

    $comp-1-2.gif

    This is the code

    Code (csharp):
    1. #pragma strict
    2.  
    3. var cubemap : Cubemap;
    4. var currentMaterial : Material;
    5. var updateRate = 1.0;
    6. private var renderFromPosition : Transform;
    7. private var minz = -1.0;
    8.  
    9. function Start () {
    10.     renderFromPosition = transform;
    11. }
    12.  
    13. function Update () {
    14.     if(Time.time - updateRate > minz){
    15.         minz = Time.time - Time.deltaTime;
    16.         RenderMe();
    17.         currentMaterial.SetTexture("_Cube",cubemap);
    18.         renderer.material = currentMaterial;
    19.     }
    20. }
    21.  
    22. function RenderMe(){
    23.     var go = new GameObject( "CubemapCamera"+Random.seed, Camera );
    24.        
    25.     go.camera.backgroundColor = Color.black;
    26.     go.camera.cullingMask = ~(1<<8);
    27.     go.transform.position = renderFromPosition.position;
    28.     if(renderFromPosition.renderer )go.transform.position = renderFromPosition.renderer.bounds.center;
    29.     go.transform.rotation = Quaternion.identity;
    30.    
    31.     go.camera.RenderToCubemap( cubemap );
    32.  
    33.     DestroyImmediate( go );
    34. }
     
    Friction, RNT and rakkarage like this.
  2. MarigoldFleur

    MarigoldFleur

    Joined:
    May 12, 2012
    Posts:
    1,353
    It's a bit too slow for anything particularly robust, but I could see this being pretty useful for things like quick and dirty reflections for something like a Wave Race clone.
     
  3. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    Or reflections off of things in an FPS or something where they are used subtlety. That could be helpful. But again, too many of the would be too slow.
     
  4. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    I was wondering about this, so camera.RenderToCubemap is working in unity free???
     
  5. kaiyum

    kaiyum

    Joined:
    Nov 25, 2012
    Posts:
    686
    I thought this is pro only feature!
     
  6. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    Well i'm confused....:confused:
     
  7. kaiyum

    kaiyum

    Joined:
    Nov 25, 2012
    Posts:
    686
    Did not work(and why would it? ;))


    The documentation says about "Camera.RenderToCubemap":

    I don't think there is any feasible solution of realtime reflection(such as mirror) on unity free.
     
    rakkarage likes this.
  8. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    Umm you material is on Diffuse...can you try change it into reflection??
     
  9. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I think there was an issue where it worked in Indie in the IDE, but not in the final build.

    Might wanna check that before jumping to false conclusions.
     
  10. smd863

    smd863

    Joined:
    Jan 26, 2014
    Posts:
    292
    You can render a mirror in Free with a depth mask shader and a second camera. You only need Pro to do refractions and reflections on curved or bumpy surfaces.
     
  11. lazygunn

    lazygunn

    Joined:
    Jul 24, 2011
    Posts:
    2,749
    I was informed there are a few things you can do in the editor that won't show up in a build, mainly rendertexture based. Definitely do a build before jumping to conclusions

    Although lack of rendertextures in unity free is a total pain in the behind
     
  12. Deleter

    Deleter

    Joined:
    Dec 21, 2012
    Posts:
    23
    Works brilliantly (25 FPS with GeForce 840m, 8G of RAM, 1 reflecting sphere). Congrats LAiKA787SV!!!



    Here the refresh of textures is at 0.03 second, works great and fluently with one reflecting sphere, but adding a reflecting lamborghini makes it fall at about 13 FPS with my GeForce 840m.

    I added vars caching for better performances, and deleted some lines in the Update function so that it works faster. The only problem is that the rendered reflection is proportionnal to the size of the object so that when you have a big object source of reflection, the reflected objects around could look bigger to themselves lol, with an excessive scale of the reflection container object, and this is not realistic, so the best is to use this solution for reasonnable-size objects or on objects that do not make it obvious that the reflection is scaled on the size of the reflecting object.


    I also disabled the camera used for rendering the texture so to avoid multi camera rendering problems, as this keeps the rendering. And this permits to avoid rendering of the viewport at runtime

    Added a static camera counter variable to avoid render camera name collisions.


    Added synamic cubemaps created dynamically for more easy-to use script.

    At last, the user just needs to parametrate the frame-rate of the texture cubemap, no need to create anymore a cubemap or other things, the context of the gameObject is now taken in account and cached properly in fields of the script in the Start() function. Don't hesitate to modify the 256 value to 128 in the cubemap creation line, as this improves quality but may impact your performaces, depending on your computer config.

    Here is the resulting script :
    Code (JavaScript):
    1.   #pragma strict
    2.    
    3.     public static var i : int = 0;
    4.     public var currentMaterial : Material;
    5.     public var updateRate = 1.0;
    6.     public var renderFromPosition : Transform;
    7.     public var minz = -1.0;
    8.     public var go : GameObject;
    9.     public var cubemap : Cubemap;
    10.    
    11.     function Start () {
    12.         renderFromPosition = transform;
    13.         go = new GameObject("Camera"+i,Camera);
    14.         go.camera.backgroundColor = Color.black;
    15.         go.camera.cullingMask = ~(1<<8);
    16.         go.camera.active = false;
    17.         go.transform.rotation = Quaternion.identity;
    18.         cubemap = new Cubemap (256, TextureFormat.ARGB32, false);      
    19.         i++;
    20.         currentMaterial = renderer.material;
    21.     }
    22.    
    23.     function Update () {
    24.         if(Time.time - updateRate > minz){
    25.             minz = Time.time - Time.deltaTime;
    26.             RenderMe();
    27.            
    28.         }
    29.     }
    30.    
    31.     function RenderMe(){
    32.         go.transform.position = renderFromPosition.position;
    33.         if(renderFromPosition.renderer )
    34.             go.transform.position = renderFromPosition.renderer.bounds.center;
    35.  
    36.         go.camera.RenderToCubemap( cubemap );
    37.        
    38.         currentMaterial.SetTexture("_Cube",cubemap);
    39.             renderer.material = currentMaterial;
    40.    
    41.         //DestroyImmediate( go );
    42.     }
    43.  
     
    Last edited: Aug 22, 2014
    rakkarage likes this.
  13. dilepoutee

    dilepoutee

    Joined:
    Aug 22, 2014
    Posts:
    10
    Works fine..thankx
     
  14. Deleter

    Deleter

    Joined:
    Dec 21, 2012
    Posts:
    23
    :)
     
  15. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Just stating the obvious here: The documentation clearly states that RenderToCubemap is a Pro feature.
    Thus, if it works in Unity Indie, this is a bug, and you should expect that sooner or later it will get fixed. Also, being a bug, it will not be likely to behave predictably; it's liable to break on builds, in the webplayer, on different platforms, etc. In other words, don't rely on this for any of your projects.
     
  16. Deleter

    Deleter

    Joined:
    Dec 21, 2012
    Posts:
    23
    It deployes perfectly on build, webplayer + standalone after my optimizations. Try it by yourself :p It seems that you work for Unity and that you don't want the people to know that it works perfeclty. Well, anyone can try and see that it works without a problem, actually.
     
    Last edited: Aug 23, 2014
  17. derkoi

    derkoi

    Joined:
    Jul 3, 2012
    Posts:
    2,255
    What he's saying is, it would be stupid to use this in a project and expect it to work flawlessly forever as it wont.
     
  18. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,614
    Yeah, he ain't saying it doesn't work. He's saying that the documentation says it shouldn't, and therefore you shouldn't rely on it.
     
  19. Deleter

    Deleter

    Joined:
    Dec 21, 2012
    Posts:
    23
    It forever will... x)) as long as Unity 5 arrive and then reflections free for everyone x)

    Yep angrypenguin, I know it shouldn't, but what doesn't work is actually the restrictions x) that's good to enjoy a little reflections for free and I wanted to tell the World that the author of this post was right because the conclusion was "I'm not sure it will work on build deployment".

    So I just wanted to confirm that it do work on deployment actually, both webplayer and standalone, so bravo LAiKA787SV (author of this post) coz of your great script, now optimized :p

    Here a little example of "infinite" reflections with this script x) (sphere reflecting sphere, in real time) ain't it so cool? x))) all for 0 euro xD

    awesome....png yoooo2.png
     
    Last edited: Aug 24, 2014
    thxfoo likes this.
  20. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    I'm pretty sure you're misinterpreting something someone said. Where have you heard that realtime reflections are coming to Free?
     
  21. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,954
    So long as you stick to the version of Unity that has it bugged.
     
  22. Deleter

    Deleter

    Joined:
    Dec 21, 2012
    Posts:
    23
    Exactly :) to any version "bugged" but this seems more a lack of restriction specification implementation than a bug (as it works well) ( bug is more something that does not work that should, than something that work and shouldn't, which is a...chance xD)
     
    Last edited: Aug 28, 2014
  23. zDemonhunter99

    zDemonhunter99

    Joined:
    Apr 23, 2014
    Posts:
    478
    Won't using Specular Ambient Light do the trick?
     
  24. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,954
    According to Wikipedia, a bug "is an error, flaw, failure, or fault in a computer program or system that causes it to produce an incorrect or unexpected result, or to behave in unintended ways."

    It is not intended to be a feature of Unity Free.

    http://en.wikipedia.org/wiki/Software_bug
     
  25. Deleter

    Deleter

    Joined:
    Dec 21, 2012
    Posts:
    23
    x) ...Well, for me I call it an opened door Unity team did not close, which is not a bug but a feature, that Unity commercials wanted to restrict to only to paid versions, but as the feature is working, it is not a bug but a feature, accessible by now not only to the pro but also free versions : feature is "on" whatever the Unity trolls say => it's working on build release, webplayer and standalone, for now at least :), and the original author of the post and script was right, that's all I wanted to prove. :) A little picture for the trolls :



    x)
     
    Last edited: Aug 29, 2014
  26. yusefkerr

    yusefkerr

    Joined:
    Apr 5, 2011
    Posts:
    179
    I tried this in Unity 4.5.5 and 4.3.0 and I'm getting the same results in both versions (its not working).

    In the image below, there are two screen grabs from the scene (1 and 2). No.2 is what I'm trying to get, but most of the time I'm getting a weird cubemap render (shown in No.1) that doesn't correspond to the environment.

    The only way to get close to No.2 is by adding two versions of the Cubomapo script to the game object, but then it flickers between the two versions. When it flickers to the correct reflection (No.2), it works dynamically like shown in the video, but then it flickers back again within (what looks like) a single frame.

    Using just one version of the script simply gives a constant No.1.

    I've tried both versions of the script, and a ton of other slight modifications such as using LateUpdate, but I'm getting no closer and wondered if anyone could suggest things to try.
     

    Attached Files:

  27. Deleter

    Deleter

    Joined:
    Dec 21, 2012
    Posts:
    23
    No it seems that it's still working actually!...let me double check :-D

    Yep I confirm, it is still working :-D

    What I can say is that the script still works on 4.5.5 version of Unity free (my project still renders reflections dynamically)
     
    Last edited: Nov 9, 2014
  28. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    You really don't need to post 3 messages back to back, just so you know you can hit reply to as many people as you want in one message.
     
  29. Deleter

    Deleter

    Joined:
    Dec 21, 2012
    Posts:
    23
    Yup you're right but is it really annoying? I'm gonna edit the messages maybe

    Ok I just edited the last 3 messages in 1 :q
     
  30. yusefkerr

    yusefkerr

    Joined:
    Apr 5, 2011
    Posts:
    179
    I'm still trying different things and still getting the same result, so I'm probably missing something basic. I can see that the correct reflections are rendering at some point, but within a frame they are replaced with an erroneous cubemap texture that is generated with some major artifacts (don't know what else to call them but see the screenshots in the last post)! A few things I notice while trying to use your script Deleter:

    For the sphere object I'm using, the Cubemap slot in the cubomapo.js section in the Inspector shows a little cube icon but no cubemap name when the game is in play mode.
    The Camera0 gameobject that gets created never shows anything assigned to the "Target Texture" variable, even during play mode.

    I don't know if these are relevant. The problem seems to be that the "erroneous cubemap" I mention is jumping in from somewhere and rendering over the top of what should be there.

    I notice above that the sphere material has to be reflective, but this wasn't one of the Tutorial steps in the original post. I'm wondering if there's something else like that I'm missing. Is there a reason there are two material slots in the top example?

    Finally - Would it be possible to post an example package or scene?

    Edit: I've included an image that might help explain what I'm seeing.
     

    Attached Files:

    Last edited: Nov 11, 2014
  31. vertgreenheart

    vertgreenheart

    Joined:
    May 6, 2011
    Posts:
    61
    Awesome work dude!!! :D

    Anyway, if someone still don't know how it works or how make it work, here, an example project.

    Sorry didn't respond to this thread so long (I didn't know this thread had so much replies), I really busy researching new technology in Unity. I rarely open Unity forum, so if you want to check my researches, please visit my blog.

    Dude, I already made so many Unity Pro on Unity Free tricks. Somewhat like playing Movie Texture on Unity Indie, Projecting soft shadow on Unity Indie, and many more. Check out my blog.

    There's so many people linkbacking my script and they also provides their working video demo of my realtime reflection indie script. And of course, they had to remix it a little like converting to C# or optimising performance.

    Anyway, it's not called hacking, I just try to find alternative way that didn't use Unity "Pro" features.
     
    Last edited: Dec 28, 2014
  32. Friction

    Friction

    Joined:
    Jan 6, 2015
    Posts:
    18
    Firstly thank you so much LAiKA787SV. It's awesome. But not working on android platform.
    On PC/Mac & Linux Standalone platform:
    pc.jpg

    On Android platform:
    android.jpg

    How do I fix it?
     
  33. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,608
    Real time reflections on mobile is somewhat of a no no. Very slow.
     
  34. Alyssa

    Alyssa

    Joined:
    Feb 13, 2015
    Posts:
    2
    What EXACTLY is Unity Indie??? I can't find any sort of answer anywhere, it's like some sort of secret society...0.o
     
  35. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,614
    Way back in the dark annals of history, there was a time when Unity's free license didn't exist. In those times there was a license very similar license, but it cost a couple of hundred dollars per seat.

    So... basically it's referring to the Unity free license by a super old name.