SphereShaderLib.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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. *
  9. * @author croqueteer / http://www.wearality.com
  10. * SphereShaderLib.js is equivalent to ShaderLib.js except that the "default_vertex" is replaced with a true
  11. * spherical transform. This allows an undistorted single pass spherical field of view up to (but not including) 180 degrees.
  12. */
  13. THREE.ShaderChunk[ "sphere_vertex"] =
  14. ["vec4 mvPosition;",
  15. "#ifdef USE_SKINNING",
  16. " mvPosition = modelViewMatrix * skinned;",
  17. "#endif",
  18. "#if !defined( USE_SKINNING ) && defined( USE_MORPHTARGETS )",
  19. " mvPosition = modelViewMatrix * vec4( morphed, 1.0 );",
  20. "#endif",
  21. "#if !defined( USE_SKINNING ) && ! defined( USE_MORPHTARGETS )",
  22. " mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  23. "#endif ",
  24. " vec4 p = mvPosition; // put the vector into the camera frame",
  25. " float len = length(p.xyz); // use the length of the vector - not the z component",
  26. " if(p.z<0.0)len = -len; // the length sign should reflect the z sign",
  27. " float ctan = projectionMatrix[0][0]; // this is the cotangent of the x-component",
  28. " p.y *= projectionMatrix[1][1]/ctan; // set the y aspect ratio",
  29. " p.w = -len; // the w component is the negative of the z component",
  30. " p.z = len*projectionMatrix[2][2]+projectionMatrix[3][2]; // the z component is really the length of the vector",
  31. " p.xy *= sqrt((ctan*ctan)+1.0); // this is the camera field of view - the narrower the fov, the larger this multiplier is",
  32. " p.x += projectionMatrix[2][0]*len;",
  33. " p.y += projectionMatrix[2][1]*len;",
  34. " gl_Position = p; // done"
  35. ].join("\n");
  36. THREE.ShaderLib = {
  37. 'basic': {
  38. uniforms: THREE.UniformsUtils.merge( [
  39. THREE.UniformsLib[ "common" ],
  40. THREE.UniformsLib[ "fog" ],
  41. THREE.UniformsLib[ "shadowmap" ]
  42. ] ),
  43. vertexShader: [
  44. THREE.ShaderChunk[ "map_pars_vertex" ],
  45. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  46. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  47. THREE.ShaderChunk[ "color_pars_vertex" ],
  48. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  49. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  50. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  51. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ],
  52. "void main() {",
  53. THREE.ShaderChunk[ "map_vertex" ],
  54. THREE.ShaderChunk[ "lightmap_vertex" ],
  55. THREE.ShaderChunk[ "color_vertex" ],
  56. THREE.ShaderChunk[ "skinbase_vertex" ],
  57. " #ifdef USE_ENVMAP",
  58. THREE.ShaderChunk[ "morphnormal_vertex" ],
  59. THREE.ShaderChunk[ "skinnormal_vertex" ],
  60. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  61. " #endif",
  62. THREE.ShaderChunk[ "morphtarget_vertex" ],
  63. THREE.ShaderChunk[ "skinning_vertex" ],
  64. THREE.ShaderChunk[ "sphere_vertex" ],
  65. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  66. THREE.ShaderChunk[ "worldpos_vertex" ],
  67. THREE.ShaderChunk[ "envmap_vertex" ],
  68. THREE.ShaderChunk[ "shadowmap_vertex" ],
  69. "}"
  70. ].join("\n"),
  71. fragmentShader: [
  72. "uniform vec3 diffuse;",
  73. "uniform float opacity;",
  74. THREE.ShaderChunk[ "color_pars_fragment" ],
  75. THREE.ShaderChunk[ "map_pars_fragment" ],
  76. THREE.ShaderChunk[ "alphamap_pars_fragment" ],
  77. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  78. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  79. THREE.ShaderChunk[ "fog_pars_fragment" ],
  80. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  81. THREE.ShaderChunk[ "specularmap_pars_fragment" ],
  82. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  83. "void main() {",
  84. " gl_FragColor = vec4( diffuse, opacity );",
  85. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  86. THREE.ShaderChunk[ "map_fragment" ],
  87. THREE.ShaderChunk[ "alphamap_fragment" ],
  88. THREE.ShaderChunk[ "alphatest_fragment" ],
  89. THREE.ShaderChunk[ "specularmap_fragment" ],
  90. THREE.ShaderChunk[ "lightmap_fragment" ],
  91. THREE.ShaderChunk[ "color_fragment" ],
  92. THREE.ShaderChunk[ "envmap_fragment" ],
  93. THREE.ShaderChunk[ "shadowmap_fragment" ],
  94. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  95. THREE.ShaderChunk[ "fog_fragment" ],
  96. "}"
  97. ].join("\n")
  98. },
  99. 'lambert': {
  100. uniforms: THREE.UniformsUtils.merge( [
  101. THREE.UniformsLib[ "common" ],
  102. THREE.UniformsLib[ "fog" ],
  103. THREE.UniformsLib[ "lights" ],
  104. THREE.UniformsLib[ "shadowmap" ],
  105. {
  106. "ambient" : { type: "c", value: new THREE.Color( 0xffffff ) },
  107. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  108. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  109. }
  110. ] ),
  111. vertexShader: [
  112. "#define LAMBERT",
  113. "varying vec3 vLightFront;",
  114. "#ifdef DOUBLE_SIDED",
  115. " varying vec3 vLightBack;",
  116. "#endif",
  117. THREE.ShaderChunk[ "map_pars_vertex" ],
  118. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  119. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  120. THREE.ShaderChunk[ "lights_lambert_pars_vertex" ],
  121. THREE.ShaderChunk[ "color_pars_vertex" ],
  122. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  123. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  124. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  125. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ],
  126. "void main() {",
  127. THREE.ShaderChunk[ "map_vertex" ],
  128. THREE.ShaderChunk[ "lightmap_vertex" ],
  129. THREE.ShaderChunk[ "color_vertex" ],
  130. THREE.ShaderChunk[ "morphnormal_vertex" ],
  131. THREE.ShaderChunk[ "skinbase_vertex" ],
  132. THREE.ShaderChunk[ "skinnormal_vertex" ],
  133. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  134. THREE.ShaderChunk[ "morphtarget_vertex" ],
  135. THREE.ShaderChunk[ "skinning_vertex" ],
  136. THREE.ShaderChunk[ "sphere_vertex" ],
  137. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  138. THREE.ShaderChunk[ "worldpos_vertex" ],
  139. THREE.ShaderChunk[ "envmap_vertex" ],
  140. THREE.ShaderChunk[ "lights_lambert_vertex" ],
  141. THREE.ShaderChunk[ "shadowmap_vertex" ],
  142. "}"
  143. ].join("\n"),
  144. fragmentShader: [
  145. "uniform float opacity;",
  146. "varying vec3 vLightFront;",
  147. "#ifdef DOUBLE_SIDED",
  148. " varying vec3 vLightBack;",
  149. "#endif",
  150. THREE.ShaderChunk[ "color_pars_fragment" ],
  151. THREE.ShaderChunk[ "map_pars_fragment" ],
  152. THREE.ShaderChunk[ "alphamap_pars_fragment" ],
  153. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  154. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  155. THREE.ShaderChunk[ "fog_pars_fragment" ],
  156. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  157. THREE.ShaderChunk[ "specularmap_pars_fragment" ],
  158. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  159. "void main() {",
  160. " gl_FragColor = vec4( vec3( 1.0 ), opacity );",
  161. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  162. THREE.ShaderChunk[ "map_fragment" ],
  163. THREE.ShaderChunk[ "alphamap_fragment" ],
  164. THREE.ShaderChunk[ "alphatest_fragment" ],
  165. THREE.ShaderChunk[ "specularmap_fragment" ],
  166. " #ifdef DOUBLE_SIDED",
  167. //"float isFront = float( gl_FrontFacing );",
  168. //"gl_FragColor.xyz *= isFront * vLightFront + ( 1.0 - isFront ) * vLightBack;",
  169. " if ( gl_FrontFacing )",
  170. " gl_FragColor.xyz *= vLightFront;",
  171. " else",
  172. " gl_FragColor.xyz *= vLightBack;",
  173. " #else",
  174. " gl_FragColor.xyz *= vLightFront;",
  175. " #endif",
  176. THREE.ShaderChunk[ "lightmap_fragment" ],
  177. THREE.ShaderChunk[ "color_fragment" ],
  178. THREE.ShaderChunk[ "envmap_fragment" ],
  179. THREE.ShaderChunk[ "shadowmap_fragment" ],
  180. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  181. THREE.ShaderChunk[ "fog_fragment" ],
  182. "}"
  183. ].join("\n")
  184. },
  185. 'phong': {
  186. uniforms: THREE.UniformsUtils.merge( [
  187. THREE.UniformsLib[ "common" ],
  188. THREE.UniformsLib[ "bump" ],
  189. THREE.UniformsLib[ "normalmap" ],
  190. THREE.UniformsLib[ "fog" ],
  191. THREE.UniformsLib[ "lights" ],
  192. THREE.UniformsLib[ "shadowmap" ],
  193. {
  194. "ambient" : { type: "c", value: new THREE.Color( 0xffffff ) },
  195. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  196. "specular" : { type: "c", value: new THREE.Color( 0x111111 ) },
  197. "shininess": { type: "f", value: 30 },
  198. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  199. }
  200. ] ),
  201. vertexShader: [
  202. "#define PHONG",
  203. "varying vec3 vViewPosition;",
  204. "varying vec3 vNormal;",
  205. THREE.ShaderChunk[ "map_pars_vertex" ],
  206. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  207. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  208. THREE.ShaderChunk[ "lights_phong_pars_vertex" ],
  209. THREE.ShaderChunk[ "color_pars_vertex" ],
  210. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  211. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  212. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  213. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ],
  214. "void main() {",
  215. THREE.ShaderChunk[ "map_vertex" ],
  216. THREE.ShaderChunk[ "lightmap_vertex" ],
  217. THREE.ShaderChunk[ "color_vertex" ],
  218. THREE.ShaderChunk[ "morphnormal_vertex" ],
  219. THREE.ShaderChunk[ "skinbase_vertex" ],
  220. THREE.ShaderChunk[ "skinnormal_vertex" ],
  221. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  222. " vNormal = normalize( transformedNormal );",
  223. THREE.ShaderChunk[ "morphtarget_vertex" ],
  224. THREE.ShaderChunk[ "skinning_vertex" ],
  225. THREE.ShaderChunk[ "sphere_vertex" ],
  226. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  227. " vViewPosition = -mvPosition.xyz;",
  228. THREE.ShaderChunk[ "worldpos_vertex" ],
  229. THREE.ShaderChunk[ "envmap_vertex" ],
  230. THREE.ShaderChunk[ "lights_phong_vertex" ],
  231. THREE.ShaderChunk[ "shadowmap_vertex" ],
  232. "}"
  233. ].join("\n"),
  234. fragmentShader: [
  235. "uniform vec3 diffuse;",
  236. "uniform float opacity;",
  237. "uniform vec3 ambient;",
  238. "uniform vec3 emissive;",
  239. "uniform vec3 specular;",
  240. "uniform float shininess;",
  241. THREE.ShaderChunk[ "color_pars_fragment" ],
  242. THREE.ShaderChunk[ "map_pars_fragment" ],
  243. THREE.ShaderChunk[ "alphamap_pars_fragment" ],
  244. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  245. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  246. THREE.ShaderChunk[ "fog_pars_fragment" ],
  247. THREE.ShaderChunk[ "lights_phong_pars_fragment" ],
  248. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  249. THREE.ShaderChunk[ "bumpmap_pars_fragment" ],
  250. THREE.ShaderChunk[ "normalmap_pars_fragment" ],
  251. THREE.ShaderChunk[ "specularmap_pars_fragment" ],
  252. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  253. "void main() {",
  254. " gl_FragColor = vec4( vec3( 1.0 ), opacity );",
  255. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  256. THREE.ShaderChunk[ "map_fragment" ],
  257. THREE.ShaderChunk[ "alphamap_fragment" ],
  258. THREE.ShaderChunk[ "alphatest_fragment" ],
  259. THREE.ShaderChunk[ "specularmap_fragment" ],
  260. THREE.ShaderChunk[ "lights_phong_fragment" ],
  261. THREE.ShaderChunk[ "lightmap_fragment" ],
  262. THREE.ShaderChunk[ "color_fragment" ],
  263. THREE.ShaderChunk[ "envmap_fragment" ],
  264. THREE.ShaderChunk[ "shadowmap_fragment" ],
  265. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  266. THREE.ShaderChunk[ "fog_fragment" ],
  267. "}"
  268. ].join("\n")
  269. },
  270. 'particle_basic': {
  271. uniforms: THREE.UniformsUtils.merge( [
  272. THREE.UniformsLib[ "particle" ],
  273. THREE.UniformsLib[ "shadowmap" ]
  274. ] ),
  275. vertexShader: [
  276. "uniform float size;",
  277. "uniform float scale;",
  278. THREE.ShaderChunk[ "color_pars_vertex" ],
  279. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  280. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ],
  281. "void main() {",
  282. THREE.ShaderChunk[ "color_vertex" ],
  283. " vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  284. " #ifdef USE_SIZEATTENUATION",
  285. " gl_PointSize = size * ( scale / length( mvPosition.xyz ) );",
  286. " #else",
  287. " gl_PointSize = size;",
  288. " #endif",
  289. " gl_Position = projectionMatrix * mvPosition;",
  290. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  291. THREE.ShaderChunk[ "worldpos_vertex" ],
  292. THREE.ShaderChunk[ "shadowmap_vertex" ],
  293. "}"
  294. ].join("\n"),
  295. fragmentShader: [
  296. "uniform vec3 psColor;",
  297. "uniform float opacity;",
  298. THREE.ShaderChunk[ "color_pars_fragment" ],
  299. THREE.ShaderChunk[ "map_particle_pars_fragment" ],
  300. THREE.ShaderChunk[ "fog_pars_fragment" ],
  301. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  302. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  303. "void main() {",
  304. " gl_FragColor = vec4( psColor, opacity );",
  305. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  306. THREE.ShaderChunk[ "map_particle_fragment" ],
  307. THREE.ShaderChunk[ "alphatest_fragment" ],
  308. THREE.ShaderChunk[ "color_fragment" ],
  309. THREE.ShaderChunk[ "shadowmap_fragment" ],
  310. THREE.ShaderChunk[ "fog_fragment" ],
  311. "}"
  312. ].join("\n")
  313. },
  314. 'dashed': {
  315. uniforms: THREE.UniformsUtils.merge( [
  316. THREE.UniformsLib[ "common" ],
  317. THREE.UniformsLib[ "fog" ],
  318. {
  319. "scale" : { type: "f", value: 1 },
  320. "dashSize" : { type: "f", value: 1 },
  321. "totalSize": { type: "f", value: 2 }
  322. }
  323. ] ),
  324. vertexShader: [
  325. "uniform float scale;",
  326. "attribute float lineDistance;",
  327. "varying float vLineDistance;",
  328. THREE.ShaderChunk[ "color_pars_vertex" ],
  329. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ],
  330. "void main() {",
  331. THREE.ShaderChunk[ "color_vertex" ],
  332. " vLineDistance = scale * lineDistance;",
  333. " vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  334. " gl_Position = projectionMatrix * mvPosition;",
  335. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  336. "}"
  337. ].join("\n"),
  338. fragmentShader: [
  339. "uniform vec3 diffuse;",
  340. "uniform float opacity;",
  341. "uniform float dashSize;",
  342. "uniform float totalSize;",
  343. "varying float vLineDistance;",
  344. THREE.ShaderChunk[ "color_pars_fragment" ],
  345. THREE.ShaderChunk[ "fog_pars_fragment" ],
  346. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  347. "void main() {",
  348. " if ( mod( vLineDistance, totalSize ) > dashSize ) {",
  349. " discard;",
  350. " }",
  351. " gl_FragColor = vec4( diffuse, opacity );",
  352. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  353. THREE.ShaderChunk[ "color_fragment" ],
  354. THREE.ShaderChunk[ "fog_fragment" ],
  355. "}"
  356. ].join("\n")
  357. },
  358. 'depth': {
  359. uniforms: {
  360. "mNear": { type: "f", value: 1.0 },
  361. "mFar" : { type: "f", value: 2000.0 },
  362. "opacity" : { type: "f", value: 1.0 }
  363. },
  364. vertexShader: [
  365. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  366. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ],
  367. "void main() {",
  368. THREE.ShaderChunk[ "morphtarget_vertex" ],
  369. THREE.ShaderChunk[ "sphere_vertex" ],
  370. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  371. "}"
  372. ].join("\n"),
  373. fragmentShader: [
  374. "uniform float mNear;",
  375. "uniform float mFar;",
  376. "uniform float opacity;",
  377. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  378. "void main() {",
  379. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  380. " #ifdef USE_LOGDEPTHBUF_EXT",
  381. " float depth = gl_FragDepthEXT / gl_FragCoord.w;",
  382. " #else",
  383. " float depth = gl_FragCoord.z / gl_FragCoord.w;",
  384. " #endif",
  385. " float color = 1.0 - smoothstep( mNear, mFar, depth );",
  386. " gl_FragColor = vec4( vec3( color ), opacity );",
  387. "}"
  388. ].join("\n")
  389. },
  390. 'normal': {
  391. uniforms: {
  392. "opacity" : { type: "f", value: 1.0 }
  393. },
  394. vertexShader: [
  395. "varying vec3 vNormal;",
  396. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  397. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ],
  398. "void main() {",
  399. " vNormal = normalize( normalMatrix * normal );",
  400. THREE.ShaderChunk[ "morphtarget_vertex" ],
  401. THREE.ShaderChunk[ "sphere_vertex" ],
  402. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  403. "}"
  404. ].join("\n"),
  405. fragmentShader: [
  406. "uniform float opacity;",
  407. "varying vec3 vNormal;",
  408. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  409. "void main() {",
  410. " gl_FragColor = vec4( 0.5 * normalize( vNormal ) + 0.5, opacity );",
  411. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  412. "}"
  413. ].join("\n")
  414. },
  415. /* -------------------------------------------------------------------------
  416. // Normal map shader
  417. // - Blinn-Phong
  418. // - normal + diffuse + specular + AO + displacement + reflection + shadow maps
  419. // - point and directional lights (use with "lights: true" material option)
  420. ------------------------------------------------------------------------- */
  421. 'normalmap' : {
  422. uniforms: THREE.UniformsUtils.merge( [
  423. THREE.UniformsLib[ "fog" ],
  424. THREE.UniformsLib[ "lights" ],
  425. THREE.UniformsLib[ "shadowmap" ],
  426. {
  427. "enableAO" : { type: "i", value: 0 },
  428. "enableDiffuse" : { type: "i", value: 0 },
  429. "enableSpecular" : { type: "i", value: 0 },
  430. "enableReflection" : { type: "i", value: 0 },
  431. "enableDisplacement": { type: "i", value: 0 },
  432. "tDisplacement": { type: "t", value: null }, // must go first as this is vertex texture
  433. "tDiffuse" : { type: "t", value: null },
  434. "tCube" : { type: "t", value: null },
  435. "tNormal" : { type: "t", value: null },
  436. "tSpecular" : { type: "t", value: null },
  437. "tAO" : { type: "t", value: null },
  438. "uNormalScale": { type: "v2", value: new THREE.Vector2( 1, 1 ) },
  439. "uDisplacementBias": { type: "f", value: 0.0 },
  440. "uDisplacementScale": { type: "f", value: 1.0 },
  441. "diffuse": { type: "c", value: new THREE.Color( 0xffffff ) },
  442. "specular": { type: "c", value: new THREE.Color( 0x111111 ) },
  443. "ambient": { type: "c", value: new THREE.Color( 0xffffff ) },
  444. "shininess": { type: "f", value: 30 },
  445. "opacity": { type: "f", value: 1 },
  446. "useRefract": { type: "i", value: 0 },
  447. "refractionRatio": { type: "f", value: 0.98 },
  448. "reflectivity": { type: "f", value: 0.5 },
  449. "uOffset" : { type: "v2", value: new THREE.Vector2( 0, 0 ) },
  450. "uRepeat" : { type: "v2", value: new THREE.Vector2( 1, 1 ) },
  451. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  452. }
  453. ] ),
  454. fragmentShader: [
  455. "uniform vec3 ambient;",
  456. "uniform vec3 diffuse;",
  457. "uniform vec3 specular;",
  458. "uniform float shininess;",
  459. "uniform float opacity;",
  460. "uniform bool enableDiffuse;",
  461. "uniform bool enableSpecular;",
  462. "uniform bool enableAO;",
  463. "uniform bool enableReflection;",
  464. "uniform sampler2D tDiffuse;",
  465. "uniform sampler2D tNormal;",
  466. "uniform sampler2D tSpecular;",
  467. "uniform sampler2D tAO;",
  468. "uniform samplerCube tCube;",
  469. "uniform vec2 uNormalScale;",
  470. "uniform bool useRefract;",
  471. "uniform float refractionRatio;",
  472. "uniform float reflectivity;",
  473. "varying vec3 vTangent;",
  474. "varying vec3 vBinormal;",
  475. "varying vec3 vNormal;",
  476. "varying vec2 vUv;",
  477. "uniform vec3 ambientLightColor;",
  478. "#if MAX_DIR_LIGHTS > 0",
  479. " uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  480. " uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  481. "#endif",
  482. "#if MAX_HEMI_LIGHTS > 0",
  483. " uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];",
  484. " uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];",
  485. " uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];",
  486. "#endif",
  487. "#if MAX_POINT_LIGHTS > 0",
  488. " uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  489. " uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  490. " uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  491. "#endif",
  492. "#if MAX_SPOT_LIGHTS > 0",
  493. " uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];",
  494. " uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];",
  495. " uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];",
  496. " uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];",
  497. " uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];",
  498. " uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];",
  499. "#endif",
  500. "#ifdef WRAP_AROUND",
  501. " uniform vec3 wrapRGB;",
  502. "#endif",
  503. "varying vec3 vWorldPosition;",
  504. "varying vec3 vViewPosition;",
  505. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  506. THREE.ShaderChunk[ "fog_pars_fragment" ],
  507. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  508. "void main() {",
  509. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  510. " gl_FragColor = vec4( vec3( 1.0 ), opacity );",
  511. " vec3 specularTex = vec3( 1.0 );",
  512. " vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
  513. " normalTex.xy *= uNormalScale;",
  514. " normalTex = normalize( normalTex );",
  515. " if( enableDiffuse ) {",
  516. " #ifdef GAMMA_INPUT",
  517. " vec4 texelColor = texture2D( tDiffuse, vUv );",
  518. " texelColor.xyz *= texelColor.xyz;",
  519. " gl_FragColor = gl_FragColor * texelColor;",
  520. " #else",
  521. " gl_FragColor = gl_FragColor * texture2D( tDiffuse, vUv );",
  522. " #endif",
  523. " }",
  524. " if( enableAO ) {",
  525. " #ifdef GAMMA_INPUT",
  526. " vec4 aoColor = texture2D( tAO, vUv );",
  527. " aoColor.xyz *= aoColor.xyz;",
  528. " gl_FragColor.xyz = gl_FragColor.xyz * aoColor.xyz;",
  529. " #else",
  530. " gl_FragColor.xyz = gl_FragColor.xyz * texture2D( tAO, vUv ).xyz;",
  531. " #endif",
  532. " }",
  533. THREE.ShaderChunk[ "alphatest_fragment" ],
  534. " if( enableSpecular )",
  535. " specularTex = texture2D( tSpecular, vUv ).xyz;",
  536. " mat3 tsb = mat3( normalize( vTangent ), normalize( vBinormal ), normalize( vNormal ) );",
  537. " vec3 finalNormal = tsb * normalTex;",
  538. " #ifdef FLIP_SIDED",
  539. " finalNormal = -finalNormal;",
  540. " #endif",
  541. " vec3 normal = normalize( finalNormal );",
  542. " vec3 viewPosition = normalize( vViewPosition );",
  543. // point lights
  544. " #if MAX_POINT_LIGHTS > 0",
  545. " vec3 pointDiffuse = vec3( 0.0 );",
  546. " vec3 pointSpecular = vec3( 0.0 );",
  547. " for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  548. " vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  549. " vec3 pointVector = lPosition.xyz + vViewPosition.xyz;",
  550. " float pointDistance = 1.0;",
  551. " if ( pointLightDistance[ i ] > 0.0 )",
  552. " pointDistance = 1.0 - min( ( length( pointVector ) / pointLightDistance[ i ] ), 1.0 );",
  553. " pointVector = normalize( pointVector );",
  554. // diffuse
  555. " #ifdef WRAP_AROUND",
  556. " float pointDiffuseWeightFull = max( dot( normal, pointVector ), 0.0 );",
  557. " float pointDiffuseWeightHalf = max( 0.5 * dot( normal, pointVector ) + 0.5, 0.0 );",
  558. " vec3 pointDiffuseWeight = mix( vec3( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );",
  559. " #else",
  560. " float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
  561. " #endif",
  562. " pointDiffuse += pointDistance * pointLightColor[ i ] * diffuse * pointDiffuseWeight;",
  563. // specular
  564. " vec3 pointHalfVector = normalize( pointVector + viewPosition );",
  565. " float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
  566. " float pointSpecularWeight = specularTex.r * max( pow( pointDotNormalHalf, shininess ), 0.0 );",
  567. " float specularNormalization = ( shininess + 2.0 ) / 8.0;",
  568. " vec3 schlick = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( pointVector, pointHalfVector ), 0.0 ), 5.0 );",
  569. " pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * pointDistance * specularNormalization;",
  570. " }",
  571. " #endif",
  572. // spot lights
  573. " #if MAX_SPOT_LIGHTS > 0",
  574. " vec3 spotDiffuse = vec3( 0.0 );",
  575. " vec3 spotSpecular = vec3( 0.0 );",
  576. " for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {",
  577. " vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );",
  578. " vec3 spotVector = lPosition.xyz + vViewPosition.xyz;",
  579. " float spotDistance = 1.0;",
  580. " if ( spotLightDistance[ i ] > 0.0 )",
  581. " spotDistance = 1.0 - min( ( length( spotVector ) / spotLightDistance[ i ] ), 1.0 );",
  582. " spotVector = normalize( spotVector );",
  583. " float spotEffect = dot( spotLightDirection[ i ], normalize( spotLightPosition[ i ] - vWorldPosition ) );",
  584. " if ( spotEffect > spotLightAngleCos[ i ] ) {",
  585. " spotEffect = max( pow( max( spotEffect, 0.0 ), spotLightExponent[ i ] ), 0.0 );",
  586. // diffuse
  587. " #ifdef WRAP_AROUND",
  588. " float spotDiffuseWeightFull = max( dot( normal, spotVector ), 0.0 );",
  589. " float spotDiffuseWeightHalf = max( 0.5 * dot( normal, spotVector ) + 0.5, 0.0 );",
  590. " vec3 spotDiffuseWeight = mix( vec3( spotDiffuseWeightFull ), vec3( spotDiffuseWeightHalf ), wrapRGB );",
  591. " #else",
  592. " float spotDiffuseWeight = max( dot( normal, spotVector ), 0.0 );",
  593. " #endif",
  594. " spotDiffuse += spotDistance * spotLightColor[ i ] * diffuse * spotDiffuseWeight * spotEffect;",
  595. // specular
  596. " vec3 spotHalfVector = normalize( spotVector + viewPosition );",
  597. " float spotDotNormalHalf = max( dot( normal, spotHalfVector ), 0.0 );",
  598. " float spotSpecularWeight = specularTex.r * max( pow( spotDotNormalHalf, shininess ), 0.0 );",
  599. " float specularNormalization = ( shininess + 2.0 ) / 8.0;",
  600. " vec3 schlick = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( spotVector, spotHalfVector ), 0.0 ), 5.0 );",
  601. " spotSpecular += schlick * spotLightColor[ i ] * spotSpecularWeight * spotDiffuseWeight * spotDistance * specularNormalization * spotEffect;",
  602. " }",
  603. " }",
  604. " #endif",
  605. // directional lights
  606. " #if MAX_DIR_LIGHTS > 0",
  607. " vec3 dirDiffuse = vec3( 0.0 );",
  608. " vec3 dirSpecular = vec3( 0.0 );",
  609. " for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
  610. " vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  611. " vec3 dirVector = normalize( lDirection.xyz );",
  612. // diffuse
  613. " #ifdef WRAP_AROUND",
  614. " float directionalLightWeightingFull = max( dot( normal, dirVector ), 0.0 );",
  615. " float directionalLightWeightingHalf = max( 0.5 * dot( normal, dirVector ) + 0.5, 0.0 );",
  616. " vec3 dirDiffuseWeight = mix( vec3( directionalLightWeightingFull ), vec3( directionalLightWeightingHalf ), wrapRGB );",
  617. " #else",
  618. " float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  619. " #endif",
  620. " dirDiffuse += directionalLightColor[ i ] * diffuse * dirDiffuseWeight;",
  621. // specular
  622. " vec3 dirHalfVector = normalize( dirVector + viewPosition );",
  623. " float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
  624. " float dirSpecularWeight = specularTex.r * max( pow( dirDotNormalHalf, shininess ), 0.0 );",
  625. " float specularNormalization = ( shininess + 2.0 ) / 8.0;",
  626. " vec3 schlick = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( dirVector, dirHalfVector ), 0.0 ), 5.0 );",
  627. " dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;",
  628. " }",
  629. " #endif",
  630. // hemisphere lights
  631. " #if MAX_HEMI_LIGHTS > 0",
  632. " vec3 hemiDiffuse = vec3( 0.0 );",
  633. " vec3 hemiSpecular = vec3( 0.0 );" ,
  634. " for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {",
  635. " vec4 lDirection = viewMatrix * vec4( hemisphereLightDirection[ i ], 0.0 );",
  636. " vec3 lVector = normalize( lDirection.xyz );",
  637. // diffuse
  638. " float dotProduct = dot( normal, lVector );",
  639. " float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
  640. " vec3 hemiColor = mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
  641. " hemiDiffuse += diffuse * hemiColor;",
  642. // specular (sky light)
  643. " vec3 hemiHalfVectorSky = normalize( lVector + viewPosition );",
  644. " float hemiDotNormalHalfSky = 0.5 * dot( normal, hemiHalfVectorSky ) + 0.5;",
  645. " float hemiSpecularWeightSky = specularTex.r * max( pow( max( hemiDotNormalHalfSky, 0.0 ), shininess ), 0.0 );",
  646. // specular (ground light)
  647. " vec3 lVectorGround = -lVector;",
  648. " vec3 hemiHalfVectorGround = normalize( lVectorGround + viewPosition );",
  649. " float hemiDotNormalHalfGround = 0.5 * dot( normal, hemiHalfVectorGround ) + 0.5;",
  650. " float hemiSpecularWeightGround = specularTex.r * max( pow( max( hemiDotNormalHalfGround, 0.0 ), shininess ), 0.0 );",
  651. " float dotProductGround = dot( normal, lVectorGround );",
  652. " float specularNormalization = ( shininess + 2.0 ) / 8.0;",
  653. " vec3 schlickSky = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( lVector, hemiHalfVectorSky ), 0.0 ), 5.0 );",
  654. " vec3 schlickGround = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( lVectorGround, hemiHalfVectorGround ), 0.0 ), 5.0 );",
  655. " hemiSpecular += hemiColor * specularNormalization * ( schlickSky * hemiSpecularWeightSky * max( dotProduct, 0.0 ) + schlickGround * hemiSpecularWeightGround * max( dotProductGround, 0.0 ) );",
  656. " }",
  657. " #endif",
  658. // all lights contribution summation
  659. " vec3 totalDiffuse = vec3( 0.0 );",
  660. " vec3 totalSpecular = vec3( 0.0 );",
  661. " #if MAX_DIR_LIGHTS > 0",
  662. " totalDiffuse += dirDiffuse;",
  663. " totalSpecular += dirSpecular;",
  664. " #endif",
  665. " #if MAX_HEMI_LIGHTS > 0",
  666. " totalDiffuse += hemiDiffuse;",
  667. " totalSpecular += hemiSpecular;",
  668. " #endif",
  669. " #if MAX_POINT_LIGHTS > 0",
  670. " totalDiffuse += pointDiffuse;",
  671. " totalSpecular += pointSpecular;",
  672. " #endif",
  673. " #if MAX_SPOT_LIGHTS > 0",
  674. " totalDiffuse += spotDiffuse;",
  675. " totalSpecular += spotSpecular;",
  676. " #endif",
  677. " #ifdef METAL",
  678. " gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * ambient + totalSpecular );",
  679. " #else",
  680. " gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * ambient ) + totalSpecular;",
  681. " #endif",
  682. " if ( enableReflection ) {",
  683. " vec3 vReflect;",
  684. " vec3 cameraToVertex = normalize( vWorldPosition - cameraPosition );",
  685. " if ( useRefract ) {",
  686. " vReflect = refract( cameraToVertex, normal, refractionRatio );",
  687. " } else {",
  688. " vReflect = reflect( cameraToVertex, normal );",
  689. " }",
  690. " vec4 cubeColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
  691. " #ifdef GAMMA_INPUT",
  692. " cubeColor.xyz *= cubeColor.xyz;",
  693. " #endif",
  694. " gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, specularTex.r * reflectivity );",
  695. " }",
  696. THREE.ShaderChunk[ "shadowmap_fragment" ],
  697. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  698. THREE.ShaderChunk[ "fog_fragment" ],
  699. "}"
  700. ].join("\n"),
  701. vertexShader: [
  702. "attribute vec4 tangent;",
  703. "uniform vec2 uOffset;",
  704. "uniform vec2 uRepeat;",
  705. "uniform bool enableDisplacement;",
  706. "#ifdef VERTEX_TEXTURES",
  707. " uniform sampler2D tDisplacement;",
  708. " uniform float uDisplacementScale;",
  709. " uniform float uDisplacementBias;",
  710. "#endif",
  711. "varying vec3 vTangent;",
  712. "varying vec3 vBinormal;",
  713. "varying vec3 vNormal;",
  714. "varying vec2 vUv;",
  715. "varying vec3 vWorldPosition;",
  716. "varying vec3 vViewPosition;",
  717. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  718. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  719. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ],
  720. "void main() {",
  721. THREE.ShaderChunk[ "skinbase_vertex" ],
  722. THREE.ShaderChunk[ "skinnormal_vertex" ],
  723. // normal, tangent and binormal vectors
  724. " #ifdef USE_SKINNING",
  725. " vNormal = normalize( normalMatrix * skinnedNormal.xyz );",
  726. " vec4 skinnedTangent = skinMatrix * vec4( tangent.xyz, 0.0 );",
  727. " vTangent = normalize( normalMatrix * skinnedTangent.xyz );",
  728. " #else",
  729. " vNormal = normalize( normalMatrix * normal );",
  730. " vTangent = normalize( normalMatrix * tangent.xyz );",
  731. " #endif",
  732. " vBinormal = normalize( cross( vNormal, vTangent ) * tangent.w );",
  733. " vUv = uv * uRepeat + uOffset;",
  734. // displacement mapping
  735. " vec3 displacedPosition;",
  736. " #ifdef VERTEX_TEXTURES",
  737. " if ( enableDisplacement ) {",
  738. " vec3 dv = texture2D( tDisplacement, uv ).xyz;",
  739. " float df = uDisplacementScale * dv.x + uDisplacementBias;",
  740. " displacedPosition = position + normalize( normal ) * df;",
  741. " } else {",
  742. " #ifdef USE_SKINNING",
  743. " vec4 skinVertex = bindMatrix * vec4( position, 1.0 );",
  744. " vec4 skinned = vec4( 0.0 );",
  745. " skinned += boneMatX * skinVertex * skinWeight.x;",
  746. " skinned += boneMatY * skinVertex * skinWeight.y;",
  747. " skinned += boneMatZ * skinVertex * skinWeight.z;",
  748. " skinned += boneMatW * skinVertex * skinWeight.w;",
  749. " skinned = bindMatrixInverse * skinned;",
  750. " displacedPosition = skinned.xyz;",
  751. " #else",
  752. " displacedPosition = position;",
  753. " #endif",
  754. " }",
  755. " #else",
  756. " #ifdef USE_SKINNING",
  757. " vec4 skinVertex = bindMatrix * vec4( position, 1.0 );",
  758. " vec4 skinned = vec4( 0.0 );",
  759. " skinned += boneMatX * skinVertex * skinWeight.x;",
  760. " skinned += boneMatY * skinVertex * skinWeight.y;",
  761. " skinned += boneMatZ * skinVertex * skinWeight.z;",
  762. " skinned += boneMatW * skinVertex * skinWeight.w;",
  763. " skinned = bindMatrixInverse * skinned;",
  764. " displacedPosition = skinned.xyz;",
  765. " #else",
  766. " displacedPosition = position;",
  767. " #endif",
  768. " #endif",
  769. //
  770. " vec4 mvPosition = modelViewMatrix * vec4( displacedPosition, 1.0 );",
  771. " vec4 worldPosition = modelMatrix * vec4( displacedPosition, 1.0 );",
  772. " gl_Position = projectionMatrix * mvPosition;",
  773. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  774. //
  775. " vWorldPosition = worldPosition.xyz;",
  776. " vViewPosition = -mvPosition.xyz;",
  777. // shadows
  778. " #ifdef USE_SHADOWMAP",
  779. " for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  780. " vShadowCoord[ i ] = shadowMatrix[ i ] * worldPosition;",
  781. " }",
  782. " #endif",
  783. "}"
  784. ].join("\n")
  785. },
  786. /* -------------------------------------------------------------------------
  787. // Cube map shader
  788. ------------------------------------------------------------------------- */
  789. 'cube': {
  790. uniforms: { "tCube": { type: "t", value: null },
  791. "tFlip": { type: "f", value: - 1 } },
  792. vertexShader: [
  793. "varying vec3 vWorldPosition;",
  794. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ],
  795. "void main() {",
  796. " vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  797. " vWorldPosition = worldPosition.xyz;",
  798. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  799. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  800. "}"
  801. ].join("\n"),
  802. fragmentShader: [
  803. "uniform samplerCube tCube;",
  804. "uniform float tFlip;",
  805. "varying vec3 vWorldPosition;",
  806. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  807. "void main() {",
  808. " gl_FragColor = textureCube( tCube, vec3( tFlip * vWorldPosition.x, vWorldPosition.yz ) );",
  809. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  810. "}"
  811. ].join("\n")
  812. },
  813. /* Depth encoding into RGBA texture
  814. *
  815. * based on SpiderGL shadow map example
  816. * http://spidergl.org/example.php?id=6
  817. *
  818. * originally from
  819. * http://www.gamedev.net/topic/442138-packing-a-float-into-a-a8r8g8b8-texture-shader/page__whichpage__1%25EF%25BF%25BD
  820. *
  821. * see also
  822. * http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
  823. */
  824. 'depthRGBA': {
  825. uniforms: {},
  826. vertexShader: [
  827. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  828. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  829. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ],
  830. "void main() {",
  831. THREE.ShaderChunk[ "skinbase_vertex" ],
  832. THREE.ShaderChunk[ "morphtarget_vertex" ],
  833. THREE.ShaderChunk[ "skinning_vertex" ],
  834. THREE.ShaderChunk[ "sphere_vertex" ],
  835. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  836. "}"
  837. ].join("\n"),
  838. fragmentShader: [
  839. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  840. "vec4 pack_depth( const in float depth ) {",
  841. " const vec4 bit_shift = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 );",
  842. " const vec4 bit_mask = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 );",
  843. " vec4 res = mod( depth * bit_shift * vec4( 255 ), vec4( 256 ) ) / vec4( 255 );", // " vec4 res = fract( depth * bit_shift );",
  844. " res -= res.xxyz * bit_mask;",
  845. " return res;",
  846. "}",
  847. "void main() {",
  848. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  849. " #ifdef USE_LOGDEPTHBUF_EXT",
  850. " gl_FragData[ 0 ] = pack_depth( gl_FragDepthEXT );",
  851. " #else",
  852. " gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z );",
  853. " #endif",
  854. //"gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z / gl_FragCoord.w );",
  855. //"float z = ( ( gl_FragCoord.z / gl_FragCoord.w ) - 3.0 ) / ( 4000.0 - 3.0 );",
  856. //"gl_FragData[ 0 ] = pack_depth( z );",
  857. //"gl_FragData[ 0 ] = vec4( z, z, z, 1.0 );",
  858. "}"
  859. ].join("\n")
  860. }
  861. };