pointcloud_new.vs 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  1. precision highp float;
  2. precision highp int;
  3. #define max_clip_polygons 8
  4. #define PI 3.141592653589793
  5. #if defined(usePanoMap)
  6. uniform samplerCube pano0Map; //随便设置一个samplerCube去使用都会让点云消失
  7. uniform samplerCube pano1Map;
  8. uniform float progress;
  9. uniform float easeInOutRatio;
  10. uniform vec3 pano0Position;
  11. uniform mat4 pano0Matrix;
  12. uniform vec3 pano1Position;
  13. uniform mat4 pano1Matrix;
  14. /*
  15. varying vec3 vWorldPosition0;
  16. varying vec3 vWorldPosition1;
  17. */
  18. #endif
  19. //--------------
  20. attribute vec3 position;
  21. attribute vec3 color;
  22. attribute float intensity;
  23. attribute float classification;
  24. attribute float returnNumber;
  25. attribute float numberOfReturns;
  26. attribute float pointSourceID;
  27. attribute vec4 indices; //每个点的index
  28. attribute float spacing;
  29. attribute float gpsTime;
  30. attribute vec3 normal;
  31. attribute float aExtra;
  32. uniform mat4 modelMatrix;
  33. uniform mat4 modelViewMatrix;
  34. uniform mat4 projectionMatrix;
  35. uniform mat4 viewMatrix;
  36. uniform mat4 uViewInv;
  37. //uniform float uScreenWidth;
  38. //uniform float uScreenHeight;
  39. uniform vec2 resolution;
  40. uniform float fov;
  41. uniform float near;
  42. uniform float far;
  43. uniform bool uDebug;
  44. uniform bool uUseOrthographicCamera;
  45. uniform float uOrthoWidth;
  46. uniform float uOrthoHeight;
  47. #define CLIPTASK_NONE 0
  48. #define CLIPTASK_HIGHLIGHT 1
  49. #define CLIPTASK_SHOW_INSIDE 2
  50. #define CLIPTASK_SHOW_OUTSIDE 3
  51. #define CLIPMETHOD_INSIDE_ANY 0
  52. #define CLIPMETHOD_INSIDE_ALL 1
  53. //最外层裁剪(下载)
  54. #if defined(bigClipInBox)
  55. uniform mat4 clipBoxBig_in;
  56. #endif
  57. //内层裁剪
  58. #if defined(num_in_clipboxes) && num_in_clipboxes > 0
  59. uniform mat4 clipBoxes_in[num_in_clipboxes];
  60. #endif
  61. #if defined(num_out_clipboxes) && num_out_clipboxes > 0
  62. uniform mat4 clipBoxes_out[num_out_clipboxes];
  63. #endif
  64. #if defined(num_clipspheres) && num_clipspheres > 0
  65. uniform mat4 uClipSpheres[num_clipspheres];
  66. #endif
  67. #if defined(num_highlightBox) && num_highlightBox > 0
  68. uniform mat4 boxes_highlight[num_highlightBox];
  69. #endif
  70. #if defined(num_prism) && num_prism > 0
  71. uniform mat3 prismList[num_prism];
  72. uniform vec2 prismPoints[prismPointCountSum];
  73. #endif
  74. uniform float size;
  75. uniform float minSize;
  76. uniform float maxSize;
  77. uniform float orthoMaxSize;//add
  78. uniform float uPCIndex;
  79. uniform float uOctreeSpacing;
  80. uniform float uNodeSpacing;
  81. uniform float uOctreeSize;
  82. uniform vec3 uBBSize;
  83. uniform float uLevel;
  84. uniform float levelPercent;//add
  85. uniform float uVNStart;
  86. uniform bool uIsLeafNode;
  87. uniform vec3 uColor;
  88. uniform float uOpacity;
  89. varying float vOpacity; //add
  90. uniform vec2 elevationRange;
  91. uniform vec2 intensityRange;
  92. uniform vec2 uFilterReturnNumberRange;
  93. uniform vec2 uFilterNumberOfReturnsRange;
  94. uniform vec2 uFilterPointSourceIDClipRange;
  95. uniform vec2 uFilterGPSTimeClipRange;
  96. //uniform float ufilterByNormalThreshold;
  97. uniform float uGpsScale;
  98. uniform float uGpsOffset;
  99. uniform vec2 uNormalizedGpsBufferRange;
  100. uniform vec3 uIntensity_gbc;
  101. uniform vec3 uRGB_gbc;
  102. uniform vec3 uExtra_gbc;
  103. uniform float uTransition;
  104. uniform float wRGB;
  105. uniform float wIntensity;
  106. uniform float wElevation;
  107. uniform float wClassification;
  108. uniform float wReturnNumber;
  109. uniform float wSourceID;
  110. uniform vec2 uExtraNormalizedRange;
  111. uniform vec2 uExtraRange;
  112. uniform float uExtraScale;
  113. uniform float uExtraOffset;
  114. uniform vec3 uShadowColor;
  115. uniform sampler2D visibleNodes;
  116. uniform sampler2D gradient;
  117. uniform sampler2D classificationLUT;
  118. #if defined(color_type_matcap)
  119. uniform sampler2D matcapTextureUniform;
  120. #endif
  121. uniform bool backfaceCulling;
  122. #if defined(num_shadowmaps) && num_shadowmaps > 0
  123. uniform sampler2D uShadowMap[num_shadowmaps];
  124. uniform mat4 uShadowWorldView[num_shadowmaps];
  125. uniform mat4 uShadowProj[num_shadowmaps];
  126. #endif
  127. varying vec3 vColor;
  128. varying float vLogDepth;
  129. varying vec3 vViewPosition;
  130. varying float vRadius;
  131. varying float vPointSize;
  132. float round(float number){
  133. return floor(number + 0.5);
  134. }
  135. //
  136. // ### ######## ### ######## ######## #### ## ## ######## ###### #### ######## ######## ######
  137. // ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
  138. // ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
  139. // ## ## ## ## ## ## ######## ## ## ## ## ###### ###### ## ## ###### ######
  140. // ######### ## ## ######### ## ## ## ## ## ## ## ## ## ## ##
  141. // ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
  142. // ## ## ######## ## ## ## ## #### ### ######## ###### #### ######## ######## ######
  143. //
  144. // ---------------------
  145. // OCTREE
  146. // ---------------------
  147. #if (defined(adaptive_point_size) || defined(color_type_level_of_detail)) && defined(tree_type_octree)
  148. /**
  149. * number of 1-bits up to inclusive index position
  150. * number is treated as if it were an integer in the range 0-255
  151. *
  152. */
  153. int numberOfOnes(int number, int index){
  154. int numOnes = 0;
  155. int tmp = 128;
  156. for(int i = 7; i >= 0; i--){
  157. if(number >= tmp){
  158. number = number - tmp;
  159. if(i <= index){
  160. numOnes++;
  161. }
  162. }
  163. tmp = tmp / 2;
  164. }
  165. return numOnes;
  166. }
  167. /**
  168. * checks whether the bit at index is 1
  169. * number is treated as if it were an integer in the range 0-255
  170. *
  171. */
  172. bool isBitSet(int number, int index){
  173. // weird multi else if due to lack of proper array, int and bitwise support in WebGL 1.0
  174. int powi = 1;
  175. if(index == 0){
  176. powi = 1;
  177. }else if(index == 1){
  178. powi = 2;
  179. }else if(index == 2){
  180. powi = 4;
  181. }else if(index == 3){
  182. powi = 8;
  183. }else if(index == 4){
  184. powi = 16;
  185. }else if(index == 5){
  186. powi = 32;
  187. }else if(index == 6){
  188. powi = 64;
  189. }else if(index == 7){
  190. powi = 128;
  191. }else{
  192. return false;
  193. }
  194. int ndp = number / powi;
  195. return mod(float(ndp), 2.0) != 0.0;
  196. }
  197. /**
  198. * find the LOD at the point position
  199. */
  200. float getLOD(){//////we use this
  201. vec3 offset = vec3(0.0, 0.0, 0.0);
  202. int iOffset = int(uVNStart);
  203. float depth = uLevel;
  204. for(float i = 0.0; i <= 30.0; i++){
  205. float nodeSizeAtLevel = uOctreeSize / pow(2.0, i + uLevel + 0.0);
  206. vec3 index3d = (position-offset) / nodeSizeAtLevel;
  207. index3d = floor(index3d + 0.5);
  208. int index = int(round(4.0 * index3d.x + 2.0 * index3d.y + index3d.z));
  209. vec4 value = texture2D(visibleNodes, vec2(float(iOffset) / 2048.0, 0.0));
  210. int mask = int(round(value.r * 255.0));
  211. if(isBitSet(mask, index)){
  212. // there are more visible child nodes at this position
  213. int advanceG = int(round(value.g * 255.0)) * 256;
  214. int advanceB = int(round(value.b * 255.0));
  215. int advanceChild = numberOfOnes(mask, index - 1);
  216. int advance = advanceG + advanceB + advanceChild;
  217. iOffset = iOffset + advance;
  218. depth++;
  219. }else{
  220. // no more visible child nodes at this position
  221. //return value.a * 255.0;
  222. float lodOffset = (255.0 * value.a) / 10.0 - 10.0;
  223. return depth + lodOffset;
  224. }
  225. offset = offset + (vec3(1.0, 1.0, 1.0) * nodeSizeAtLevel * 0.5) * index3d;
  226. }
  227. return depth;
  228. }
  229. float getSpacing(){
  230. vec3 offset = vec3(0.0, 0.0, 0.0);
  231. int iOffset = int(uVNStart);
  232. float depth = uLevel;
  233. float spacing = uNodeSpacing;
  234. for(float i = 0.0; i <= 30.0; i++){
  235. float nodeSizeAtLevel = uOctreeSize / pow(2.0, i + uLevel + 0.0);
  236. vec3 index3d = (position-offset) / nodeSizeAtLevel;
  237. index3d = floor(index3d + 0.5);
  238. int index = int(round(4.0 * index3d.x + 2.0 * index3d.y + index3d.z));
  239. vec4 value = texture2D(visibleNodes, vec2(float(iOffset) / 2048.0, 0.0));
  240. int mask = int(round(value.r * 255.0));
  241. float spacingFactor = value.a;
  242. if(i > 0.0){
  243. spacing = spacing / (255.0 * spacingFactor);
  244. }
  245. if(isBitSet(mask, index)){
  246. // there are more visible child nodes at this position
  247. int advanceG = int(round(value.g * 255.0)) * 256;
  248. int advanceB = int(round(value.b * 255.0));
  249. int advanceChild = numberOfOnes(mask, index - 1);
  250. int advance = advanceG + advanceB + advanceChild;
  251. iOffset = iOffset + advance;
  252. //spacing = spacing / (255.0 * spacingFactor);
  253. //spacing = spacing / 3.0;
  254. depth++;
  255. }else{
  256. // no more visible child nodes at this position
  257. return spacing;
  258. }
  259. offset = offset + (vec3(1.0, 1.0, 1.0) * nodeSizeAtLevel * 0.5) * index3d;
  260. }
  261. return spacing;
  262. }
  263. float getPointSizeAttenuation(){
  264. return pow(2.0, getLOD());
  265. }
  266. #endif
  267. // ---------------------
  268. // KD-TREE
  269. // ---------------------
  270. #if (defined(adaptive_point_size) || defined(color_type_level_of_detail)) && defined(tree_type_kdtree)
  271. float getLOD(){
  272. vec3 offset = vec3(0.0, 0.0, 0.0);
  273. float iOffset = 0.0;
  274. float depth = 0.0;
  275. vec3 size = uBBSize;
  276. vec3 pos = position;
  277. for(float i = 0.0; i <= 1000.0; i++){
  278. vec4 value = texture2D(visibleNodes, vec2(iOffset / 2048.0, 0.0));
  279. int children = int(value.r * 255.0);
  280. float next = value.g * 255.0;
  281. int split = int(value.b * 255.0);
  282. if(next == 0.0){
  283. return depth;
  284. }
  285. vec3 splitv = vec3(0.0, 0.0, 0.0);
  286. if(split == 1){
  287. splitv.x = 1.0;
  288. }else if(split == 2){
  289. splitv.y = 1.0;
  290. }else if(split == 4){
  291. splitv.z = 1.0;
  292. }
  293. iOffset = iOffset + next;
  294. float factor = length(pos * splitv / size);
  295. if(factor < 0.5){
  296. // left
  297. if(children == 0 || children == 2){
  298. return depth;
  299. }
  300. }else{
  301. // right
  302. pos = pos - size * splitv * 0.5;
  303. if(children == 0 || children == 1){
  304. return depth;
  305. }
  306. if(children == 3){
  307. iOffset = iOffset + 1.0;
  308. }
  309. }
  310. size = size * ((1.0 - (splitv + 1.0) / 2.0) + 0.5);
  311. depth++;
  312. }
  313. return depth;
  314. }
  315. float getPointSizeAttenuation(){
  316. return 0.5 * pow(1.3, getLOD());
  317. }
  318. #endif
  319. //
  320. // ### ######## ######## ######## #### ######## ## ## ######## ######## ######
  321. // ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
  322. // ## ## ## ## ## ## ## ## ## ## ## ## ## ##
  323. // ## ## ## ## ######## ## ######## ## ## ## ###### ######
  324. // ######### ## ## ## ## ## ## ## ## ## ## ## ##
  325. // ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
  326. // ## ## ## ## ## ## #### ######## ####### ## ######## ######
  327. //
  328. // formula adapted from: http://www.dfstudios.co.uk/articles/programming/image-programming-algorithms/image-processing-algorithms-part-5-contrast-adjustment/
  329. float getContrastFactor(float contrast){
  330. return (1.0158730158730156 * (contrast + 1.0)) / (1.0158730158730156 - contrast);
  331. }
  332. vec3 getRGB(){
  333. vec3 rgb = color;
  334. rgb = pow(rgb, vec3(uRGB_gbc.x));
  335. rgb = rgb + uRGB_gbc.y;
  336. rgb = (rgb - 0.5) * getContrastFactor(uRGB_gbc.z) + 0.5;
  337. rgb = clamp(rgb, 0.0, 1.0);
  338. return rgb;
  339. }
  340. float getIntensity(){
  341. float w = (intensity - intensityRange.x) / (intensityRange.y - intensityRange.x);
  342. w = pow(w, uIntensity_gbc.x);
  343. w = w + uIntensity_gbc.y;
  344. w = (w - 0.5) * getContrastFactor(uIntensity_gbc.z) + 0.5;
  345. w = clamp(w, 0.0, 1.0);
  346. return w;
  347. }
  348. vec3 getGpsTime(){
  349. float w = (gpsTime + uGpsOffset) * uGpsScale;
  350. vec3 c = texture2D(gradient, vec2(w, 1.0 - w)).rgb;
  351. // vec2 r = uNormalizedGpsBufferRange;
  352. // float w = gpsTime * (r.y - r.x) + r.x;
  353. // w = clamp(w, 0.0, 1.0);
  354. // vec3 c = texture2D(gradient, vec2(w,1.0-w)).rgb;
  355. return c;
  356. }
  357. vec3 getElevation(vec4 world){
  358. float w = (world.z - elevationRange.x) / (elevationRange.y - elevationRange.x);
  359. vec3 cElevation = texture2D(gradient, vec2(w,1.0-w)).rgb;
  360. return cElevation;
  361. }
  362. vec4 getClassification(){
  363. vec2 uv = vec2(classification / 255.0, 0.5);
  364. vec4 classColor = texture2D(classificationLUT, uv);
  365. return classColor;
  366. }
  367. vec3 getReturns(){
  368. // 0b 00_000_111
  369. float rn = mod(returnNumber, 8.0);
  370. // 0b 00_111_000
  371. float nr = mod(returnNumber / 8.0, 8.0);
  372. if(nr <= 1.0){
  373. return vec3(1.0, 0.0, 0.0);
  374. }else{
  375. return vec3(0.0, 1.0, 0.0);
  376. }
  377. // return vec3(nr / 4.0, 0.0, 0.0);
  378. // if(nr == 1.0){
  379. // return vec3(1.0, 1.0, 0.0);
  380. // }else{
  381. // if(rn == 1.0){
  382. // return vec3(1.0, 0.0, 0.0);
  383. // }else if(rn == nr){
  384. // return vec3(0.0, 0.0, 1.0);
  385. // }else{
  386. // return vec3(0.0, 1.0, 0.0);
  387. // }
  388. // }
  389. // if(numberOfReturns == 1.0){
  390. // return vec3(1.0, 1.0, 0.0);
  391. // }else{
  392. // if(returnNumber == 1.0){
  393. // return vec3(1.0, 0.0, 0.0);
  394. // }else if(returnNumber == numberOfReturns){
  395. // return vec3(0.0, 0.0, 1.0);
  396. // }else{
  397. // return vec3(0.0, 1.0, 0.0);
  398. // }
  399. // }
  400. }
  401. vec3 getReturnNumber(){
  402. if(numberOfReturns == 1.0){
  403. return vec3(1.0, 1.0, 0.0);
  404. }else{
  405. if(returnNumber == 1.0){
  406. return vec3(1.0, 0.0, 0.0);
  407. }else if(returnNumber == numberOfReturns){
  408. return vec3(0.0, 0.0, 1.0);
  409. }else{
  410. return vec3(0.0, 1.0, 0.0);
  411. }
  412. }
  413. }
  414. vec3 getNumberOfReturns(){
  415. float value = numberOfReturns;
  416. float w = value / 6.0;
  417. vec3 color = texture2D(gradient, vec2(w, 1.0 - w)).rgb;
  418. return color;
  419. }
  420. vec3 getSourceID(){
  421. float w = mod(pointSourceID, 10.0) / 10.0;
  422. return texture2D(gradient, vec2(w,1.0 - w)).rgb;
  423. }
  424. vec3 getCompositeColor(vec4 world){
  425. vec3 c;
  426. float w;
  427. c += wRGB * getRGB();
  428. w += wRGB;
  429. c += wIntensity * getIntensity() * vec3(1.0, 1.0, 1.0);
  430. w += wIntensity;
  431. c += wElevation * getElevation(world);
  432. w += wElevation;
  433. c += wReturnNumber * getReturnNumber();
  434. w += wReturnNumber;
  435. c += wSourceID * getSourceID();
  436. w += wSourceID;
  437. vec4 cl = wClassification * getClassification();
  438. c += cl.a * cl.rgb;
  439. w += wClassification * cl.a;
  440. c = c / w;
  441. if(w == 0.0){
  442. //c = color;
  443. gl_Position = vec4(100.0, 100.0, 100.0, 0.0);
  444. }
  445. return c;
  446. }
  447. vec3 getNormal(){
  448. //vec3 n_hsv = vec3( modelMatrix * vec4( normal, 0.0 )) * 0.5 + 0.5; // (n_world.xyz + vec3(1.,1.,1.)) / 2.;
  449. vec3 n_view = normalize( vec3(modelViewMatrix * vec4( normal, 0.0 )) );
  450. return n_view;
  451. }
  452. bool applyBackfaceCulling() {
  453. // Black not facing vertices / Backface culling
  454. vec3 e = normalize(vec3(modelViewMatrix * vec4( position, 1. )));
  455. vec3 n = getNormal(); // normalize( vec3(modelViewMatrix * vec4( normal, 0.0 )) );
  456. if((uUseOrthographicCamera && n.z <= 0.) || (!uUseOrthographicCamera && dot( n, e ) >= 0.)) {
  457. return true;
  458. } else {
  459. return false;
  460. }
  461. }
  462. #if defined(color_type_matcap)
  463. // Matcap Material
  464. vec3 getMatcap(){
  465. vec3 eye = normalize( vec3( modelViewMatrix * vec4( position, 1. ) ) );
  466. if(uUseOrthographicCamera) {
  467. eye = vec3(0., 0., -1.);
  468. }
  469. vec3 r_en = reflect( eye, getNormal() ); // or r_en = e - 2. * dot( n, e ) * n;
  470. float m = 2. * sqrt(pow( r_en.x, 2. ) + pow( r_en.y, 2. ) + pow( r_en.z + 1., 2. ));
  471. vec2 vN = r_en.xy / m + .5;
  472. return texture2D(matcapTextureUniform, vN).rgb;
  473. }
  474. #endif
  475. vec3 getExtra(){
  476. float w = (aExtra + uExtraOffset) * uExtraScale;
  477. w = clamp(w, 0.0, 1.0);
  478. vec3 color = texture2D(gradient, vec2(w,1.0-w)).rgb;
  479. // vec2 r = uExtraNormalizedRange;
  480. // float w = aExtra * (r.y - r.x) + r.x;
  481. // w = (w - uExtraRange.x) / (uExtraRange.y - uExtraRange.x);
  482. // w = clamp(w, 0.0, 1.0);
  483. // vec3 color = texture2D(gradient, vec2(w,1.0-w)).rgb;
  484. return color;
  485. }
  486. vec3 getColor(vec4 world){
  487. vec3 color;
  488. #ifdef color_type_rgba
  489. color = getRGB();
  490. #elif defined color_type_height || defined color_type_elevation
  491. color = getElevation(world);
  492. #elif defined color_type_rgb_height
  493. vec3 cHeight = getElevation();
  494. color = (1.0 - uTransition) * getRGB() + uTransition * cHeight;
  495. #elif defined color_type_depth
  496. float linearDepth = gl_Position.w;
  497. float expDepth = (gl_Position.z / gl_Position.w) * 0.5 + 0.5;
  498. color = vec3(linearDepth, expDepth, 0.0);
  499. //color = vec3(1.0, 0.5, 0.3);
  500. #elif defined color_type_intensity
  501. float w = getIntensity();
  502. color = vec3(w, w, w);
  503. #elif defined color_type_gps_time
  504. color = getGpsTime();
  505. #elif defined color_type_intensity_gradient
  506. float w = getIntensity();
  507. color = texture2D(gradient, vec2(w,1.0-w)).rgb;
  508. #elif defined color_type_color
  509. color = uColor;
  510. #elif defined color_type_level_of_detail
  511. float depth = getLOD();
  512. float w = depth / 10.0;
  513. color = texture2D(gradient, vec2(w,1.0-w)).rgb;
  514. #elif defined color_type_indices
  515. color = indices.rgb;
  516. #elif defined color_type_classification
  517. vec4 cl = getClassification();
  518. color = cl.rgb;
  519. #elif defined color_type_return_number
  520. color = getReturnNumber();
  521. #elif defined color_type_returns
  522. color = getReturns();
  523. #elif defined color_type_number_of_returns
  524. color = getNumberOfReturns();
  525. #elif defined color_type_source_id
  526. color = getSourceID();
  527. #elif defined color_type_point_source_id
  528. color = getSourceID();
  529. #elif defined color_type_normal
  530. color = (modelMatrix * vec4(normal, 0.0)).xyz;
  531. #elif defined color_type_phong
  532. color = color;
  533. #elif defined color_type_composite
  534. color = getCompositeColor(world);
  535. #elif defined color_type_matcap
  536. color = getMatcap();
  537. #else
  538. color = getExtra();
  539. #endif
  540. if (backfaceCulling && applyBackfaceCulling()){
  541. //color = vec3(0.);
  542. }
  543. //applyBackfaceCulling直接返回false或者注释color = vec3(0.);都没问题
  544. return color;
  545. }
  546. float getPointSize(){
  547. float pointSize = 1.0;
  548. float maxSize_ = maxSize;
  549. float slope = tan(fov / 2.0);
  550. float projFactor = -0.5 * resolution.y / (slope * vViewPosition.z);
  551. /*
  552. float scale = length(
  553. modelViewMatrix * vec4(0, 0, 0, 1) -
  554. modelViewMatrix * vec4(uOctreeSpacing, 0, 0, 1)
  555. ) / uOctreeSpacing;
  556. projFactor = projFactor * scale;
  557. */
  558. float r = uOctreeSpacing * 1.7;
  559. //vRadius = r;
  560. #if defined fixed_point_size
  561. pointSize = size;
  562. #elif defined attenuated_point_size
  563. if(uUseOrthographicCamera){
  564. pointSize = size / uOrthoWidth * resolution.x; //改成近似adaptive_point_size根据窗口缩放。其实就是size * zoom (size单位转化为米,如size为1代表一个点云在场景中1米宽,zoom代表1px=1/zoom米)
  565. maxSize_ = orthoMaxSize; //for panoEditor, when zoom in, need more details, rather than always same size
  566. }else{ //近大远小,模拟真实mesh,边缘放大
  567. //pointSize = size * spacing * projFactor; //spacing是attribute 为空 如果有这个值就能更自适应填补
  568. //pointSize = size * uOctreeSpacing * projFactor / 18.0; //直接用cloud的spacing里,不过因为都一样所以可能没有什么意义
  569. //pointSize = pointSize * projFactor;
  570. pointSize = size * projFactor ;
  571. }
  572. #elif defined adaptive_point_size
  573. if(uUseOrthographicCamera) {
  574. float worldSpaceSize = 1.0 * size * r / getPointSizeAttenuation();
  575. pointSize = (worldSpaceSize / uOrthoWidth) * resolution.x; //uScreenWidth;
  576. maxSize_ = orthoMaxSize;
  577. } else {
  578. float worldSpaceSize = 1.0 * size * r / getPointSizeAttenuation();
  579. pointSize = worldSpaceSize * projFactor;
  580. }
  581. #endif
  582. pointSize = max(minSize, pointSize);
  583. pointSize = min(maxSize_, pointSize);
  584. vRadius = pointSize / projFactor;
  585. return pointSize;
  586. }
  587. bool insideBox(mat4 clipBox, vec4 worldPos){//add
  588. vec4 clipPosition = clipBox * worldPos;
  589. bool inside = -0.5 <= clipPosition.x && clipPosition.x <= 0.5;
  590. inside = inside && -0.5 <= clipPosition.y && clipPosition.y <= 0.5;
  591. inside = inside && -0.5 <= clipPosition.z && clipPosition.z <= 0.5;
  592. return inside;
  593. }
  594. #if defined(num_prism) && num_prism > 0
  595. #if defined(color_type_prismHeight)
  596. vec3 percentToByte(float num){
  597. //输出是0-1, shader精度不够,只够存3个数
  598. float a = 1.0;
  599. float result[4];
  600. for(int i=0;i<3;i++){
  601. a = i == 2 ? a/255.0 : a / 256.0 ;
  602. //分成256份,其中255份给当前位置,最后一份给后一个位置,而到了最后一个位置时没有下一个位置了所以只分成255份
  603. if(num > a){
  604. float c = num / a;
  605. float r = floor(c);
  606. r = min(255.0, r);
  607. result[i] = r;
  608. num -= r * a;
  609. }else{
  610. result[i] = 0.0;
  611. }
  612. }
  613. return vec3(result[0]/255.0, result[1]/255.0,result[2]/255.0);
  614. //除以多少255还是256? 参考uPCIndex / 255.0 应该是255
  615. }
  616. #endif
  617. int insidePrism(mat3 prismInfo, int pointIndexStart, vec4 worldPos){//是否在棱柱里
  618. float zMin = prismInfo[0][0];
  619. float zMid = prismInfo[0][1];
  620. float zMax = prismInfo[0][2];
  621. float xMin = prismInfo[1][0];
  622. float xMax = prismInfo[1][1];
  623. float yMin = prismInfo[1][2];
  624. float yMax = prismInfo[2][0];
  625. int pointCount = int(round(prismInfo[2][1]));
  626. if(worldPos.z < zMin || worldPos.z > zMax || worldPos.x < xMin || worldPos.x > xMax || worldPos.y < yMin || worldPos.y > yMax)return 0;
  627. bool inside = false;
  628. int j=pointCount-1;
  629. for(int i=0; i<prism_maxPointsCount; i++){
  630. if(i>=pointCount)break;
  631. float xi = prismPoints[i+pointIndexStart].x, yi = prismPoints[i+pointIndexStart].y, xj = prismPoints[j+pointIndexStart].x, yj = prismPoints[j+pointIndexStart].y;
  632. if(((yi > worldPos.y) != (yj > worldPos.y)) && (worldPos.x < (xj - xi) * (worldPos.y - yi) / (yj - yi) + xi)){
  633. inside = !inside;
  634. }
  635. j=i;
  636. }
  637. if(inside){
  638. #if defined(color_type_prismHeight)
  639. vColor = percentToByte((worldPos.z - zMin) / (zMax - zMin));
  640. #endif
  641. return worldPos.z < zMid ? 1 : 2;
  642. }else return 0;
  643. }
  644. #endif
  645. void doClipping(vec4 world){
  646. {
  647. vec4 cl = getClassification();
  648. if(cl.a == 0.0){
  649. gl_Position = vec4(100.0, 100.0, 100.0, 0.0);
  650. return;
  651. }
  652. }
  653. #if defined(clip_return_number_enabled)
  654. { // return number filter
  655. vec2 range = uFilterReturnNumberRange;
  656. if(returnNumber < range.x || returnNumber > range.y){
  657. gl_Position = vec4(100.0, 100.0, 100.0, 0.0);
  658. return;
  659. }
  660. }
  661. #endif
  662. #if defined(clip_number_of_returns_enabled)
  663. { // number of return filter
  664. vec2 range = uFilterNumberOfReturnsRange;
  665. if(numberOfReturns < range.x || numberOfReturns > range.y){
  666. gl_Position = vec4(100.0, 100.0, 100.0, 0.0);
  667. return;
  668. }
  669. }
  670. #endif
  671. #if defined(clip_gps_enabled)
  672. { // GPS time filter
  673. float time = (gpsTime + uGpsOffset) * uGpsScale;
  674. vec2 range = uFilterGPSTimeClipRange;
  675. if(time < range.x || time > range.y){
  676. gl_Position = vec4(100.0, 100.0, 100.0, 0.0);
  677. return;
  678. }
  679. }
  680. #endif
  681. #if defined(clip_point_source_id_enabled)
  682. { // point source id filter
  683. vec2 range = uFilterPointSourceIDClipRange;
  684. if(pointSourceID < range.x || pointSourceID > range.y){
  685. gl_Position = vec4(100.0, 100.0, 100.0, 0.0);
  686. return;
  687. }
  688. }
  689. #endif
  690. //总共三种box : 最外层可见、内层可见和不可见(外层可见和内层可见是交集,内层可见和内层不可见是并集)
  691. #if defined(bigClipInBox)
  692. if(!insideBox(clipBoxBig_in, world)){
  693. gl_Position = vec4(100.0, 100.0, 100.0, 1.0);
  694. return;;
  695. }
  696. #endif
  697. #if defined(num_in_clipboxes) && num_in_clipboxes > 0
  698. //当有可见box时,需要在任一可见box内才可见
  699. bool visi1 = false;
  700. for(int i = 0; i < num_in_clipboxes; i++){
  701. if(insideBox(clipBoxes_in[i], world)){
  702. visi1 = true;
  703. break;
  704. }
  705. }
  706. if(!visi1){
  707. gl_Position = vec4(100.0, 100.0, 100.0, 1.0);
  708. return;
  709. }
  710. #endif
  711. #if defined(num_out_clipboxes) && num_out_clipboxes > 0
  712. //当有不可见box时,不在所有不可见box内才可见
  713. bool visi2 = true;
  714. for(int i = 0; i < num_out_clipboxes; i++){
  715. if(insideBox(clipBoxes_out[i], world)){
  716. visi2 = false;
  717. break;
  718. }
  719. }
  720. if(!visi2){
  721. gl_Position = vec4(100.0, 100.0, 100.0, 1.0);
  722. return;
  723. }
  724. #endif
  725. #if defined(num_highlightBox) && num_highlightBox > 0
  726. //当有高亮box时,需要在任一可见高亮内都高宽
  727. bool highlight = false;
  728. for(int i = 0; i < num_highlightBox; i++){
  729. if(insideBox(boxes_highlight[i], world)){
  730. highlight = true;
  731. break;
  732. }
  733. }
  734. if(highlight){
  735. vColor.r += 0.5;
  736. }
  737. #endif
  738. #if defined(num_prism) && num_prism > 0
  739. int highlight = 0;
  740. int pointIndexStart = 0;
  741. for(int i = 0; i < num_prism; i++){
  742. highlight = insidePrism(prismList[i], pointIndexStart, world);
  743. if(highlight>0){
  744. #ifndef color_type_prismHeight
  745. if(highlight == 1){
  746. vColor.r += 0.5;
  747. }else if(highlight == 2){
  748. vColor.g += 0.5;
  749. }
  750. #endif
  751. break;
  752. }
  753. pointIndexStart += int(round(prismList[i][2][1]));
  754. }
  755. #ifdef color_type_prismHeight
  756. if(highlight==0){
  757. gl_Position = vec4(100.0, 100.0, 100.0, 1.0);
  758. }
  759. #endif
  760. #endif
  761. }
  762. //
  763. // ## ## ### #### ## ##
  764. // ### ### ## ## ## ### ##
  765. // #### #### ## ## ## #### ##
  766. // ## ### ## ## ## ## ## ## ##
  767. // ## ## ######### ## ## ####
  768. // ## ## ## ## ## ## ###
  769. // ## ## ## ## #### ## ##
  770. //
  771. vec2 getSamplerCoord( vec3 direction )
  772. {
  773. direction = normalize(direction);
  774. float tx=atan(direction.x,-direction.y)/(PI*2.0)+0.5;
  775. float ty=acos(direction.z)/PI;
  776. return vec2(tx,ty);
  777. }
  778. vec3 transformAxis( vec3 direction ) //navvis->4dkk
  779. {
  780. float y = direction.y;
  781. direction.y = direction.z;
  782. direction.z = -y;
  783. return direction;
  784. }
  785. void main() {
  786. //bool filtered_by_normal = false;
  787. float normalZ = 0.0;
  788. #ifdef use_filter_by_normal
  789. /*if(abs(getNormal().z) > 0.4) { //ufilterByNormalThreshold 暂定 3
  790. // Move point outside clip space space to discard it.
  791. //gl_Position = vec4(0.0, 0.0, 2.0, 1.0); //gl_Position的可视区域是 x,y,z都是[-1,1]
  792. //return;
  793. //filtered_by_normal = true; //标记一下。不直接不绘制,因为有的法线都是垂直向上
  794. }*/
  795. normalZ = abs(getNormal().z);
  796. #endif
  797. vec4 worldPos = modelMatrix * vec4(position, 1.0);
  798. vec4 mvPosition = modelViewMatrix * vec4(position, 1.0 );
  799. vViewPosition = mvPosition.xyz;
  800. gl_Position = projectionMatrix * mvPosition;
  801. vLogDepth = log2(-mvPosition.z);
  802. // COLOR
  803. //加-------------------
  804. #if defined(usePanoMap)
  805. vec3 positionLocalToPanoCenter0 = worldPos.xyz - pano0Position;
  806. vec3 vWorldPosition0 = (vec4(positionLocalToPanoCenter0, 1.0) * pano0Matrix).xyz;
  807. vWorldPosition0.x *= -1.0;
  808. vWorldPosition0 = transformAxis(vWorldPosition0);
  809. vec3 positionLocalToPanoCenter1 = worldPos.xyz - pano1Position;
  810. vec3 vWorldPosition1 = (vec4(positionLocalToPanoCenter1, 1.0) * pano1Matrix).xyz;
  811. vWorldPosition1.x *= -1.0;
  812. vWorldPosition1 = transformAxis(vWorldPosition1);
  813. /*
  814. vec2 samplerCoord0 = getSamplerCoord(vWorldPosition0.xyz);
  815. vec2 samplerCoord1 = getSamplerCoord(vWorldPosition1.xyz);
  816. vec4 colorFromPano0 = texture2D(pano0Map,samplerCoord0);
  817. vec4 colorFromPano1 = texture2D(pano1Map,samplerCoord1);
  818. */
  819. vec4 colorFromPano0=textureCube(pano0Map,vWorldPosition0.xyz);
  820. vec4 colorFromPano1=textureCube(pano1Map,vWorldPosition1.xyz);
  821. vColor = mix(colorFromPano0,colorFromPano1,progress).xyz;
  822. //float easeInOutRatio = 0.0; //缓冲,渐变点云到贴图的颜色
  823. if(progress < easeInOutRatio){
  824. float easeProgress = (easeInOutRatio - progress) / easeInOutRatio;
  825. vec3 vColor1 = getColor(worldPos);
  826. vColor = mix(vColor,vColor1,easeProgress);
  827. }else if(progress > 1.0 - easeInOutRatio){
  828. float easeProgress = (progress - (1.0 - easeInOutRatio) ) / easeInOutRatio;
  829. vec3 vColor1 = getColor(worldPos);
  830. vColor = mix(vColor,vColor1,easeProgress);
  831. }
  832. #else
  833. vColor = getColor(worldPos);
  834. #endif
  835. //-------------------
  836. if(uOpacity>=1.0 || uUseOrthographicCamera){
  837. vOpacity = uOpacity;
  838. if(uOpacity<1.0){ //uUseOrthographicCamera
  839. //防止缩小后点云几乎看不见
  840. vOpacity *= 4.0-3.0 * levelPercent;
  841. }
  842. }else{ //#ifdef attenuated_opacity zoom不会改变z 所以这并不是用在分屏时候的
  843. float v = -gl_Position.z-1.0 ; // 范围从-2到0, e的-2到0次方的范围是0.818到1
  844. //vOpacity = uOpacity * exp(v/ (levelPercent * 20.0 ) );
  845. //近处加深,远处变淡 gl_Position.z似乎朝向屏幕里为正
  846. float r = clamp( pow(1.1, v/10.0 ), 0.1, 1.0 ); //除以的数字越大,近高远低的程度越小,范围越长。 程度太高远处单薄的区域看不见 pow的底数越大改变率越大
  847. vOpacity = uOpacity * r ;
  848. }
  849. vOpacity *= max(0.3, pow((1.0 - normalZ),5.0));//垂直朝相机时降低透明度
  850. //vOpacity = clamp(vOpacity, 0.0, 1.0);
  851. // POINT SIZE
  852. float pointSize = getPointSize();
  853. gl_PointSize = pointSize;
  854. vPointSize = pointSize;
  855. // only for "replacing" approaches
  856. // if(getLOD() != uLevel){
  857. // gl_Position = vec4(10.0, 10.0, 10.0, 1.0);
  858. // }
  859. #if defined hq_depth_pass
  860. float originalDepth = gl_Position.w;
  861. float adjustedDepth = originalDepth + 2.0 * vRadius;
  862. float adjust = adjustedDepth / originalDepth;
  863. mvPosition.xyz = mvPosition.xyz * adjust;
  864. gl_Position = projectionMatrix * mvPosition;
  865. #endif
  866. // CLIPPING
  867. doClipping(worldPos);
  868. #if defined(num_clipspheres) && num_clipspheres > 0
  869. for(int i = 0; i < num_clipspheres; i++){
  870. vec4 sphereLocal = uClipSpheres[i] * mvPosition;
  871. float distance = length(sphereLocal.xyz);
  872. if(distance < 1.0){
  873. float w = distance;
  874. vec3 cGradient = texture2D(gradient, vec2(w, 1.0 - w)).rgb;
  875. vColor = cGradient;
  876. //vColor = cGradient * 0.7 + vColor * 0.3;
  877. }
  878. }
  879. #endif
  880. #if defined(num_shadowmaps) && num_shadowmaps > 0
  881. const float sm_near = 0.1;
  882. const float sm_far = 10000.0;
  883. for(int i = 0; i < num_shadowmaps; i++){
  884. vec3 viewPos = (uShadowWorldView[i] * vec4(position, 1.0)).xyz;
  885. float distanceToLight = abs(viewPos.z);
  886. vec4 projPos = uShadowProj[i] * uShadowWorldView[i] * vec4(position, 1);
  887. vec3 nc = projPos.xyz / projPos.w;
  888. float u = nc.x * 0.5 + 0.5;
  889. float v = nc.y * 0.5 + 0.5;
  890. vec2 sampleStep = vec2(1.0 / (2.0*1024.0), 1.0 / (2.0*1024.0)) * 1.5;
  891. vec2 sampleLocations[9];
  892. sampleLocations[0] = vec2(0.0, 0.0);
  893. sampleLocations[1] = sampleStep;
  894. sampleLocations[2] = -sampleStep;
  895. sampleLocations[3] = vec2(sampleStep.x, -sampleStep.y);
  896. sampleLocations[4] = vec2(-sampleStep.x, sampleStep.y);
  897. sampleLocations[5] = vec2(0.0, sampleStep.y);
  898. sampleLocations[6] = vec2(0.0, -sampleStep.y);
  899. sampleLocations[7] = vec2(sampleStep.x, 0.0);
  900. sampleLocations[8] = vec2(-sampleStep.x, 0.0);
  901. float visibleSamples = 0.0;
  902. float numSamples = 0.0;
  903. float bias = vRadius * 2.0;
  904. for(int j = 0; j < 9; j++){
  905. vec4 depthMapValue = texture2D(uShadowMap[i], vec2(u, v) + sampleLocations[j]);
  906. float linearDepthFromSM = depthMapValue.x + bias;
  907. float linearDepthFromViewer = distanceToLight;
  908. if(linearDepthFromSM > linearDepthFromViewer){
  909. visibleSamples += 1.0;
  910. }
  911. numSamples += 1.0;
  912. }
  913. float visibility = visibleSamples / numSamples;
  914. 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){
  915. //vColor = vec3(0.0, 0.0, 0.2);
  916. }else{
  917. //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);
  918. vColor = vColor * visibility + vColor * uShadowColor * (1.0 - visibility);
  919. }
  920. }
  921. #endif
  922. }