pointcloud_new.vs 31 KB

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