shaders.js 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883
  1. let Shaders = {};
  2. Shaders["pointcloud.vs"] = `
  3. precision highp float;
  4. precision highp int;
  5. #define max_clip_polygons 8
  6. #define PI 3.141592653589793
  7. #if defined(usePanoMap)
  8. uniform samplerCube pano0Map; //随便设置一个samplerCube去使用都会让点云消失
  9. uniform samplerCube pano1Map;
  10. uniform float progress;
  11. uniform float easeInOutRatio;
  12. uniform vec3 pano0Position;
  13. uniform mat4 pano0Matrix;
  14. uniform vec3 pano1Position;
  15. uniform mat4 pano1Matrix;
  16. /*
  17. varying vec3 vWorldPosition0;
  18. varying vec3 vWorldPosition1;
  19. */
  20. #endif
  21. //--------------
  22. attribute vec3 position;
  23. attribute vec3 color;
  24. attribute float intensity;
  25. attribute float classification;
  26. attribute float returnNumber;
  27. attribute float numberOfReturns;
  28. attribute float pointSourceID;
  29. attribute vec4 indices;
  30. attribute float spacing;
  31. attribute float gpsTime;
  32. attribute vec3 normal;
  33. attribute float aExtra;
  34. uniform mat4 modelMatrix;
  35. uniform mat4 modelViewMatrix;
  36. uniform mat4 projectionMatrix;
  37. uniform mat4 viewMatrix;
  38. uniform mat4 uViewInv;
  39. uniform float uScreenWidth;
  40. uniform float uScreenHeight;
  41. uniform float fov;
  42. uniform float near;
  43. uniform float far;
  44. uniform bool uDebug;
  45. uniform bool uUseOrthographicCamera;
  46. uniform float uOrthoWidth;
  47. uniform float uOrthoHeight;
  48. #define CLIPTASK_NONE 0
  49. #define CLIPTASK_HIGHLIGHT 1
  50. #define CLIPTASK_SHOW_INSIDE 2
  51. #define CLIPTASK_SHOW_OUTSIDE 3
  52. #define CLIPMETHOD_INSIDE_ANY 0
  53. #define CLIPMETHOD_INSIDE_ALL 1
  54. uniform int clipTask;
  55. uniform int clipMethod;
  56. #if defined(num_clipboxes) && num_clipboxes > 0
  57. uniform mat4 clipBoxes[num_clipboxes];
  58. #endif
  59. #if defined(num_clipspheres) && num_clipspheres > 0
  60. uniform mat4 uClipSpheres[num_clipspheres];
  61. #endif
  62. #if defined(num_clippolygons) && num_clippolygons > 0
  63. uniform int uClipPolygonVCount[num_clippolygons];
  64. uniform vec3 uClipPolygonVertices[num_clippolygons * 8];
  65. uniform mat4 uClipPolygonWVP[num_clippolygons];
  66. #endif
  67. uniform float size;
  68. uniform float minSize;
  69. uniform float maxSize;
  70. uniform float uPCIndex;
  71. uniform float uOctreeSpacing;
  72. uniform float uNodeSpacing;
  73. uniform float uOctreeSize;
  74. uniform vec3 uBBSize;
  75. uniform float uLevel;
  76. uniform float uVNStart;
  77. uniform bool uIsLeafNode;
  78. uniform vec3 uColor;
  79. uniform float uOpacity;
  80. varying float vOpacity; //add
  81. uniform vec2 elevationRange;
  82. uniform vec2 intensityRange;
  83. uniform vec2 uFilterReturnNumberRange;
  84. uniform vec2 uFilterNumberOfReturnsRange;
  85. uniform vec2 uFilterPointSourceIDClipRange;
  86. uniform vec2 uFilterGPSTimeClipRange;
  87. //uniform float ufilterByNormalThreshold;
  88. uniform float uGpsScale;
  89. uniform float uGpsOffset;
  90. uniform vec2 uNormalizedGpsBufferRange;
  91. uniform vec3 uIntensity_gbc;
  92. uniform vec3 uRGB_gbc;
  93. uniform vec3 uExtra_gbc;
  94. uniform float uTransition;
  95. uniform float wRGB;
  96. uniform float wIntensity;
  97. uniform float wElevation;
  98. uniform float wClassification;
  99. uniform float wReturnNumber;
  100. uniform float wSourceID;
  101. uniform vec2 uExtraNormalizedRange;
  102. uniform vec2 uExtraRange;
  103. uniform float uExtraScale;
  104. uniform float uExtraOffset;
  105. uniform vec3 uShadowColor;
  106. uniform sampler2D visibleNodes;
  107. uniform sampler2D gradient;
  108. uniform sampler2D classificationLUT;
  109. #if defined(color_type_matcap)
  110. uniform sampler2D matcapTextureUniform;
  111. #endif
  112. uniform bool backfaceCulling;
  113. #if defined(num_shadowmaps) && num_shadowmaps > 0
  114. uniform sampler2D uShadowMap[num_shadowmaps];
  115. uniform mat4 uShadowWorldView[num_shadowmaps];
  116. uniform mat4 uShadowProj[num_shadowmaps];
  117. #endif
  118. varying vec3 vColor;
  119. varying float vLogDepth;
  120. varying vec3 vViewPosition;
  121. varying float vRadius;
  122. varying float vPointSize;
  123. float round(float number){
  124. return floor(number + 0.5);
  125. }
  126. //
  127. // ### ######## ### ######## ######## #### ## ## ######## ###### #### ######## ######## ######
  128. // ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
  129. // ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
  130. // ## ## ## ## ## ## ######## ## ## ## ## ###### ###### ## ## ###### ######
  131. // ######### ## ## ######### ## ## ## ## ## ## ## ## ## ## ##
  132. // ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
  133. // ## ## ######## ## ## ## ## #### ### ######## ###### #### ######## ######## ######
  134. //
  135. // ---------------------
  136. // OCTREE
  137. // ---------------------
  138. #if (defined(adaptive_point_size) || defined(color_type_level_of_detail)) && defined(tree_type_octree)
  139. /**
  140. * number of 1-bits up to inclusive index position
  141. * number is treated as if it were an integer in the range 0-255
  142. *
  143. */
  144. int numberOfOnes(int number, int index){
  145. int numOnes = 0;
  146. int tmp = 128;
  147. for(int i = 7; i >= 0; i--){
  148. if(number >= tmp){
  149. number = number - tmp;
  150. if(i <= index){
  151. numOnes++;
  152. }
  153. }
  154. tmp = tmp / 2;
  155. }
  156. return numOnes;
  157. }
  158. /**
  159. * checks whether the bit at index is 1
  160. * number is treated as if it were an integer in the range 0-255
  161. *
  162. */
  163. bool isBitSet(int number, int index){
  164. // weird multi else if due to lack of proper array, int and bitwise support in WebGL 1.0
  165. int powi = 1;
  166. if(index == 0){
  167. powi = 1;
  168. }else if(index == 1){
  169. powi = 2;
  170. }else if(index == 2){
  171. powi = 4;
  172. }else if(index == 3){
  173. powi = 8;
  174. }else if(index == 4){
  175. powi = 16;
  176. }else if(index == 5){
  177. powi = 32;
  178. }else if(index == 6){
  179. powi = 64;
  180. }else if(index == 7){
  181. powi = 128;
  182. }else{
  183. return false;
  184. }
  185. int ndp = number / powi;
  186. return mod(float(ndp), 2.0) != 0.0;
  187. }
  188. /**
  189. * find the LOD at the point position
  190. */
  191. float getLOD(){//////
  192. vec3 offset = vec3(0.0, 0.0, 0.0);
  193. int iOffset = int(uVNStart);
  194. float depth = uLevel;
  195. for(float i = 0.0; i <= 30.0; i++){
  196. float nodeSizeAtLevel = uOctreeSize / pow(2.0, i + uLevel + 0.0);
  197. vec3 index3d = (position-offset) / nodeSizeAtLevel;
  198. index3d = floor(index3d + 0.5);
  199. int index = int(round(4.0 * index3d.x + 2.0 * index3d.y + index3d.z));
  200. vec4 value = texture2D(visibleNodes, vec2(float(iOffset) / 2048.0, 0.0));
  201. int mask = int(round(value.r * 255.0));
  202. if(isBitSet(mask, index)){
  203. // there are more visible child nodes at this position
  204. int advanceG = int(round(value.g * 255.0)) * 256;
  205. int advanceB = int(round(value.b * 255.0));
  206. int advanceChild = numberOfOnes(mask, index - 1);
  207. int advance = advanceG + advanceB + advanceChild;
  208. iOffset = iOffset + advance;
  209. depth++;
  210. }else{
  211. // no more visible child nodes at this position
  212. //return value.a * 255.0;
  213. float lodOffset = (255.0 * value.a) / 10.0 - 10.0;
  214. return depth + lodOffset;
  215. }
  216. offset = offset + (vec3(1.0, 1.0, 1.0) * nodeSizeAtLevel * 0.5) * index3d;
  217. }
  218. return depth;
  219. }
  220. float getSpacing(){
  221. vec3 offset = vec3(0.0, 0.0, 0.0);
  222. int iOffset = int(uVNStart);
  223. float depth = uLevel;
  224. float spacing = uNodeSpacing;
  225. for(float i = 0.0; i <= 30.0; i++){
  226. float nodeSizeAtLevel = uOctreeSize / pow(2.0, i + uLevel + 0.0);
  227. vec3 index3d = (position-offset) / nodeSizeAtLevel;
  228. index3d = floor(index3d + 0.5);
  229. int index = int(round(4.0 * index3d.x + 2.0 * index3d.y + index3d.z));
  230. vec4 value = texture2D(visibleNodes, vec2(float(iOffset) / 2048.0, 0.0));
  231. int mask = int(round(value.r * 255.0));
  232. float spacingFactor = value.a;
  233. if(i > 0.0){
  234. spacing = spacing / (255.0 * spacingFactor);
  235. }
  236. if(isBitSet(mask, index)){
  237. // there are more visible child nodes at this position
  238. int advanceG = int(round(value.g * 255.0)) * 256;
  239. int advanceB = int(round(value.b * 255.0));
  240. int advanceChild = numberOfOnes(mask, index - 1);
  241. int advance = advanceG + advanceB + advanceChild;
  242. iOffset = iOffset + advance;
  243. //spacing = spacing / (255.0 * spacingFactor);
  244. //spacing = spacing / 3.0;
  245. depth++;
  246. }else{
  247. // no more visible child nodes at this position
  248. return spacing;
  249. }
  250. offset = offset + (vec3(1.0, 1.0, 1.0) * nodeSizeAtLevel * 0.5) * index3d;
  251. }
  252. return spacing;
  253. }
  254. float getPointSizeAttenuation(){
  255. return pow(2.0, getLOD());
  256. }
  257. #endif
  258. // ---------------------
  259. // KD-TREE
  260. // ---------------------
  261. #if (defined(adaptive_point_size) || defined(color_type_level_of_detail)) && defined(tree_type_kdtree)
  262. float getLOD(){
  263. vec3 offset = vec3(0.0, 0.0, 0.0);
  264. float iOffset = 0.0;
  265. float depth = 0.0;
  266. vec3 size = uBBSize;
  267. vec3 pos = position;
  268. for(float i = 0.0; i <= 1000.0; i++){
  269. vec4 value = texture2D(visibleNodes, vec2(iOffset / 2048.0, 0.0));
  270. int children = int(value.r * 255.0);
  271. float next = value.g * 255.0;
  272. int split = int(value.b * 255.0);
  273. if(next == 0.0){
  274. return depth;
  275. }
  276. vec3 splitv = vec3(0.0, 0.0, 0.0);
  277. if(split == 1){
  278. splitv.x = 1.0;
  279. }else if(split == 2){
  280. splitv.y = 1.0;
  281. }else if(split == 4){
  282. splitv.z = 1.0;
  283. }
  284. iOffset = iOffset + next;
  285. float factor = length(pos * splitv / size);
  286. if(factor < 0.5){
  287. // left
  288. if(children == 0 || children == 2){
  289. return depth;
  290. }
  291. }else{
  292. // right
  293. pos = pos - size * splitv * 0.5;
  294. if(children == 0 || children == 1){
  295. return depth;
  296. }
  297. if(children == 3){
  298. iOffset = iOffset + 1.0;
  299. }
  300. }
  301. size = size * ((1.0 - (splitv + 1.0) / 2.0) + 0.5);
  302. depth++;
  303. }
  304. return depth;
  305. }
  306. float getPointSizeAttenuation(){
  307. return 0.5 * pow(1.3, getLOD());
  308. }
  309. #endif
  310. //
  311. // ### ######## ######## ######## #### ######## ## ## ######## ######## ######
  312. // ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
  313. // ## ## ## ## ## ## ## ## ## ## ## ## ## ##
  314. // ## ## ## ## ######## ## ######## ## ## ## ###### ######
  315. // ######### ## ## ## ## ## ## ## ## ## ## ## ##
  316. // ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
  317. // ## ## ## ## ## ## #### ######## ####### ## ######## ######
  318. //
  319. // formula adapted from: http://www.dfstudios.co.uk/articles/programming/image-programming-algorithms/image-processing-algorithms-part-5-contrast-adjustment/
  320. float getContrastFactor(float contrast){
  321. return (1.0158730158730156 * (contrast + 1.0)) / (1.0158730158730156 - contrast);
  322. }
  323. vec3 getRGB(){
  324. vec3 rgb = color;
  325. rgb = pow(rgb, vec3(uRGB_gbc.x));
  326. rgb = rgb + uRGB_gbc.y;
  327. rgb = (rgb - 0.5) * getContrastFactor(uRGB_gbc.z) + 0.5;
  328. rgb = clamp(rgb, 0.0, 1.0);
  329. return rgb;
  330. }
  331. float getIntensity(){
  332. float w = (intensity - intensityRange.x) / (intensityRange.y - intensityRange.x);
  333. w = pow(w, uIntensity_gbc.x);
  334. w = w + uIntensity_gbc.y;
  335. w = (w - 0.5) * getContrastFactor(uIntensity_gbc.z) + 0.5;
  336. w = clamp(w, 0.0, 1.0);
  337. return w;
  338. }
  339. vec3 getGpsTime(){
  340. float w = (gpsTime + uGpsOffset) * uGpsScale;
  341. vec3 c = texture2D(gradient, vec2(w, 1.0 - w)).rgb;
  342. // vec2 r = uNormalizedGpsBufferRange;
  343. // float w = gpsTime * (r.y - r.x) + r.x;
  344. // w = clamp(w, 0.0, 1.0);
  345. // vec3 c = texture2D(gradient, vec2(w,1.0-w)).rgb;
  346. return c;
  347. }
  348. vec3 getElevation(){
  349. vec4 world = modelMatrix * vec4( position, 1.0 );
  350. float w = (world.z - elevationRange.x) / (elevationRange.y - elevationRange.x);
  351. vec3 cElevation = texture2D(gradient, vec2(w,1.0-w)).rgb;
  352. return cElevation;
  353. }
  354. vec4 getClassification(){
  355. vec2 uv = vec2(classification / 255.0, 0.5);
  356. vec4 classColor = texture2D(classificationLUT, uv);
  357. return classColor;
  358. }
  359. vec3 getReturns(){
  360. // 0b 00_000_111
  361. float rn = mod(returnNumber, 8.0);
  362. // 0b 00_111_000
  363. float nr = mod(returnNumber / 8.0, 8.0);
  364. if(nr <= 1.0){
  365. return vec3(1.0, 0.0, 0.0);
  366. }else{
  367. return vec3(0.0, 1.0, 0.0);
  368. }
  369. // return vec3(nr / 4.0, 0.0, 0.0);
  370. // if(nr == 1.0){
  371. // return vec3(1.0, 1.0, 0.0);
  372. // }else{
  373. // if(rn == 1.0){
  374. // return vec3(1.0, 0.0, 0.0);
  375. // }else if(rn == nr){
  376. // return vec3(0.0, 0.0, 1.0);
  377. // }else{
  378. // return vec3(0.0, 1.0, 0.0);
  379. // }
  380. // }
  381. // if(numberOfReturns == 1.0){
  382. // return vec3(1.0, 1.0, 0.0);
  383. // }else{
  384. // if(returnNumber == 1.0){
  385. // return vec3(1.0, 0.0, 0.0);
  386. // }else if(returnNumber == numberOfReturns){
  387. // return vec3(0.0, 0.0, 1.0);
  388. // }else{
  389. // return vec3(0.0, 1.0, 0.0);
  390. // }
  391. // }
  392. }
  393. vec3 getReturnNumber(){
  394. if(numberOfReturns == 1.0){
  395. return vec3(1.0, 1.0, 0.0);
  396. }else{
  397. if(returnNumber == 1.0){
  398. return vec3(1.0, 0.0, 0.0);
  399. }else if(returnNumber == numberOfReturns){
  400. return vec3(0.0, 0.0, 1.0);
  401. }else{
  402. return vec3(0.0, 1.0, 0.0);
  403. }
  404. }
  405. }
  406. vec3 getNumberOfReturns(){
  407. float value = numberOfReturns;
  408. float w = value / 6.0;
  409. vec3 color = texture2D(gradient, vec2(w, 1.0 - w)).rgb;
  410. return color;
  411. }
  412. vec3 getSourceID(){
  413. float w = mod(pointSourceID, 10.0) / 10.0;
  414. return texture2D(gradient, vec2(w,1.0 - w)).rgb;
  415. }
  416. vec3 getCompositeColor(){
  417. vec3 c;
  418. float w;
  419. c += wRGB * getRGB();
  420. w += wRGB;
  421. c += wIntensity * getIntensity() * vec3(1.0, 1.0, 1.0);
  422. w += wIntensity;
  423. c += wElevation * getElevation();
  424. w += wElevation;
  425. c += wReturnNumber * getReturnNumber();
  426. w += wReturnNumber;
  427. c += wSourceID * getSourceID();
  428. w += wSourceID;
  429. vec4 cl = wClassification * getClassification();
  430. c += cl.a * cl.rgb;
  431. w += wClassification * cl.a;
  432. c = c / w;
  433. if(w == 0.0){
  434. //c = color;
  435. gl_Position = vec4(100.0, 100.0, 100.0, 0.0);
  436. }
  437. return c;
  438. }
  439. vec3 getNormal(){
  440. //vec3 n_hsv = vec3( modelMatrix * vec4( normal, 0.0 )) * 0.5 + 0.5; // (n_world.xyz + vec3(1.,1.,1.)) / 2.;
  441. vec3 n_view = normalize( vec3(modelViewMatrix * vec4( normal, 0.0 )) );
  442. return n_view;
  443. }
  444. bool applyBackfaceCulling() {
  445. // Black not facing vertices / Backface culling
  446. vec3 e = normalize(vec3(modelViewMatrix * vec4( position, 1. )));
  447. vec3 n = getNormal(); // normalize( vec3(modelViewMatrix * vec4( normal, 0.0 )) );
  448. if((uUseOrthographicCamera && n.z <= 0.) || (!uUseOrthographicCamera && dot( n, e ) >= 0.)) {
  449. return true;
  450. } else {
  451. return false;
  452. }
  453. }
  454. #if defined(color_type_matcap)
  455. // Matcap Material
  456. vec3 getMatcap(){
  457. vec3 eye = normalize( vec3( modelViewMatrix * vec4( position, 1. ) ) );
  458. if(uUseOrthographicCamera) {
  459. eye = vec3(0., 0., -1.);
  460. }
  461. vec3 r_en = reflect( eye, getNormal() ); // or r_en = e - 2. * dot( n, e ) * n;
  462. float m = 2. * sqrt(pow( r_en.x, 2. ) + pow( r_en.y, 2. ) + pow( r_en.z + 1., 2. ));
  463. vec2 vN = r_en.xy / m + .5;
  464. return texture2D(matcapTextureUniform, vN).rgb;
  465. }
  466. #endif
  467. vec3 getExtra(){
  468. float w = (aExtra + uExtraOffset) * uExtraScale;
  469. w = clamp(w, 0.0, 1.0);
  470. vec3 color = texture2D(gradient, vec2(w,1.0-w)).rgb;
  471. // vec2 r = uExtraNormalizedRange;
  472. // float w = aExtra * (r.y - r.x) + r.x;
  473. // w = (w - uExtraRange.x) / (uExtraRange.y - uExtraRange.x);
  474. // w = clamp(w, 0.0, 1.0);
  475. // vec3 color = texture2D(gradient, vec2(w,1.0-w)).rgb;
  476. return color;
  477. }
  478. vec3 getColor(){
  479. vec3 color;
  480. #ifdef color_type_rgba
  481. color = getRGB();
  482. #elif defined color_type_height || defined color_type_elevation
  483. color = getElevation();
  484. #elif defined color_type_rgb_height
  485. vec3 cHeight = getElevation();
  486. color = (1.0 - uTransition) * getRGB() + uTransition * cHeight;
  487. #elif defined color_type_depth
  488. float linearDepth = gl_Position.w;
  489. float expDepth = (gl_Position.z / gl_Position.w) * 0.5 + 0.5;
  490. color = vec3(linearDepth, expDepth, 0.0);
  491. //color = vec3(1.0, 0.5, 0.3);
  492. #elif defined color_type_intensity
  493. float w = getIntensity();
  494. color = vec3(w, w, w);
  495. #elif defined color_type_gps_time
  496. color = getGpsTime();
  497. #elif defined color_type_intensity_gradient
  498. float w = getIntensity();
  499. color = texture2D(gradient, vec2(w,1.0-w)).rgb;
  500. #elif defined color_type_color
  501. color = uColor;
  502. #elif defined color_type_level_of_detail
  503. float depth = getLOD();
  504. float w = depth / 10.0;
  505. color = texture2D(gradient, vec2(w,1.0-w)).rgb;
  506. #elif defined color_type_indices
  507. color = indices.rgb;
  508. #elif defined color_type_classification
  509. vec4 cl = getClassification();
  510. color = cl.rgb;
  511. #elif defined color_type_return_number
  512. color = getReturnNumber();
  513. #elif defined color_type_returns
  514. color = getReturns();
  515. #elif defined color_type_number_of_returns
  516. color = getNumberOfReturns();
  517. #elif defined color_type_source_id
  518. color = getSourceID();
  519. #elif defined color_type_point_source_id
  520. color = getSourceID();
  521. #elif defined color_type_normal
  522. color = (modelMatrix * vec4(normal, 0.0)).xyz;
  523. #elif defined color_type_phong
  524. color = color;
  525. #elif defined color_type_composite
  526. color = getCompositeColor();
  527. #elif defined color_type_matcap
  528. color = getMatcap();
  529. #else
  530. color = getExtra();
  531. #endif
  532. if (backfaceCulling && applyBackfaceCulling()){
  533. //color = vec3(0.);
  534. }
  535. //applyBackfaceCulling直接返回false或者注释color = vec3(0.);都没问题
  536. return color;
  537. }
  538. float getPointSize(){
  539. float pointSize = 1.0;
  540. float slope = tan(fov / 2.0);
  541. float projFactor = -0.5 * uScreenHeight / (slope * vViewPosition.z);
  542. /*
  543. float scale = length(
  544. modelViewMatrix * vec4(0, 0, 0, 1) -
  545. modelViewMatrix * vec4(uOctreeSpacing, 0, 0, 1)
  546. ) / uOctreeSpacing;
  547. projFactor = projFactor * scale;
  548. */
  549. float r = uOctreeSpacing * 1.7;
  550. //vRadius = r;
  551. #if defined fixed_point_size
  552. pointSize = size;
  553. #elif defined attenuated_point_size
  554. if(uUseOrthographicCamera){
  555. pointSize = size;
  556. }else{ //近大远小,模拟真实mesh,边缘放大
  557. //pointSize = size * spacing * projFactor; //spacing是attribute 为空 如果有这个值就能更自适应填补
  558. //pointSize = size * uOctreeSpacing * projFactor / 18.0; //直接用cloud的spacing里,不过因为都一样所以可能没有什么意义
  559. //pointSize = pointSize * projFactor;
  560. pointSize = size * projFactor ;
  561. }
  562. #elif defined adaptive_point_size
  563. if(uUseOrthographicCamera) {
  564. float worldSpaceSize = 1.0 * size * r / getPointSizeAttenuation();
  565. pointSize = (worldSpaceSize / uOrthoWidth) * uScreenWidth;
  566. } else {
  567. float worldSpaceSize = 1.0 * size * r / getPointSizeAttenuation();
  568. pointSize = worldSpaceSize * projFactor;
  569. }
  570. #endif
  571. pointSize = max(minSize, pointSize);
  572. pointSize = min(maxSize, pointSize);
  573. vRadius = pointSize / projFactor;
  574. return pointSize;
  575. }
  576. #if defined(num_clippolygons) && num_clippolygons > 0
  577. bool pointInClipPolygon(vec3 point, int polyIdx) {
  578. mat4 wvp = uClipPolygonWVP[polyIdx];
  579. //vec4 screenClipPos = uClipPolygonVP[polyIdx] * modelMatrix * vec4(point, 1.0);
  580. //screenClipPos.xy = screenClipPos.xy / screenClipPos.w * 0.5 + 0.5;
  581. vec4 pointNDC = wvp * vec4(point, 1.0);
  582. pointNDC.xy = pointNDC.xy / pointNDC.w;
  583. int j = uClipPolygonVCount[polyIdx] - 1;
  584. bool c = false;
  585. for(int i = 0; i < 8; i++) {
  586. if(i == uClipPolygonVCount[polyIdx]) {
  587. break;
  588. }
  589. //vec4 verti = wvp * vec4(uClipPolygonVertices[polyIdx * 8 + i], 1);
  590. //vec4 vertj = wvp * vec4(uClipPolygonVertices[polyIdx * 8 + j], 1);
  591. //verti.xy = verti.xy / verti.w;
  592. //vertj.xy = vertj.xy / vertj.w;
  593. //verti.xy = verti.xy / verti.w * 0.5 + 0.5;
  594. //vertj.xy = vertj.xy / vertj.w * 0.5 + 0.5;
  595. vec3 verti = uClipPolygonVertices[polyIdx * 8 + i];
  596. vec3 vertj = uClipPolygonVertices[polyIdx * 8 + j];
  597. if( ((verti.y > pointNDC.y) != (vertj.y > pointNDC.y)) &&
  598. (pointNDC.x < (vertj.x-verti.x) * (pointNDC.y-verti.y) / (vertj.y-verti.y) + verti.x) ) {
  599. c = !c;
  600. }
  601. j = i;
  602. }
  603. return c;
  604. }
  605. #endif
  606. void doClipping(){
  607. {
  608. vec4 cl = getClassification();
  609. if(cl.a == 0.0){
  610. gl_Position = vec4(100.0, 100.0, 100.0, 0.0);
  611. return;
  612. }
  613. }
  614. #if defined(clip_return_number_enabled)
  615. { // return number filter
  616. vec2 range = uFilterReturnNumberRange;
  617. if(returnNumber < range.x || returnNumber > range.y){
  618. gl_Position = vec4(100.0, 100.0, 100.0, 0.0);
  619. return;
  620. }
  621. }
  622. #endif
  623. #if defined(clip_number_of_returns_enabled)
  624. { // number of return filter
  625. vec2 range = uFilterNumberOfReturnsRange;
  626. if(numberOfReturns < range.x || numberOfReturns > range.y){
  627. gl_Position = vec4(100.0, 100.0, 100.0, 0.0);
  628. return;
  629. }
  630. }
  631. #endif
  632. #if defined(clip_gps_enabled)
  633. { // GPS time filter
  634. float time = (gpsTime + uGpsOffset) * uGpsScale;
  635. vec2 range = uFilterGPSTimeClipRange;
  636. if(time < range.x || time > range.y){
  637. gl_Position = vec4(100.0, 100.0, 100.0, 0.0);
  638. return;
  639. }
  640. }
  641. #endif
  642. #if defined(clip_point_source_id_enabled)
  643. { // point source id filter
  644. vec2 range = uFilterPointSourceIDClipRange;
  645. if(pointSourceID < range.x || pointSourceID > range.y){
  646. gl_Position = vec4(100.0, 100.0, 100.0, 0.0);
  647. return;
  648. }
  649. }
  650. #endif
  651. int clipVolumesCount = 0;
  652. int insideCount = 0;
  653. #if defined(num_clipboxes) && num_clipboxes > 0
  654. for(int i = 0; i < num_clipboxes; i++){
  655. vec4 clipPosition = clipBoxes[i] * modelMatrix * vec4( position, 1.0 );
  656. bool inside = -0.5 <= clipPosition.x && clipPosition.x <= 0.5;
  657. inside = inside && -0.5 <= clipPosition.y && clipPosition.y <= 0.5;
  658. inside = inside && -0.5 <= clipPosition.z && clipPosition.z <= 0.5;
  659. insideCount = insideCount + (inside ? 1 : 0);
  660. clipVolumesCount++;
  661. }
  662. #endif
  663. #if defined(num_clippolygons) && num_clippolygons > 0
  664. for(int i = 0; i < num_clippolygons; i++) {
  665. bool inside = pointInClipPolygon(position, i);
  666. insideCount = insideCount + (inside ? 1 : 0);
  667. clipVolumesCount++;
  668. }
  669. #endif
  670. bool insideAny = insideCount > 0;
  671. bool insideAll = (clipVolumesCount > 0) && (clipVolumesCount == insideCount);
  672. if(clipMethod == CLIPMETHOD_INSIDE_ANY){
  673. if(insideAny && clipTask == CLIPTASK_HIGHLIGHT){
  674. vColor.r += 0.5;
  675. }else if(!insideAny && clipTask == CLIPTASK_SHOW_INSIDE){
  676. gl_Position = vec4(100.0, 100.0, 100.0, 1.0);
  677. }else if(insideAny && clipTask == CLIPTASK_SHOW_OUTSIDE){
  678. gl_Position = vec4(100.0, 100.0, 100.0, 1.0);
  679. }
  680. }else if(clipMethod == CLIPMETHOD_INSIDE_ALL){
  681. if(insideAll && clipTask == CLIPTASK_HIGHLIGHT){
  682. vColor.r += 0.5;
  683. }else if(!insideAll && clipTask == CLIPTASK_SHOW_INSIDE){
  684. gl_Position = vec4(100.0, 100.0, 100.0, 1.0);
  685. }else if(insideAll && clipTask == CLIPTASK_SHOW_OUTSIDE){
  686. gl_Position = vec4(100.0, 100.0, 100.0, 1.0);
  687. }
  688. }
  689. }
  690. //
  691. // ## ## ### #### ## ##
  692. // ### ### ## ## ## ### ##
  693. // #### #### ## ## ## #### ##
  694. // ## ### ## ## ## ## ## ## ##
  695. // ## ## ######### ## ## ####
  696. // ## ## ## ## ## ## ###
  697. // ## ## ## ## #### ## ##
  698. //
  699. vec2 getSamplerCoord( vec3 direction )
  700. {
  701. direction = normalize(direction);
  702. float tx=atan(direction.x,-direction.y)/(PI*2.0)+0.5;
  703. float ty=acos(direction.z)/PI;
  704. return vec2(tx,ty);
  705. }
  706. vec3 transformAxis( vec3 direction ) //navvis->4dkk
  707. {
  708. float y = direction.y;
  709. direction.y = direction.z;
  710. direction.z = -y;
  711. return direction;
  712. }
  713. void main() {
  714. #ifdef use_filter_by_normal
  715. if(abs(getNormal().z) > 0.3) { //ufilterByNormalThreshold 暂定0.3
  716. // Move point outside clip space space to discard it.
  717. gl_Position = vec4(0.0, 0.0, 2.0, 1.0);
  718. return;
  719. }
  720. #endif
  721. vec4 mvPosition = modelViewMatrix * vec4(position, 1.0 );
  722. vViewPosition = mvPosition.xyz;
  723. gl_Position = projectionMatrix * mvPosition;
  724. vLogDepth = log2(-mvPosition.z);
  725. // COLOR
  726. //加-------------------
  727. #if defined(usePanoMap)
  728. vec4 worldPosition = modelMatrix * vec4(position, 1.0);
  729. vec3 positionLocalToPanoCenter0 = worldPosition.xyz - pano0Position;
  730. vec3 vWorldPosition0 = (vec4(positionLocalToPanoCenter0, 1.0) * pano0Matrix).xyz;
  731. vWorldPosition0.x *= -1.0;
  732. vWorldPosition0 = transformAxis(vWorldPosition0);
  733. vec3 positionLocalToPanoCenter1 = worldPosition.xyz - pano1Position;
  734. vec3 vWorldPosition1 = (vec4(positionLocalToPanoCenter1, 1.0) * pano1Matrix).xyz;
  735. vWorldPosition1.x *= -1.0;
  736. vWorldPosition1 = transformAxis(vWorldPosition1);
  737. /*
  738. vec2 samplerCoord0 = getSamplerCoord(vWorldPosition0.xyz);
  739. vec2 samplerCoord1 = getSamplerCoord(vWorldPosition1.xyz);
  740. vec4 colorFromPano0 = texture2D(pano0Map,samplerCoord0);
  741. vec4 colorFromPano1 = texture2D(pano1Map,samplerCoord1);
  742. */
  743. vec4 colorFromPano0=textureCube(pano0Map,vWorldPosition0.xyz);
  744. vec4 colorFromPano1=textureCube(pano1Map,vWorldPosition1.xyz);
  745. vColor = mix(colorFromPano0,colorFromPano1,progress).xyz;
  746. //float easeInOutRatio = 0.0; //缓冲,渐变点云到贴图的颜色
  747. if(progress < easeInOutRatio){
  748. float easeProgress = (easeInOutRatio - progress) / easeInOutRatio;
  749. vec3 vColor1 = getColor();
  750. vColor = mix(vColor,vColor1,easeProgress);
  751. }else if(progress > 1.0 - easeInOutRatio){
  752. float easeProgress = (progress - (1.0 - easeInOutRatio) ) / easeInOutRatio;
  753. vec3 vColor1 = getColor();
  754. vColor = mix(vColor,vColor1,easeProgress);
  755. }
  756. #else
  757. vColor = getColor();
  758. #endif
  759. //-------------------
  760. //数据集校准时,相机拉远后随着点云密集需降低透明度
  761. #ifdef attenuated_opacity
  762. vOpacity = uOpacity * exp(-length(-mvPosition.xyz) / 1000.0); //opacityAttenuation = 1000
  763. #else
  764. vOpacity = uOpacity;
  765. #endif
  766. // POINT SIZE
  767. float pointSize = getPointSize();
  768. gl_PointSize = pointSize;
  769. vPointSize = pointSize;
  770. // only for "replacing" approaches
  771. // if(getLOD() != uLevel){
  772. // gl_Position = vec4(10.0, 10.0, 10.0, 1.0);
  773. // }
  774. #if defined hq_depth_pass
  775. float originalDepth = gl_Position.w;
  776. float adjustedDepth = originalDepth + 2.0 * vRadius;
  777. float adjust = adjustedDepth / originalDepth;
  778. mvPosition.xyz = mvPosition.xyz * adjust;
  779. gl_Position = projectionMatrix * mvPosition;
  780. #endif
  781. // CLIPPING
  782. doClipping();
  783. #if defined(num_clipspheres) && num_clipspheres > 0
  784. for(int i = 0; i < num_clipspheres; i++){
  785. vec4 sphereLocal = uClipSpheres[i] * mvPosition;
  786. float distance = length(sphereLocal.xyz);
  787. if(distance < 1.0){
  788. float w = distance;
  789. vec3 cGradient = texture2D(gradient, vec2(w, 1.0 - w)).rgb;
  790. vColor = cGradient;
  791. //vColor = cGradient * 0.7 + vColor * 0.3;
  792. }
  793. }
  794. #endif
  795. #if defined(num_shadowmaps) && num_shadowmaps > 0
  796. const float sm_near = 0.1;
  797. const float sm_far = 10000.0;
  798. for(int i = 0; i < num_shadowmaps; i++){
  799. vec3 viewPos = (uShadowWorldView[i] * vec4(position, 1.0)).xyz;
  800. float distanceToLight = abs(viewPos.z);
  801. vec4 projPos = uShadowProj[i] * uShadowWorldView[i] * vec4(position, 1);
  802. vec3 nc = projPos.xyz / projPos.w;
  803. float u = nc.x * 0.5 + 0.5;
  804. float v = nc.y * 0.5 + 0.5;
  805. vec2 sampleStep = vec2(1.0 / (2.0*1024.0), 1.0 / (2.0*1024.0)) * 1.5;
  806. vec2 sampleLocations[9];
  807. sampleLocations[0] = vec2(0.0, 0.0);
  808. sampleLocations[1] = sampleStep;
  809. sampleLocations[2] = -sampleStep;
  810. sampleLocations[3] = vec2(sampleStep.x, -sampleStep.y);
  811. sampleLocations[4] = vec2(-sampleStep.x, sampleStep.y);
  812. sampleLocations[5] = vec2(0.0, sampleStep.y);
  813. sampleLocations[6] = vec2(0.0, -sampleStep.y);
  814. sampleLocations[7] = vec2(sampleStep.x, 0.0);
  815. sampleLocations[8] = vec2(-sampleStep.x, 0.0);
  816. float visibleSamples = 0.0;
  817. float numSamples = 0.0;
  818. float bias = vRadius * 2.0;
  819. for(int j = 0; j < 9; j++){
  820. vec4 depthMapValue = texture2D(uShadowMap[i], vec2(u, v) + sampleLocations[j]);
  821. float linearDepthFromSM = depthMapValue.x + bias;
  822. float linearDepthFromViewer = distanceToLight;
  823. if(linearDepthFromSM > linearDepthFromViewer){
  824. visibleSamples += 1.0;
  825. }
  826. numSamples += 1.0;
  827. }
  828. float visibility = visibleSamples / numSamples;
  829. if(u < 0.0 || u > 1.0 || v < 0.0 || v > 1.0 || nc.x < -1.0 || nc.x > 1.0 || nc.y < -1.0 || nc.y > 1.0 || nc.z < -1.0 || nc.z > 1.0){
  830. //vColor = vec3(0.0, 0.0, 0.2);
  831. }else{
  832. //vColor = vec3(1.0, 1.0, 1.0) * visibility + vec3(1.0, 1.0, 1.0) * vec3(0.5, 0.0, 0.0) * (1.0 - visibility);
  833. vColor = vColor * visibility + vColor * uShadowColor * (1.0 - visibility);
  834. }
  835. }
  836. #endif
  837. }
  838. `
  839. Shaders["pointcloud.fs"] = `
  840. #if defined paraboloid_point_shape
  841. #extension GL_EXT_frag_depth : enable
  842. #endif
  843. #define PI 3.141592653589793
  844. precision highp float;
  845. precision highp int;
  846. /*
  847. #if defined(usePanoMap)
  848. uniform samplerCube pano0Map; //随便设置一个samplerCube去使用都会让点云消失
  849. uniform samplerCube pano1Map;
  850. uniform float progress;
  851. uniform float easeInOutRatio;
  852. uniform vec3 pano0Position;
  853. uniform mat4 pano0Matrix;
  854. uniform vec3 pano1Position;
  855. uniform mat4 pano1Matrix;
  856. varying vec3 vWorldPosition0;
  857. varying vec3 vWorldPosition1;
  858. #endif
  859. */
  860. //------------
  861. uniform mat4 viewMatrix;
  862. uniform mat4 uViewInv;
  863. uniform mat4 uProjInv;
  864. uniform vec3 cameraPosition;
  865. uniform mat4 projectionMatrix;
  866. //uniform float uOpacity;
  867. varying float vOpacity; //add
  868. uniform float blendHardness;
  869. uniform float blendDepthSupplement;
  870. uniform float fov;
  871. uniform float uSpacing;
  872. uniform float near;
  873. uniform float far;
  874. uniform float uPCIndex;
  875. uniform float uScreenWidth;
  876. uniform float uScreenHeight;
  877. varying vec3 vColor;
  878. varying float vLogDepth;
  879. varying vec3 vViewPosition;
  880. varying float vRadius;
  881. varying float vPointSize;
  882. varying vec3 vPosition;
  883. float specularStrength = 1.0;
  884. vec2 getSamplerCoord( vec3 direction )
  885. {
  886. direction = normalize(direction);
  887. float tx=atan(direction.x,-direction.y)/(PI*2.0)+0.5;
  888. float ty=acos(direction.z)/PI;
  889. return vec2(tx,ty);
  890. }
  891. void main() {
  892. vec3 color = vColor;
  893. /*#if defined(usePanoMap) //加 经测试,即使全部写在fragment里也是无论pointsize多大都是一个点一个颜色,所以干脆写在vectex里
  894. vec4 colorFromPano0=textureCube(pano0Map,vWorldPosition0.xyz);
  895. vec4 colorFromPano1=textureCube(pano1Map,vWorldPosition1.xyz);
  896. color = mix(colorFromPano0,colorFromPano1,progress).xyz;
  897. //float easeInOutRatio = 0.0; //缓冲,渐变点云到贴图的颜色
  898. if(progress < easeInOutRatio){
  899. float easeProgress = (easeInOutRatio - progress) / easeInOutRatio;
  900. color = mix(color,vColor,easeProgress);
  901. }else if(progress > 1.0 - easeInOutRatio){
  902. float easeProgress = (progress - (1.0 - easeInOutRatio) ) / easeInOutRatio;
  903. color = mix(color,vColor,easeProgress);
  904. }
  905. #else
  906. color = vColor;
  907. #endif*/
  908. float depth = gl_FragCoord.z;
  909. #if defined(circle_point_shape) || defined(paraboloid_point_shape)
  910. float u = 2.0 * gl_PointCoord.x - 1.0;
  911. float v = 2.0 * gl_PointCoord.y - 1.0;
  912. #endif
  913. #if defined(circle_point_shape)
  914. float cc = u*u + v*v;
  915. if(cc > 1.0){
  916. discard;
  917. }
  918. #endif
  919. #if defined color_type_indices //pick point recognize
  920. gl_FragColor = vec4(color, uPCIndex / 255.0);
  921. #else
  922. gl_FragColor = vec4(color, vOpacity);
  923. #endif
  924. #if defined paraboloid_point_shape
  925. float wi = 0.0 - ( u*u + v*v);
  926. vec4 pos = vec4(vViewPosition, 1.0);
  927. pos.z += wi * vRadius;
  928. float linearDepth = -pos.z;
  929. pos = projectionMatrix * pos;
  930. pos = pos / pos.w;
  931. float expDepth = pos.z;
  932. depth = (pos.z + 1.0) / 2.0;
  933. gl_FragDepthEXT = depth;
  934. #if defined(color_type_depth)
  935. color.r = linearDepth;
  936. color.g = expDepth;
  937. #endif
  938. #if defined(use_edl)
  939. gl_FragColor.a = log2(linearDepth);
  940. #endif
  941. #else
  942. #if defined(use_edl)
  943. gl_FragColor.a = vLogDepth;
  944. #endif
  945. #endif
  946. #if defined(weighted_splats)
  947. float distance = 2.0 * length(gl_PointCoord.xy - 0.5);
  948. float weight = max(0.0, 1.0 - distance);
  949. weight = pow(weight, 1.5);
  950. gl_FragColor.a = weight;
  951. gl_FragColor.xyz = gl_FragColor.xyz * weight;
  952. #endif
  953. //gl_FragColor = vec4(0.0, 0.7, 0.0, 1.0);
  954. }
  955. `
  956. Shaders["pointcloud_sm.vs"] = `
  957. precision mediump float;
  958. precision mediump int;
  959. attribute vec3 position;
  960. attribute vec3 color;
  961. uniform mat4 modelMatrix;
  962. uniform mat4 modelViewMatrix;
  963. uniform mat4 projectionMatrix;
  964. uniform mat4 viewMatrix;
  965. uniform float uScreenWidth;
  966. uniform float uScreenHeight;
  967. uniform float near;
  968. uniform float far;
  969. uniform float uSpacing;
  970. uniform float uOctreeSize;
  971. uniform float uLevel;
  972. uniform float uVNStart;
  973. uniform sampler2D visibleNodes;
  974. varying float vLinearDepth;
  975. varying vec3 vColor;
  976. #define PI 3.141592653589793
  977. // ---------------------
  978. // OCTREE
  979. // ---------------------
  980. #if defined(adaptive_point_size)
  981. /**
  982. * number of 1-bits up to inclusive index position
  983. * number is treated as if it were an integer in the range 0-255
  984. *
  985. */
  986. float numberOfOnes(float number, float index){
  987. float tmp = mod(number, pow(2.0, index + 1.0));
  988. float numOnes = 0.0;
  989. for(float i = 0.0; i < 8.0; i++){
  990. if(mod(tmp, 2.0) != 0.0){
  991. numOnes++;
  992. }
  993. tmp = floor(tmp / 2.0);
  994. }
  995. return numOnes;
  996. }
  997. /**
  998. * checks whether the bit at index is 1
  999. * number is treated as if it were an integer in the range 0-255
  1000. *
  1001. */
  1002. bool isBitSet(float number, float index){
  1003. return mod(floor(number / pow(2.0, index)), 2.0) != 0.0;
  1004. }
  1005. /**
  1006. * find the LOD at the point position
  1007. */
  1008. float getLOD(){
  1009. vec3 offset = vec3(0.0, 0.0, 0.0);
  1010. float iOffset = uVNStart;
  1011. float depth = uLevel;
  1012. for(float i = 0.0; i <= 30.0; i++){
  1013. float nodeSizeAtLevel = uOctreeSize / pow(2.0, i + uLevel + 0.0);
  1014. vec3 index3d = (position-offset) / nodeSizeAtLevel;
  1015. index3d = floor(index3d + 0.5);
  1016. float index = 4.0 * index3d.x + 2.0 * index3d.y + index3d.z;
  1017. vec4 value = texture2D(visibleNodes, vec2(iOffset / 2048.0, 0.0));
  1018. float mask = value.r * 255.0;
  1019. if(isBitSet(mask, index)){
  1020. // there are more visible child nodes at this position
  1021. iOffset = iOffset + value.g * 255.0 * 256.0 + value.b * 255.0 + numberOfOnes(mask, index - 1.0);
  1022. depth++;
  1023. }else{
  1024. // no more visible child nodes at this position
  1025. return depth;
  1026. }
  1027. offset = offset + (vec3(1.0, 1.0, 1.0) * nodeSizeAtLevel * 0.5) * index3d;
  1028. }
  1029. return depth;
  1030. }
  1031. #endif
  1032. float getPointSize(){
  1033. float pointSize = 1.0;
  1034. float slope = tan(fov / 2.0);
  1035. float projFactor = -0.5 * uScreenHeight / (slope * vViewPosition.z);
  1036. float r = uOctreeSpacing * 1.5;
  1037. vRadius = r;
  1038. #if defined fixed_point_size
  1039. pointSize = size;
  1040. #elif defined attenuated_point_size
  1041. if(uUseOrthographicCamera){
  1042. pointSize = size;
  1043. }else{
  1044. pointSize = pointSize * projFactor;
  1045. }
  1046. #elif defined adaptive_point_size
  1047. if(uUseOrthographicCamera) {
  1048. float worldSpaceSize = 1.5 * size * r / getPointSizeAttenuation();
  1049. pointSize = (worldSpaceSize / uOrthoWidth) * uScreenWidth;
  1050. } else {
  1051. float worldSpaceSize = 1.5 * size * r / getPointSizeAttenuation();
  1052. pointSize = worldSpaceSize * projFactor;
  1053. }
  1054. #endif
  1055. pointSize = max(minSize, pointSize);
  1056. pointSize = min(maxSize, pointSize);
  1057. vRadius = pointSize / projFactor;
  1058. return pointSize;
  1059. }
  1060. void main() {
  1061. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  1062. vLinearDepth = gl_Position.w;
  1063. float pointSize = getPointSize();
  1064. gl_PointSize = pointSize;
  1065. }
  1066. `
  1067. Shaders["pointcloud_sm.fs"] = `
  1068. precision mediump float;
  1069. precision mediump int;
  1070. varying vec3 vColor;
  1071. varying float vLinearDepth;
  1072. void main() {
  1073. //gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
  1074. //gl_FragColor = vec4(vColor, 1.0);
  1075. //gl_FragColor = vec4(vLinearDepth, pow(vLinearDepth, 2.0), 0.0, 1.0);
  1076. gl_FragColor = vec4(vLinearDepth, vLinearDepth / 30.0, vLinearDepth / 30.0, 1.0);
  1077. }
  1078. `
  1079. Shaders["normalize.vs"] = `
  1080. precision mediump float;
  1081. precision mediump int;
  1082. attribute vec3 position;
  1083. attribute vec2 uv;
  1084. uniform mat4 projectionMatrix;
  1085. uniform mat4 modelViewMatrix;
  1086. varying vec2 vUv;
  1087. void main() {
  1088. vUv = uv;
  1089. gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
  1090. }`
  1091. Shaders["normalize.fs"] = `
  1092. #extension GL_EXT_frag_depth : enable
  1093. precision mediump float;
  1094. precision mediump int;
  1095. uniform sampler2D uWeightMap;
  1096. uniform sampler2D uDepthMap;
  1097. varying vec2 vUv;
  1098. void main() {
  1099. float depth = texture2D(uDepthMap, vUv).r;
  1100. if(depth >= 1.0){
  1101. discard;
  1102. }
  1103. gl_FragColor = vec4(depth, 1.0, 0.0, 1.0);
  1104. vec4 color = texture2D(uWeightMap, vUv);
  1105. color = color / color.w;
  1106. gl_FragColor = vec4(color.xyz, 1.0);
  1107. gl_FragDepthEXT = depth;
  1108. }`
  1109. Shaders["normalize_and_edl.fs"] = `
  1110. #extension GL_EXT_frag_depth : enable
  1111. //
  1112. // adapted from the EDL shader code from Christian Boucheny in cloud compare:
  1113. // https://github.com/cloudcompare/trunk/tree/master/plugins/qEDL/shaders/EDL
  1114. //
  1115. precision mediump float;
  1116. precision mediump int;
  1117. uniform sampler2D uWeightMap;
  1118. uniform sampler2D uEDLMap;
  1119. uniform sampler2D uDepthMap;
  1120. uniform float screenWidth;
  1121. uniform float screenHeight;
  1122. uniform vec2 neighbours[NEIGHBOUR_COUNT];
  1123. uniform float edlStrength;
  1124. uniform float radius;
  1125. varying vec2 vUv;
  1126. float response(float depth){
  1127. vec2 uvRadius = radius / vec2(screenWidth, screenHeight);
  1128. float sum = 0.0;
  1129. for(int i = 0; i < NEIGHBOUR_COUNT; i++){
  1130. vec2 uvNeighbor = vUv + uvRadius * neighbours[i];
  1131. float neighbourDepth = texture2D(uEDLMap, uvNeighbor).a;
  1132. if(neighbourDepth != 0.0){
  1133. if(depth == 0.0){
  1134. sum += 100.0;
  1135. }else{
  1136. sum += max(0.0, depth - neighbourDepth);
  1137. }
  1138. }
  1139. }
  1140. return sum / float(NEIGHBOUR_COUNT);
  1141. }
  1142. void main() {
  1143. float edlDepth = texture2D(uEDLMap, vUv).a;
  1144. float res = response(edlDepth);
  1145. float shade = exp(-res * 300.0 * edlStrength);
  1146. float depth = texture2D(uDepthMap, vUv).r;
  1147. if(depth >= 1.0 && res == 0.0){
  1148. discard;
  1149. }
  1150. vec4 color = texture2D(uWeightMap, vUv);
  1151. color = color / color.w;
  1152. color = color * shade;
  1153. gl_FragColor = vec4(color.xyz, 1.0);
  1154. gl_FragDepthEXT = depth;
  1155. }`
  1156. Shaders["edl.vs"] = `
  1157. precision mediump float;
  1158. precision mediump int;
  1159. attribute vec3 position;
  1160. attribute vec2 uv;
  1161. uniform mat4 projectionMatrix;
  1162. uniform mat4 modelViewMatrix;
  1163. varying vec2 vUv;
  1164. void main() {
  1165. vUv = uv;
  1166. vec4 mvPosition = modelViewMatrix * vec4(position,1.0);
  1167. gl_Position = projectionMatrix * mvPosition;
  1168. }`
  1169. Shaders["edl.fs"] = `
  1170. #extension GL_EXT_frag_depth : enable
  1171. //
  1172. // adapted from the EDL shader code from Christian Boucheny in cloud compare:
  1173. // https://github.com/cloudcompare/trunk/tree/master/plugins/qEDL/shaders/EDL
  1174. //
  1175. precision mediump float;
  1176. precision mediump int;
  1177. uniform float screenWidth;
  1178. uniform float screenHeight;
  1179. uniform vec2 neighbours[NEIGHBOUR_COUNT];
  1180. uniform float edlStrength;
  1181. uniform float radius;
  1182. uniform float opacity;
  1183. //uniform float uNear;
  1184. //uniform float uFar;
  1185. uniform mat4 uProj;
  1186. uniform sampler2D uEDLColor;
  1187. uniform sampler2D uEDLDepth;
  1188. varying vec2 vUv;
  1189. uniform int useEDL;
  1190. float response(float depth){
  1191. vec2 uvRadius = radius / vec2(screenWidth, screenHeight);
  1192. float sum = 0.0;
  1193. for(int i = 0; i < NEIGHBOUR_COUNT; i++){
  1194. vec2 uvNeighbor = vUv + uvRadius * neighbours[i];
  1195. float neighbourDepth = texture2D(uEDLColor, uvNeighbor).a;
  1196. neighbourDepth = (neighbourDepth == 1.0) ? 0.0 : neighbourDepth;
  1197. if(neighbourDepth != 0.0){
  1198. if(depth == 0.0){
  1199. sum += 100.0;
  1200. }else{
  1201. sum += max(0.0, depth - neighbourDepth);
  1202. }
  1203. }
  1204. }
  1205. return sum / float(NEIGHBOUR_COUNT);
  1206. }
  1207. void main(){
  1208. vec4 cEDL = texture2D(uEDLColor, vUv);
  1209. float depth = cEDL.a;
  1210. depth = (depth == 1.0) ? 0.0 : depth;
  1211. if(depth == 0.0){
  1212. discard;
  1213. }
  1214. if(useEDL == 1){
  1215. float res = response(depth);
  1216. float shade = exp(-res * 300.0 * edlStrength);
  1217. gl_FragColor = vec4(cEDL.rgb * shade, opacity);
  1218. }else{//加 不改颜色的情况
  1219. gl_FragColor = vec4(cEDL.rgb, opacity);
  1220. }
  1221. { // write regular hyperbolic depth values to depth buffer 修改深度
  1222. float dl = pow(2.0, depth);
  1223. vec4 dp = uProj * vec4(0.0, 0.0, -dl, 1.0);
  1224. float pz = dp.z / dp.w;
  1225. float fragDepth = (pz + 1.0) / 2.0;
  1226. gl_FragDepthEXT = fragDepth;
  1227. }
  1228. }
  1229. `
  1230. Shaders["blur.vs"] = `
  1231. varying vec2 vUv;
  1232. void main() {
  1233. vUv = uv;
  1234. gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
  1235. }`
  1236. Shaders["blur.fs"] = `
  1237. uniform mat4 projectionMatrix;
  1238. uniform float screenWidth;
  1239. uniform float screenHeight;
  1240. uniform float near;
  1241. uniform float far;
  1242. uniform sampler2D map;
  1243. varying vec2 vUv;
  1244. void main() {
  1245. float dx = 1.0 / screenWidth;
  1246. float dy = 1.0 / screenHeight;
  1247. vec3 color = vec3(0.0, 0.0, 0.0);
  1248. color += texture2D(map, vUv + vec2(-dx, -dy)).rgb;
  1249. color += texture2D(map, vUv + vec2( 0, -dy)).rgb;
  1250. color += texture2D(map, vUv + vec2(+dx, -dy)).rgb;
  1251. color += texture2D(map, vUv + vec2(-dx, 0)).rgb;
  1252. color += texture2D(map, vUv + vec2( 0, 0)).rgb;
  1253. color += texture2D(map, vUv + vec2(+dx, 0)).rgb;
  1254. color += texture2D(map, vUv + vec2(-dx, dy)).rgb;
  1255. color += texture2D(map, vUv + vec2( 0, dy)).rgb;
  1256. color += texture2D(map, vUv + vec2(+dx, dy)).rgb;
  1257. color = color / 9.0;
  1258. gl_FragColor = vec4(color, 1.0);
  1259. }`
  1260. Shaders["depthBasic.vs"] = `
  1261. varying vec2 vUv;
  1262. void main() {
  1263. vUv = uv;
  1264. gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
  1265. }
  1266. `
  1267. Shaders["depthBasic.fs"] = `varying vec2 vUv;
  1268. uniform float opacity;
  1269. uniform vec3 baseColor;
  1270. #if defined use_map
  1271. uniform sampler2D map;
  1272. #endif
  1273. #if defined(GL_EXT_frag_depth) && defined(useDepth)
  1274. //似乎通过gl.getExtension('EXT_frag_depth')得到的GL_EXT_frag_depth
  1275. uniform sampler2D depthTexture;
  1276. uniform float nearPlane;
  1277. uniform float farPlane;
  1278. uniform vec2 resolution;
  1279. uniform vec2 viewportOffset; // viewportOffset 范围从0-整个画布的像素
  1280. float convertToLinear(float zValue)
  1281. {
  1282. float z = zValue * 2.0 - 1.0;
  1283. return (2.0 * nearPlane * farPlane) / (farPlane + nearPlane - z * (farPlane - nearPlane));
  1284. }
  1285. #endif
  1286. void main() {
  1287. vec4 color = vec4(baseColor, opacity);
  1288. #if defined(GL_EXT_frag_depth) && defined(useDepth)
  1289. // mixFactor and clipFactor define the color mixing proportion between the states of
  1290. // full visibility and occluded visibility
  1291. // and
  1292. // full visibility and total invisibility
  1293. float mixFactor = 0.0;
  1294. float clipFactor = 0.0;
  1295. // The linear depth value of the current fragment
  1296. float fragDepth = convertToLinear(gl_FragCoord.z);
  1297. // The coordinates of the current fragment in the depth texture
  1298. vec2 depthTxtCoords = vec2(gl_FragCoord.x-viewportOffset.x, gl_FragCoord.y) / resolution;
  1299. // The linear depth value of the pixel occupied by this fragment in the depth buffer
  1300. float textureDepth = convertToLinear(texture2D(depthTexture, depthTxtCoords).r);
  1301. // The difference between the two depths
  1302. float delta = textureDepth - fragDepth;
  1303. if (delta < 0.0)//差距
  1304. {
  1305. // occlusionDistance and clipDistance define the width of the respective zones and
  1306. // mixFactor and clipFactor express the interpolation between the two colors depending on the position
  1307. // of the current fragment withing those zones.
  1308. float occlusionDistance = - 1.0; //1米
  1309. float clipDistance = - 4.0;
  1310. mixFactor = clamp(delta / occlusionDistance, 0.0, 1.0);
  1311. clipFactor = clamp(delta / clipDistance, 0.0, 1.0);
  1312. }
  1313. // If the fragment is totally transparent, don't bother drawing it
  1314. if (clipFactor == 1.0)
  1315. {
  1316. discard;
  1317. }else{
  1318. #if defined use_map
  1319. color = texture2D(map, vUv) * color;
  1320. #endif
  1321. vec3 backColor = vec3(0.8,0.8,0.8);
  1322. color = vec4(mix(color.rgb, backColor, mixFactor), color.a * (1.0 - clipFactor));
  1323. }
  1324. #else
  1325. #if defined use_map
  1326. color = texture2D(map, vUv) * color;
  1327. #endif
  1328. #endif
  1329. gl_FragColor = color;
  1330. }
  1331. `
  1332. Shaders["copyCubeMap.vs"] = `varying vec3 vWorldPos;
  1333. vec3 transformAxis( vec3 direction ) //navvis->4dkk
  1334. {
  1335. float y = direction.y;
  1336. direction.y = direction.z;
  1337. direction.z = -y;
  1338. return direction;
  1339. }
  1340. void main() {
  1341. vWorldPos = vec3(-position.x, -position.y, position.z);
  1342. //vWorldPos = transformAxis(vWorldPos);
  1343. gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
  1344. }
  1345. `
  1346. Shaders["copyCubeMap.fs"] = `varying vec3 vWorldPos;
  1347. uniform float alpha;
  1348. uniform samplerCube tDiffuse;
  1349. void main() {
  1350. vec4 texColor = textureCube(tDiffuse, vWorldPos);
  1351. gl_FragColor = vec4(texColor.rgb, texColor.a * alpha);
  1352. }
  1353. `
  1354. Shaders["basicTextured.vs"] = `varying vec2 vUv;
  1355. void main() {
  1356. vUv = uv;
  1357. gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
  1358. }
  1359. `
  1360. Shaders["basicTextured.fs"] = `varying vec2 vUv;
  1361. uniform float alpha;
  1362. uniform sampler2D tDiffuse;
  1363. void main() {
  1364. vec4 texColor = texture2D(tDiffuse, vUv);
  1365. gl_FragColor = vec4(texColor.rgb, texColor.a * alpha);
  1366. }
  1367. `
  1368. export {Shaders};