Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Blend Modes

Discussion in 'Assets and Asset Store' started by Elringus, Jan 16, 2015.

  1. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    578
    Hi,

    It seems like this error could be related to a bug in Unity: https://issuetracker.unity3d.com/issues/ios-shader-compilation-failed-error-in-the-log

    I'm currently don't have access to iOS device and can't test this myself, but it seems like it should be fixed in Unity 5.4.0. Could you please test this out? If that's not the case, please contact me via support email and I will assist you further.
     
  2. s141797

    s141797

    Joined:
    Nov 18, 2014
    Posts:
    7
    Thanks I'll get back to you if I figure something out!
     
  3. TeorikDeli

    TeorikDeli

    Joined:
    Apr 6, 2014
    Posts:
    29
    Hi @Elringus !

    I purchased your plugin, everything works well on the Editor, but there is a problem with the iOS. There is no build error/warning in Unity & Xcode; but in the runtime if I try to open the scene with blend mode, Xcode gives me an error:

    "Metal: Error creating pipeline state: Shaders reads from a color attachment whose pixel format is MTLPixelFormatInvalid
    (null)
    (lldb)"

    I used framebuffer mode with 1 Color Burn layer & 1 Multiply layer. I don't know if this is a Unity 5.3.4p5. And, if I use Grab mode, there is a problem with rendering on iPhone 6 & 6S (also, tested on iPhone 5, iPad 3, some Android devices with no render problem at all with grab mode). Maybe this is related to 64bit. I don't want to use grab mode for production (bad for performance), so I can wait until the framebuffer problem be solved.

    I can send you screenshots from each devices, if you need. I also added framebuffer logs for iPhone 4 & 5. Scene opens on iPhone 4 & 5, but the render is not correct (it's pale).
     

    Attached Files:

    Last edited: May 7, 2016
  4. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    578
    Hi,

    It looks like a Metal issue and I’ve seen quite a lot of bugs with it in Unity 5.3 lately. Try to switch graphics API in the build setting from Metal to OpenGL ES 3.0.

    In case it won’t help, please contact me via the support email for further assistance.
     
  5. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    578
  6. rainisto

    rainisto

    Joined:
    Jul 19, 2015
    Posts:
    41
    Hi, I bought your asset, it works well, thank you!

    One question / feature suggestion: would it be possible for the blocks to render their shadows on their textures?

    For instance, put a white Cube in normal mode and and another object so that its shadow is hitting the cube surface and a shadow is then visible. When I put this cube into Multiply mode, nothing is seen: the shadow is gone.

    In my case I'd really love to be able to have just the shadow showing on the cube surface that is seen in Normal mode. Quick screenshots of what I mean.

    A Matte shadow, essentially. http://forum.unity3d.com/threads/matte-shadow.14438/ But with blending modes: I'd love to be able to use all of these blending modes with that...
     

    Attached Files:

    Last edited: Aug 4, 2016
  7. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    578
    Hi rainisto,

    It’s possible, but the shader won’t be able to use transparency (only cutout) and it won’t blend with skybox and other objects, which are drawn at sorting levels beyond "Transparent" layer. This is due to Unity limitation (more about it here).

    I’ve made a shader working like that:



    As you can see, it receives the shadow, but ignores skybox and uses camera “Background” color instead.

    If those limitations are ok for you, you may grab the shader here: https://gist.github.com/Elringus/7790892b9608e8a30180b7b8f70707f0 Just drop it with replacement into BlendModes\Resources\BlendModes\MeshDefault folder and all the blend modes for 3D meshes will use it when under Grab mode. If you need this shader for other modes – let me know.
     
    rainisto likes this.
  8. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    578
    Update: version 2.5 with new camera overlay effect and extra shaders

    It's now possible to apply blend mode effect to a camera, which allows blending an arbitrary overlay texture on top of the camera output with any of the 25 available blend modes. In this mode the plugin will use Blit function to access the render texture, which will work smoothly on any platform, including mobiles.



    You may now also find some additional shaders at the “BlendModes/Extra” directory. They utilize custom blend material editor and could be useful in some cases.

    Following extra shaders are currently available:
    • GlassDistortion — Combines glass distortion and blend mode effects;
    • SurfMesh — Surface version of the MeshDefault Grab shader. Could be used when a mesh with blend mode effect needs to receive shadows.
    Feel free to contact me if you need any additional shaders — I will be happy to expand this "extra" collection :)
     
    hopeful likes this.
  9. rainisto

    rainisto

    Joined:
    Jul 19, 2015
    Posts:
    41
    Great, thank you for this! I'll play around with it for a while and let you know how it goes.
     
  10. rainisto

    rainisto

    Joined:
    Jul 19, 2015
    Posts:
    41
    Thanks for the SurfMesh shader. It does a lot things well (I see what you mean by not working with Skyboxes though - that's ok).

    It's now transparent as in that it lets light and shadows also through. I was ideally hoping for something that would use a blend mode, capture shadows - but not let them through to the next object... It kind of doubles all lights and shadows, which is slightly annoying.
     

    Attached Files:

  11. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    578
    Well, it’s a known issue in Unity when a spotlight passes through walls. For example, if you switch to the default-diffuse shaders, it will still happen:


    That said, I believe the issue has been somewhat solved with the new standard shader in Unity 5. Please insure, that you have shadow casting enabled for the spotlight and the shadow strength is set to 1. Also, verify that the “receiver” object (the one behind the “blend mode” object) has a standard material, uses opaque mode and “receive shadows” is enabled. This way I was able to make SurfMesh shader to receive shadows and “block” the light from passing through:
     
  12. rainisto

    rainisto

    Joined:
    Jul 19, 2015
    Posts:
    41
    Hi, I have an unusual request. :) It's a bit of a challenge.

    I'd love to have a blend mode / shader that works the following:
    1. If it's on a material in a scene that has no lights, the surface is purely transparent, you see through it.
    2. If there's a light (point light, spotlight at least) that is in range of the surface, and the light hits this surface, it lights up this transparent surface as you would expect.
    3. If there's a shadow created by this light, i.e. an object that casts an shadow, the shadow is seen on this surface.

    So if you imagine a scene with one light and one cube next to it, you see the light on the ground, and the shadow cast by the block, but the rest would be purely transparent.

    1. messes me up - shaders now assume that everywhere else is dark if there isn't a light in the room. No, i'd like those to be transparent. Only the light hit on the surface and the shadows cast should be visible, the rest should be transparent.
     
  13. rainisto

    rainisto

    Joined:
    Jul 19, 2015
    Posts:
    41
    Sorry, I was being stupid - actually it looks like I can pretty do this already with SurfMesh, I just needed to fiddle around with the settings a bit to make the shadows and lights appear at the same time. It's beautiful work, thanks again!
     
  14. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    578
    Yes, it’s more of the usual scripting than shaders thing :) Shadow casting/receiving settings are already exposed in the default Unity shaders. But feel free to request any additional changes for the shader, if you happen to need anything.
     
  15. rainisto

    rainisto

    Joined:
    Jul 19, 2015
    Posts:
    41
    Yes, being the shader guru that you obviously are :) - the 'problem' with SurfMesh is that it's very tricky to control since it shows all indirect shadows also.

    Would it be possible to create a DirectSurfMesh, i.e. a shader that
    - only shows direct lights, no indirect lighting
    - only shows direct shadows, no indirect shadows
    - if there isn't either of them, it's purely transparent
     
  16. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    578
    I'm not sure what you mean by indirect shadows (as far as I know, there are only dynamic and baked shadows in Unity), but here is a modified SurfMesh shader, which only receives directional light and have all the light-mapping / GI features disabled: https://gist.github.com/Elringus/64a3572f851abf1674aed86eeda08c8c

    As for making it "conditionally" transparent: I'm not sure if this is possible with only shaders code. I think you'd better try to achieve it with a scripting. For example, you may check if there any light sources in the scene and toggle mesh renderer component accordingly.
     
  17. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    578
  18. camel82106

    camel82106

    Joined:
    Jul 2, 2013
    Posts:
    306
    Hello,
    is it possible to achieve effect that I will describe on this map?

    Those magenta lines and circle overlaps a little, although you cannot see it on screenshot.
    What I want to achieve is to make them transparent to some degree. But I want to have same magenta color and transparency everywhere. This is now problem as overlapping areas will be darker, because lines and circles blend with each other.

    So point is that lines and circles may blend with map background but not with each other.

    Is this possible?
    Thanks
    Peter
     
  19. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    578
    Hi,

    You can make the lines and circles to not blend with each other by setting the render mode to "Unified Grab", however, if you use transparency, the overlapping areas will still be visible. What you can do is set the transparency to 0 (make them opaque) and use a blend mode effect with Unified Grab mode. That way you should be able to achieve what you want.

    I’ve made a quick example. Here I have two magenta lines (as sprites) with the Blend Mode overlay applied under the default Grab mode. The overlap areas are visible:



    And now, if we set render mode to Unified Grab:



    The overlap areas disappear.
     
    camel82106 likes this.
  20. camel82106

    camel82106

    Joined:
    Jul 2, 2013
    Posts:
    306
    One last question before buy, sorry. Is it possible to use mesh renderer with shader Unlit/color. And then use your blending with it?
    Thanks
     
  21. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    578
    When Blend Mode Effect component is applied to an object, it replaces the material used in the renderer (the effect itself is achieved via shader). When it’s used with a mesh it actually utilizes standard Unity unlit-color shader where you can choose tint color and a texture:



    There are also additional "extra" shaders, that can be used to couple the blend effect with some other techniques, like glass distortion or a specific light\shadow behavior. In case you’ll need some other special shader to work with the blend modes – feel free to request it.
     
    camel82106 likes this.
  22. MooMoo-Games

    MooMoo-Games

    Joined:
    Jan 10, 2013
    Posts:
    12
    Hi,

    I bought Blend Mode Effect then just launched the demo scene.

    Nothing works correctly except Normal shader in my Unity 5.2.2.

    What's wrong with mine?


    2016-09-16 (2).png
     
  23. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    578
    Hi,

    Seems like some of the shaders are missing or corrupt. Please try to delete the BlendModes folder from the project, then download and import it again from the Asset Store (sometimes the package got corrupted).

    If the above won’t resolve the issue, please contact me via the support email and I will assist you further.
     
  24. MooMoo-Games

    MooMoo-Games

    Joined:
    Jan 10, 2013
    Posts:
    12
    You are right.

    It works fine after I re-download and import the your assets.

    Thank you.
     
  25. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    578
    Coming soon: masking (selective blending) feature

    New feature will allow to selectively apply blend mode effect. For example, it will be possible to apply the effect only when the blended object is over specific (masked) objects and fill the rest with normal blend mode or even cut the pixels out (make them transparent).



    Additionally, a number of new extra shaders and bug fixes will be included with the new version.

    If you wish to try new version right now, feel free to request it via the support email (don't forget to include the invoice number).
     
    wetcircuit likes this.
  26. anton-melegov

    anton-melegov

    Joined:
    Jun 18, 2015
    Posts:
    10
    Hello I have and issue with asset. After update to new version all UGUI masks become broken. Please add mask mode which supports UGUI mask and set it to default value. Masking technique which one you provide doesn't suitable to UGUI because it is doesn't support Z ordering
     
  27. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    578
    Hello,

    Blend Modes masking feature will not work with Unity UI masks, as both of them utilize Stencil Buffer. If you want to use blend effects and UI masks together, you can either refrain from using Blend Modes masks (don’t activate the ‘Auto Mask’ property) or disable selective blending feature completely by setting DEFAULT_STENCIL_REF constant to 0 (the constant can be found in BlendModes/Scripts/BlendModeEffect.cs). You can find more about masking feature limitations in the docs: https://goo.gl/k8LuWP
     
  28. mankenDeveloper

    mankenDeveloper

    Joined:
    Dec 19, 2016
    Posts:
    11
    Hi Elringus,

    for some blend mode, i want to edit the formula, i try to edit the BlendModes.cginc's formula, but i doesn't work. how should i do? thankyou
     
  29. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    578
    Hi,

    Changing the formulas in BlendModes.cginc should work just fine, but you may need to force the shaders to recompile in order to see the results. Try clicking RMB on the BlendModes folder and selecting 'Reimport' -- that should do the trick.
     
  30. fastgamedev

    fastgamedev

    Joined:
    Mar 27, 2014
    Posts:
    91
    Hi @Elringus,

    Great asset, thank you for making it.

    I am puzzling how to achieve a very common effect in game UIs, and while Blend Mode asset seems to fit perfectly, I've been running into walls with every approach. Can you help me figure it out please?

    The effect I want is an animated highlight line over UI, like this:
    https://gfycat.com/UnawareElasticClownanemonefish
    Here it's used on buttons "Buy Coins" and "Rewards" at the top, "Boost Me" on the bottom, the blue border in the lower right corner, on jackpot box that's partially hidden by the guy's avatar.

    So, the goal is to have a button with an animated highlight over its border.
    The basic approach is this: I have a UI texture A of a button, a masking texture B of the button's border, and a highlight line texture C. I need to mask C with B and blend the result with A.
    What I have tried so far:
    1. Use Blend Mode's auto-mask feature to mask C with B. This works well, except elsewhere I have a ton of UI elements with ScrollView that use UGUI masks. These UGUI masks break Blend Mode's mask.
    2. Use UGUI mask to mask C with B. This works, but when I add Blend Mode to C (without auto-masking) it for some reason ignores UGUI mask.
    3. Another way this effect can be achieved is by first blending C with B with a difference/multiply blend mode (basically faking a mask), and then blending the result with A. I haven't tried this since Blend Mode doesn't support selective blending.

    So, how can I get this to work?
     
    Last edited: Feb 11, 2017
  31. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    578
    Hi,

    Blend Modes is not compatible with Unity’s UI masking feature by default. However, you can achieve the effect with the MaskableUI and MaskableFont extra shaders using custom blend materials feature. Just create a material, set the shader (you can find extra shaders in BlendModes/Extra), choose desired blend mode and apply the material to UI objects you want to be masked.You can find more about custom blend materials and extra shaders in the docs: https://goo.gl/tSAmB7
     
  32. fastgamedev

    fastgamedev

    Joined:
    Mar 27, 2014
    Posts:
    91
    Wow, this works! Excellent, thank you.

    A couple of things:
    1. There are many parameters in the MaskableUI shader inspector. Are any of them useful to tweak?
    2. Bug? When changing the blend mode in the MaskableUI shader, the texture is not visually updated in the scene/game view. I have to disable/enable its gameobject to show changes.
    3. Is there any way to do sequential blending, ie blend A with B using screen mode, then blend the result with C using soft light mode?
     
  33. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    578
    1. Those are the params needed for Unity UI masks to work and usually you won’t tweak them. I’ll hide them from inspector in the next update to avoid confusion. Thank you for pointing this out!
    2. Yes, this looks like a Unity bug. It only happens when the blended object is under the effect of UI mask. If you disable the mask component changing material properties will work as intended. I’ll try looking for corresponding reports on the issue tracker and file a new one, if there is none.
    3. Sure, you just stack the blended objects on top of each other. For example, you’ll place A image on the bottom, image B with screen mode on top of A and then C (with soft light effect) on top of B.
     
  34. skeetlebead

    skeetlebead

    Joined:
    Nov 24, 2015
    Posts:
    4
    Elringus, I am not able to get the blend mode working with a mask as described above. I am unable to select a blend mode on my new material (see screenshot below), as opposed to the example shown in the documentation. Can you point me towards what I'm doing wrong? My steps:
    1) Create a new material
    2) Change the shader to BlendModes/Extras/MaskableUI

    Thanks!

    Screen Shot 2017-03-17 at 12.13.56 PM.png
     
  35. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    578
    Well, that's weird. There should be a 'Blend Mode' property right after 'Render Queue':



    Ensure that you're using the latest version of the asset (2.7, currently). Try to delete Blend Modes folder from the project, than re-download and re-import the package from the Asset Store. If it won't help, please check the following:
    • what Unity version you're using
    • are there any warnings/errors in the console
    • is the issue reproducible in a clean new project
     
  36. skeetlebead

    skeetlebead

    Joined:
    Nov 24, 2015
    Posts:
    4
    Looks like the first time around I didn't correctly delete the old plugin before installing the new version. Now that that's done I am seeing the blend mode drop down! However, I am getting the following error now when selecting a blend mode:

    I'm on 5.5.0p4 btw.
     
  37. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    578
    Try to re-import the BlendModes folder if you happen to move it after initial import:
     
  38. skeetlebead

    skeetlebead

    Joined:
    Nov 24, 2015
    Posts:
    4
    That fixed it! Thank you so much!
     
  39. stefanob

    stefanob

    Joined:
    Nov 26, 2012
    Posts:
    45
    I'm using multiply on UI sprites and it works fine on every device (different iPads and iPhones) but not on iPhone 7 plus. There the sprites are black. Do you know why?
    Thanks
     
  40. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    578
    Oh, I’ve somehow missed your message here, sorry. If the issue is still relevant, please check if the build logs any errors/warning (debug options should be enabled) and try switching graphics API in the build settings (Metal, OpenGL ES3) and check if there are any differences.
     
  41. mcmorry

    mcmorry

    Joined:
    Dec 2, 2012
    Posts:
    581
    Hi @Elringus,
    would be possible to add a blurring effect together with the blend modes?
     
  42. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    578
    Hi,

    It depends on how the blurring effect is achieved. The blend mode effect is done via shader, so it would replace material used by the affected object. If the blurring is also done via shader -- they won't work together. It's still could be possible to "unite" them in one custom shader, though.
    That said, as far as I know, blurring is usually achieved via post-process effects (like this one https://docs.unity3d.com/500/Documentation/Manual/script-BlurOptimized.html ). In this case blend mode effect will work without any issues.
     
  43. mcmorry

    mcmorry

    Joined:
    Dec 2, 2012
    Posts:
    581
    Sorry I wasn't clear. I'm aware of the blurring methods (full screen and not), this is why I was wondering if you could extend your shaders to support also blurring of the background (for example for an UI panel).

    I'm asking this because, if I'm not wrong, you are already using a GrabPass for your shader and could be good for the performance to reuse the same pass.
     
  44. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    578
    I see. Yes, I think I can add an extra UI shader for that. If you have a reference implementation of the blur effect -- would be nice if you can share it with me :)
     
  45. mcmorry

    mcmorry

    Joined:
    Dec 2, 2012
    Posts:
    581
  46. mcmorry

    mcmorry

    Joined:
    Dec 2, 2012
    Posts:
    581
  47. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    578
    Ok, here is the shader: https://gist.github.com/Elringus/080dfcf6fa147f72a130ccd1cc92908d

    Just drop it inside \BlendModes\Resources\BlendModes\Extra folder and it should become available in the Extra shaders collection as 'UIBlured'. Create a custom material, select this shader and assign the material to UI object. You can control blend mode and blur amount (Blur Radius slider) in the material properties: https://gyazo.com/59e455e9388ccfb83b82517f5e7f129e
     
  48. mcmorry

    mcmorry

    Joined:
    Dec 2, 2012
    Posts:
    581
    Wow, that was fast! :) Thanks!

    I see that you have only one blurring pass. Was a problem to have the 2 passes? The blur effect seems a bit "weak" like this.
     
  49. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    578
    Well, I've actually messed it up a bit: alpha of the UI texture was affecting blur strength. Fixed that and added additional blur sampling, it looks like this now on a moderate blur level:

     
  50. mcmorry

    mcmorry

    Joined:
    Dec 2, 2012
    Posts:
    581
    Looks better. I'll try it :)
    Thank you again. Wonderful support!
     
    hopeful likes this.
unityunity