Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity Curved World Shader

Discussion in 'Shaders' started by Batman_831, Sep 18, 2015.

  1. Batman_831

    Batman_831

    Joined:
    Oct 17, 2014
    Posts:
    106
    Today, I want to share a shader I created(not originally) to have curved horizon or curved world effect in unity.

    I was looking for curved world shader for my game when I just stumbled on this. It worked perfectly, only thing it was missing was some properties and better shadows and graphics so I decided to integrate or mix it with Unity's default shader and I was successful to add some more properties to it like normal mapping, tiling, smoothness and metallic. I tested it and it works perfectly. So if anyone is looking for any such shader for game, here it is -

    Code (CSharp):
    1. Shader "Custom/CurvedWorld" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _BumpMap ("Normalmap", 2D) = "bump" {}
    6.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    7.         _Metallic ("Metallic", Range(0,1)) = 0.0
    8.         _Curvature ("Curvature", Float) = 0.001
    9.     }
    10.     SubShader {
    11.         Tags { "RenderType"="Opaque" }
    12.         LOD 200
    13.      
    14.         CGPROGRAM
    15.         // Physically based Standard lighting model, and enable shadows on all light types
    16.         #pragma surface surf Standard vertex:vert fullforwardshadows
    17.  
    18.         // Use shader model 3.0 target, to get nicer looking lighting
    19.         #pragma target 3.0
    20.  
    21.         sampler2D _MainTex;
    22.         sampler2D _BumpMap;
    23.         float _Curvature;
    24.  
    25.         struct Input {
    26.             float2 uv_MainTex;
    27.             float2 uv_BumpMap;
    28.         };
    29.  
    30.         half _Glossiness;
    31.         half _Metallic;
    32.         fixed4 _Color;
    33.      
    34.          void vert( inout appdata_full v)
    35.         {        
    36.             float4 vv = mul( _Object2World, v.vertex );
    37.             vv.xyz -= _WorldSpaceCameraPos.xyz;
    38.             vv = float4( 0.0f, ((vv.z * vv.z) + (vv.x * vv.x)) * - _Curvature, 0.0f, 0.0f );
    39.             v.vertex += mul(_World2Object, vv);
    40.         }
    41.  
    42.         void surf (Input IN, inout SurfaceOutputStandard o) {
    43.             // Albedo comes from a texture tinted by color
    44.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    45.             o.Albedo = c.rgb;
    46.             o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
    47.             // Metallic and smoothness come from slider variables
    48.             o.Metallic = _Metallic;
    49.             o.Smoothness = _Glossiness;
    50.             o.Alpha = c.a;
    51.         }
    52.         ENDCG
    53.     }
    54.     FallBack "Diffuse"
    55. }
    or Download CurvedWorld.shader

    Usage - You don't need to attach this to camera. You need to attach this to all your meshes and gameObject which will be rendered.
    Some screenshots -

     
    Last edited: Sep 26, 2015
  2. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
  3. Batman_831

    Batman_831

    Joined:
    Oct 17, 2014
    Posts:
    106
    I know about it, but since it's not free so I decided to create my own for anybody who needs to use this effect for free.
     
    idurvesh likes this.
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,141
    Those images aren't appearing on my end. You may want to check their permissions or throw them up on Imgur.
     
  5. Batman_831

    Batman_831

    Joined:
    Oct 17, 2014
    Posts:
    106
    @Ryiah , Fixed it. I think it was Google+ permissions. Uploaded to imgur.
     
    goat likes this.
  6. tng2903

    tng2903

    Joined:
    Apr 8, 2013
    Posts:
    41
    wow, this shader is so beautiful, thank you a lot for this. The shader realm is still very new to me
     
    Batman_831 likes this.
  7. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    Thanks for sharing this. I don't currently have a need for it, but it seems hella fun to tinker with, and it's always nice to see people share interesting things for free.

    BTW, the shader file download you posted also has permission issues, though one who isn't lazy can just copy from the script you posted.
     
    Batman_831 likes this.
  8. Batman_831

    Batman_831

    Joined:
    Oct 17, 2014
    Posts:
    106
    Oh, thanks for pointing out, I just missed it. I will update the permissions.
     
  9. Weelsix

    Weelsix

    Joined:
    Nov 27, 2015
    Posts:
    8
    Thanks for this man, it's awesome! May I apply that to a normal terrain?
     
  10. Batman_831

    Batman_831

    Joined:
    Oct 17, 2014
    Posts:
    106
    Only shaders or materials specialised for terrain could be used with Terrain system in unity, hence this shader can not be used with the terrain system.
     
  11. RichyK

    RichyK

    Joined:
    Feb 8, 2015
    Posts:
    49
    Hi, I'm developing an infinite racer and I've got bridges going over water, and I'm using the Curved World water shader for that. Because of the technique being used (car stays stationary, scenery moves) the actual water detail itself stays still while the water plane moves. How can I get the shader to operate in real world coordinates? Maybe the shader needs a new option for that! Surely it wouldn't be that difficult?

    Thanks,

    Richard