Coder Social home page Coder Social logo

erik-sutton95 / unityurpunlitscreenspacedecalshader Goto Github PK

View Code? Open in Web Editor NEW

This project forked from colinleung-nilocat/unityurpunlitscreenspacedecalshader

0.0 1.0 0.0 162 KB

Unity unlit screen space decal shader for URP. Just create a new material using this shader, then assign it to a new unity cube GameObject = DONE, now you have unlit decal working in URP

License: MIT License

unityurpunlitscreenspacedecalshader's Introduction

UnityURP-Unlit ScreenSpaceDecal Shader(SRP batcher compatible)

Before adding decal screenshot

After adding decal screenshot

Before adding decal screenshot

After adding decal(multiply blend mode) screenshot

Each decal is just a unity cube GameObject, using material of this shader, nothing else. screenshot

Before adding decal screenshot

After adding decal: (alpha blending) screenshot (additive) screenshot (multiply) screenshot (alpha blending, tint to HDR red, extra multiply alpha to rgb in shader) screenshot (opaque) screenshot (each decal is just a unity cube GameObject) screenshot (multiply) screenshot (additive, blue tint) screenshot

(no tiling) screenshot (add 4x4 tiling) screenshot (add 4x4 tiling , alpha * 2 - 1) screenshot (add 4x4 tiling , alpha * 4 - 3) screenshot (add 4x4 tiling , alpha * 10 - 9) screenshot

When should I use this shader?

if you need to render bullet holes, dirt/logo on wall, 3D projected UI, explosion dirt mark, blood splat, projected texture fake shadow(blob shadow) ..... and the receiver surface is not flat(can't use a flat transparent quad to finish the job), try using this shader.

How to use this shader in my project?

  1. clone the shader to your project
  2. create a new material using that shader
  3. assign any texture to material's Texture slot
  4. create a new unity default cube GameObject in scene (in Hierarchy window, click +/3D Object/Cube)
  5. apply that material to Cube Gameobject's MeshRenderer component's material slot
  6. edit the GameObject's transform so the local forward vector (blue Z arrow) is pointing to scene objects, and the cube is intersecting scene objects
  7. you should now see your new decal cube is rendering correctly(projecting alpha blending texture to scene objects correctly)
  8. (optional)edit _Color / BlendingOption, according to your needs
  9. (optional)finally make the cube as thin/small as possible to improve GPU rendering performance

I can see decal renders correctly, but which BlendMode should I use in the material inspector?

Blend SrcAlpha OneMinusSrcAlpha // Traditional transparency

Blend One OneMinusSrcAlpha // Premultiplied transparency

Blend One One // Additive

Blend OneMinusDstColor One // Soft Additive

Blend DstColor Zero // Multiplicative

Blend DstColor SrcColor // 2x Multiplicative

https://docs.unity3d.com/Manual/SL-Blend.html

Requirement when using this shader

  • Forward rendering in URP
  • Perspective camera
  • _CameraDepthTexture is already rendering by unity (toggle on DepthTexture in your Universal Render Pipeline Asset)
  • For mobile, you need at least OpenGLES3.0 (#pragma target 3.0 due to ddx() & ddy())

Is this shader optimized for mobile?

This screen space decal shader is SRP batcher compatible, so you can put lots of decals in scene without hurting CPU performance too much(even all decals use different materials).

Also, this shader moved all matrix mul() inside the fragment shader to vertex shader, so you can put lots of decals in scene without hurting GPU performance too much, as long as they are thin, small and don't overlap(overdraw).

I need LOTs of decals in my game, is there performance best practice?

  • make all decal cube as thin/small as possible
  • don't overlap decals(overdraw)
  • Set ZTest to LessEqual, and Cull to Back in the material inspector, if your camera never goes into decal's cube volume, doing this will improve GPU performance a lot! (due to effective early-Z, GPU only need to render visible decals)
  • disable _ProjectionAngleDiscardEnable, doing this will improve GPU performance a lot!
  • enable "generate mipmap" for your decal texture, else a high resolution decal texture will make your game slow due to cache miss in GPU memory

if you do every optimzations listed above, and your game is still slow due to this decal shader, please send me an issue, I will treat it as bug.

Editor System Requirements

  • Unity 2019.1 or later (due to "shader_feature_local"). But you can replace to "shader_feature" if you want to use this shader in older unity versions

Implementation Reference

Low Complexity, High Fidelity: The Rendering of INSIDE's optimized decal shader

https://youtu.be/RdN06E6Xn9E?t=2153

Screen Space Decals in Warhammer 40,000: Space Marine

https://www.slideshare.net/blindrenderer/screen-space-decals-in-warhammer-40000-space-marine-14699854?fbclid=IwAR2X6yYeWmDiz1Ho4labx3zA3GATpC7fi5qNkzjEj-MYTOBpXnkIsnA3T-A

unityurpunlitscreenspacedecalshader's People

Contributors

colinleung-nilocat avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.