- Home /
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"
}
Your answer
Follow this Question
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