Make mirror and reflections

  • #1, by TinTinWednesday, 30. March 2016, 15:56 8 years ago
    Hi

    Is it any sample for mirror reflection? Also for reflection water in ground like rainy weather.

    Forum Fan

    196 Posts


  • #2, by afrlmeWednesday, 30. March 2016, 17:08 8 years ago
    Someone else recently asked for something similar to this on here. SimonS provided an openGL shader script for it, but I don't know exactly how it works as it's written in C / GL language.

    Here's the link to the thread: http://www.visionaire-studio.net/forum/thread/shader-questio... I can confirm the script works, but I have no clue how to modify it.

    Alternative solution to mirror is to create a duplicated character of you active character with mirrored (opposite animations assigned to their directions - so north would be same as your south animation & west would be the same as your east animation but flipped horizontally & so on). How you would then implement it into the mirror itself... I'm not sure, but I would probably layer something over the mirror with part of the background then place the mirrored character behind that.

    Imperator

    7278 Posts

  • #3, by zurikWednesday, 29. May 2019, 09:04 5 years ago
    Hey. I'm picking up an old thread here and wonder how can I make that duplicated character to react, so he looks and moves in the opposite direction as the main character. Thanks

    Newbie

    40 Posts

  • #4, by zurikTuesday, 25. June 2019, 15:53 5 years ago
    I have tried different stuff, but I can't make my duplicate (mirror) character to move depending of the original character's movement.

    Newbie

    40 Posts

  • #5, by sebastianWednesday, 26. June 2019, 08:22 5 years ago
    i would try to create a function and register it tonthe main loop.
    It should set the position of the Dummy to the current characters position but have it multiplied by - 1 to invert the direction (either its x or y position depending on the mirror being horizontal or vertical). Then adding an offset depending on the starting position inside that scene/where the mirror is placed . 

    At least thats what i have in mind right now as a start

    Thread Captain

    2346 Posts

  • #6, by zurikWednesday, 26. June 2019, 14:42 5 years ago
    Thanks for you reply. How shall I "put position of the Dummy to the current characters position"?

    Newbie

    40 Posts

  • #7, by afrlmeWednesday, 26. June 2019, 15:57 5 years ago
    I have to be honest... trying to do this without using shaders to generate a mirrored reflection is a ton of work. You need to listen out for character animations, their positions, sort out offets & so on. Only having to deal with a characters idle/walk animations is one thing, but if you want to also include random animations/interaction animations, etc then things will get complicated real fast.

    I'm not sure if you are in our Discord server, but Caligari Marte has shared a bunch of shader stuff on there in the past & I believe one of the things was a reflection shader script. You can always join & @ping them about it.

    Imperator

    7278 Posts

  • #8, by nenad-asanovicWednesday, 26. June 2019, 17:18 5 years ago
    Well you should really check this post LINK it's really easy to make things work as you would basically need just to copy and adjust shader to your game as you wish. HEre is sample of how it looks in my game (bottom part of screen is shader reflection, perfect mirror).

    Newbie

    59 Posts

  • #9, by zurikWednesday, 26. June 2019, 19:25 5 years ago
    Thank you very much, Nenad. Your image looks exactly like what I need, but unfortunately it is difficult for me to understand how to create a reflection out of that discussion. Can I ask you to help me with more details? smile

    Newbie

    40 Posts

  • #10, by nenad-asanovicWednesday, 26. June 2019, 23:32 5 years ago
    Ok you need to add first code to your shader list and call it ReflectionShader01 (or any other name but you will then need to add it to Lua part. Then you can call this shader with Lua (second code here) with any action you create (at begin of scene or whatever you use). Also you will need to have your mask png linkded correctly so game knows where shader is appled on the screen. I will attach one I am using for my game but you can make any shape of it... Also other picture is used if you want to create waves effect alike water. My code if for clear non moving mirror tho. But you can play with settings to customize as you wish smile
    #ifdef GL_ES
    
    precision lowp float;
    
    precision lowp int;
    
    #endif
    
    uniform sampler2D texel;
    
    uniform sampler2D DistortTex;
    
    uniform sampler2D MaskTex;
    
    
    
    varying vec2 uvVarying;
    
    uniform float time;
    
    uniform float TintR;
    
    uniform float TintG;
    
    uniform float TintB;
    
    uniform float TintMix;
    
    uniform float Mirror_PosY;
    
    uniform float Distort_MoveX;
    
    uniform float Distort_MoveY;
    
    uniform float Strength;
    
    
    
    //uniform float Mirror_PosX;
    
    //uniform float Brechung;
    
    
    
    void main ()
    
    {
    
      vec2 uv = uvVarying;
    
      float Mask = texture2D(MaskTex, uv).r;
    
      float Distort = texture2D(DistortTex, fract(uv + vec2(time*Distort_MoveX,time*Distort_MoveY) ) ).r;
    
      vec4 ColorTint = vec4(TintR,TintG,TintB,1.0);
    
      float Brechung = 1.0;
    
      vec4 result = vec4(0.0);
    
      vec4 base = texture2D(texel, uv);
    
      float Offset = (Mirror_PosY-0.5)*2.0;
    
        // if y 0.67 then 0.34
    
        // if y 0.75 then 0.5
    
        // if y 0.5 then 0.0
    
      //
    
      if (uv.y > Mirror_PosY) {
    
        vec4 reflection = mix( texture2D(texel, vec2(uv.x,((1.0-uv.y)*Brechung) + (Distort*Strength) +Offset ) ) , ColorTint, TintMix);
    
        result = mix(base, reflection, Mask);
    
    
    
      } else {
    
        result = base;
    
      }
    
    
    
      gl_FragColor = result;
    
    }
    
    local eff = "ReflectionShader01"
    
    
    
    shader_effects[eff] = { shader = Shaders[eff].Compiled }
    
    shaderAddEffect(eff)
    
    
    
    bind(eff, "time", field("getTime()*0.001"))
    
    
    
    bind(eff, "TintR", field("0.3"))
    
    bind(eff, "TintG", field("0.3"))
    
    bind(eff, "TintB", field("0.3"))
    
    bind(eff, "TintMix", field("0.5"))
    
    bind(eff, "Mirror_PosY", field("0.78"))
    
    bind(eff, "Distort_MoveX", field("0.0"))
    
    bind(eff, "Distort_MoveY", field("0.0"))
    
    bind(eff, "Strength", field("0.0"))
    
    
    
    --bind(eff, "Mirror_PosX", field("1.0"))
    
    --bind(eff, "Brechung", field("1.0"))
    
    
    
    graphics.shaderUniform(shader_effects[eff].num.num, "_t_DistortTex", "vispath:effects/clouds02.png")
    
    
    
    graphics.shaderUniform(shader_effects[eff].num.num, "_t_MaskTex", "vispath:effects/mask02.png")

    Newbie

    59 Posts

  • #11, by zurikThursday, 27. June 2019, 15:30 5 years ago
    Thank you very much for the detailed description, man!
    I did as you told me and I call the action - Is there any changes I have to make in the code? I adjusted the names in the lines 20 and 22 in the second code, but I get "Failed to run string in Lua". 

    Newbie

    40 Posts