1 /** 2 * Geometry pass shader for standard meshes 3 */ 4 module dash.graphics.shaders.glsl.geometry; 5 import dash.graphics.shaders.glsl; 6 7 package: 8 9 /// Standard mesh vertex shader, transforms position to screen space and normals/tangents to view space 10 immutable string geometryVS = glslVersion ~ q{ 11 layout(location = 0) in vec3 vPosition_m; 12 layout(location = 1) in vec2 vUV; 13 layout(location = 2) in vec3 vNormal_m; 14 layout(location = 3) in vec3 vTangent_m; 15 16 out vec4 fPosition_s; 17 out vec3 fNormal_v; 18 out vec2 fUV; 19 out vec3 fTangent_v; 20 flat out uint fObjectId; 21 22 uniform mat4 worldView; 23 uniform mat4 worldViewProj; 24 uniform uint objectId; 25 26 void main( void ) 27 { 28 // gl_Position is like SV_Position 29 fPosition_s = worldViewProj * vec4( vPosition_m, 1.0f ); 30 gl_Position = fPosition_s; 31 fUV = vUV; 32 33 fNormal_v = ( worldView * vec4( vNormal_m, 0.0f ) ).xyz; 34 fTangent_v = ( worldView * vec4( vTangent_m, 0.0f ) ).xyz; 35 fObjectId = objectId; 36 } 37 }; 38 39 /// Saves diffuse, specular, mappedNormals (encoded to spheremapped XY), and object ID to appropriate FBO textures 40 immutable string geometryFS = glslVersion ~ q{ 41 in vec4 fPosition_s; 42 in vec3 fNormal_v; 43 in vec2 fUV; 44 in vec3 fTangent_v; 45 flat in uint fObjectId; 46 47 layout( location = 0 ) out vec4 color; 48 layout( location = 1 ) out vec4 normal_v; 49 50 uniform sampler2D diffuseTexture; 51 uniform sampler2D normalTexture; 52 uniform sampler2D specularTexture; 53 54 vec2 encode( vec3 normal ) 55 { 56 float t = sqrt( 2 / ( 1 - normal.z ) ); 57 return normal.xy * t; 58 } 59 60 vec3 calculateMappedNormal() 61 { 62 vec3 normal = normalize( fNormal_v ); 63 vec3 tangent = normalize( fTangent_v ); 64 //Use Gramm-Schmidt process to orthogonalize the two 65 tangent = normalize( tangent - dot( tangent, normal ) * normal ); 66 vec3 bitangent = -cross( tangent, normal ); 67 vec3 normalMap = ((texture( normalTexture, fUV ).xyz) * 2) - 1; 68 mat3 TBN = mat3( tangent, bitangent, normal ); 69 return normalize( TBN * normalMap ); 70 } 71 72 void main( void ) 73 { 74 color = texture( diffuseTexture, fUV ); 75 // specular intensity 76 vec3 specularSample = texture( specularTexture, fUV ).xyz; 77 color.w = ( specularSample.x + specularSample.y + specularSample.z ) / 3; 78 normal_v = vec4( calculateMappedNormal(), float(fObjectId) ); 79 } 80 }; 81