WebGLShaders.js 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714
  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. "varying vec3 vReflect;",
  36. "uniform float reflectivity;",
  37. "uniform samplerCube envMap;",
  38. "uniform float flipEnvMap;",
  39. "uniform int combine;",
  40. "#endif"
  41. ].join("\n"),
  42. envmap_fragment: [
  43. "#ifdef USE_ENVMAP",
  44. "#ifdef DOUBLE_SIDED",
  45. "float flipNormal = ( -1.0 + 2.0 * float( gl_FrontFacing ) );",
  46. "vec4 cubeColor = textureCube( envMap, flipNormal * vec3( flipEnvMap * vReflect.x, vReflect.yz ) );",
  47. "#else",
  48. "vec4 cubeColor = textureCube( envMap, vec3( flipEnvMap * vReflect.x, vReflect.yz ) );",
  49. "#endif",
  50. "#ifdef GAMMA_INPUT",
  51. "cubeColor.xyz *= cubeColor.xyz;",
  52. "#endif",
  53. "if ( combine == 1 ) {",
  54. "gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, reflectivity );",
  55. "} else {",
  56. "gl_FragColor.xyz = gl_FragColor.xyz * cubeColor.xyz;",
  57. "}",
  58. "#endif"
  59. ].join("\n"),
  60. envmap_pars_vertex: [
  61. "#ifdef USE_ENVMAP",
  62. "varying vec3 vReflect;",
  63. "uniform float refractionRatio;",
  64. "uniform bool useRefract;",
  65. "#endif"
  66. ].join("\n"),
  67. envmap_vertex : [
  68. "#ifdef USE_ENVMAP",
  69. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  70. "vec3 nWorld = mat3( objectMatrix[ 0 ].xyz, objectMatrix[ 1 ].xyz, objectMatrix[ 2 ].xyz ) * normal;",
  71. "if ( useRefract ) {",
  72. "vReflect = refract( normalize( mPosition.xyz - cameraPosition ), normalize( nWorld.xyz ), refractionRatio );",
  73. "} else {",
  74. "vReflect = reflect( normalize( mPosition.xyz - cameraPosition ), normalize( nWorld.xyz ) );",
  75. "}",
  76. "#endif"
  77. ].join("\n"),
  78. // COLOR MAP (particles)
  79. map_particle_pars_fragment: [
  80. "#ifdef USE_MAP",
  81. "uniform sampler2D map;",
  82. "#endif"
  83. ].join("\n"),
  84. map_particle_fragment: [
  85. "#ifdef USE_MAP",
  86. "gl_FragColor = gl_FragColor * texture2D( map, gl_PointCoord );",
  87. "#endif"
  88. ].join("\n"),
  89. // COLOR MAP (triangles)
  90. map_pars_vertex: [
  91. "#ifdef USE_MAP",
  92. "varying vec2 vUv;",
  93. "uniform vec4 offsetRepeat;",
  94. "#endif"
  95. ].join("\n"),
  96. map_pars_fragment: [
  97. "#ifdef USE_MAP",
  98. "varying vec2 vUv;",
  99. "uniform sampler2D map;",
  100. "#endif"
  101. ].join("\n"),
  102. map_vertex: [
  103. "#ifdef USE_MAP",
  104. "vUv = uv * offsetRepeat.zw + offsetRepeat.xy;",
  105. "#endif"
  106. ].join("\n"),
  107. map_fragment: [
  108. "#ifdef USE_MAP",
  109. "#ifdef GAMMA_INPUT",
  110. "vec4 texelColor = texture2D( map, vUv );",
  111. "texelColor.xyz *= texelColor.xyz;",
  112. "gl_FragColor = gl_FragColor * texelColor;",
  113. "#else",
  114. "gl_FragColor = gl_FragColor * texture2D( map, vUv );",
  115. "#endif",
  116. "#endif"
  117. ].join("\n"),
  118. // LIGHT MAP
  119. lightmap_pars_fragment: [
  120. "#ifdef USE_LIGHTMAP",
  121. "varying vec2 vUv2;",
  122. "uniform sampler2D lightMap;",
  123. "#endif"
  124. ].join("\n"),
  125. lightmap_pars_vertex: [
  126. "#ifdef USE_LIGHTMAP",
  127. "varying vec2 vUv2;",
  128. "#endif"
  129. ].join("\n"),
  130. lightmap_fragment: [
  131. "#ifdef USE_LIGHTMAP",
  132. "gl_FragColor = gl_FragColor * texture2D( lightMap, vUv2 );",
  133. "#endif"
  134. ].join("\n"),
  135. lightmap_vertex: [
  136. "#ifdef USE_LIGHTMAP",
  137. "vUv2 = uv2;",
  138. "#endif"
  139. ].join("\n"),
  140. // LIGHTS LAMBERT
  141. lights_lambert_pars_vertex: [
  142. "uniform vec3 ambient;",
  143. "uniform vec3 diffuse;",
  144. "uniform vec3 ambientLightColor;",
  145. "#if MAX_DIR_LIGHTS > 0",
  146. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  147. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  148. "#endif",
  149. "#if MAX_POINT_LIGHTS > 0",
  150. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  151. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  152. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  153. "#endif",
  154. "#ifdef WRAP_AROUND",
  155. "uniform vec3 wrapRGB;",
  156. "#endif",
  157. ].join("\n"),
  158. lights_lambert_vertex: [
  159. "vLightFront = vec3( 0.0 );",
  160. "#ifdef DOUBLE_SIDED",
  161. "vLightBack = vec3( 0.0 );",
  162. "#endif",
  163. "transformedNormal = normalize( transformedNormal );",
  164. "#if MAX_DIR_LIGHTS > 0",
  165. "for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {",
  166. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  167. "vec3 dirVector = normalize( lDirection.xyz );",
  168. "float dotProduct = dot( transformedNormal, dirVector );",
  169. "vec3 directionalLightWeighting = vec3( max( dotProduct, 0.0 ) );",
  170. "#ifdef DOUBLE_SIDED",
  171. "vec3 directionalLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );",
  172. "#ifdef WRAP_AROUND",
  173. "vec3 directionalLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );",
  174. "#endif",
  175. "#endif",
  176. "#ifdef WRAP_AROUND",
  177. "vec3 directionalLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );",
  178. "directionalLightWeighting = mix( directionalLightWeighting, directionalLightWeightingHalf, wrapRGB );",
  179. "#ifdef DOUBLE_SIDED",
  180. "directionalLightWeightingBack = mix( directionalLightWeightingBack, directionalLightWeightingHalfBack, wrapRGB );",
  181. "#endif",
  182. "#endif",
  183. "vLightFront += directionalLightColor[ i ] * directionalLightWeighting;",
  184. "#ifdef DOUBLE_SIDED",
  185. "vLightBack += directionalLightColor[ i ] * directionalLightWeightingBack;",
  186. "#endif",
  187. "}",
  188. "#endif",
  189. "#if MAX_POINT_LIGHTS > 0",
  190. "for( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  191. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  192. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  193. "float lDistance = 1.0;",
  194. "if ( pointLightDistance[ i ] > 0.0 )",
  195. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  196. "lVector = normalize( lVector );",
  197. "float dotProduct = dot( transformedNormal, lVector );",
  198. "vec3 pointLightWeighting = vec3( max( dotProduct, 0.0 ) );",
  199. "#ifdef DOUBLE_SIDED",
  200. "vec3 pointLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );",
  201. "#ifdef WRAP_AROUND",
  202. "vec3 pointLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );",
  203. "#endif",
  204. "#endif",
  205. "#ifdef WRAP_AROUND",
  206. "vec3 pointLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );",
  207. "pointLightWeighting = mix( pointLightWeighting, pointLightWeightingHalf, wrapRGB );",
  208. "#ifdef DOUBLE_SIDED",
  209. "pointLightWeightingBack = mix( pointLightWeightingBack, pointLightWeightingHalfBack, wrapRGB );",
  210. "#endif",
  211. "#endif",
  212. "vLightFront += pointLightColor[ i ] * pointLightWeighting * lDistance;",
  213. "#ifdef DOUBLE_SIDED",
  214. "vLightBack += pointLightColor[ i ] * pointLightWeightingBack * lDistance;",
  215. "#endif",
  216. "}",
  217. "#endif",
  218. "vLightFront = vLightFront * diffuse + ambient * ambientLightColor;",
  219. "#ifdef DOUBLE_SIDED",
  220. "vLightBack = vLightBack * diffuse + ambient * ambientLightColor;",
  221. "#endif",
  222. ].join("\n"),
  223. // LIGHTS PHONG
  224. lights_phong_pars_vertex: [
  225. "#if MAX_POINT_LIGHTS > 0",
  226. "#ifndef PHONG_PER_PIXEL",
  227. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  228. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  229. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  230. "#endif",
  231. "#endif"
  232. ].join("\n"),
  233. lights_phong_vertex: [
  234. "#if MAX_POINT_LIGHTS > 0",
  235. "#ifndef PHONG_PER_PIXEL",
  236. "for( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  237. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  238. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  239. "float lDistance = 1.0;",
  240. "if ( pointLightDistance[ i ] > 0.0 )",
  241. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  242. "vPointLight[ i ] = vec4( lVector, lDistance );",
  243. "}",
  244. "#endif",
  245. "#endif"
  246. ].join("\n"),
  247. lights_phong_pars_fragment: [
  248. "uniform vec3 ambientLightColor;",
  249. "#if MAX_DIR_LIGHTS > 0",
  250. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  251. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  252. "#endif",
  253. "#if MAX_POINT_LIGHTS > 0",
  254. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  255. "#ifdef PHONG_PER_PIXEL",
  256. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  257. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  258. "#else",
  259. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  260. "#endif",
  261. "#endif",
  262. "#ifdef WRAP_AROUND",
  263. "uniform vec3 wrapRGB;",
  264. "#endif",
  265. "varying vec3 vViewPosition;",
  266. "varying vec3 vNormal;"
  267. ].join("\n"),
  268. lights_phong_fragment: [
  269. "vec3 normal = normalize( vNormal );",
  270. "vec3 viewPosition = normalize( vViewPosition );",
  271. "#ifdef DOUBLE_SIDED",
  272. "normal = normal * ( -1.0 + 2.0 * float( gl_FrontFacing ) );",
  273. "#endif",
  274. "#if MAX_POINT_LIGHTS > 0",
  275. "vec3 pointDiffuse = vec3( 0.0 );",
  276. "vec3 pointSpecular = vec3( 0.0 );",
  277. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  278. "#ifdef PHONG_PER_PIXEL",
  279. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  280. "vec3 lVector = lPosition.xyz + vViewPosition.xyz;",
  281. "float lDistance = 1.0;",
  282. "if ( pointLightDistance[ i ] > 0.0 )",
  283. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  284. "lVector = normalize( lVector );",
  285. "#else",
  286. "vec3 lVector = normalize( vPointLight[ i ].xyz );",
  287. "float lDistance = vPointLight[ i ].w;",
  288. "#endif",
  289. // diffuse
  290. "float dotProduct = dot( normal, lVector );",
  291. "#ifdef WRAP_AROUND",
  292. "float pointDiffuseWeightFull = max( dotProduct, 0.0 );",
  293. "float pointDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );",
  294. "vec3 pointDiffuseWeight = mix( vec3 ( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );",
  295. "#else",
  296. "float pointDiffuseWeight = max( dotProduct, 0.0 );",
  297. "#endif",
  298. "pointDiffuse += diffuse * pointLightColor[ i ] * pointDiffuseWeight * lDistance;",
  299. // specular
  300. "vec3 pointHalfVector = normalize( lVector + viewPosition );",
  301. "float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
  302. "float pointSpecularWeight = max( pow( pointDotNormalHalf, shininess ), 0.0 );",
  303. "#ifdef PHYSICALLY_BASED_SHADING",
  304. "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( dot( lVector, pointHalfVector ), 5.0 );",
  305. "pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * lDistance;",
  306. "#else",
  307. "pointSpecular += specular * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * lDistance;",
  308. "#endif",
  309. "}",
  310. "#endif",
  311. "#if MAX_DIR_LIGHTS > 0",
  312. "vec3 dirDiffuse = vec3( 0.0 );",
  313. "vec3 dirSpecular = vec3( 0.0 );" ,
  314. "for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {",
  315. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  316. "vec3 dirVector = normalize( lDirection.xyz );",
  317. // diffuse
  318. "float dotProduct = dot( normal, dirVector );",
  319. "#ifdef WRAP_AROUND",
  320. "float dirDiffuseWeightFull = max( dotProduct, 0.0 );",
  321. "float dirDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );",
  322. "vec3 dirDiffuseWeight = mix( vec3( dirDiffuseWeightFull ), vec3( dirDiffuseWeightHalf ), wrapRGB );",
  323. "#else",
  324. "float dirDiffuseWeight = max( dotProduct, 0.0 );",
  325. "#endif",
  326. "dirDiffuse += diffuse * directionalLightColor[ i ] * dirDiffuseWeight;",
  327. // specular
  328. "vec3 dirHalfVector = normalize( dirVector + viewPosition );",
  329. "float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
  330. "float dirSpecularWeight = max( pow( dirDotNormalHalf, shininess ), 0.0 );",
  331. "#ifdef PHYSICALLY_BASED_SHADING",
  332. /*
  333. // fresnel term from skin shader
  334. "const float F0 = 0.128;",
  335. "float base = 1.0 - dot( viewPosition, dirHalfVector );",
  336. "float exponential = pow( base, 5.0 );",
  337. "float fresnel = exponential + F0 * ( 1.0 - exponential );",
  338. */
  339. /*
  340. // fresnel term from fresnel shader
  341. "const float mFresnelBias = 0.08;",
  342. "const float mFresnelScale = 0.3;",
  343. "const float mFresnelPower = 5.0;",
  344. "float fresnel = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( -viewPosition ), normal ), mFresnelPower );",
  345. */
  346. // normalization factor
  347. //float specularNormalization = ( shininess + 2.0 ) / 8.0;
  348. //"dirSpecular += specular * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization * fresnel;",
  349. "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( dot( dirVector, dirHalfVector ), 5.0 );",
  350. "dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight;",
  351. "#else",
  352. "dirSpecular += specular * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight;",
  353. "#endif",
  354. "}",
  355. "#endif",
  356. "vec3 totalDiffuse = vec3( 0.0 );",
  357. "vec3 totalSpecular = vec3( 0.0 );",
  358. "#if MAX_DIR_LIGHTS > 0",
  359. "totalDiffuse += dirDiffuse;",
  360. "totalSpecular += dirSpecular;",
  361. "#endif",
  362. "#if MAX_POINT_LIGHTS > 0",
  363. "totalDiffuse += pointDiffuse;",
  364. "totalSpecular += pointSpecular;",
  365. "#endif",
  366. "#ifdef METAL",
  367. "gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * ambient + totalSpecular );",
  368. "#else",
  369. "gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * ambient ) + totalSpecular;",
  370. "#endif"
  371. ].join("\n"),
  372. // VERTEX COLORS
  373. color_pars_fragment: [
  374. "#ifdef USE_COLOR",
  375. "varying vec3 vColor;",
  376. "#endif"
  377. ].join("\n"),
  378. color_fragment: [
  379. "#ifdef USE_COLOR",
  380. "gl_FragColor = gl_FragColor * vec4( vColor, opacity );",
  381. "#endif"
  382. ].join("\n"),
  383. color_pars_vertex: [
  384. "#ifdef USE_COLOR",
  385. "varying vec3 vColor;",
  386. "#endif"
  387. ].join("\n"),
  388. color_vertex: [
  389. "#ifdef USE_COLOR",
  390. "#ifdef GAMMA_INPUT",
  391. "vColor = color * color;",
  392. "#else",
  393. "vColor = color;",
  394. "#endif",
  395. "#endif"
  396. ].join("\n"),
  397. // SKINNING
  398. skinning_pars_vertex: [
  399. "#ifdef USE_SKINNING",
  400. "uniform mat4 boneGlobalMatrices[ MAX_BONES ];",
  401. "#endif"
  402. ].join("\n"),
  403. skinning_vertex: [
  404. "#ifdef USE_SKINNING",
  405. "gl_Position = ( boneGlobalMatrices[ int( skinIndex.x ) ] * skinVertexA ) * skinWeight.x;",
  406. "gl_Position += ( boneGlobalMatrices[ int( skinIndex.y ) ] * skinVertexB ) * skinWeight.y;",
  407. "gl_Position = projectionMatrix * modelViewMatrix * gl_Position;",
  408. "#endif"
  409. ].join("\n"),
  410. // MORPHING
  411. morphtarget_pars_vertex: [
  412. "#ifdef USE_MORPHTARGETS",
  413. "#ifndef USE_MORPHNORMALS",
  414. "uniform float morphTargetInfluences[ 8 ];",
  415. "#else",
  416. "uniform float morphTargetInfluences[ 4 ];",
  417. "#endif",
  418. "#endif"
  419. ].join("\n"),
  420. morphtarget_vertex: [
  421. "#ifdef USE_MORPHTARGETS",
  422. "vec3 morphed = vec3( 0.0 );",
  423. "morphed += ( morphTarget0 - position ) * morphTargetInfluences[ 0 ];",
  424. "morphed += ( morphTarget1 - position ) * morphTargetInfluences[ 1 ];",
  425. "morphed += ( morphTarget2 - position ) * morphTargetInfluences[ 2 ];",
  426. "morphed += ( morphTarget3 - position ) * morphTargetInfluences[ 3 ];",
  427. "#ifndef USE_MORPHNORMALS",
  428. "morphed += ( morphTarget4 - position ) * morphTargetInfluences[ 4 ];",
  429. "morphed += ( morphTarget5 - position ) * morphTargetInfluences[ 5 ];",
  430. "morphed += ( morphTarget6 - position ) * morphTargetInfluences[ 6 ];",
  431. "morphed += ( morphTarget7 - position ) * morphTargetInfluences[ 7 ];",
  432. "#endif",
  433. "morphed += position;",
  434. "gl_Position = projectionMatrix * modelViewMatrix * vec4( morphed, 1.0 );",
  435. "#endif"
  436. ].join("\n"),
  437. default_vertex : [
  438. "#ifndef USE_MORPHTARGETS",
  439. "#ifndef USE_SKINNING",
  440. "gl_Position = projectionMatrix * mvPosition;",
  441. "#endif",
  442. "#endif"
  443. ].join("\n"),
  444. morphnormal_vertex: [
  445. "#ifdef USE_MORPHNORMALS",
  446. "vec3 morphedNormal = vec3( 0.0 );",
  447. "morphedNormal += ( morphNormal0 - normal ) * morphTargetInfluences[ 0 ];",
  448. "morphedNormal += ( morphNormal1 - normal ) * morphTargetInfluences[ 1 ];",
  449. "morphedNormal += ( morphNormal2 - normal ) * morphTargetInfluences[ 2 ];",
  450. "morphedNormal += ( morphNormal3 - normal ) * morphTargetInfluences[ 3 ];",
  451. "morphedNormal += normal;",
  452. "vec3 transformedNormal = normalMatrix * morphedNormal;",
  453. "#else",
  454. "vec3 transformedNormal = normalMatrix * normal;",
  455. "#endif"
  456. ].join("\n"),
  457. // SHADOW MAP
  458. // based on SpiderGL shadow map and Fabien Sanglard's GLSL shadow mapping examples
  459. // http://spidergl.org/example.php?id=6
  460. // http://fabiensanglard.net/shadowmapping
  461. shadowmap_pars_fragment: [
  462. "#ifdef USE_SHADOWMAP",
  463. "uniform sampler2D shadowMap[ MAX_SHADOWS ];",
  464. "uniform vec2 shadowMapSize[ MAX_SHADOWS ];",
  465. "uniform float shadowDarkness[ MAX_SHADOWS ];",
  466. "uniform float shadowBias[ MAX_SHADOWS ];",
  467. "varying vec4 vShadowCoord[ MAX_SHADOWS ];",
  468. "float unpackDepth( const in vec4 rgba_depth ) {",
  469. "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 );",
  470. "float depth = dot( rgba_depth, bit_shift );",
  471. "return depth;",
  472. "}",
  473. "#endif"
  474. ].join("\n"),
  475. shadowmap_fragment: [
  476. "#ifdef USE_SHADOWMAP",
  477. "#ifdef SHADOWMAP_DEBUG",
  478. "vec3 frustumColors[3];",
  479. "frustumColors[0] = vec3( 1.0, 0.5, 0.0 );",
  480. "frustumColors[1] = vec3( 0.0, 1.0, 0.8 );",
  481. "frustumColors[2] = vec3( 0.0, 0.5, 1.0 );",
  482. "#endif",
  483. "#ifdef SHADOWMAP_CASCADE",
  484. "int inFrustumCount = 0;",
  485. "#endif",
  486. "float fDepth;",
  487. "vec3 shadowColor = vec3( 1.0 );",
  488. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  489. "vec3 shadowCoord = vShadowCoord[ i ].xyz / vShadowCoord[ i ].w;",
  490. // "if ( something && something )" breaks ATI OpenGL shader compiler
  491. // "if ( all( something, something ) )" using this instead
  492. "bvec4 inFrustumVec = bvec4 ( shadowCoord.x >= 0.0, shadowCoord.x <= 1.0, shadowCoord.y >= 0.0, shadowCoord.y <= 1.0 );",
  493. "bool inFrustum = all( inFrustumVec );",
  494. // don't shadow pixels outside of light frustum
  495. // use just first frustum (for cascades)
  496. // don't shadow pixels behind far plane of light frustum
  497. "#ifdef SHADOWMAP_CASCADE",
  498. "inFrustumCount += int( inFrustum );",
  499. "bvec3 frustumTestVec = bvec3( inFrustum, inFrustumCount == 1, shadowCoord.z <= 1.0 );",
  500. "#else",
  501. "bvec2 frustumTestVec = bvec2( inFrustum, shadowCoord.z <= 1.0 );",
  502. "#endif",
  503. "bool frustumTest = all( frustumTestVec );",
  504. "if ( frustumTest ) {",
  505. "shadowCoord.z += shadowBias[ i ];",
  506. "#ifdef SHADOWMAP_SOFT",
  507. // Percentage-close filtering
  508. // (9 pixel kernel)
  509. // http://fabiensanglard.net/shadowmappingPCF/
  510. "float shadow = 0.0;",
  511. /*
  512. // nested loops breaks shader compiler / validator on some ATI cards when using OpenGL
  513. // must enroll loop manually
  514. "for ( float y = -1.25; y <= 1.25; y += 1.25 )",
  515. "for ( float x = -1.25; x <= 1.25; x += 1.25 ) {",
  516. "vec4 rgbaDepth = texture2D( shadowMap[ i ], vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy );",
  517. // doesn't seem to produce any noticeable visual difference compared to simple "texture2D" lookup
  518. //"vec4 rgbaDepth = texture2DProj( shadowMap[ i ], vec4( vShadowCoord[ i ].w * ( vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy ), 0.05, vShadowCoord[ i ].w ) );",
  519. "float fDepth = unpackDepth( rgbaDepth );",
  520. "if ( fDepth < shadowCoord.z )",
  521. "shadow += 1.0;",
  522. "}",
  523. "shadow /= 9.0;",
  524. */
  525. "const float shadowDelta = 1.0 / 9.0;",
  526. "float xPixelOffset = 1.0 / shadowMapSize[ i ].x;",
  527. "float yPixelOffset = 1.0 / shadowMapSize[ i ].y;",
  528. "float dx0 = -1.25 * xPixelOffset;",
  529. "float dy0 = -1.25 * yPixelOffset;",
  530. "float dx1 = 1.25 * xPixelOffset;",
  531. "float dy1 = 1.25 * yPixelOffset;",
  532. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy0 ) ) );",
  533. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  534. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy0 ) ) );",
  535. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  536. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy0 ) ) );",
  537. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  538. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, 0.0 ) ) );",
  539. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  540. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy ) );",
  541. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  542. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, 0.0 ) ) );",
  543. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  544. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy1 ) ) );",
  545. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  546. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy1 ) ) );",
  547. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  548. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy1 ) ) );",
  549. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  550. "shadowColor = shadowColor * vec3( ( 1.0 - shadowDarkness[ i ] * shadow ) );",
  551. "#else",
  552. "vec4 rgbaDepth = texture2D( shadowMap[ i ], shadowCoord.xy );",
  553. "float fDepth = unpackDepth( rgbaDepth );",
  554. "if ( fDepth < shadowCoord.z )",
  555. // spot with multiple shadows is darker
  556. "shadowColor = shadowColor * vec3( 1.0 - shadowDarkness[ i ] );",
  557. // spot with multiple shadows has the same color as single shadow spot
  558. //"shadowColor = min( shadowColor, vec3( shadowDarkness[ i ] ) );",
  559. "#endif",
  560. "}",
  561. "#ifdef SHADOWMAP_DEBUG",
  562. "#ifdef SHADOWMAP_CASCADE",
  563. "if ( inFrustum && inFrustumCount == 1 ) gl_FragColor.xyz *= frustumColors[ i ];",
  564. "#else",
  565. "if ( inFrustum ) gl_FragColor.xyz *= frustumColors[ i ];",
  566. "#endif",
  567. "#endif",
  568. "}",
  569. "#ifdef GAMMA_OUTPUT",
  570. "shadowColor *= shadowColor;",
  571. "#endif",
  572. "gl_FragColor.xyz = gl_FragColor.xyz * shadowColor;",
  573. "#endif"
  574. ].join("\n"),
  575. shadowmap_pars_vertex: [
  576. "#ifdef USE_SHADOWMAP",
  577. "varying vec4 vShadowCoord[ MAX_SHADOWS ];",
  578. "uniform mat4 shadowMatrix[ MAX_SHADOWS ];",
  579. "#endif"
  580. ].join("\n"),
  581. shadowmap_vertex: [
  582. "#ifdef USE_SHADOWMAP",
  583. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  584. "#ifdef USE_MORPHTARGETS",
  585. "vShadowCoord[ i ] = shadowMatrix[ i ] * objectMatrix * vec4( morphed, 1.0 );",
  586. "#else",
  587. "vShadowCoord[ i ] = shadowMatrix[ i ] * objectMatrix * vec4( position, 1.0 );",
  588. "#endif",
  589. "}",
  590. "#endif"
  591. ].join("\n"),
  592. // ALPHATEST
  593. alphatest_fragment: [
  594. "#ifdef ALPHATEST",
  595. "if ( gl_FragColor.a < ALPHATEST ) discard;",
  596. "#endif"
  597. ].join("\n"),
  598. // LINEAR SPACE
  599. linear_to_gamma_fragment: [
  600. "#ifdef GAMMA_OUTPUT",
  601. "gl_FragColor.xyz = sqrt( gl_FragColor.xyz );",
  602. "#endif"
  603. ].join("\n"),
  604. };
  605. THREE.UniformsUtils = {
  606. merge: function ( uniforms ) {
  607. var u, p, tmp, merged = {};
  608. for ( u = 0; u < uniforms.length; u++ ) {
  609. tmp = this.clone( uniforms[ u ] );
  610. for ( p in tmp ) {
  611. merged[ p ] = tmp[ p ];
  612. }
  613. }
  614. return merged;
  615. },
  616. clone: function ( uniforms_src ) {
  617. var u, p, parameter, parameter_src, uniforms_dst = {};
  618. for ( u in uniforms_src ) {
  619. uniforms_dst[ u ] = {};
  620. for ( p in uniforms_src[ u ] ) {
  621. parameter_src = uniforms_src[ u ][ p ];
  622. if ( parameter_src instanceof THREE.Color ||
  623. parameter_src instanceof THREE.Vector2 ||
  624. parameter_src instanceof THREE.Vector3 ||
  625. parameter_src instanceof THREE.Vector4 ||
  626. parameter_src instanceof THREE.Matrix4 ||
  627. parameter_src instanceof THREE.Texture ) {
  628. uniforms_dst[ u ][ p ] = parameter_src.clone();
  629. } else if ( parameter_src instanceof Array ) {
  630. uniforms_dst[ u ][ p ] = parameter_src.slice();
  631. } else {
  632. uniforms_dst[ u ][ p ] = parameter_src;
  633. }
  634. }
  635. }
  636. return uniforms_dst;
  637. }
  638. };
  639. THREE.UniformsLib = {
  640. common: {
  641. "diffuse" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  642. "opacity" : { type: "f", value: 1.0 },
  643. "map" : { type: "t", value: 0, texture: null },
  644. "offsetRepeat" : { type: "v4", value: new THREE.Vector4( 0, 0, 1, 1 ) },
  645. "lightMap" : { type: "t", value: 2, texture: null },
  646. "envMap" : { type: "t", value: 1, texture: null },
  647. "flipEnvMap" : { type: "f", value: -1 },
  648. "useRefract" : { type: "i", value: 0 },
  649. "reflectivity" : { type: "f", value: 1.0 },
  650. "refractionRatio" : { type: "f", value: 0.98 },
  651. "combine" : { type: "i", value: 0 },
  652. "morphTargetInfluences" : { type: "f", value: 0 }
  653. },
  654. fog : {
  655. "fogDensity" : { type: "f", value: 0.00025 },
  656. "fogNear" : { type: "f", value: 1 },
  657. "fogFar" : { type: "f", value: 2000 },
  658. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  659. },
  660. lights: {
  661. "ambientLightColor" : { type: "fv", value: [] },
  662. "directionalLightDirection" : { type: "fv", value: [] },
  663. "directionalLightColor" : { type: "fv", value: [] },
  664. "pointLightColor" : { type: "fv", value: [] },
  665. "pointLightPosition" : { type: "fv", value: [] },
  666. "pointLightDistance" : { type: "fv1", value: [] }
  667. },
  668. particle: {
  669. "psColor" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  670. "opacity" : { type: "f", value: 1.0 },
  671. "size" : { type: "f", value: 1.0 },
  672. "scale" : { type: "f", value: 1.0 },
  673. "map" : { type: "t", value: 0, texture: null },
  674. "fogDensity" : { type: "f", value: 0.00025 },
  675. "fogNear" : { type: "f", value: 1 },
  676. "fogFar" : { type: "f", value: 2000 },
  677. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  678. },
  679. shadowmap: {
  680. "shadowMap": { type: "tv", value: 6, texture: [] },
  681. "shadowMapSize": { type: "v2v", value: [] },
  682. "shadowBias" : { type: "fv1", value: [] },
  683. "shadowDarkness": { type: "fv1", value: [] },
  684. "shadowMatrix" : { type: "m4v", value: [] },
  685. }
  686. };
  687. THREE.ShaderLib = {
  688. 'depth': {
  689. uniforms: {
  690. "mNear": { type: "f", value: 1.0 },
  691. "mFar" : { type: "f", value: 2000.0 },
  692. "opacity" : { type: "f", value: 1.0 }
  693. },
  694. vertexShader: [
  695. "void main() {",
  696. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  697. "}"
  698. ].join("\n"),
  699. fragmentShader: [
  700. "uniform float mNear;",
  701. "uniform float mFar;",
  702. "uniform float opacity;",
  703. "void main() {",
  704. "float depth = gl_FragCoord.z / gl_FragCoord.w;",
  705. "float color = 1.0 - smoothstep( mNear, mFar, depth );",
  706. "gl_FragColor = vec4( vec3( color ), opacity );",
  707. "}"
  708. ].join("\n")
  709. },
  710. 'normal': {
  711. uniforms: {
  712. "opacity" : { type: "f", value: 1.0 }
  713. },
  714. vertexShader: [
  715. "varying vec3 vNormal;",
  716. "void main() {",
  717. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  718. "vNormal = normalize( normalMatrix * normal );",
  719. "gl_Position = projectionMatrix * mvPosition;",
  720. "}"
  721. ].join("\n"),
  722. fragmentShader: [
  723. "uniform float opacity;",
  724. "varying vec3 vNormal;",
  725. "void main() {",
  726. "gl_FragColor = vec4( 0.5 * normalize( vNormal ) + 0.5, opacity );",
  727. "}"
  728. ].join("\n")
  729. },
  730. 'basic': {
  731. uniforms: THREE.UniformsUtils.merge( [
  732. THREE.UniformsLib[ "common" ],
  733. THREE.UniformsLib[ "fog" ],
  734. THREE.UniformsLib[ "shadowmap" ]
  735. ] ),
  736. vertexShader: [
  737. THREE.ShaderChunk[ "map_pars_vertex" ],
  738. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  739. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  740. THREE.ShaderChunk[ "color_pars_vertex" ],
  741. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  742. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  743. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  744. "void main() {",
  745. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  746. THREE.ShaderChunk[ "map_vertex" ],
  747. THREE.ShaderChunk[ "lightmap_vertex" ],
  748. THREE.ShaderChunk[ "envmap_vertex" ],
  749. THREE.ShaderChunk[ "color_vertex" ],
  750. THREE.ShaderChunk[ "skinning_vertex" ],
  751. THREE.ShaderChunk[ "morphtarget_vertex" ],
  752. THREE.ShaderChunk[ "default_vertex" ],
  753. THREE.ShaderChunk[ "shadowmap_vertex" ],
  754. "}"
  755. ].join("\n"),
  756. fragmentShader: [
  757. "uniform vec3 diffuse;",
  758. "uniform float opacity;",
  759. THREE.ShaderChunk[ "color_pars_fragment" ],
  760. THREE.ShaderChunk[ "map_pars_fragment" ],
  761. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  762. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  763. THREE.ShaderChunk[ "fog_pars_fragment" ],
  764. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  765. "void main() {",
  766. "gl_FragColor = vec4( diffuse, opacity );",
  767. THREE.ShaderChunk[ "map_fragment" ],
  768. THREE.ShaderChunk[ "alphatest_fragment" ],
  769. THREE.ShaderChunk[ "lightmap_fragment" ],
  770. THREE.ShaderChunk[ "color_fragment" ],
  771. THREE.ShaderChunk[ "envmap_fragment" ],
  772. THREE.ShaderChunk[ "shadowmap_fragment" ],
  773. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  774. THREE.ShaderChunk[ "fog_fragment" ],
  775. "}"
  776. ].join("\n")
  777. },
  778. 'lambert': {
  779. uniforms: THREE.UniformsUtils.merge( [
  780. THREE.UniformsLib[ "common" ],
  781. THREE.UniformsLib[ "fog" ],
  782. THREE.UniformsLib[ "lights" ],
  783. THREE.UniformsLib[ "shadowmap" ],
  784. {
  785. "ambient" : { type: "c", value: new THREE.Color( 0x050505 ) },
  786. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  787. }
  788. ] ),
  789. vertexShader: [
  790. "varying vec3 vLightFront;",
  791. "#ifdef DOUBLE_SIDED",
  792. "varying vec3 vLightBack;",
  793. "#endif",
  794. THREE.ShaderChunk[ "map_pars_vertex" ],
  795. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  796. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  797. THREE.ShaderChunk[ "lights_lambert_pars_vertex" ],
  798. THREE.ShaderChunk[ "color_pars_vertex" ],
  799. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  800. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  801. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  802. "void main() {",
  803. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  804. THREE.ShaderChunk[ "map_vertex" ],
  805. THREE.ShaderChunk[ "lightmap_vertex" ],
  806. THREE.ShaderChunk[ "envmap_vertex" ],
  807. THREE.ShaderChunk[ "color_vertex" ],
  808. THREE.ShaderChunk[ "morphnormal_vertex" ],
  809. THREE.ShaderChunk[ "lights_lambert_vertex" ],
  810. THREE.ShaderChunk[ "skinning_vertex" ],
  811. THREE.ShaderChunk[ "morphtarget_vertex" ],
  812. THREE.ShaderChunk[ "default_vertex" ],
  813. THREE.ShaderChunk[ "shadowmap_vertex" ],
  814. "}"
  815. ].join("\n"),
  816. fragmentShader: [
  817. "uniform float opacity;",
  818. "varying vec3 vLightFront;",
  819. "#ifdef DOUBLE_SIDED",
  820. "varying vec3 vLightBack;",
  821. "#endif",
  822. THREE.ShaderChunk[ "color_pars_fragment" ],
  823. THREE.ShaderChunk[ "map_pars_fragment" ],
  824. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  825. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  826. THREE.ShaderChunk[ "fog_pars_fragment" ],
  827. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  828. "void main() {",
  829. "gl_FragColor = vec4( vec3 ( 1.0 ), opacity );",
  830. THREE.ShaderChunk[ "map_fragment" ],
  831. THREE.ShaderChunk[ "alphatest_fragment" ],
  832. "#ifdef DOUBLE_SIDED",
  833. //"float isFront = float( gl_FrontFacing );",
  834. //"gl_FragColor.xyz *= isFront * vLightFront + ( 1.0 - isFront ) * vLightBack;",
  835. "if ( gl_FrontFacing )",
  836. "gl_FragColor.xyz *= vLightFront;",
  837. "else",
  838. "gl_FragColor.xyz *= vLightBack;",
  839. "#else",
  840. "gl_FragColor.xyz *= vLightFront;",
  841. "#endif",
  842. THREE.ShaderChunk[ "lightmap_fragment" ],
  843. THREE.ShaderChunk[ "color_fragment" ],
  844. THREE.ShaderChunk[ "envmap_fragment" ],
  845. THREE.ShaderChunk[ "shadowmap_fragment" ],
  846. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  847. THREE.ShaderChunk[ "fog_fragment" ],
  848. "}"
  849. ].join("\n")
  850. },
  851. 'phong': {
  852. uniforms: THREE.UniformsUtils.merge( [
  853. THREE.UniformsLib[ "common" ],
  854. THREE.UniformsLib[ "fog" ],
  855. THREE.UniformsLib[ "lights" ],
  856. THREE.UniformsLib[ "shadowmap" ],
  857. {
  858. "ambient" : { type: "c", value: new THREE.Color( 0x050505 ) },
  859. "specular" : { type: "c", value: new THREE.Color( 0x111111 ) },
  860. "shininess": { type: "f", value: 30 },
  861. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  862. }
  863. ] ),
  864. vertexShader: [
  865. "varying vec3 vViewPosition;",
  866. "varying vec3 vNormal;",
  867. THREE.ShaderChunk[ "map_pars_vertex" ],
  868. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  869. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  870. THREE.ShaderChunk[ "lights_phong_pars_vertex" ],
  871. THREE.ShaderChunk[ "color_pars_vertex" ],
  872. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  873. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  874. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  875. "void main() {",
  876. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  877. THREE.ShaderChunk[ "map_vertex" ],
  878. THREE.ShaderChunk[ "lightmap_vertex" ],
  879. THREE.ShaderChunk[ "envmap_vertex" ],
  880. THREE.ShaderChunk[ "color_vertex" ],
  881. "#ifndef USE_ENVMAP",
  882. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  883. "#endif",
  884. "vViewPosition = -mvPosition.xyz;",
  885. THREE.ShaderChunk[ "morphnormal_vertex" ],
  886. "vNormal = transformedNormal;",
  887. THREE.ShaderChunk[ "lights_phong_vertex" ],
  888. THREE.ShaderChunk[ "skinning_vertex" ],
  889. THREE.ShaderChunk[ "morphtarget_vertex" ],
  890. THREE.ShaderChunk[ "default_vertex" ],
  891. THREE.ShaderChunk[ "shadowmap_vertex" ],
  892. "}"
  893. ].join("\n"),
  894. fragmentShader: [
  895. "uniform vec3 diffuse;",
  896. "uniform float opacity;",
  897. "uniform vec3 ambient;",
  898. "uniform vec3 specular;",
  899. "uniform float shininess;",
  900. THREE.ShaderChunk[ "color_pars_fragment" ],
  901. THREE.ShaderChunk[ "map_pars_fragment" ],
  902. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  903. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  904. THREE.ShaderChunk[ "fog_pars_fragment" ],
  905. THREE.ShaderChunk[ "lights_phong_pars_fragment" ],
  906. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  907. "void main() {",
  908. "gl_FragColor = vec4( vec3 ( 1.0 ), opacity );",
  909. THREE.ShaderChunk[ "map_fragment" ],
  910. THREE.ShaderChunk[ "alphatest_fragment" ],
  911. THREE.ShaderChunk[ "lights_phong_fragment" ],
  912. THREE.ShaderChunk[ "lightmap_fragment" ],
  913. THREE.ShaderChunk[ "color_fragment" ],
  914. THREE.ShaderChunk[ "envmap_fragment" ],
  915. THREE.ShaderChunk[ "shadowmap_fragment" ],
  916. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  917. THREE.ShaderChunk[ "fog_fragment" ],
  918. "}"
  919. ].join("\n")
  920. },
  921. 'particle_basic': {
  922. uniforms: THREE.UniformsUtils.merge( [
  923. THREE.UniformsLib[ "particle" ],
  924. THREE.UniformsLib[ "shadowmap" ]
  925. ] ),
  926. vertexShader: [
  927. "uniform float size;",
  928. "uniform float scale;",
  929. THREE.ShaderChunk[ "color_pars_vertex" ],
  930. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  931. "void main() {",
  932. THREE.ShaderChunk[ "color_vertex" ],
  933. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  934. "#ifdef USE_SIZEATTENUATION",
  935. "gl_PointSize = size * ( scale / length( mvPosition.xyz ) );",
  936. "#else",
  937. "gl_PointSize = size;",
  938. "#endif",
  939. "gl_Position = projectionMatrix * mvPosition;",
  940. THREE.ShaderChunk[ "shadowmap_vertex" ],
  941. "}"
  942. ].join("\n"),
  943. fragmentShader: [
  944. "uniform vec3 psColor;",
  945. "uniform float opacity;",
  946. THREE.ShaderChunk[ "color_pars_fragment" ],
  947. THREE.ShaderChunk[ "map_particle_pars_fragment" ],
  948. THREE.ShaderChunk[ "fog_pars_fragment" ],
  949. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  950. "void main() {",
  951. "gl_FragColor = vec4( psColor, opacity );",
  952. THREE.ShaderChunk[ "map_particle_fragment" ],
  953. THREE.ShaderChunk[ "alphatest_fragment" ],
  954. THREE.ShaderChunk[ "color_fragment" ],
  955. THREE.ShaderChunk[ "shadowmap_fragment" ],
  956. THREE.ShaderChunk[ "fog_fragment" ],
  957. "}"
  958. ].join("\n")
  959. },
  960. // Depth encoding into RGBA texture
  961. // based on SpiderGL shadow map example
  962. // http://spidergl.org/example.php?id=6
  963. // originally from
  964. // http://www.gamedev.net/topic/442138-packing-a-float-into-a-a8r8g8b8-texture-shader/page__whichpage__1%25EF%25BF%25BD
  965. // see also here:
  966. // http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
  967. 'depthRGBA': {
  968. uniforms: {},
  969. vertexShader: [
  970. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  971. "void main() {",
  972. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  973. THREE.ShaderChunk[ "morphtarget_vertex" ],
  974. THREE.ShaderChunk[ "default_vertex" ],
  975. "}"
  976. ].join("\n"),
  977. fragmentShader: [
  978. "vec4 pack_depth( const in float depth ) {",
  979. "const vec4 bit_shift = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 );",
  980. "const vec4 bit_mask = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 );",
  981. "vec4 res = fract( depth * bit_shift );",
  982. "res -= res.xxyz * bit_mask;",
  983. "return res;",
  984. "}",
  985. "void main() {",
  986. "gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z );",
  987. //"gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z / gl_FragCoord.w );",
  988. //"float z = ( ( gl_FragCoord.z / gl_FragCoord.w ) - 3.0 ) / ( 4000.0 - 3.0 );",
  989. //"gl_FragData[ 0 ] = pack_depth( z );",
  990. //"gl_FragData[ 0 ] = vec4( z, z, z, 1.0 );",
  991. "}"
  992. ].join("\n")
  993. }
  994. };
粤ICP备19079148号