Search Unity

Reflective Shader with transparency...

Discussion in 'Shaders' started by chaosmonger, Dec 24, 2019.

  1. chaosmonger

    chaosmonger

    Joined:
    Jan 23, 2019
    Posts:
    71
    Ok, so, first of all, Merry Xmas to everybody! And Happy New Year...

    What I'm struggling this holiday, is to find a real-time reflective shader, that I can put on top of a 2D background and at the same time having a reflective map (i.e. on black no reflection, on white full reflection, on gray mid reflection) as well as a bump/normal map for roughness.

    The reflective area is always a plane, but I've had problems using Keijiro Adam Plane Reflection (that doesn't work on Mac and doesn't have the possibility to texturize the reflectivity).

    My game is 2.5D (2D background with 3D characters). And I'd like to reflect the characters to match the puddles of my 2D backdrops. Like this one:
    BrightAlley02.jpg


    That, as you can notice, has the left side of the road more reflective than the right side:
    BrightAlley02_puddle.jpg


    The background is created in 3D, so I can export the plane and camera perfectly into Unity to have a perfect match.
    But my problem is: how can I apply to the plane mesh a shader that is transparent (or semi-transpratent: i.e. returning only the reflection, without having any other texture to not cover my background), and also setting how much of the reflection through a map? (i.e. more reflective in one area, less in others) All of this in real-time to reflect properly on the ground my 3D characters.

    Please consider I cannot switch to HDRP due to some other shaders and settings I'm using...

    Thank you very very much! If my request is not clear, please ask me anything and I'll try to clarify as much as possible!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    There's no reason the Adam Planar Reflections couldn't work on Mac. It’s not doing anything particularly exotic, it’s just that it was written for the Adam demo which they explicitly targeted DirectX 11 for, but if you remove the “only_renderers” line from the Convolve.shader it may “just work”.
     
  3. chaosmonger

    chaosmonger

    Joined:
    Jan 23, 2019
    Posts:
    71
    Thanks a lot. Will give it a try. I'm not a Mac user, but when building for Mac, all my testers reported the ground to be "pink". While for Linux and Win all work perfectly.
    Anyway, the other problem with Adam Planar Reflection, is to have a mask map, I cannot find a way to set a map for not having the whole plane to be reflecting... Any hints in that sense?
     
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I highly suggest listening to bgolus :) Wouldn't mask map simply be your smoothness in the materials?
     
  5. chaosmonger

    chaosmonger

    Joined:
    Jan 23, 2019
    Posts:
    71
    Thanks for your reply.
    Will dig more into the "specular alpha" setting of the smoothness... I've tried in the past with bad results, but probably because I was using just a black/white mask, while probably I've to use a black sprite with alpha...
     
    Last edited: Dec 25, 2019
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    The Adam Planar Reflections are a starting point, not the end. It does some useful stuff, but it's also explicitly designed for reflecting the world, not to reflect 3D objects and otherwise be transparent. The existing blurring (Convolve.shader) only blurs the RGB color, and explicitly ignores & removes the alpha, which you'd want to keep so you have the mask you need for the real-time 3D and pre-rendered background. So you'd need to modify the Convolve.shader to include the alpha. Then you'd need to modify the reflection shader to use the alpha of the blurred reflection to fade out the reflection object, or otherwise blend to the background texture.

    There's also hard problems like how to have pre-rendered objects show up in front of characters in the reflections.
     
  7. chaosmonger

    chaosmonger

    Joined:
    Jan 23, 2019
    Posts:
    71
    Actually through Culling Mask, playing with alphas and colors, I managed to obtain exactly what I wanted!
    Will publish a video later on.
     
  8. chaosmonger

    chaosmonger

    Joined:
    Jan 23, 2019
    Posts:
    71
    So... I've made it work perfeclty on Windows, but on Mac I still have problems, even if I removed the line as you suggested on the Adam Planar Reflections script...

    This is how it looks on Mac:
    Screen_Shot_2020-01-01_at_5.34.46_PM.png
     
  9. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Have your Mac user send you the game’s log file. It should have a shader error message, hopefully with a line number and a description of the error (like you’d see in the editor).

    MacOS’s shader compilers are much more strict about being explicit about certain things. Things like mis-matched vectors or matrix multiplies will just get handled by Windows shader compilers, but MacOS (and really most other platforms) will throw a fit.
     
  10. chaosmonger

    chaosmonger

    Joined:
    Jan 23, 2019
    Posts:
    71
    This is the errors he gets...

    Shader 'Adam/PlaneReflections Standard (Specular setup)': fallback shader 'Adam/Standard (Specular setup)' not found
    WARNING: Shader Unsupported: 'Adam/PlaneReflections Standard (Specular setup)' - Pass 'FORWARD' has no vertex shader
    WARNING: Shader Unsupported: 'Adam/PlaneReflections Standard (Specular setup)' - Pass 'FORWARD_DELTA' has no vertex shader
    WARNING: Shader Unsupported: 'Adam/PlaneReflections Standard (Specular setup)' - Pass 'ShadowCaster' has no vertex shader
    WARNING: Shader Unsupported: 'Adam/PlaneReflections Standard (Specular setup)' - All passes removed
    ERROR: Shader Shader is not supported on this GPU (none of subshaders/fallbacks are suitable)WARNING: Shader Unsupported: 'Adam/PlaneReflections Standard (Specular setup)' - Setting to default shader.
     
  11. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    That shader also has the “only_renderers” line. That along with
    #pragma target 5.0
    , which also won’t run on MacOS unless your project is set to use Metal as the graphics API. Really the shader isn’t doing anything super fancy, and
    #pragma target 3.0
    (aka Direct3D 9) will work, and run on MacOS OpenGL.
     
  12. chaosmonger

    chaosmonger

    Joined:
    Jan 23, 2019
    Posts:
    71
    Done! And it works... thanks a lot!