Search Unity

Adding a special effect on top of an existing material

Discussion in 'General Graphics' started by Tibor0991, May 3, 2018.

  1. Tibor0991

    Tibor0991

    Joined:
    Dec 11, 2016
    Posts:
    27
    Here's the situation:

    Suppose I have a set of objects in the scene, each rendering its own mesh and possessing its own material. I want to add an effect to a single item or to a set of items based on a certain trigger event (doesn't matter if it triggers from code, from mouse clicks or whatever).
    This effect must apply to the whole mesh and must affect the texture of the host item, for example like a force-field texture all over a character or a fresnel outline.
    What would you do?

    Replacing the material of each object with a custom-made shader bearing this effect would be unfeasible, too many materials and it will break up as soon as I want to use another material or if I want to add a new feature.
    I've tried adding another material slot to a specific item, but Unity warns you about being an expensive operation render-wise, so it looks like it's not the best practice.
    What I would like to achieve is something similar to the reddish outlines/fresnel highlight some enemy NPCs have in hack'n'slash games, it looks like something that gets applied to the model on top of its original material and it blends with the texture underneath.
    EDIT: For the sake of clarity, I'm not looking for ways to add a material to a single or multiple objects, that was just for context.

    How can I implement this feature in the best way possible?
     
  2. Artaani

    Artaani

    Joined:
    Aug 5, 2012
    Posts:
    423
    For some special effects the only way to do it - custom shader for all of your objects. For example if you need rim light, it should be supported by shader.

    However seems like in your case simple outline effect will be enough. For that task you can use this asset.
    https://www.assetstore.unity3d.com/en/#!/content/78608
    It will require just add component on the object.

    If this asset will not suit your need, just search "outline" word on the asset store.
     
  3. Tibor0991

    Tibor0991

    Joined:
    Dec 11, 2016
    Posts:
    27
    Ouch, this sounds like's going to be a really long work...
    I really don't need the outline asset but thank you anyway, I can make them myself with Amplify.
    Still, I hoped there was a one-size-fits-all approach for this, because I have some scenes filled with lots of items that need to "disappear" with a shader effect.

    So in the end, the only solution I have is to replace every standard material with a shader carrying all the effects I require for my project?
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    This is fine. It's just a warning to keep people from doing it unintentionally. Pretty much any other option, apart from replacing the shader, is just as or more expensive. However there are a few things to note. If your mesh has multiple materials to begin with, this is broken and additional material indices just draw the first sub mesh. Also if your glow shader vertex pass and base shader vertex pass do have their output position match perfectly there may be some z fighting. This can happen even if the shaders are identical due to Unity batching / instancing one pass but not the other, or many other reasons, so a small offset may be needed.

    The method I use is a command buffer DrawRenderer() with a material override.
     
    Opeth001 likes this.
  5. Artaani

    Artaani

    Joined:
    Aug 5, 2012
    Posts:
    423
    Maybe not manually. Most likely in 99% cases it it standard Unity PBR shader. So you may create a new material, and if you need to perform "dissolve" effect for any object - just replace material to the new one using a code, and setup the same textures for the new material.
     
  6. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    do you have an idea about:
    How do games like SpellBreak apply these kinds of effects to other objects like the ground in this case?
    is there a way to do this in Unity Engine?

     
    Last edited: Jan 23, 2021
  7. fritzlloydw

    fritzlloydw

    Joined:
    Oct 28, 2018
    Posts:
    1
    You can apply additional materials to an objects mesh renderer. This might be an additional material on the ground that applies this snow texture, based on a texture that the spell in the game write to or something like this.
     
    Opeth001 likes this.
  8. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    this seems like the way to go, i think this is done by also applying the material to specific areas of the mesh using texture channels per object renderer (like Unity Terrain does).
    Is there a way to apply multiple materials to a mesh using an API like graphics.drawmeshinstanced / indirect instead of the standard MeshRenderer component?
    I'm asking this question because in my project I'm using my own render pipeline based on DOTS.
     
  9. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    I suspect most games do this using a separate mesh decal, so that the underlying ground mesh requires no special preparation or modification. I don't think unity has a built-in feature for mesh decals but I've seen some on github or the asset store.

    Edit: or a projector might work too. Not sure what the difference is, actually. since I was under the impression that projectors render on a separate mesh as well.
     
    Last edited: Jul 20, 2021
    Opeth001 likes this.
  10. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    Mesh decals seem to be expensive because they require the special mesh to be generated at runtime and do not work well with SkinnedMeshes. (im doing some research)

    Thanks!
     
  11. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    The answer for how this is done is "it depends".

    Spellbreak itself appears to be using several different techniques depending on the the effect and use case.

    Option 1: "Deferred" Decals
    Basically rendering a big transparent box in the world that uses the depth texture and normal texture to determine where to render to. Usually used with either stencils or a separately rendered mask so it doesn't affect animated meshes like characters. Fast, cheap-ish in small numbers, conforms to any surface automatically. Doesn't necessarily require using a deferred rendering path, but does need at least a screen space depth texture. Can get expensive due to overdraw, and can have some issues with mip mapping on depth discontinuities.

    Most of Spellbreak's effects appear to be using this.

    Option 2: Mesh "Decals"
    Yes, these can get expensive when dealing with lots of objects if done the traditional way of just rendering the entire mesh again (which is how Unity's built in projectors work). But generally you want to limit this to skinned meshes. Basically it's a fancy way of saying "render the character mesh a second time with another material".

    More advanced systems might take the original mesh and chop it up so only those triangles that you want to see get re-rendered. This was common in very old versions of Unreal Engine or console games when the triangle count on everything was way lower.

    Option 3: Direct Surface Material Modification
    Most common on terrain. Any time you want to render a big change to the ground, why not just change the terrain data used to blend textures! Have the terrain already have "snow", "burned", and "slime" textures and paint them in / out at runtime.

    Similarly, games like Portal 2 and Splatoon do something similar where they're painting textures that are mapped to the world like light maps that can hide / show extra texture layers.

    Option 4: Mesh "Decals" Part 2
    Instead of re-rendering the mesh, just render a completely different mesh on top! Maybe use alpha blended edges or a deferred decal to hide the seam. Works pretty well on flat ground or even mostly smooth terrain. This has been a mainstay in the VFX artist bag of tricks for making ground crack open, etc.
     
    dudleyhk and Opeth001 like this.
  12. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    it's very well explained :)
    I think I will use a combination of these techniques depending on the game use cases.
    Thanks!!!