Orihaus

UE4 - PBR WebGL v0.5

Feb 8th, 2014
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.01 KB | None | 0 0
  1.   <script id="vertexShader" type="x-shader/x-vertex">
  2.             attribute vec4 tangent;
  3.             uniform vec3 ambientLightColor;
  4.  
  5.             #if MAX_POINT_LIGHTS > 0
  6.                 uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];
  7.                 varying vec4 vPointLight[ MAX_POINT_LIGHTS ];
  8.             #endif
  9.  
  10.             varying vec3 vNormal;
  11.             varying vec3 vViewPosition;
  12.  
  13.             void main()
  14.             {
  15.                 vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  16.  
  17.                 vNormal = normalize( normalMatrix * normal );
  18.                
  19.                 vec3 warp = vec3(
  20.                     sin( vNormal.x / 0.5 ),
  21.                     sin( vNormal.y * 0.25 ),
  22.                     sin( vNormal.z * 0.7 )
  23.                 );
  24.                
  25.                 float dist = distance( mvPosition.xyz, mvPosition.xyz + warp * warp );
  26.                 mvPosition.xyz = mix( mvPosition.xyz + warp * -120.0, mvPosition.xyz, dist );
  27.  
  28.                 vViewPosition = -mvPosition.xyz;
  29.                 gl_Position = projectionMatrix * mvPosition;
  30.  
  31.                 #ifdef USE_SHADOWMAP
  32.                     for( int i = 0; i < MAX_SHADOWS; i ++ )
  33.                     {
  34.                         vShadowCoord[ i ] = shadowMatrix[ i ] * ( modelMatrix * vec4( position, 1.0 ) );
  35.                     }
  36.                 #endif
  37.             }
  38.         </script>
  39.  
  40.         <script id="fragmentShader" type="x-shader/x-fragment">
  41.             #extension GL_OES_standard_derivatives : enable
  42.  
  43.             uniform vec2 resolution;
  44.             uniform float time;
  45.             uniform vec3 ambientLightColor;
  46.             uniform float roughnessScale;
  47.             uniform float diffuseScale;
  48.  
  49.             #if MAX_DIR_LIGHTS > 0
  50.                 uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];
  51.                 uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];
  52.             #endif
  53.  
  54.             #if MAX_POINT_LIGHTS > 0
  55.                 uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];
  56.                 uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];
  57.                 varying vec4 vPointLight[ MAX_POINT_LIGHTS ];
  58.             #endif
  59.  
  60.             varying vec3 vNormal;
  61.  
  62.             varying vec3 vViewPosition;
  63.  
  64.             #define saturate( value ) clamp( value, 0.0, 1.0 )
  65.             #define lerp( x, y, a ) mix( x, y, a )
  66.  
  67.             float CalculateGGX( float alpha, float cosThetaM )
  68.             {
  69.                 float roughness = alpha * alpha;
  70.                 float CosSquared = cosThetaM * cosThetaM;
  71.                 float GGX = ( CosSquared * ( roughness - 1.0 ) ) + 1.0;
  72.                 GGX = 0.31831 * GGX * GGX;
  73.                 return roughness / GGX;
  74.             }
  75.  
  76.             float CalculateGeometric( float n_dot_x, float roughness )
  77.             {
  78.                 return n_dot_x / ( n_dot_x * ( 1.0 - roughness ) + roughness );
  79.             }
  80.  
  81.             float CalculateGeometricAtten( float n_dot_l, float n_dot_v, float roughness )
  82.             {
  83.                 float roughnessRemap = roughness + 1.0;
  84.                 roughnessRemap *= roughnessRemap;
  85.                 roughnessRemap *= 0.125;
  86.  
  87.                 float atten = CalculateGeometric( n_dot_v, roughnessRemap ) * CalculateGeometric( n_dot_l, roughnessRemap );
  88.                 return atten;
  89.             }
  90.  
  91.             float CalculatePBRLighting( vec3 normal, float n_dot_v, vec3 lightDirection, vec3 viewPosition, float roughness, float diffuse )
  92.             {
  93.                 float n_dot_l = saturate( dot(lightDirection, normal ) );
  94.                 if( n_dot_l <= 0.0 )
  95.                     return 0.0;
  96.  
  97.                 vec3 halfVector = normalize( lightDirection + viewPosition );
  98.  
  99.                 float h_dot_n = saturate( dot(    halfVector, normal ) );
  100.                 float h_dot_v = max( dot(    halfVector, viewPosition ), 0.0001 );
  101.  
  102.                 float SRNI = 0.05;
  103.                 float fresnel = pow( 1.0 - h_dot_v, 5.0 );
  104.                 fresnel = SRNI + ( 1.0 - SRNI ) * fresnel;
  105.  
  106.                 float distribution = CalculateGGX( roughness, h_dot_n );
  107.                 float geometricAttenuation = CalculateGeometricAtten( n_dot_l, h_dot_v, roughness );
  108.                 float specFinal = fresnel * distribution * geometricAttenuation * saturate( 1.0 / ( 4.0 * n_dot_l * n_dot_v ) ) ;
  109.                 return diffuse * n_dot_l * 0.3183 + specFinal;
  110.             }
  111.  
  112.             void main()
  113.             {
  114.                 vec3 normal = normalize( vNormal );
  115.                 vec3 viewPosition = normalize( vViewPosition );
  116.  
  117.                 float n_dot_v = max( dot( normal, viewPosition ), 0.0001 );
  118.  
  119.                 float roughness = 0.6;
  120.                 float diffuse = 1.0;
  121.  
  122.                 // PBR Lighting
  123.                 vec3 lightAccumulation = vec3( finalFresnel );
  124.                 #if MAX_DIR_LIGHTS > 0
  125.                     for( int i = 0; i < MAX_DIR_LIGHTS; i++ )
  126.                     {
  127.                         vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );
  128.                         vec3 dirVector = normalize( lDirection.xyz );
  129.                         lightAccumulation += directionalLightColor[i].xyz * CalculatePBRLighting( normal, n_dot_v, dirVector, viewPosition, roughness, diffuse ) * shadowTerm;
  130.                     }
  131.                 #endif
  132.  
  133.                 #if MAX_POINT_LIGHTS > 0
  134.                     for( int i = 0; i < MAX_POINT_LIGHTS; i ++ )
  135.                     {
  136.  
  137.                         vec3 pointVector = ( viewMatrix * vec4( pointLightPosition[ i ], 1.0 ) ).xyz + vViewPosition.xyz;
  138.                         vec3 pointDirection = normalize( pointVector );
  139.                         float pointDistance = length( pointVector ) * 0.001;
  140.  
  141.                         float falloff = saturate( 1.0 - pow( pointDistance / 0.875, 4.0 ) );
  142.                         falloff *= falloff;
  143.                         falloff /= ( pointDistance * pointDistance ) + 1.0;
  144.  
  145.                         lightAccumulation += pointLightColor[i].xyz * CalculatePBRLighting( normal, n_dot_v, pointDirection, viewPosition, roughness, diffuse ) * falloff;
  146.                     }
  147.                 #endif
  148.                
  149.                 gl_FragColor = vec4( lightAccumulation, 1.0 );
  150.             }
  151.         </script>
Advertisement
Add Comment
Please, Sign In to add comment