Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @param {Float32Array} positions coordinates of line points written
- * like this: [x1, y1, z1, x2, y2, z2, ...]
- * @param {Float32Array} colors of line points written like this:
- * [x1, y1, z1, x2, y2, z2, ...]
- * @param {Float32Array} sizes of line points written like this:
- * [size1, size2, ...]
- */
- function wideLine(positions, colors, sizes) {
- var vertexShader = [
- "attribute float size;",
- "attribute vec3 customColor;",
- "varying vec3 vColor;",
- "void main() {",
- "vColor = customColor;",
- "vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);",
- "gl_PointSize = size * (300.0 / length(mvPosition.xyz));",
- "gl_Position = projectionMatrix * mvPosition;",
- "}"
- ].join("\n");
- var fragmentShader = [
- "varying vec3 vColor;",
- "void main() {",
- "gl_FragColor = vec4(vColor, 1.0);",
- "}"
- ].join("\n");
- var shaderMaterial = new THREE.ShaderMaterial( {
- vertexShader: vertexShader,
- fragmentShader: fragmentShader
- });
- this.linkGeometry = new THREE.BufferGeometry();
- this.linkGeometry.addAttribute('position', new THREE.BufferAttribute(positions, 3));
- this.linkGeometry.addAttribute('customColor', new THREE.BufferAttribute(colors, 3));
- this.linkGeometry.addAttribute('size', new THREE.BufferAttribute(sizes, 1));
- this.particleMesh = new THREE.PointCloud(this.linkGeometry, shaderMaterial);
- this.particleMesh.dynamic = false;
- this.particleMesh.frustrumCulled = true;
- this.hide();
- }
- wideLine.prototype.getMesh = function() {
- return this.particleMesh;
- };
- wideLine.prototype.hide = function() {
- this.linkGeometry.setDrawRange(0, 0);
- }
- wideLine.prototype.show = function(lBound, uBound) {
- this.linkGeometry.setDrawRange(lBound, uBound);
- };
- /**
- * draws a set of labeled curves with desired thickness and color
- * is able to hide all curves or show certain curves
- * curves don't have to have unique names
- * @param {Object[]} data — array of objects, e.g.
- * {
- "name": "TUR",
- "points": splinePoints, //array of THREE.Vector3 points corresponding to a curve
- "color": "#00ffff",
- "size": 0.35, //curve thickness
- }
- */
- function wideCurves(data) {
- this.bounds = {};
- var count = 0;
- for (var i = 0; i < data.length; i++) {
- count += data[i].points.length;
- }
- var positions = new Float32Array(count * 3);
- var colors = new Float32Array(count * 3);
- var sizes = new Float32Array(count);
- var offset = 0;
- var name = '';
- var lBound = 0;
- var uBound = 0;
- var writeToBounds = false;
- for (var i = 0; i < data.length; i ++) {
- var points = data[i].points,
- color = new THREE.Color(data[i].color),
- pointsNum = data[i].points.length;
- for (var j = 0; j < pointsNum; j++) {
- positions[offset + j*3] = points[j].x;
- positions[offset + j*3 + 1] = points[j].y;
- positions[offset + j*3 + 2] = points[j].z;
- colors[offset + j*3] = color.r;
- colors[offset + j*3+1] = color.g;
- colors[offset + j*3+2] = color.b;
- sizes[offset/3 + j] = data[i].size;
- }
- if (data[i].name != name) {
- if (lBound != 0 || uBound != 0) {
- this.bounds[name] = [lBound, uBound];
- writeToBounds = false;
- }
- name = data[i].name;
- lBound = offset/3;
- uBound = offset /3 + pointsNum;
- writeToBounds = true;
- } else {
- uBound += pointsNum;
- }
- offset += pointsNum*3;
- }
- if (writeToBounds) {
- this.bounds[name] = [lBound, uBound];
- }
- this.curves = new wideLine(positions, colors, sizes);
- this.uBound = count;
- this.hide();
- }
- wideCurves.prototype.getMesh = function() {
- return this.curves.getMesh();
- }
- wideCurves.prototype.hide = function() {
- this.curves.hide();
- }
- wideCurves.prototype.show = function(name) {
- if (typeof(name) != "undefined") {
- var bounds = this.bounds[name];
- var lBound = bounds[0], uBound = bounds[1];
- this.curves.show(lBound, uBound);
- } else {
- this.curves.show(0, this.uBound);
- }
- };
Add Comment
Please, Sign In to add comment