#define PHONG uniform vec3 diffuse; uniform vec3 emissive; uniform vec3 specular; uniform float shininess; uniform float opacity; #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // #include uniform sampler2D grassTexture; uniform vec3 grassLODColour; uniform float time; uniform mat3 normalMatrix; varying vec3 vGrassColour; varying vec4 vGrassParams; varying vec3 vNormal2; varying vec3 vWorldPosition; varying vec3 vViewPosition; struct BlinnPhongMaterial { vec3 diffuseColor; vec3 specularColor; float specularShininess; float specularStrength; }; void RE_Direct_BlinnPhong( const in IncidentLight directLight, const in GeometricContext geometry, const in BlinnPhongMaterial material, inout ReflectedLight reflectedLight ) { float wrap = 0.5; float dotNL = saturate( (dot( geometry.normal, directLight.direction ) + wrap) / (1.0 + wrap) ); vec3 irradiance = dotNL * directLight.color; reflectedLight.directDiffuse += irradiance * BRDF_Lambert( material.diffuseColor ); reflectedLight.directSpecular += irradiance * BRDF_BlinnPhong( directLight.direction, geometry.viewDir, geometry.normal, material.specularColor, material.specularShininess ) * material.specularStrength; // Backscatter fakery wrap = 0.5; float backLight = saturate((dot(geometry.viewDir, -directLight.direction) + wrap) / (1.0 + wrap)); float falloff = 0.5;//mix(0.5, pow(1.0 - saturate(dot(geometry.viewDir, geometry.normal)), 2.0), 1.0) * 0.5; vec3 scatter = directLight.color * pow(backLight, 1.0) * falloff * BRDF_Lambert(material.diffuseColor); reflectedLight.indirectDiffuse += scatter * (1.0 - vGrassParams.z); } void RE_IndirectDiffuse_BlinnPhong( const in vec3 irradiance, const in GeometricContext geometry, const in BlinnPhongMaterial material, inout ReflectedLight reflectedLight ) { reflectedLight.indirectDiffuse += irradiance * BRDF_Lambert( material.diffuseColor ); } #define RE_Direct RE_Direct_BlinnPhong #define RE_IndirectDiffuse RE_IndirectDiffuse_BlinnPhong #include #include #include #include #include #include void main() { vec3 viewDir = normalize(cameraPosition - vWorldPosition); #include vec4 diffuseColor = vec4( diffuse, opacity ); // Grass float heightPercent = vGrassParams.x; float height = vGrassParams.y; float lodFadeIn = vGrassParams.z; float lodFadeOut = 1.0 - lodFadeIn; float grassMiddle = mix( smoothstep(abs(vGrassParams.w - 0.5), 0.0, 0.1), 1.0, lodFadeIn); float isSandy = saturate(linearstep(-11.0, -14.0, height)); float density = 1.0 - isSandy; // Density is in the range [0, 1] // 0 being no grass // 1 being full grass float aoForDensity = mix(1.0, 0.25, density); float ao = mix(aoForDensity, 1.0, easeIn(heightPercent, 2.0)); diffuseColor.rgb *= vGrassColour; diffuseColor.rgb *= mix(0.85, 1.0, grassMiddle); diffuseColor.rgb *= ao; ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) ); vec3 totalEmissiveRadiance = emissive; #include #include #include #include #include #include #include #include #include vec3 normal2 = normalize(vNormal2); normal = normalize(mix(vNormal, normal2, vGrassParams.w)); #include // #include BlinnPhongMaterial material; material.diffuseColor = diffuseColor.rgb; material.specularColor = specular; #include #include #include #include vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance; #include #include #include #include // #include gl_FragColor.xyz = CalculateFog(gl_FragColor.xyz, viewDir, vFogDepth); #include #include }