Playing with shaders – isolines

Posted by admin on December 23, 2013 in Unity3d |

World space isolines shader

The shaders in Unity still remain a myth for me. But when I have a goal, I would find a way to make it by modifying already existing ones, basing on the resources in the web.
This time I targeted myself at getting an isolines shader. Its demo is here. There is a slider provider for changing the heiht itervals relative to the centre of the sphere.
I usually find the shader examples from http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaderExamples.html as a good starting point for most what I need.
This time I have started fom the “Slices via World Space Position” shader found there.

  Shader "Custom/Isolines" {
    Properties {
      _MainTex ("Texture", 2D) = "white" {}
      _BumpMap ("Bumpmap", 2D) = "bump" {}
      _IsolinesColor ("IsolinesColor", Color) = (1,0,0,1)
      _heightInterval ("_heightInterval",float) = 1
      _lineWidth ("_lineWidth",float) = 0.1
    }
    SubShader {
      Tags { "RenderType" = "Opaque" }
      CGPROGRAM
      	  #pragma surface surf Lambert
          fixed4 _IsolinesColor;
          float _heightInterval;
          float _lineWidth;

      struct Input {
          float3 worldPos;
          float2 uv_MainTex;
          float2 uv_BumpMap;
      };
      sampler2D _MainTex;
      sampler2D _BumpMap;

      void surf (Input IN, inout SurfaceOutput o) {
         float a1 = frac(IN.worldPos.y/_heightInterval) - 1 +_lineWidth/_heightInterval;
         if(a1>0){
             o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb + _IsolinesColor.rgb;
         }else{
             o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
         }
         o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
      }
      ENDCG
    }
    Fallback "Diffuse"
  }

Below is the outcome:
isolines1
Some measures have to be taken to provide the equal line width for all the sphere’s latitudes so our picture looked like here:
isolines2
We just have to take the WorldSpace normal into account. That requires adding the vert function. Below is the final shader’s code and the file download is here:

1671 Downloads

  Shader "Custom/Isolines" {
    Properties {
      _MainTex ("Texture", 2D) = "white" {}
      _BumpMap ("Bumpmap", 2D) = "bump" {}
      _IsolinesColor ("IsolinesColor", Color) = (1,0,0,1)
      _heightInterval ("_heightInterval",float) = 1
      _lineWidth ("_lineWidth",float) = 0.1
      _Up ("_Up (x, y, z)", vector) = (0,1,0)
    }
    SubShader {
      Tags { "RenderType" = "Opaque" }
      CGPROGRAM
      #pragma surface surf Lambert vertex:vert
      	  fixed4 _IsolinesColor;
          float _heightInterval;
      	  float _lineWidth;
      	  fixed3 _Up;

      struct Input {
          float3 worldPos;
          float2 uv_MainTex;
          float2 uv_BumpMap;
          float a11;
      };
      sampler2D _MainTex;
      sampler2D _BumpMap;

      void vert (inout appdata_full v, out Input o) {
          UNITY_INITIALIZE_OUTPUT(Input,o);
          o.a11 = length(cross(v.normal, _Up));
      } 

      void surf (Input IN, inout SurfaceOutput o) {
          float a1 = frac(IN.worldPos.y/_heightInterval) - 1 +_lineWidth* IN.a11/_heightInterval;
          if(a1>0){
          	  o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb + _IsolinesColor.rgb;
          }else{
           	  o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
          }
          o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
      }
      ENDCG
    } 
    Fallback "Diffuse"
  }

1 Comment

Leave a Reply

Copyright © 2011-2024 virtualPlayground All rights reserved.
VP based on Desk Mess Mirrored v1.8.3 theme from BuyNowShop.com.