WebGLShaders.js 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author mrdoob / http://mrdoob.com/
  4. * @author mikael emtinger / http://gomo.se/
  5. */
  6. THREE.ShaderChunk = {
  7. // FOG
  8. fog_pars_fragment: [
  9. "#ifdef USE_FOG",
  10. "uniform vec3 fogColor;",
  11. "#ifdef FOG_EXP2",
  12. "uniform float fogDensity;",
  13. "#else",
  14. "uniform float fogNear;",
  15. "uniform float fogFar;",
  16. "#endif",
  17. "#endif"
  18. ].join("\n"),
  19. fog_fragment: [
  20. "#ifdef USE_FOG",
  21. "float depth = gl_FragCoord.z / gl_FragCoord.w;",
  22. "#ifdef FOG_EXP2",
  23. "const float LOG2 = 1.442695;",
  24. "float fogFactor = exp2( - fogDensity * fogDensity * depth * depth * LOG2 );",
  25. "fogFactor = 1.0 - clamp( fogFactor, 0.0, 1.0 );",
  26. "#else",
  27. "float fogFactor = smoothstep( fogNear, fogFar, depth );",
  28. "#endif",
  29. "gl_FragColor = mix( gl_FragColor, vec4( fogColor, gl_FragColor.w ), fogFactor );",
  30. "#endif"
  31. ].join("\n"),
  32. // ENVIRONMENT MAP
  33. envmap_pars_fragment: [
  34. "#ifdef USE_ENVMAP",
  35. "uniform float reflectivity;",
  36. "uniform samplerCube envMap;",
  37. "uniform float flipEnvMap;",
  38. "uniform int combine;",
  39. "#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP )",
  40. "uniform bool useRefract;",
  41. "uniform float refractionRatio;",
  42. "#else",
  43. "varying vec3 vReflect;",
  44. "#endif",
  45. "#endif"
  46. ].join("\n"),
  47. envmap_fragment: [
  48. "#ifdef USE_ENVMAP",
  49. "vec3 reflectVec;",
  50. "#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP )",
  51. "vec3 cameraToVertex = normalize( vWorldPosition - cameraPosition );",
  52. "if ( useRefract ) {",
  53. "reflectVec = refract( cameraToVertex, normal, refractionRatio );",
  54. "} else { ",
  55. "reflectVec = reflect( cameraToVertex, normal );",
  56. "}",
  57. "#else",
  58. "reflectVec = vReflect;",
  59. "#endif",
  60. "#ifdef DOUBLE_SIDED",
  61. "float flipNormal = ( -1.0 + 2.0 * float( gl_FrontFacing ) );",
  62. "vec4 cubeColor = textureCube( envMap, flipNormal * vec3( flipEnvMap * reflectVec.x, reflectVec.yz ) );",
  63. "#else",
  64. "vec4 cubeColor = textureCube( envMap, vec3( flipEnvMap * reflectVec.x, reflectVec.yz ) );",
  65. "#endif",
  66. "#ifdef GAMMA_INPUT",
  67. "cubeColor.xyz *= cubeColor.xyz;",
  68. "#endif",
  69. "if ( combine == 1 ) {",
  70. "gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, specularStrength * reflectivity );",
  71. "} else if ( combine == 2 ) {",
  72. "gl_FragColor.xyz += cubeColor.xyz * specularStrength * reflectivity;",
  73. "} else {",
  74. "gl_FragColor.xyz = mix( gl_FragColor.xyz, gl_FragColor.xyz * cubeColor.xyz, specularStrength * reflectivity );",
  75. "}",
  76. "#endif"
  77. ].join("\n"),
  78. envmap_pars_vertex: [
  79. "#if defined( USE_ENVMAP ) && ! defined( USE_BUMPMAP ) && ! defined( USE_NORMALMAP )",
  80. "varying vec3 vReflect;",
  81. "uniform float refractionRatio;",
  82. "uniform bool useRefract;",
  83. "#endif"
  84. ].join("\n"),
  85. worldpos_vertex : [
  86. "#if defined( USE_ENVMAP ) || defined( PHONG ) || defined( LAMBERT ) || defined ( USE_SHADOWMAP )",
  87. "#ifdef USE_SKINNING",
  88. "vec4 worldPosition = modelMatrix * skinned;",
  89. "#endif",
  90. "#if defined( USE_MORPHTARGETS ) && ! defined( USE_SKINNING )",
  91. "vec4 worldPosition = modelMatrix * vec4( morphed, 1.0 );",
  92. "#endif",
  93. "#if ! defined( USE_MORPHTARGETS ) && ! defined( USE_SKINNING )",
  94. "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  95. "#endif",
  96. "#endif"
  97. ].join("\n"),
  98. envmap_vertex : [
  99. "#if defined( USE_ENVMAP ) && ! defined( USE_BUMPMAP ) && ! defined( USE_NORMALMAP )",
  100. "vec3 worldNormal = mat3( modelMatrix[ 0 ].xyz, modelMatrix[ 1 ].xyz, modelMatrix[ 2 ].xyz ) * objectNormal;",
  101. "worldNormal = normalize( worldNormal );",
  102. "vec3 cameraToVertex = normalize( worldPosition.xyz - cameraPosition );",
  103. "if ( useRefract ) {",
  104. "vReflect = refract( cameraToVertex, worldNormal, refractionRatio );",
  105. "} else {",
  106. "vReflect = reflect( cameraToVertex, worldNormal );",
  107. "}",
  108. "#endif"
  109. ].join("\n"),
  110. // COLOR MAP (particles)
  111. map_particle_pars_fragment: [
  112. "#ifdef USE_MAP",
  113. "uniform sampler2D map;",
  114. "#endif"
  115. ].join("\n"),
  116. map_particle_fragment: [
  117. "#ifdef USE_MAP",
  118. "gl_FragColor = gl_FragColor * texture2D( map, vec2( gl_PointCoord.x, 1.0 - gl_PointCoord.y ) );",
  119. "#endif"
  120. ].join("\n"),
  121. // COLOR MAP (triangles)
  122. map_pars_vertex: [
  123. "#if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP )",
  124. "varying vec2 vUv;",
  125. "uniform vec4 offsetRepeat;",
  126. "#endif"
  127. ].join("\n"),
  128. map_pars_fragment: [
  129. "#if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP )",
  130. "varying vec2 vUv;",
  131. "#endif",
  132. "#ifdef USE_MAP",
  133. "uniform sampler2D map;",
  134. "#endif"
  135. ].join("\n"),
  136. map_vertex: [
  137. "#if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP )",
  138. "vUv = uv * offsetRepeat.zw + offsetRepeat.xy;",
  139. "#endif"
  140. ].join("\n"),
  141. map_fragment: [
  142. "#ifdef USE_MAP",
  143. "vec4 texelColor = texture2D( map, vUv );",
  144. "#ifdef GAMMA_INPUT",
  145. "texelColor.xyz *= texelColor.xyz;",
  146. "#endif",
  147. "gl_FragColor = gl_FragColor * texelColor;",
  148. "#endif"
  149. ].join("\n"),
  150. // LIGHT MAP
  151. lightmap_pars_fragment: [
  152. "#ifdef USE_LIGHTMAP",
  153. "varying vec2 vUv2;",
  154. "uniform sampler2D lightMap;",
  155. "#endif"
  156. ].join("\n"),
  157. lightmap_pars_vertex: [
  158. "#ifdef USE_LIGHTMAP",
  159. "varying vec2 vUv2;",
  160. "#endif"
  161. ].join("\n"),
  162. lightmap_fragment: [
  163. "#ifdef USE_LIGHTMAP",
  164. "gl_FragColor = gl_FragColor * texture2D( lightMap, vUv2 );",
  165. "#endif"
  166. ].join("\n"),
  167. lightmap_vertex: [
  168. "#ifdef USE_LIGHTMAP",
  169. "vUv2 = uv2;",
  170. "#endif"
  171. ].join("\n"),
  172. // BUMP MAP
  173. bumpmap_pars_fragment: [
  174. "#ifdef USE_BUMPMAP",
  175. "uniform sampler2D bumpMap;",
  176. "uniform float bumpScale;",
  177. // Derivative maps - bump mapping unparametrized surfaces by Morten Mikkelsen
  178. // http://mmikkelsen3d.blogspot.sk/2011/07/derivative-maps.html
  179. // Evaluate the derivative of the height w.r.t. screen-space using forward differencing (listing 2)
  180. "vec2 dHdxy_fwd() {",
  181. "vec2 dSTdx = dFdx( vUv );",
  182. "vec2 dSTdy = dFdy( vUv );",
  183. "float Hll = bumpScale * texture2D( bumpMap, vUv ).x;",
  184. "float dBx = bumpScale * texture2D( bumpMap, vUv + dSTdx ).x - Hll;",
  185. "float dBy = bumpScale * texture2D( bumpMap, vUv + dSTdy ).x - Hll;",
  186. "return vec2( dBx, dBy );",
  187. "}",
  188. "vec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy ) {",
  189. "vec3 vSigmaX = dFdx( surf_pos );",
  190. "vec3 vSigmaY = dFdy( surf_pos );",
  191. "vec3 vN = surf_norm;", // normalized
  192. "vec3 R1 = cross( vSigmaY, vN );",
  193. "vec3 R2 = cross( vN, vSigmaX );",
  194. "float fDet = dot( vSigmaX, R1 );",
  195. "vec3 vGrad = sign( fDet ) * ( dHdxy.x * R1 + dHdxy.y * R2 );",
  196. "return normalize( abs( fDet ) * surf_norm - vGrad );",
  197. "}",
  198. "#endif"
  199. ].join("\n"),
  200. // NORMAL MAP
  201. normalmap_pars_fragment: [
  202. "#ifdef USE_NORMALMAP",
  203. "uniform sampler2D normalMap;",
  204. "uniform vec2 normalScale;",
  205. // Per-Pixel Tangent Space Normal Mapping
  206. // http://hacksoflife.blogspot.ch/2009/11/per-pixel-tangent-space-normal-mapping.html
  207. "vec3 perturbNormal2Arb( vec3 eye_pos, vec3 surf_norm ) {",
  208. "vec3 q0 = dFdx( eye_pos.xyz );",
  209. "vec3 q1 = dFdy( eye_pos.xyz );",
  210. "vec2 st0 = dFdx( vUv.st );",
  211. "vec2 st1 = dFdy( vUv.st );",
  212. "vec3 S = normalize( q0 * st1.t - q1 * st0.t );",
  213. "vec3 T = normalize( -q0 * st1.s + q1 * st0.s );",
  214. "vec3 N = normalize( surf_norm );",
  215. "vec3 mapN = texture2D( normalMap, vUv ).xyz * 2.0 - 1.0;",
  216. "mapN.xy = normalScale * mapN.xy;",
  217. "mat3 tsn = mat3( S, T, N );",
  218. "return normalize( tsn * mapN );",
  219. "}",
  220. "#endif"
  221. ].join("\n"),
  222. // SPECULAR MAP
  223. specularmap_pars_fragment: [
  224. "#ifdef USE_SPECULARMAP",
  225. "uniform sampler2D specularMap;",
  226. "#endif"
  227. ].join("\n"),
  228. specularmap_fragment: [
  229. "float specularStrength;",
  230. "#ifdef USE_SPECULARMAP",
  231. "vec4 texelSpecular = texture2D( specularMap, vUv );",
  232. "specularStrength = texelSpecular.r;",
  233. "#else",
  234. "specularStrength = 1.0;",
  235. "#endif"
  236. ].join("\n"),
  237. // LIGHTS LAMBERT
  238. lights_lambert_pars_vertex: [
  239. "uniform vec3 ambient;",
  240. "uniform vec3 diffuse;",
  241. "uniform vec3 emissive;",
  242. "uniform vec3 ambientLightColor;",
  243. "#if MAX_DIR_LIGHTS > 0",
  244. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  245. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  246. "#endif",
  247. "#if MAX_HEMI_LIGHTS > 0",
  248. "uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];",
  249. "uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];",
  250. "uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];",
  251. "#endif",
  252. "#if MAX_POINT_LIGHTS > 0",
  253. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  254. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  255. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  256. "#endif",
  257. "#if MAX_SPOT_LIGHTS > 0",
  258. "uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];",
  259. "uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];",
  260. "uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];",
  261. "uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];",
  262. "uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];",
  263. "uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];",
  264. "#endif",
  265. "#ifdef WRAP_AROUND",
  266. "uniform vec3 wrapRGB;",
  267. "#endif"
  268. ].join("\n"),
  269. lights_lambert_vertex: [
  270. "vLightFront = vec3( 0.0 );",
  271. "#ifdef DOUBLE_SIDED",
  272. "vLightBack = vec3( 0.0 );",
  273. "#endif",
  274. "transformedNormal = normalize( transformedNormal );",
  275. "#if MAX_DIR_LIGHTS > 0",
  276. "for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {",
  277. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  278. "vec3 dirVector = normalize( lDirection.xyz );",
  279. "float dotProduct = dot( transformedNormal, dirVector );",
  280. "vec3 directionalLightWeighting = vec3( max( dotProduct, 0.0 ) );",
  281. "#ifdef DOUBLE_SIDED",
  282. "vec3 directionalLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );",
  283. "#ifdef WRAP_AROUND",
  284. "vec3 directionalLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );",
  285. "#endif",
  286. "#endif",
  287. "#ifdef WRAP_AROUND",
  288. "vec3 directionalLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );",
  289. "directionalLightWeighting = mix( directionalLightWeighting, directionalLightWeightingHalf, wrapRGB );",
  290. "#ifdef DOUBLE_SIDED",
  291. "directionalLightWeightingBack = mix( directionalLightWeightingBack, directionalLightWeightingHalfBack, wrapRGB );",
  292. "#endif",
  293. "#endif",
  294. "vLightFront += directionalLightColor[ i ] * directionalLightWeighting;",
  295. "#ifdef DOUBLE_SIDED",
  296. "vLightBack += directionalLightColor[ i ] * directionalLightWeightingBack;",
  297. "#endif",
  298. "}",
  299. "#endif",
  300. "#if MAX_POINT_LIGHTS > 0",
  301. "for( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  302. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  303. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  304. "float lDistance = 1.0;",
  305. "if ( pointLightDistance[ i ] > 0.0 )",
  306. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  307. "lVector = normalize( lVector );",
  308. "float dotProduct = dot( transformedNormal, lVector );",
  309. "vec3 pointLightWeighting = vec3( max( dotProduct, 0.0 ) );",
  310. "#ifdef DOUBLE_SIDED",
  311. "vec3 pointLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );",
  312. "#ifdef WRAP_AROUND",
  313. "vec3 pointLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );",
  314. "#endif",
  315. "#endif",
  316. "#ifdef WRAP_AROUND",
  317. "vec3 pointLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );",
  318. "pointLightWeighting = mix( pointLightWeighting, pointLightWeightingHalf, wrapRGB );",
  319. "#ifdef DOUBLE_SIDED",
  320. "pointLightWeightingBack = mix( pointLightWeightingBack, pointLightWeightingHalfBack, wrapRGB );",
  321. "#endif",
  322. "#endif",
  323. "vLightFront += pointLightColor[ i ] * pointLightWeighting * lDistance;",
  324. "#ifdef DOUBLE_SIDED",
  325. "vLightBack += pointLightColor[ i ] * pointLightWeightingBack * lDistance;",
  326. "#endif",
  327. "}",
  328. "#endif",
  329. "#if MAX_SPOT_LIGHTS > 0",
  330. "for( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {",
  331. "vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );",
  332. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  333. "float spotEffect = dot( spotLightDirection[ i ], normalize( spotLightPosition[ i ] - worldPosition.xyz ) );",
  334. "if ( spotEffect > spotLightAngleCos[ i ] ) {",
  335. "spotEffect = max( pow( spotEffect, spotLightExponent[ i ] ), 0.0 );",
  336. "float lDistance = 1.0;",
  337. "if ( spotLightDistance[ i ] > 0.0 )",
  338. "lDistance = 1.0 - min( ( length( lVector ) / spotLightDistance[ i ] ), 1.0 );",
  339. "lVector = normalize( lVector );",
  340. "float dotProduct = dot( transformedNormal, lVector );",
  341. "vec3 spotLightWeighting = vec3( max( dotProduct, 0.0 ) );",
  342. "#ifdef DOUBLE_SIDED",
  343. "vec3 spotLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );",
  344. "#ifdef WRAP_AROUND",
  345. "vec3 spotLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );",
  346. "#endif",
  347. "#endif",
  348. "#ifdef WRAP_AROUND",
  349. "vec3 spotLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );",
  350. "spotLightWeighting = mix( spotLightWeighting, spotLightWeightingHalf, wrapRGB );",
  351. "#ifdef DOUBLE_SIDED",
  352. "spotLightWeightingBack = mix( spotLightWeightingBack, spotLightWeightingHalfBack, wrapRGB );",
  353. "#endif",
  354. "#endif",
  355. "vLightFront += spotLightColor[ i ] * spotLightWeighting * lDistance * spotEffect;",
  356. "#ifdef DOUBLE_SIDED",
  357. "vLightBack += spotLightColor[ i ] * spotLightWeightingBack * lDistance * spotEffect;",
  358. "#endif",
  359. "}",
  360. "}",
  361. "#endif",
  362. "#if MAX_HEMI_LIGHTS > 0",
  363. "for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {",
  364. "vec4 lDirection = viewMatrix * vec4( hemisphereLightDirection[ i ], 0.0 );",
  365. "vec3 lVector = normalize( lDirection.xyz );",
  366. "float dotProduct = dot( transformedNormal, lVector );",
  367. "float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
  368. "float hemiDiffuseWeightBack = -0.5 * dotProduct + 0.5;",
  369. "vLightFront += mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
  370. "#ifdef DOUBLE_SIDED",
  371. "vLightBack += mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeightBack );",
  372. "#endif",
  373. "}",
  374. "#endif",
  375. "vLightFront = vLightFront * diffuse + ambient * ambientLightColor + emissive;",
  376. "#ifdef DOUBLE_SIDED",
  377. "vLightBack = vLightBack * diffuse + ambient * ambientLightColor + emissive;",
  378. "#endif"
  379. ].join("\n"),
  380. // LIGHTS PHONG
  381. lights_phong_pars_vertex: [
  382. "#ifndef PHONG_PER_PIXEL",
  383. "#if MAX_POINT_LIGHTS > 0",
  384. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  385. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  386. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  387. "#endif",
  388. "#if MAX_SPOT_LIGHTS > 0",
  389. "uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];",
  390. "uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];",
  391. "varying vec4 vSpotLight[ MAX_SPOT_LIGHTS ];",
  392. "#endif",
  393. "#endif",
  394. "#if MAX_SPOT_LIGHTS > 0 || defined( USE_BUMPMAP )",
  395. "varying vec3 vWorldPosition;",
  396. "#endif"
  397. ].join("\n"),
  398. lights_phong_vertex: [
  399. "#ifndef PHONG_PER_PIXEL",
  400. "#if MAX_POINT_LIGHTS > 0",
  401. "for( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  402. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  403. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  404. "float lDistance = 1.0;",
  405. "if ( pointLightDistance[ i ] > 0.0 )",
  406. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  407. "vPointLight[ i ] = vec4( lVector, lDistance );",
  408. "}",
  409. "#endif",
  410. "#if MAX_SPOT_LIGHTS > 0",
  411. "for( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {",
  412. "vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );",
  413. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  414. "float lDistance = 1.0;",
  415. "if ( spotLightDistance[ i ] > 0.0 )",
  416. "lDistance = 1.0 - min( ( length( lVector ) / spotLightDistance[ i ] ), 1.0 );",
  417. "vSpotLight[ i ] = vec4( lVector, lDistance );",
  418. "}",
  419. "#endif",
  420. "#endif",
  421. "#if MAX_SPOT_LIGHTS > 0 || defined( USE_BUMPMAP )",
  422. "vWorldPosition = worldPosition.xyz;",
  423. "#endif"
  424. ].join("\n"),
  425. lights_phong_pars_fragment: [
  426. "uniform vec3 ambientLightColor;",
  427. "#if MAX_DIR_LIGHTS > 0",
  428. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  429. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  430. "#endif",
  431. "#if MAX_HEMI_LIGHTS > 0",
  432. "uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];",
  433. "uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];",
  434. "uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];",
  435. "#endif",
  436. "#if MAX_POINT_LIGHTS > 0",
  437. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  438. "#ifdef PHONG_PER_PIXEL",
  439. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  440. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  441. "#else",
  442. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  443. "#endif",
  444. "#endif",
  445. "#if MAX_SPOT_LIGHTS > 0",
  446. "uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];",
  447. "uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];",
  448. "uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];",
  449. "uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];",
  450. "uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];",
  451. "#ifdef PHONG_PER_PIXEL",
  452. "uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];",
  453. "#else",
  454. "varying vec4 vSpotLight[ MAX_SPOT_LIGHTS ];",
  455. "#endif",
  456. "#endif",
  457. "#if MAX_SPOT_LIGHTS > 0 || defined( USE_BUMPMAP )",
  458. "varying vec3 vWorldPosition;",
  459. "#endif",
  460. "#ifdef WRAP_AROUND",
  461. "uniform vec3 wrapRGB;",
  462. "#endif",
  463. "varying vec3 vViewPosition;",
  464. "varying vec3 vNormal;"
  465. ].join("\n"),
  466. lights_phong_fragment: [
  467. "vec3 normal = normalize( vNormal );",
  468. "vec3 viewPosition = normalize( vViewPosition );",
  469. "#ifdef DOUBLE_SIDED",
  470. "normal = normal * ( -1.0 + 2.0 * float( gl_FrontFacing ) );",
  471. "#endif",
  472. "#ifdef USE_NORMALMAP",
  473. "normal = perturbNormal2Arb( -vViewPosition, normal );",
  474. "#elif defined( USE_BUMPMAP )",
  475. "normal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd() );",
  476. "#endif",
  477. "#if MAX_POINT_LIGHTS > 0",
  478. "vec3 pointDiffuse = vec3( 0.0 );",
  479. "vec3 pointSpecular = vec3( 0.0 );",
  480. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  481. "#ifdef PHONG_PER_PIXEL",
  482. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  483. "vec3 lVector = lPosition.xyz + vViewPosition.xyz;",
  484. "float lDistance = 1.0;",
  485. "if ( pointLightDistance[ i ] > 0.0 )",
  486. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  487. "lVector = normalize( lVector );",
  488. "#else",
  489. "vec3 lVector = normalize( vPointLight[ i ].xyz );",
  490. "float lDistance = vPointLight[ i ].w;",
  491. "#endif",
  492. // diffuse
  493. "float dotProduct = dot( normal, lVector );",
  494. "#ifdef WRAP_AROUND",
  495. "float pointDiffuseWeightFull = max( dotProduct, 0.0 );",
  496. "float pointDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );",
  497. "vec3 pointDiffuseWeight = mix( vec3 ( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );",
  498. "#else",
  499. "float pointDiffuseWeight = max( dotProduct, 0.0 );",
  500. "#endif",
  501. "pointDiffuse += diffuse * pointLightColor[ i ] * pointDiffuseWeight * lDistance;",
  502. // specular
  503. "vec3 pointHalfVector = normalize( lVector + viewPosition );",
  504. "float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
  505. "float pointSpecularWeight = specularStrength * max( pow( pointDotNormalHalf, shininess ), 0.0 );",
  506. "#ifdef PHYSICALLY_BASED_SHADING",
  507. // 2.0 => 2.0001 is hack to work around ANGLE bug
  508. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  509. "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVector, pointHalfVector ), 5.0 );",
  510. "pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * lDistance * specularNormalization;",
  511. "#else",
  512. "pointSpecular += specular * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * lDistance;",
  513. "#endif",
  514. "}",
  515. "#endif",
  516. "#if MAX_SPOT_LIGHTS > 0",
  517. "vec3 spotDiffuse = vec3( 0.0 );",
  518. "vec3 spotSpecular = vec3( 0.0 );",
  519. "for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {",
  520. "#ifdef PHONG_PER_PIXEL",
  521. "vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );",
  522. "vec3 lVector = lPosition.xyz + vViewPosition.xyz;",
  523. "float lDistance = 1.0;",
  524. "if ( spotLightDistance[ i ] > 0.0 )",
  525. "lDistance = 1.0 - min( ( length( lVector ) / spotLightDistance[ i ] ), 1.0 );",
  526. "lVector = normalize( lVector );",
  527. "#else",
  528. "vec3 lVector = normalize( vSpotLight[ i ].xyz );",
  529. "float lDistance = vSpotLight[ i ].w;",
  530. "#endif",
  531. "float spotEffect = dot( spotLightDirection[ i ], normalize( spotLightPosition[ i ] - vWorldPosition ) );",
  532. "if ( spotEffect > spotLightAngleCos[ i ] ) {",
  533. "spotEffect = max( pow( spotEffect, spotLightExponent[ i ] ), 0.0 );",
  534. // diffuse
  535. "float dotProduct = dot( normal, lVector );",
  536. "#ifdef WRAP_AROUND",
  537. "float spotDiffuseWeightFull = max( dotProduct, 0.0 );",
  538. "float spotDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );",
  539. "vec3 spotDiffuseWeight = mix( vec3 ( spotDiffuseWeightFull ), vec3( spotDiffuseWeightHalf ), wrapRGB );",
  540. "#else",
  541. "float spotDiffuseWeight = max( dotProduct, 0.0 );",
  542. "#endif",
  543. "spotDiffuse += diffuse * spotLightColor[ i ] * spotDiffuseWeight * lDistance * spotEffect;",
  544. // specular
  545. "vec3 spotHalfVector = normalize( lVector + viewPosition );",
  546. "float spotDotNormalHalf = max( dot( normal, spotHalfVector ), 0.0 );",
  547. "float spotSpecularWeight = specularStrength * max( pow( spotDotNormalHalf, shininess ), 0.0 );",
  548. "#ifdef PHYSICALLY_BASED_SHADING",
  549. // 2.0 => 2.0001 is hack to work around ANGLE bug
  550. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  551. "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVector, spotHalfVector ), 5.0 );",
  552. "spotSpecular += schlick * spotLightColor[ i ] * spotSpecularWeight * spotDiffuseWeight * lDistance * specularNormalization * spotEffect;",
  553. "#else",
  554. "spotSpecular += specular * spotLightColor[ i ] * spotSpecularWeight * spotDiffuseWeight * lDistance * spotEffect;",
  555. "#endif",
  556. "}",
  557. "}",
  558. "#endif",
  559. "#if MAX_DIR_LIGHTS > 0",
  560. "vec3 dirDiffuse = vec3( 0.0 );",
  561. "vec3 dirSpecular = vec3( 0.0 );" ,
  562. "for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {",
  563. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  564. "vec3 dirVector = normalize( lDirection.xyz );",
  565. // diffuse
  566. "float dotProduct = dot( normal, dirVector );",
  567. "#ifdef WRAP_AROUND",
  568. "float dirDiffuseWeightFull = max( dotProduct, 0.0 );",
  569. "float dirDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );",
  570. "vec3 dirDiffuseWeight = mix( vec3( dirDiffuseWeightFull ), vec3( dirDiffuseWeightHalf ), wrapRGB );",
  571. "#else",
  572. "float dirDiffuseWeight = max( dotProduct, 0.0 );",
  573. "#endif",
  574. "dirDiffuse += diffuse * directionalLightColor[ i ] * dirDiffuseWeight;",
  575. // specular
  576. "vec3 dirHalfVector = normalize( dirVector + viewPosition );",
  577. "float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
  578. "float dirSpecularWeight = specularStrength * max( pow( dirDotNormalHalf, shininess ), 0.0 );",
  579. "#ifdef PHYSICALLY_BASED_SHADING",
  580. /*
  581. // fresnel term from skin shader
  582. "const float F0 = 0.128;",
  583. "float base = 1.0 - dot( viewPosition, dirHalfVector );",
  584. "float exponential = pow( base, 5.0 );",
  585. "float fresnel = exponential + F0 * ( 1.0 - exponential );",
  586. */
  587. /*
  588. // fresnel term from fresnel shader
  589. "const float mFresnelBias = 0.08;",
  590. "const float mFresnelScale = 0.3;",
  591. "const float mFresnelPower = 5.0;",
  592. "float fresnel = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( -viewPosition ), normal ), mFresnelPower );",
  593. */
  594. // 2.0 => 2.0001 is hack to work around ANGLE bug
  595. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  596. //"dirSpecular += specular * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization * fresnel;",
  597. "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( dirVector, dirHalfVector ), 5.0 );",
  598. "dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;",
  599. "#else",
  600. "dirSpecular += specular * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight;",
  601. "#endif",
  602. "}",
  603. "#endif",
  604. "#if MAX_HEMI_LIGHTS > 0",
  605. "vec3 hemiDiffuse = vec3( 0.0 );",
  606. "vec3 hemiSpecular = vec3( 0.0 );" ,
  607. "for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {",
  608. "vec4 lDirection = viewMatrix * vec4( hemisphereLightDirection[ i ], 0.0 );",
  609. "vec3 lVector = normalize( lDirection.xyz );",
  610. // diffuse
  611. "float dotProduct = dot( normal, lVector );",
  612. "float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
  613. "vec3 hemiColor = mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
  614. "hemiDiffuse += diffuse * hemiColor;",
  615. // specular (sky light)
  616. "vec3 hemiHalfVectorSky = normalize( lVector + viewPosition );",
  617. "float hemiDotNormalHalfSky = 0.5 * dot( normal, hemiHalfVectorSky ) + 0.5;",
  618. "float hemiSpecularWeightSky = specularStrength * max( pow( hemiDotNormalHalfSky, shininess ), 0.0 );",
  619. // specular (ground light)
  620. "vec3 lVectorGround = -lVector;",
  621. "vec3 hemiHalfVectorGround = normalize( lVectorGround + viewPosition );",
  622. "float hemiDotNormalHalfGround = 0.5 * dot( normal, hemiHalfVectorGround ) + 0.5;",
  623. "float hemiSpecularWeightGround = specularStrength * max( pow( hemiDotNormalHalfGround, shininess ), 0.0 );",
  624. "#ifdef PHYSICALLY_BASED_SHADING",
  625. "float dotProductGround = dot( normal, lVectorGround );",
  626. // 2.0 => 2.0001 is hack to work around ANGLE bug
  627. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  628. "vec3 schlickSky = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVector, hemiHalfVectorSky ), 5.0 );",
  629. "vec3 schlickGround = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVectorGround, hemiHalfVectorGround ), 5.0 );",
  630. "hemiSpecular += hemiColor * specularNormalization * ( schlickSky * hemiSpecularWeightSky * max( dotProduct, 0.0 ) + schlickGround * hemiSpecularWeightGround * max( dotProductGround, 0.0 ) );",
  631. "#else",
  632. "hemiSpecular += specular * hemiColor * ( hemiSpecularWeightSky + hemiSpecularWeightGround ) * hemiDiffuseWeight;",
  633. "#endif",
  634. "}",
  635. "#endif",
  636. "vec3 totalDiffuse = vec3( 0.0 );",
  637. "vec3 totalSpecular = vec3( 0.0 );",
  638. "#if MAX_DIR_LIGHTS > 0",
  639. "totalDiffuse += dirDiffuse;",
  640. "totalSpecular += dirSpecular;",
  641. "#endif",
  642. "#if MAX_HEMI_LIGHTS > 0",
  643. "totalDiffuse += hemiDiffuse;",
  644. "totalSpecular += hemiSpecular;",
  645. "#endif",
  646. "#if MAX_POINT_LIGHTS > 0",
  647. "totalDiffuse += pointDiffuse;",
  648. "totalSpecular += pointSpecular;",
  649. "#endif",
  650. "#if MAX_SPOT_LIGHTS > 0",
  651. "totalDiffuse += spotDiffuse;",
  652. "totalSpecular += spotSpecular;",
  653. "#endif",
  654. "#ifdef METAL",
  655. "gl_FragColor.xyz = gl_FragColor.xyz * ( emissive + totalDiffuse + ambientLightColor * ambient + totalSpecular );",
  656. "#else",
  657. "gl_FragColor.xyz = gl_FragColor.xyz * ( emissive + totalDiffuse + ambientLightColor * ambient ) + totalSpecular;",
  658. "#endif"
  659. ].join("\n"),
  660. // VERTEX COLORS
  661. color_pars_fragment: [
  662. "#ifdef USE_COLOR",
  663. "varying vec3 vColor;",
  664. "#endif"
  665. ].join("\n"),
  666. color_fragment: [
  667. "#ifdef USE_COLOR",
  668. "gl_FragColor = gl_FragColor * vec4( vColor, opacity );",
  669. "#endif"
  670. ].join("\n"),
  671. color_pars_vertex: [
  672. "#ifdef USE_COLOR",
  673. "varying vec3 vColor;",
  674. "#endif"
  675. ].join("\n"),
  676. color_vertex: [
  677. "#ifdef USE_COLOR",
  678. "#ifdef GAMMA_INPUT",
  679. "vColor = color * color;",
  680. "#else",
  681. "vColor = color;",
  682. "#endif",
  683. "#endif"
  684. ].join("\n"),
  685. // SKINNING
  686. skinning_pars_vertex: [
  687. "#ifdef USE_SKINNING",
  688. "#ifdef BONE_TEXTURE",
  689. "uniform sampler2D boneTexture;",
  690. "mat4 getBoneMatrix( const in float i ) {",
  691. "float j = i * 4.0;",
  692. "float x = mod( j, N_BONE_PIXEL_X );",
  693. "float y = floor( j / N_BONE_PIXEL_X );",
  694. "const float dx = 1.0 / N_BONE_PIXEL_X;",
  695. "const float dy = 1.0 / N_BONE_PIXEL_Y;",
  696. "y = dy * ( y + 0.5 );",
  697. "vec4 v1 = texture2D( boneTexture, vec2( dx * ( x + 0.5 ), y ) );",
  698. "vec4 v2 = texture2D( boneTexture, vec2( dx * ( x + 1.5 ), y ) );",
  699. "vec4 v3 = texture2D( boneTexture, vec2( dx * ( x + 2.5 ), y ) );",
  700. "vec4 v4 = texture2D( boneTexture, vec2( dx * ( x + 3.5 ), y ) );",
  701. "mat4 bone = mat4( v1, v2, v3, v4 );",
  702. "return bone;",
  703. "}",
  704. "#else",
  705. "uniform mat4 boneGlobalMatrices[ MAX_BONES ];",
  706. "mat4 getBoneMatrix( const in float i ) {",
  707. "mat4 bone = boneGlobalMatrices[ int(i) ];",
  708. "return bone;",
  709. "}",
  710. "#endif",
  711. "#endif"
  712. ].join("\n"),
  713. skinbase_vertex: [
  714. "#ifdef USE_SKINNING",
  715. "mat4 boneMatX = getBoneMatrix( skinIndex.x );",
  716. "mat4 boneMatY = getBoneMatrix( skinIndex.y );",
  717. "#endif"
  718. ].join("\n"),
  719. skinning_vertex: [
  720. "#ifdef USE_SKINNING",
  721. "#ifdef USE_MORPHTARGETS",
  722. "vec4 skinVertex = vec4( morphed, 1.0 );",
  723. "#else",
  724. "vec4 skinVertex = vec4( position, 1.0 );",
  725. "#endif",
  726. "vec4 skinned = boneMatX * skinVertex * skinWeight.x;",
  727. "skinned += boneMatY * skinVertex * skinWeight.y;",
  728. "#endif"
  729. ].join("\n"),
  730. // MORPHING
  731. morphtarget_pars_vertex: [
  732. "#ifdef USE_MORPHTARGETS",
  733. "#ifndef USE_MORPHNORMALS",
  734. "uniform float morphTargetInfluences[ 8 ];",
  735. "#else",
  736. "uniform float morphTargetInfluences[ 4 ];",
  737. "#endif",
  738. "#endif"
  739. ].join("\n"),
  740. morphtarget_vertex: [
  741. "#ifdef USE_MORPHTARGETS",
  742. "vec3 morphed = vec3( 0.0 );",
  743. "morphed += ( morphTarget0 - position ) * morphTargetInfluences[ 0 ];",
  744. "morphed += ( morphTarget1 - position ) * morphTargetInfluences[ 1 ];",
  745. "morphed += ( morphTarget2 - position ) * morphTargetInfluences[ 2 ];",
  746. "morphed += ( morphTarget3 - position ) * morphTargetInfluences[ 3 ];",
  747. "#ifndef USE_MORPHNORMALS",
  748. "morphed += ( morphTarget4 - position ) * morphTargetInfluences[ 4 ];",
  749. "morphed += ( morphTarget5 - position ) * morphTargetInfluences[ 5 ];",
  750. "morphed += ( morphTarget6 - position ) * morphTargetInfluences[ 6 ];",
  751. "morphed += ( morphTarget7 - position ) * morphTargetInfluences[ 7 ];",
  752. "#endif",
  753. "morphed += position;",
  754. "#endif"
  755. ].join("\n"),
  756. default_vertex : [
  757. "vec4 mvPosition;",
  758. "#ifdef USE_SKINNING",
  759. "mvPosition = modelViewMatrix * skinned;",
  760. "#endif",
  761. "#if !defined( USE_SKINNING ) && defined( USE_MORPHTARGETS )",
  762. "mvPosition = modelViewMatrix * vec4( morphed, 1.0 );",
  763. "#endif",
  764. "#if !defined( USE_SKINNING ) && ! defined( USE_MORPHTARGETS )",
  765. "mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  766. "#endif",
  767. "gl_Position = projectionMatrix * mvPosition;"
  768. ].join("\n"),
  769. morphnormal_vertex: [
  770. "#ifdef USE_MORPHNORMALS",
  771. "vec3 morphedNormal = vec3( 0.0 );",
  772. "morphedNormal += ( morphNormal0 - normal ) * morphTargetInfluences[ 0 ];",
  773. "morphedNormal += ( morphNormal1 - normal ) * morphTargetInfluences[ 1 ];",
  774. "morphedNormal += ( morphNormal2 - normal ) * morphTargetInfluences[ 2 ];",
  775. "morphedNormal += ( morphNormal3 - normal ) * morphTargetInfluences[ 3 ];",
  776. "morphedNormal += normal;",
  777. "#endif"
  778. ].join("\n"),
  779. skinnormal_vertex: [
  780. "#ifdef USE_SKINNING",
  781. "mat4 skinMatrix = skinWeight.x * boneMatX;",
  782. "skinMatrix += skinWeight.y * boneMatY;",
  783. "#ifdef USE_MORPHNORMALS",
  784. "vec4 skinnedNormal = skinMatrix * vec4( morphedNormal, 0.0 );",
  785. "#else",
  786. "vec4 skinnedNormal = skinMatrix * vec4( normal, 0.0 );",
  787. "#endif",
  788. "#endif"
  789. ].join("\n"),
  790. defaultnormal_vertex: [
  791. "vec3 objectNormal;",
  792. "#ifdef USE_SKINNING",
  793. "objectNormal = skinnedNormal.xyz;",
  794. "#endif",
  795. "#if !defined( USE_SKINNING ) && defined( USE_MORPHNORMALS )",
  796. "objectNormal = morphedNormal;",
  797. "#endif",
  798. "#if !defined( USE_SKINNING ) && ! defined( USE_MORPHNORMALS )",
  799. "objectNormal = normal;",
  800. "#endif",
  801. "#ifdef FLIP_SIDED",
  802. "objectNormal = -objectNormal;",
  803. "#endif",
  804. "vec3 transformedNormal = normalMatrix * objectNormal;"
  805. ].join("\n"),
  806. // SHADOW MAP
  807. // based on SpiderGL shadow map and Fabien Sanglard's GLSL shadow mapping examples
  808. // http://spidergl.org/example.php?id=6
  809. // http://fabiensanglard.net/shadowmapping
  810. shadowmap_pars_fragment: [
  811. "#ifdef USE_SHADOWMAP",
  812. "uniform sampler2D shadowMap[ MAX_SHADOWS ];",
  813. "uniform vec2 shadowMapSize[ MAX_SHADOWS ];",
  814. "uniform float shadowDarkness[ MAX_SHADOWS ];",
  815. "uniform float shadowBias[ MAX_SHADOWS ];",
  816. "varying vec4 vShadowCoord[ MAX_SHADOWS ];",
  817. "float unpackDepth( const in vec4 rgba_depth ) {",
  818. "const vec4 bit_shift = vec4( 1.0 / ( 256.0 * 256.0 * 256.0 ), 1.0 / ( 256.0 * 256.0 ), 1.0 / 256.0, 1.0 );",
  819. "float depth = dot( rgba_depth, bit_shift );",
  820. "return depth;",
  821. "}",
  822. "#endif"
  823. ].join("\n"),
  824. shadowmap_fragment: [
  825. "#ifdef USE_SHADOWMAP",
  826. "#ifdef SHADOWMAP_DEBUG",
  827. "vec3 frustumColors[3];",
  828. "frustumColors[0] = vec3( 1.0, 0.5, 0.0 );",
  829. "frustumColors[1] = vec3( 0.0, 1.0, 0.8 );",
  830. "frustumColors[2] = vec3( 0.0, 0.5, 1.0 );",
  831. "#endif",
  832. "#ifdef SHADOWMAP_CASCADE",
  833. "int inFrustumCount = 0;",
  834. "#endif",
  835. "float fDepth;",
  836. "vec3 shadowColor = vec3( 1.0 );",
  837. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  838. "vec3 shadowCoord = vShadowCoord[ i ].xyz / vShadowCoord[ i ].w;",
  839. // "if ( something && something )" breaks ATI OpenGL shader compiler
  840. // "if ( all( something, something ) )" using this instead
  841. "bvec4 inFrustumVec = bvec4 ( shadowCoord.x >= 0.0, shadowCoord.x <= 1.0, shadowCoord.y >= 0.0, shadowCoord.y <= 1.0 );",
  842. "bool inFrustum = all( inFrustumVec );",
  843. // don't shadow pixels outside of light frustum
  844. // use just first frustum (for cascades)
  845. // don't shadow pixels behind far plane of light frustum
  846. "#ifdef SHADOWMAP_CASCADE",
  847. "inFrustumCount += int( inFrustum );",
  848. "bvec3 frustumTestVec = bvec3( inFrustum, inFrustumCount == 1, shadowCoord.z <= 1.0 );",
  849. "#else",
  850. "bvec2 frustumTestVec = bvec2( inFrustum, shadowCoord.z <= 1.0 );",
  851. "#endif",
  852. "bool frustumTest = all( frustumTestVec );",
  853. "if ( frustumTest ) {",
  854. "shadowCoord.z += shadowBias[ i ];",
  855. "#if defined( SHADOWMAP_TYPE_PCF )",
  856. // Percentage-close filtering
  857. // (9 pixel kernel)
  858. // http://fabiensanglard.net/shadowmappingPCF/
  859. "float shadow = 0.0;",
  860. /*
  861. // nested loops breaks shader compiler / validator on some ATI cards when using OpenGL
  862. // must enroll loop manually
  863. "for ( float y = -1.25; y <= 1.25; y += 1.25 )",
  864. "for ( float x = -1.25; x <= 1.25; x += 1.25 ) {",
  865. "vec4 rgbaDepth = texture2D( shadowMap[ i ], vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy );",
  866. // doesn't seem to produce any noticeable visual difference compared to simple "texture2D" lookup
  867. //"vec4 rgbaDepth = texture2DProj( shadowMap[ i ], vec4( vShadowCoord[ i ].w * ( vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy ), 0.05, vShadowCoord[ i ].w ) );",
  868. "float fDepth = unpackDepth( rgbaDepth );",
  869. "if ( fDepth < shadowCoord.z )",
  870. "shadow += 1.0;",
  871. "}",
  872. "shadow /= 9.0;",
  873. */
  874. "const float shadowDelta = 1.0 / 9.0;",
  875. "float xPixelOffset = 1.0 / shadowMapSize[ i ].x;",
  876. "float yPixelOffset = 1.0 / shadowMapSize[ i ].y;",
  877. "float dx0 = -1.25 * xPixelOffset;",
  878. "float dy0 = -1.25 * yPixelOffset;",
  879. "float dx1 = 1.25 * xPixelOffset;",
  880. "float dy1 = 1.25 * yPixelOffset;",
  881. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy0 ) ) );",
  882. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  883. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy0 ) ) );",
  884. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  885. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy0 ) ) );",
  886. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  887. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, 0.0 ) ) );",
  888. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  889. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy ) );",
  890. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  891. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, 0.0 ) ) );",
  892. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  893. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy1 ) ) );",
  894. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  895. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy1 ) ) );",
  896. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  897. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy1 ) ) );",
  898. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  899. "shadowColor = shadowColor * vec3( ( 1.0 - shadowDarkness[ i ] * shadow ) );",
  900. "#elif defined( SHADOWMAP_TYPE_PCF_SOFT )",
  901. // Percentage-close filtering
  902. // (9 pixel kernel)
  903. // http://fabiensanglard.net/shadowmappingPCF/
  904. "float shadow = 0.0;",
  905. "float xPixelOffset = 1.0 / shadowMapSize[ i ].x;",
  906. "float yPixelOffset = 1.0 / shadowMapSize[ i ].y;",
  907. "float dx0 = -1.0 * xPixelOffset;",
  908. "float dy0 = -1.0 * yPixelOffset;",
  909. "float dx1 = 1.0 * xPixelOffset;",
  910. "float dy1 = 1.0 * yPixelOffset;",
  911. "mat3 shadowKernel;",
  912. "mat3 depthKernel;",
  913. "depthKernel[0][0] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy0 ) ) );",
  914. "depthKernel[0][1] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, 0.0 ) ) );",
  915. "depthKernel[0][2] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy1 ) ) );",
  916. "depthKernel[1][0] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy0 ) ) );",
  917. "depthKernel[1][1] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy ) );",
  918. "depthKernel[1][2] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy1 ) ) );",
  919. "depthKernel[2][0] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy0 ) ) );",
  920. "depthKernel[2][1] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, 0.0 ) ) );",
  921. "depthKernel[2][2] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy1 ) ) );",
  922. "vec3 shadowZ = vec3( shadowCoord.z );",
  923. "shadowKernel[0] = vec3(lessThan(depthKernel[0], shadowZ ));",
  924. "shadowKernel[0] *= vec3(0.25);",
  925. "shadowKernel[1] = vec3(lessThan(depthKernel[1], shadowZ ));",
  926. "shadowKernel[1] *= vec3(0.25);",
  927. "shadowKernel[2] = vec3(lessThan(depthKernel[2], shadowZ ));",
  928. "shadowKernel[2] *= vec3(0.25);",
  929. "vec2 fractionalCoord = 1.0 - fract( shadowCoord.xy * shadowMapSize[i].xy );",
  930. "shadowKernel[0] = mix( shadowKernel[1], shadowKernel[0], fractionalCoord.x );",
  931. "shadowKernel[1] = mix( shadowKernel[2], shadowKernel[1], fractionalCoord.x );",
  932. "vec4 shadowValues;",
  933. "shadowValues.x = mix( shadowKernel[0][1], shadowKernel[0][0], fractionalCoord.y );",
  934. "shadowValues.y = mix( shadowKernel[0][2], shadowKernel[0][1], fractionalCoord.y );",
  935. "shadowValues.z = mix( shadowKernel[1][1], shadowKernel[1][0], fractionalCoord.y );",
  936. "shadowValues.w = mix( shadowKernel[1][2], shadowKernel[1][1], fractionalCoord.y );",
  937. "shadow = dot( shadowValues, vec4( 1.0 ) );",
  938. "shadowColor = shadowColor * vec3( ( 1.0 - shadowDarkness[ i ] * shadow ) );",
  939. "#else",
  940. "vec4 rgbaDepth = texture2D( shadowMap[ i ], shadowCoord.xy );",
  941. "float fDepth = unpackDepth( rgbaDepth );",
  942. "if ( fDepth < shadowCoord.z )",
  943. // spot with multiple shadows is darker
  944. "shadowColor = shadowColor * vec3( 1.0 - shadowDarkness[ i ] );",
  945. // spot with multiple shadows has the same color as single shadow spot
  946. //"shadowColor = min( shadowColor, vec3( shadowDarkness[ i ] ) );",
  947. "#endif",
  948. "}",
  949. "#ifdef SHADOWMAP_DEBUG",
  950. "#ifdef SHADOWMAP_CASCADE",
  951. "if ( inFrustum && inFrustumCount == 1 ) gl_FragColor.xyz *= frustumColors[ i ];",
  952. "#else",
  953. "if ( inFrustum ) gl_FragColor.xyz *= frustumColors[ i ];",
  954. "#endif",
  955. "#endif",
  956. "}",
  957. "#ifdef GAMMA_OUTPUT",
  958. "shadowColor *= shadowColor;",
  959. "#endif",
  960. "gl_FragColor.xyz = gl_FragColor.xyz * shadowColor;",
  961. "#endif"
  962. ].join("\n"),
  963. shadowmap_pars_vertex: [
  964. "#ifdef USE_SHADOWMAP",
  965. "varying vec4 vShadowCoord[ MAX_SHADOWS ];",
  966. "uniform mat4 shadowMatrix[ MAX_SHADOWS ];",
  967. "#endif"
  968. ].join("\n"),
  969. shadowmap_vertex: [
  970. "#ifdef USE_SHADOWMAP",
  971. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  972. "vShadowCoord[ i ] = shadowMatrix[ i ] * worldPosition;",
  973. "}",
  974. "#endif"
  975. ].join("\n"),
  976. // ALPHATEST
  977. alphatest_fragment: [
  978. "#ifdef ALPHATEST",
  979. "if ( gl_FragColor.a < ALPHATEST ) discard;",
  980. "#endif"
  981. ].join("\n"),
  982. // LINEAR SPACE
  983. linear_to_gamma_fragment: [
  984. "#ifdef GAMMA_OUTPUT",
  985. "gl_FragColor.xyz = sqrt( gl_FragColor.xyz );",
  986. "#endif"
  987. ].join("\n")
  988. };
  989. THREE.UniformsUtils = {
  990. merge: function ( uniforms ) {
  991. var u, p, tmp, merged = {};
  992. for ( u = 0; u < uniforms.length; u ++ ) {
  993. tmp = this.clone( uniforms[ u ] );
  994. for ( p in tmp ) {
  995. merged[ p ] = tmp[ p ];
  996. }
  997. }
  998. return merged;
  999. },
  1000. clone: function ( uniforms_src ) {
  1001. var u, p, parameter, parameter_src, uniforms_dst = {};
  1002. for ( u in uniforms_src ) {
  1003. uniforms_dst[ u ] = {};
  1004. for ( p in uniforms_src[ u ] ) {
  1005. parameter_src = uniforms_src[ u ][ p ];
  1006. if ( parameter_src instanceof THREE.Color ||
  1007. parameter_src instanceof THREE.Vector2 ||
  1008. parameter_src instanceof THREE.Vector3 ||
  1009. parameter_src instanceof THREE.Vector4 ||
  1010. parameter_src instanceof THREE.Matrix4 ||
  1011. parameter_src instanceof THREE.Texture ) {
  1012. uniforms_dst[ u ][ p ] = parameter_src.clone();
  1013. } else if ( parameter_src instanceof Array ) {
  1014. uniforms_dst[ u ][ p ] = parameter_src.slice();
  1015. } else {
  1016. uniforms_dst[ u ][ p ] = parameter_src;
  1017. }
  1018. }
  1019. }
  1020. return uniforms_dst;
  1021. }
  1022. };
  1023. THREE.UniformsLib = {
  1024. common: {
  1025. "diffuse" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  1026. "opacity" : { type: "f", value: 1.0 },
  1027. "map" : { type: "t", value: null },
  1028. "offsetRepeat" : { type: "v4", value: new THREE.Vector4( 0, 0, 1, 1 ) },
  1029. "lightMap" : { type: "t", value: null },
  1030. "specularMap" : { type: "t", value: null },
  1031. "envMap" : { type: "t", value: null },
  1032. "flipEnvMap" : { type: "f", value: -1 },
  1033. "useRefract" : { type: "i", value: 0 },
  1034. "reflectivity" : { type: "f", value: 1.0 },
  1035. "refractionRatio" : { type: "f", value: 0.98 },
  1036. "combine" : { type: "i", value: 0 },
  1037. "morphTargetInfluences" : { type: "f", value: 0 }
  1038. },
  1039. bump: {
  1040. "bumpMap" : { type: "t", value: null },
  1041. "bumpScale" : { type: "f", value: 1 }
  1042. },
  1043. normalmap: {
  1044. "normalMap" : { type: "t", value: null },
  1045. "normalScale" : { type: "v2", value: new THREE.Vector2( 1, 1 ) }
  1046. },
  1047. fog : {
  1048. "fogDensity" : { type: "f", value: 0.00025 },
  1049. "fogNear" : { type: "f", value: 1 },
  1050. "fogFar" : { type: "f", value: 2000 },
  1051. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  1052. },
  1053. lights: {
  1054. "ambientLightColor" : { type: "fv", value: [] },
  1055. "directionalLightDirection" : { type: "fv", value: [] },
  1056. "directionalLightColor" : { type: "fv", value: [] },
  1057. "hemisphereLightDirection" : { type: "fv", value: [] },
  1058. "hemisphereLightSkyColor" : { type: "fv", value: [] },
  1059. "hemisphereLightGroundColor" : { type: "fv", value: [] },
  1060. "pointLightColor" : { type: "fv", value: [] },
  1061. "pointLightPosition" : { type: "fv", value: [] },
  1062. "pointLightDistance" : { type: "fv1", value: [] },
  1063. "spotLightColor" : { type: "fv", value: [] },
  1064. "spotLightPosition" : { type: "fv", value: [] },
  1065. "spotLightDirection" : { type: "fv", value: [] },
  1066. "spotLightDistance" : { type: "fv1", value: [] },
  1067. "spotLightAngleCos" : { type: "fv1", value: [] },
  1068. "spotLightExponent" : { type: "fv1", value: [] }
  1069. },
  1070. particle: {
  1071. "psColor" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  1072. "opacity" : { type: "f", value: 1.0 },
  1073. "size" : { type: "f", value: 1.0 },
  1074. "scale" : { type: "f", value: 1.0 },
  1075. "map" : { type: "t", value: null },
  1076. "fogDensity" : { type: "f", value: 0.00025 },
  1077. "fogNear" : { type: "f", value: 1 },
  1078. "fogFar" : { type: "f", value: 2000 },
  1079. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  1080. },
  1081. shadowmap: {
  1082. "shadowMap": { type: "tv", value: [] },
  1083. "shadowMapSize": { type: "v2v", value: [] },
  1084. "shadowBias" : { type: "fv1", value: [] },
  1085. "shadowDarkness": { type: "fv1", value: [] },
  1086. "shadowMatrix" : { type: "m4v", value: [] }
  1087. }
  1088. };
  1089. THREE.ShaderLib = {
  1090. 'basic': {
  1091. uniforms: THREE.UniformsUtils.merge( [
  1092. THREE.UniformsLib[ "common" ],
  1093. THREE.UniformsLib[ "fog" ],
  1094. THREE.UniformsLib[ "shadowmap" ]
  1095. ] ),
  1096. vertexShader: [
  1097. THREE.ShaderChunk[ "map_pars_vertex" ],
  1098. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  1099. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  1100. THREE.ShaderChunk[ "color_pars_vertex" ],
  1101. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  1102. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  1103. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  1104. "void main() {",
  1105. THREE.ShaderChunk[ "map_vertex" ],
  1106. THREE.ShaderChunk[ "lightmap_vertex" ],
  1107. THREE.ShaderChunk[ "color_vertex" ],
  1108. THREE.ShaderChunk[ "skinbase_vertex" ],
  1109. "#ifdef USE_ENVMAP",
  1110. THREE.ShaderChunk[ "morphnormal_vertex" ],
  1111. THREE.ShaderChunk[ "skinnormal_vertex" ],
  1112. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  1113. "#endif",
  1114. THREE.ShaderChunk[ "morphtarget_vertex" ],
  1115. THREE.ShaderChunk[ "skinning_vertex" ],
  1116. THREE.ShaderChunk[ "default_vertex" ],
  1117. THREE.ShaderChunk[ "worldpos_vertex" ],
  1118. THREE.ShaderChunk[ "envmap_vertex" ],
  1119. THREE.ShaderChunk[ "shadowmap_vertex" ],
  1120. "}"
  1121. ].join("\n"),
  1122. fragmentShader: [
  1123. "uniform vec3 diffuse;",
  1124. "uniform float opacity;",
  1125. THREE.ShaderChunk[ "color_pars_fragment" ],
  1126. THREE.ShaderChunk[ "map_pars_fragment" ],
  1127. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  1128. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  1129. THREE.ShaderChunk[ "fog_pars_fragment" ],
  1130. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  1131. THREE.ShaderChunk[ "specularmap_pars_fragment" ],
  1132. "void main() {",
  1133. "gl_FragColor = vec4( diffuse, opacity );",
  1134. THREE.ShaderChunk[ "map_fragment" ],
  1135. THREE.ShaderChunk[ "alphatest_fragment" ],
  1136. THREE.ShaderChunk[ "specularmap_fragment" ],
  1137. THREE.ShaderChunk[ "lightmap_fragment" ],
  1138. THREE.ShaderChunk[ "color_fragment" ],
  1139. THREE.ShaderChunk[ "envmap_fragment" ],
  1140. THREE.ShaderChunk[ "shadowmap_fragment" ],
  1141. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  1142. THREE.ShaderChunk[ "fog_fragment" ],
  1143. "}"
  1144. ].join("\n")
  1145. },
  1146. 'lambert': {
  1147. uniforms: THREE.UniformsUtils.merge( [
  1148. THREE.UniformsLib[ "common" ],
  1149. THREE.UniformsLib[ "fog" ],
  1150. THREE.UniformsLib[ "lights" ],
  1151. THREE.UniformsLib[ "shadowmap" ],
  1152. {
  1153. "ambient" : { type: "c", value: new THREE.Color( 0xffffff ) },
  1154. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  1155. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  1156. }
  1157. ] ),
  1158. vertexShader: [
  1159. "#define LAMBERT",
  1160. "varying vec3 vLightFront;",
  1161. "#ifdef DOUBLE_SIDED",
  1162. "varying vec3 vLightBack;",
  1163. "#endif",
  1164. THREE.ShaderChunk[ "map_pars_vertex" ],
  1165. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  1166. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  1167. THREE.ShaderChunk[ "lights_lambert_pars_vertex" ],
  1168. THREE.ShaderChunk[ "color_pars_vertex" ],
  1169. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  1170. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  1171. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  1172. "void main() {",
  1173. THREE.ShaderChunk[ "map_vertex" ],
  1174. THREE.ShaderChunk[ "lightmap_vertex" ],
  1175. THREE.ShaderChunk[ "color_vertex" ],
  1176. THREE.ShaderChunk[ "morphnormal_vertex" ],
  1177. THREE.ShaderChunk[ "skinbase_vertex" ],
  1178. THREE.ShaderChunk[ "skinnormal_vertex" ],
  1179. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  1180. THREE.ShaderChunk[ "morphtarget_vertex" ],
  1181. THREE.ShaderChunk[ "skinning_vertex" ],
  1182. THREE.ShaderChunk[ "default_vertex" ],
  1183. THREE.ShaderChunk[ "worldpos_vertex" ],
  1184. THREE.ShaderChunk[ "envmap_vertex" ],
  1185. THREE.ShaderChunk[ "lights_lambert_vertex" ],
  1186. THREE.ShaderChunk[ "shadowmap_vertex" ],
  1187. "}"
  1188. ].join("\n"),
  1189. fragmentShader: [
  1190. "uniform float opacity;",
  1191. "varying vec3 vLightFront;",
  1192. "#ifdef DOUBLE_SIDED",
  1193. "varying vec3 vLightBack;",
  1194. "#endif",
  1195. THREE.ShaderChunk[ "color_pars_fragment" ],
  1196. THREE.ShaderChunk[ "map_pars_fragment" ],
  1197. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  1198. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  1199. THREE.ShaderChunk[ "fog_pars_fragment" ],
  1200. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  1201. THREE.ShaderChunk[ "specularmap_pars_fragment" ],
  1202. "void main() {",
  1203. "gl_FragColor = vec4( vec3 ( 1.0 ), opacity );",
  1204. THREE.ShaderChunk[ "map_fragment" ],
  1205. THREE.ShaderChunk[ "alphatest_fragment" ],
  1206. THREE.ShaderChunk[ "specularmap_fragment" ],
  1207. "#ifdef DOUBLE_SIDED",
  1208. //"float isFront = float( gl_FrontFacing );",
  1209. //"gl_FragColor.xyz *= isFront * vLightFront + ( 1.0 - isFront ) * vLightBack;",
  1210. "if ( gl_FrontFacing )",
  1211. "gl_FragColor.xyz *= vLightFront;",
  1212. "else",
  1213. "gl_FragColor.xyz *= vLightBack;",
  1214. "#else",
  1215. "gl_FragColor.xyz *= vLightFront;",
  1216. "#endif",
  1217. THREE.ShaderChunk[ "lightmap_fragment" ],
  1218. THREE.ShaderChunk[ "color_fragment" ],
  1219. THREE.ShaderChunk[ "envmap_fragment" ],
  1220. THREE.ShaderChunk[ "shadowmap_fragment" ],
  1221. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  1222. THREE.ShaderChunk[ "fog_fragment" ],
  1223. "}"
  1224. ].join("\n")
  1225. },
  1226. 'phong': {
  1227. uniforms: THREE.UniformsUtils.merge( [
  1228. THREE.UniformsLib[ "common" ],
  1229. THREE.UniformsLib[ "bump" ],
  1230. THREE.UniformsLib[ "normalmap" ],
  1231. THREE.UniformsLib[ "fog" ],
  1232. THREE.UniformsLib[ "lights" ],
  1233. THREE.UniformsLib[ "shadowmap" ],
  1234. {
  1235. "ambient" : { type: "c", value: new THREE.Color( 0xffffff ) },
  1236. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  1237. "specular" : { type: "c", value: new THREE.Color( 0x111111 ) },
  1238. "shininess": { type: "f", value: 30 },
  1239. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  1240. }
  1241. ] ),
  1242. vertexShader: [
  1243. "#define PHONG",
  1244. "varying vec3 vViewPosition;",
  1245. "varying vec3 vNormal;",
  1246. THREE.ShaderChunk[ "map_pars_vertex" ],
  1247. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  1248. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  1249. THREE.ShaderChunk[ "lights_phong_pars_vertex" ],
  1250. THREE.ShaderChunk[ "color_pars_vertex" ],
  1251. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  1252. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  1253. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  1254. "void main() {",
  1255. THREE.ShaderChunk[ "map_vertex" ],
  1256. THREE.ShaderChunk[ "lightmap_vertex" ],
  1257. THREE.ShaderChunk[ "color_vertex" ],
  1258. THREE.ShaderChunk[ "morphnormal_vertex" ],
  1259. THREE.ShaderChunk[ "skinbase_vertex" ],
  1260. THREE.ShaderChunk[ "skinnormal_vertex" ],
  1261. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  1262. "vNormal = normalize( transformedNormal );",
  1263. THREE.ShaderChunk[ "morphtarget_vertex" ],
  1264. THREE.ShaderChunk[ "skinning_vertex" ],
  1265. THREE.ShaderChunk[ "default_vertex" ],
  1266. "vViewPosition = -mvPosition.xyz;",
  1267. THREE.ShaderChunk[ "worldpos_vertex" ],
  1268. THREE.ShaderChunk[ "envmap_vertex" ],
  1269. THREE.ShaderChunk[ "lights_phong_vertex" ],
  1270. THREE.ShaderChunk[ "shadowmap_vertex" ],
  1271. "}"
  1272. ].join("\n"),
  1273. fragmentShader: [
  1274. "uniform vec3 diffuse;",
  1275. "uniform float opacity;",
  1276. "uniform vec3 ambient;",
  1277. "uniform vec3 emissive;",
  1278. "uniform vec3 specular;",
  1279. "uniform float shininess;",
  1280. THREE.ShaderChunk[ "color_pars_fragment" ],
  1281. THREE.ShaderChunk[ "map_pars_fragment" ],
  1282. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  1283. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  1284. THREE.ShaderChunk[ "fog_pars_fragment" ],
  1285. THREE.ShaderChunk[ "lights_phong_pars_fragment" ],
  1286. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  1287. THREE.ShaderChunk[ "bumpmap_pars_fragment" ],
  1288. THREE.ShaderChunk[ "normalmap_pars_fragment" ],
  1289. THREE.ShaderChunk[ "specularmap_pars_fragment" ],
  1290. "void main() {",
  1291. "gl_FragColor = vec4( vec3 ( 1.0 ), opacity );",
  1292. THREE.ShaderChunk[ "map_fragment" ],
  1293. THREE.ShaderChunk[ "alphatest_fragment" ],
  1294. THREE.ShaderChunk[ "specularmap_fragment" ],
  1295. THREE.ShaderChunk[ "lights_phong_fragment" ],
  1296. THREE.ShaderChunk[ "lightmap_fragment" ],
  1297. THREE.ShaderChunk[ "color_fragment" ],
  1298. THREE.ShaderChunk[ "envmap_fragment" ],
  1299. THREE.ShaderChunk[ "shadowmap_fragment" ],
  1300. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  1301. THREE.ShaderChunk[ "fog_fragment" ],
  1302. "}"
  1303. ].join("\n")
  1304. },
  1305. 'particle_basic': {
  1306. uniforms: THREE.UniformsUtils.merge( [
  1307. THREE.UniformsLib[ "particle" ],
  1308. THREE.UniformsLib[ "shadowmap" ]
  1309. ] ),
  1310. vertexShader: [
  1311. "uniform float size;",
  1312. "uniform float scale;",
  1313. THREE.ShaderChunk[ "color_pars_vertex" ],
  1314. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  1315. "void main() {",
  1316. THREE.ShaderChunk[ "color_vertex" ],
  1317. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  1318. "#ifdef USE_SIZEATTENUATION",
  1319. "gl_PointSize = size * ( scale / length( mvPosition.xyz ) );",
  1320. "#else",
  1321. "gl_PointSize = size;",
  1322. "#endif",
  1323. "gl_Position = projectionMatrix * mvPosition;",
  1324. THREE.ShaderChunk[ "worldpos_vertex" ],
  1325. THREE.ShaderChunk[ "shadowmap_vertex" ],
  1326. "}"
  1327. ].join("\n"),
  1328. fragmentShader: [
  1329. "uniform vec3 psColor;",
  1330. "uniform float opacity;",
  1331. THREE.ShaderChunk[ "color_pars_fragment" ],
  1332. THREE.ShaderChunk[ "map_particle_pars_fragment" ],
  1333. THREE.ShaderChunk[ "fog_pars_fragment" ],
  1334. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  1335. "void main() {",
  1336. "gl_FragColor = vec4( psColor, opacity );",
  1337. THREE.ShaderChunk[ "map_particle_fragment" ],
  1338. THREE.ShaderChunk[ "alphatest_fragment" ],
  1339. THREE.ShaderChunk[ "color_fragment" ],
  1340. THREE.ShaderChunk[ "shadowmap_fragment" ],
  1341. THREE.ShaderChunk[ "fog_fragment" ],
  1342. "}"
  1343. ].join("\n")
  1344. },
  1345. 'dashed': {
  1346. uniforms: THREE.UniformsUtils.merge( [
  1347. THREE.UniformsLib[ "common" ],
  1348. THREE.UniformsLib[ "fog" ],
  1349. {
  1350. "scale": { type: "f", value: 1 },
  1351. "dashSize": { type: "f", value: 1 },
  1352. "totalSize": { type: "f", value: 2 }
  1353. }
  1354. ] ),
  1355. vertexShader: [
  1356. "uniform float scale;",
  1357. "attribute float lineDistance;",
  1358. "varying float vLineDistance;",
  1359. THREE.ShaderChunk[ "color_pars_vertex" ],
  1360. "void main() {",
  1361. THREE.ShaderChunk[ "color_vertex" ],
  1362. "vLineDistance = scale * lineDistance;",
  1363. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  1364. "gl_Position = projectionMatrix * mvPosition;",
  1365. "}"
  1366. ].join("\n"),
  1367. fragmentShader: [
  1368. "uniform vec3 diffuse;",
  1369. "uniform float opacity;",
  1370. "uniform float dashSize;",
  1371. "uniform float totalSize;",
  1372. "varying float vLineDistance;",
  1373. THREE.ShaderChunk[ "color_pars_fragment" ],
  1374. THREE.ShaderChunk[ "fog_pars_fragment" ],
  1375. "void main() {",
  1376. "if ( mod( vLineDistance, totalSize ) > dashSize ) {",
  1377. "discard;",
  1378. "}",
  1379. "gl_FragColor = vec4( diffuse, opacity );",
  1380. THREE.ShaderChunk[ "color_fragment" ],
  1381. THREE.ShaderChunk[ "fog_fragment" ],
  1382. "}"
  1383. ].join("\n")
  1384. },
  1385. 'depth': {
  1386. uniforms: {
  1387. "mNear": { type: "f", value: 1.0 },
  1388. "mFar" : { type: "f", value: 2000.0 },
  1389. "opacity" : { type: "f", value: 1.0 }
  1390. },
  1391. vertexShader: [
  1392. "void main() {",
  1393. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  1394. "}"
  1395. ].join("\n"),
  1396. fragmentShader: [
  1397. "uniform float mNear;",
  1398. "uniform float mFar;",
  1399. "uniform float opacity;",
  1400. "void main() {",
  1401. "float depth = gl_FragCoord.z / gl_FragCoord.w;",
  1402. "float color = 1.0 - smoothstep( mNear, mFar, depth );",
  1403. "gl_FragColor = vec4( vec3( color ), opacity );",
  1404. "}"
  1405. ].join("\n")
  1406. },
  1407. 'normal': {
  1408. uniforms: {
  1409. "opacity" : { type: "f", value: 1.0 }
  1410. },
  1411. vertexShader: [
  1412. "varying vec3 vNormal;",
  1413. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  1414. "void main() {",
  1415. "vNormal = normalize( normalMatrix * normal );",
  1416. THREE.ShaderChunk[ "morphtarget_vertex" ],
  1417. THREE.ShaderChunk[ "default_vertex" ],
  1418. "}"
  1419. ].join("\n"),
  1420. fragmentShader: [
  1421. "uniform float opacity;",
  1422. "varying vec3 vNormal;",
  1423. "void main() {",
  1424. "gl_FragColor = vec4( 0.5 * normalize( vNormal ) + 0.5, opacity );",
  1425. "}"
  1426. ].join("\n")
  1427. },
  1428. /* -------------------------------------------------------------------------
  1429. // Normal map shader
  1430. // - Blinn-Phong
  1431. // - normal + diffuse + specular + AO + displacement + reflection + shadow maps
  1432. // - point and directional lights (use with "lights: true" material option)
  1433. ------------------------------------------------------------------------- */
  1434. 'normalmap' : {
  1435. uniforms: THREE.UniformsUtils.merge( [
  1436. THREE.UniformsLib[ "fog" ],
  1437. THREE.UniformsLib[ "lights" ],
  1438. THREE.UniformsLib[ "shadowmap" ],
  1439. {
  1440. "enableAO" : { type: "i", value: 0 },
  1441. "enableDiffuse" : { type: "i", value: 0 },
  1442. "enableSpecular" : { type: "i", value: 0 },
  1443. "enableReflection": { type: "i", value: 0 },
  1444. "enableDisplacement": { type: "i", value: 0 },
  1445. "tDisplacement": { type: "t", value: null }, // must go first as this is vertex texture
  1446. "tDiffuse" : { type: "t", value: null },
  1447. "tCube" : { type: "t", value: null },
  1448. "tNormal" : { type: "t", value: null },
  1449. "tSpecular" : { type: "t", value: null },
  1450. "tAO" : { type: "t", value: null },
  1451. "uNormalScale": { type: "v2", value: new THREE.Vector2( 1, 1 ) },
  1452. "uDisplacementBias": { type: "f", value: 0.0 },
  1453. "uDisplacementScale": { type: "f", value: 1.0 },
  1454. "uDiffuseColor": { type: "c", value: new THREE.Color( 0xffffff ) },
  1455. "uSpecularColor": { type: "c", value: new THREE.Color( 0x111111 ) },
  1456. "uAmbientColor": { type: "c", value: new THREE.Color( 0xffffff ) },
  1457. "uShininess": { type: "f", value: 30 },
  1458. "uOpacity": { type: "f", value: 1 },
  1459. "useRefract": { type: "i", value: 0 },
  1460. "uRefractionRatio": { type: "f", value: 0.98 },
  1461. "uReflectivity": { type: "f", value: 0.5 },
  1462. "uOffset" : { type: "v2", value: new THREE.Vector2( 0, 0 ) },
  1463. "uRepeat" : { type: "v2", value: new THREE.Vector2( 1, 1 ) },
  1464. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  1465. }
  1466. ] ),
  1467. fragmentShader: [
  1468. "uniform vec3 uAmbientColor;",
  1469. "uniform vec3 uDiffuseColor;",
  1470. "uniform vec3 uSpecularColor;",
  1471. "uniform float uShininess;",
  1472. "uniform float uOpacity;",
  1473. "uniform bool enableDiffuse;",
  1474. "uniform bool enableSpecular;",
  1475. "uniform bool enableAO;",
  1476. "uniform bool enableReflection;",
  1477. "uniform sampler2D tDiffuse;",
  1478. "uniform sampler2D tNormal;",
  1479. "uniform sampler2D tSpecular;",
  1480. "uniform sampler2D tAO;",
  1481. "uniform samplerCube tCube;",
  1482. "uniform vec2 uNormalScale;",
  1483. "uniform bool useRefract;",
  1484. "uniform float uRefractionRatio;",
  1485. "uniform float uReflectivity;",
  1486. "varying vec3 vTangent;",
  1487. "varying vec3 vBinormal;",
  1488. "varying vec3 vNormal;",
  1489. "varying vec2 vUv;",
  1490. "uniform vec3 ambientLightColor;",
  1491. "#if MAX_DIR_LIGHTS > 0",
  1492. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  1493. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  1494. "#endif",
  1495. "#if MAX_HEMI_LIGHTS > 0",
  1496. "uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];",
  1497. "uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];",
  1498. "uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];",
  1499. "#endif",
  1500. "#if MAX_POINT_LIGHTS > 0",
  1501. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  1502. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  1503. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  1504. "#endif",
  1505. "#if MAX_SPOT_LIGHTS > 0",
  1506. "uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];",
  1507. "uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];",
  1508. "uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];",
  1509. "uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];",
  1510. "uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];",
  1511. "uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];",
  1512. "#endif",
  1513. "#ifdef WRAP_AROUND",
  1514. "uniform vec3 wrapRGB;",
  1515. "#endif",
  1516. "varying vec3 vWorldPosition;",
  1517. "varying vec3 vViewPosition;",
  1518. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  1519. THREE.ShaderChunk[ "fog_pars_fragment" ],
  1520. "void main() {",
  1521. "gl_FragColor = vec4( vec3( 1.0 ), uOpacity );",
  1522. "vec3 specularTex = vec3( 1.0 );",
  1523. "vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
  1524. "normalTex.xy *= uNormalScale;",
  1525. "normalTex = normalize( normalTex );",
  1526. "if( enableDiffuse ) {",
  1527. "#ifdef GAMMA_INPUT",
  1528. "vec4 texelColor = texture2D( tDiffuse, vUv );",
  1529. "texelColor.xyz *= texelColor.xyz;",
  1530. "gl_FragColor = gl_FragColor * texelColor;",
  1531. "#else",
  1532. "gl_FragColor = gl_FragColor * texture2D( tDiffuse, vUv );",
  1533. "#endif",
  1534. "}",
  1535. "if( enableAO ) {",
  1536. "#ifdef GAMMA_INPUT",
  1537. "vec4 aoColor = texture2D( tAO, vUv );",
  1538. "aoColor.xyz *= aoColor.xyz;",
  1539. "gl_FragColor.xyz = gl_FragColor.xyz * aoColor.xyz;",
  1540. "#else",
  1541. "gl_FragColor.xyz = gl_FragColor.xyz * texture2D( tAO, vUv ).xyz;",
  1542. "#endif",
  1543. "}",
  1544. "if( enableSpecular )",
  1545. "specularTex = texture2D( tSpecular, vUv ).xyz;",
  1546. "mat3 tsb = mat3( normalize( vTangent ), normalize( vBinormal ), normalize( vNormal ) );",
  1547. "vec3 finalNormal = tsb * normalTex;",
  1548. "#ifdef FLIP_SIDED",
  1549. "finalNormal = -finalNormal;",
  1550. "#endif",
  1551. "vec3 normal = normalize( finalNormal );",
  1552. "vec3 viewPosition = normalize( vViewPosition );",
  1553. // point lights
  1554. "#if MAX_POINT_LIGHTS > 0",
  1555. "vec3 pointDiffuse = vec3( 0.0 );",
  1556. "vec3 pointSpecular = vec3( 0.0 );",
  1557. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  1558. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  1559. "vec3 pointVector = lPosition.xyz + vViewPosition.xyz;",
  1560. "float pointDistance = 1.0;",
  1561. "if ( pointLightDistance[ i ] > 0.0 )",
  1562. "pointDistance = 1.0 - min( ( length( pointVector ) / pointLightDistance[ i ] ), 1.0 );",
  1563. "pointVector = normalize( pointVector );",
  1564. // diffuse
  1565. "#ifdef WRAP_AROUND",
  1566. "float pointDiffuseWeightFull = max( dot( normal, pointVector ), 0.0 );",
  1567. "float pointDiffuseWeightHalf = max( 0.5 * dot( normal, pointVector ) + 0.5, 0.0 );",
  1568. "vec3 pointDiffuseWeight = mix( vec3 ( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );",
  1569. "#else",
  1570. "float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
  1571. "#endif",
  1572. "pointDiffuse += pointDistance * pointLightColor[ i ] * uDiffuseColor * pointDiffuseWeight;",
  1573. // specular
  1574. "vec3 pointHalfVector = normalize( pointVector + viewPosition );",
  1575. "float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
  1576. "float pointSpecularWeight = specularTex.r * max( pow( pointDotNormalHalf, uShininess ), 0.0 );",
  1577. "#ifdef PHYSICALLY_BASED_SHADING",
  1578. // 2.0 => 2.0001 is hack to work around ANGLE bug
  1579. "float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",
  1580. "vec3 schlick = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( pointVector, pointHalfVector ), 5.0 );",
  1581. "pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * pointDistance * specularNormalization;",
  1582. "#else",
  1583. "pointSpecular += pointDistance * pointLightColor[ i ] * uSpecularColor * pointSpecularWeight * pointDiffuseWeight;",
  1584. "#endif",
  1585. "}",
  1586. "#endif",
  1587. // spot lights
  1588. "#if MAX_SPOT_LIGHTS > 0",
  1589. "vec3 spotDiffuse = vec3( 0.0 );",
  1590. "vec3 spotSpecular = vec3( 0.0 );",
  1591. "for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {",
  1592. "vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );",
  1593. "vec3 spotVector = lPosition.xyz + vViewPosition.xyz;",
  1594. "float spotDistance = 1.0;",
  1595. "if ( spotLightDistance[ i ] > 0.0 )",
  1596. "spotDistance = 1.0 - min( ( length( spotVector ) / spotLightDistance[ i ] ), 1.0 );",
  1597. "spotVector = normalize( spotVector );",
  1598. "float spotEffect = dot( spotLightDirection[ i ], normalize( spotLightPosition[ i ] - vWorldPosition ) );",
  1599. "if ( spotEffect > spotLightAngleCos[ i ] ) {",
  1600. "spotEffect = max( pow( spotEffect, spotLightExponent[ i ] ), 0.0 );",
  1601. // diffuse
  1602. "#ifdef WRAP_AROUND",
  1603. "float spotDiffuseWeightFull = max( dot( normal, spotVector ), 0.0 );",
  1604. "float spotDiffuseWeightHalf = max( 0.5 * dot( normal, spotVector ) + 0.5, 0.0 );",
  1605. "vec3 spotDiffuseWeight = mix( vec3 ( spotDiffuseWeightFull ), vec3( spotDiffuseWeightHalf ), wrapRGB );",
  1606. "#else",
  1607. "float spotDiffuseWeight = max( dot( normal, spotVector ), 0.0 );",
  1608. "#endif",
  1609. "spotDiffuse += spotDistance * spotLightColor[ i ] * uDiffuseColor * spotDiffuseWeight * spotEffect;",
  1610. // specular
  1611. "vec3 spotHalfVector = normalize( spotVector + viewPosition );",
  1612. "float spotDotNormalHalf = max( dot( normal, spotHalfVector ), 0.0 );",
  1613. "float spotSpecularWeight = specularTex.r * max( pow( spotDotNormalHalf, uShininess ), 0.0 );",
  1614. "#ifdef PHYSICALLY_BASED_SHADING",
  1615. // 2.0 => 2.0001 is hack to work around ANGLE bug
  1616. "float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",
  1617. "vec3 schlick = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( spotVector, spotHalfVector ), 5.0 );",
  1618. "spotSpecular += schlick * spotLightColor[ i ] * spotSpecularWeight * spotDiffuseWeight * spotDistance * specularNormalization * spotEffect;",
  1619. "#else",
  1620. "spotSpecular += spotDistance * spotLightColor[ i ] * uSpecularColor * spotSpecularWeight * spotDiffuseWeight * spotEffect;",
  1621. "#endif",
  1622. "}",
  1623. "}",
  1624. "#endif",
  1625. // directional lights
  1626. "#if MAX_DIR_LIGHTS > 0",
  1627. "vec3 dirDiffuse = vec3( 0.0 );",
  1628. "vec3 dirSpecular = vec3( 0.0 );",
  1629. "for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
  1630. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  1631. "vec3 dirVector = normalize( lDirection.xyz );",
  1632. // diffuse
  1633. "#ifdef WRAP_AROUND",
  1634. "float directionalLightWeightingFull = max( dot( normal, dirVector ), 0.0 );",
  1635. "float directionalLightWeightingHalf = max( 0.5 * dot( normal, dirVector ) + 0.5, 0.0 );",
  1636. "vec3 dirDiffuseWeight = mix( vec3( directionalLightWeightingFull ), vec3( directionalLightWeightingHalf ), wrapRGB );",
  1637. "#else",
  1638. "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  1639. "#endif",
  1640. "dirDiffuse += directionalLightColor[ i ] * uDiffuseColor * dirDiffuseWeight;",
  1641. // specular
  1642. "vec3 dirHalfVector = normalize( dirVector + viewPosition );",
  1643. "float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
  1644. "float dirSpecularWeight = specularTex.r * max( pow( dirDotNormalHalf, uShininess ), 0.0 );",
  1645. "#ifdef PHYSICALLY_BASED_SHADING",
  1646. // 2.0 => 2.0001 is hack to work around ANGLE bug
  1647. "float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",
  1648. "vec3 schlick = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( dirVector, dirHalfVector ), 5.0 );",
  1649. "dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;",
  1650. "#else",
  1651. "dirSpecular += directionalLightColor[ i ] * uSpecularColor * dirSpecularWeight * dirDiffuseWeight;",
  1652. "#endif",
  1653. "}",
  1654. "#endif",
  1655. // hemisphere lights
  1656. "#if MAX_HEMI_LIGHTS > 0",
  1657. "vec3 hemiDiffuse = vec3( 0.0 );",
  1658. "vec3 hemiSpecular = vec3( 0.0 );" ,
  1659. "for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {",
  1660. "vec4 lDirection = viewMatrix * vec4( hemisphereLightDirection[ i ], 0.0 );",
  1661. "vec3 lVector = normalize( lDirection.xyz );",
  1662. // diffuse
  1663. "float dotProduct = dot( normal, lVector );",
  1664. "float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
  1665. "vec3 hemiColor = mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
  1666. "hemiDiffuse += uDiffuseColor * hemiColor;",
  1667. // specular (sky light)
  1668. "vec3 hemiHalfVectorSky = normalize( lVector + viewPosition );",
  1669. "float hemiDotNormalHalfSky = 0.5 * dot( normal, hemiHalfVectorSky ) + 0.5;",
  1670. "float hemiSpecularWeightSky = specularTex.r * max( pow( hemiDotNormalHalfSky, uShininess ), 0.0 );",
  1671. // specular (ground light)
  1672. "vec3 lVectorGround = -lVector;",
  1673. "vec3 hemiHalfVectorGround = normalize( lVectorGround + viewPosition );",
  1674. "float hemiDotNormalHalfGround = 0.5 * dot( normal, hemiHalfVectorGround ) + 0.5;",
  1675. "float hemiSpecularWeightGround = specularTex.r * max( pow( hemiDotNormalHalfGround, uShininess ), 0.0 );",
  1676. "#ifdef PHYSICALLY_BASED_SHADING",
  1677. "float dotProductGround = dot( normal, lVectorGround );",
  1678. // 2.0 => 2.0001 is hack to work around ANGLE bug
  1679. "float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",
  1680. "vec3 schlickSky = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( lVector, hemiHalfVectorSky ), 5.0 );",
  1681. "vec3 schlickGround = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( lVectorGround, hemiHalfVectorGround ), 5.0 );",
  1682. "hemiSpecular += hemiColor * specularNormalization * ( schlickSky * hemiSpecularWeightSky * max( dotProduct, 0.0 ) + schlickGround * hemiSpecularWeightGround * max( dotProductGround, 0.0 ) );",
  1683. "#else",
  1684. "hemiSpecular += uSpecularColor * hemiColor * ( hemiSpecularWeightSky + hemiSpecularWeightGround ) * hemiDiffuseWeight;",
  1685. "#endif",
  1686. "}",
  1687. "#endif",
  1688. // all lights contribution summation
  1689. "vec3 totalDiffuse = vec3( 0.0 );",
  1690. "vec3 totalSpecular = vec3( 0.0 );",
  1691. "#if MAX_DIR_LIGHTS > 0",
  1692. "totalDiffuse += dirDiffuse;",
  1693. "totalSpecular += dirSpecular;",
  1694. "#endif",
  1695. "#if MAX_HEMI_LIGHTS > 0",
  1696. "totalDiffuse += hemiDiffuse;",
  1697. "totalSpecular += hemiSpecular;",
  1698. "#endif",
  1699. "#if MAX_POINT_LIGHTS > 0",
  1700. "totalDiffuse += pointDiffuse;",
  1701. "totalSpecular += pointSpecular;",
  1702. "#endif",
  1703. "#if MAX_SPOT_LIGHTS > 0",
  1704. "totalDiffuse += spotDiffuse;",
  1705. "totalSpecular += spotSpecular;",
  1706. "#endif",
  1707. "#ifdef METAL",
  1708. "gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * uAmbientColor + totalSpecular );",
  1709. "#else",
  1710. "gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * uAmbientColor ) + totalSpecular;",
  1711. "#endif",
  1712. "if ( enableReflection ) {",
  1713. "vec3 vReflect;",
  1714. "vec3 cameraToVertex = normalize( vWorldPosition - cameraPosition );",
  1715. "if ( useRefract ) {",
  1716. "vReflect = refract( cameraToVertex, normal, uRefractionRatio );",
  1717. "} else {",
  1718. "vReflect = reflect( cameraToVertex, normal );",
  1719. "}",
  1720. "vec4 cubeColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
  1721. "#ifdef GAMMA_INPUT",
  1722. "cubeColor.xyz *= cubeColor.xyz;",
  1723. "#endif",
  1724. "gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, specularTex.r * uReflectivity );",
  1725. "}",
  1726. THREE.ShaderChunk[ "shadowmap_fragment" ],
  1727. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  1728. THREE.ShaderChunk[ "fog_fragment" ],
  1729. "}"
  1730. ].join("\n"),
  1731. vertexShader: [
  1732. "attribute vec4 tangent;",
  1733. "uniform vec2 uOffset;",
  1734. "uniform vec2 uRepeat;",
  1735. "uniform bool enableDisplacement;",
  1736. "#ifdef VERTEX_TEXTURES",
  1737. "uniform sampler2D tDisplacement;",
  1738. "uniform float uDisplacementScale;",
  1739. "uniform float uDisplacementBias;",
  1740. "#endif",
  1741. "varying vec3 vTangent;",
  1742. "varying vec3 vBinormal;",
  1743. "varying vec3 vNormal;",
  1744. "varying vec2 vUv;",
  1745. "varying vec3 vWorldPosition;",
  1746. "varying vec3 vViewPosition;",
  1747. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  1748. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  1749. "void main() {",
  1750. THREE.ShaderChunk[ "skinbase_vertex" ],
  1751. THREE.ShaderChunk[ "skinnormal_vertex" ],
  1752. // normal, tangent and binormal vectors
  1753. "#ifdef USE_SKINNING",
  1754. "vNormal = normalize( normalMatrix * skinnedNormal.xyz );",
  1755. "vec4 skinnedTangent = skinMatrix * vec4( tangent.xyz, 0.0 );",
  1756. "vTangent = normalize( normalMatrix * skinnedTangent.xyz );",
  1757. "#else",
  1758. "vNormal = normalize( normalMatrix * normal );",
  1759. "vTangent = normalize( normalMatrix * tangent.xyz );",
  1760. "#endif",
  1761. "vBinormal = normalize( cross( vNormal, vTangent ) * tangent.w );",
  1762. "vUv = uv * uRepeat + uOffset;",
  1763. // displacement mapping
  1764. "vec3 displacedPosition;",
  1765. "#ifdef VERTEX_TEXTURES",
  1766. "if ( enableDisplacement ) {",
  1767. "vec3 dv = texture2D( tDisplacement, uv ).xyz;",
  1768. "float df = uDisplacementScale * dv.x + uDisplacementBias;",
  1769. "displacedPosition = position + normalize( normal ) * df;",
  1770. "} else {",
  1771. "#ifdef USE_SKINNING",
  1772. "vec4 skinVertex = vec4( position, 1.0 );",
  1773. "vec4 skinned = boneMatX * skinVertex * skinWeight.x;",
  1774. "skinned += boneMatY * skinVertex * skinWeight.y;",
  1775. "displacedPosition = skinned.xyz;",
  1776. "#else",
  1777. "displacedPosition = position;",
  1778. "#endif",
  1779. "}",
  1780. "#else",
  1781. "#ifdef USE_SKINNING",
  1782. "vec4 skinVertex = vec4( position, 1.0 );",
  1783. "vec4 skinned = boneMatX * skinVertex * skinWeight.x;",
  1784. "skinned += boneMatY * skinVertex * skinWeight.y;",
  1785. "displacedPosition = skinned.xyz;",
  1786. "#else",
  1787. "displacedPosition = position;",
  1788. "#endif",
  1789. "#endif",
  1790. //
  1791. "vec4 mvPosition = modelViewMatrix * vec4( displacedPosition, 1.0 );",
  1792. "vec4 worldPosition = modelMatrix * vec4( displacedPosition, 1.0 );",
  1793. "gl_Position = projectionMatrix * mvPosition;",
  1794. //
  1795. "vWorldPosition = worldPosition.xyz;",
  1796. "vViewPosition = -mvPosition.xyz;",
  1797. // shadows
  1798. "#ifdef USE_SHADOWMAP",
  1799. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  1800. "vShadowCoord[ i ] = shadowMatrix[ i ] * worldPosition;",
  1801. "}",
  1802. "#endif",
  1803. "}"
  1804. ].join("\n")
  1805. },
  1806. /* -------------------------------------------------------------------------
  1807. // Cube map shader
  1808. ------------------------------------------------------------------------- */
  1809. 'cube': {
  1810. uniforms: { "tCube": { type: "t", value: null },
  1811. "tFlip": { type: "f", value: -1 } },
  1812. vertexShader: [
  1813. "varying vec3 vWorldPosition;",
  1814. "void main() {",
  1815. "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  1816. "vWorldPosition = worldPosition.xyz;",
  1817. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  1818. "}"
  1819. ].join("\n"),
  1820. fragmentShader: [
  1821. "uniform samplerCube tCube;",
  1822. "uniform float tFlip;",
  1823. "varying vec3 vWorldPosition;",
  1824. "void main() {",
  1825. "gl_FragColor = textureCube( tCube, vec3( tFlip * vWorldPosition.x, vWorldPosition.yz ) );",
  1826. "}"
  1827. ].join("\n")
  1828. },
  1829. // Depth encoding into RGBA texture
  1830. // based on SpiderGL shadow map example
  1831. // http://spidergl.org/example.php?id=6
  1832. // originally from
  1833. // http://www.gamedev.net/topic/442138-packing-a-float-into-a-a8r8g8b8-texture-shader/page__whichpage__1%25EF%25BF%25BD
  1834. // see also here:
  1835. // http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
  1836. 'depthRGBA': {
  1837. uniforms: {},
  1838. vertexShader: [
  1839. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  1840. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  1841. "void main() {",
  1842. THREE.ShaderChunk[ "skinbase_vertex" ],
  1843. THREE.ShaderChunk[ "morphtarget_vertex" ],
  1844. THREE.ShaderChunk[ "skinning_vertex" ],
  1845. THREE.ShaderChunk[ "default_vertex" ],
  1846. "}"
  1847. ].join("\n"),
  1848. fragmentShader: [
  1849. "vec4 pack_depth( const in float depth ) {",
  1850. "const vec4 bit_shift = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 );",
  1851. "const vec4 bit_mask = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 );",
  1852. "vec4 res = fract( depth * bit_shift );",
  1853. "res -= res.xxyz * bit_mask;",
  1854. "return res;",
  1855. "}",
  1856. "void main() {",
  1857. "gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z );",
  1858. //"gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z / gl_FragCoord.w );",
  1859. //"float z = ( ( gl_FragCoord.z / gl_FragCoord.w ) - 3.0 ) / ( 4000.0 - 3.0 );",
  1860. //"gl_FragData[ 0 ] = pack_depth( z );",
  1861. //"gl_FragData[ 0 ] = vec4( z, z, z, 1.0 );",
  1862. "}"
  1863. ].join("\n")
  1864. }
  1865. };