WebGLShaders.js 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389
  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. "hemiDiffuse += diffuse * mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
  613. // specular (sky light)
  614. "float hemiSpecularWeight = 0.0;",
  615. "vec3 hemiHalfVectorSky = normalize( lVector + viewPosition );",
  616. "float hemiDotNormalHalfSky = 0.5 * dot( normal, hemiHalfVectorSky ) + 0.5;",
  617. "hemiSpecularWeight += 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. "hemiSpecularWeight += specularStrength * max( pow( hemiDotNormalHalfGround, shininess ), 0.0 );",
  623. "#ifdef PHYSICALLY_BASED_SHADING",
  624. // 2.0 => 2.0001 is hack to work around ANGLE bug
  625. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  626. "vec3 schlickSky = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVector, hemiHalfVectorSky ), 5.0 );",
  627. "vec3 schlickGround = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVectorGround, hemiHalfVectorGround ), 5.0 );",
  628. "hemiSpecular += ( schlickSky + schlickGround ) * mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight ) * hemiSpecularWeight * hemiDiffuseWeight * specularNormalization;",
  629. "#else",
  630. "hemiSpecular += specular * mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight ) * hemiSpecularWeight * hemiDiffuseWeight;",
  631. "#endif",
  632. "}",
  633. "#endif",
  634. "vec3 totalDiffuse = vec3( 0.0 );",
  635. "vec3 totalSpecular = vec3( 0.0 );",
  636. "#if MAX_DIR_LIGHTS > 0",
  637. "totalDiffuse += dirDiffuse;",
  638. "totalSpecular += dirSpecular;",
  639. "#endif",
  640. "#if MAX_HEMI_LIGHTS > 0",
  641. "totalDiffuse += hemiDiffuse;",
  642. "totalSpecular += hemiSpecular;",
  643. "#endif",
  644. "#if MAX_POINT_LIGHTS > 0",
  645. "totalDiffuse += pointDiffuse;",
  646. "totalSpecular += pointSpecular;",
  647. "#endif",
  648. "#if MAX_SPOT_LIGHTS > 0",
  649. "totalDiffuse += spotDiffuse;",
  650. "totalSpecular += spotSpecular;",
  651. "#endif",
  652. "#ifdef METAL",
  653. "gl_FragColor.xyz = gl_FragColor.xyz * ( emissive + totalDiffuse + ambientLightColor * ambient + totalSpecular );",
  654. "#else",
  655. "gl_FragColor.xyz = gl_FragColor.xyz * ( emissive + totalDiffuse + ambientLightColor * ambient ) + totalSpecular;",
  656. "#endif"
  657. ].join("\n"),
  658. // VERTEX COLORS
  659. color_pars_fragment: [
  660. "#ifdef USE_COLOR",
  661. "varying vec3 vColor;",
  662. "#endif"
  663. ].join("\n"),
  664. color_fragment: [
  665. "#ifdef USE_COLOR",
  666. "gl_FragColor = gl_FragColor * vec4( vColor, opacity );",
  667. "#endif"
  668. ].join("\n"),
  669. color_pars_vertex: [
  670. "#ifdef USE_COLOR",
  671. "varying vec3 vColor;",
  672. "#endif"
  673. ].join("\n"),
  674. color_vertex: [
  675. "#ifdef USE_COLOR",
  676. "#ifdef GAMMA_INPUT",
  677. "vColor = color * color;",
  678. "#else",
  679. "vColor = color;",
  680. "#endif",
  681. "#endif"
  682. ].join("\n"),
  683. // SKINNING
  684. skinning_pars_vertex: [
  685. "#ifdef USE_SKINNING",
  686. "#ifdef BONE_TEXTURE",
  687. "uniform sampler2D boneTexture;",
  688. "mat4 getBoneMatrix( const in float i ) {",
  689. "float j = i * 4.0;",
  690. "float x = mod( j, N_BONE_PIXEL_X );",
  691. "float y = floor( j / N_BONE_PIXEL_X );",
  692. "const float dx = 1.0 / N_BONE_PIXEL_X;",
  693. "const float dy = 1.0 / N_BONE_PIXEL_Y;",
  694. "y = dy * ( y + 0.5 );",
  695. "vec4 v1 = texture2D( boneTexture, vec2( dx * ( x + 0.5 ), y ) );",
  696. "vec4 v2 = texture2D( boneTexture, vec2( dx * ( x + 1.5 ), y ) );",
  697. "vec4 v3 = texture2D( boneTexture, vec2( dx * ( x + 2.5 ), y ) );",
  698. "vec4 v4 = texture2D( boneTexture, vec2( dx * ( x + 3.5 ), y ) );",
  699. "mat4 bone = mat4( v1, v2, v3, v4 );",
  700. "return bone;",
  701. "}",
  702. "#else",
  703. "uniform mat4 boneGlobalMatrices[ MAX_BONES ];",
  704. "mat4 getBoneMatrix( const in float i ) {",
  705. "mat4 bone = boneGlobalMatrices[ int(i) ];",
  706. "return bone;",
  707. "}",
  708. "#endif",
  709. "#endif"
  710. ].join("\n"),
  711. skinbase_vertex: [
  712. "#ifdef USE_SKINNING",
  713. "mat4 boneMatX = getBoneMatrix( skinIndex.x );",
  714. "mat4 boneMatY = getBoneMatrix( skinIndex.y );",
  715. "#endif"
  716. ].join("\n"),
  717. skinning_vertex: [
  718. "#ifdef USE_SKINNING",
  719. "#ifdef USE_MORPHTARGETS",
  720. "vec4 skinVertex = vec4( morphed, 1.0 );",
  721. "#else",
  722. "vec4 skinVertex = vec4( position, 1.0 );",
  723. "#endif",
  724. "vec4 skinned = boneMatX * skinVertex * skinWeight.x;",
  725. "skinned += boneMatY * skinVertex * skinWeight.y;",
  726. "#endif"
  727. ].join("\n"),
  728. // MORPHING
  729. morphtarget_pars_vertex: [
  730. "#ifdef USE_MORPHTARGETS",
  731. "#ifndef USE_MORPHNORMALS",
  732. "uniform float morphTargetInfluences[ 8 ];",
  733. "#else",
  734. "uniform float morphTargetInfluences[ 4 ];",
  735. "#endif",
  736. "#endif"
  737. ].join("\n"),
  738. morphtarget_vertex: [
  739. "#ifdef USE_MORPHTARGETS",
  740. "vec3 morphed = vec3( 0.0 );",
  741. "morphed += ( morphTarget0 - position ) * morphTargetInfluences[ 0 ];",
  742. "morphed += ( morphTarget1 - position ) * morphTargetInfluences[ 1 ];",
  743. "morphed += ( morphTarget2 - position ) * morphTargetInfluences[ 2 ];",
  744. "morphed += ( morphTarget3 - position ) * morphTargetInfluences[ 3 ];",
  745. "#ifndef USE_MORPHNORMALS",
  746. "morphed += ( morphTarget4 - position ) * morphTargetInfluences[ 4 ];",
  747. "morphed += ( morphTarget5 - position ) * morphTargetInfluences[ 5 ];",
  748. "morphed += ( morphTarget6 - position ) * morphTargetInfluences[ 6 ];",
  749. "morphed += ( morphTarget7 - position ) * morphTargetInfluences[ 7 ];",
  750. "#endif",
  751. "morphed += position;",
  752. "#endif"
  753. ].join("\n"),
  754. default_vertex : [
  755. "vec4 mvPosition;",
  756. "#ifdef USE_SKINNING",
  757. "mvPosition = modelViewMatrix * skinned;",
  758. "#endif",
  759. "#if !defined( USE_SKINNING ) && defined( USE_MORPHTARGETS )",
  760. "mvPosition = modelViewMatrix * vec4( morphed, 1.0 );",
  761. "#endif",
  762. "#if !defined( USE_SKINNING ) && ! defined( USE_MORPHTARGETS )",
  763. "mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  764. "#endif",
  765. "gl_Position = projectionMatrix * mvPosition;",
  766. ].join("\n"),
  767. morphnormal_vertex: [
  768. "#ifdef USE_MORPHNORMALS",
  769. "vec3 morphedNormal = vec3( 0.0 );",
  770. "morphedNormal += ( morphNormal0 - normal ) * morphTargetInfluences[ 0 ];",
  771. "morphedNormal += ( morphNormal1 - normal ) * morphTargetInfluences[ 1 ];",
  772. "morphedNormal += ( morphNormal2 - normal ) * morphTargetInfluences[ 2 ];",
  773. "morphedNormal += ( morphNormal3 - normal ) * morphTargetInfluences[ 3 ];",
  774. "morphedNormal += normal;",
  775. "#endif"
  776. ].join("\n"),
  777. skinnormal_vertex: [
  778. "#ifdef USE_SKINNING",
  779. "mat4 skinMatrix = skinWeight.x * boneMatX;",
  780. "skinMatrix += skinWeight.y * boneMatY;",
  781. "#ifdef USE_MORPHNORMALS",
  782. "vec4 skinnedNormal = skinMatrix * vec4( morphedNormal, 0.0 );",
  783. "#else",
  784. "vec4 skinnedNormal = skinMatrix * vec4( normal, 0.0 );",
  785. "#endif",
  786. "#endif"
  787. ].join("\n"),
  788. defaultnormal_vertex: [
  789. "vec3 objectNormal;",
  790. "#ifdef USE_SKINNING",
  791. "objectNormal = skinnedNormal.xyz;",
  792. "#endif",
  793. "#if !defined( USE_SKINNING ) && defined( USE_MORPHNORMALS )",
  794. "objectNormal = morphedNormal;",
  795. "#endif",
  796. "#if !defined( USE_SKINNING ) && ! defined( USE_MORPHNORMALS )",
  797. "objectNormal = normal;",
  798. "#endif",
  799. "#ifdef FLIP_SIDED",
  800. "objectNormal = -objectNormal;",
  801. "#endif",
  802. "vec3 transformedNormal = normalMatrix * objectNormal;",
  803. ].join("\n"),
  804. // SHADOW MAP
  805. // based on SpiderGL shadow map and Fabien Sanglard's GLSL shadow mapping examples
  806. // http://spidergl.org/example.php?id=6
  807. // http://fabiensanglard.net/shadowmapping
  808. shadowmap_pars_fragment: [
  809. "#ifdef USE_SHADOWMAP",
  810. "uniform sampler2D shadowMap[ MAX_SHADOWS ];",
  811. "uniform vec2 shadowMapSize[ MAX_SHADOWS ];",
  812. "uniform float shadowDarkness[ MAX_SHADOWS ];",
  813. "uniform float shadowBias[ MAX_SHADOWS ];",
  814. "varying vec4 vShadowCoord[ MAX_SHADOWS ];",
  815. "float unpackDepth( const in vec4 rgba_depth ) {",
  816. "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 );",
  817. "float depth = dot( rgba_depth, bit_shift );",
  818. "return depth;",
  819. "}",
  820. "#endif"
  821. ].join("\n"),
  822. shadowmap_fragment: [
  823. "#ifdef USE_SHADOWMAP",
  824. "#ifdef SHADOWMAP_DEBUG",
  825. "vec3 frustumColors[3];",
  826. "frustumColors[0] = vec3( 1.0, 0.5, 0.0 );",
  827. "frustumColors[1] = vec3( 0.0, 1.0, 0.8 );",
  828. "frustumColors[2] = vec3( 0.0, 0.5, 1.0 );",
  829. "#endif",
  830. "#ifdef SHADOWMAP_CASCADE",
  831. "int inFrustumCount = 0;",
  832. "#endif",
  833. "float fDepth;",
  834. "vec3 shadowColor = vec3( 1.0 );",
  835. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  836. "vec3 shadowCoord = vShadowCoord[ i ].xyz / vShadowCoord[ i ].w;",
  837. // "if ( something && something )" breaks ATI OpenGL shader compiler
  838. // "if ( all( something, something ) )" using this instead
  839. "bvec4 inFrustumVec = bvec4 ( shadowCoord.x >= 0.0, shadowCoord.x <= 1.0, shadowCoord.y >= 0.0, shadowCoord.y <= 1.0 );",
  840. "bool inFrustum = all( inFrustumVec );",
  841. // don't shadow pixels outside of light frustum
  842. // use just first frustum (for cascades)
  843. // don't shadow pixels behind far plane of light frustum
  844. "#ifdef SHADOWMAP_CASCADE",
  845. "inFrustumCount += int( inFrustum );",
  846. "bvec3 frustumTestVec = bvec3( inFrustum, inFrustumCount == 1, shadowCoord.z <= 1.0 );",
  847. "#else",
  848. "bvec2 frustumTestVec = bvec2( inFrustum, shadowCoord.z <= 1.0 );",
  849. "#endif",
  850. "bool frustumTest = all( frustumTestVec );",
  851. "if ( frustumTest ) {",
  852. "shadowCoord.z += shadowBias[ i ];",
  853. "#ifdef SHADOWMAP_SOFT",
  854. // Percentage-close filtering
  855. // (9 pixel kernel)
  856. // http://fabiensanglard.net/shadowmappingPCF/
  857. "float shadow = 0.0;",
  858. /*
  859. // nested loops breaks shader compiler / validator on some ATI cards when using OpenGL
  860. // must enroll loop manually
  861. "for ( float y = -1.25; y <= 1.25; y += 1.25 )",
  862. "for ( float x = -1.25; x <= 1.25; x += 1.25 ) {",
  863. "vec4 rgbaDepth = texture2D( shadowMap[ i ], vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy );",
  864. // doesn't seem to produce any noticeable visual difference compared to simple "texture2D" lookup
  865. //"vec4 rgbaDepth = texture2DProj( shadowMap[ i ], vec4( vShadowCoord[ i ].w * ( vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy ), 0.05, vShadowCoord[ i ].w ) );",
  866. "float fDepth = unpackDepth( rgbaDepth );",
  867. "if ( fDepth < shadowCoord.z )",
  868. "shadow += 1.0;",
  869. "}",
  870. "shadow /= 9.0;",
  871. */
  872. "const float shadowDelta = 1.0 / 9.0;",
  873. "float xPixelOffset = 1.0 / shadowMapSize[ i ].x;",
  874. "float yPixelOffset = 1.0 / shadowMapSize[ i ].y;",
  875. "float dx0 = -1.25 * xPixelOffset;",
  876. "float dy0 = -1.25 * yPixelOffset;",
  877. "float dx1 = 1.25 * xPixelOffset;",
  878. "float dy1 = 1.25 * yPixelOffset;",
  879. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy0 ) ) );",
  880. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  881. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy0 ) ) );",
  882. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  883. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy0 ) ) );",
  884. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  885. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, 0.0 ) ) );",
  886. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  887. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy ) );",
  888. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  889. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, 0.0 ) ) );",
  890. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  891. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy1 ) ) );",
  892. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  893. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy1 ) ) );",
  894. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  895. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy1 ) ) );",
  896. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  897. "shadowColor = shadowColor * vec3( ( 1.0 - shadowDarkness[ i ] * shadow ) );",
  898. "#else",
  899. "vec4 rgbaDepth = texture2D( shadowMap[ i ], shadowCoord.xy );",
  900. "float fDepth = unpackDepth( rgbaDepth );",
  901. "if ( fDepth < shadowCoord.z )",
  902. // spot with multiple shadows is darker
  903. "shadowColor = shadowColor * vec3( 1.0 - shadowDarkness[ i ] );",
  904. // spot with multiple shadows has the same color as single shadow spot
  905. //"shadowColor = min( shadowColor, vec3( shadowDarkness[ i ] ) );",
  906. "#endif",
  907. "}",
  908. "#ifdef SHADOWMAP_DEBUG",
  909. "#ifdef SHADOWMAP_CASCADE",
  910. "if ( inFrustum && inFrustumCount == 1 ) gl_FragColor.xyz *= frustumColors[ i ];",
  911. "#else",
  912. "if ( inFrustum ) gl_FragColor.xyz *= frustumColors[ i ];",
  913. "#endif",
  914. "#endif",
  915. "}",
  916. "#ifdef GAMMA_OUTPUT",
  917. "shadowColor *= shadowColor;",
  918. "#endif",
  919. "gl_FragColor.xyz = gl_FragColor.xyz * shadowColor;",
  920. "#endif"
  921. ].join("\n"),
  922. shadowmap_pars_vertex: [
  923. "#ifdef USE_SHADOWMAP",
  924. "varying vec4 vShadowCoord[ MAX_SHADOWS ];",
  925. "uniform mat4 shadowMatrix[ MAX_SHADOWS ];",
  926. "#endif"
  927. ].join("\n"),
  928. shadowmap_vertex: [
  929. "#ifdef USE_SHADOWMAP",
  930. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  931. "vShadowCoord[ i ] = shadowMatrix[ i ] * mPosition;",
  932. "}",
  933. "#endif"
  934. ].join("\n"),
  935. // ALPHATEST
  936. alphatest_fragment: [
  937. "#ifdef ALPHATEST",
  938. "if ( gl_FragColor.a < ALPHATEST ) discard;",
  939. "#endif"
  940. ].join("\n"),
  941. // LINEAR SPACE
  942. linear_to_gamma_fragment: [
  943. "#ifdef GAMMA_OUTPUT",
  944. "gl_FragColor.xyz = sqrt( gl_FragColor.xyz );",
  945. "#endif"
  946. ].join("\n"),
  947. };
  948. THREE.UniformsUtils = {
  949. merge: function ( uniforms ) {
  950. var u, p, tmp, merged = {};
  951. for ( u = 0; u < uniforms.length; u ++ ) {
  952. tmp = this.clone( uniforms[ u ] );
  953. for ( p in tmp ) {
  954. merged[ p ] = tmp[ p ];
  955. }
  956. }
  957. return merged;
  958. },
  959. clone: function ( uniforms_src ) {
  960. var u, p, parameter, parameter_src, uniforms_dst = {};
  961. for ( u in uniforms_src ) {
  962. uniforms_dst[ u ] = {};
  963. for ( p in uniforms_src[ u ] ) {
  964. parameter_src = uniforms_src[ u ][ p ];
  965. if ( parameter_src instanceof THREE.Color ||
  966. parameter_src instanceof THREE.Vector2 ||
  967. parameter_src instanceof THREE.Vector3 ||
  968. parameter_src instanceof THREE.Vector4 ||
  969. parameter_src instanceof THREE.Matrix4 ||
  970. parameter_src instanceof THREE.Texture ) {
  971. uniforms_dst[ u ][ p ] = parameter_src.clone();
  972. } else if ( parameter_src instanceof Array ) {
  973. uniforms_dst[ u ][ p ] = parameter_src.slice();
  974. } else {
  975. uniforms_dst[ u ][ p ] = parameter_src;
  976. }
  977. }
  978. }
  979. return uniforms_dst;
  980. }
  981. };
  982. THREE.UniformsLib = {
  983. common: {
  984. "diffuse" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  985. "opacity" : { type: "f", value: 1.0 },
  986. "map" : { type: "t", value: null },
  987. "offsetRepeat" : { type: "v4", value: new THREE.Vector4( 0, 0, 1, 1 ) },
  988. "lightMap" : { type: "t", value: null },
  989. "specularMap" : { type: "t", value: null },
  990. "envMap" : { type: "t", value: null },
  991. "flipEnvMap" : { type: "f", value: -1 },
  992. "useRefract" : { type: "i", value: 0 },
  993. "reflectivity" : { type: "f", value: 1.0 },
  994. "refractionRatio" : { type: "f", value: 0.98 },
  995. "combine" : { type: "i", value: 0 },
  996. "morphTargetInfluences" : { type: "f", value: 0 }
  997. },
  998. bump: {
  999. "bumpMap" : { type: "t", value: null },
  1000. "bumpScale" : { type: "f", value: 1 }
  1001. },
  1002. normalmap: {
  1003. "normalMap" : { type: "t", value: null },
  1004. "normalScale" : { type: "v2", value: new THREE.Vector2( 1, 1 ) }
  1005. },
  1006. fog : {
  1007. "fogDensity" : { type: "f", value: 0.00025 },
  1008. "fogNear" : { type: "f", value: 1 },
  1009. "fogFar" : { type: "f", value: 2000 },
  1010. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  1011. },
  1012. lights: {
  1013. "ambientLightColor" : { type: "fv", value: [] },
  1014. "directionalLightDirection" : { type: "fv", value: [] },
  1015. "directionalLightColor" : { type: "fv", value: [] },
  1016. "hemisphereLightPosition" : { type: "fv", value: [] },
  1017. "hemisphereLightSkyColor" : { type: "fv", value: [] },
  1018. "hemisphereLightGroundColor" : { type: "fv", value: [] },
  1019. "pointLightColor" : { type: "fv", value: [] },
  1020. "pointLightPosition" : { type: "fv", value: [] },
  1021. "pointLightDistance" : { type: "fv1", value: [] },
  1022. "spotLightColor" : { type: "fv", value: [] },
  1023. "spotLightPosition" : { type: "fv", value: [] },
  1024. "spotLightDirection" : { type: "fv", value: [] },
  1025. "spotLightDistance" : { type: "fv1", value: [] },
  1026. "spotLightAngle" : { type: "fv1", value: [] },
  1027. "spotLightExponent" : { type: "fv1", value: [] }
  1028. },
  1029. particle: {
  1030. "psColor" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  1031. "opacity" : { type: "f", value: 1.0 },
  1032. "size" : { type: "f", value: 1.0 },
  1033. "scale" : { type: "f", value: 1.0 },
  1034. "map" : { type: "t", value: null },
  1035. "fogDensity" : { type: "f", value: 0.00025 },
  1036. "fogNear" : { type: "f", value: 1 },
  1037. "fogFar" : { type: "f", value: 2000 },
  1038. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  1039. },
  1040. shadowmap: {
  1041. "shadowMap": { type: "tv", value: [] },
  1042. "shadowMapSize": { type: "v2v", value: [] },
  1043. "shadowBias" : { type: "fv1", value: [] },
  1044. "shadowDarkness": { type: "fv1", value: [] },
  1045. "shadowMatrix" : { type: "m4v", value: [] },
  1046. }
  1047. };
  1048. THREE.ShaderLib = {
  1049. 'depth': {
  1050. uniforms: {
  1051. "mNear": { type: "f", value: 1.0 },
  1052. "mFar" : { type: "f", value: 2000.0 },
  1053. "opacity" : { type: "f", value: 1.0 }
  1054. },
  1055. vertexShader: [
  1056. "void main() {",
  1057. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  1058. "}"
  1059. ].join("\n"),
  1060. fragmentShader: [
  1061. "uniform float mNear;",
  1062. "uniform float mFar;",
  1063. "uniform float opacity;",
  1064. "void main() {",
  1065. "float depth = gl_FragCoord.z / gl_FragCoord.w;",
  1066. "float color = 1.0 - smoothstep( mNear, mFar, depth );",
  1067. "gl_FragColor = vec4( vec3( color ), opacity );",
  1068. "}"
  1069. ].join("\n")
  1070. },
  1071. 'normal': {
  1072. uniforms: {
  1073. "opacity" : { type: "f", value: 1.0 }
  1074. },
  1075. vertexShader: [
  1076. "varying vec3 vNormal;",
  1077. "void main() {",
  1078. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  1079. "vNormal = normalMatrix * normal;",
  1080. "gl_Position = projectionMatrix * mvPosition;",
  1081. "}"
  1082. ].join("\n"),
  1083. fragmentShader: [
  1084. "uniform float opacity;",
  1085. "varying vec3 vNormal;",
  1086. "void main() {",
  1087. "gl_FragColor = vec4( 0.5 * normalize( vNormal ) + 0.5, opacity );",
  1088. "}"
  1089. ].join("\n")
  1090. },
  1091. 'basic': {
  1092. uniforms: THREE.UniformsUtils.merge( [
  1093. THREE.UniformsLib[ "common" ],
  1094. THREE.UniformsLib[ "fog" ],
  1095. THREE.UniformsLib[ "shadowmap" ]
  1096. ] ),
  1097. vertexShader: [
  1098. THREE.ShaderChunk[ "map_pars_vertex" ],
  1099. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  1100. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  1101. THREE.ShaderChunk[ "color_pars_vertex" ],
  1102. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  1103. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  1104. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  1105. "void main() {",
  1106. THREE.ShaderChunk[ "map_vertex" ],
  1107. THREE.ShaderChunk[ "lightmap_vertex" ],
  1108. THREE.ShaderChunk[ "color_vertex" ],
  1109. "#ifdef USE_ENVMAP",
  1110. THREE.ShaderChunk[ "morphnormal_vertex" ],
  1111. THREE.ShaderChunk[ "skinbase_vertex" ],
  1112. THREE.ShaderChunk[ "skinnormal_vertex" ],
  1113. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  1114. "#endif",
  1115. THREE.ShaderChunk[ "morphtarget_vertex" ],
  1116. THREE.ShaderChunk[ "skinning_vertex" ],
  1117. THREE.ShaderChunk[ "default_vertex" ],
  1118. THREE.ShaderChunk[ "worldpos_vertex" ],
  1119. THREE.ShaderChunk[ "envmap_vertex" ],
  1120. THREE.ShaderChunk[ "shadowmap_vertex" ],
  1121. "}"
  1122. ].join("\n"),
  1123. fragmentShader: [
  1124. "uniform vec3 diffuse;",
  1125. "uniform float opacity;",
  1126. THREE.ShaderChunk[ "color_pars_fragment" ],
  1127. THREE.ShaderChunk[ "map_pars_fragment" ],
  1128. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  1129. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  1130. THREE.ShaderChunk[ "fog_pars_fragment" ],
  1131. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  1132. THREE.ShaderChunk[ "specularmap_pars_fragment" ],
  1133. "void main() {",
  1134. "gl_FragColor = vec4( diffuse, opacity );",
  1135. THREE.ShaderChunk[ "map_fragment" ],
  1136. THREE.ShaderChunk[ "alphatest_fragment" ],
  1137. THREE.ShaderChunk[ "specularmap_fragment" ],
  1138. THREE.ShaderChunk[ "lightmap_fragment" ],
  1139. THREE.ShaderChunk[ "color_fragment" ],
  1140. THREE.ShaderChunk[ "envmap_fragment" ],
  1141. THREE.ShaderChunk[ "shadowmap_fragment" ],
  1142. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  1143. THREE.ShaderChunk[ "fog_fragment" ],
  1144. "}"
  1145. ].join("\n")
  1146. },
  1147. 'lambert': {
  1148. uniforms: THREE.UniformsUtils.merge( [
  1149. THREE.UniformsLib[ "common" ],
  1150. THREE.UniformsLib[ "fog" ],
  1151. THREE.UniformsLib[ "lights" ],
  1152. THREE.UniformsLib[ "shadowmap" ],
  1153. {
  1154. "ambient" : { type: "c", value: new THREE.Color( 0xffffff ) },
  1155. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  1156. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  1157. }
  1158. ] ),
  1159. vertexShader: [
  1160. "#define LAMBERT",
  1161. "varying vec3 vLightFront;",
  1162. "#ifdef DOUBLE_SIDED",
  1163. "varying vec3 vLightBack;",
  1164. "#endif",
  1165. THREE.ShaderChunk[ "map_pars_vertex" ],
  1166. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  1167. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  1168. THREE.ShaderChunk[ "lights_lambert_pars_vertex" ],
  1169. THREE.ShaderChunk[ "color_pars_vertex" ],
  1170. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  1171. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  1172. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  1173. "void main() {",
  1174. THREE.ShaderChunk[ "map_vertex" ],
  1175. THREE.ShaderChunk[ "lightmap_vertex" ],
  1176. THREE.ShaderChunk[ "color_vertex" ],
  1177. THREE.ShaderChunk[ "morphnormal_vertex" ],
  1178. THREE.ShaderChunk[ "skinbase_vertex" ],
  1179. THREE.ShaderChunk[ "skinnormal_vertex" ],
  1180. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  1181. THREE.ShaderChunk[ "morphtarget_vertex" ],
  1182. THREE.ShaderChunk[ "skinning_vertex" ],
  1183. THREE.ShaderChunk[ "default_vertex" ],
  1184. THREE.ShaderChunk[ "worldpos_vertex" ],
  1185. THREE.ShaderChunk[ "envmap_vertex" ],
  1186. THREE.ShaderChunk[ "lights_lambert_vertex" ],
  1187. THREE.ShaderChunk[ "shadowmap_vertex" ],
  1188. "}"
  1189. ].join("\n"),
  1190. fragmentShader: [
  1191. "uniform float opacity;",
  1192. "varying vec3 vLightFront;",
  1193. "#ifdef DOUBLE_SIDED",
  1194. "varying vec3 vLightBack;",
  1195. "#endif",
  1196. THREE.ShaderChunk[ "color_pars_fragment" ],
  1197. THREE.ShaderChunk[ "map_pars_fragment" ],
  1198. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  1199. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  1200. THREE.ShaderChunk[ "fog_pars_fragment" ],
  1201. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  1202. THREE.ShaderChunk[ "specularmap_pars_fragment" ],
  1203. "void main() {",
  1204. "gl_FragColor = vec4( vec3 ( 1.0 ), opacity );",
  1205. THREE.ShaderChunk[ "map_fragment" ],
  1206. THREE.ShaderChunk[ "alphatest_fragment" ],
  1207. THREE.ShaderChunk[ "specularmap_fragment" ],
  1208. "#ifdef DOUBLE_SIDED",
  1209. //"float isFront = float( gl_FrontFacing );",
  1210. //"gl_FragColor.xyz *= isFront * vLightFront + ( 1.0 - isFront ) * vLightBack;",
  1211. "if ( gl_FrontFacing )",
  1212. "gl_FragColor.xyz *= vLightFront;",
  1213. "else",
  1214. "gl_FragColor.xyz *= vLightBack;",
  1215. "#else",
  1216. "gl_FragColor.xyz *= vLightFront;",
  1217. "#endif",
  1218. THREE.ShaderChunk[ "lightmap_fragment" ],
  1219. THREE.ShaderChunk[ "color_fragment" ],
  1220. THREE.ShaderChunk[ "envmap_fragment" ],
  1221. THREE.ShaderChunk[ "shadowmap_fragment" ],
  1222. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  1223. THREE.ShaderChunk[ "fog_fragment" ],
  1224. "}"
  1225. ].join("\n")
  1226. },
  1227. 'phong': {
  1228. uniforms: THREE.UniformsUtils.merge( [
  1229. THREE.UniformsLib[ "common" ],
  1230. THREE.UniformsLib[ "bump" ],
  1231. THREE.UniformsLib[ "normalmap" ],
  1232. THREE.UniformsLib[ "fog" ],
  1233. THREE.UniformsLib[ "lights" ],
  1234. THREE.UniformsLib[ "shadowmap" ],
  1235. {
  1236. "ambient" : { type: "c", value: new THREE.Color( 0xffffff ) },
  1237. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  1238. "specular" : { type: "c", value: new THREE.Color( 0x111111 ) },
  1239. "shininess": { type: "f", value: 30 },
  1240. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  1241. }
  1242. ] ),
  1243. vertexShader: [
  1244. "#define PHONG",
  1245. "varying vec3 vViewPosition;",
  1246. "varying vec3 vNormal;",
  1247. THREE.ShaderChunk[ "map_pars_vertex" ],
  1248. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  1249. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  1250. THREE.ShaderChunk[ "lights_phong_pars_vertex" ],
  1251. THREE.ShaderChunk[ "color_pars_vertex" ],
  1252. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  1253. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  1254. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  1255. "void main() {",
  1256. THREE.ShaderChunk[ "map_vertex" ],
  1257. THREE.ShaderChunk[ "lightmap_vertex" ],
  1258. THREE.ShaderChunk[ "color_vertex" ],
  1259. THREE.ShaderChunk[ "morphnormal_vertex" ],
  1260. THREE.ShaderChunk[ "skinbase_vertex" ],
  1261. THREE.ShaderChunk[ "skinnormal_vertex" ],
  1262. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  1263. "vNormal = transformedNormal;",
  1264. THREE.ShaderChunk[ "morphtarget_vertex" ],
  1265. THREE.ShaderChunk[ "skinning_vertex" ],
  1266. THREE.ShaderChunk[ "default_vertex" ],
  1267. "vViewPosition = -mvPosition.xyz;",
  1268. THREE.ShaderChunk[ "worldpos_vertex" ],
  1269. THREE.ShaderChunk[ "envmap_vertex" ],
  1270. THREE.ShaderChunk[ "lights_phong_vertex" ],
  1271. THREE.ShaderChunk[ "shadowmap_vertex" ],
  1272. "}"
  1273. ].join("\n"),
  1274. fragmentShader: [
  1275. "uniform vec3 diffuse;",
  1276. "uniform float opacity;",
  1277. "uniform vec3 ambient;",
  1278. "uniform vec3 emissive;",
  1279. "uniform vec3 specular;",
  1280. "uniform float shininess;",
  1281. THREE.ShaderChunk[ "color_pars_fragment" ],
  1282. THREE.ShaderChunk[ "map_pars_fragment" ],
  1283. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  1284. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  1285. THREE.ShaderChunk[ "fog_pars_fragment" ],
  1286. THREE.ShaderChunk[ "lights_phong_pars_fragment" ],
  1287. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  1288. THREE.ShaderChunk[ "bumpmap_pars_fragment" ],
  1289. THREE.ShaderChunk[ "normalmap_pars_fragment" ],
  1290. THREE.ShaderChunk[ "specularmap_pars_fragment" ],
  1291. "void main() {",
  1292. "gl_FragColor = vec4( vec3 ( 1.0 ), opacity );",
  1293. THREE.ShaderChunk[ "map_fragment" ],
  1294. THREE.ShaderChunk[ "alphatest_fragment" ],
  1295. THREE.ShaderChunk[ "specularmap_fragment" ],
  1296. THREE.ShaderChunk[ "lights_phong_fragment" ],
  1297. THREE.ShaderChunk[ "lightmap_fragment" ],
  1298. THREE.ShaderChunk[ "color_fragment" ],
  1299. THREE.ShaderChunk[ "envmap_fragment" ],
  1300. THREE.ShaderChunk[ "shadowmap_fragment" ],
  1301. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  1302. THREE.ShaderChunk[ "fog_fragment" ],
  1303. "}"
  1304. ].join("\n")
  1305. },
  1306. 'particle_basic': {
  1307. uniforms: THREE.UniformsUtils.merge( [
  1308. THREE.UniformsLib[ "particle" ],
  1309. THREE.UniformsLib[ "shadowmap" ]
  1310. ] ),
  1311. vertexShader: [
  1312. "uniform float size;",
  1313. "uniform float scale;",
  1314. THREE.ShaderChunk[ "color_pars_vertex" ],
  1315. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  1316. "void main() {",
  1317. THREE.ShaderChunk[ "color_vertex" ],
  1318. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  1319. "#ifdef USE_SIZEATTENUATION",
  1320. "gl_PointSize = size * ( scale / length( mvPosition.xyz ) );",
  1321. "#else",
  1322. "gl_PointSize = size;",
  1323. "#endif",
  1324. "gl_Position = projectionMatrix * mvPosition;",
  1325. THREE.ShaderChunk[ "worldpos_vertex" ],
  1326. THREE.ShaderChunk[ "shadowmap_vertex" ],
  1327. "}"
  1328. ].join("\n"),
  1329. fragmentShader: [
  1330. "uniform vec3 psColor;",
  1331. "uniform float opacity;",
  1332. THREE.ShaderChunk[ "color_pars_fragment" ],
  1333. THREE.ShaderChunk[ "map_particle_pars_fragment" ],
  1334. THREE.ShaderChunk[ "fog_pars_fragment" ],
  1335. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  1336. "void main() {",
  1337. "gl_FragColor = vec4( psColor, opacity );",
  1338. THREE.ShaderChunk[ "map_particle_fragment" ],
  1339. THREE.ShaderChunk[ "alphatest_fragment" ],
  1340. THREE.ShaderChunk[ "color_fragment" ],
  1341. THREE.ShaderChunk[ "shadowmap_fragment" ],
  1342. THREE.ShaderChunk[ "fog_fragment" ],
  1343. "}"
  1344. ].join("\n")
  1345. },
  1346. // Depth encoding into RGBA texture
  1347. // based on SpiderGL shadow map example
  1348. // http://spidergl.org/example.php?id=6
  1349. // originally from
  1350. // http://www.gamedev.net/topic/442138-packing-a-float-into-a-a8r8g8b8-texture-shader/page__whichpage__1%25EF%25BF%25BD
  1351. // see also here:
  1352. // http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
  1353. 'depthRGBA': {
  1354. uniforms: {},
  1355. vertexShader: [
  1356. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  1357. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  1358. "void main() {",
  1359. THREE.ShaderChunk[ "skinbase_vertex" ],
  1360. THREE.ShaderChunk[ "morphtarget_vertex" ],
  1361. THREE.ShaderChunk[ "skinning_vertex" ],
  1362. THREE.ShaderChunk[ "default_vertex" ],
  1363. "}"
  1364. ].join("\n"),
  1365. fragmentShader: [
  1366. "vec4 pack_depth( const in float depth ) {",
  1367. "const vec4 bit_shift = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 );",
  1368. "const vec4 bit_mask = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 );",
  1369. "vec4 res = fract( depth * bit_shift );",
  1370. "res -= res.xxyz * bit_mask;",
  1371. "return res;",
  1372. "}",
  1373. "void main() {",
  1374. "gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z );",
  1375. //"gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z / gl_FragCoord.w );",
  1376. //"float z = ( ( gl_FragCoord.z / gl_FragCoord.w ) - 3.0 ) / ( 4000.0 - 3.0 );",
  1377. //"gl_FragData[ 0 ] = pack_depth( z );",
  1378. //"gl_FragData[ 0 ] = vec4( z, z, z, 1.0 );",
  1379. "}"
  1380. ].join("\n")
  1381. }
  1382. };
粤ICP备19079148号