pointcloud.vs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. precision highp float;
  2. precision highp int;
  3. #define max_clip_polygons 8
  4. #define PI 3.141592653589793
  5. //--------------
  6. //varying vec3 vWorldPosition0;
  7. //varying vec3 vWorldPosition1;
  8. uniform vec3 pano0Position;
  9. uniform mat4 pano0Matrix;
  10. uniform vec3 pano1Position;
  11. uniform mat4 pano1Matrix;
  12. uniform int usePanoMap;
  13. /*uniform sampler2D pano0Map;
  14. uniform sampler2D pano1Map;
  15. uniform samplerCube pano0Map; //随便设置一个samplerCube去使用都会让点云消失
  16. uniform samplerCube pano1Map;*/
  17. #if defined(usePanoMap)
  18. uniform samplerCube pano0Map; //随便设置一个samplerCube去使用都会让点云消失
  19. uniform samplerCube pano1Map;
  20. #endif
  21. uniform float progress;
  22. //--------------
  23. attribute vec3 position;
  24. attribute vec3 color;
  25. attribute float intensity;
  26. attribute float classification;
  27. attribute float returnNumber;
  28. attribute float numberOfReturns;
  29. attribute float pointSourceID;
  30. attribute vec4 indices;
  31. attribute float spacing;
  32. attribute float gpsTime;
  33. attribute vec3 normal;
  34. attribute float aExtra;
  35. uniform mat4 modelMatrix;
  36. uniform mat4 modelViewMatrix;
  37. uniform mat4 projectionMatrix;
  38. uniform mat4 viewMatrix;
  39. uniform mat4 uViewInv;
  40. uniform float uScreenWidth;
  41. uniform float uScreenHeight;
  42. uniform float fov;
  43. uniform float near;
  44. uniform float far;
  45. uniform float easeInOutRatio;
  46. uniform bool uDebug;
  47. uniform bool uUseOrthographicCamera;
  48. uniform float uOrthoWidth;
  49. uniform float uOrthoHeight;
  50. #define CLIPTASK_NONE 0
  51. #define CLIPTASK_HIGHLIGHT 1
  52. #define CLIPTASK_SHOW_INSIDE 2
  53. #define CLIPTASK_SHOW_OUTSIDE 3
  54. #define CLIPMETHOD_INSIDE_ANY 0
  55. #define CLIPMETHOD_INSIDE_ALL 1
  56. uniform int clipTask;
  57. uniform int clipMethod;
  58. #if defined(num_clipboxes) && num_clipboxes > 0
  59. uniform mat4 clipBoxes[num_clipboxes];
  60. #endif
  61. #if defined(num_clipspheres) && num_clipspheres > 0
  62. uniform mat4 uClipSpheres[num_clipspheres];
  63. #endif
  64. #if defined(num_clippolygons) && num_clippolygons > 0
  65. uniform int uClipPolygonVCount[num_clippolygons];
  66. uniform vec3 uClipPolygonVertices[num_clippolygons * 8];
  67. uniform mat4 uClipPolygonWVP[num_clippolygons];
  68. #endif
  69. uniform float size;
  70. uniform float minSize;
  71. uniform float maxSize;
  72. uniform float uPCIndex;
  73. uniform float uOctreeSpacing;
  74. uniform float uNodeSpacing;
  75. uniform float uOctreeSize;
  76. uniform vec3 uBBSize;
  77. uniform float uLevel;
  78. uniform float uVNStart;
  79. uniform bool uIsLeafNode;
  80. uniform vec3 uColor;
  81. uniform float uOpacity;
  82. uniform vec2 elevationRange;
  83. uniform vec2 intensityRange;
  84. uniform vec2 uFilterReturnNumberRange;
  85. uniform vec2 uFilterNumberOfReturnsRange;
  86. uniform vec2 uFilterPointSourceIDClipRange;
  87. uniform vec2 uFilterGPSTimeClipRange;
  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. return color;
  536. }
  537. float getPointSize(){
  538. float pointSize = 1.0;
  539. float slope = tan(fov / 2.0);
  540. float projFactor = -0.5 * uScreenHeight / (slope * vViewPosition.z);
  541. float scale = length(
  542. modelViewMatrix * vec4(0, 0, 0, 1) -
  543. modelViewMatrix * vec4(uOctreeSpacing, 0, 0, 1)
  544. ) / uOctreeSpacing;
  545. projFactor = projFactor * scale;
  546. float r = uOctreeSpacing * 1.7;
  547. vRadius = r;
  548. #if defined fixed_point_size
  549. pointSize = size;
  550. #elif defined attenuated_point_size
  551. if(uUseOrthographicCamera){
  552. pointSize = size;
  553. }else{ //近大远小,模拟真实mesh,边缘放大
  554. //pointSize = size * spacing * projFactor; //spacing是attribute 为空 如果有这个值就能更自适应填补
  555. pointSize = size * uOctreeSpacing * projFactor / 18.0; //直接用cloud的spacing里,不过因为都一样所以可能没有什么意义
  556. //pointSize = pointSize * projFactor;
  557. }
  558. #elif defined adaptive_point_size
  559. if(uUseOrthographicCamera) {
  560. float worldSpaceSize = 1.0 * size * r / getPointSizeAttenuation();
  561. pointSize = (worldSpaceSize / uOrthoWidth) * uScreenWidth;
  562. } else {
  563. float worldSpaceSize = 1.0 * size * r / getPointSizeAttenuation();
  564. pointSize = worldSpaceSize * projFactor;
  565. }
  566. #endif
  567. pointSize = max(minSize, pointSize);
  568. pointSize = min(maxSize, pointSize);
  569. vRadius = pointSize / projFactor;
  570. return pointSize;
  571. }
  572. #if defined(num_clippolygons) && num_clippolygons > 0
  573. bool pointInClipPolygon(vec3 point, int polyIdx) {
  574. mat4 wvp = uClipPolygonWVP[polyIdx];
  575. //vec4 screenClipPos = uClipPolygonVP[polyIdx] * modelMatrix * vec4(point, 1.0);
  576. //screenClipPos.xy = screenClipPos.xy / screenClipPos.w * 0.5 + 0.5;
  577. vec4 pointNDC = wvp * vec4(point, 1.0);
  578. pointNDC.xy = pointNDC.xy / pointNDC.w;
  579. int j = uClipPolygonVCount[polyIdx] - 1;
  580. bool c = false;
  581. for(int i = 0; i < 8; i++) {
  582. if(i == uClipPolygonVCount[polyIdx]) {
  583. break;
  584. }
  585. //vec4 verti = wvp * vec4(uClipPolygonVertices[polyIdx * 8 + i], 1);
  586. //vec4 vertj = wvp * vec4(uClipPolygonVertices[polyIdx * 8 + j], 1);
  587. //verti.xy = verti.xy / verti.w;
  588. //vertj.xy = vertj.xy / vertj.w;
  589. //verti.xy = verti.xy / verti.w * 0.5 + 0.5;
  590. //vertj.xy = vertj.xy / vertj.w * 0.5 + 0.5;
  591. vec3 verti = uClipPolygonVertices[polyIdx * 8 + i];
  592. vec3 vertj = uClipPolygonVertices[polyIdx * 8 + j];
  593. if( ((verti.y > pointNDC.y) != (vertj.y > pointNDC.y)) &&
  594. (pointNDC.x < (vertj.x-verti.x) * (pointNDC.y-verti.y) / (vertj.y-verti.y) + verti.x) ) {
  595. c = !c;
  596. }
  597. j = i;
  598. }
  599. return c;
  600. }
  601. #endif
  602. void doClipping(){
  603. {
  604. vec4 cl = getClassification();
  605. if(cl.a == 0.0){
  606. gl_Position = vec4(100.0, 100.0, 100.0, 0.0);
  607. return;
  608. }
  609. }
  610. #if defined(clip_return_number_enabled)
  611. { // return number filter
  612. vec2 range = uFilterReturnNumberRange;
  613. if(returnNumber < range.x || returnNumber > range.y){
  614. gl_Position = vec4(100.0, 100.0, 100.0, 0.0);
  615. return;
  616. }
  617. }
  618. #endif
  619. #if defined(clip_number_of_returns_enabled)
  620. { // number of return filter
  621. vec2 range = uFilterNumberOfReturnsRange;
  622. if(numberOfReturns < range.x || numberOfReturns > range.y){
  623. gl_Position = vec4(100.0, 100.0, 100.0, 0.0);
  624. return;
  625. }
  626. }
  627. #endif
  628. #if defined(clip_gps_enabled)
  629. { // GPS time filter
  630. float time = (gpsTime + uGpsOffset) * uGpsScale;
  631. vec2 range = uFilterGPSTimeClipRange;
  632. if(time < range.x || time > range.y){
  633. gl_Position = vec4(100.0, 100.0, 100.0, 0.0);
  634. return;
  635. }
  636. }
  637. #endif
  638. #if defined(clip_point_source_id_enabled)
  639. { // point source id filter
  640. vec2 range = uFilterPointSourceIDClipRange;
  641. if(pointSourceID < range.x || pointSourceID > range.y){
  642. gl_Position = vec4(100.0, 100.0, 100.0, 0.0);
  643. return;
  644. }
  645. }
  646. #endif
  647. int clipVolumesCount = 0;
  648. int insideCount = 0;
  649. #if defined(num_clipboxes) && num_clipboxes > 0
  650. for(int i = 0; i < num_clipboxes; i++){
  651. vec4 clipPosition = clipBoxes[i] * modelMatrix * vec4( position, 1.0 );
  652. bool inside = -0.5 <= clipPosition.x && clipPosition.x <= 0.5;
  653. inside = inside && -0.5 <= clipPosition.y && clipPosition.y <= 0.5;
  654. inside = inside && -0.5 <= clipPosition.z && clipPosition.z <= 0.5;
  655. insideCount = insideCount + (inside ? 1 : 0);
  656. clipVolumesCount++;
  657. }
  658. #endif
  659. #if defined(num_clippolygons) && num_clippolygons > 0
  660. for(int i = 0; i < num_clippolygons; i++) {
  661. bool inside = pointInClipPolygon(position, i);
  662. insideCount = insideCount + (inside ? 1 : 0);
  663. clipVolumesCount++;
  664. }
  665. #endif
  666. bool insideAny = insideCount > 0;
  667. bool insideAll = (clipVolumesCount > 0) && (clipVolumesCount == insideCount);
  668. if(clipMethod == CLIPMETHOD_INSIDE_ANY){
  669. if(insideAny && clipTask == CLIPTASK_HIGHLIGHT){
  670. vColor.r += 0.5;
  671. }else if(!insideAny && clipTask == CLIPTASK_SHOW_INSIDE){
  672. gl_Position = vec4(100.0, 100.0, 100.0, 1.0);
  673. }else if(insideAny && clipTask == CLIPTASK_SHOW_OUTSIDE){
  674. gl_Position = vec4(100.0, 100.0, 100.0, 1.0);
  675. }
  676. }else if(clipMethod == CLIPMETHOD_INSIDE_ALL){
  677. if(insideAll && clipTask == CLIPTASK_HIGHLIGHT){
  678. vColor.r += 0.5;
  679. }else if(!insideAll && clipTask == CLIPTASK_SHOW_INSIDE){
  680. gl_Position = vec4(100.0, 100.0, 100.0, 1.0);
  681. }else if(insideAll && clipTask == CLIPTASK_SHOW_OUTSIDE){
  682. gl_Position = vec4(100.0, 100.0, 100.0, 1.0);
  683. }
  684. }
  685. }
  686. //
  687. // ## ## ### #### ## ##
  688. // ### ### ## ## ## ### ##
  689. // #### #### ## ## ## #### ##
  690. // ## ### ## ## ## ## ## ## ##
  691. // ## ## ######### ## ## ####
  692. // ## ## ## ## ## ## ###
  693. // ## ## ## ## #### ## ##
  694. //
  695. vec2 getSamplerCoord( vec3 direction )
  696. {
  697. direction = normalize(direction);
  698. float tx=atan(direction.x,-direction.y)/(PI*2.0)+0.5;
  699. float ty=acos(direction.z)/PI;
  700. return vec2(tx,ty);
  701. }
  702. vec3 transformAxis( vec3 direction ) //navvis->4dkk
  703. {
  704. float y = direction.y;
  705. direction.y = direction.z;
  706. direction.z = -y;
  707. return direction;
  708. }
  709. void main() {
  710. vec4 mvPosition = modelViewMatrix * vec4(position, 1.0 );
  711. vViewPosition = mvPosition.xyz;
  712. gl_Position = projectionMatrix * mvPosition;
  713. vLogDepth = log2(-mvPosition.z);
  714. //加-------------------
  715. // COLOR
  716. #if defined(usePanoMap)
  717. vec4 worldPosition = modelMatrix * vec4(position, 1.0);
  718. vec3 positionLocalToPanoCenter0 = worldPosition.xyz - pano0Position;
  719. vec3 vWorldPosition0 = (vec4(positionLocalToPanoCenter0, 1.0) * pano0Matrix).xyz;
  720. vWorldPosition0.x *= -1.0;
  721. vWorldPosition0 = transformAxis(vWorldPosition0);
  722. vec3 positionLocalToPanoCenter1 = worldPosition.xyz - pano1Position;
  723. vec3 vWorldPosition1 = (vec4(positionLocalToPanoCenter1, 1.0) * pano1Matrix).xyz;
  724. vWorldPosition1.x *= -1.0;
  725. vWorldPosition1 = transformAxis(vWorldPosition1);
  726. /*vec2 samplerCoord0 = getSamplerCoord(vWorldPosition0.xyz);
  727. vec2 samplerCoord1 = getSamplerCoord(vWorldPosition1.xyz);
  728. vec4 colorFromPano0 = texture2D(pano0Map,samplerCoord0);
  729. vec4 colorFromPano1 = texture2D(pano1Map,samplerCoord1);*/
  730. //pano0Map;
  731. vec4 colorFromPano0=textureCube(pano0Map,vWorldPosition0.xyz);
  732. vec4 colorFromPano1=textureCube(pano1Map,vWorldPosition1.xyz);
  733. vColor = mix(colorFromPano0,colorFromPano1,progress).xyz;
  734. //-----
  735. //float easeInOutRatio = 0.0; //缓冲,渐变点云到贴图的颜色
  736. if(progress < easeInOutRatio){
  737. float easeProgress = (easeInOutRatio - progress) / easeInOutRatio;
  738. vec3 vColor1 = getColor();
  739. vColor = mix(vColor,vColor1,easeProgress);
  740. }else if(progress > 1.0 - easeInOutRatio){
  741. float easeProgress = (progress - (1.0 - easeInOutRatio) ) / easeInOutRatio;
  742. vec3 vColor1 = getColor();
  743. vColor = mix(vColor,vColor1,easeProgress);
  744. }
  745. #else
  746. vColor = getColor();
  747. #endif
  748. //-------------------
  749. //gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
  750. //gl_PointSize = 5.0;
  751. // POINT SIZE
  752. float pointSize = getPointSize();
  753. //float pointSize = 2.0;
  754. gl_PointSize = pointSize;
  755. vPointSize = pointSize;
  756. // vColor = vec3(1.0, 0.0, 0.0);
  757. //gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
  758. //gl_Position = vec4(position.xzy / 1000.0, 1.0 );
  759. //gl_PointSize = 5.0;
  760. //vColor = vec3(1.0, 1.0, 1.0);
  761. // only for "replacing" approaches
  762. // if(getLOD() != uLevel){
  763. // gl_Position = vec4(10.0, 10.0, 10.0, 1.0);
  764. // }
  765. #if defined hq_depth_pass
  766. float originalDepth = gl_Position.w;
  767. float adjustedDepth = originalDepth + 2.0 * vRadius;
  768. float adjust = adjustedDepth / originalDepth;
  769. mvPosition.xyz = mvPosition.xyz * adjust;
  770. gl_Position = projectionMatrix * mvPosition;
  771. #endif
  772. // CLIPPING
  773. doClipping();
  774. #if defined(num_clipspheres) && num_clipspheres > 0
  775. for(int i = 0; i < num_clipspheres; i++){
  776. vec4 sphereLocal = uClipSpheres[i] * mvPosition;
  777. float distance = length(sphereLocal.xyz);
  778. if(distance < 1.0){
  779. float w = distance;
  780. vec3 cGradient = texture2D(gradient, vec2(w, 1.0 - w)).rgb;
  781. vColor = cGradient;
  782. //vColor = cGradient * 0.7 + vColor * 0.3;
  783. }
  784. }
  785. #endif
  786. #if defined(num_shadowmaps) && num_shadowmaps > 0
  787. const float sm_near = 0.1;
  788. const float sm_far = 10000.0;
  789. for(int i = 0; i < num_shadowmaps; i++){
  790. vec3 viewPos = (uShadowWorldView[i] * vec4(position, 1.0)).xyz;
  791. float distanceToLight = abs(viewPos.z);
  792. vec4 projPos = uShadowProj[i] * uShadowWorldView[i] * vec4(position, 1);
  793. vec3 nc = projPos.xyz / projPos.w;
  794. float u = nc.x * 0.5 + 0.5;
  795. float v = nc.y * 0.5 + 0.5;
  796. vec2 sampleStep = vec2(1.0 / (2.0*1024.0), 1.0 / (2.0*1024.0)) * 1.5;
  797. vec2 sampleLocations[9];
  798. sampleLocations[0] = vec2(0.0, 0.0);
  799. sampleLocations[1] = sampleStep;
  800. sampleLocations[2] = -sampleStep;
  801. sampleLocations[3] = vec2(sampleStep.x, -sampleStep.y);
  802. sampleLocations[4] = vec2(-sampleStep.x, sampleStep.y);
  803. sampleLocations[5] = vec2(0.0, sampleStep.y);
  804. sampleLocations[6] = vec2(0.0, -sampleStep.y);
  805. sampleLocations[7] = vec2(sampleStep.x, 0.0);
  806. sampleLocations[8] = vec2(-sampleStep.x, 0.0);
  807. float visibleSamples = 0.0;
  808. float numSamples = 0.0;
  809. float bias = vRadius * 2.0;
  810. for(int j = 0; j < 9; j++){
  811. vec4 depthMapValue = texture2D(uShadowMap[i], vec2(u, v) + sampleLocations[j]);
  812. float linearDepthFromSM = depthMapValue.x + bias;
  813. float linearDepthFromViewer = distanceToLight;
  814. if(linearDepthFromSM > linearDepthFromViewer){
  815. visibleSamples += 1.0;
  816. }
  817. numSamples += 1.0;
  818. }
  819. float visibility = visibleSamples / numSamples;
  820. 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){
  821. //vColor = vec3(0.0, 0.0, 0.2);
  822. }else{
  823. //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);
  824. vColor = vColor * visibility + vColor * uShadowColor * (1.0 - visibility);
  825. }
  826. }
  827. #endif
  828. }