- Unity 5.x Shaders and Effects Cookbook
- Alan Zucconi Kenneth Lammers
- 694字
- 2021-07-16 12:59:39
Creating a Holographic Shader
More and more space-themed games are being released every year. An important part of a good sci-fi game is the way futuristic technology is presented and integrated in the gameplay. There's nothing that screams futuristic more than holograms. Despite being present in many flavors, holograms are often represented as semi-transparent, thin projections of an object. This recipe shows you how to create a shader that simulates such effects. Take this as a starting point: you can add noise, animated scanlines, and vibrations to create a truly outstanding holographic effect. The following image shows an example of a holographic effect:

Getting ready
As the holographic effects shows only the outlines of an object, we'll call this shader Silhouette
. Attach it to a material and assign it to your 3D model.
How to do it…
The following changes will modify our existing shader into a holographic one:
- Add the following property to the shader:
_DotProduct("Rim effect", Range(-1,1)) = 0.25
- Add its respective variable to the
CGPROGRAM
section:float _DotProduct;
- As this material is transparent, add the following tags:
Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
Note
According to the type of object that you will use, you might want its backside to appear. If this is the case, add
Cull Off
so that the back of the model won't be removed (culled). - This shader is not trying to simulate a realistic material, so there is no need to use the PBR lighting model. The Lambertian reflectance, which is very cheap, is used instead. Additionally, we should disable any lighting with
nolighting
and signal to Cg that this is a Transparent Shader usingalpha:fade
:#pragma surface surf Lambert alpha:fade nolighting
- Change the
Input
structure so that Unity will fill it with the current view direction and world normal direction:struct Input { float2 uv_MainTex; float3 worldNormal; float3 viewDir; };
- Use the following surface function. Remember that as this shader is using the Lambertian reflectance as its lighting function, the name of the surface output structure should be changed accordingly to
SurfaeOutput
instead ofSurfaceOutputStandard
:void surf(Input IN, inout SurfaceOutput o) { float4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color; o.Albedo = c.rgb; float border = 1 - (abs(dot(IN.viewDir, IN.worldNormal))); float alpha = (border * (1 - _DotProduct) + _DotProduct); o.Alpha = c.a * alpha; }
You can now use the Rim effect slider to choose the strength of the holographic effect.
How it works…
As mentioned before, this shader works by showing only the silhouette of an object. If we look at the object from another angle, its outline will change. Geometrically speaking, the edges of a model are all those triangles whose normal direction is orthogonal (90 degrees) to the current view direction. The Input
structure declares these parameters, worldNormal
and viewDir
, respectively.
The problem of understanding when two vectors are orthogonal can be solved using the dot product. It's an operator that takes two vectors and returns zero if they are orthogonal. We use _DotProduct
to determine how close to zero the dot product has to be for the triangle to fade completely.
The second aspect that is used in this shader is the gentle fading between the edge of the model (fully visible) and the angle determined by _DotProduct
(invisible). This linear interpolation is done as follows:
float alpha = (border * (1 - _DotProduct) + _DotProduct);
Finally, the original alpha from the texture is multiplied with the newly calculated coefficient to achieve the final look.
There's more…
This technique is very simple and relatively inexpensive. Yet, it can be used for a large variety of effects, such as the following:
- The slightly colored atmosphere of a planet in sci-fi games
- The edge of an object that has been selected or is currently under the mouse
- A ghost or specter
- Smoke coming out of an engine
- The shockwave of an explosion
- The bubble shield of a spaceship under attack
See also
The dot product plays an important role in the way reflections are calculated. Chapter 3, Understanding Lighting Models, will explain in detail how it works and why it is widely used in so many shaders.
- Learn TypeScript 3 by Building Web Applications
- 自己動手寫搜索引擎
- Learning Data Mining with Python
- 軟件項目管理實用教程
- INSTANT Django 1.5 Application Development Starter
- Learning ArcGIS for Desktop
- Creating Mobile Apps with jQuery Mobile(Second Edition)
- OpenCV 4計算機視覺項目實戰(原書第2版)
- HTML5開發精要與實例詳解
- 進入IT企業必讀的324個Java面試題
- 實戰Python網絡爬蟲
- Java設計模式深入研究
- Go Systems Programming
- 基于MATLAB的控制系統仿真及應用
- Learning Apache Thrift