ShaderUtils.js 21 KB

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