ShaderLib.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. /**
  2. * Webgl Shader Library for three.js
  3. *
  4. * @author alteredq / http://alteredqualia.com/
  5. * @author mrdoob / http://mrdoob.com/
  6. * @author mikael emtinger / http://gomo.se/
  7. */
  8. THREE.ShaderLib = {
  9. 'basic': {
  10. uniforms: THREE.UniformsUtils.merge( [
  11. THREE.UniformsLib[ "common" ],
  12. THREE.UniformsLib[ "aomap" ],
  13. THREE.UniformsLib[ "fog" ]
  14. ] ),
  15. vertexShader: [
  16. '#include <common>',
  17. '#include <uv_pars_vertex>',
  18. '#include <uv2_pars_vertex>',
  19. '#include <envmap_pars_vertex>',
  20. '#include <color_pars_vertex>',
  21. '#include <morphtarget_pars_vertex>',
  22. '#include <skinning_pars_vertex>',
  23. '#include <logdepthbuf_pars_vertex>',
  24. "void main() {",
  25. '#include <uv_vertex>',
  26. '#include <uv2_vertex>',
  27. '#include <color_vertex>',
  28. '#include <skinbase_vertex>',
  29. " #ifdef USE_ENVMAP",
  30. '#include <beginnormal_vertex>',
  31. '#include <morphnormal_vertex>',
  32. '#include <skinnormal_vertex>',
  33. '#include <defaultnormal_vertex>',
  34. " #endif",
  35. '#include <begin_vertex>',
  36. '#include <morphtarget_vertex>',
  37. '#include <skinning_vertex>',
  38. '#include <project_vertex>',
  39. '#include <logdepthbuf_vertex>',
  40. '#include <worldpos_vertex>',
  41. '#include <envmap_vertex>',
  42. "}"
  43. ].join( "\n" ),
  44. fragmentShader: [
  45. "uniform vec3 diffuse;",
  46. "uniform float opacity;",
  47. "#ifndef FLAT_SHADED",
  48. " varying vec3 vNormal;",
  49. "#endif",
  50. '#include <common>',
  51. '#include <color_pars_fragment>',
  52. '#include <uv_pars_fragment>',
  53. '#include <uv2_pars_fragment>',
  54. '#include <map_pars_fragment>',
  55. '#include <alphamap_pars_fragment>',
  56. '#include <aomap_pars_fragment>',
  57. '#include <envmap_pars_fragment>',
  58. '#include <fog_pars_fragment>',
  59. '#include <specularmap_pars_fragment>',
  60. '#include <logdepthbuf_pars_fragment>',
  61. "void main() {",
  62. " vec4 diffuseColor = vec4( diffuse, opacity );",
  63. '#include <logdepthbuf_fragment>',
  64. '#include <map_fragment>',
  65. '#include <color_fragment>',
  66. '#include <alphamap_fragment>',
  67. '#include <alphatest_fragment>',
  68. '#include <specularmap_fragment>',
  69. " ReflectedLight reflectedLight;",
  70. " reflectedLight.directDiffuse = vec3( 0.0 );",
  71. " reflectedLight.directSpecular = vec3( 0.0 );",
  72. " reflectedLight.indirectDiffuse = diffuseColor.rgb;",
  73. " reflectedLight.indirectSpecular = vec3( 0.0 );",
  74. '#include <aomap_fragment>',
  75. " vec3 outgoingLight = reflectedLight.indirectDiffuse;",
  76. '#include <envmap_fragment>',
  77. '#include <linear_to_gamma_fragment>',
  78. '#include <fog_fragment>',
  79. " gl_FragColor = vec4( outgoingLight, diffuseColor.a );",
  80. "}"
  81. ].join( "\n" )
  82. },
  83. 'lambert': {
  84. uniforms: THREE.UniformsUtils.merge( [
  85. THREE.UniformsLib[ "common" ],
  86. THREE.UniformsLib[ "aomap" ],
  87. THREE.UniformsLib[ "lightmap" ],
  88. THREE.UniformsLib[ "emissivemap" ],
  89. THREE.UniformsLib[ "fog" ],
  90. THREE.UniformsLib[ "ambient" ],
  91. THREE.UniformsLib[ "lights" ],
  92. {
  93. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) }
  94. }
  95. ] ),
  96. vertexShader: [
  97. "#define LAMBERT",
  98. "varying vec3 vLightFront;",
  99. "#ifdef DOUBLE_SIDED",
  100. " varying vec3 vLightBack;",
  101. "#endif",
  102. '#include <common>',
  103. '#include <uv_pars_vertex>',
  104. '#include <uv2_pars_vertex>',
  105. '#include <envmap_pars_vertex>',
  106. '#include <bsdfs>',
  107. '#include <lights_pars>',
  108. '#include <color_pars_vertex>',
  109. '#include <morphtarget_pars_vertex>',
  110. '#include <skinning_pars_vertex>',
  111. '#include <shadowmap_pars_vertex>',
  112. '#include <logdepthbuf_pars_vertex>',
  113. "void main() {",
  114. '#include <uv_vertex>',
  115. '#include <uv2_vertex>',
  116. '#include <color_vertex>',
  117. '#include <beginnormal_vertex>',
  118. '#include <morphnormal_vertex>',
  119. '#include <skinbase_vertex>',
  120. '#include <skinnormal_vertex>',
  121. '#include <defaultnormal_vertex>',
  122. '#include <begin_vertex>',
  123. '#include <morphtarget_vertex>',
  124. '#include <skinning_vertex>',
  125. '#include <project_vertex>',
  126. '#include <logdepthbuf_vertex>',
  127. '#include <worldpos_vertex>',
  128. '#include <envmap_vertex>',
  129. '#include <lights_lambert_vertex>',
  130. '#include <shadowmap_vertex>',
  131. "}"
  132. ].join( "\n" ),
  133. fragmentShader: [
  134. "uniform vec3 diffuse;",
  135. "uniform vec3 emissive;",
  136. "uniform float opacity;",
  137. "varying vec3 vLightFront;",
  138. "#ifdef DOUBLE_SIDED",
  139. " varying vec3 vLightBack;",
  140. "#endif",
  141. '#include <common>',
  142. '#include <color_pars_fragment>',
  143. '#include <uv_pars_fragment>',
  144. '#include <uv2_pars_fragment>',
  145. '#include <map_pars_fragment>',
  146. '#include <alphamap_pars_fragment>',
  147. '#include <aomap_pars_fragment>',
  148. '#include <lightmap_pars_fragment>',
  149. '#include <emissivemap_pars_fragment>',
  150. '#include <envmap_pars_fragment>',
  151. '#include <bsdfs>',
  152. '#include <ambient_pars>',
  153. '#include <lights_pars>',
  154. '#include <fog_pars_fragment>',
  155. '#include <shadowmap_pars_fragment>',
  156. '#include <shadowmask_pars_fragment>',
  157. '#include <specularmap_pars_fragment>',
  158. '#include <logdepthbuf_pars_fragment>',
  159. "void main() {",
  160. " vec4 diffuseColor = vec4( diffuse, opacity );",
  161. " ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
  162. " vec3 totalEmissiveLight = emissive;",
  163. '#include <logdepthbuf_fragment>',
  164. '#include <map_fragment>',
  165. '#include <color_fragment>',
  166. '#include <alphamap_fragment>',
  167. '#include <alphatest_fragment>',
  168. '#include <specularmap_fragment>',
  169. '#include <emissivemap_fragment>',
  170. // accumulation
  171. " reflectedLight.indirectDiffuse = getAmbientLightIrradiance( ambientLightColor );",
  172. '#include <lightmap_fragment>',
  173. " reflectedLight.indirectDiffuse *= BRDF_Diffuse_Lambert( diffuseColor.rgb );",
  174. " #ifdef DOUBLE_SIDED",
  175. " reflectedLight.directDiffuse = ( gl_FrontFacing ) ? vLightFront : vLightBack;",
  176. " #else",
  177. " reflectedLight.directDiffuse = vLightFront;",
  178. " #endif",
  179. " reflectedLight.directDiffuse *= BRDF_Diffuse_Lambert( diffuseColor.rgb ) * getShadowMask();",
  180. // modulation
  181. '#include <aomap_fragment>',
  182. " vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + totalEmissiveLight;",
  183. '#include <envmap_fragment>',
  184. '#include <linear_to_gamma_fragment>',
  185. '#include <fog_fragment>',
  186. " gl_FragColor = vec4( outgoingLight, diffuseColor.a );",
  187. "}"
  188. ].join( "\n" )
  189. },
  190. 'phong': {
  191. uniforms: THREE.UniformsUtils.merge( [
  192. THREE.UniformsLib[ "common" ],
  193. THREE.UniformsLib[ "aomap" ],
  194. THREE.UniformsLib[ "lightmap" ],
  195. THREE.UniformsLib[ "emissivemap" ],
  196. THREE.UniformsLib[ "bumpmap" ],
  197. THREE.UniformsLib[ "normalmap" ],
  198. THREE.UniformsLib[ "displacementmap" ],
  199. THREE.UniformsLib[ "fog" ],
  200. THREE.UniformsLib[ "ambient" ],
  201. THREE.UniformsLib[ "lights" ],
  202. {
  203. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  204. "specular" : { type: "c", value: new THREE.Color( 0x111111 ) },
  205. "shininess": { type: "f", value: 30 }
  206. }
  207. ] ),
  208. vertexShader: [
  209. "#define PHONG",
  210. "varying vec3 vViewPosition;",
  211. "#ifndef FLAT_SHADED",
  212. " varying vec3 vNormal;",
  213. "#endif",
  214. '#include <common>',
  215. '#include <uv_pars_vertex>',
  216. '#include <uv2_pars_vertex>',
  217. '#include <displacementmap_pars_vertex>',
  218. '#include <envmap_pars_vertex>',
  219. '#include <lights_phong_pars_vertex>',
  220. '#include <color_pars_vertex>',
  221. '#include <morphtarget_pars_vertex>',
  222. '#include <skinning_pars_vertex>',
  223. '#include <shadowmap_pars_vertex>',
  224. '#include <logdepthbuf_pars_vertex>',
  225. "void main() {",
  226. '#include <uv_vertex>',
  227. '#include <uv2_vertex>',
  228. '#include <color_vertex>',
  229. '#include <beginnormal_vertex>',
  230. '#include <morphnormal_vertex>',
  231. '#include <skinbase_vertex>',
  232. '#include <skinnormal_vertex>',
  233. '#include <defaultnormal_vertex>',
  234. "#ifndef FLAT_SHADED", // Normal computed with derivatives when FLAT_SHADED
  235. " vNormal = normalize( transformedNormal );",
  236. "#endif",
  237. '#include <begin_vertex>',
  238. '#include <displacementmap_vertex>',
  239. '#include <morphtarget_vertex>',
  240. '#include <skinning_vertex>',
  241. '#include <project_vertex>',
  242. '#include <logdepthbuf_vertex>',
  243. " vViewPosition = - mvPosition.xyz;",
  244. '#include <worldpos_vertex>',
  245. '#include <envmap_vertex>',
  246. '#include <lights_phong_vertex>',
  247. '#include <shadowmap_vertex>',
  248. "}"
  249. ].join( "\n" ),
  250. fragmentShader: [
  251. "#define PHONG",
  252. "uniform vec3 diffuse;",
  253. "uniform vec3 emissive;",
  254. "uniform vec3 specular;",
  255. "uniform float shininess;",
  256. "uniform float opacity;",
  257. '#include <common>',
  258. '#include <color_pars_fragment>',
  259. '#include <uv_pars_fragment>',
  260. '#include <uv2_pars_fragment>',
  261. '#include <map_pars_fragment>',
  262. '#include <alphamap_pars_fragment>',
  263. '#include <aomap_pars_fragment>',
  264. '#include <lightmap_pars_fragment>',
  265. '#include <emissivemap_pars_fragment>',
  266. '#include <envmap_pars_fragment>',
  267. '#include <fog_pars_fragment>',
  268. '#include <bsdfs>',
  269. '#include <ambient_pars>',
  270. '#include <lights_pars>',
  271. '#include <lights_phong_pars_fragment>',
  272. '#include <shadowmap_pars_fragment>',
  273. '#include <bumpmap_pars_fragment>',
  274. '#include <normalmap_pars_fragment>',
  275. '#include <specularmap_pars_fragment>',
  276. '#include <logdepthbuf_pars_fragment>',
  277. "void main() {",
  278. " vec4 diffuseColor = vec4( diffuse, opacity );",
  279. " ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
  280. " vec3 totalEmissiveLight = emissive;",
  281. '#include <logdepthbuf_fragment>',
  282. '#include <map_fragment>',
  283. '#include <color_fragment>',
  284. '#include <alphamap_fragment>',
  285. '#include <alphatest_fragment>',
  286. '#include <specularmap_fragment>',
  287. '#include <normal_fragment>',
  288. '#include <emissivemap_fragment>',
  289. // accumulation
  290. '#include <lights_phong_fragment>',
  291. '#include <lights_template>',
  292. // modulation
  293. '#include <aomap_fragment>',
  294. "vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveLight;",
  295. '#include <envmap_fragment>',
  296. '#include <linear_to_gamma_fragment>',
  297. '#include <fog_fragment>',
  298. " gl_FragColor = vec4( outgoingLight, diffuseColor.a );",
  299. "}"
  300. ].join( "\n" )
  301. },
  302. 'standard': {
  303. uniforms: THREE.UniformsUtils.merge( [
  304. THREE.UniformsLib[ "common" ],
  305. THREE.UniformsLib[ "aomap" ],
  306. THREE.UniformsLib[ "lightmap" ],
  307. THREE.UniformsLib[ "emissivemap" ],
  308. THREE.UniformsLib[ "bumpmap" ],
  309. THREE.UniformsLib[ "normalmap" ],
  310. THREE.UniformsLib[ "displacementmap" ],
  311. THREE.UniformsLib[ "roughnessmap" ],
  312. THREE.UniformsLib[ "metalnessmap" ],
  313. THREE.UniformsLib[ "fog" ],
  314. THREE.UniformsLib[ "ambient" ],
  315. THREE.UniformsLib[ "lights" ],
  316. {
  317. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  318. "roughness": { type: "f", value: 0.5 },
  319. "metalness": { type: "f", value: 0 },
  320. "envMapIntensity" : { type: "f", value: 1 } // temporary
  321. }
  322. ] ),
  323. vertexShader: [
  324. "#define STANDARD",
  325. "varying vec3 vViewPosition;",
  326. "#ifndef FLAT_SHADED",
  327. " varying vec3 vNormal;",
  328. "#endif",
  329. '#include <common>',
  330. '#include <uv_pars_vertex>',
  331. '#include <uv2_pars_vertex>',
  332. '#include <displacementmap_pars_vertex>',
  333. '#include <envmap_pars_vertex>',
  334. '#include <color_pars_vertex>',
  335. '#include <morphtarget_pars_vertex>',
  336. '#include <skinning_pars_vertex>',
  337. '#include <shadowmap_pars_vertex>',
  338. '#include <specularmap_pars_fragment>',
  339. '#include <logdepthbuf_pars_vertex>',
  340. "void main() {", // STANDARD
  341. '#include <uv_vertex>',
  342. '#include <uv2_vertex>',
  343. '#include <color_vertex>',
  344. '#include <beginnormal_vertex>',
  345. '#include <morphnormal_vertex>',
  346. '#include <skinbase_vertex>',
  347. '#include <skinnormal_vertex>',
  348. '#include <defaultnormal_vertex>',
  349. "#ifndef FLAT_SHADED", // Normal computed with derivatives when FLAT_SHADED
  350. " vNormal = normalize( transformedNormal );",
  351. "#endif",
  352. '#include <begin_vertex>',
  353. '#include <displacementmap_vertex>',
  354. '#include <morphtarget_vertex>',
  355. '#include <skinning_vertex>',
  356. '#include <project_vertex>',
  357. '#include <logdepthbuf_vertex>',
  358. " vViewPosition = - mvPosition.xyz;",
  359. '#include <worldpos_vertex>',
  360. '#include <envmap_vertex>',
  361. '#include <shadowmap_vertex>',
  362. "}"
  363. ].join( "\n" ),
  364. fragmentShader: [
  365. "#define STANDARD",
  366. "uniform vec3 diffuse;",
  367. "uniform vec3 emissive;",
  368. "uniform float roughness;",
  369. "uniform float metalness;",
  370. "uniform float opacity;",
  371. "uniform float envMapIntensity;", // temporary
  372. "varying vec3 vViewPosition;",
  373. "#ifndef FLAT_SHADED",
  374. " varying vec3 vNormal;",
  375. "#endif",
  376. '#include <common>',
  377. '#include <color_pars_fragment>',
  378. '#include <uv_pars_fragment>',
  379. '#include <uv2_pars_fragment>',
  380. '#include <map_pars_fragment>',
  381. '#include <alphamap_pars_fragment>',
  382. '#include <aomap_pars_fragment>',
  383. '#include <lightmap_pars_fragment>',
  384. '#include <emissivemap_pars_fragment>',
  385. '#include <envmap_pars_fragment>',
  386. '#include <fog_pars_fragment>',
  387. '#include <bsdfs>',
  388. '#include <ambient_pars>',
  389. '#include <lights_pars>',
  390. '#include <lights_standard_pars_fragment>',
  391. '#include <shadowmap_pars_fragment>',
  392. '#include <bumpmap_pars_fragment>',
  393. '#include <normalmap_pars_fragment>',
  394. '#include <roughnessmap_pars_fragment>',
  395. '#include <metalnessmap_pars_fragment>',
  396. '#include <logdepthbuf_pars_fragment>',
  397. "void main() {",
  398. " vec4 diffuseColor = vec4( diffuse, opacity );",
  399. " ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
  400. " vec3 totalEmissiveLight = emissive;",
  401. '#include <logdepthbuf_fragment>',
  402. '#include <map_fragment>',
  403. '#include <color_fragment>',
  404. '#include <alphamap_fragment>',
  405. '#include <alphatest_fragment>',
  406. '#include <specularmap_fragment>',
  407. '#include <roughnessmap_fragment>',
  408. '#include <metalnessmap_fragment>',
  409. '#include <normal_fragment>',
  410. '#include <emissivemap_fragment>',
  411. // accumulation
  412. '#include <lights_standard_fragment>',
  413. '#include <lights_template>',
  414. // modulation
  415. '#include <aomap_fragment>',
  416. "vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveLight;",
  417. '#include <linear_to_gamma_fragment>',
  418. '#include <fog_fragment>',
  419. " gl_FragColor = vec4( outgoingLight, diffuseColor.a );",
  420. "}"
  421. ].join( "\n" )
  422. },
  423. 'points': {
  424. uniforms: THREE.UniformsUtils.merge( [
  425. THREE.UniformsLib[ "points" ],
  426. THREE.UniformsLib[ "fog" ]
  427. ] ),
  428. vertexShader: [
  429. "uniform float size;",
  430. "uniform float scale;",
  431. '#include <common>',
  432. '#include <color_pars_vertex>',
  433. '#include <shadowmap_pars_vertex>',
  434. '#include <logdepthbuf_pars_vertex>',
  435. "void main() {",
  436. '#include <color_vertex>',
  437. '#include <begin_vertex>',
  438. '#include <project_vertex>',
  439. " #ifdef USE_SIZEATTENUATION",
  440. " gl_PointSize = size * ( scale / - mvPosition.z );",
  441. " #else",
  442. " gl_PointSize = size;",
  443. " #endif",
  444. '#include <logdepthbuf_vertex>',
  445. '#include <worldpos_vertex>',
  446. '#include <shadowmap_vertex>',
  447. "}"
  448. ].join( "\n" ),
  449. fragmentShader: [
  450. "uniform vec3 diffuse;",
  451. "uniform float opacity;",
  452. '#include <common>',
  453. '#include <color_pars_fragment>',
  454. '#include <map_particle_pars_fragment>',
  455. '#include <fog_pars_fragment>',
  456. '#include <shadowmap_pars_fragment>',
  457. '#include <logdepthbuf_pars_fragment>',
  458. "void main() {",
  459. " vec3 outgoingLight = vec3( 0.0 );",
  460. " vec4 diffuseColor = vec4( diffuse, opacity );",
  461. '#include <logdepthbuf_fragment>',
  462. '#include <map_particle_fragment>',
  463. '#include <color_fragment>',
  464. '#include <alphatest_fragment>',
  465. " outgoingLight = diffuseColor.rgb;",
  466. '#include <fog_fragment>',
  467. " gl_FragColor = vec4( outgoingLight, diffuseColor.a );",
  468. "}"
  469. ].join( "\n" )
  470. },
  471. 'dashed': {
  472. uniforms: THREE.UniformsUtils.merge( [
  473. THREE.UniformsLib[ "common" ],
  474. THREE.UniformsLib[ "fog" ],
  475. {
  476. "scale" : { type: "f", value: 1 },
  477. "dashSize" : { type: "f", value: 1 },
  478. "totalSize": { type: "f", value: 2 }
  479. }
  480. ] ),
  481. vertexShader: [
  482. "uniform float scale;",
  483. "attribute float lineDistance;",
  484. "varying float vLineDistance;",
  485. '#include <common>',
  486. '#include <color_pars_vertex>',
  487. '#include <logdepthbuf_pars_vertex>',
  488. "void main() {",
  489. '#include <color_vertex>',
  490. " vLineDistance = scale * lineDistance;",
  491. " vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  492. " gl_Position = projectionMatrix * mvPosition;",
  493. '#include <logdepthbuf_vertex>',
  494. "}"
  495. ].join( "\n" ),
  496. fragmentShader: [
  497. "uniform vec3 diffuse;",
  498. "uniform float opacity;",
  499. "uniform float dashSize;",
  500. "uniform float totalSize;",
  501. "varying float vLineDistance;",
  502. '#include <common>',
  503. '#include <color_pars_fragment>',
  504. '#include <fog_pars_fragment>',
  505. '#include <logdepthbuf_pars_fragment>',
  506. "void main() {",
  507. " if ( mod( vLineDistance, totalSize ) > dashSize ) {",
  508. " discard;",
  509. " }",
  510. " vec3 outgoingLight = vec3( 0.0 );",
  511. " vec4 diffuseColor = vec4( diffuse, opacity );",
  512. '#include <logdepthbuf_fragment>',
  513. '#include <color_fragment>',
  514. " outgoingLight = diffuseColor.rgb;", // simple shader
  515. '#include <fog_fragment>',
  516. " gl_FragColor = vec4( outgoingLight, diffuseColor.a );",
  517. "}"
  518. ].join( "\n" )
  519. },
  520. 'depth': {
  521. uniforms: {
  522. "mNear": { type: "f", value: 1.0 },
  523. "mFar" : { type: "f", value: 2000.0 },
  524. "opacity" : { type: "f", value: 1.0 }
  525. },
  526. vertexShader: [
  527. '#include <common>',
  528. '#include <morphtarget_pars_vertex>',
  529. '#include <logdepthbuf_pars_vertex>',
  530. "void main() {",
  531. '#include <begin_vertex>',
  532. '#include <morphtarget_vertex>',
  533. '#include <project_vertex>',
  534. '#include <logdepthbuf_vertex>',
  535. "}"
  536. ].join( "\n" ),
  537. fragmentShader: [
  538. "uniform float mNear;",
  539. "uniform float mFar;",
  540. "uniform float opacity;",
  541. '#include <common>',
  542. '#include <logdepthbuf_pars_fragment>',
  543. "void main() {",
  544. '#include <logdepthbuf_fragment>',
  545. " #ifdef USE_LOGDEPTHBUF_EXT",
  546. " float depth = gl_FragDepthEXT / gl_FragCoord.w;",
  547. " #else",
  548. " float depth = gl_FragCoord.z / gl_FragCoord.w;",
  549. " #endif",
  550. " float color = 1.0 - smoothstep( mNear, mFar, depth );",
  551. " gl_FragColor = vec4( vec3( color ), opacity );",
  552. "}"
  553. ].join( "\n" )
  554. },
  555. 'normal': {
  556. uniforms: {
  557. "opacity" : { type: "f", value: 1.0 }
  558. },
  559. vertexShader: [
  560. "varying vec3 vNormal;",
  561. '#include <common>',
  562. '#include <morphtarget_pars_vertex>',
  563. '#include <logdepthbuf_pars_vertex>',
  564. "void main() {",
  565. " vNormal = normalize( normalMatrix * normal );",
  566. '#include <begin_vertex>',
  567. '#include <morphtarget_vertex>',
  568. '#include <project_vertex>',
  569. '#include <logdepthbuf_vertex>',
  570. "}"
  571. ].join( "\n" ),
  572. fragmentShader: [
  573. "uniform float opacity;",
  574. "varying vec3 vNormal;",
  575. '#include <common>',
  576. '#include <logdepthbuf_pars_fragment>',
  577. "void main() {",
  578. " gl_FragColor = vec4( 0.5 * normalize( vNormal ) + 0.5, opacity );",
  579. '#include <logdepthbuf_fragment>',
  580. "}"
  581. ].join( "\n" )
  582. },
  583. /* -------------------------------------------------------------------------
  584. // Cube map shader
  585. ------------------------------------------------------------------------- */
  586. 'cube': {
  587. uniforms: {
  588. "tCube": { type: "t", value: null },
  589. "tFlip": { type: "f", value: - 1 }
  590. },
  591. vertexShader: [
  592. "varying vec3 vWorldPosition;",
  593. '#include <common>',
  594. '#include <logdepthbuf_pars_vertex>',
  595. "void main() {",
  596. " vWorldPosition = transformDirection( position, modelMatrix );",
  597. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  598. '#include <logdepthbuf_vertex>',
  599. "}"
  600. ].join( "\n" ),
  601. fragmentShader: [
  602. "uniform samplerCube tCube;",
  603. "uniform float tFlip;",
  604. "varying vec3 vWorldPosition;",
  605. '#include <common>',
  606. '#include <logdepthbuf_pars_fragment>',
  607. "void main() {",
  608. " gl_FragColor = textureCube( tCube, vec3( tFlip * vWorldPosition.x, vWorldPosition.yz ) );",
  609. '#include <logdepthbuf_fragment>',
  610. "}"
  611. ].join( "\n" )
  612. },
  613. /* -------------------------------------------------------------------------
  614. // Cube map shader
  615. ------------------------------------------------------------------------- */
  616. 'equirect': {
  617. uniforms: {
  618. "tEquirect": { type: "t", value: null },
  619. "tFlip": { type: "f", value: - 1 }
  620. },
  621. vertexShader: [
  622. "varying vec3 vWorldPosition;",
  623. '#include <common>',
  624. '#include <logdepthbuf_pars_vertex>',
  625. "void main() {",
  626. " vWorldPosition = transformDirection( position, modelMatrix );",
  627. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  628. '#include <logdepthbuf_vertex>',
  629. "}"
  630. ].join( "\n" ),
  631. fragmentShader: [
  632. "uniform sampler2D tEquirect;",
  633. "uniform float tFlip;",
  634. "varying vec3 vWorldPosition;",
  635. '#include <common>',
  636. '#include <logdepthbuf_pars_fragment>',
  637. "void main() {",
  638. // " gl_FragColor = textureCube( tCube, vec3( tFlip * vWorldPosition.x, vWorldPosition.yz ) );",
  639. "vec3 direction = normalize( vWorldPosition );",
  640. "vec2 sampleUV;",
  641. "sampleUV.y = saturate( tFlip * direction.y * -0.5 + 0.5 );",
  642. "sampleUV.x = atan( direction.z, direction.x ) * RECIPROCAL_PI2 + 0.5;",
  643. "gl_FragColor = texture2D( tEquirect, sampleUV );",
  644. '#include <logdepthbuf_fragment>',
  645. "}"
  646. ].join( "\n" )
  647. },
  648. /* Depth encoding into RGBA texture
  649. *
  650. * based on SpiderGL shadow map example
  651. * http://spidergl.org/example.php?id=6
  652. *
  653. * originally from
  654. * http://www.gamedev.net/topic/442138-packing-a-float-into-a-a8r8g8b8-texture-shader/page__whichpage__1%25EF%25BF%25BD
  655. *
  656. * see also
  657. * http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
  658. */
  659. 'depthRGBA': {
  660. uniforms: {},
  661. vertexShader: [
  662. '#include <common>',
  663. '#include <morphtarget_pars_vertex>',
  664. '#include <skinning_pars_vertex>',
  665. '#include <logdepthbuf_pars_vertex>',
  666. "void main() {",
  667. '#include <skinbase_vertex>',
  668. '#include <begin_vertex>',
  669. '#include <morphtarget_vertex>',
  670. '#include <skinning_vertex>',
  671. '#include <project_vertex>',
  672. '#include <logdepthbuf_vertex>',
  673. "}"
  674. ].join( "\n" ),
  675. fragmentShader: [
  676. '#include <common>',
  677. '#include <logdepthbuf_pars_fragment>',
  678. "vec4 pack_depth( const in float depth ) {",
  679. " const vec4 bit_shift = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 );",
  680. " const vec4 bit_mask = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 );",
  681. " vec4 res = mod( depth * bit_shift * vec4( 255 ), vec4( 256 ) ) / vec4( 255 );",
  682. " res -= res.xxyz * bit_mask;",
  683. " return res;",
  684. "}",
  685. "void main() {",
  686. '#include <logdepthbuf_fragment>',
  687. " #ifdef USE_LOGDEPTHBUF_EXT",
  688. " gl_FragData[ 0 ] = pack_depth( gl_FragDepthEXT );",
  689. " #else",
  690. " gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z );",
  691. " #endif",
  692. //"gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z / gl_FragCoord.w );",
  693. //"float z = ( ( gl_FragCoord.z / gl_FragCoord.w ) - 3.0 ) / ( 4000.0 - 3.0 );",
  694. //"gl_FragData[ 0 ] = pack_depth( z );",
  695. //"gl_FragData[ 0 ] = vec4( z, z, z, 1.0 );",
  696. "}"
  697. ].join( "\n" )
  698. },
  699. 'distanceRGBA': {
  700. uniforms: {
  701. "lightPos": { type: "v3", value: new THREE.Vector3( 0, 0, 0 ) }
  702. },
  703. vertexShader: [
  704. "varying vec4 vWorldPosition;",
  705. '#include <common>',
  706. '#include <morphtarget_pars_vertex>',
  707. '#include <skinning_pars_vertex>',
  708. "void main() {",
  709. '#include <skinbase_vertex>',
  710. '#include <begin_vertex>',
  711. '#include <morphtarget_vertex>',
  712. '#include <skinning_vertex>',
  713. '#include <project_vertex>',
  714. '#include <worldpos_vertex>',
  715. "vWorldPosition = worldPosition;",
  716. "}"
  717. ].join( "\n" ),
  718. fragmentShader: [
  719. "uniform vec3 lightPos;",
  720. "varying vec4 vWorldPosition;",
  721. '#include <common>',
  722. "vec4 pack1K ( float depth ) {",
  723. " depth /= 1000.0;",
  724. " const vec4 bitSh = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 );",
  725. " const vec4 bitMsk = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 );",
  726. " vec4 res = mod( depth * bitSh * vec4( 255 ), vec4( 256 ) ) / vec4( 255 );",
  727. " res -= res.xxyz * bitMsk;",
  728. " return res; ",
  729. "}",
  730. "float unpack1K ( vec4 color ) {",
  731. " const vec4 bitSh = vec4( 1.0 / ( 256.0 * 256.0 * 256.0 ), 1.0 / ( 256.0 * 256.0 ), 1.0 / 256.0, 1.0 );",
  732. " return dot( color, bitSh ) * 1000.0;",
  733. "}",
  734. "void main () {",
  735. " gl_FragColor = pack1K( length( vWorldPosition.xyz - lightPos.xyz ) );",
  736. "}"
  737. ].join( "\n" )
  738. }
  739. };