WebGLShaders.js 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391
  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 {",
  72. "gl_FragColor.xyz = mix( gl_FragColor.xyz, gl_FragColor.xyz * cubeColor.xyz, specularStrength * reflectivity );",
  73. "}",
  74. "#endif"
  75. ].join("\n"),
  76. envmap_pars_vertex: [
  77. "#if defined( USE_ENVMAP ) && ! defined( USE_BUMPMAP ) && ! defined( USE_NORMALMAP )",
  78. "varying vec3 vReflect;",
  79. "uniform float refractionRatio;",
  80. "uniform bool useRefract;",
  81. "#endif"
  82. ].join("\n"),
  83. worldpos_vertex : [
  84. "#if defined( USE_ENVMAP ) || defined( PHONG ) || defined( LAMBERT ) || defined ( USE_SHADOWMAP )",
  85. "#ifdef USE_SKINNING",
  86. "vec4 mPosition = modelMatrix * skinned;",
  87. "#endif",
  88. "#if defined( USE_MORPHTARGETS ) && ! defined( USE_SKINNING )",
  89. "vec4 mPosition = modelMatrix * vec4( morphed, 1.0 );",
  90. "#endif",
  91. "#if ! defined( USE_MORPHTARGETS ) && ! defined( USE_SKINNING )",
  92. "vec4 mPosition = modelMatrix * vec4( position, 1.0 );",
  93. "#endif",
  94. "#endif"
  95. ].join("\n"),
  96. envmap_vertex : [
  97. "#if defined( USE_ENVMAP ) && ! defined( USE_BUMPMAP ) && ! defined( USE_NORMALMAP )",
  98. "vec3 nWorld = mat3( modelMatrix[ 0 ].xyz, modelMatrix[ 1 ].xyz, modelMatrix[ 2 ].xyz ) * objectNormal;",
  99. "if ( useRefract ) {",
  100. "vReflect = refract( normalize( mPosition.xyz - cameraPosition ), normalize( nWorld.xyz ), refractionRatio );",
  101. "} else {",
  102. "vReflect = reflect( normalize( mPosition.xyz - cameraPosition ), normalize( nWorld.xyz ) );",
  103. "}",
  104. "#endif"
  105. ].join("\n"),
  106. // COLOR MAP (particles)
  107. map_particle_pars_fragment: [
  108. "#ifdef USE_MAP",
  109. "uniform sampler2D map;",
  110. "#endif"
  111. ].join("\n"),
  112. map_particle_fragment: [
  113. "#ifdef USE_MAP",
  114. "gl_FragColor = gl_FragColor * texture2D( map, vec2( gl_PointCoord.x, 1.0 - gl_PointCoord.y ) );",
  115. "#endif"
  116. ].join("\n"),
  117. // COLOR MAP (triangles)
  118. map_pars_vertex: [
  119. "#if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP )",
  120. "varying vec2 vUv;",
  121. "uniform vec4 offsetRepeat;",
  122. "#endif"
  123. ].join("\n"),
  124. map_pars_fragment: [
  125. "#if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP )",
  126. "varying vec2 vUv;",
  127. "#endif",
  128. "#ifdef USE_MAP",
  129. "uniform sampler2D map;",
  130. "#endif",
  131. ].join("\n"),
  132. map_vertex: [
  133. "#if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP )",
  134. "vUv = uv * offsetRepeat.zw + offsetRepeat.xy;",
  135. "#endif"
  136. ].join("\n"),
  137. map_fragment: [
  138. "#ifdef USE_MAP",
  139. "#ifdef GAMMA_INPUT",
  140. "vec4 texelColor = texture2D( map, vUv );",
  141. "texelColor.xyz *= texelColor.xyz;",
  142. "gl_FragColor = gl_FragColor * texelColor;",
  143. "#else",
  144. "gl_FragColor = gl_FragColor * texture2D( map, vUv );",
  145. "#endif",
  146. "#endif"
  147. ].join("\n"),
  148. // LIGHT MAP
  149. lightmap_pars_fragment: [
  150. "#ifdef USE_LIGHTMAP",
  151. "varying vec2 vUv2;",
  152. "uniform sampler2D lightMap;",
  153. "#endif"
  154. ].join("\n"),
  155. lightmap_pars_vertex: [
  156. "#ifdef USE_LIGHTMAP",
  157. "varying vec2 vUv2;",
  158. "#endif"
  159. ].join("\n"),
  160. lightmap_fragment: [
  161. "#ifdef USE_LIGHTMAP",
  162. "gl_FragColor = gl_FragColor * texture2D( lightMap, vUv2 );",
  163. "#endif"
  164. ].join("\n"),
  165. lightmap_vertex: [
  166. "#ifdef USE_LIGHTMAP",
  167. "vUv2 = uv2;",
  168. "#endif"
  169. ].join("\n"),
  170. // BUMP MAP
  171. bumpmap_pars_fragment: [
  172. "#ifdef USE_BUMPMAP",
  173. "uniform sampler2D bumpMap;",
  174. "uniform float bumpScale;",
  175. // Derivative maps - bump mapping unparametrized surfaces by Morten Mikkelsen
  176. // http://mmikkelsen3d.blogspot.sk/2011/07/derivative-maps.html
  177. // Evaluate the derivative of the height w.r.t. screen-space using forward differencing (listing 2)
  178. "vec2 dHdxy_fwd() {",
  179. "vec2 dSTdx = dFdx( vUv );",
  180. "vec2 dSTdy = dFdy( vUv );",
  181. "float Hll = bumpScale * texture2D( bumpMap, vUv ).x;",
  182. "float dBx = bumpScale * texture2D( bumpMap, vUv + dSTdx ).x - Hll;",
  183. "float dBy = bumpScale * texture2D( bumpMap, vUv + dSTdy ).x - Hll;",
  184. "return vec2( dBx, dBy );",
  185. "}",
  186. "vec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy ) {",
  187. "vec3 vSigmaX = dFdx( surf_pos );",
  188. "vec3 vSigmaY = dFdy( surf_pos );",
  189. "vec3 vN = surf_norm;", // normalized
  190. "vec3 R1 = cross( vSigmaY, vN );",
  191. "vec3 R2 = cross( vN, vSigmaX );",
  192. "float fDet = dot( vSigmaX, R1 );",
  193. "vec3 vGrad = sign( fDet ) * ( dHdxy.x * R1 + dHdxy.y * R2 );",
  194. "return normalize( abs( fDet ) * surf_norm - vGrad );",
  195. "}",
  196. "#endif"
  197. ].join("\n"),
  198. // NORMAL MAP
  199. normalmap_pars_fragment: [
  200. "#ifdef USE_NORMALMAP",
  201. "uniform sampler2D normalMap;",
  202. "uniform vec2 normalScale;",
  203. // Per-Pixel Tangent Space Normal Mapping
  204. // http://hacksoflife.blogspot.ch/2009/11/per-pixel-tangent-space-normal-mapping.html
  205. "vec3 perturbNormal2Arb( vec3 eye_pos, vec3 surf_norm ) {",
  206. "vec3 q0 = dFdx( eye_pos.xyz );",
  207. "vec3 q1 = dFdy( eye_pos.xyz );",
  208. "vec2 st0 = dFdx( vUv.st );",
  209. "vec2 st1 = dFdy( vUv.st );",
  210. "vec3 S = normalize( q0 * st1.t - q1 * st0.t );",
  211. "vec3 T = normalize( -q0 * st1.s + q1 * st0.s );",
  212. "vec3 N = normalize( surf_norm );",
  213. "vec3 mapN = texture2D( normalMap, vUv ).xyz * 2.0 - 1.0;",
  214. "mapN.xy = normalScale * mapN.xy;",
  215. "mat3 tsn = mat3( S, T, N );",
  216. "return normalize( tsn * mapN );",
  217. "}",
  218. "#endif"
  219. ].join("\n"),
  220. // SPECULAR MAP
  221. specularmap_pars_fragment: [
  222. "#ifdef USE_SPECULARMAP",
  223. "uniform sampler2D specularMap;",
  224. "#endif"
  225. ].join("\n"),
  226. specularmap_fragment: [
  227. "float specularStrength;",
  228. "#ifdef USE_SPECULARMAP",
  229. "vec4 texelSpecular = texture2D( specularMap, vUv );",
  230. "specularStrength = texelSpecular.r;",
  231. "#else",
  232. "specularStrength = 1.0;",
  233. "#endif"
  234. ].join("\n"),
  235. // LIGHTS LAMBERT
  236. lights_lambert_pars_vertex: [
  237. "uniform vec3 ambient;",
  238. "uniform vec3 diffuse;",
  239. "uniform vec3 emissive;",
  240. "uniform vec3 ambientLightColor;",
  241. "#if MAX_DIR_LIGHTS > 0",
  242. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  243. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  244. "#endif",
  245. "#if MAX_HEMI_LIGHTS > 0",
  246. "uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];",
  247. "uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];",
  248. "uniform vec3 hemisphereLightPosition[ MAX_HEMI_LIGHTS ];",
  249. "#endif",
  250. "#if MAX_POINT_LIGHTS > 0",
  251. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  252. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  253. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  254. "#endif",
  255. "#if MAX_SPOT_LIGHTS > 0",
  256. "uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];",
  257. "uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];",
  258. "uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];",
  259. "uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];",
  260. "uniform float spotLightAngle[ MAX_SPOT_LIGHTS ];",
  261. "uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];",
  262. "#endif",
  263. "#ifdef WRAP_AROUND",
  264. "uniform vec3 wrapRGB;",
  265. "#endif"
  266. ].join("\n"),
  267. lights_lambert_vertex: [
  268. "vLightFront = vec3( 0.0 );",
  269. "#ifdef DOUBLE_SIDED",
  270. "vLightBack = vec3( 0.0 );",
  271. "#endif",
  272. "transformedNormal = normalize( transformedNormal );",
  273. "#if MAX_DIR_LIGHTS > 0",
  274. "for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {",
  275. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  276. "vec3 dirVector = normalize( lDirection.xyz );",
  277. "float dotProduct = dot( transformedNormal, dirVector );",
  278. "vec3 directionalLightWeighting = vec3( max( dotProduct, 0.0 ) );",
  279. "#ifdef DOUBLE_SIDED",
  280. "vec3 directionalLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );",
  281. "#ifdef WRAP_AROUND",
  282. "vec3 directionalLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );",
  283. "#endif",
  284. "#endif",
  285. "#ifdef WRAP_AROUND",
  286. "vec3 directionalLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );",
  287. "directionalLightWeighting = mix( directionalLightWeighting, directionalLightWeightingHalf, wrapRGB );",
  288. "#ifdef DOUBLE_SIDED",
  289. "directionalLightWeightingBack = mix( directionalLightWeightingBack, directionalLightWeightingHalfBack, wrapRGB );",
  290. "#endif",
  291. "#endif",
  292. "vLightFront += directionalLightColor[ i ] * directionalLightWeighting;",
  293. "#ifdef DOUBLE_SIDED",
  294. "vLightBack += directionalLightColor[ i ] * directionalLightWeightingBack;",
  295. "#endif",
  296. "}",
  297. "#endif",
  298. "#if MAX_POINT_LIGHTS > 0",
  299. "for( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  300. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  301. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  302. "float lDistance = 1.0;",
  303. "if ( pointLightDistance[ i ] > 0.0 )",
  304. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  305. "lVector = normalize( lVector );",
  306. "float dotProduct = dot( transformedNormal, lVector );",
  307. "vec3 pointLightWeighting = vec3( max( dotProduct, 0.0 ) );",
  308. "#ifdef DOUBLE_SIDED",
  309. "vec3 pointLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );",
  310. "#ifdef WRAP_AROUND",
  311. "vec3 pointLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );",
  312. "#endif",
  313. "#endif",
  314. "#ifdef WRAP_AROUND",
  315. "vec3 pointLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );",
  316. "pointLightWeighting = mix( pointLightWeighting, pointLightWeightingHalf, wrapRGB );",
  317. "#ifdef DOUBLE_SIDED",
  318. "pointLightWeightingBack = mix( pointLightWeightingBack, pointLightWeightingHalfBack, wrapRGB );",
  319. "#endif",
  320. "#endif",
  321. "vLightFront += pointLightColor[ i ] * pointLightWeighting * lDistance;",
  322. "#ifdef DOUBLE_SIDED",
  323. "vLightBack += pointLightColor[ i ] * pointLightWeightingBack * lDistance;",
  324. "#endif",
  325. "}",
  326. "#endif",
  327. "#if MAX_SPOT_LIGHTS > 0",
  328. "for( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {",
  329. "vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );",
  330. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  331. "lVector = normalize( lVector );",
  332. "float spotEffect = dot( spotLightDirection[ i ], normalize( spotLightPosition[ i ] - mPosition.xyz ) );",
  333. "if ( spotEffect > spotLightAngle[ i ] ) {",
  334. "spotEffect = pow( spotEffect, spotLightExponent[ i ] );",
  335. "float lDistance = 1.0;",
  336. "if ( spotLightDistance[ i ] > 0.0 )",
  337. "lDistance = 1.0 - min( ( length( lVector ) / spotLightDistance[ i ] ), 1.0 );",
  338. "float dotProduct = dot( transformedNormal, lVector );",
  339. "vec3 spotLightWeighting = vec3( max( dotProduct, 0.0 ) );",
  340. "#ifdef DOUBLE_SIDED",
  341. "vec3 spotLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );",
  342. "#ifdef WRAP_AROUND",
  343. "vec3 spotLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );",
  344. "#endif",
  345. "#endif",
  346. "#ifdef WRAP_AROUND",
  347. "vec3 spotLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );",
  348. "spotLightWeighting = mix( spotLightWeighting, spotLightWeightingHalf, wrapRGB );",
  349. "#ifdef DOUBLE_SIDED",
  350. "spotLightWeightingBack = mix( spotLightWeightingBack, spotLightWeightingHalfBack, wrapRGB );",
  351. "#endif",
  352. "#endif",
  353. "vLightFront += spotLightColor[ i ] * spotLightWeighting * lDistance * spotEffect;",
  354. "#ifdef DOUBLE_SIDED",
  355. "vLightBack += spotLightColor[ i ] * spotLightWeightingBack * lDistance * spotEffect;",
  356. "#endif",
  357. "}",
  358. "}",
  359. "#endif",
  360. "#if MAX_HEMI_LIGHTS > 0",
  361. "for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {",
  362. "vec4 lPosition = viewMatrix * vec4( hemisphereLightPosition[ i ], 1.0 );",
  363. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  364. "lVector = normalize( lVector );",
  365. "float dotProduct = dot( transformedNormal, lVector );",
  366. "float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
  367. "float hemiDiffuseWeightBack = -0.5 * dotProduct + 0.5;",
  368. "vLightFront += mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
  369. "#ifdef DOUBLE_SIDED",
  370. "vLightBack += mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeightBack );",
  371. "#endif",
  372. "}",
  373. "#endif",
  374. "vLightFront = vLightFront * diffuse + ambient * ambientLightColor + emissive;",
  375. "#ifdef DOUBLE_SIDED",
  376. "vLightBack = vLightBack * diffuse + ambient * ambientLightColor + emissive;",
  377. "#endif"
  378. ].join("\n"),
  379. // LIGHTS PHONG
  380. lights_phong_pars_vertex: [
  381. "#ifndef PHONG_PER_PIXEL",
  382. "#if MAX_POINT_LIGHTS > 0",
  383. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  384. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  385. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  386. "#endif",
  387. "#if MAX_SPOT_LIGHTS > 0",
  388. "uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];",
  389. "uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];",
  390. "varying vec4 vSpotLight[ MAX_SPOT_LIGHTS ];",
  391. "#endif",
  392. "#endif",
  393. "#if MAX_SPOT_LIGHTS > 0 || defined( USE_BUMPMAP )",
  394. "varying vec3 vWorldPosition;",
  395. "#endif"
  396. ].join("\n"),
  397. lights_phong_vertex: [
  398. "#ifndef PHONG_PER_PIXEL",
  399. "#if MAX_POINT_LIGHTS > 0",
  400. "for( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  401. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  402. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  403. "float lDistance = 1.0;",
  404. "if ( pointLightDistance[ i ] > 0.0 )",
  405. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  406. "vPointLight[ i ] = vec4( lVector, lDistance );",
  407. "}",
  408. "#endif",
  409. "#if MAX_SPOT_LIGHTS > 0",
  410. "for( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {",
  411. "vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );",
  412. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  413. "float lDistance = 1.0;",
  414. "if ( spotLightDistance[ i ] > 0.0 )",
  415. "lDistance = 1.0 - min( ( length( lVector ) / spotLightDistance[ i ] ), 1.0 );",
  416. "vSpotLight[ i ] = vec4( lVector, lDistance );",
  417. "}",
  418. "#endif",
  419. "#endif",
  420. "#if MAX_SPOT_LIGHTS > 0 || defined( USE_BUMPMAP )",
  421. "vWorldPosition = mPosition.xyz;",
  422. "#endif"
  423. ].join("\n"),
  424. lights_phong_pars_fragment: [
  425. "uniform vec3 ambientLightColor;",
  426. "#if MAX_DIR_LIGHTS > 0",
  427. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  428. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  429. "#endif",
  430. "#if MAX_HEMI_LIGHTS > 0",
  431. "uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];",
  432. "uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];",
  433. "uniform vec3 hemisphereLightPosition[ MAX_HEMI_LIGHTS ];",
  434. "#endif",
  435. "#if MAX_POINT_LIGHTS > 0",
  436. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  437. "#ifdef PHONG_PER_PIXEL",
  438. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  439. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  440. "#else",
  441. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  442. "#endif",
  443. "#endif",
  444. "#if MAX_SPOT_LIGHTS > 0",
  445. "uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];",
  446. "uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];",
  447. "uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];",
  448. "uniform float spotLightAngle[ MAX_SPOT_LIGHTS ];",
  449. "uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];",
  450. "#ifdef PHONG_PER_PIXEL",
  451. "uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];",
  452. "#else",
  453. "varying vec4 vSpotLight[ MAX_SPOT_LIGHTS ];",
  454. "#endif",
  455. "#endif",
  456. "#if MAX_SPOT_LIGHTS > 0 || defined( USE_BUMPMAP )",
  457. "varying vec3 vWorldPosition;",
  458. "#endif",
  459. "#ifdef WRAP_AROUND",
  460. "uniform vec3 wrapRGB;",
  461. "#endif",
  462. "varying vec3 vViewPosition;",
  463. "varying vec3 vNormal;"
  464. ].join("\n"),
  465. lights_phong_fragment: [
  466. "vec3 normal = normalize( vNormal );",
  467. "vec3 viewPosition = normalize( vViewPosition );",
  468. "#ifdef DOUBLE_SIDED",
  469. "normal = normal * ( -1.0 + 2.0 * float( gl_FrontFacing ) );",
  470. "#endif",
  471. "#ifdef USE_NORMALMAP",
  472. "normal = perturbNormal2Arb( -viewPosition, normal );",
  473. "#elif defined( USE_BUMPMAP )",
  474. "normal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd() );",
  475. "#endif",
  476. "#if MAX_POINT_LIGHTS > 0",
  477. "vec3 pointDiffuse = vec3( 0.0 );",
  478. "vec3 pointSpecular = vec3( 0.0 );",
  479. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  480. "#ifdef PHONG_PER_PIXEL",
  481. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  482. "vec3 lVector = lPosition.xyz + vViewPosition.xyz;",
  483. "float lDistance = 1.0;",
  484. "if ( pointLightDistance[ i ] > 0.0 )",
  485. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  486. "lVector = normalize( lVector );",
  487. "#else",
  488. "vec3 lVector = normalize( vPointLight[ i ].xyz );",
  489. "float lDistance = vPointLight[ i ].w;",
  490. "#endif",
  491. // diffuse
  492. "float dotProduct = dot( normal, lVector );",
  493. "#ifdef WRAP_AROUND",
  494. "float pointDiffuseWeightFull = max( dotProduct, 0.0 );",
  495. "float pointDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );",
  496. "vec3 pointDiffuseWeight = mix( vec3 ( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );",
  497. "#else",
  498. "float pointDiffuseWeight = max( dotProduct, 0.0 );",
  499. "#endif",
  500. "pointDiffuse += diffuse * pointLightColor[ i ] * pointDiffuseWeight * lDistance;",
  501. // specular
  502. "vec3 pointHalfVector = normalize( lVector + viewPosition );",
  503. "float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
  504. "float pointSpecularWeight = specularStrength * max( pow( pointDotNormalHalf, shininess ), 0.0 );",
  505. "#ifdef PHYSICALLY_BASED_SHADING",
  506. // 2.0 => 2.0001 is hack to work around ANGLE bug
  507. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  508. "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVector, pointHalfVector ), 5.0 );",
  509. "pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * lDistance * specularNormalization;",
  510. "#else",
  511. "pointSpecular += specular * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * lDistance;",
  512. "#endif",
  513. "}",
  514. "#endif",
  515. "#if MAX_SPOT_LIGHTS > 0",
  516. "vec3 spotDiffuse = vec3( 0.0 );",
  517. "vec3 spotSpecular = vec3( 0.0 );",
  518. "for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {",
  519. "#ifdef PHONG_PER_PIXEL",
  520. "vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );",
  521. "vec3 lVector = lPosition.xyz + vViewPosition.xyz;",
  522. "float lDistance = 1.0;",
  523. "if ( spotLightDistance[ i ] > 0.0 )",
  524. "lDistance = 1.0 - min( ( length( lVector ) / spotLightDistance[ i ] ), 1.0 );",
  525. "lVector = normalize( lVector );",
  526. "#else",
  527. "vec3 lVector = normalize( vSpotLight[ i ].xyz );",
  528. "float lDistance = vSpotLight[ i ].w;",
  529. "#endif",
  530. "float spotEffect = dot( spotLightDirection[ i ], normalize( spotLightPosition[ i ] - vWorldPosition ) );",
  531. "if ( spotEffect > spotLightAngle[ i ] ) {",
  532. "spotEffect = pow( spotEffect, spotLightExponent[ i ] );",
  533. // diffuse
  534. "float dotProduct = dot( normal, lVector );",
  535. "#ifdef WRAP_AROUND",
  536. "float spotDiffuseWeightFull = max( dotProduct, 0.0 );",
  537. "float spotDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );",
  538. "vec3 spotDiffuseWeight = mix( vec3 ( spotDiffuseWeightFull ), vec3( spotDiffuseWeightHalf ), wrapRGB );",
  539. "#else",
  540. "float spotDiffuseWeight = max( dotProduct, 0.0 );",
  541. "#endif",
  542. "spotDiffuse += diffuse * spotLightColor[ i ] * spotDiffuseWeight * lDistance * spotEffect;",
  543. // specular
  544. "vec3 spotHalfVector = normalize( lVector + viewPosition );",
  545. "float spotDotNormalHalf = max( dot( normal, spotHalfVector ), 0.0 );",
  546. "float spotSpecularWeight = specularStrength * max( pow( spotDotNormalHalf, shininess ), 0.0 );",
  547. "#ifdef PHYSICALLY_BASED_SHADING",
  548. // 2.0 => 2.0001 is hack to work around ANGLE bug
  549. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  550. "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVector, spotHalfVector ), 5.0 );",
  551. "spotSpecular += schlick * spotLightColor[ i ] * spotSpecularWeight * spotDiffuseWeight * lDistance * specularNormalization * spotEffect;",
  552. "#else",
  553. "spotSpecular += specular * spotLightColor[ i ] * spotSpecularWeight * spotDiffuseWeight * lDistance * spotEffect;",
  554. "#endif",
  555. "}",
  556. "}",
  557. "#endif",
  558. "#if MAX_DIR_LIGHTS > 0",
  559. "vec3 dirDiffuse = vec3( 0.0 );",
  560. "vec3 dirSpecular = vec3( 0.0 );" ,
  561. "for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {",
  562. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  563. "vec3 dirVector = normalize( lDirection.xyz );",
  564. // diffuse
  565. "float dotProduct = dot( normal, dirVector );",
  566. "#ifdef WRAP_AROUND",
  567. "float dirDiffuseWeightFull = max( dotProduct, 0.0 );",
  568. "float dirDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );",
  569. "vec3 dirDiffuseWeight = mix( vec3( dirDiffuseWeightFull ), vec3( dirDiffuseWeightHalf ), wrapRGB );",
  570. "#else",
  571. "float dirDiffuseWeight = max( dotProduct, 0.0 );",
  572. "#endif",
  573. "dirDiffuse += diffuse * directionalLightColor[ i ] * dirDiffuseWeight;",
  574. // specular
  575. "vec3 dirHalfVector = normalize( dirVector + viewPosition );",
  576. "float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
  577. "float dirSpecularWeight = specularStrength * max( pow( dirDotNormalHalf, shininess ), 0.0 );",
  578. "#ifdef PHYSICALLY_BASED_SHADING",
  579. /*
  580. // fresnel term from skin shader
  581. "const float F0 = 0.128;",
  582. "float base = 1.0 - dot( viewPosition, dirHalfVector );",
  583. "float exponential = pow( base, 5.0 );",
  584. "float fresnel = exponential + F0 * ( 1.0 - exponential );",
  585. */
  586. /*
  587. // fresnel term from fresnel shader
  588. "const float mFresnelBias = 0.08;",
  589. "const float mFresnelScale = 0.3;",
  590. "const float mFresnelPower = 5.0;",
  591. "float fresnel = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( -viewPosition ), normal ), mFresnelPower );",
  592. */
  593. // 2.0 => 2.0001 is hack to work around ANGLE bug
  594. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  595. //"dirSpecular += specular * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization * fresnel;",
  596. "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( dirVector, dirHalfVector ), 5.0 );",
  597. "dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;",
  598. "#else",
  599. "dirSpecular += specular * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight;",
  600. "#endif",
  601. "}",
  602. "#endif",
  603. "#if MAX_HEMI_LIGHTS > 0",
  604. "vec3 hemiDiffuse = vec3( 0.0 );",
  605. "vec3 hemiSpecular = vec3( 0.0 );" ,
  606. "for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {",
  607. "vec4 lPosition = viewMatrix * vec4( hemisphereLightPosition[ i ], 1.0 );",
  608. "vec3 lVector = normalize( lPosition.xyz + vViewPosition.xyz );",
  609. // diffuse
  610. "float dotProduct = dot( normal, lVector );",
  611. "float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
  612. "vec3 hemiColor = mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
  613. "hemiDiffuse += diffuse * hemiColor;",
  614. // specular (sky light)
  615. "vec3 hemiHalfVectorSky = normalize( lVector + viewPosition );",
  616. "float hemiDotNormalHalfSky = 0.5 * dot( normal, hemiHalfVectorSky ) + 0.5;",
  617. "float hemiSpecularWeightSky = specularStrength * max( pow( hemiDotNormalHalfSky, shininess ), 0.0 );",
  618. // specular (ground light)
  619. "vec3 lVectorGround = normalize( -lPosition.xyz + vViewPosition.xyz );",
  620. "vec3 hemiHalfVectorGround = normalize( lVectorGround + viewPosition );",
  621. "float hemiDotNormalHalfGround = 0.5 * dot( normal, hemiHalfVectorGround ) + 0.5;",
  622. "float hemiSpecularWeightGround = specularStrength * max( pow( hemiDotNormalHalfGround, shininess ), 0.0 );",
  623. "#ifdef PHYSICALLY_BASED_SHADING",
  624. "float dotProductGround = dot( normal, lVectorGround );",
  625. // 2.0 => 2.0001 is hack to work around ANGLE bug
  626. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  627. "vec3 schlickSky = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVector, hemiHalfVectorSky ), 5.0 );",
  628. "vec3 schlickGround = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVectorGround, hemiHalfVectorGround ), 5.0 );",
  629. "hemiSpecular += hemiColor * specularNormalization * ( schlickSky * hemiSpecularWeightSky * max( dotProduct, 0.0 ) + schlickGround * hemiSpecularWeightGround * max( dotProductGround, 0.0 ) );",
  630. "#else",
  631. "hemiSpecular += specular * hemiColor * ( hemiSpecularWeightSky + hemiSpecularWeightGround ) * hemiDiffuseWeight;",
  632. "#endif",
  633. "}",
  634. "#endif",
  635. "vec3 totalDiffuse = vec3( 0.0 );",
  636. "vec3 totalSpecular = vec3( 0.0 );",
  637. "#if MAX_DIR_LIGHTS > 0",
  638. "totalDiffuse += dirDiffuse;",
  639. "totalSpecular += dirSpecular;",
  640. "#endif",
  641. "#if MAX_HEMI_LIGHTS > 0",
  642. "totalDiffuse += hemiDiffuse;",
  643. "totalSpecular += hemiSpecular;",
  644. "#endif",
  645. "#if MAX_POINT_LIGHTS > 0",
  646. "totalDiffuse += pointDiffuse;",
  647. "totalSpecular += pointSpecular;",
  648. "#endif",
  649. "#if MAX_SPOT_LIGHTS > 0",
  650. "totalDiffuse += spotDiffuse;",
  651. "totalSpecular += spotSpecular;",
  652. "#endif",
  653. "#ifdef METAL",
  654. "gl_FragColor.xyz = gl_FragColor.xyz * ( emissive + totalDiffuse + ambientLightColor * ambient + totalSpecular );",
  655. "#else",
  656. "gl_FragColor.xyz = gl_FragColor.xyz * ( emissive + totalDiffuse + ambientLightColor * ambient ) + totalSpecular;",
  657. "#endif"
  658. ].join("\n"),
  659. // VERTEX COLORS
  660. color_pars_fragment: [
  661. "#ifdef USE_COLOR",
  662. "varying vec3 vColor;",
  663. "#endif"
  664. ].join("\n"),
  665. color_fragment: [
  666. "#ifdef USE_COLOR",
  667. "gl_FragColor = gl_FragColor * vec4( vColor, opacity );",
  668. "#endif"
  669. ].join("\n"),
  670. color_pars_vertex: [
  671. "#ifdef USE_COLOR",
  672. "varying vec3 vColor;",
  673. "#endif"
  674. ].join("\n"),
  675. color_vertex: [
  676. "#ifdef USE_COLOR",
  677. "#ifdef GAMMA_INPUT",
  678. "vColor = color * color;",
  679. "#else",
  680. "vColor = color;",
  681. "#endif",
  682. "#endif"
  683. ].join("\n"),
  684. // SKINNING
  685. skinning_pars_vertex: [
  686. "#ifdef USE_SKINNING",
  687. "#ifdef BONE_TEXTURE",
  688. "uniform sampler2D boneTexture;",
  689. "mat4 getBoneMatrix( const in float i ) {",
  690. "float j = i * 4.0;",
  691. "float x = mod( j, N_BONE_PIXEL_X );",
  692. "float y = floor( j / N_BONE_PIXEL_X );",
  693. "const float dx = 1.0 / N_BONE_PIXEL_X;",
  694. "const float dy = 1.0 / N_BONE_PIXEL_Y;",
  695. "y = dy * ( y + 0.5 );",
  696. "vec4 v1 = texture2D( boneTexture, vec2( dx * ( x + 0.5 ), y ) );",
  697. "vec4 v2 = texture2D( boneTexture, vec2( dx * ( x + 1.5 ), y ) );",
  698. "vec4 v3 = texture2D( boneTexture, vec2( dx * ( x + 2.5 ), y ) );",
  699. "vec4 v4 = texture2D( boneTexture, vec2( dx * ( x + 3.5 ), y ) );",
  700. "mat4 bone = mat4( v1, v2, v3, v4 );",
  701. "return bone;",
  702. "}",
  703. "#else",
  704. "uniform mat4 boneGlobalMatrices[ MAX_BONES ];",
  705. "mat4 getBoneMatrix( const in float i ) {",
  706. "mat4 bone = boneGlobalMatrices[ int(i) ];",
  707. "return bone;",
  708. "}",
  709. "#endif",
  710. "#endif"
  711. ].join("\n"),
  712. skinbase_vertex: [
  713. "#ifdef USE_SKINNING",
  714. "mat4 boneMatX = getBoneMatrix( skinIndex.x );",
  715. "mat4 boneMatY = getBoneMatrix( skinIndex.y );",
  716. "#endif"
  717. ].join("\n"),
  718. skinning_vertex: [
  719. "#ifdef USE_SKINNING",
  720. "#ifdef USE_MORPHTARGETS",
  721. "vec4 skinVertex = vec4( morphed, 1.0 );",
  722. "#else",
  723. "vec4 skinVertex = vec4( position, 1.0 );",
  724. "#endif",
  725. "vec4 skinned = boneMatX * skinVertex * skinWeight.x;",
  726. "skinned += boneMatY * skinVertex * skinWeight.y;",
  727. "#endif"
  728. ].join("\n"),
  729. // MORPHING
  730. morphtarget_pars_vertex: [
  731. "#ifdef USE_MORPHTARGETS",
  732. "#ifndef USE_MORPHNORMALS",
  733. "uniform float morphTargetInfluences[ 8 ];",
  734. "#else",
  735. "uniform float morphTargetInfluences[ 4 ];",
  736. "#endif",
  737. "#endif"
  738. ].join("\n"),
  739. morphtarget_vertex: [
  740. "#ifdef USE_MORPHTARGETS",
  741. "vec3 morphed = vec3( 0.0 );",
  742. "morphed += ( morphTarget0 - position ) * morphTargetInfluences[ 0 ];",
  743. "morphed += ( morphTarget1 - position ) * morphTargetInfluences[ 1 ];",
  744. "morphed += ( morphTarget2 - position ) * morphTargetInfluences[ 2 ];",
  745. "morphed += ( morphTarget3 - position ) * morphTargetInfluences[ 3 ];",
  746. "#ifndef USE_MORPHNORMALS",
  747. "morphed += ( morphTarget4 - position ) * morphTargetInfluences[ 4 ];",
  748. "morphed += ( morphTarget5 - position ) * morphTargetInfluences[ 5 ];",
  749. "morphed += ( morphTarget6 - position ) * morphTargetInfluences[ 6 ];",
  750. "morphed += ( morphTarget7 - position ) * morphTargetInfluences[ 7 ];",
  751. "#endif",
  752. "morphed += position;",
  753. "#endif"
  754. ].join("\n"),
  755. default_vertex : [
  756. "vec4 mvPosition;",
  757. "#ifdef USE_SKINNING",
  758. "mvPosition = modelViewMatrix * skinned;",
  759. "#endif",
  760. "#if !defined( USE_SKINNING ) && defined( USE_MORPHTARGETS )",
  761. "mvPosition = modelViewMatrix * vec4( morphed, 1.0 );",
  762. "#endif",
  763. "#if !defined( USE_SKINNING ) && ! defined( USE_MORPHTARGETS )",
  764. "mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  765. "#endif",
  766. "gl_Position = projectionMatrix * mvPosition;",
  767. ].join("\n"),
  768. morphnormal_vertex: [
  769. "#ifdef USE_MORPHNORMALS",
  770. "vec3 morphedNormal = vec3( 0.0 );",
  771. "morphedNormal += ( morphNormal0 - normal ) * morphTargetInfluences[ 0 ];",
  772. "morphedNormal += ( morphNormal1 - normal ) * morphTargetInfluences[ 1 ];",
  773. "morphedNormal += ( morphNormal2 - normal ) * morphTargetInfluences[ 2 ];",
  774. "morphedNormal += ( morphNormal3 - normal ) * morphTargetInfluences[ 3 ];",
  775. "morphedNormal += normal;",
  776. "#endif"
  777. ].join("\n"),
  778. skinnormal_vertex: [
  779. "#ifdef USE_SKINNING",
  780. "mat4 skinMatrix = skinWeight.x * boneMatX;",
  781. "skinMatrix += skinWeight.y * boneMatY;",
  782. "#ifdef USE_MORPHNORMALS",
  783. "vec4 skinnedNormal = skinMatrix * vec4( morphedNormal, 0.0 );",
  784. "#else",
  785. "vec4 skinnedNormal = skinMatrix * vec4( normal, 0.0 );",
  786. "#endif",
  787. "#endif"
  788. ].join("\n"),
  789. defaultnormal_vertex: [
  790. "vec3 objectNormal;",
  791. "#ifdef USE_SKINNING",
  792. "objectNormal = skinnedNormal.xyz;",
  793. "#endif",
  794. "#if !defined( USE_SKINNING ) && defined( USE_MORPHNORMALS )",
  795. "objectNormal = morphedNormal;",
  796. "#endif",
  797. "#if !defined( USE_SKINNING ) && ! defined( USE_MORPHNORMALS )",
  798. "objectNormal = normal;",
  799. "#endif",
  800. "#ifdef FLIP_SIDED",
  801. "objectNormal = -objectNormal;",
  802. "#endif",
  803. "vec3 transformedNormal = normalMatrix * objectNormal;",
  804. ].join("\n"),
  805. // SHADOW MAP
  806. // based on SpiderGL shadow map and Fabien Sanglard's GLSL shadow mapping examples
  807. // http://spidergl.org/example.php?id=6
  808. // http://fabiensanglard.net/shadowmapping
  809. shadowmap_pars_fragment: [
  810. "#ifdef USE_SHADOWMAP",
  811. "uniform sampler2D shadowMap[ MAX_SHADOWS ];",
  812. "uniform vec2 shadowMapSize[ MAX_SHADOWS ];",
  813. "uniform float shadowDarkness[ MAX_SHADOWS ];",
  814. "uniform float shadowBias[ MAX_SHADOWS ];",
  815. "varying vec4 vShadowCoord[ MAX_SHADOWS ];",
  816. "float unpackDepth( const in vec4 rgba_depth ) {",
  817. "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 );",
  818. "float depth = dot( rgba_depth, bit_shift );",
  819. "return depth;",
  820. "}",
  821. "#endif"
  822. ].join("\n"),
  823. shadowmap_fragment: [
  824. "#ifdef USE_SHADOWMAP",
  825. "#ifdef SHADOWMAP_DEBUG",
  826. "vec3 frustumColors[3];",
  827. "frustumColors[0] = vec3( 1.0, 0.5, 0.0 );",
  828. "frustumColors[1] = vec3( 0.0, 1.0, 0.8 );",
  829. "frustumColors[2] = vec3( 0.0, 0.5, 1.0 );",
  830. "#endif",
  831. "#ifdef SHADOWMAP_CASCADE",
  832. "int inFrustumCount = 0;",
  833. "#endif",
  834. "float fDepth;",
  835. "vec3 shadowColor = vec3( 1.0 );",
  836. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  837. "vec3 shadowCoord = vShadowCoord[ i ].xyz / vShadowCoord[ i ].w;",
  838. // "if ( something && something )" breaks ATI OpenGL shader compiler
  839. // "if ( all( something, something ) )" using this instead
  840. "bvec4 inFrustumVec = bvec4 ( shadowCoord.x >= 0.0, shadowCoord.x <= 1.0, shadowCoord.y >= 0.0, shadowCoord.y <= 1.0 );",
  841. "bool inFrustum = all( inFrustumVec );",
  842. // don't shadow pixels outside of light frustum
  843. // use just first frustum (for cascades)
  844. // don't shadow pixels behind far plane of light frustum
  845. "#ifdef SHADOWMAP_CASCADE",
  846. "inFrustumCount += int( inFrustum );",
  847. "bvec3 frustumTestVec = bvec3( inFrustum, inFrustumCount == 1, shadowCoord.z <= 1.0 );",
  848. "#else",
  849. "bvec2 frustumTestVec = bvec2( inFrustum, shadowCoord.z <= 1.0 );",
  850. "#endif",
  851. "bool frustumTest = all( frustumTestVec );",
  852. "if ( frustumTest ) {",
  853. "shadowCoord.z += shadowBias[ i ];",
  854. "#ifdef SHADOWMAP_SOFT",
  855. // Percentage-close filtering
  856. // (9 pixel kernel)
  857. // http://fabiensanglard.net/shadowmappingPCF/
  858. "float shadow = 0.0;",
  859. /*
  860. // nested loops breaks shader compiler / validator on some ATI cards when using OpenGL
  861. // must enroll loop manually
  862. "for ( float y = -1.25; y <= 1.25; y += 1.25 )",
  863. "for ( float x = -1.25; x <= 1.25; x += 1.25 ) {",
  864. "vec4 rgbaDepth = texture2D( shadowMap[ i ], vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy );",
  865. // doesn't seem to produce any noticeable visual difference compared to simple "texture2D" lookup
  866. //"vec4 rgbaDepth = texture2DProj( shadowMap[ i ], vec4( vShadowCoord[ i ].w * ( vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy ), 0.05, vShadowCoord[ i ].w ) );",
  867. "float fDepth = unpackDepth( rgbaDepth );",
  868. "if ( fDepth < shadowCoord.z )",
  869. "shadow += 1.0;",
  870. "}",
  871. "shadow /= 9.0;",
  872. */
  873. "const float shadowDelta = 1.0 / 9.0;",
  874. "float xPixelOffset = 1.0 / shadowMapSize[ i ].x;",
  875. "float yPixelOffset = 1.0 / shadowMapSize[ i ].y;",
  876. "float dx0 = -1.25 * xPixelOffset;",
  877. "float dy0 = -1.25 * yPixelOffset;",
  878. "float dx1 = 1.25 * xPixelOffset;",
  879. "float dy1 = 1.25 * yPixelOffset;",
  880. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy0 ) ) );",
  881. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  882. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy0 ) ) );",
  883. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  884. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy0 ) ) );",
  885. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  886. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, 0.0 ) ) );",
  887. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  888. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy ) );",
  889. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  890. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, 0.0 ) ) );",
  891. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  892. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy1 ) ) );",
  893. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  894. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy1 ) ) );",
  895. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  896. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy1 ) ) );",
  897. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  898. "shadowColor = shadowColor * vec3( ( 1.0 - shadowDarkness[ i ] * shadow ) );",
  899. "#else",
  900. "vec4 rgbaDepth = texture2D( shadowMap[ i ], shadowCoord.xy );",
  901. "float fDepth = unpackDepth( rgbaDepth );",
  902. "if ( fDepth < shadowCoord.z )",
  903. // spot with multiple shadows is darker
  904. "shadowColor = shadowColor * vec3( 1.0 - shadowDarkness[ i ] );",
  905. // spot with multiple shadows has the same color as single shadow spot
  906. //"shadowColor = min( shadowColor, vec3( shadowDarkness[ i ] ) );",
  907. "#endif",
  908. "}",
  909. "#ifdef SHADOWMAP_DEBUG",
  910. "#ifdef SHADOWMAP_CASCADE",
  911. "if ( inFrustum && inFrustumCount == 1 ) gl_FragColor.xyz *= frustumColors[ i ];",
  912. "#else",
  913. "if ( inFrustum ) gl_FragColor.xyz *= frustumColors[ i ];",
  914. "#endif",
  915. "#endif",
  916. "}",
  917. "#ifdef GAMMA_OUTPUT",
  918. "shadowColor *= shadowColor;",
  919. "#endif",
  920. "gl_FragColor.xyz = gl_FragColor.xyz * shadowColor;",
  921. "#endif"
  922. ].join("\n"),
  923. shadowmap_pars_vertex: [
  924. "#ifdef USE_SHADOWMAP",
  925. "varying vec4 vShadowCoord[ MAX_SHADOWS ];",
  926. "uniform mat4 shadowMatrix[ MAX_SHADOWS ];",
  927. "#endif"
  928. ].join("\n"),
  929. shadowmap_vertex: [
  930. "#ifdef USE_SHADOWMAP",
  931. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  932. "vShadowCoord[ i ] = shadowMatrix[ i ] * mPosition;",
  933. "}",
  934. "#endif"
  935. ].join("\n"),
  936. // ALPHATEST
  937. alphatest_fragment: [
  938. "#ifdef ALPHATEST",
  939. "if ( gl_FragColor.a < ALPHATEST ) discard;",
  940. "#endif"
  941. ].join("\n"),
  942. // LINEAR SPACE
  943. linear_to_gamma_fragment: [
  944. "#ifdef GAMMA_OUTPUT",
  945. "gl_FragColor.xyz = sqrt( gl_FragColor.xyz );",
  946. "#endif"
  947. ].join("\n"),
  948. };
  949. THREE.UniformsUtils = {
  950. merge: function ( uniforms ) {
  951. var u, p, tmp, merged = {};
  952. for ( u = 0; u < uniforms.length; u ++ ) {
  953. tmp = this.clone( uniforms[ u ] );
  954. for ( p in tmp ) {
  955. merged[ p ] = tmp[ p ];
  956. }
  957. }
  958. return merged;
  959. },
  960. clone: function ( uniforms_src ) {
  961. var u, p, parameter, parameter_src, uniforms_dst = {};
  962. for ( u in uniforms_src ) {
  963. uniforms_dst[ u ] = {};
  964. for ( p in uniforms_src[ u ] ) {
  965. parameter_src = uniforms_src[ u ][ p ];
  966. if ( parameter_src instanceof THREE.Color ||
  967. parameter_src instanceof THREE.Vector2 ||
  968. parameter_src instanceof THREE.Vector3 ||
  969. parameter_src instanceof THREE.Vector4 ||
  970. parameter_src instanceof THREE.Matrix4 ||
  971. parameter_src instanceof THREE.Texture ) {
  972. uniforms_dst[ u ][ p ] = parameter_src.clone();
  973. } else if ( parameter_src instanceof Array ) {
  974. uniforms_dst[ u ][ p ] = parameter_src.slice();
  975. } else {
  976. uniforms_dst[ u ][ p ] = parameter_src;
  977. }
  978. }
  979. }
  980. return uniforms_dst;
  981. }
  982. };
  983. THREE.UniformsLib = {
  984. common: {
  985. "diffuse" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  986. "opacity" : { type: "f", value: 1.0 },
  987. "map" : { type: "t", value: null },
  988. "offsetRepeat" : { type: "v4", value: new THREE.Vector4( 0, 0, 1, 1 ) },
  989. "lightMap" : { type: "t", value: null },
  990. "specularMap" : { type: "t", value: null },
  991. "envMap" : { type: "t", value: null },
  992. "flipEnvMap" : { type: "f", value: -1 },
  993. "useRefract" : { type: "i", value: 0 },
  994. "reflectivity" : { type: "f", value: 1.0 },
  995. "refractionRatio" : { type: "f", value: 0.98 },
  996. "combine" : { type: "i", value: 0 },
  997. "morphTargetInfluences" : { type: "f", value: 0 }
  998. },
  999. bump: {
  1000. "bumpMap" : { type: "t", value: null },
  1001. "bumpScale" : { type: "f", value: 1 }
  1002. },
  1003. normalmap: {
  1004. "normalMap" : { type: "t", value: null },
  1005. "normalScale" : { type: "v2", value: new THREE.Vector2( 1, 1 ) }
  1006. },
  1007. fog : {
  1008. "fogDensity" : { type: "f", value: 0.00025 },
  1009. "fogNear" : { type: "f", value: 1 },
  1010. "fogFar" : { type: "f", value: 2000 },
  1011. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  1012. },
  1013. lights: {
  1014. "ambientLightColor" : { type: "fv", value: [] },
  1015. "directionalLightDirection" : { type: "fv", value: [] },
  1016. "directionalLightColor" : { type: "fv", value: [] },
  1017. "hemisphereLightPosition" : { type: "fv", value: [] },
  1018. "hemisphereLightSkyColor" : { type: "fv", value: [] },
  1019. "hemisphereLightGroundColor" : { type: "fv", value: [] },
  1020. "pointLightColor" : { type: "fv", value: [] },
  1021. "pointLightPosition" : { type: "fv", value: [] },
  1022. "pointLightDistance" : { type: "fv1", value: [] },
  1023. "spotLightColor" : { type: "fv", value: [] },
  1024. "spotLightPosition" : { type: "fv", value: [] },
  1025. "spotLightDirection" : { type: "fv", value: [] },
  1026. "spotLightDistance" : { type: "fv1", value: [] },
  1027. "spotLightAngle" : { type: "fv1", value: [] },
  1028. "spotLightExponent" : { type: "fv1", value: [] }
  1029. },
  1030. particle: {
  1031. "psColor" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  1032. "opacity" : { type: "f", value: 1.0 },
  1033. "size" : { type: "f", value: 1.0 },
  1034. "scale" : { type: "f", value: 1.0 },
  1035. "map" : { type: "t", value: null },
  1036. "fogDensity" : { type: "f", value: 0.00025 },
  1037. "fogNear" : { type: "f", value: 1 },
  1038. "fogFar" : { type: "f", value: 2000 },
  1039. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  1040. },
  1041. shadowmap: {
  1042. "shadowMap": { type: "tv", value: [] },
  1043. "shadowMapSize": { type: "v2v", value: [] },
  1044. "shadowBias" : { type: "fv1", value: [] },
  1045. "shadowDarkness": { type: "fv1", value: [] },
  1046. "shadowMatrix" : { type: "m4v", value: [] },
  1047. }
  1048. };
  1049. THREE.ShaderLib = {
  1050. 'depth': {
  1051. uniforms: {
  1052. "mNear": { type: "f", value: 1.0 },
  1053. "mFar" : { type: "f", value: 2000.0 },
  1054. "opacity" : { type: "f", value: 1.0 }
  1055. },
  1056. vertexShader: [
  1057. "void main() {",
  1058. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  1059. "}"
  1060. ].join("\n"),
  1061. fragmentShader: [
  1062. "uniform float mNear;",
  1063. "uniform float mFar;",
  1064. "uniform float opacity;",
  1065. "void main() {",
  1066. "float depth = gl_FragCoord.z / gl_FragCoord.w;",
  1067. "float color = 1.0 - smoothstep( mNear, mFar, depth );",
  1068. "gl_FragColor = vec4( vec3( color ), opacity );",
  1069. "}"
  1070. ].join("\n")
  1071. },
  1072. 'normal': {
  1073. uniforms: {
  1074. "opacity" : { type: "f", value: 1.0 }
  1075. },
  1076. vertexShader: [
  1077. "varying vec3 vNormal;",
  1078. "void main() {",
  1079. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  1080. "vNormal = normalMatrix * normal;",
  1081. "gl_Position = projectionMatrix * mvPosition;",
  1082. "}"
  1083. ].join("\n"),
  1084. fragmentShader: [
  1085. "uniform float opacity;",
  1086. "varying vec3 vNormal;",
  1087. "void main() {",
  1088. "gl_FragColor = vec4( 0.5 * normalize( vNormal ) + 0.5, opacity );",
  1089. "}"
  1090. ].join("\n")
  1091. },
  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. "#ifdef USE_ENVMAP",
  1111. THREE.ShaderChunk[ "morphnormal_vertex" ],
  1112. THREE.ShaderChunk[ "skinbase_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 = 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. // Depth encoding into RGBA texture
  1348. // based on SpiderGL shadow map example
  1349. // http://spidergl.org/example.php?id=6
  1350. // originally from
  1351. // http://www.gamedev.net/topic/442138-packing-a-float-into-a-a8r8g8b8-texture-shader/page__whichpage__1%25EF%25BF%25BD
  1352. // see also here:
  1353. // http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
  1354. 'depthRGBA': {
  1355. uniforms: {},
  1356. vertexShader: [
  1357. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  1358. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  1359. "void main() {",
  1360. THREE.ShaderChunk[ "skinbase_vertex" ],
  1361. THREE.ShaderChunk[ "morphtarget_vertex" ],
  1362. THREE.ShaderChunk[ "skinning_vertex" ],
  1363. THREE.ShaderChunk[ "default_vertex" ],
  1364. "}"
  1365. ].join("\n"),
  1366. fragmentShader: [
  1367. "vec4 pack_depth( const in float depth ) {",
  1368. "const vec4 bit_shift = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 );",
  1369. "const vec4 bit_mask = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 );",
  1370. "vec4 res = fract( depth * bit_shift );",
  1371. "res -= res.xxyz * bit_mask;",
  1372. "return res;",
  1373. "}",
  1374. "void main() {",
  1375. "gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z );",
  1376. //"gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z / gl_FragCoord.w );",
  1377. //"float z = ( ( gl_FragCoord.z / gl_FragCoord.w ) - 3.0 ) / ( 4000.0 - 3.0 );",
  1378. //"gl_FragData[ 0 ] = pack_depth( z );",
  1379. //"gl_FragData[ 0 ] = vec4( z, z, z, 1.0 );",
  1380. "}"
  1381. ].join("\n")
  1382. }
  1383. };