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

MirrorReflection.shader in V2.0

Discussion in 'Shaders' started by diese440, Oct 22, 2007.

  1. diese440

    diese440

    Joined:
    May 25, 2007
    Posts:
    105
    i have a strange problem with the GREAT "MirrorReflection.shader" written by Aras Pranckevicius (wiki link : http://www.unifycommunity.com/wiki/index.php?title=MirrorReflection)

    This is an very helpfull shader BUT it don't work on Windows since Unity V2.0 :( (in fact it seems don't work with directX, b'cause it was OK with Unity 1.6.2 -OpenGl)

    Any idea Aras ? :roll:
    Thanks to the UnityTeam for 2.0 :D
     
  2. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    The public gets what the public wants!

    Mirror Reflection for Unity 2.0

    It has several bonuses over 1.x version: works in scene view and with multiple cameras, does not require vertex shaders, the mirror can have a texture, and the mirror can be arbitrarily scaled.
     
  3. diese440

    diese440

    Joined:
    May 25, 2007
    Posts:
    105
    WhooAaa ! [public is happy]...

    You are the best 8)
    Many thanks Aras :!:
     
    Last edited: Dec 24, 2010
  4. GusM

    GusM

    Joined:
    Aug 27, 2005
    Posts:
    585
    Many thanks, your work is awesome as usual ;)
     
  5. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    I just setup the Mirror reflection shader and its super cool. I have a couple of problems though:

    1. Doesn't work with Orthographic cameras
    2. I want to use a black texture for my reflective surface (a la Leopards Cover flow style) but the reflection is almost invisible
    3. The reflective layer seems to lose the nice effect of the pixel lights in the scene, which makes it look a little dull - is there a way to just "add" the reflections to an existing material?

    Thanks
    Shaun
     
  6. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Maybe, I didn't try. Will take a look.

    The shader multiplies texture with the reflection (combine texture * previous). You can change that to add (combine texture + previous), add signed (combine previous +- texture) and so on, whatever you like.

    I did not try, but I think what could work is:
    1. setup the mirror renderer to use two materials (change array size in mesh renderer),
    2. set first material to use regular material you want,
    3. set second material to use mirror shader,
    4. change shader to do additive blending, something like (untested):
    Code (csharp):
    1.  
    2. Shader "FX/Mirror Reflection Additive" {
    3. Properties {
    4.     _Color("Color", Color) = (1,1,1,1)
    5.     _ReflectionTex ("Reflection", 2D) = "white" { TexGen ObjectLinear }
    6. }
    7. Subshader {
    8.     Blend One One
    9.     ZWrite Off
    10.     Pass {
    11.         SetTexture[_ReflectionTex] {
    12.             matrix [_ProjMatrix]
    13.             constantColor [_Color]
    14.             combine texture * constant
    15.         }
    16.     }
    17. }
    18. }
    Edit: fixed syntax error in the shader (removed {})
    Edit2: changed 'primary' to 'constant', makes the shader actually work
     
  7. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    Awesome, thanks Aras. Will see what I can wrangle together with my limited shader skills ;)

    What could cause the shader to fail under Ortho camera conditions?
     
  8. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    Aras - I've tried that script and setup things as best I can, but I'm not seeing the reflection in the final result.

    I've assigned 2 materials to the Mesh, the first being the base, the 2nd being the reflection with the new Additive Mirror shader.

    I checked the Rendertexture at runtime, and it seems to contain the correct reflect image, but its just not being combined with the final somehow.

    Also the example shader generated an exception at {} in this line (so i removed it)
    Code (csharp):
    1.  _Color("Color", Color) = (1,1,1,1) {}
    Thanks for your help
    Shaun

    EDIT:
    ah - and I tried this with both Ortho and Persp modes... same result.
     
  9. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Oh yeah, turns out posting untested shaders directly to the forum makes them not work in some cases... :roll:

    I updated the shader above. Also updated the script on MirrorReflection2 to support multiple materials on the object.

    This makes it work for me.
     
  10. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    Thx again Aras :) - I seem to have it pretty much working(grey is Ok, but pure black gives a transparent output)

    Ortho mode is still a no-no... is there any way around this?
     
  11. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Didn't look into that yet... will do once I'm done with whatever I'm doing right now :)
     
  12. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    Rock n roll!
    I hereby request a pay-rise for Aras - maybe better in the wish list ;)
     
  13. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Aras is overworked. Doesn't even know what he's working on right now!

    All of you guys need a vacation...but make it after you do the small list of things that is the wish-list ok? ;-)

    -Jeremy
     
  14. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    If you've got nothing under your mirror plane, then just commenting out call to CalculateObliqueMatrix in the mirror reflection script should work.

    Meanwhile I'm trying to figure out how to do this oblique clipping matrix for orthographic camera, and this projective math is a bit over my head. If anyone knows how to do it, let me know...
     
  15. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    Nice - I'll give that a shot as its good enough in the meantime.

    As for the shader math - if its over your head, then it must be pretty bloody difficult. Hope someone can help - Stephen Hawkings maybe ;)
     
  16. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    I figured it out. Wasn't that hard.

    Updated the MirrorReflection2 on the wiki, now supports orthographic cameras. Only the CalculateObliqueMatrix function in MirrorReflection script has changed.
     
  17. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    716
    this works great. is there anyway of making the reflection say 10% strength... trying to achieve a highly polished concrete floor that reflects the columns and ceiling around it.... any suggestions?

    cheers.
     
  18. Charles Hinshaw

    Charles Hinshaw

    Joined:
    Feb 6, 2008
    Posts:
    1,070
    @thylaxene to get a reflection on the surface of a material rather than a mirror, take a look at Aras's suggestion earlier in this thread:

     
  19. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Since I was doing something similar just now, the alpha channel of the main texture controls the reflection strength (I just changed the FX/Mirror Reflection shader slightly):

    Code (csharp):
    1. Shader "FX/Mirror Blend" {
    2. Properties {
    3.     _MainTex ("Base (RGB) RefStrength (A)", 2D) = "white" {}
    4.     _ReflectionTex ("Reflection", 2D) = "white" { TexGen ObjectLinear }
    5. }
    6.  
    7. // two texture cards: full thing
    8. Subshader {
    9.     Pass {
    10.         SetTexture [_ReflectionTex] { matrix [_ProjMatrix] combine texture }
    11.         SetTexture [_MainTex] { combine texture lerp (texture) previous }
    12.     }
    13. }
    14.  
    15. // fallback: just main texture
    16. Subshader {
    17.     Pass {
    18.         SetTexture [_MainTex] { combine texture }
    19.     }
    20. }
    21.  
    22. }
    --Eric
     
  20. bluindigo

    bluindigo

    Joined:
    Jun 5, 2008
    Posts:
    14
    Is it my imagination, or can unity not read a solid color as an alpha channel?
    I created a flat color with a solid grey alpha channel and it kept coming up white ie no alpha. I changed the levels of gray from white through black still nothing. I then scribbled black and white and there is was. Even a small dot of different shade enabled the alpha. But flat no way. Is it me or is that weird?

    Try it.
     
  21. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    Check that the import settings for the texture are set to be RGBA. For some reason, same-color alpha does not put us into importing the texture as RGBA for default.
     
  22. Yann

    Yann

    Joined:
    Oct 20, 2007
    Posts:
    432
    Is it possible to apply a fog effect to the reflected image ? I'm currently trying to obtain this well-known effect on a ground plane :
     

    Attached Files:

  23. anti-alias

    anti-alias

    Joined:
    Nov 8, 2008
    Posts:
    62
    Dear Aras,

    I would like to ask if is it possible to change the axis of reféection? I import FBX from 3ds Max and the object is in its plce, but the reflection is weird.

    Thanks
     
  24. anti-alias

    anti-alias

    Joined:
    Nov 8, 2008
    Posts:
    62
    After testing is for a while the problem solved....

    I have 2 other questions: is it possible to blur the "mirror texture" inside the shader and is it possible to adjust the opacity and colors of the mirror like for example in the transparent vertexlit shader?

    THX
     
  25. lakru

    lakru

    Joined:
    Feb 19, 2008
    Posts:
    116
    So, anyone know if it's possible to make that effect that Yann is asking for?
     
  26. francksitbon

    francksitbon

    Joined:
    Jan 22, 2010
    Posts:
    268
    Hi, is it possible to enhance the mirror reflection?

    - add amount of reflection slider (0 -100%)
    - add reflection blur slider ((0 -100%)
    - add range slider (object Z reflection disappearing distance)
    - add normal map
    - add light illumination and shadows reception
     
  27. junkotron

    junkotron

    Joined:
    Feb 22, 2009
    Posts:
    158
    i've got this mirror reflection working great but how would i make it slightly translucent. I'm basically looking to make a somewhat transparent mirror. Ideas?
     
  28. jcarpay

    jcarpay

    Joined:
    Aug 15, 2008
    Posts:
    561
    Any ideas how to make the mirror shader blend properly with a lightmap material?
     
  29. Clamps

    Clamps

    Joined:
    Nov 7, 2009
    Posts:
    220
    Hey junkotron,

    Could you please post that shader up here so others might use it? If it's useful as a reflective floor shader I'd be thrilled.

    thanks
     
  30. vollnull

    vollnull

    Joined:
    Aug 26, 2010
    Posts:
    70
    Is it possible, that the shader or the corresponding script do not work in Unity 3? I upgraded not long ago and my ground is flickering.
     
  31. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
  32. vollnull

    vollnull

    Joined:
    Aug 26, 2010
    Posts:
    70
    I found out, that it is working if I deactivate the shadows of my directional light in the scene. If I open a new scene and arrange a ground, some boxes and a directional light with shadows it is working too. So actualy there might be something wrong with my scene, but I did not find out what exactly.
     
  33. vollnull

    vollnull

    Joined:
    Aug 26, 2010
    Posts:
    70
    After some testing I now found out, that the flickering only appears for objects, that are grouped under an empty game object. If I "unparent" them, then it is visually correct. Any idea, why this might be?
     
  34. Q_B

    Q_B

    Joined:
    Nov 29, 2010
    Posts:
    14
    Hi there great people from the Unity comunity!

    I've been testing the pro version, and I'm developing some small demos. There's a high probability that the company I'm working for will aquire a license, but I just have 4 trial days left ...

    Alas, been following this thread, in hopes of finding a working mirror reflection shader, with reflection value, and most important, wich still receives shadows!

    Seems no one has come up with one, so I tried using my limited "code hacking" abilities and came up with a shader that does this. Problem is, and that's due to my ignorance, I can't "assign" the projection matrix to the reflection texture!

    If anybody can give a helping hand, would be very thankfull, as I'm sure many more users!

    Thanx!

    Code (csharp):
    1. Shader "Reflective/Mirror Diffuse" {
    2. Properties {
    3.     _Color ("Main Color", Color) = (1,1,1,1)
    4.     _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
    5.     _MainTex ("Base (RGB) RefStrength (A)", 2D) = "white" {}
    6.     _ReflectionTex ("Reflection", 2D) = "white" { TexGen ObjectLinear }
    7. }
    8. SubShader {
    9.     LOD 200
    10.     Tags { "RenderType"="Opaque" }
    11.    
    12. CGPROGRAM
    13. #pragma surface surf Lambert
    14.  
    15. sampler2D _MainTex;
    16. sampler2D _ReflectionTex;
    17. //samplerCUBE _Cube;
    18.  
    19. float4 _Color;
    20. float4 _ReflectColor;
    21.  
    22. struct Input {
    23.     float2 uv_MainTex;
    24.     float2 uv_ReflectionTex;
    25.     //float2 p_ProjectionMatrix;
    26.     //float3 worldRefl;
    27. };
    28.  
    29. void surf (Input IN, inout SurfaceOutput o) {
    30.     half4 tex = tex2D(_MainTex, IN.uv_MainTex);
    31.     half4 c = tex * _Color;
    32.     o.Albedo = c.rgb;
    33.    
    34.     //half4 reflcol = texCUBE (_Cube, IN.worldRefl);
    35.     half4 reflcol = tex2D (_ReflectionTex, IN.uv_ReflectionTex);
    36.     //half4 reflcol = tex2D (_ReflectionTex, IN.worldRefl);
    37.     //half4 reflcol = tex2D (_ReflectionTex, IN.p_ProjectionMatrix);
    38.     reflcol *= tex.a;
    39.     o.Emission = reflcol.rgb * _ReflectColor.rgb;
    40.     o.Alpha = reflcol.a * _ReflectColor.a;
    41. }
    42. ENDCG
    43. }
    44.    
    45. FallBack "Reflective/VertexLit"
    46. }
    47.  
    (Edit) This shader is based on the source of the reflect-diffuse shader, obviously, and is intended to use in conjunction with the C# MirrorReflection script of the MirrorReflection shader 2.0 :)
     
    Last edited: Nov 29, 2010
  35. Q_B

    Q_B

    Joined:
    Nov 29, 2010
    Posts:
    14
    Just to make myself clear, the problem is in

    Code (csharp):
    1. //half4 reflcol = texCUBE (_Cube, IN.worldRefl);
    2. half4 reflcol = tex2D (_ReflectionTex, IN.uv_ReflectionTex);
    Obviously, the "IN.uv_ReflectionTex" is wrong. Now, on the mirror shader, I see a " matrix [_ProjMatrix] " on the _ReflectionTex SetTexture part, and this seems to be what I'm looking for.

    But how can I access this correct projection matrix from within the CGPROGRAM shader section?

    Many thanx!
     
  36. Q_B

    Q_B

    Joined:
    Nov 29, 2010
    Posts:
    14
    I noticed I should have posted this on answers.unity instead (with a little advice from Proclyon :) ) . So, just to wrap things up, I started a thread there: http://answers.unity3d.com/questions/29508/complete-flat-mirror-reflection-shader

    Please, if you have any suggestions, do post it on that thread. If you're looking for answers for the same thing, well, try checking the thread to see if something has come up :)

    Sorry for the inconvenience, and hope this makes it right :)

    Many thanx!