123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - interactive - raycasting - points</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="container"></div>
- <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - interactive - raycasting - points </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- var renderer, scene, camera, stats;
- var pointclouds;
- var raycaster;
- var mouse = new THREE.Vector2();
- var intersection = null;
- var spheres = [];
- var spheresIndex = 0;
- var clock;
- var threshold = 0.1;
- var pointSize = 0.05;
- var width = 80;
- var length = 160;
- 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 ++ ) {
- for ( var j = 0; j < length; j ++ ) {
- var u = i / width;
- var v = j / length;
- var x = u - 0.5;
- var y = ( Math.cos( u * Math.PI * 4 ) + 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;
- colors[ 3 * k + 2 ] = color.b * intensity;
- k ++;
- }
- }
- geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
- geometry.setAttribute( '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.PointsMaterial( { size: pointSize, vertexColors: THREE.VertexColors } );
- return new THREE.Points( geometry, material );
- }
- 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 ++ ) {
- for ( var j = 0; j < length; j ++ ) {
- indices[ k ] = k;
- k ++;
- }
- }
- geometry.setIndex( new THREE.BufferAttribute( indices, 1 ) );
- var material = new THREE.PointsMaterial( { size: pointSize, vertexColors: THREE.VertexColors } );
- return new THREE.Points( geometry, material );
- }
- 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 ++ ) {
- for ( var j = 0; j < length; j ++ ) {
- indices[ k ] = k;
- k ++;
- }
- }
- geometry.setIndex( new THREE.BufferAttribute( indices, 1 ) );
- geometry.addGroup( 0, indices.length );
- var material = new THREE.PointsMaterial( { size: pointSize, vertexColors: THREE.VertexColors } );
- return new THREE.Points( geometry, material );
- }
- function init() {
- var container = document.getElementById( 'container' );
- scene = new THREE.Scene();
- clock = new THREE.Clock();
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
- camera.position.set( 10, 10, 10 );
- camera.lookAt( scene.position );
- camera.updateMatrix();
- //
- var pcBuffer = generatePointcloud( new THREE.Color( 1, 0, 0 ), width, length );
- pcBuffer.scale.set( 5, 10, 10 );
- pcBuffer.position.set( - 5, 0, 0 );
- scene.add( pcBuffer );
- var pcIndexed = generateIndexedPointcloud( new THREE.Color( 0, 1, 0 ), width, length );
- pcIndexed.scale.set( 5, 10, 10 );
- pcIndexed.position.set( 0, 0, 0 );
- scene.add( pcIndexed );
- var pcIndexedOffset = generateIndexedWithOffsetPointcloud( new THREE.Color( 0, 1, 1 ), width, length );
- pcIndexedOffset.scale.set( 5, 10, 10 );
- pcIndexedOffset.position.set( 5, 0, 0 );
- scene.add( pcIndexedOffset );
- pointclouds = [ pcBuffer, pcIndexed, pcIndexedOffset ];
- //
- var sphereGeometry = new THREE.SphereBufferGeometry( 0.1, 32, 32 );
- var sphereMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
- for ( var i = 0; i < 40; i ++ ) {
- var sphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
- scene.add( sphere );
- spheres.push( sphere );
- }
- //
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- //
- raycaster = new THREE.Raycaster();
- raycaster.params.Points.threshold = threshold;
- //
- stats = new Stats();
- container.appendChild( stats.dom );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- document.addEventListener( 'mousemove', onDocumentMouseMove, false );
- }
- function onDocumentMouseMove( event ) {
- event.preventDefault();
- mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
- mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- requestAnimationFrame( animate );
- render();
- stats.update();
- }
- var toggle = 0;
- function render() {
- camera.applyMatrix4( rotateY );
- camera.updateMatrixWorld();
- raycaster.setFromCamera( mouse, camera );
- 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 );
- }
- </script>
- </body>
- </html>
|