Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Shared functions for all shaders in the engine. Contents of this
- // file will be *automatically* included in all shaders!
- // Projects world space position (typical use case) by given matrix.
- vec3 S_Project(vec3 worldPosition, mat4 matrix)
- {
- vec4 screenPos = matrix * vec4(worldPosition, 1);
- screenPos.xyz /= screenPos.w;
- return screenPos.xyz * 0.5 + 0.5;
- }
- // Returns matrix-space position from given screen position.
- // Real space of returned value is defined by matrix and can
- // be any, but there are few common use cases:
- // - To get position in view space pass inverse projection matrix.
- // - To get position in world space pass inverse view-projection matrix.
- vec3 S_UnProject(vec3 screenPos, mat4 matrix)
- {
- vec4 clipSpacePos = vec4(screenPos * 2.0 - 1.0, 1.0);
- vec4 position = matrix * clipSpacePos;
- return position.xyz / position.w;
- }
- struct TBlinnPhongContext {
- vec3 lightPosition;
- float lightRadius;
- vec3 fragmentNormal;
- vec3 fragmentPosition;
- vec3 cameraPosition;
- float specularPower;
- };
- struct TBlinnPhong {
- // Total "brightness" of fragment.
- float attenuation;
- // Specular component of lighting.
- float specular;
- // Distance from light to fragment.
- float distance;
- // Normalized vector from fragment position to light.
- vec3 direction;
- };
- TBlinnPhong S_BlinnPhong(TBlinnPhongContext ctx)
- {
- vec3 lightVector = ctx.lightPosition - ctx.fragmentPosition;
- float distance = length(lightVector);
- float clampedDistance = min(distance, ctx.lightRadius);
- vec3 normLightVector = lightVector / clampedDistance;
- vec3 h = normalize(lightVector + (ctx.cameraPosition - ctx.fragmentPosition));
- float specular = pow(clamp(dot(ctx.fragmentNormal, h), 0.0, 1.0), ctx.specularPower);
- float lambertian = max(dot(ctx.fragmentNormal, normLightVector), 0);
- float distance_attenuation = 1.0 + cos((clampedDistance / ctx.lightRadius) * 3.14159);
- float attenuation = lambertian * distance_attenuation;
- return TBlinnPhong(attenuation, specular, distance, normLightVector);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement