diff --git a/src/pointcloud/Particles.js b/src/pointcloud/Particles.js new file mode 100644 index 00000000..426005ed --- /dev/null +++ b/src/pointcloud/Particles.js @@ -0,0 +1,127 @@ +/** + * @author David V. Lu!! - davidvlu@gmail.com + */ + +/** + * A set of particles. Used by PointCloud2. + * + * @constructor + * @param options - object with following keys: + * + * * tfClient - the TF client handle to use + * * texture - (optional) Image url for a texture to use for the points. Defaults to a single white pixel. + * * rootObject (optional) - the root object to add this marker to + * * size (optional) - size to draw each point (default 0.05) + * * max_pts (optional) - number of points to draw (default 100) + */ +ROS3D.Particles = function(options) { + options = options || {}; + this.tfClient = options.tfClient; + var texture = options.texture || 'https://upload.wikimedia.org/wikipedia/commons/a/a2/Pixel-white.png'; + var size = options.size || 0.05; + this.max_pts = options.max_pts || 100; + this.first_size = null; + this.prev_pts = 0; + this.rootObject = options.rootObject || new THREE.Object3D(); + var that = this; + THREE.Object3D.call(this); + + this.vertex_shader = [ + 'attribute vec3 customColor;', + 'attribute float alpha;', + 'varying vec3 vColor;', + 'varying float falpha;', + 'void main() ', + '{', + ' vColor = customColor; // set color associated to vertex; use later in fragment shader', + ' vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );', + ' falpha = alpha; ', + '', + ' // option (1): draw particles at constant size on screen', + ' // gl_PointSize = size;', + ' // option (2): scale particles as objects in 3D space', + ' gl_PointSize = ', size, '* ( 300.0 / length( mvPosition.xyz ) );', + ' gl_Position = projectionMatrix * mvPosition;', + '}' + ].join('\n'); + + this.fragment_shader = [ + 'uniform sampler2D texture;', + 'varying vec3 vColor; // colors associated to vertices; assigned by vertex shader', + 'varying float falpha;', + 'void main() ', + '{', + ' // calculates a color for the particle', + ' gl_FragColor = vec4( vColor, falpha );', + ' // sets particle texture to desired color', + ' gl_FragColor = gl_FragColor * texture2D( texture, gl_PointCoord );', + '}' + ].join('\n'); + + this.geom = new THREE.Geometry(); + for(var i=0;iparticles.max_pts){ + throw 'Attempted to draw more points than max_pts allows'; + } +} diff --git a/src/pointcloud/PointCloud2.js b/src/pointcloud/PointCloud2.js index ed8e12b7..abe0db89 100644 --- a/src/pointcloud/PointCloud2.js +++ b/src/pointcloud/PointCloud2.js @@ -50,6 +50,7 @@ function decode64(x) { * * ros - the ROSLIB.Ros connection handle * * topic - the marker topic to listen to * * tfClient - the TF client handle to use + * * texture - (optional) Image url for a texture to use for the points. Defaults to a single white pixel. * * rootObject (optional) - the root object to add this marker to * * size (optional) - size to draw each point (default 0.05) * * max_pts (optional) - number of points to draw (default 100) @@ -58,117 +59,34 @@ ROS3D.PointCloud2 = function(options) { options = options || {}; var ros = options.ros; var topic = options.topic || '/points'; - this.tfClient = options.tfClient; - var size = options.size || 0.05; - var max_pts = options.max_pts || 100; - this.prev_pts = 0; - this.rootObject = options.rootObject || new THREE.Object3D(); var that = this; - THREE.Object3D.call(this); - this.vertex_shader = [ - 'attribute vec3 customColor;', - 'attribute float alpha;', - 'varying vec3 vColor;', - 'varying float falpha;', - 'void main() ', - '{', - ' vColor = customColor; // set color associated to vertex; use later in fragment shader', - ' vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );', - ' falpha = alpha; ', - '', - ' // option (1): draw particles at constant size on screen', - ' // gl_PointSize = size;', - ' // option (2): scale particles as objects in 3D space', - ' gl_PointSize = ', size, '* ( 300.0 / length( mvPosition.xyz ) );', - ' gl_Position = projectionMatrix * mvPosition;', - '}' - ].join('\n'); + this.particles = new ROS3D.Particles(options); - this.fragment_shader = [ - 'uniform sampler2D texture;', - 'varying vec3 vColor; // colors associated to vertices; assigned by vertex shader', - 'varying float falpha;', - 'void main() ', - '{', - ' // calculates a color for the particle', - ' gl_FragColor = vec4( vColor, falpha );', - ' // sets particle texture to desired color', - ' gl_FragColor = gl_FragColor * texture2D( texture, gl_PointCoord );', - '}' - ].join('\n'); + var rosTopic = new ROSLIB.Topic({ + ros : ros, + name : topic, + messageType : 'sensor_msgs/PointCloud2' + }); - this.geom = new THREE.Geometry(); - for(var i=0;i