Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by ohlin · Feb 26, 2015 at 02:24 AM · shaderterraincustom

Adding BRDF lighting to custom Terrain Shader.

So I have this modified terrain shader that has toon outlines, amongst some other changes. But how would I add BRDF to this?

I tried to add it. The error it gives me is: "Too many texture interpolators would be used for ForwardBass pass."

What am I doing wrong, I am new to working with shaders. Or does some one have an example of a Terrain shader with brdf?

Please point me in the right direction with some feedback. Your help would be greatly appreciated.

here is the code:

  Shader "Hidden/TerrainEngine/Splatmap/Lightmap-FirstPass" {
     Properties {
         _Control ("Control (RGBA)", 2D) = "red" {}
         _Splat3 ("Layer 3 (A)", 2D) = "white" {}
         _Splat2 ("Layer 2 (B)", 2D) = "white" {}
         _Splat1 ("Layer 1 (G)", 2D) = "white" {}
         _Splat0 ("Layer 0 (R)", 2D) = "white" {}
         // used in fallback on old cards
         _MainTex ("BaseMap (RGB)", 2D) = "white" {}
         
         _Detail ("Texture", 2D) = "white" {}
         _Blend ( "Blend", Range ( 0, 1 ) ) = 0.001
         
         _OutlineColor ("Outline Color", Color) = (0,0,0,1)
         _Outline ("Outline width", Range (.002, 0.03)) = .001
         _Color ("Main Color", Color) = (1,1,1,1)
         _ToonShade ("ToonShader Cubemap(RGB)", CUBE) = "" { Texgen CubeNormal }
         
         _Lookup ("BRDF Color Ramp", 2D) = "gray" {}
     }
     
     CGINCLUDE
         #include "UnityCG.cginc"
         struct appdata {
             float4 vertex : POSITION;
             float3 normal : NORMAL;
         };
         struct v2f {
             float4 pos : POSITION;
             float4 color : COLOR;
         };
         uniform float _Outline;
         uniform float4 _OutlineColor;
         v2f vert(appdata v) {
             v2f o;
             o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
             float3 norm = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
             float2 offset = TransformViewToProjection(norm.xy);
             o.pos.xy += offset * o.pos.z * _Outline;
             o.color = _OutlineColor;
             return o;
         }
     ENDCG
 
     SubShader {
     Tags {
         "SplatCount" = "4"
         "Queue" = "Geometry-100"
         "RenderType" = "Opaque"
     }
     Pass {
         Name "OUTLINE"
         Tags { "LightMode" = "Always" }
         Cull Front
         ZWrite On
         ColorMask RGB
         Blend SrcAlpha OneMinusSrcAlpha
         CGPROGRAM
                 #pragma vertex vert
                #pragma fragment frag
               half4 frag(v2f i) :COLOR { return i.color; }
           ENDCG
     }
     
     
     CGPROGRAM
     #pragma surface surf Ramp
     
        float _Blend;
        sampler2D _Detail;
        
         sampler2D _Ramp2D;  //<======= and this
        
     struct Input {
         float2 uv_Control : TEXCOORD0;
         float2 uv_Splat0 : TEXCOORD1;
         float2 uv_Splat1 : TEXCOORD2;
         float2 uv_Splat2 : TEXCOORD3;
         float2 uv_Splat3 : TEXCOORD4;
         float4 screenPos;
     };
     //this is what i added to try brdf===============================================
     half4 LightingRamp (SurfaceOutput s, half3 lightDir,half3 viewDir, half atten){
         
             float NdotL = dot(s.Normal, -lightDir);
             float NdotE = dot(s.Normal, viewDir);
             
             //do diffuse wrap here
             float diff = (NdotL * 0.3) +0.5;
             float2 brdfUV = float2(NdotE*0.8,diff);
             float3 BRDF = tex2D(_Ramp2D, brdfUV.xy).rgb;
             
             float4 c;
             c.rgb =  BRDF; 
             //float3(diff,diff,diff);
             c.a = s.Alpha;
             return c;
         
         }
     //=======================================================================
     
     
     sampler2D _Control;
     sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
     void surf (Input IN, inout SurfaceOutput o) {
     fixed4 splat_control = tex2D (_Control, IN.uv_Control);
     fixed3 col;
     col = splat_control.r * tex2D (_Splat0, IN.uv_Splat0).rgb;
     col += splat_control.g * tex2D (_Splat1, IN.uv_Splat1).rgb;
     col += splat_control.b * tex2D (_Splat2, IN.uv_Splat2).rgb;
     col += splat_control.a * tex2D (_Splat3, IN.uv_Splat3).rgb;
     //o.Albedo = col;
     
     float2 screenUV = IN.screenPos.xy / IN.screenPos.w;
                   screenUV *= float2(1,1);
     fixed4 t2 = tex2D( _Detail, screenUV)*2;
     //o.Albedo = lerp( col, t2, _Blend);
     o.Albedo = lerp( col, t2, .1);
     
     o.Alpha = 0.0;
     }
     ENDCG
     }
     SubShader {
     Tags { "RenderType"="Opaque" }
     UsePass "Toon/Basic/BASE"
     Pass {
     Name "OUTLINE"
     Tags { "LightMode" = "Always" }
     Cull Front
     ZWrite On
     ColorMask RGB
     Blend SrcAlpha OneMinusSrcAlpha
     CGPROGRAM
     // Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it does not contain a surface program or both vertex and fragment programs.
     #pragma exclude_renderers gles
     #pragma vertex vert
     #pragma exclude_renderers shaderonly
     ENDCG
     SetTexture [_MainTex] { combine primary }
     }
     }
     // Fallback to Diffuse
     Fallback "Diffuse"
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Cannot paint Texture onto Terrain using Custom Material 2018.4 1 Answer

Custom Terrain Shader not taking Textures 0 Answers

How can I add Normal mapping to a Terrain custom shader? 0 Answers

Shader for ZTest Between Terrain and Objects 0 Answers

Forward Rendering for Terrain, overriding the shader? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges