ShaderUtils.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. var ShaderUtils = {
  2. lib: { 'fresnel': {
  3. uniforms: {
  4. "mRefractionRatio": { type: "f", value: 1.02 },
  5. "mFresnelBias": { type: "f", value: 0.1 },
  6. "mFresnelPower": { type: "f", value: 2.0 },
  7. "mFresnelScale": { type: "f", value: 1.0 },
  8. "tCube": { type: "t", value: 1, texture: null }
  9. },
  10. fragment_shader: [
  11. "uniform samplerCube tCube;",
  12. "varying vec3 vReflect;",
  13. "varying vec3 vRefract[3];",
  14. "varying float vReflectionFactor;",
  15. "void main() {",
  16. "vec4 reflectedColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
  17. "vec4 refractedColor = vec4( 1.0, 1.0, 1.0, 1.0 );",
  18. "refractedColor.r = textureCube( tCube, vec3( -vRefract[0].x, vRefract[0].yz ) ).r;",
  19. "refractedColor.g = textureCube( tCube, vec3( -vRefract[1].x, vRefract[1].yz ) ).g;",
  20. "refractedColor.b = textureCube( tCube, vec3( -vRefract[2].x, vRefract[2].yz ) ).b;",
  21. "refractedColor.a = 1.0;",
  22. "gl_FragColor = mix( refractedColor, reflectedColor, clamp( vReflectionFactor, 0.0, 1.0 ) );",
  23. "}"
  24. ].join("\n"),
  25. vertex_shader: [
  26. "uniform float mRefractionRatio;",
  27. "uniform float mFresnelBias;",
  28. "uniform float mFresnelScale;",
  29. "uniform float mFresnelPower;",
  30. "varying vec3 vReflect;",
  31. "varying vec3 vRefract[3];",
  32. "varying float vReflectionFactor;",
  33. "void main(void) {",
  34. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  35. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  36. "vec3 nWorld = normalize ( mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal );",
  37. "vec3 I = mPosition.xyz - cameraPosition;",
  38. "vReflect = reflect( I, nWorld );",
  39. "vRefract[0] = refract( normalize( I ), nWorld, mRefractionRatio );",
  40. "vRefract[1] = refract( normalize( I ), nWorld, mRefractionRatio * 0.99 );",
  41. "vRefract[2] = refract( normalize( I ), nWorld, mRefractionRatio * 0.98 );",
  42. "vReflectionFactor = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( I ), nWorld ), mFresnelPower );",
  43. "gl_Position = projectionMatrix * mvPosition;",
  44. "}"
  45. ].join("\n")
  46. },
  47. 'normal' : {
  48. uniforms: {
  49. "enableAO": { type: "i", value: 0 },
  50. "enableDiffuse": { type: "i", value: 0 },
  51. "tDiffuse": { type: "t", value: 0, texture: null },
  52. "tNormal": { type: "t", value: 2, texture: null },
  53. "tAO": { type: "t", value: 3, texture: null },
  54. "uNormalScale": { type: "f", value: 1.0 },
  55. "tDisplacement": { type: "t", value: 4, texture: null },
  56. "uDisplacementBias": { type: "f", value: -0.5 },
  57. "uDisplacementScale": { type: "f", value: 2.5 },
  58. "uPointLightPos": { type: "v3", value: new THREE.Vector3() },
  59. "uPointLightColor": { type: "c", value: new THREE.Color( 0xeeeeee ) },
  60. "uDirLightPos": { type: "v3", value: new THREE.Vector3() },
  61. "uDirLightColor": { type: "c", value: new THREE.Color( 0xeeeeee ) },
  62. "uAmbientLightColor": { type: "c", value: new THREE.Color( 0x050505 ) },
  63. "uDiffuseColor": { type: "c", value: new THREE.Color( 0xeeeeee ) },
  64. "uSpecularColor": { type: "c", value: new THREE.Color( 0x111111 ) },
  65. "uAmbientColor": { type: "c", value: new THREE.Color( 0x050505 ) },
  66. "uShininess": { type: "f", value: 30 }
  67. },
  68. fragment_shader: [
  69. "uniform vec3 uDirLightPos;",
  70. "uniform vec3 uAmbientLightColor;",
  71. "uniform vec3 uDirLightColor;",
  72. "uniform vec3 uPointLightColor;",
  73. "uniform vec3 uAmbientColor;",
  74. "uniform vec3 uDiffuseColor;",
  75. "uniform vec3 uSpecularColor;",
  76. "uniform float uShininess;",
  77. "uniform bool enableDiffuse;",
  78. "uniform bool enableAO;",
  79. "uniform sampler2D tDiffuse;",
  80. "uniform sampler2D tNormal;",
  81. "uniform sampler2D tAO;",
  82. "uniform float uNormalScale;",
  83. "varying vec3 vTangent;",
  84. "varying vec3 vBinormal;",
  85. "varying vec3 vNormal;",
  86. "varying vec2 vUv;",
  87. "varying vec3 vPointLightVector;",
  88. "varying vec3 vViewPosition;",
  89. "void main() {",
  90. "vec3 diffuseTex = vec3( 1.0, 1.0, 1.0 );",
  91. "vec3 aoTex = vec3( 1.0, 1.0, 1.0 );",
  92. "vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
  93. "normalTex.xy *= uNormalScale;",
  94. "normalTex = normalize( normalTex );",
  95. "if( enableDiffuse )",
  96. "diffuseTex = texture2D( tDiffuse, vUv ).xyz;",
  97. "if( enableAO )",
  98. "aoTex = texture2D( tAO, vUv ).xyz;",
  99. "mat3 tsb = mat3( vTangent, vBinormal, vNormal );",
  100. "vec3 finalNormal = tsb * normalTex;",
  101. "vec3 normal = normalize( finalNormal );",
  102. "vec3 viewPosition = normalize( vViewPosition );",
  103. // point light
  104. "vec4 pointDiffuse = vec4( 0.0, 0.0, 0.0, 0.0 );",
  105. "vec4 pointSpecular = vec4( 0.0, 0.0, 0.0, 0.0 );",
  106. "vec3 pointVector = normalize( vPointLightVector );",
  107. "vec3 pointHalfVector = normalize( vPointLightVector + vViewPosition );",
  108. "float pointDotNormalHalf = dot( normal, pointHalfVector );",
  109. "float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
  110. "float pointSpecularWeight = 0.0;",
  111. "if ( pointDotNormalHalf >= 0.0 )",
  112. "pointSpecularWeight = pow( pointDotNormalHalf, uShininess );",
  113. "pointDiffuse += vec4( uDiffuseColor, 1.0 ) * pointDiffuseWeight;",
  114. "pointSpecular += vec4( uSpecularColor, 1.0 ) * pointSpecularWeight;",
  115. // directional light
  116. "vec4 dirDiffuse = vec4( 0.0, 0.0, 0.0, 0.0 );",
  117. "vec4 dirSpecular = vec4( 0.0, 0.0, 0.0, 0.0 );",
  118. "vec4 lDirection = viewMatrix * vec4( uDirLightPos, 0.0 );",
  119. "vec3 dirVector = normalize( lDirection.xyz );",
  120. "vec3 dirHalfVector = normalize( lDirection.xyz + vViewPosition );",
  121. "float dirDotNormalHalf = dot( normal, dirHalfVector );",
  122. "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  123. "float dirSpecularWeight = 0.0;",
  124. "if ( dirDotNormalHalf >= 0.0 )",
  125. "dirSpecularWeight = pow( dirDotNormalHalf, uShininess );",
  126. "dirDiffuse += vec4( uDiffuseColor, 1.0 ) * dirDiffuseWeight;",
  127. "dirSpecular += vec4( uSpecularColor, 1.0 ) * dirSpecularWeight;",
  128. // all lights contribution summation
  129. "vec4 totalLight = vec4( uAmbientLightColor * uAmbientColor, 1.0 );",
  130. "totalLight += vec4( uDirLightColor, 1.0 ) * ( dirDiffuse + dirSpecular );",
  131. "totalLight += vec4( uPointLightColor, 1.0 ) * ( pointDiffuse + pointSpecular );",
  132. "gl_FragColor = vec4( totalLight.xyz * aoTex * diffuseTex, 1.0 );",
  133. "}"
  134. ].join("\n"),
  135. vertex_shader: [
  136. "attribute vec4 tangent;",
  137. "uniform vec3 uPointLightPos;",
  138. "#ifdef VERTEX_TEXTURES",
  139. "uniform sampler2D tDisplacement;",
  140. "uniform float uDisplacementScale;",
  141. "uniform float uDisplacementBias;",
  142. "#endif",
  143. "varying vec3 vTangent;",
  144. "varying vec3 vBinormal;",
  145. "varying vec3 vNormal;",
  146. "varying vec2 vUv;",
  147. "varying vec3 vPointLightVector;",
  148. "varying vec3 vViewPosition;",
  149. "void main() {",
  150. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  151. "vViewPosition = cameraPosition - mPosition.xyz;",
  152. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  153. "vNormal = normalize( normalMatrix * normal );",
  154. // tangent and binormal vectors
  155. "vTangent = normalize( normalMatrix * tangent.xyz );",
  156. "vBinormal = cross( vNormal, vTangent ) * tangent.w;",
  157. "vBinormal = normalize( vBinormal );",
  158. "vUv = uv;",
  159. // point light
  160. "vec4 lPosition = viewMatrix * vec4( uPointLightPos, 1.0 );",
  161. "vPointLightVector = normalize( lPosition.xyz - mvPosition.xyz );",
  162. // displacement mapping
  163. "#ifdef VERTEX_TEXTURES",
  164. "vec3 dv = texture2D( tDisplacement, uv ).xyz;",
  165. "float df = uDisplacementScale * dv.x + uDisplacementBias;",
  166. "vec4 displacedPosition = vec4( vNormal.xyz * df, 0.0 ) + mvPosition;",
  167. "gl_Position = projectionMatrix * displacedPosition;",
  168. "#else",
  169. "gl_Position = projectionMatrix * mvPosition;",
  170. "#endif",
  171. "}"
  172. ].join("\n")
  173. },
  174. 'cube': {
  175. uniforms: { "tCube": { type: "t", value: 1, texture: null } },
  176. vertex_shader: [
  177. "varying vec3 vViewPosition;",
  178. "void main() {",
  179. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  180. "vViewPosition = cameraPosition - mPosition.xyz;",
  181. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  182. "}"
  183. ].join("\n"),
  184. fragment_shader: [
  185. "uniform samplerCube tCube;",
  186. "varying vec3 vViewPosition;",
  187. "void main() {",
  188. "vec3 wPos = cameraPosition - vViewPosition;",
  189. "gl_FragColor = textureCube( tCube, vec3( - wPos.x, wPos.yz ) );",
  190. "}"
  191. ].join("\n")
  192. },
  193. 'basic': {
  194. uniforms: {},
  195. vertex_shader: [
  196. "void main() {",
  197. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  198. "}"
  199. ].join("\n"),
  200. fragment_shader: [
  201. "void main() {",
  202. "gl_FragColor = vec4(1.0, 0.0, 0.0, 0.5);",
  203. "}"
  204. ].join("\n")
  205. }
  206. }
  207. };