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

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.

主站蜘蛛池模板: 北宁市| 毕节市| 庄浪县| 台南市| 泽库县| 合水县| 句容市| 方正县| 杭锦后旗| 句容市| 肥西县| 千阳县| 商河县| 彭山县| 泽库县| 吉安县| 石河子市| 云浮市| 剑阁县| 吕梁市| 鸡泽县| 普格县| 永靖县| 宣城市| 青冈县| 天镇县| 马尔康县| 东辽县| 揭阳市| 陆川县| 剑阁县| 安西县| 淅川县| 瑞金市| 郸城县| 盐津县| 栾城县| 余庆县| 阳城县| 宜兴市| 柘城县|