2
0

WebGLShaders.js 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204
  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. "uniform int boneTextureWidth;",
  691. "uniform int boneTextureHeight;",
  692. "mat4 getBoneMatrix( const in float i ) {",
  693. "float j = i * 4.0;",
  694. "float x = mod( j, float( boneTextureWidth ) );",
  695. "float y = floor( j / float( boneTextureWidth ) );",
  696. "float dx = 1.0 / float( boneTextureWidth );",
  697. "float dy = 1.0 / float( boneTextureHeight );",
  698. "y = dy * ( y + 0.5 );",
  699. "vec4 v1 = texture2D( boneTexture, vec2( dx * ( x + 0.5 ), y ) );",
  700. "vec4 v2 = texture2D( boneTexture, vec2( dx * ( x + 1.5 ), y ) );",
  701. "vec4 v3 = texture2D( boneTexture, vec2( dx * ( x + 2.5 ), y ) );",
  702. "vec4 v4 = texture2D( boneTexture, vec2( dx * ( x + 3.5 ), y ) );",
  703. "mat4 bone = mat4( v1, v2, v3, v4 );",
  704. "return bone;",
  705. "}",
  706. "#else",
  707. "uniform mat4 boneGlobalMatrices[ MAX_BONES ];",
  708. "mat4 getBoneMatrix( const in float i ) {",
  709. "mat4 bone = boneGlobalMatrices[ int(i) ];",
  710. "return bone;",
  711. "}",
  712. "#endif",
  713. "#endif"
  714. ].join("\n"),
  715. skinbase_vertex: [
  716. "#ifdef USE_SKINNING",
  717. "mat4 boneMatX = getBoneMatrix( skinIndex.x );",
  718. "mat4 boneMatY = getBoneMatrix( skinIndex.y );",
  719. "#endif"
  720. ].join("\n"),
  721. skinning_vertex: [
  722. "#ifdef USE_SKINNING",
  723. "#ifdef USE_MORPHTARGETS",
  724. "vec4 skinVertex = vec4( morphed, 1.0 );",
  725. "#else",
  726. "vec4 skinVertex = vec4( position, 1.0 );",
  727. "#endif",
  728. "vec4 skinned = boneMatX * skinVertex * skinWeight.x;",
  729. "skinned += boneMatY * skinVertex * skinWeight.y;",
  730. "#endif"
  731. ].join("\n"),
  732. // MORPHING
  733. morphtarget_pars_vertex: [
  734. "#ifdef USE_MORPHTARGETS",
  735. "#ifndef USE_MORPHNORMALS",
  736. "uniform float morphTargetInfluences[ 8 ];",
  737. "#else",
  738. "uniform float morphTargetInfluences[ 4 ];",
  739. "#endif",
  740. "#endif"
  741. ].join("\n"),
  742. morphtarget_vertex: [
  743. "#ifdef USE_MORPHTARGETS",
  744. "vec3 morphed = vec3( 0.0 );",
  745. "morphed += ( morphTarget0 - position ) * morphTargetInfluences[ 0 ];",
  746. "morphed += ( morphTarget1 - position ) * morphTargetInfluences[ 1 ];",
  747. "morphed += ( morphTarget2 - position ) * morphTargetInfluences[ 2 ];",
  748. "morphed += ( morphTarget3 - position ) * morphTargetInfluences[ 3 ];",
  749. "#ifndef USE_MORPHNORMALS",
  750. "morphed += ( morphTarget4 - position ) * morphTargetInfluences[ 4 ];",
  751. "morphed += ( morphTarget5 - position ) * morphTargetInfluences[ 5 ];",
  752. "morphed += ( morphTarget6 - position ) * morphTargetInfluences[ 6 ];",
  753. "morphed += ( morphTarget7 - position ) * morphTargetInfluences[ 7 ];",
  754. "#endif",
  755. "morphed += position;",
  756. "#endif"
  757. ].join("\n"),
  758. default_vertex : [
  759. "vec4 mvPosition;",
  760. "#ifdef USE_SKINNING",
  761. "mvPosition = modelViewMatrix * skinned;",
  762. "#endif",
  763. "#if !defined( USE_SKINNING ) && defined( USE_MORPHTARGETS )",
  764. "mvPosition = modelViewMatrix * vec4( morphed, 1.0 );",
  765. "#endif",
  766. "#if !defined( USE_SKINNING ) && ! defined( USE_MORPHTARGETS )",
  767. "mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  768. "#endif",
  769. "gl_Position = projectionMatrix * mvPosition;"
  770. ].join("\n"),
  771. morphnormal_vertex: [
  772. "#ifdef USE_MORPHNORMALS",
  773. "vec3 morphedNormal = vec3( 0.0 );",
  774. "morphedNormal += ( morphNormal0 - normal ) * morphTargetInfluences[ 0 ];",
  775. "morphedNormal += ( morphNormal1 - normal ) * morphTargetInfluences[ 1 ];",
  776. "morphedNormal += ( morphNormal2 - normal ) * morphTargetInfluences[ 2 ];",
  777. "morphedNormal += ( morphNormal3 - normal ) * morphTargetInfluences[ 3 ];",
  778. "morphedNormal += normal;",
  779. "#endif"
  780. ].join("\n"),
  781. skinnormal_vertex: [
  782. "#ifdef USE_SKINNING",
  783. "mat4 skinMatrix = skinWeight.x * boneMatX;",
  784. "skinMatrix += skinWeight.y * boneMatY;",
  785. "#ifdef USE_MORPHNORMALS",
  786. "vec4 skinnedNormal = skinMatrix * vec4( morphedNormal, 0.0 );",
  787. "#else",
  788. "vec4 skinnedNormal = skinMatrix * vec4( normal, 0.0 );",
  789. "#endif",
  790. "#endif"
  791. ].join("\n"),
  792. defaultnormal_vertex: [
  793. "vec3 objectNormal;",
  794. "#ifdef USE_SKINNING",
  795. "objectNormal = skinnedNormal.xyz;",
  796. "#endif",
  797. "#if !defined( USE_SKINNING ) && defined( USE_MORPHNORMALS )",
  798. "objectNormal = morphedNormal;",
  799. "#endif",
  800. "#if !defined( USE_SKINNING ) && ! defined( USE_MORPHNORMALS )",
  801. "objectNormal = normal;",
  802. "#endif",
  803. "#ifdef FLIP_SIDED",
  804. "objectNormal = -objectNormal;",
  805. "#endif",
  806. "vec3 transformedNormal = normalMatrix * objectNormal;"
  807. ].join("\n"),
  808. // SHADOW MAP
  809. // based on SpiderGL shadow map and Fabien Sanglard's GLSL shadow mapping examples
  810. // http://spidergl.org/example.php?id=6
  811. // http://fabiensanglard.net/shadowmapping
  812. shadowmap_pars_fragment: [
  813. "#ifdef USE_SHADOWMAP",
  814. "uniform sampler2D shadowMap[ MAX_SHADOWS ];",
  815. "uniform vec2 shadowMapSize[ MAX_SHADOWS ];",
  816. "uniform float shadowDarkness[ MAX_SHADOWS ];",
  817. "uniform float shadowBias[ MAX_SHADOWS ];",
  818. "varying vec4 vShadowCoord[ MAX_SHADOWS ];",
  819. "float unpackDepth( const in vec4 rgba_depth ) {",
  820. "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 );",
  821. "float depth = dot( rgba_depth, bit_shift );",
  822. "return depth;",
  823. "}",
  824. "#endif"
  825. ].join("\n"),
  826. shadowmap_fragment: [
  827. "#ifdef USE_SHADOWMAP",
  828. "#ifdef SHADOWMAP_DEBUG",
  829. "vec3 frustumColors[3];",
  830. "frustumColors[0] = vec3( 1.0, 0.5, 0.0 );",
  831. "frustumColors[1] = vec3( 0.0, 1.0, 0.8 );",
  832. "frustumColors[2] = vec3( 0.0, 0.5, 1.0 );",
  833. "#endif",
  834. "#ifdef SHADOWMAP_CASCADE",
  835. "int inFrustumCount = 0;",
  836. "#endif",
  837. "float fDepth;",
  838. "vec3 shadowColor = vec3( 1.0 );",
  839. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  840. "vec3 shadowCoord = vShadowCoord[ i ].xyz / vShadowCoord[ i ].w;",
  841. // "if ( something && something )" breaks ATI OpenGL shader compiler
  842. // "if ( all( something, something ) )" using this instead
  843. "bvec4 inFrustumVec = bvec4 ( shadowCoord.x >= 0.0, shadowCoord.x <= 1.0, shadowCoord.y >= 0.0, shadowCoord.y <= 1.0 );",
  844. "bool inFrustum = all( inFrustumVec );",
  845. // don't shadow pixels outside of light frustum
  846. // use just first frustum (for cascades)
  847. // don't shadow pixels behind far plane of light frustum
  848. "#ifdef SHADOWMAP_CASCADE",
  849. "inFrustumCount += int( inFrustum );",
  850. "bvec3 frustumTestVec = bvec3( inFrustum, inFrustumCount == 1, shadowCoord.z <= 1.0 );",
  851. "#else",
  852. "bvec2 frustumTestVec = bvec2( inFrustum, shadowCoord.z <= 1.0 );",
  853. "#endif",
  854. "bool frustumTest = all( frustumTestVec );",
  855. "if ( frustumTest ) {",
  856. "shadowCoord.z += shadowBias[ i ];",
  857. "#if defined( SHADOWMAP_TYPE_PCF )",
  858. // Percentage-close filtering
  859. // (9 pixel kernel)
  860. // http://fabiensanglard.net/shadowmappingPCF/
  861. "float shadow = 0.0;",
  862. /*
  863. // nested loops breaks shader compiler / validator on some ATI cards when using OpenGL
  864. // must enroll loop manually
  865. "for ( float y = -1.25; y <= 1.25; y += 1.25 )",
  866. "for ( float x = -1.25; x <= 1.25; x += 1.25 ) {",
  867. "vec4 rgbaDepth = texture2D( shadowMap[ i ], vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy );",
  868. // doesn't seem to produce any noticeable visual difference compared to simple "texture2D" lookup
  869. //"vec4 rgbaDepth = texture2DProj( shadowMap[ i ], vec4( vShadowCoord[ i ].w * ( vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy ), 0.05, vShadowCoord[ i ].w ) );",
  870. "float fDepth = unpackDepth( rgbaDepth );",
  871. "if ( fDepth < shadowCoord.z )",
  872. "shadow += 1.0;",
  873. "}",
  874. "shadow /= 9.0;",
  875. */
  876. "const float shadowDelta = 1.0 / 9.0;",
  877. "float xPixelOffset = 1.0 / shadowMapSize[ i ].x;",
  878. "float yPixelOffset = 1.0 / shadowMapSize[ i ].y;",
  879. "float dx0 = -1.25 * xPixelOffset;",
  880. "float dy0 = -1.25 * yPixelOffset;",
  881. "float dx1 = 1.25 * xPixelOffset;",
  882. "float dy1 = 1.25 * yPixelOffset;",
  883. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy0 ) ) );",
  884. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  885. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy0 ) ) );",
  886. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  887. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy0 ) ) );",
  888. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  889. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, 0.0 ) ) );",
  890. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  891. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy ) );",
  892. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  893. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, 0.0 ) ) );",
  894. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  895. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy1 ) ) );",
  896. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  897. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy1 ) ) );",
  898. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  899. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy1 ) ) );",
  900. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  901. "shadowColor = shadowColor * vec3( ( 1.0 - shadowDarkness[ i ] * shadow ) );",
  902. "#elif defined( SHADOWMAP_TYPE_PCF_SOFT )",
  903. // Percentage-close filtering
  904. // (9 pixel kernel)
  905. // http://fabiensanglard.net/shadowmappingPCF/
  906. "float shadow = 0.0;",
  907. "float xPixelOffset = 1.0 / shadowMapSize[ i ].x;",
  908. "float yPixelOffset = 1.0 / shadowMapSize[ i ].y;",
  909. "float dx0 = -1.0 * xPixelOffset;",
  910. "float dy0 = -1.0 * yPixelOffset;",
  911. "float dx1 = 1.0 * xPixelOffset;",
  912. "float dy1 = 1.0 * yPixelOffset;",
  913. "mat3 shadowKernel;",
  914. "mat3 depthKernel;",
  915. "depthKernel[0][0] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy0 ) ) );",
  916. "depthKernel[0][1] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, 0.0 ) ) );",
  917. "depthKernel[0][2] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy1 ) ) );",
  918. "depthKernel[1][0] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy0 ) ) );",
  919. "depthKernel[1][1] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy ) );",
  920. "depthKernel[1][2] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy1 ) ) );",
  921. "depthKernel[2][0] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy0 ) ) );",
  922. "depthKernel[2][1] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, 0.0 ) ) );",
  923. "depthKernel[2][2] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy1 ) ) );",
  924. "vec3 shadowZ = vec3( shadowCoord.z );",
  925. "shadowKernel[0] = vec3(lessThan(depthKernel[0], shadowZ ));",
  926. "shadowKernel[0] *= vec3(0.25);",
  927. "shadowKernel[1] = vec3(lessThan(depthKernel[1], shadowZ ));",
  928. "shadowKernel[1] *= vec3(0.25);",
  929. "shadowKernel[2] = vec3(lessThan(depthKernel[2], shadowZ ));",
  930. "shadowKernel[2] *= vec3(0.25);",
  931. "vec2 fractionalCoord = 1.0 - fract( shadowCoord.xy * shadowMapSize[i].xy );",
  932. "shadowKernel[0] = mix( shadowKernel[1], shadowKernel[0], fractionalCoord.x );",
  933. "shadowKernel[1] = mix( shadowKernel[2], shadowKernel[1], fractionalCoord.x );",
  934. "vec4 shadowValues;",
  935. "shadowValues.x = mix( shadowKernel[0][1], shadowKernel[0][0], fractionalCoord.y );",
  936. "shadowValues.y = mix( shadowKernel[0][2], shadowKernel[0][1], fractionalCoord.y );",
  937. "shadowValues.z = mix( shadowKernel[1][1], shadowKernel[1][0], fractionalCoord.y );",
  938. "shadowValues.w = mix( shadowKernel[1][2], shadowKernel[1][1], fractionalCoord.y );",
  939. "shadow = dot( shadowValues, vec4( 1.0 ) );",
  940. "shadowColor = shadowColor * vec3( ( 1.0 - shadowDarkness[ i ] * shadow ) );",
  941. "#else",
  942. "vec4 rgbaDepth = texture2D( shadowMap[ i ], shadowCoord.xy );",
  943. "float fDepth = unpackDepth( rgbaDepth );",
  944. "if ( fDepth < shadowCoord.z )",
  945. // spot with multiple shadows is darker
  946. "shadowColor = shadowColor * vec3( 1.0 - shadowDarkness[ i ] );",
  947. // spot with multiple shadows has the same color as single shadow spot
  948. //"shadowColor = min( shadowColor, vec3( shadowDarkness[ i ] ) );",
  949. "#endif",
  950. "}",
  951. "#ifdef SHADOWMAP_DEBUG",
  952. "#ifdef SHADOWMAP_CASCADE",
  953. "if ( inFrustum && inFrustumCount == 1 ) gl_FragColor.xyz *= frustumColors[ i ];",
  954. "#else",
  955. "if ( inFrustum ) gl_FragColor.xyz *= frustumColors[ i ];",
  956. "#endif",
  957. "#endif",
  958. "}",
  959. "#ifdef GAMMA_OUTPUT",
  960. "shadowColor *= shadowColor;",
  961. "#endif",
  962. "gl_FragColor.xyz = gl_FragColor.xyz * shadowColor;",
  963. "#endif"
  964. ].join("\n"),
  965. shadowmap_pars_vertex: [
  966. "#ifdef USE_SHADOWMAP",
  967. "varying vec4 vShadowCoord[ MAX_SHADOWS ];",
  968. "uniform mat4 shadowMatrix[ MAX_SHADOWS ];",
  969. "#endif"
  970. ].join("\n"),
  971. shadowmap_vertex: [
  972. "#ifdef USE_SHADOWMAP",
  973. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  974. "vShadowCoord[ i ] = shadowMatrix[ i ] * worldPosition;",
  975. "}",
  976. "#endif"
  977. ].join("\n"),
  978. // ALPHATEST
  979. alphatest_fragment: [
  980. "#ifdef ALPHATEST",
  981. "if ( gl_FragColor.a < ALPHATEST ) discard;",
  982. "#endif"
  983. ].join("\n"),
  984. // LINEAR SPACE
  985. linear_to_gamma_fragment: [
  986. "#ifdef GAMMA_OUTPUT",
  987. "gl_FragColor.xyz = sqrt( gl_FragColor.xyz );",
  988. "#endif"
  989. ].join("\n")
  990. };
  991. THREE.UniformsUtils = {
  992. merge: function ( uniforms ) {
  993. var u, p, tmp, merged = {};
  994. for ( u = 0; u < uniforms.length; u ++ ) {
  995. tmp = this.clone( uniforms[ u ] );
  996. for ( p in tmp ) {
  997. merged[ p ] = tmp[ p ];
  998. }
  999. }
  1000. return merged;
  1001. },
  1002. clone: function ( uniforms_src ) {
  1003. var u, p, parameter, parameter_src, uniforms_dst = {};
  1004. for ( u in uniforms_src ) {
  1005. uniforms_dst[ u ] = {};
  1006. for ( p in uniforms_src[ u ] ) {
  1007. parameter_src = uniforms_src[ u ][ p ];
  1008. if ( parameter_src instanceof THREE.Color ||
  1009. parameter_src instanceof THREE.Vector2 ||
  1010. parameter_src instanceof THREE.Vector3 ||
  1011. parameter_src instanceof THREE.Vector4 ||
  1012. parameter_src instanceof THREE.Matrix4 ||
  1013. parameter_src instanceof THREE.Texture ) {
  1014. uniforms_dst[ u ][ p ] = parameter_src.clone();
  1015. } else if ( parameter_src instanceof Array ) {
  1016. uniforms_dst[ u ][ p ] = parameter_src.slice();
  1017. } else {
  1018. uniforms_dst[ u ][ p ] = parameter_src;
  1019. }
  1020. }
  1021. }
  1022. return uniforms_dst;
  1023. }
  1024. };
  1025. THREE.UniformsLib = {
  1026. common: {
  1027. "diffuse" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  1028. "opacity" : { type: "f", value: 1.0 },
  1029. "map" : { type: "t", value: null },
  1030. "offsetRepeat" : { type: "v4", value: new THREE.Vector4( 0, 0, 1, 1 ) },
  1031. "lightMap" : { type: "t", value: null },
  1032. "specularMap" : { type: "t", value: null },
  1033. "envMap" : { type: "t", value: null },
  1034. "flipEnvMap" : { type: "f", value: -1 },
  1035. "useRefract" : { type: "i", value: 0 },
  1036. "reflectivity" : { type: "f", value: 1.0 },
  1037. "refractionRatio" : { type: "f", value: 0.98 },
  1038. "combine" : { type: "i", value: 0 },
  1039. "morphTargetInfluences" : { type: "f", value: 0 }
  1040. },
  1041. bump: {
  1042. "bumpMap" : { type: "t", value: null },
  1043. "bumpScale" : { type: "f", value: 1 }
  1044. },
  1045. normalmap: {
  1046. "normalMap" : { type: "t", value: null },
  1047. "normalScale" : { type: "v2", value: new THREE.Vector2( 1, 1 ) }
  1048. },
  1049. fog : {
  1050. "fogDensity" : { type: "f", value: 0.00025 },
  1051. "fogNear" : { type: "f", value: 1 },
  1052. "fogFar" : { type: "f", value: 2000 },
  1053. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  1054. },
  1055. lights: {
  1056. "ambientLightColor" : { type: "fv", value: [] },
  1057. "directionalLightDirection" : { type: "fv", value: [] },
  1058. "directionalLightColor" : { type: "fv", value: [] },
  1059. "hemisphereLightDirection" : { type: "fv", value: [] },
  1060. "hemisphereLightSkyColor" : { type: "fv", value: [] },
  1061. "hemisphereLightGroundColor" : { type: "fv", value: [] },
  1062. "pointLightColor" : { type: "fv", value: [] },
  1063. "pointLightPosition" : { type: "fv", value: [] },
  1064. "pointLightDistance" : { type: "fv1", value: [] },
  1065. "spotLightColor" : { type: "fv", value: [] },
  1066. "spotLightPosition" : { type: "fv", value: [] },
  1067. "spotLightDirection" : { type: "fv", value: [] },
  1068. "spotLightDistance" : { type: "fv1", value: [] },
  1069. "spotLightAngleCos" : { type: "fv1", value: [] },
  1070. "spotLightExponent" : { type: "fv1", value: [] }
  1071. },
  1072. particle: {
  1073. "psColor" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  1074. "opacity" : { type: "f", value: 1.0 },
  1075. "size" : { type: "f", value: 1.0 },
  1076. "scale" : { type: "f", value: 1.0 },
  1077. "map" : { type: "t", value: null },
  1078. "fogDensity" : { type: "f", value: 0.00025 },
  1079. "fogNear" : { type: "f", value: 1 },
  1080. "fogFar" : { type: "f", value: 2000 },
  1081. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  1082. },
  1083. shadowmap: {
  1084. "shadowMap": { type: "tv", value: [] },
  1085. "shadowMapSize": { type: "v2v", value: [] },
  1086. "shadowBias" : { type: "fv1", value: [] },
  1087. "shadowDarkness": { type: "fv1", value: [] },
  1088. "shadowMatrix" : { type: "m4v", value: [] }
  1089. }
  1090. };
  1091. THREE.ShaderLib = {
  1092. 'basic': {
  1093. uniforms: THREE.UniformsUtils.merge( [
  1094. THREE.UniformsLib[ "common" ],
  1095. THREE.UniformsLib[ "fog" ],
  1096. THREE.UniformsLib[ "shadowmap" ]
  1097. ] ),
  1098. vertexShader: [
  1099. THREE.ShaderChunk[ "map_pars_vertex" ],
  1100. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  1101. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  1102. THREE.ShaderChunk[ "color_pars_vertex" ],
  1103. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  1104. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  1105. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  1106. "void main() {",
  1107. THREE.ShaderChunk[ "map_vertex" ],
  1108. THREE.ShaderChunk[ "lightmap_vertex" ],
  1109. THREE.ShaderChunk[ "color_vertex" ],
  1110. THREE.ShaderChunk[ "skinbase_vertex" ],
  1111. "#ifdef USE_ENVMAP",
  1112. THREE.ShaderChunk[ "morphnormal_vertex" ],
  1113. THREE.ShaderChunk[ "skinnormal_vertex" ],
  1114. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  1115. "#endif",
  1116. THREE.ShaderChunk[ "morphtarget_vertex" ],
  1117. THREE.ShaderChunk[ "skinning_vertex" ],
  1118. THREE.ShaderChunk[ "default_vertex" ],
  1119. THREE.ShaderChunk[ "worldpos_vertex" ],
  1120. THREE.ShaderChunk[ "envmap_vertex" ],
  1121. THREE.ShaderChunk[ "shadowmap_vertex" ],
  1122. "}"
  1123. ].join("\n"),
  1124. fragmentShader: [
  1125. "uniform vec3 diffuse;",
  1126. "uniform float opacity;",
  1127. THREE.ShaderChunk[ "color_pars_fragment" ],
  1128. THREE.ShaderChunk[ "map_pars_fragment" ],
  1129. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  1130. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  1131. THREE.ShaderChunk[ "fog_pars_fragment" ],
  1132. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  1133. THREE.ShaderChunk[ "specularmap_pars_fragment" ],
  1134. "void main() {",
  1135. "gl_FragColor = vec4( diffuse, opacity );",
  1136. THREE.ShaderChunk[ "map_fragment" ],
  1137. THREE.ShaderChunk[ "alphatest_fragment" ],
  1138. THREE.ShaderChunk[ "specularmap_fragment" ],
  1139. THREE.ShaderChunk[ "lightmap_fragment" ],
  1140. THREE.ShaderChunk[ "color_fragment" ],
  1141. THREE.ShaderChunk[ "envmap_fragment" ],
  1142. THREE.ShaderChunk[ "shadowmap_fragment" ],
  1143. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  1144. THREE.ShaderChunk[ "fog_fragment" ],
  1145. "}"
  1146. ].join("\n")
  1147. },
  1148. 'lambert': {
  1149. uniforms: THREE.UniformsUtils.merge( [
  1150. THREE.UniformsLib[ "common" ],
  1151. THREE.UniformsLib[ "fog" ],
  1152. THREE.UniformsLib[ "lights" ],
  1153. THREE.UniformsLib[ "shadowmap" ],
  1154. {
  1155. "ambient" : { type: "c", value: new THREE.Color( 0xffffff ) },
  1156. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  1157. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  1158. }
  1159. ] ),
  1160. vertexShader: [
  1161. "#define LAMBERT",
  1162. "varying vec3 vLightFront;",
  1163. "#ifdef DOUBLE_SIDED",
  1164. "varying vec3 vLightBack;",
  1165. "#endif",
  1166. THREE.ShaderChunk[ "map_pars_vertex" ],
  1167. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  1168. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  1169. THREE.ShaderChunk[ "lights_lambert_pars_vertex" ],
  1170. THREE.ShaderChunk[ "color_pars_vertex" ],
  1171. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  1172. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  1173. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  1174. "void main() {",
  1175. THREE.ShaderChunk[ "map_vertex" ],
  1176. THREE.ShaderChunk[ "lightmap_vertex" ],
  1177. THREE.ShaderChunk[ "color_vertex" ],
  1178. THREE.ShaderChunk[ "morphnormal_vertex" ],
  1179. THREE.ShaderChunk[ "skinbase_vertex" ],
  1180. THREE.ShaderChunk[ "skinnormal_vertex" ],
  1181. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  1182. THREE.ShaderChunk[ "morphtarget_vertex" ],
  1183. THREE.ShaderChunk[ "skinning_vertex" ],
  1184. THREE.ShaderChunk[ "default_vertex" ],
  1185. THREE.ShaderChunk[ "worldpos_vertex" ],
  1186. THREE.ShaderChunk[ "envmap_vertex" ],
  1187. THREE.ShaderChunk[ "lights_lambert_vertex" ],
  1188. THREE.ShaderChunk[ "shadowmap_vertex" ],
  1189. "}"
  1190. ].join("\n"),
  1191. fragmentShader: [
  1192. "uniform float opacity;",
  1193. "varying vec3 vLightFront;",
  1194. "#ifdef DOUBLE_SIDED",
  1195. "varying vec3 vLightBack;",
  1196. "#endif",
  1197. THREE.ShaderChunk[ "color_pars_fragment" ],
  1198. THREE.ShaderChunk[ "map_pars_fragment" ],
  1199. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  1200. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  1201. THREE.ShaderChunk[ "fog_pars_fragment" ],
  1202. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  1203. THREE.ShaderChunk[ "specularmap_pars_fragment" ],
  1204. "void main() {",
  1205. "gl_FragColor = vec4( vec3 ( 1.0 ), opacity );",
  1206. THREE.ShaderChunk[ "map_fragment" ],
  1207. THREE.ShaderChunk[ "alphatest_fragment" ],
  1208. THREE.ShaderChunk[ "specularmap_fragment" ],
  1209. "#ifdef DOUBLE_SIDED",
  1210. //"float isFront = float( gl_FrontFacing );",
  1211. //"gl_FragColor.xyz *= isFront * vLightFront + ( 1.0 - isFront ) * vLightBack;",
  1212. "if ( gl_FrontFacing )",
  1213. "gl_FragColor.xyz *= vLightFront;",
  1214. "else",
  1215. "gl_FragColor.xyz *= vLightBack;",
  1216. "#else",
  1217. "gl_FragColor.xyz *= vLightFront;",
  1218. "#endif",
  1219. THREE.ShaderChunk[ "lightmap_fragment" ],
  1220. THREE.ShaderChunk[ "color_fragment" ],
  1221. THREE.ShaderChunk[ "envmap_fragment" ],
  1222. THREE.ShaderChunk[ "shadowmap_fragment" ],
  1223. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  1224. THREE.ShaderChunk[ "fog_fragment" ],
  1225. "}"
  1226. ].join("\n")
  1227. },
  1228. 'phong': {
  1229. uniforms: THREE.UniformsUtils.merge( [
  1230. THREE.UniformsLib[ "common" ],
  1231. THREE.UniformsLib[ "bump" ],
  1232. THREE.UniformsLib[ "normalmap" ],
  1233. THREE.UniformsLib[ "fog" ],
  1234. THREE.UniformsLib[ "lights" ],
  1235. THREE.UniformsLib[ "shadowmap" ],
  1236. {
  1237. "ambient" : { type: "c", value: new THREE.Color( 0xffffff ) },
  1238. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  1239. "specular" : { type: "c", value: new THREE.Color( 0x111111 ) },
  1240. "shininess": { type: "f", value: 30 },
  1241. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  1242. }
  1243. ] ),
  1244. vertexShader: [
  1245. "#define PHONG",
  1246. "varying vec3 vViewPosition;",
  1247. "varying vec3 vNormal;",
  1248. THREE.ShaderChunk[ "map_pars_vertex" ],
  1249. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  1250. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  1251. THREE.ShaderChunk[ "lights_phong_pars_vertex" ],
  1252. THREE.ShaderChunk[ "color_pars_vertex" ],
  1253. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  1254. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  1255. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  1256. "void main() {",
  1257. THREE.ShaderChunk[ "map_vertex" ],
  1258. THREE.ShaderChunk[ "lightmap_vertex" ],
  1259. THREE.ShaderChunk[ "color_vertex" ],
  1260. THREE.ShaderChunk[ "morphnormal_vertex" ],
  1261. THREE.ShaderChunk[ "skinbase_vertex" ],
  1262. THREE.ShaderChunk[ "skinnormal_vertex" ],
  1263. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  1264. "vNormal = normalize( transformedNormal );",
  1265. THREE.ShaderChunk[ "morphtarget_vertex" ],
  1266. THREE.ShaderChunk[ "skinning_vertex" ],
  1267. THREE.ShaderChunk[ "default_vertex" ],
  1268. "vViewPosition = -mvPosition.xyz;",
  1269. THREE.ShaderChunk[ "worldpos_vertex" ],
  1270. THREE.ShaderChunk[ "envmap_vertex" ],
  1271. THREE.ShaderChunk[ "lights_phong_vertex" ],
  1272. THREE.ShaderChunk[ "shadowmap_vertex" ],
  1273. "}"
  1274. ].join("\n"),
  1275. fragmentShader: [
  1276. "uniform vec3 diffuse;",
  1277. "uniform float opacity;",
  1278. "uniform vec3 ambient;",
  1279. "uniform vec3 emissive;",
  1280. "uniform vec3 specular;",
  1281. "uniform float shininess;",
  1282. THREE.ShaderChunk[ "color_pars_fragment" ],
  1283. THREE.ShaderChunk[ "map_pars_fragment" ],
  1284. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  1285. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  1286. THREE.ShaderChunk[ "fog_pars_fragment" ],
  1287. THREE.ShaderChunk[ "lights_phong_pars_fragment" ],
  1288. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  1289. THREE.ShaderChunk[ "bumpmap_pars_fragment" ],
  1290. THREE.ShaderChunk[ "normalmap_pars_fragment" ],
  1291. THREE.ShaderChunk[ "specularmap_pars_fragment" ],
  1292. "void main() {",
  1293. "gl_FragColor = vec4( vec3 ( 1.0 ), opacity );",
  1294. THREE.ShaderChunk[ "map_fragment" ],
  1295. THREE.ShaderChunk[ "alphatest_fragment" ],
  1296. THREE.ShaderChunk[ "specularmap_fragment" ],
  1297. THREE.ShaderChunk[ "lights_phong_fragment" ],
  1298. THREE.ShaderChunk[ "lightmap_fragment" ],
  1299. THREE.ShaderChunk[ "color_fragment" ],
  1300. THREE.ShaderChunk[ "envmap_fragment" ],
  1301. THREE.ShaderChunk[ "shadowmap_fragment" ],
  1302. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  1303. THREE.ShaderChunk[ "fog_fragment" ],
  1304. "}"
  1305. ].join("\n")
  1306. },
  1307. 'particle_basic': {
  1308. uniforms: THREE.UniformsUtils.merge( [
  1309. THREE.UniformsLib[ "particle" ],
  1310. THREE.UniformsLib[ "shadowmap" ]
  1311. ] ),
  1312. vertexShader: [
  1313. "uniform float size;",
  1314. "uniform float scale;",
  1315. THREE.ShaderChunk[ "color_pars_vertex" ],
  1316. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  1317. "void main() {",
  1318. THREE.ShaderChunk[ "color_vertex" ],
  1319. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  1320. "#ifdef USE_SIZEATTENUATION",
  1321. "gl_PointSize = size * ( scale / length( mvPosition.xyz ) );",
  1322. "#else",
  1323. "gl_PointSize = size;",
  1324. "#endif",
  1325. "gl_Position = projectionMatrix * mvPosition;",
  1326. THREE.ShaderChunk[ "worldpos_vertex" ],
  1327. THREE.ShaderChunk[ "shadowmap_vertex" ],
  1328. "}"
  1329. ].join("\n"),
  1330. fragmentShader: [
  1331. "uniform vec3 psColor;",
  1332. "uniform float opacity;",
  1333. THREE.ShaderChunk[ "color_pars_fragment" ],
  1334. THREE.ShaderChunk[ "map_particle_pars_fragment" ],
  1335. THREE.ShaderChunk[ "fog_pars_fragment" ],
  1336. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  1337. "void main() {",
  1338. "gl_FragColor = vec4( psColor, opacity );",
  1339. THREE.ShaderChunk[ "map_particle_fragment" ],
  1340. THREE.ShaderChunk[ "alphatest_fragment" ],
  1341. THREE.ShaderChunk[ "color_fragment" ],
  1342. THREE.ShaderChunk[ "shadowmap_fragment" ],
  1343. THREE.ShaderChunk[ "fog_fragment" ],
  1344. "}"
  1345. ].join("\n")
  1346. },
  1347. 'dashed': {
  1348. uniforms: THREE.UniformsUtils.merge( [
  1349. THREE.UniformsLib[ "common" ],
  1350. THREE.UniformsLib[ "fog" ],
  1351. {
  1352. "scale": { type: "f", value: 1 },
  1353. "dashSize": { type: "f", value: 1 },
  1354. "totalSize": { type: "f", value: 2 }
  1355. }
  1356. ] ),
  1357. vertexShader: [
  1358. "uniform float scale;",
  1359. "attribute float lineDistance;",
  1360. "varying float vLineDistance;",
  1361. THREE.ShaderChunk[ "color_pars_vertex" ],
  1362. "void main() {",
  1363. THREE.ShaderChunk[ "color_vertex" ],
  1364. "vLineDistance = scale * lineDistance;",
  1365. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  1366. "gl_Position = projectionMatrix * mvPosition;",
  1367. "}"
  1368. ].join("\n"),
  1369. fragmentShader: [
  1370. "uniform vec3 diffuse;",
  1371. "uniform float opacity;",
  1372. "uniform float dashSize;",
  1373. "uniform float totalSize;",
  1374. "varying float vLineDistance;",
  1375. THREE.ShaderChunk[ "color_pars_fragment" ],
  1376. THREE.ShaderChunk[ "fog_pars_fragment" ],
  1377. "void main() {",
  1378. "if ( mod( vLineDistance, totalSize ) > dashSize ) {",
  1379. "discard;",
  1380. "}",
  1381. "gl_FragColor = vec4( diffuse, opacity );",
  1382. THREE.ShaderChunk[ "color_fragment" ],
  1383. THREE.ShaderChunk[ "fog_fragment" ],
  1384. "}"
  1385. ].join("\n")
  1386. },
  1387. 'depth': {
  1388. uniforms: {
  1389. "mNear": { type: "f", value: 1.0 },
  1390. "mFar" : { type: "f", value: 2000.0 },
  1391. "opacity" : { type: "f", value: 1.0 }
  1392. },
  1393. vertexShader: [
  1394. "void main() {",
  1395. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  1396. "}"
  1397. ].join("\n"),
  1398. fragmentShader: [
  1399. "uniform float mNear;",
  1400. "uniform float mFar;",
  1401. "uniform float opacity;",
  1402. "void main() {",
  1403. "float depth = gl_FragCoord.z / gl_FragCoord.w;",
  1404. "float color = 1.0 - smoothstep( mNear, mFar, depth );",
  1405. "gl_FragColor = vec4( vec3( color ), opacity );",
  1406. "}"
  1407. ].join("\n")
  1408. },
  1409. 'normal': {
  1410. uniforms: {
  1411. "opacity" : { type: "f", value: 1.0 }
  1412. },
  1413. vertexShader: [
  1414. "varying vec3 vNormal;",
  1415. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  1416. "void main() {",
  1417. "vNormal = normalize( normalMatrix * normal );",
  1418. THREE.ShaderChunk[ "morphtarget_vertex" ],
  1419. THREE.ShaderChunk[ "default_vertex" ],
  1420. "}"
  1421. ].join("\n"),
  1422. fragmentShader: [
  1423. "uniform float opacity;",
  1424. "varying vec3 vNormal;",
  1425. "void main() {",
  1426. "gl_FragColor = vec4( 0.5 * normalize( vNormal ) + 0.5, opacity );",
  1427. "}"
  1428. ].join("\n")
  1429. },
  1430. /* -------------------------------------------------------------------------
  1431. // Normal map shader
  1432. // - Blinn-Phong
  1433. // - normal + diffuse + specular + AO + displacement + reflection + shadow maps
  1434. // - point and directional lights (use with "lights: true" material option)
  1435. ------------------------------------------------------------------------- */
  1436. 'normalmap' : {
  1437. uniforms: THREE.UniformsUtils.merge( [
  1438. THREE.UniformsLib[ "fog" ],
  1439. THREE.UniformsLib[ "lights" ],
  1440. THREE.UniformsLib[ "shadowmap" ],
  1441. {
  1442. "enableAO" : { type: "i", value: 0 },
  1443. "enableDiffuse" : { type: "i", value: 0 },
  1444. "enableSpecular" : { type: "i", value: 0 },
  1445. "enableReflection": { type: "i", value: 0 },
  1446. "enableDisplacement": { type: "i", value: 0 },
  1447. "tDisplacement": { type: "t", value: null }, // must go first as this is vertex texture
  1448. "tDiffuse" : { type: "t", value: null },
  1449. "tCube" : { type: "t", value: null },
  1450. "tNormal" : { type: "t", value: null },
  1451. "tSpecular" : { type: "t", value: null },
  1452. "tAO" : { type: "t", value: null },
  1453. "uNormalScale": { type: "v2", value: new THREE.Vector2( 1, 1 ) },
  1454. "uDisplacementBias": { type: "f", value: 0.0 },
  1455. "uDisplacementScale": { type: "f", value: 1.0 },
  1456. "uDiffuseColor": { type: "c", value: new THREE.Color( 0xffffff ) },
  1457. "uSpecularColor": { type: "c", value: new THREE.Color( 0x111111 ) },
  1458. "uAmbientColor": { type: "c", value: new THREE.Color( 0xffffff ) },
  1459. "uShininess": { type: "f", value: 30 },
  1460. "uOpacity": { type: "f", value: 1 },
  1461. "useRefract": { type: "i", value: 0 },
  1462. "uRefractionRatio": { type: "f", value: 0.98 },
  1463. "uReflectivity": { type: "f", value: 0.5 },
  1464. "uOffset" : { type: "v2", value: new THREE.Vector2( 0, 0 ) },
  1465. "uRepeat" : { type: "v2", value: new THREE.Vector2( 1, 1 ) },
  1466. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  1467. }
  1468. ] ),
  1469. fragmentShader: [
  1470. "uniform vec3 uAmbientColor;",
  1471. "uniform vec3 uDiffuseColor;",
  1472. "uniform vec3 uSpecularColor;",
  1473. "uniform float uShininess;",
  1474. "uniform float uOpacity;",
  1475. "uniform bool enableDiffuse;",
  1476. "uniform bool enableSpecular;",
  1477. "uniform bool enableAO;",
  1478. "uniform bool enableReflection;",
  1479. "uniform sampler2D tDiffuse;",
  1480. "uniform sampler2D tNormal;",
  1481. "uniform sampler2D tSpecular;",
  1482. "uniform sampler2D tAO;",
  1483. "uniform samplerCube tCube;",
  1484. "uniform vec2 uNormalScale;",
  1485. "uniform bool useRefract;",
  1486. "uniform float uRefractionRatio;",
  1487. "uniform float uReflectivity;",
  1488. "varying vec3 vTangent;",
  1489. "varying vec3 vBinormal;",
  1490. "varying vec3 vNormal;",
  1491. "varying vec2 vUv;",
  1492. "uniform vec3 ambientLightColor;",
  1493. "#if MAX_DIR_LIGHTS > 0",
  1494. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  1495. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  1496. "#endif",
  1497. "#if MAX_HEMI_LIGHTS > 0",
  1498. "uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];",
  1499. "uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];",
  1500. "uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];",
  1501. "#endif",
  1502. "#if MAX_POINT_LIGHTS > 0",
  1503. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  1504. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  1505. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  1506. "#endif",
  1507. "#if MAX_SPOT_LIGHTS > 0",
  1508. "uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];",
  1509. "uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];",
  1510. "uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];",
  1511. "uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];",
  1512. "uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];",
  1513. "uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];",
  1514. "#endif",
  1515. "#ifdef WRAP_AROUND",
  1516. "uniform vec3 wrapRGB;",
  1517. "#endif",
  1518. "varying vec3 vWorldPosition;",
  1519. "varying vec3 vViewPosition;",
  1520. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  1521. THREE.ShaderChunk[ "fog_pars_fragment" ],
  1522. "void main() {",
  1523. "gl_FragColor = vec4( vec3( 1.0 ), uOpacity );",
  1524. "vec3 specularTex = vec3( 1.0 );",
  1525. "vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
  1526. "normalTex.xy *= uNormalScale;",
  1527. "normalTex = normalize( normalTex );",
  1528. "if( enableDiffuse ) {",
  1529. "#ifdef GAMMA_INPUT",
  1530. "vec4 texelColor = texture2D( tDiffuse, vUv );",
  1531. "texelColor.xyz *= texelColor.xyz;",
  1532. "gl_FragColor = gl_FragColor * texelColor;",
  1533. "#else",
  1534. "gl_FragColor = gl_FragColor * texture2D( tDiffuse, vUv );",
  1535. "#endif",
  1536. "}",
  1537. "if( enableAO ) {",
  1538. "#ifdef GAMMA_INPUT",
  1539. "vec4 aoColor = texture2D( tAO, vUv );",
  1540. "aoColor.xyz *= aoColor.xyz;",
  1541. "gl_FragColor.xyz = gl_FragColor.xyz * aoColor.xyz;",
  1542. "#else",
  1543. "gl_FragColor.xyz = gl_FragColor.xyz * texture2D( tAO, vUv ).xyz;",
  1544. "#endif",
  1545. "}",
  1546. "if( enableSpecular )",
  1547. "specularTex = texture2D( tSpecular, vUv ).xyz;",
  1548. "mat3 tsb = mat3( normalize( vTangent ), normalize( vBinormal ), normalize( vNormal ) );",
  1549. "vec3 finalNormal = tsb * normalTex;",
  1550. "#ifdef FLIP_SIDED",
  1551. "finalNormal = -finalNormal;",
  1552. "#endif",
  1553. "vec3 normal = normalize( finalNormal );",
  1554. "vec3 viewPosition = normalize( vViewPosition );",
  1555. // point lights
  1556. "#if MAX_POINT_LIGHTS > 0",
  1557. "vec3 pointDiffuse = vec3( 0.0 );",
  1558. "vec3 pointSpecular = vec3( 0.0 );",
  1559. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  1560. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  1561. "vec3 pointVector = lPosition.xyz + vViewPosition.xyz;",
  1562. "float pointDistance = 1.0;",
  1563. "if ( pointLightDistance[ i ] > 0.0 )",
  1564. "pointDistance = 1.0 - min( ( length( pointVector ) / pointLightDistance[ i ] ), 1.0 );",
  1565. "pointVector = normalize( pointVector );",
  1566. // diffuse
  1567. "#ifdef WRAP_AROUND",
  1568. "float pointDiffuseWeightFull = max( dot( normal, pointVector ), 0.0 );",
  1569. "float pointDiffuseWeightHalf = max( 0.5 * dot( normal, pointVector ) + 0.5, 0.0 );",
  1570. "vec3 pointDiffuseWeight = mix( vec3 ( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );",
  1571. "#else",
  1572. "float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
  1573. "#endif",
  1574. "pointDiffuse += pointDistance * pointLightColor[ i ] * uDiffuseColor * pointDiffuseWeight;",
  1575. // specular
  1576. "vec3 pointHalfVector = normalize( pointVector + viewPosition );",
  1577. "float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
  1578. "float pointSpecularWeight = specularTex.r * max( pow( pointDotNormalHalf, uShininess ), 0.0 );",
  1579. "#ifdef PHYSICALLY_BASED_SHADING",
  1580. // 2.0 => 2.0001 is hack to work around ANGLE bug
  1581. "float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",
  1582. "vec3 schlick = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( pointVector, pointHalfVector ), 5.0 );",
  1583. "pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * pointDistance * specularNormalization;",
  1584. "#else",
  1585. "pointSpecular += pointDistance * pointLightColor[ i ] * uSpecularColor * pointSpecularWeight * pointDiffuseWeight;",
  1586. "#endif",
  1587. "}",
  1588. "#endif",
  1589. // spot lights
  1590. "#if MAX_SPOT_LIGHTS > 0",
  1591. "vec3 spotDiffuse = vec3( 0.0 );",
  1592. "vec3 spotSpecular = vec3( 0.0 );",
  1593. "for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {",
  1594. "vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );",
  1595. "vec3 spotVector = lPosition.xyz + vViewPosition.xyz;",
  1596. "float spotDistance = 1.0;",
  1597. "if ( spotLightDistance[ i ] > 0.0 )",
  1598. "spotDistance = 1.0 - min( ( length( spotVector ) / spotLightDistance[ i ] ), 1.0 );",
  1599. "spotVector = normalize( spotVector );",
  1600. "float spotEffect = dot( spotLightDirection[ i ], normalize( spotLightPosition[ i ] - vWorldPosition ) );",
  1601. "if ( spotEffect > spotLightAngleCos[ i ] ) {",
  1602. "spotEffect = max( pow( spotEffect, spotLightExponent[ i ] ), 0.0 );",
  1603. // diffuse
  1604. "#ifdef WRAP_AROUND",
  1605. "float spotDiffuseWeightFull = max( dot( normal, spotVector ), 0.0 );",
  1606. "float spotDiffuseWeightHalf = max( 0.5 * dot( normal, spotVector ) + 0.5, 0.0 );",
  1607. "vec3 spotDiffuseWeight = mix( vec3 ( spotDiffuseWeightFull ), vec3( spotDiffuseWeightHalf ), wrapRGB );",
  1608. "#else",
  1609. "float spotDiffuseWeight = max( dot( normal, spotVector ), 0.0 );",
  1610. "#endif",
  1611. "spotDiffuse += spotDistance * spotLightColor[ i ] * uDiffuseColor * spotDiffuseWeight * spotEffect;",
  1612. // specular
  1613. "vec3 spotHalfVector = normalize( spotVector + viewPosition );",
  1614. "float spotDotNormalHalf = max( dot( normal, spotHalfVector ), 0.0 );",
  1615. "float spotSpecularWeight = specularTex.r * max( pow( spotDotNormalHalf, uShininess ), 0.0 );",
  1616. "#ifdef PHYSICALLY_BASED_SHADING",
  1617. // 2.0 => 2.0001 is hack to work around ANGLE bug
  1618. "float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",
  1619. "vec3 schlick = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( spotVector, spotHalfVector ), 5.0 );",
  1620. "spotSpecular += schlick * spotLightColor[ i ] * spotSpecularWeight * spotDiffuseWeight * spotDistance * specularNormalization * spotEffect;",
  1621. "#else",
  1622. "spotSpecular += spotDistance * spotLightColor[ i ] * uSpecularColor * spotSpecularWeight * spotDiffuseWeight * spotEffect;",
  1623. "#endif",
  1624. "}",
  1625. "}",
  1626. "#endif",
  1627. // directional lights
  1628. "#if MAX_DIR_LIGHTS > 0",
  1629. "vec3 dirDiffuse = vec3( 0.0 );",
  1630. "vec3 dirSpecular = vec3( 0.0 );",
  1631. "for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
  1632. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  1633. "vec3 dirVector = normalize( lDirection.xyz );",
  1634. // diffuse
  1635. "#ifdef WRAP_AROUND",
  1636. "float directionalLightWeightingFull = max( dot( normal, dirVector ), 0.0 );",
  1637. "float directionalLightWeightingHalf = max( 0.5 * dot( normal, dirVector ) + 0.5, 0.0 );",
  1638. "vec3 dirDiffuseWeight = mix( vec3( directionalLightWeightingFull ), vec3( directionalLightWeightingHalf ), wrapRGB );",
  1639. "#else",
  1640. "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  1641. "#endif",
  1642. "dirDiffuse += directionalLightColor[ i ] * uDiffuseColor * dirDiffuseWeight;",
  1643. // specular
  1644. "vec3 dirHalfVector = normalize( dirVector + viewPosition );",
  1645. "float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
  1646. "float dirSpecularWeight = specularTex.r * max( pow( dirDotNormalHalf, uShininess ), 0.0 );",
  1647. "#ifdef PHYSICALLY_BASED_SHADING",
  1648. // 2.0 => 2.0001 is hack to work around ANGLE bug
  1649. "float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",
  1650. "vec3 schlick = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( dirVector, dirHalfVector ), 5.0 );",
  1651. "dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;",
  1652. "#else",
  1653. "dirSpecular += directionalLightColor[ i ] * uSpecularColor * dirSpecularWeight * dirDiffuseWeight;",
  1654. "#endif",
  1655. "}",
  1656. "#endif",
  1657. // hemisphere lights
  1658. "#if MAX_HEMI_LIGHTS > 0",
  1659. "vec3 hemiDiffuse = vec3( 0.0 );",
  1660. "vec3 hemiSpecular = vec3( 0.0 );" ,
  1661. "for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {",
  1662. "vec4 lDirection = viewMatrix * vec4( hemisphereLightDirection[ i ], 0.0 );",
  1663. "vec3 lVector = normalize( lDirection.xyz );",
  1664. // diffuse
  1665. "float dotProduct = dot( normal, lVector );",
  1666. "float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
  1667. "vec3 hemiColor = mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
  1668. "hemiDiffuse += uDiffuseColor * hemiColor;",
  1669. // specular (sky light)
  1670. "vec3 hemiHalfVectorSky = normalize( lVector + viewPosition );",
  1671. "float hemiDotNormalHalfSky = 0.5 * dot( normal, hemiHalfVectorSky ) + 0.5;",
  1672. "float hemiSpecularWeightSky = specularTex.r * max( pow( hemiDotNormalHalfSky, uShininess ), 0.0 );",
  1673. // specular (ground light)
  1674. "vec3 lVectorGround = -lVector;",
  1675. "vec3 hemiHalfVectorGround = normalize( lVectorGround + viewPosition );",
  1676. "float hemiDotNormalHalfGround = 0.5 * dot( normal, hemiHalfVectorGround ) + 0.5;",
  1677. "float hemiSpecularWeightGround = specularTex.r * max( pow( hemiDotNormalHalfGround, uShininess ), 0.0 );",
  1678. "#ifdef PHYSICALLY_BASED_SHADING",
  1679. "float dotProductGround = dot( normal, lVectorGround );",
  1680. // 2.0 => 2.0001 is hack to work around ANGLE bug
  1681. "float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",
  1682. "vec3 schlickSky = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( lVector, hemiHalfVectorSky ), 5.0 );",
  1683. "vec3 schlickGround = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( lVectorGround, hemiHalfVectorGround ), 5.0 );",
  1684. "hemiSpecular += hemiColor * specularNormalization * ( schlickSky * hemiSpecularWeightSky * max( dotProduct, 0.0 ) + schlickGround * hemiSpecularWeightGround * max( dotProductGround, 0.0 ) );",
  1685. "#else",
  1686. "hemiSpecular += uSpecularColor * hemiColor * ( hemiSpecularWeightSky + hemiSpecularWeightGround ) * hemiDiffuseWeight;",
  1687. "#endif",
  1688. "}",
  1689. "#endif",
  1690. // all lights contribution summation
  1691. "vec3 totalDiffuse = vec3( 0.0 );",
  1692. "vec3 totalSpecular = vec3( 0.0 );",
  1693. "#if MAX_DIR_LIGHTS > 0",
  1694. "totalDiffuse += dirDiffuse;",
  1695. "totalSpecular += dirSpecular;",
  1696. "#endif",
  1697. "#if MAX_HEMI_LIGHTS > 0",
  1698. "totalDiffuse += hemiDiffuse;",
  1699. "totalSpecular += hemiSpecular;",
  1700. "#endif",
  1701. "#if MAX_POINT_LIGHTS > 0",
  1702. "totalDiffuse += pointDiffuse;",
  1703. "totalSpecular += pointSpecular;",
  1704. "#endif",
  1705. "#if MAX_SPOT_LIGHTS > 0",
  1706. "totalDiffuse += spotDiffuse;",
  1707. "totalSpecular += spotSpecular;",
  1708. "#endif",
  1709. "#ifdef METAL",
  1710. "gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * uAmbientColor + totalSpecular );",
  1711. "#else",
  1712. "gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * uAmbientColor ) + totalSpecular;",
  1713. "#endif",
  1714. "if ( enableReflection ) {",
  1715. "vec3 vReflect;",
  1716. "vec3 cameraToVertex = normalize( vWorldPosition - cameraPosition );",
  1717. "if ( useRefract ) {",
  1718. "vReflect = refract( cameraToVertex, normal, uRefractionRatio );",
  1719. "} else {",
  1720. "vReflect = reflect( cameraToVertex, normal );",
  1721. "}",
  1722. "vec4 cubeColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
  1723. "#ifdef GAMMA_INPUT",
  1724. "cubeColor.xyz *= cubeColor.xyz;",
  1725. "#endif",
  1726. "gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, specularTex.r * uReflectivity );",
  1727. "}",
  1728. THREE.ShaderChunk[ "shadowmap_fragment" ],
  1729. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  1730. THREE.ShaderChunk[ "fog_fragment" ],
  1731. "}"
  1732. ].join("\n"),
  1733. vertexShader: [
  1734. "attribute vec4 tangent;",
  1735. "uniform vec2 uOffset;",
  1736. "uniform vec2 uRepeat;",
  1737. "uniform bool enableDisplacement;",
  1738. "#ifdef VERTEX_TEXTURES",
  1739. "uniform sampler2D tDisplacement;",
  1740. "uniform float uDisplacementScale;",
  1741. "uniform float uDisplacementBias;",
  1742. "#endif",
  1743. "varying vec3 vTangent;",
  1744. "varying vec3 vBinormal;",
  1745. "varying vec3 vNormal;",
  1746. "varying vec2 vUv;",
  1747. "varying vec3 vWorldPosition;",
  1748. "varying vec3 vViewPosition;",
  1749. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  1750. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  1751. "void main() {",
  1752. THREE.ShaderChunk[ "skinbase_vertex" ],
  1753. THREE.ShaderChunk[ "skinnormal_vertex" ],
  1754. // normal, tangent and binormal vectors
  1755. "#ifdef USE_SKINNING",
  1756. "vNormal = normalize( normalMatrix * skinnedNormal.xyz );",
  1757. "vec4 skinnedTangent = skinMatrix * vec4( tangent.xyz, 0.0 );",
  1758. "vTangent = normalize( normalMatrix * skinnedTangent.xyz );",
  1759. "#else",
  1760. "vNormal = normalize( normalMatrix * normal );",
  1761. "vTangent = normalize( normalMatrix * tangent.xyz );",
  1762. "#endif",
  1763. "vBinormal = normalize( cross( vNormal, vTangent ) * tangent.w );",
  1764. "vUv = uv * uRepeat + uOffset;",
  1765. // displacement mapping
  1766. "vec3 displacedPosition;",
  1767. "#ifdef VERTEX_TEXTURES",
  1768. "if ( enableDisplacement ) {",
  1769. "vec3 dv = texture2D( tDisplacement, uv ).xyz;",
  1770. "float df = uDisplacementScale * dv.x + uDisplacementBias;",
  1771. "displacedPosition = position + normalize( normal ) * df;",
  1772. "} else {",
  1773. "#ifdef USE_SKINNING",
  1774. "vec4 skinVertex = vec4( position, 1.0 );",
  1775. "vec4 skinned = boneMatX * skinVertex * skinWeight.x;",
  1776. "skinned += boneMatY * skinVertex * skinWeight.y;",
  1777. "displacedPosition = skinned.xyz;",
  1778. "#else",
  1779. "displacedPosition = position;",
  1780. "#endif",
  1781. "}",
  1782. "#else",
  1783. "#ifdef USE_SKINNING",
  1784. "vec4 skinVertex = vec4( position, 1.0 );",
  1785. "vec4 skinned = boneMatX * skinVertex * skinWeight.x;",
  1786. "skinned += boneMatY * skinVertex * skinWeight.y;",
  1787. "displacedPosition = skinned.xyz;",
  1788. "#else",
  1789. "displacedPosition = position;",
  1790. "#endif",
  1791. "#endif",
  1792. //
  1793. "vec4 mvPosition = modelViewMatrix * vec4( displacedPosition, 1.0 );",
  1794. "vec4 worldPosition = modelMatrix * vec4( displacedPosition, 1.0 );",
  1795. "gl_Position = projectionMatrix * mvPosition;",
  1796. //
  1797. "vWorldPosition = worldPosition.xyz;",
  1798. "vViewPosition = -mvPosition.xyz;",
  1799. // shadows
  1800. "#ifdef USE_SHADOWMAP",
  1801. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  1802. "vShadowCoord[ i ] = shadowMatrix[ i ] * worldPosition;",
  1803. "}",
  1804. "#endif",
  1805. "}"
  1806. ].join("\n")
  1807. },
  1808. /* -------------------------------------------------------------------------
  1809. // Cube map shader
  1810. ------------------------------------------------------------------------- */
  1811. 'cube': {
  1812. uniforms: { "tCube": { type: "t", value: null },
  1813. "tFlip": { type: "f", value: -1 } },
  1814. vertexShader: [
  1815. "varying vec3 vWorldPosition;",
  1816. "void main() {",
  1817. "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  1818. "vWorldPosition = worldPosition.xyz;",
  1819. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  1820. "}"
  1821. ].join("\n"),
  1822. fragmentShader: [
  1823. "uniform samplerCube tCube;",
  1824. "uniform float tFlip;",
  1825. "varying vec3 vWorldPosition;",
  1826. "void main() {",
  1827. "gl_FragColor = textureCube( tCube, vec3( tFlip * vWorldPosition.x, vWorldPosition.yz ) );",
  1828. "}"
  1829. ].join("\n")
  1830. },
  1831. // Depth encoding into RGBA texture
  1832. // based on SpiderGL shadow map example
  1833. // http://spidergl.org/example.php?id=6
  1834. // originally from
  1835. // http://www.gamedev.net/topic/442138-packing-a-float-into-a-a8r8g8b8-texture-shader/page__whichpage__1%25EF%25BF%25BD
  1836. // see also here:
  1837. // http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
  1838. 'depthRGBA': {
  1839. uniforms: {},
  1840. vertexShader: [
  1841. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  1842. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  1843. "void main() {",
  1844. THREE.ShaderChunk[ "skinbase_vertex" ],
  1845. THREE.ShaderChunk[ "morphtarget_vertex" ],
  1846. THREE.ShaderChunk[ "skinning_vertex" ],
  1847. THREE.ShaderChunk[ "default_vertex" ],
  1848. "}"
  1849. ].join("\n"),
  1850. fragmentShader: [
  1851. "vec4 pack_depth( const in float depth ) {",
  1852. "const vec4 bit_shift = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 );",
  1853. "const vec4 bit_mask = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 );",
  1854. "vec4 res = fract( depth * bit_shift );",
  1855. "res -= res.xxyz * bit_mask;",
  1856. "return res;",
  1857. "}",
  1858. "void main() {",
  1859. "gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z );",
  1860. //"gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z / gl_FragCoord.w );",
  1861. //"float z = ( ( gl_FragCoord.z / gl_FragCoord.w ) - 3.0 ) / ( 4000.0 - 3.0 );",
  1862. //"gl_FragData[ 0 ] = pack_depth( z );",
  1863. //"gl_FragData[ 0 ] = vec4( z, z, z, 1.0 );",
  1864. "}"
  1865. ].join("\n")
  1866. }
  1867. };