|
@@ -49,24 +49,24 @@
|
|
|
var spheres = [];
|
|
|
var spheresIndex = 0;
|
|
|
var clock;
|
|
|
-
|
|
|
+
|
|
|
var threshold = 0.1;
|
|
|
- var pointSize = 0.01;
|
|
|
+ var pointSize = 0.05;
|
|
|
var width = 150;
|
|
|
var length = 150;
|
|
|
var rotateY = new THREE.Matrix4().makeRotationY( 0.005 );
|
|
|
|
|
|
init();
|
|
|
animate();
|
|
|
-
|
|
|
+
|
|
|
function generatePointCloudGeometry( color, width, length ){
|
|
|
-
|
|
|
+
|
|
|
var geometry = new THREE.BufferGeometry();
|
|
|
var numPoints = width*length;
|
|
|
-
|
|
|
+
|
|
|
var positions = new Float32Array( numPoints*3 );
|
|
|
var colors = new Float32Array( numPoints*3 );
|
|
|
-
|
|
|
+
|
|
|
var k = 0;
|
|
|
|
|
|
for( var i = 0; i < width; i++ ) {
|
|
@@ -78,11 +78,11 @@
|
|
|
var x = u - 0.5;
|
|
|
var y = ( Math.cos( u * Math.PI * 8 ) + Math.sin( v * Math.PI * 8 ) ) / 20;
|
|
|
var z = v - 0.5;
|
|
|
-
|
|
|
+
|
|
|
positions[ 3 * k ] = x;
|
|
|
positions[ 3 * k + 1 ] = y;
|
|
|
positions[ 3 * k + 2 ] = z;
|
|
|
-
|
|
|
+
|
|
|
var intensity = ( y + 0.1 ) * 5;
|
|
|
colors[ 3 * k ] = color.r * intensity;
|
|
|
colors[ 3 * k + 1 ] = color.g * intensity;
|
|
@@ -93,32 +93,32 @@
|
|
|
}
|
|
|
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
|
|
|
geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
|
|
|
geometry.computeBoundingBox();
|
|
|
-
|
|
|
+
|
|
|
return geometry;
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
function generatePointcloud( color, width, length ) {
|
|
|
-
|
|
|
+
|
|
|
var geometry = generatePointCloudGeometry( color, width, length );
|
|
|
-
|
|
|
+
|
|
|
var material = new THREE.PointCloudMaterial( { size: pointSize, vertexColors: THREE.VertexColors } );
|
|
|
var pointcloud = new THREE.PointCloud( geometry, material );
|
|
|
-
|
|
|
+
|
|
|
return pointcloud;
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
function generateIndexedPointcloud( color, width, length ) {
|
|
|
-
|
|
|
+
|
|
|
var geometry = generatePointCloudGeometry( color, width, length );
|
|
|
var numPoints = width * length;
|
|
|
var indices = new Uint16Array( numPoints );
|
|
|
-
|
|
|
+
|
|
|
var k = 0;
|
|
|
|
|
|
for( var i = 0; i < width; i++ ) {
|
|
@@ -131,22 +131,22 @@
|
|
|
}
|
|
|
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
geometry.addAttribute( 'index', new THREE.BufferAttribute( indices, 1 ) );
|
|
|
-
|
|
|
+
|
|
|
var material = new THREE.PointCloudMaterial( { size: pointSize, vertexColors: THREE.VertexColors } );
|
|
|
var pointcloud = new THREE.PointCloud( geometry, material );
|
|
|
-
|
|
|
+
|
|
|
return pointcloud;
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
function generateIndexedWithOffsetPointcloud( color, width, length ){
|
|
|
-
|
|
|
+
|
|
|
var geometry = generatePointCloudGeometry( color, width, length );
|
|
|
var numPoints = width * length;
|
|
|
var indices = new Uint16Array( numPoints );
|
|
|
-
|
|
|
+
|
|
|
var k = 0;
|
|
|
|
|
|
for( var i = 0; i < width; i++ ){
|
|
@@ -159,26 +159,26 @@
|
|
|
}
|
|
|
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
geometry.addAttribute( 'index', new THREE.BufferAttribute( indices, 1 ) );
|
|
|
-
|
|
|
+
|
|
|
var offset = { start: 0, count: indices.length, index: 0 };
|
|
|
geometry.offsets.push( offset );
|
|
|
-
|
|
|
+
|
|
|
var material = new THREE.PointCloudMaterial( { size: pointSize, vertexColors: THREE.VertexColors } );
|
|
|
var pointcloud = new THREE.PointCloud( geometry, material );
|
|
|
-
|
|
|
+
|
|
|
return pointcloud;
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
function generateRegularPointcloud( color, width, length ) {
|
|
|
-
|
|
|
+
|
|
|
var geometry = new THREE.Geometry();
|
|
|
var numPoints = width * length;
|
|
|
-
|
|
|
+
|
|
|
var colors = [];
|
|
|
-
|
|
|
+
|
|
|
var k = 0;
|
|
|
|
|
|
for( var i = 0; i < width; i++ ) {
|
|
@@ -189,31 +189,31 @@
|
|
|
var v = j / length;
|
|
|
var x = u - 0.5;
|
|
|
var y = ( Math.cos( u * Math.PI * 8 ) + Math.sin( v * Math.PI * 8) ) / 20;
|
|
|
- var z = v - 0.5;
|
|
|
+ var z = v - 0.5;
|
|
|
var v = new THREE.Vector3( x,y,z );
|
|
|
-
|
|
|
+
|
|
|
var intensity = ( y + 0.1 ) * 7;
|
|
|
colors[ 3 * k ] = color.r * intensity;
|
|
|
colors[ 3 * k + 1 ] = color.g * intensity;
|
|
|
colors[ 3 * k + 2 ] = color.b * intensity;
|
|
|
-
|
|
|
+
|
|
|
geometry.vertices.push( v );
|
|
|
colors[ k ] = ( color.clone().multiplyScalar( intensity ) );
|
|
|
-
|
|
|
+
|
|
|
k++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
geometry.colors = colors;
|
|
|
geometry.computeBoundingBox();
|
|
|
-
|
|
|
+
|
|
|
var material = new THREE.PointCloudMaterial( { size: pointSize, vertexColors: THREE.VertexColors } );
|
|
|
var pointcloud = new THREE.PointCloud( geometry, material );
|
|
|
-
|
|
|
+
|
|
|
return pointcloud;
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
|
|
|
function init() {
|
|
@@ -221,7 +221,7 @@
|
|
|
container = document.getElementById( 'container' );
|
|
|
|
|
|
scene = new THREE.Scene();
|
|
|
-
|
|
|
+
|
|
|
clock = new THREE.Clock();
|
|
|
|
|
|
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
|
|
@@ -234,35 +234,35 @@
|
|
|
pcBuffer.scale.set( 10,10,10 );
|
|
|
pcBuffer.position.set( -5,0,5 );
|
|
|
scene.add( pcBuffer );
|
|
|
-
|
|
|
+
|
|
|
var pcIndexed = generateIndexedPointcloud( new THREE.Color( 0,1,0 ), width, length );
|
|
|
pcIndexed.scale.set( 10,10,10 );
|
|
|
pcIndexed.position.set( 5,0,5 );
|
|
|
scene.add( pcIndexed );
|
|
|
-
|
|
|
+
|
|
|
var pcIndexedOffset = generateIndexedWithOffsetPointcloud( new THREE.Color( 0,1,1 ), width, length );
|
|
|
pcIndexedOffset.scale.set( 10,10,10 );
|
|
|
pcIndexedOffset.position.set( 5,0,-5 );
|
|
|
scene.add( pcIndexedOffset );
|
|
|
-
|
|
|
+
|
|
|
var pcRegular = generateRegularPointcloud( new THREE.Color( 1,0,1 ), width, length );
|
|
|
pcRegular.scale.set( 10,10,10 );
|
|
|
pcRegular.position.set( -5,0,-5 );
|
|
|
scene.add( pcRegular );
|
|
|
|
|
|
pointclouds = [ pcBuffer, pcIndexed, pcIndexedOffset, pcRegular ];
|
|
|
-
|
|
|
+
|
|
|
//
|
|
|
-
|
|
|
+
|
|
|
var sphereGeometry = new THREE.SphereGeometry( 0.1, 32, 32 );
|
|
|
var sphereMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000, shading: THREE.FlatShading } );
|
|
|
-
|
|
|
- for ( var i = 0; i < 40; i++ ) {
|
|
|
-
|
|
|
+
|
|
|
+ for ( var i = 0; i < 40; i++ ) {
|
|
|
+
|
|
|
var sphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
|
|
|
scene.add( sphere );
|
|
|
spheres.push( sphere );
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
|
|
|
//
|
|
@@ -289,7 +289,7 @@
|
|
|
|
|
|
window.addEventListener( 'resize', onWindowResize, false );
|
|
|
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
|
|
|
function onDocumentMouseMove( event ) {
|
|
@@ -318,41 +318,41 @@
|
|
|
stats.update();
|
|
|
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
var toggle = 0;
|
|
|
-
|
|
|
- function render() {
|
|
|
-
|
|
|
+
|
|
|
+ function render() {
|
|
|
+
|
|
|
camera.applyMatrix( rotateY );
|
|
|
camera.updateMatrixWorld( true );
|
|
|
-
|
|
|
+
|
|
|
vector.set( mouse.x, mouse.y, 0.1 );
|
|
|
|
|
|
projector.unprojectVector( vector, camera );
|
|
|
|
|
|
raycaster.ray.set( camera.position, vector.sub( camera.position ).normalize() );
|
|
|
-
|
|
|
+
|
|
|
var intersections = raycaster.intersectObjects( pointclouds );
|
|
|
intersection = ( intersections.length ) > 0 ? intersections[ 0 ] : null;
|
|
|
-
|
|
|
+
|
|
|
if ( toggle > 0.02 && intersection !== null) {
|
|
|
-
|
|
|
+
|
|
|
spheres[ spheresIndex ].position.copy( intersection.point );
|
|
|
spheres[ spheresIndex ].scale.set( 1, 1, 1 );
|
|
|
spheresIndex = ( spheresIndex + 1 ) % spheres.length;
|
|
|
-
|
|
|
+
|
|
|
toggle = 0;
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
for ( var i = 0; i < spheres.length; i++ ) {
|
|
|
-
|
|
|
+
|
|
|
var sphere = spheres[ i ];
|
|
|
sphere.scale.multiplyScalar( 0.98 );
|
|
|
sphere.scale.clampScalar( 0.01, 1 );
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
toggle += clock.getDelta();
|
|
|
|
|
|
renderer.render( scene, camera );
|