Skip to content

Instantly share code, notes, and snippets.

@bhouston
Created January 22, 2016 15:56
Show Gist options
  • Save bhouston/12a25896ad16331c1e17 to your computer and use it in GitHub Desktop.
Save bhouston/12a25896ad16331c1e17 to your computer and use it in GitHub Desktop.
"#if MAX_AREA_LIGHTS > 0",
"for( int i = 0; i < MAX_AREA_LIGHTS; i ++ ) {",
"vec3 lPosition = ( viewMatrix * vec4( areaLightPosition[ i ], 1.0 ) ).xyz;",
//"vec3 lVector = lPosition.xyz + vViewPosition.xyz;",
"vec3 width = areaLightWidth[ i ];",
"vec3 height = areaLightHeight[ i ];",
"vec3 up = normalize( ( viewMatrix * vec4( height, 0.0 ) ).xyz );",
"vec3 right = normalize( ( viewMatrix * vec4( width, 0.0 ) ).xyz );",
"vec3 pnormal = normalize( cross( right, up ) );",
"float widthScalar = length( width );",
"float heightScalar = length( height );",
//project onto plane and calculate direction from center to the projection.
"vec3 projection = projectOnPlane( -vViewPosition.xyz, lPosition, pnormal );", // projection in plane
"vec3 dir = projection - lPosition;",
//calculate distance from area:
"vec2 diagonal = vec2( dot( dir, right ), dot( dir, up ) );",
"vec2 nearest2D = vec2( clamp( diagonal.x, -widthScalar, widthScalar ), clamp( diagonal.y, -heightScalar, heightScalar ) );",
"vec3 nearestPointInside = lPosition + ( right *nearest2D.x + up * nearest2D.y );",
"vec3 lVector = ( nearestPointInside + vViewPosition.xyz );",
"float distanceAttenuation = calcLightAttenuation( length( lVector ), areaLightDistance[ i ], areaLightDecayExponent[i] );",
"lVector = normalize( lVector );",
"vec3 incidentLight = areaLightColor[ i ] * distanceAttenuation * 0.01;", // the 0.01 is the area light intensity scaling.
"float nDotLDiffuse = saturate( dot( normal, lVector ) );",
"vec3 diff = Diffuse_Lambert() * diffuseColor * widthScalar * heightScalar;",
"vec3 viewReflection = reflect( viewDirection.xyz, normal );",
"vec3 reflectionLightPlaneIntersection = linePlaneIntersect( -vViewPosition.xyz, viewReflection, lPosition, pnormal );",
"float specAngle = dot( viewReflection, pnormal );",
// && dot( -vViewPosition.xyz - areaLightPosition[ i ], -pnormal ) >= 0.0
"if ( specAngle < 0.0 ) {",
"vec3 dirSpec = reflectionLightPlaneIntersection - lPosition;",
"vec2 dirSpec2D = vec2( dot( dirSpec, right ), dot( dirSpec, up ) );",
"vec2 nearestSpec2D = vec2( clamp( dirSpec2D.x, -widthScalar, widthScalar ), clamp( dirSpec2D.y, -heightScalar, heightScalar ) );",
"lVector = normalize( lPosition + ( right *nearestSpec2D.x + up * nearestSpec2D.y ) + vViewPosition.xyz );",
"} else { ",
"lVector = vec3( 0 );",
"}",
"vec3 hVector = normalize( viewDirection.xyz + lVector.xyz );",
"float nDotH = saturate( dot( normal, hVector ) );",
"float nDotL = saturate( dot( normal, lVector ) );",
"float hDotV = saturate( dot( hVector, viewDirection ) );",
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment