官术网_书友最值得收藏!

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:

  1. Add the following property to the shader:
    _DotProduct("Rim effect", Range(-1,1)) = 0.25
  2. Add its respective variable to the CGPROGRAM section:
    float _DotProduct;
  3. 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).

  4. 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 using alpha:fade:
    #pragma surface surf Lambert alpha:fade nolighting
  5. 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;
    };
  6. 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 of SurfaceOutputStandard:
    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.

主站蜘蛛池模板: 青岛市| 威远县| 南丹县| 连州市| 廊坊市| 萍乡市| 铜梁县| 土默特左旗| 古田县| 丰原市| 盖州市| 井冈山市| 合水县| 广州市| 平顶山市| 铅山县| 南漳县| 南召县| 郁南县| 吉首市| 杭州市| 黄山市| 通州市| 手机| 广安市| 大邑县| 海盐县| 沁源县| 镇江市| 黄浦区| 泸西县| 海阳市| 桐乡市| 文化| 富蕴县| 玉林市| 潞城市| 平果县| 腾冲县| 杭州市| 桂林市|