ShaderUtils.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author mrdoob / http://mrdoob.com/
  4. *
  5. * ShaderUtils currently contains:
  6. *
  7. * fresnel
  8. * normal
  9. * cube
  10. *
  11. */
  12. THREE.ShaderUtils = {
  13. lib: {
  14. /* -------------------------------------------------------------------------
  15. // Fresnel shader
  16. // - based on Nvidia Cg tutorial
  17. ------------------------------------------------------------------------- */
  18. 'fresnel': {
  19. uniforms: {
  20. "mRefractionRatio": { type: "f", value: 1.02 },
  21. "mFresnelBias": { type: "f", value: 0.1 },
  22. "mFresnelPower": { type: "f", value: 2.0 },
  23. "mFresnelScale": { type: "f", value: 1.0 },
  24. "tCube": { type: "t", value: null }
  25. },
  26. fragmentShader: [
  27. "uniform samplerCube tCube;",
  28. "varying vec3 vReflect;",
  29. "varying vec3 vRefract[3];",
  30. "varying float vReflectionFactor;",
  31. "void main() {",
  32. "vec4 reflectedColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
  33. "vec4 refractedColor = vec4( 1.0 );",
  34. "refractedColor.r = textureCube( tCube, vec3( -vRefract[0].x, vRefract[0].yz ) ).r;",
  35. "refractedColor.g = textureCube( tCube, vec3( -vRefract[1].x, vRefract[1].yz ) ).g;",
  36. "refractedColor.b = textureCube( tCube, vec3( -vRefract[2].x, vRefract[2].yz ) ).b;",
  37. "gl_FragColor = mix( refractedColor, reflectedColor, clamp( vReflectionFactor, 0.0, 1.0 ) );",
  38. "}"
  39. ].join("\n"),
  40. vertexShader: [
  41. "uniform float mRefractionRatio;",
  42. "uniform float mFresnelBias;",
  43. "uniform float mFresnelScale;",
  44. "uniform float mFresnelPower;",
  45. "varying vec3 vReflect;",
  46. "varying vec3 vRefract[3];",
  47. "varying float vReflectionFactor;",
  48. "void main() {",
  49. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  50. "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  51. "vec3 worldNormal = normalize( mat3( modelMatrix[0].xyz, modelMatrix[1].xyz, modelMatrix[2].xyz ) * normal );",
  52. "vec3 I = worldPosition.xyz - cameraPosition;",
  53. "vReflect = reflect( I, worldNormal );",
  54. "vRefract[0] = refract( normalize( I ), worldNormal, mRefractionRatio );",
  55. "vRefract[1] = refract( normalize( I ), worldNormal, mRefractionRatio * 0.99 );",
  56. "vRefract[2] = refract( normalize( I ), worldNormal, mRefractionRatio * 0.98 );",
  57. "vReflectionFactor = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( I ), worldNormal ), mFresnelPower );",
  58. "gl_Position = projectionMatrix * mvPosition;",
  59. "}"
  60. ].join("\n")
  61. },
  62. /* -------------------------------------------------------------------------
  63. // Normal map shader
  64. // - Blinn-Phong
  65. // - normal + diffuse + specular + AO + displacement + reflection + shadow maps
  66. // - point and directional lights (use with "lights: true" material option)
  67. ------------------------------------------------------------------------- */
  68. 'normal' : {
  69. uniforms: THREE.UniformsUtils.merge( [
  70. THREE.UniformsLib[ "fog" ],
  71. THREE.UniformsLib[ "lights" ],
  72. THREE.UniformsLib[ "shadowmap" ],
  73. {
  74. "enableAO" : { type: "i", value: 0 },
  75. "enableDiffuse" : { type: "i", value: 0 },
  76. "enableSpecular" : { type: "i", value: 0 },
  77. "enableReflection": { type: "i", value: 0 },
  78. "enableDisplacement": { type: "i", value: 0 },
  79. "tDisplacement": { type: "t", value: null }, // must go first as this is vertex texture
  80. "tDiffuse" : { type: "t", value: null },
  81. "tCube" : { type: "t", value: null },
  82. "tNormal" : { type: "t", value: null },
  83. "tSpecular" : { type: "t", value: null },
  84. "tAO" : { type: "t", value: null },
  85. "uNormalScale": { type: "v2", value: new THREE.Vector2( 1, 1 ) },
  86. "uDisplacementBias": { type: "f", value: 0.0 },
  87. "uDisplacementScale": { type: "f", value: 1.0 },
  88. "uDiffuseColor": { type: "c", value: new THREE.Color( 0xffffff ) },
  89. "uSpecularColor": { type: "c", value: new THREE.Color( 0x111111 ) },
  90. "uAmbientColor": { type: "c", value: new THREE.Color( 0xffffff ) },
  91. "uShininess": { type: "f", value: 30 },
  92. "uOpacity": { type: "f", value: 1 },
  93. "useRefract": { type: "i", value: 0 },
  94. "uRefractionRatio": { type: "f", value: 0.98 },
  95. "uReflectivity": { type: "f", value: 0.5 },
  96. "uOffset" : { type: "v2", value: new THREE.Vector2( 0, 0 ) },
  97. "uRepeat" : { type: "v2", value: new THREE.Vector2( 1, 1 ) },
  98. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  99. }
  100. ] ),
  101. fragmentShader: [
  102. "uniform vec3 uAmbientColor;",
  103. "uniform vec3 uDiffuseColor;",
  104. "uniform vec3 uSpecularColor;",
  105. "uniform float uShininess;",
  106. "uniform float uOpacity;",
  107. "uniform bool enableDiffuse;",
  108. "uniform bool enableSpecular;",
  109. "uniform bool enableAO;",
  110. "uniform bool enableReflection;",
  111. "uniform sampler2D tDiffuse;",
  112. "uniform sampler2D tNormal;",
  113. "uniform sampler2D tSpecular;",
  114. "uniform sampler2D tAO;",
  115. "uniform samplerCube tCube;",
  116. "uniform vec2 uNormalScale;",
  117. "uniform bool useRefract;",
  118. "uniform float uRefractionRatio;",
  119. "uniform float uReflectivity;",
  120. "varying vec3 vTangent;",
  121. "varying vec3 vBinormal;",
  122. "varying vec3 vNormal;",
  123. "varying vec2 vUv;",
  124. "uniform vec3 ambientLightColor;",
  125. "#if MAX_DIR_LIGHTS > 0",
  126. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  127. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  128. "#endif",
  129. "#if MAX_HEMI_LIGHTS > 0",
  130. "uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];",
  131. "uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];",
  132. "uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];",
  133. "#endif",
  134. "#if MAX_POINT_LIGHTS > 0",
  135. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  136. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  137. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  138. "#endif",
  139. "#if MAX_SPOT_LIGHTS > 0",
  140. "uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];",
  141. "uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];",
  142. "uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];",
  143. "uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];",
  144. "uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];",
  145. "uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];",
  146. "#endif",
  147. "#ifdef WRAP_AROUND",
  148. "uniform vec3 wrapRGB;",
  149. "#endif",
  150. "varying vec3 vWorldPosition;",
  151. "varying vec3 vViewPosition;",
  152. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  153. THREE.ShaderChunk[ "fog_pars_fragment" ],
  154. "void main() {",
  155. "gl_FragColor = vec4( vec3( 1.0 ), uOpacity );",
  156. "vec3 specularTex = vec3( 1.0 );",
  157. "vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
  158. "normalTex.xy *= uNormalScale;",
  159. "normalTex = normalize( normalTex );",
  160. "if( enableDiffuse ) {",
  161. "#ifdef GAMMA_INPUT",
  162. "vec4 texelColor = texture2D( tDiffuse, vUv );",
  163. "texelColor.xyz *= texelColor.xyz;",
  164. "gl_FragColor = gl_FragColor * texelColor;",
  165. "#else",
  166. "gl_FragColor = gl_FragColor * texture2D( tDiffuse, vUv );",
  167. "#endif",
  168. "}",
  169. "if( enableAO ) {",
  170. "#ifdef GAMMA_INPUT",
  171. "vec4 aoColor = texture2D( tAO, vUv );",
  172. "aoColor.xyz *= aoColor.xyz;",
  173. "gl_FragColor.xyz = gl_FragColor.xyz * aoColor.xyz;",
  174. "#else",
  175. "gl_FragColor.xyz = gl_FragColor.xyz * texture2D( tAO, vUv ).xyz;",
  176. "#endif",
  177. "}",
  178. "if( enableSpecular )",
  179. "specularTex = texture2D( tSpecular, vUv ).xyz;",
  180. "mat3 tsb = mat3( normalize( vTangent ), normalize( vBinormal ), normalize( vNormal ) );",
  181. "vec3 finalNormal = tsb * normalTex;",
  182. "#ifdef FLIP_SIDED",
  183. "finalNormal = -finalNormal;",
  184. "#endif",
  185. "vec3 normal = normalize( finalNormal );",
  186. "vec3 viewPosition = normalize( vViewPosition );",
  187. // point lights
  188. "#if MAX_POINT_LIGHTS > 0",
  189. "vec3 pointDiffuse = vec3( 0.0 );",
  190. "vec3 pointSpecular = vec3( 0.0 );",
  191. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  192. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  193. "vec3 pointVector = lPosition.xyz + vViewPosition.xyz;",
  194. "float pointDistance = 1.0;",
  195. "if ( pointLightDistance[ i ] > 0.0 )",
  196. "pointDistance = 1.0 - min( ( length( pointVector ) / pointLightDistance[ i ] ), 1.0 );",
  197. "pointVector = normalize( pointVector );",
  198. // diffuse
  199. "#ifdef WRAP_AROUND",
  200. "float pointDiffuseWeightFull = max( dot( normal, pointVector ), 0.0 );",
  201. "float pointDiffuseWeightHalf = max( 0.5 * dot( normal, pointVector ) + 0.5, 0.0 );",
  202. "vec3 pointDiffuseWeight = mix( vec3 ( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );",
  203. "#else",
  204. "float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
  205. "#endif",
  206. "pointDiffuse += pointDistance * pointLightColor[ i ] * uDiffuseColor * pointDiffuseWeight;",
  207. // specular
  208. "vec3 pointHalfVector = normalize( pointVector + viewPosition );",
  209. "float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
  210. "float pointSpecularWeight = specularTex.r * max( pow( pointDotNormalHalf, uShininess ), 0.0 );",
  211. "#ifdef PHYSICALLY_BASED_SHADING",
  212. // 2.0 => 2.0001 is hack to work around ANGLE bug
  213. "float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",
  214. "vec3 schlick = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( pointVector, pointHalfVector ), 5.0 );",
  215. "pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * pointDistance * specularNormalization;",
  216. "#else",
  217. "pointSpecular += pointDistance * pointLightColor[ i ] * uSpecularColor * pointSpecularWeight * pointDiffuseWeight;",
  218. "#endif",
  219. "}",
  220. "#endif",
  221. // spot lights
  222. "#if MAX_SPOT_LIGHTS > 0",
  223. "vec3 spotDiffuse = vec3( 0.0 );",
  224. "vec3 spotSpecular = vec3( 0.0 );",
  225. "for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {",
  226. "vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );",
  227. "vec3 spotVector = lPosition.xyz + vViewPosition.xyz;",
  228. "float spotDistance = 1.0;",
  229. "if ( spotLightDistance[ i ] > 0.0 )",
  230. "spotDistance = 1.0 - min( ( length( spotVector ) / spotLightDistance[ i ] ), 1.0 );",
  231. "spotVector = normalize( spotVector );",
  232. "float spotEffect = dot( spotLightDirection[ i ], normalize( spotLightPosition[ i ] - vWorldPosition ) );",
  233. "if ( spotEffect > spotLightAngleCos[ i ] ) {",
  234. "spotEffect = max( pow( spotEffect, spotLightExponent[ i ] ), 0.0 );",
  235. // diffuse
  236. "#ifdef WRAP_AROUND",
  237. "float spotDiffuseWeightFull = max( dot( normal, spotVector ), 0.0 );",
  238. "float spotDiffuseWeightHalf = max( 0.5 * dot( normal, spotVector ) + 0.5, 0.0 );",
  239. "vec3 spotDiffuseWeight = mix( vec3 ( spotDiffuseWeightFull ), vec3( spotDiffuseWeightHalf ), wrapRGB );",
  240. "#else",
  241. "float spotDiffuseWeight = max( dot( normal, spotVector ), 0.0 );",
  242. "#endif",
  243. "spotDiffuse += spotDistance * spotLightColor[ i ] * uDiffuseColor * spotDiffuseWeight * spotEffect;",
  244. // specular
  245. "vec3 spotHalfVector = normalize( spotVector + viewPosition );",
  246. "float spotDotNormalHalf = max( dot( normal, spotHalfVector ), 0.0 );",
  247. "float spotSpecularWeight = specularTex.r * max( pow( spotDotNormalHalf, uShininess ), 0.0 );",
  248. "#ifdef PHYSICALLY_BASED_SHADING",
  249. // 2.0 => 2.0001 is hack to work around ANGLE bug
  250. "float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",
  251. "vec3 schlick = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( spotVector, spotHalfVector ), 5.0 );",
  252. "spotSpecular += schlick * spotLightColor[ i ] * spotSpecularWeight * spotDiffuseWeight * spotDistance * specularNormalization * spotEffect;",
  253. "#else",
  254. "spotSpecular += spotDistance * spotLightColor[ i ] * uSpecularColor * spotSpecularWeight * spotDiffuseWeight * spotEffect;",
  255. "#endif",
  256. "}",
  257. "}",
  258. "#endif",
  259. // directional lights
  260. "#if MAX_DIR_LIGHTS > 0",
  261. "vec3 dirDiffuse = vec3( 0.0 );",
  262. "vec3 dirSpecular = vec3( 0.0 );",
  263. "for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
  264. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  265. "vec3 dirVector = normalize( lDirection.xyz );",
  266. // diffuse
  267. "#ifdef WRAP_AROUND",
  268. "float directionalLightWeightingFull = max( dot( normal, dirVector ), 0.0 );",
  269. "float directionalLightWeightingHalf = max( 0.5 * dot( normal, dirVector ) + 0.5, 0.0 );",
  270. "vec3 dirDiffuseWeight = mix( vec3( directionalLightWeightingFull ), vec3( directionalLightWeightingHalf ), wrapRGB );",
  271. "#else",
  272. "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  273. "#endif",
  274. "dirDiffuse += directionalLightColor[ i ] * uDiffuseColor * dirDiffuseWeight;",
  275. // specular
  276. "vec3 dirHalfVector = normalize( dirVector + viewPosition );",
  277. "float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
  278. "float dirSpecularWeight = specularTex.r * max( pow( dirDotNormalHalf, uShininess ), 0.0 );",
  279. "#ifdef PHYSICALLY_BASED_SHADING",
  280. // 2.0 => 2.0001 is hack to work around ANGLE bug
  281. "float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",
  282. "vec3 schlick = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( dirVector, dirHalfVector ), 5.0 );",
  283. "dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;",
  284. "#else",
  285. "dirSpecular += directionalLightColor[ i ] * uSpecularColor * dirSpecularWeight * dirDiffuseWeight;",
  286. "#endif",
  287. "}",
  288. "#endif",
  289. // hemisphere lights
  290. "#if MAX_HEMI_LIGHTS > 0",
  291. "vec3 hemiDiffuse = vec3( 0.0 );",
  292. "vec3 hemiSpecular = vec3( 0.0 );" ,
  293. "for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {",
  294. "vec4 lDirection = viewMatrix * vec4( hemisphereLightDirection[ i ], 0.0 );",
  295. "vec3 lVector = normalize( lDirection.xyz );",
  296. // diffuse
  297. "float dotProduct = dot( normal, lVector );",
  298. "float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
  299. "vec3 hemiColor = mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
  300. "hemiDiffuse += uDiffuseColor * hemiColor;",
  301. // specular (sky light)
  302. "vec3 hemiHalfVectorSky = normalize( lVector + viewPosition );",
  303. "float hemiDotNormalHalfSky = 0.5 * dot( normal, hemiHalfVectorSky ) + 0.5;",
  304. "float hemiSpecularWeightSky = specularTex.r * max( pow( hemiDotNormalHalfSky, uShininess ), 0.0 );",
  305. // specular (ground light)
  306. "vec3 lVectorGround = -lVector;",
  307. "vec3 hemiHalfVectorGround = normalize( lVectorGround + viewPosition );",
  308. "float hemiDotNormalHalfGround = 0.5 * dot( normal, hemiHalfVectorGround ) + 0.5;",
  309. "float hemiSpecularWeightGround = specularTex.r * max( pow( hemiDotNormalHalfGround, uShininess ), 0.0 );",
  310. "#ifdef PHYSICALLY_BASED_SHADING",
  311. "float dotProductGround = dot( normal, lVectorGround );",
  312. // 2.0 => 2.0001 is hack to work around ANGLE bug
  313. "float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",
  314. "vec3 schlickSky = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( lVector, hemiHalfVectorSky ), 5.0 );",
  315. "vec3 schlickGround = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( lVectorGround, hemiHalfVectorGround ), 5.0 );",
  316. "hemiSpecular += hemiColor * specularNormalization * ( schlickSky * hemiSpecularWeightSky * max( dotProduct, 0.0 ) + schlickGround * hemiSpecularWeightGround * max( dotProductGround, 0.0 ) );",
  317. "#else",
  318. "hemiSpecular += uSpecularColor * hemiColor * ( hemiSpecularWeightSky + hemiSpecularWeightGround ) * hemiDiffuseWeight;",
  319. "#endif",
  320. "}",
  321. "#endif",
  322. // all lights contribution summation
  323. "vec3 totalDiffuse = vec3( 0.0 );",
  324. "vec3 totalSpecular = vec3( 0.0 );",
  325. "#if MAX_DIR_LIGHTS > 0",
  326. "totalDiffuse += dirDiffuse;",
  327. "totalSpecular += dirSpecular;",
  328. "#endif",
  329. "#if MAX_HEMI_LIGHTS > 0",
  330. "totalDiffuse += hemiDiffuse;",
  331. "totalSpecular += hemiSpecular;",
  332. "#endif",
  333. "#if MAX_POINT_LIGHTS > 0",
  334. "totalDiffuse += pointDiffuse;",
  335. "totalSpecular += pointSpecular;",
  336. "#endif",
  337. "#if MAX_SPOT_LIGHTS > 0",
  338. "totalDiffuse += spotDiffuse;",
  339. "totalSpecular += spotSpecular;",
  340. "#endif",
  341. "#ifdef METAL",
  342. "gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * uAmbientColor + totalSpecular );",
  343. "#else",
  344. "gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * uAmbientColor ) + totalSpecular;",
  345. "#endif",
  346. "if ( enableReflection ) {",
  347. "vec3 vReflect;",
  348. "vec3 cameraToVertex = normalize( vWorldPosition - cameraPosition );",
  349. "if ( useRefract ) {",
  350. "vReflect = refract( cameraToVertex, normal, uRefractionRatio );",
  351. "} else {",
  352. "vReflect = reflect( cameraToVertex, normal );",
  353. "}",
  354. "vec4 cubeColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
  355. "#ifdef GAMMA_INPUT",
  356. "cubeColor.xyz *= cubeColor.xyz;",
  357. "#endif",
  358. "gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, specularTex.r * uReflectivity );",
  359. "}",
  360. THREE.ShaderChunk[ "shadowmap_fragment" ],
  361. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  362. THREE.ShaderChunk[ "fog_fragment" ],
  363. "}"
  364. ].join("\n"),
  365. vertexShader: [
  366. "attribute vec4 tangent;",
  367. "uniform vec2 uOffset;",
  368. "uniform vec2 uRepeat;",
  369. "uniform bool enableDisplacement;",
  370. "#ifdef VERTEX_TEXTURES",
  371. "uniform sampler2D tDisplacement;",
  372. "uniform float uDisplacementScale;",
  373. "uniform float uDisplacementBias;",
  374. "#endif",
  375. "varying vec3 vTangent;",
  376. "varying vec3 vBinormal;",
  377. "varying vec3 vNormal;",
  378. "varying vec2 vUv;",
  379. "varying vec3 vWorldPosition;",
  380. "varying vec3 vViewPosition;",
  381. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  382. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  383. "void main() {",
  384. THREE.ShaderChunk[ "skinbase_vertex" ],
  385. THREE.ShaderChunk[ "skinnormal_vertex" ],
  386. // normal, tangent and binormal vectors
  387. "#ifdef USE_SKINNING",
  388. "vNormal = normalize( normalMatrix * skinnedNormal.xyz );",
  389. "vec4 skinnedTangent = skinMatrix * vec4( tangent.xyz, 0.0 );",
  390. "vTangent = normalize( normalMatrix * skinnedTangent.xyz );",
  391. "#else",
  392. "vNormal = normalize( normalMatrix * normal );",
  393. "vTangent = normalize( normalMatrix * tangent.xyz );",
  394. "#endif",
  395. "vBinormal = normalize( cross( vNormal, vTangent ) * tangent.w );",
  396. "vUv = uv * uRepeat + uOffset;",
  397. // displacement mapping
  398. "vec3 displacedPosition;",
  399. "#ifdef VERTEX_TEXTURES",
  400. "if ( enableDisplacement ) {",
  401. "vec3 dv = texture2D( tDisplacement, uv ).xyz;",
  402. "float df = uDisplacementScale * dv.x + uDisplacementBias;",
  403. "displacedPosition = position + normalize( normal ) * df;",
  404. "} else {",
  405. "#ifdef USE_SKINNING",
  406. "vec4 skinVertex = vec4( position, 1.0 );",
  407. "vec4 skinned = boneMatX * skinVertex * skinWeight.x;",
  408. "skinned += boneMatY * skinVertex * skinWeight.y;",
  409. "displacedPosition = skinned.xyz;",
  410. "#else",
  411. "displacedPosition = position;",
  412. "#endif",
  413. "}",
  414. "#else",
  415. "#ifdef USE_SKINNING",
  416. "vec4 skinVertex = vec4( position, 1.0 );",
  417. "vec4 skinned = boneMatX * skinVertex * skinWeight.x;",
  418. "skinned += boneMatY * skinVertex * skinWeight.y;",
  419. "displacedPosition = skinned.xyz;",
  420. "#else",
  421. "displacedPosition = position;",
  422. "#endif",
  423. "#endif",
  424. //
  425. "vec4 mvPosition = modelViewMatrix * vec4( displacedPosition, 1.0 );",
  426. "vec4 worldPosition = modelMatrix * vec4( displacedPosition, 1.0 );",
  427. "gl_Position = projectionMatrix * mvPosition;",
  428. //
  429. "vWorldPosition = worldPosition.xyz;",
  430. "vViewPosition = -mvPosition.xyz;",
  431. // shadows
  432. "#ifdef USE_SHADOWMAP",
  433. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  434. "vShadowCoord[ i ] = shadowMatrix[ i ] * worldPosition;",
  435. "}",
  436. "#endif",
  437. "}"
  438. ].join("\n")
  439. },
  440. /* -------------------------------------------------------------------------
  441. // Cube map shader
  442. ------------------------------------------------------------------------- */
  443. 'cube': {
  444. uniforms: { "tCube": { type: "t", value: null },
  445. "tFlip": { type: "f", value: -1 } },
  446. vertexShader: [
  447. "varying vec3 vWorldPosition;",
  448. "void main() {",
  449. "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  450. "vWorldPosition = worldPosition.xyz;",
  451. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  452. "}"
  453. ].join("\n"),
  454. fragmentShader: [
  455. "uniform samplerCube tCube;",
  456. "uniform float tFlip;",
  457. "varying vec3 vWorldPosition;",
  458. "void main() {",
  459. "gl_FragColor = textureCube( tCube, vec3( tFlip * vWorldPosition.x, vWorldPosition.yz ) );",
  460. "}"
  461. ].join("\n")
  462. }
  463. }
  464. };