Guest User

Rage - Sample Shader

a guest
Oct 23rd, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.67 KB | None | 0 0
  1.  
  2. //== PROGRAM LINK STATUS = TRUE
  3. //== PROGRAM VALIDATE STATUS = TRUE
  4.  
  5. //======================================================
  6. // Vertex Shader 1
  7. //======================================================
  8.  
  9. //== SHADER COMPILE STATUS = TRUE
  10. #version 150
  11.  
  12. float saturate( float v ) { return clamp( v, 0.0, 1.0 ); }
  13. vec2 saturate( vec2 v ) { return clamp( v, 0.0, 1.0 ); }
  14. vec3 saturate( vec3 v ) { return clamp( v, 0.0, 1.0 ); }
  15. vec4 saturate( vec4 v ) { return clamp( v, 0.0, 1.0 ); }
  16.  
  17. float dot3 ( vec3 a, vec3 b ) { return dot( a, b ); }
  18. float dot3 ( vec3 a, vec4 b ) { return dot( a, b.xyz ); }
  19. float dot3 ( vec4 a, vec3 b ) { return dot( a.xyz, b ); }
  20. float dot3 ( vec4 a, vec4 b ) { return dot( a.xyz, b.xyz ); }
  21. float dot4 ( vec4 a, vec4 b ) { return dot( a, b ); }
  22. float dot4 ( vec2 a, vec4 b ) { return dot( vec4( a, 0, 1 ), b ); }
  23. uniform vec4 _va_ [13];
  24. uniform vec4 skinOffsets ;
  25. uniform vec4 hardwareSkinning ;
  26. uniform matrices_ubo { vec4 matrices[768]; };
  27.  
  28. in vec4 in_Position;
  29. in vec2 in_TexCoord;
  30. in vec4 in_Normal;
  31. in vec4 in_Tangent;
  32. in vec4 in_Color;
  33. in vec4 in_Morph;
  34.  
  35. out vec4 gl_Position;
  36. out vec4 vofi_TexCoord0;
  37. out vec4 vofi_TexCoord1;
  38. out vec4 vofi_TexCoord2;
  39. out vec4 vofi_TexCoord3;
  40. out vec4 vofi_TexCoord4;
  41.  
  42. void main() {
  43. vec4 position = in_Position * _va_[0 ] + _va_[1 ];
  44. vec4 normal = in_Normal * 2.0 - 1.0;
  45. vec4 tangent = in_Tangent * 2.0 - 1.0;
  46. float morphScale = 0.0;
  47. if ( hardwareSkinning.x != 0.0 ) {
  48. vec4 vertexPosition = position;
  49. if ( hardwareSkinning.y != 0.0 ) {
  50. vec3 morphVector = ( in_Morph.xyz * 2 ) - 1;
  51. morphScale = in_Morph.w;
  52. vertexPosition.xyz += ( morphVector * morphScale * 32.0 );
  53. }
  54. float weights = in_Normal.w * 255;
  55. float w1 = floor( weights / 16 );
  56. float w2 = floor( weights / 4 ) - ( w1 * 4 );
  57. float w3 = weights - ( w2 * 4 ) - ( w1 * 16 );
  58. w1 *= 1.0 / 30.0;
  59. w2 *= 1.0 / 9.0;
  60. w3 *= 1.0 / 12.0;
  61. float w0 = 1 - ( w1 + w2 + w3 );
  62. vec4 matX, matY, matZ;
  63. float joint = in_Color.r * 255;
  64. matX = matrices[int(joint*3+0)] * w0;
  65. matY = matrices[int(joint*3+1)] * w0;
  66. matZ = matrices[int(joint*3+2)] * w0;
  67. joint = in_Color.g * 255;
  68. matX += matrices[int(joint*3+0)] * w1;
  69. matY += matrices[int(joint*3+1)] * w1;
  70. matZ += matrices[int(joint*3+2)] * w1;
  71. joint = in_Color.b * 255;
  72. matX += matrices[int(joint*3+0)] * w2;
  73. matY += matrices[int(joint*3+1)] * w2;
  74. matZ += matrices[int(joint*3+2)] * w2;
  75. joint = in_Color.a * 255;
  76. matX += matrices[int(joint*3+0)] * w3;
  77. matY += matrices[int(joint*3+1)] * w3;
  78. matZ += matrices[int(joint*3+2)] * w3;
  79. position.x = dot4( vertexPosition, matX );
  80. position.y = dot4( vertexPosition, matY );
  81. position.z = dot4( vertexPosition, matZ );
  82. position.w = 1.0;
  83. vec4 tempNormal = normal;
  84. normal.x = dot3( tempNormal, matX );
  85. normal.y = dot3( tempNormal, matY );
  86. normal.z = dot3( tempNormal, matZ );
  87. vec4 tempTangent = tangent;
  88. tangent.x = dot3( tempTangent, matX );
  89. tangent.y = dot3( tempTangent, matY );
  90. tangent.z = dot3( tempTangent, matZ );
  91. }
  92. vec3 bitangent = cross( normal.xyz, tangent.xyz ) * tangent.w;
  93. gl_Position.x = dot4( position, _va_[2 ] );
  94. gl_Position.y = dot4( position, _va_[3 ] );
  95. gl_Position.z = dot4( position, _va_[4 ] );
  96. gl_Position.w = dot4( position, _va_[5 ] );
  97. vofi_TexCoord0.xy = in_TexCoord.xy + skinOffsets.xy;
  98. vofi_TexCoord0.zw = in_TexCoord.xy + skinOffsets.zw;
  99. vofi_TexCoord1.x = dot3( tangent, _va_[6 ] );
  100. vofi_TexCoord1.y = dot3( bitangent, _va_[6 ] );
  101. vofi_TexCoord1.z = dot3( normal, _va_[6 ] );
  102. vofi_TexCoord1.w = morphScale;
  103. vofi_TexCoord2.x = dot3( tangent, _va_[7 ] );
  104. vofi_TexCoord2.y = dot3( bitangent, _va_[7 ] );
  105. vofi_TexCoord2.z = dot3( normal, _va_[7 ] );
  106. vofi_TexCoord2.w = 0.0;
  107. vofi_TexCoord3.x = dot3( tangent, _va_[8 ] );
  108. vofi_TexCoord3.y = dot3( bitangent, _va_[8 ] );
  109. vofi_TexCoord3.z = dot3( normal, _va_[8 ] );
  110. vofi_TexCoord3.w = 0.0;
  111. vofi_TexCoord4.x = dot4( position, _va_[6 ] );
  112. vofi_TexCoord4.y = dot4( position, _va_[7 ] );
  113. vofi_TexCoord4.z = dot4( position, _va_[8 ] );
  114. vofi_TexCoord4.xyz -= _va_[9 ].xyz;
  115. vofi_TexCoord4.w = -( clamp( gl_Position.w - _va_[10 ].x, 0.0, _va_[11 ].x ) * _va_[12 ].x );
  116. }
  117. //======================================================
  118. // Fragment Shader 2
  119. //======================================================
  120.  
  121. //== SHADER COMPILE STATUS = TRUE
  122. #version 150
  123.  
  124. void clip( float v ) { if ( v < 0.0 ) { discard; } }
  125. void clip( vec2 v ) { if ( any( lessThan( v, vec2( 0.0 ) ) ) ) { discard; } }
  126. void clip( vec3 v ) { if ( any( lessThan( v, vec3( 0.0 ) ) ) ) { discard; } }
  127. void clip( vec4 v ) { if ( any( lessThan( v, vec4( 0.0 ) ) ) ) { discard; } }
  128.  
  129. float saturate( float v ) { return clamp( v, 0.0, 1.0 ); }
  130. vec2 saturate( vec2 v ) { return clamp( v, 0.0, 1.0 ); }
  131. vec3 saturate( vec3 v ) { return clamp( v, 0.0, 1.0 ); }
  132. vec4 saturate( vec4 v ) { return clamp( v, 0.0, 1.0 ); }
  133.  
  134. vec4 tex2D( sampler2D sampler, vec2 texcoord ) { return texture( sampler, texcoord.xy ); }
  135. vec4 tex2D( sampler2DShadow sampler, vec3 texcoord ) { return vec4( texture( sampler, texcoord.xyz ) ); }
  136.  
  137. vec4 tex2D( sampler2D sampler, vec2 texcoord, vec2 dx, vec2 dy ) { return textureGrad( sampler, texcoord.xy, dx, dy ); }
  138. vec4 tex2D( sampler2DShadow sampler, vec3 texcoord, vec2 dx, vec2 dy ) { return vec4( textureGrad( sampler, texcoord.xyz, dx, dy ) ); }
  139.  
  140. vec4 texCUBE( samplerCube sampler, vec3 texcoord ) { return texture( sampler, texcoord.xyz ); }
  141. vec4 texCUBE( samplerCubeShadow sampler, vec4 texcoord ) { return vec4( texture( sampler, texcoord.xyzw ) ); }
  142.  
  143. vec4 tex1Dproj( sampler1D sampler, vec2 texcoord ) { return textureProj( sampler, texcoord ); }
  144. vec4 tex2Dproj( sampler2D sampler, vec3 texcoord ) { return textureProj( sampler, texcoord ); }
  145. vec4 tex3Dproj( sampler3D sampler, vec4 texcoord ) { return textureProj( sampler, texcoord ); }
  146.  
  147. vec4 tex1Dbias( sampler1D sampler, vec4 texcoord ) { return texture( sampler, texcoord.x, texcoord.w ); }
  148. vec4 tex2Dbias( sampler2D sampler, vec4 texcoord ) { return texture( sampler, texcoord.xy, texcoord.w ); }
  149. vec4 tex3Dbias( sampler3D sampler, vec4 texcoord ) { return texture( sampler, texcoord.xyz, texcoord.w ); }
  150. vec4 texCUBEbias( samplerCube sampler, vec4 texcoord ) { return texture( sampler, texcoord.xyz, texcoord.w ); }
  151.  
  152. vec4 tex1Dlod( sampler1D sampler, vec4 texcoord ) { return textureLod( sampler, texcoord.x, texcoord.w ); }
  153. vec4 tex2Dlod( sampler2D sampler, vec4 texcoord ) { return textureLod( sampler, texcoord.xy, texcoord.w ); }
  154. vec4 tex3Dlod( sampler3D sampler, vec4 texcoord ) { return textureLod( sampler, texcoord.xyz, texcoord.w ); }
  155. vec4 texCUBElod( samplerCube sampler, vec4 texcoord ) { return textureLod( sampler, texcoord.xyz, texcoord.w ); }
  156.  
  157. float dot3 ( vec3 a, vec3 b ) { return dot( a, b ); }
  158. float dot3 ( vec3 a, vec4 b ) { return dot( a, b.xyz ); }
  159. float dot3 ( vec4 a, vec3 b ) { return dot( a.xyz, b ); }
  160. float dot3 ( vec4 a, vec4 b ) { return dot( a.xyz, b.xyz ); }
  161. float dot4 ( vec4 a, vec4 b ) { return dot( a, b ); }
  162. float dot4 ( vec2 a, vec4 b ) { return dot( vec4( a, 0, 1 ), b ); }
  163. vec4 texPhys ( sampler2D image, vec2 texcoord ) { return tex2D( image, texcoord ); }
  164. vec4 texPhys ( sampler2D image, vec2 texcoord, vec2 dx, vec2 dy ) { return tex2D( image, texcoord, dx, dy ); }
  165. vec2 screenPosToTexcoord ( vec2 pos, vec4 bias_scale ) { return ( pos * bias_scale.zw + bias_scale.xy ); }
  166. const vec4 matrixCoCg1YtoRGB1X = vec4( 1.0, -1.0, 0.0, 1.0 );
  167. const vec4 matrixCoCg1YtoRGB1Y = vec4( 0.0, 1.0, -0.50196078, 1.0 );
  168. const vec4 matrixCoCg1YtoRGB1Z = vec4( -1.0, -1.0, 1.00392156, 1.0 );
  169. vec3 VirtualToPhysical2 ( sampler2D virtToPhysPage, sampler2D physPageToXform0, vec2 virtCoords ) {
  170. vec2 physPage = tex2D( virtToPhysPage, virtCoords).xy;
  171. vec4 xform = tex2D( physPageToXform0, physPage );
  172. vec3 physCoord;
  173. physCoord.xy = virtCoords * xform.x + xform.zw;
  174. physCoord.z = xform.y;
  175. return physCoord;
  176. }
  177. vec3 ScaleSpecular ( vec3 unscaled, float factor ) {
  178. return ( unscaled * 8.0 ) / ( factor * 255.0 + 8.0 );
  179. }
  180. uniform vec4 _fa_ [20];
  181. uniform sampler2D samp_pagetablemap;
  182. uniform sampler2D samp_physicalvmtrmappingsmap0;
  183. uniform sampler2D samp_physicalvmtrpagesmap0;
  184. uniform sampler2D samp_physicalvmtrpagesmap1;
  185. uniform sampler2D samp_physicalvmtrpagesmap2;
  186. uniform samplerCube samp_dynamicenvmap;
  187. uniform sampler2D samp_viewcolormap;
  188.  
  189. in vec4 gl_FragCoord;
  190. in vec4 vofi_TexCoord0;
  191. in vec4 vofi_TexCoord1;
  192. in vec4 vofi_TexCoord2;
  193. in vec4 vofi_TexCoord3;
  194. in vec4 vofi_TexCoord4;
  195.  
  196. out vec4 out_FragColor;
  197.  
  198. void main() {
  199. vec2 texcoord = vofi_TexCoord0.xy;
  200. vec2 texcoord2 = vofi_TexCoord0.zw; if ( _fa_[0 ].x > 0 ) {
  201. texcoord = fract( texcoord ) * _fa_[1 ].xy + _fa_[1 ].zw;
  202. texcoord2 = fract( texcoord2 ) * _fa_[1 ].xy + _fa_[1 ].zw;
  203. }
  204. vec3 physCoord = VirtualToPhysical2( samp_pagetablemap, samp_physicalvmtrmappingsmap0, texcoord);
  205. vec4 specularPage = texPhys( samp_physicalvmtrpagesmap0, physCoord.xy );
  206. vec4 YCoCg = texPhys( samp_physicalvmtrpagesmap1, physCoord.xy );
  207. vec4 bumpPage = texPhys( samp_physicalvmtrpagesmap2, physCoord.xy ); if ( _fa_[2 ].x > 0.0 ) {
  208. vec3 physCoord = VirtualToPhysical2( samp_pagetablemap, samp_physicalvmtrmappingsmap0, texcoord2);
  209. vec4 specularPage2 = texPhys( samp_physicalvmtrpagesmap0, physCoord.xy );
  210. vec4 YCoCg2 = texPhys( samp_physicalvmtrpagesmap1, physCoord.xy );
  211. vec4 bumpPage2 = texPhys( samp_physicalvmtrpagesmap2, physCoord.xy );
  212. float morphScale = vofi_TexCoord1.w;
  213. specularPage = mix( specularPage, specularPage2, morphScale );
  214. YCoCg = mix( YCoCg, YCoCg2, morphScale );
  215. bumpPage = mix( bumpPage, bumpPage2, morphScale );
  216. }
  217. vec3 diffuse;
  218. YCoCg.z = ( YCoCg.z * 31.875 ) + 1.0;
  219. YCoCg.z = 1.0 / YCoCg.z;
  220. YCoCg.xy *= YCoCg.z;
  221. diffuse.x = dot4( YCoCg, matrixCoCg1YtoRGB1X );
  222. diffuse.y = dot4( YCoCg, matrixCoCg1YtoRGB1Y );
  223. diffuse.z = dot4( YCoCg, matrixCoCg1YtoRGB1Z );
  224. specularPage.xyz = ScaleSpecular( specularPage.xyz, bumpPage.z );
  225. float powerFactor = bumpPage.x;
  226. vec3 localNormal = bumpPage.wyz - 0.5;
  227. localNormal.z = sqrt( abs( dot( localNormal.xy, localNormal.xy ) - 0.25 ) );
  228. vec3 globalNormal;
  229. globalNormal.x = dot3( localNormal, vofi_TexCoord1.xyz );
  230. globalNormal.y = dot3( localNormal, vofi_TexCoord2.xyz );
  231. globalNormal.z = dot3( localNormal, vofi_TexCoord3.xyz );
  232. globalNormal = normalize( globalNormal );
  233. vec3 fromViewer = normalize( vofi_TexCoord4.xyz );
  234. vec4 reflection;
  235. reflection.xyz = fromViewer - ( globalNormal * dot3( fromViewer, globalNormal ) * 2.0 );
  236. reflection.w = floor( ( 1.0 - powerFactor ) * 4.0 + _fa_[3 ].x );
  237. vec3 envColor = texCUBElod( samp_dynamicenvmap, reflection ).xyz;
  238. vec4 window;
  239. window.xy = screenPosToTexcoord( gl_FragCoord.xy, _fa_[4 ] );
  240. window.z = 0.0;
  241. window.w = 0.0;
  242. float shadow = tex2Dlod( samp_viewcolormap, window ).w;
  243. shadow = saturate( shadow + _fa_[5 ].x );
  244. vec3 primeLight = shadow * _fa_[6 ].xyz *
  245. saturate( dot3( _fa_[7 ], globalNormal ) - 0.2 );
  246. vec3 light = primeLight
  247. + max( globalNormal.x * _fa_[8 ].xyz, 0 )
  248. + max( -globalNormal.x * _fa_[9 ].xyz, 0 )
  249. + max( globalNormal.y * _fa_[10 ].xyz, 0 )
  250. + max( -globalNormal.y * _fa_[11 ].xyz, 0 )
  251. + max( globalNormal.z * _fa_[12 ].xyz, 0 )
  252. + max( -globalNormal.z * _fa_[13 ].xyz, 0 );
  253. float graze = saturate( dot3( -fromViewer, globalNormal ) );
  254. graze = saturate( ( _fa_[14 ].x - max( graze, -graze ) ) / max( _fa_[14 ].x, 0.0001 ) );
  255. diffuse.xyz = mix( diffuse.xyz, _fa_[15 ].xyz, graze );
  256. specularPage.xyz = mix( specularPage.xyz, _fa_[16 ].xyz, graze );
  257. vec3 color = light * ( envColor * specularPage.xyz * _fa_[17 ].xyz )
  258. + light * ( diffuse.xyz * _fa_[18 ].xyz * 2.0 );
  259. float fog = saturate( exp2( vofi_TexCoord4.w ) );
  260. out_FragColor.xyz = mix( _fa_[19 ].xyz, color.xyz, fog );
  261. out_FragColor.w = specularPage.w;
  262. }
Advertisement
Add Comment
Please, Sign In to add comment