Guest User

Untitled

a guest
Nov 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. /**
  2. * @param {Float32Array} positions coordinates of line points written
  3. * like this: [x1, y1, z1, x2, y2, z2, ...]
  4. * @param {Float32Array} colors of line points written like this:
  5. * [x1, y1, z1, x2, y2, z2, ...]
  6. * @param {Float32Array} sizes of line points written like this:
  7. * [size1, size2, ...]
  8. */
  9. function wideLine(positions, colors, sizes) {
  10. var vertexShader = [
  11. "attribute float size;",
  12. "attribute vec3 customColor;",
  13. "varying vec3 vColor;",
  14. "void main() {",
  15. "vColor = customColor;",
  16. "vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);",
  17. "gl_PointSize = size * (300.0 / length(mvPosition.xyz));",
  18. "gl_Position = projectionMatrix * mvPosition;",
  19. "}"
  20. ].join("\n");
  21.  
  22. var fragmentShader = [
  23. "varying vec3 vColor;",
  24. "void main() {",
  25. "gl_FragColor = vec4(vColor, 1.0);",
  26. "}"
  27. ].join("\n");
  28.  
  29. var shaderMaterial = new THREE.ShaderMaterial( {
  30. vertexShader: vertexShader,
  31. fragmentShader: fragmentShader
  32. });
  33.  
  34. this.linkGeometry = new THREE.BufferGeometry();
  35.  
  36. this.linkGeometry.addAttribute('position', new THREE.BufferAttribute(positions, 3));
  37. this.linkGeometry.addAttribute('customColor', new THREE.BufferAttribute(colors, 3));
  38. this.linkGeometry.addAttribute('size', new THREE.BufferAttribute(sizes, 1));
  39.  
  40. this.particleMesh = new THREE.PointCloud(this.linkGeometry, shaderMaterial);
  41. this.particleMesh.dynamic = false;
  42. this.particleMesh.frustrumCulled = true;
  43. this.hide();
  44. }
  45.  
  46. wideLine.prototype.getMesh = function() {
  47. return this.particleMesh;
  48. };
  49.  
  50. wideLine.prototype.hide = function() {
  51. this.linkGeometry.setDrawRange(0, 0);
  52. }
  53.  
  54. wideLine.prototype.show = function(lBound, uBound) {
  55. this.linkGeometry.setDrawRange(lBound, uBound);
  56. };
  57. /**
  58. * draws a set of labeled curves with desired thickness and color
  59. * is able to hide all curves or show certain curves
  60. * curves don't have to have unique names
  61. * @param {Object[]} data — array of objects, e.g.
  62. * {
  63. "name": "TUR",
  64. "points": splinePoints, //array of THREE.Vector3 points corresponding to a curve
  65. "color": "#00ffff",
  66. "size": 0.35, //curve thickness
  67. }
  68. */
  69. function wideCurves(data) {
  70. this.bounds = {};
  71. var count = 0;
  72. for (var i = 0; i < data.length; i++) {
  73. count += data[i].points.length;
  74. }
  75. var positions = new Float32Array(count * 3);
  76. var colors = new Float32Array(count * 3);
  77. var sizes = new Float32Array(count);
  78. var offset = 0;
  79. var name = '';
  80. var lBound = 0;
  81. var uBound = 0;
  82. var writeToBounds = false;
  83.  
  84. for (var i = 0; i < data.length; i ++) {
  85. var points = data[i].points,
  86. color = new THREE.Color(data[i].color),
  87. pointsNum = data[i].points.length;
  88.  
  89. for (var j = 0; j < pointsNum; j++) {
  90. positions[offset + j*3] = points[j].x;
  91. positions[offset + j*3 + 1] = points[j].y;
  92. positions[offset + j*3 + 2] = points[j].z;
  93. colors[offset + j*3] = color.r;
  94. colors[offset + j*3+1] = color.g;
  95. colors[offset + j*3+2] = color.b;
  96. sizes[offset/3 + j] = data[i].size;
  97. }
  98.  
  99. if (data[i].name != name) {
  100. if (lBound != 0 || uBound != 0) {
  101. this.bounds[name] = [lBound, uBound];
  102. writeToBounds = false;
  103. }
  104. name = data[i].name;
  105. lBound = offset/3;
  106. uBound = offset /3 + pointsNum;
  107. writeToBounds = true;
  108. } else {
  109. uBound += pointsNum;
  110. }
  111.  
  112. offset += pointsNum*3;
  113. }
  114.  
  115. if (writeToBounds) {
  116. this.bounds[name] = [lBound, uBound];
  117. }
  118.  
  119. this.curves = new wideLine(positions, colors, sizes);
  120. this.uBound = count;
  121. this.hide();
  122. }
  123.  
  124. wideCurves.prototype.getMesh = function() {
  125. return this.curves.getMesh();
  126. }
  127.  
  128. wideCurves.prototype.hide = function() {
  129. this.curves.hide();
  130. }
  131.  
  132. wideCurves.prototype.show = function(name) {
  133. if (typeof(name) != "undefined") {
  134. var bounds = this.bounds[name];
  135. var lBound = bounds[0], uBound = bounds[1];
  136. this.curves.show(lBound, uBound);
  137. } else {
  138. this.curves.show(0, this.uBound);
  139. }
  140. };
Add Comment
Please, Sign In to add comment