|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <title></title> |
| 5 | + <link type="text/css" rel="stylesheet" href="/webgl/points/main.css"> |
| 6 | + <script type="text/javascript" src="/webgl/points/js/three.min.js" ></script> |
| 7 | + |
| 8 | + </head> |
| 9 | + <body> |
| 10 | + |
| 11 | + <div id="container"></div> |
| 12 | + |
| 13 | + <script defer src="/build/pyscript.js"></script> |
| 14 | + <link rel="stylesheet" href="/build/pyscript.css" /> |
| 15 | + <script> |
| 16 | + var group; |
| 17 | + var container; |
| 18 | + var particlesData = []; |
| 19 | + var camera, scene, renderer, obc; |
| 20 | + var positions, colors; |
| 21 | + var particles; |
| 22 | + var pointCloud; |
| 23 | + var particlePositions; |
| 24 | + var linesMesh; |
| 25 | + |
| 26 | + var maxParticleCount = 1000; |
| 27 | + var particleCount = 500; |
| 28 | + var r = 800; |
| 29 | + var rHalf = r / 2; |
| 30 | + |
| 31 | + var effectController = { |
| 32 | + showDots: true, |
| 33 | + showLines: true, |
| 34 | + minDistance: 150, |
| 35 | + limitConnections: false, |
| 36 | + maxConnections: 20, |
| 37 | + particleCount: 500 |
| 38 | + }; |
| 39 | + |
| 40 | + function init() { |
| 41 | + container = document.getElementById( 'container' ); |
| 42 | + |
| 43 | + camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 4000 ); |
| 44 | + camera.position.z = 1750; |
| 45 | + |
| 46 | + //obc = new THREE.OrbitControls(); |
| 47 | + |
| 48 | + scene = new THREE.Scene(); |
| 49 | + |
| 50 | + |
| 51 | + group = new THREE.Group(); |
| 52 | + scene.add( group ); |
| 53 | + |
| 54 | + var helper = new THREE.BoxHelper( new THREE.Mesh( new THREE.BoxGeometry( r, r, r ) ) ); |
| 55 | + helper.material.color.setHex( 0x101010 ); |
| 56 | + helper.material.blending = THREE.AdditiveBlending; |
| 57 | + helper.material.transparent = true; |
| 58 | + group.add( helper ); |
| 59 | + |
| 60 | + var segments = maxParticleCount * maxParticleCount; |
| 61 | + |
| 62 | + positions = new Float32Array( segments * 3 ); |
| 63 | + colors = new Float32Array( segments * 3 ); |
| 64 | + |
| 65 | + var pMaterial = new THREE.PointsMaterial( { |
| 66 | + color: 0xFFFFFF, |
| 67 | + size: 3, |
| 68 | + blending: THREE.AdditiveBlending, |
| 69 | + transparent: true, |
| 70 | + sizeAttenuation: false |
| 71 | + } ); |
| 72 | + |
| 73 | + particles = new THREE.BufferGeometry(); |
| 74 | + particlePositions = new Float32Array( maxParticleCount * 3 ); |
| 75 | + |
| 76 | + for ( let i = 0; i < maxParticleCount; i ++ ) { |
| 77 | + |
| 78 | + var x = Math.random() * r - r / 2; |
| 79 | + var y = Math.random() * r - r / 2; |
| 80 | + var z = Math.random() * r - r / 2; |
| 81 | + |
| 82 | + particlePositions[ i * 3 ] = x; |
| 83 | + particlePositions[ i * 3 + 1 ] = y; |
| 84 | + particlePositions[ i * 3 + 2 ] = z; |
| 85 | + |
| 86 | + |
| 87 | + particlesData.push( { |
| 88 | + velocity: new THREE.Vector3( - 1 + Math.random() * 2, - 1 + Math.random() * 2, - 1 + Math.random() * 2 ), |
| 89 | + numConnections: 0 |
| 90 | + } ); |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + particles.setDrawRange( 0, particleCount ); |
| 95 | + particles.setAttribute( 'position', new THREE.BufferAttribute( particlePositions, 3 ).setUsage( THREE.DynamicDrawUsage ) ); |
| 96 | + |
| 97 | + |
| 98 | + pointCloud = new THREE.Points( particles, pMaterial ); |
| 99 | + group.add( pointCloud ); |
| 100 | + |
| 101 | + var geometry = new THREE.BufferGeometry(); |
| 102 | + |
| 103 | + geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ).setUsage( THREE.DynamicDrawUsage ) ); |
| 104 | + geometry.setAttribute( 'color', new THREE.BufferAttribute( colors, 3 ).setUsage( THREE.DynamicDrawUsage ) ); |
| 105 | + |
| 106 | + geometry.computeBoundingSphere(); |
| 107 | + |
| 108 | + geometry.setDrawRange( 0, 0 ); |
| 109 | + |
| 110 | + var material = new THREE.LineBasicMaterial( { |
| 111 | + vertexColors: true, |
| 112 | + blending: THREE.AdditiveBlending, |
| 113 | + transparent: true |
| 114 | + } ); |
| 115 | + |
| 116 | + linesMesh = new THREE.LineSegments( geometry, material ); |
| 117 | + group.add( linesMesh ); |
| 118 | + |
| 119 | + renderer = new THREE.WebGLRenderer( { antialias: true } ); |
| 120 | + renderer.setPixelRatio( window.devicePixelRatio ); |
| 121 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 122 | + renderer.outputEncoding = THREE.sRGBEncoding; |
| 123 | + |
| 124 | + container.appendChild( renderer.domElement ); |
| 125 | + } |
| 126 | + |
| 127 | + function animate() { |
| 128 | + |
| 129 | + let vertexpos = 0; |
| 130 | + let colorpos = 0; |
| 131 | + let numConnected = 0; |
| 132 | + |
| 133 | + for ( let i = 0; i < particleCount; i ++ ) |
| 134 | + particlesData[ i ].numConnections = 0; |
| 135 | + |
| 136 | + for ( let i = 0; i < particleCount; i ++ ) { |
| 137 | + |
| 138 | + var particleData = particlesData[ i ]; |
| 139 | + |
| 140 | + particlePositions[ i * 3 ] += particleData.velocity.x; |
| 141 | + particlePositions[ i * 3 + 1 ] += particleData.velocity.y; |
| 142 | + particlePositions[ i * 3 + 2 ] += particleData.velocity.z; |
| 143 | + |
| 144 | + if ( particlePositions[ i * 3 + 1 ] < - rHalf || particlePositions[ i * 3 + 1 ] > rHalf ) |
| 145 | + particleData.velocity.y = - particleData.velocity.y; |
| 146 | + |
| 147 | + if ( particlePositions[ i * 3 ] < - rHalf || particlePositions[ i * 3 ] > rHalf ) |
| 148 | + particleData.velocity.x = - particleData.velocity.x; |
| 149 | + |
| 150 | + if ( particlePositions[ i * 3 + 2 ] < - rHalf || particlePositions[ i * 3 + 2 ] > rHalf ) |
| 151 | + particleData.velocity.z = - particleData.velocity.z; |
| 152 | + |
| 153 | + if ( effectController.limitConnections && particleData.numConnections >= effectController.maxConnections ) |
| 154 | + continue; |
| 155 | + |
| 156 | + |
| 157 | + for ( let j = i + 1; j < particleCount; j ++ ) { |
| 158 | + |
| 159 | + var particleDataB = particlesData[ j ]; |
| 160 | + if ( effectController.limitConnections && particleDataB.numConnections >= effectController.maxConnections ) |
| 161 | + continue; |
| 162 | + |
| 163 | + var dx = particlePositions[ i * 3 ] - particlePositions[ j * 3 ]; |
| 164 | + var dy = particlePositions[ i * 3 + 1 ] - particlePositions[ j * 3 + 1 ]; |
| 165 | + var dz = particlePositions[ i * 3 + 2 ] - particlePositions[ j * 3 + 2 ]; |
| 166 | + var dist = Math.sqrt( dx * dx + dy * dy + dz * <
10BC0
span class=pl-s1>dz ); |
| 167 | + |
| 168 | + if ( dist < effectController.minDistance ) { |
| 169 | + |
| 170 | + particleData.numConnections ++; |
| 171 | + particleDataB.numConnections ++; |
| 172 | + |
| 173 | + var alpha = 1.0 - dist / effectController.minDistance; |
| 174 | + |
| 175 | + positions[ vertexpos ++ ] = particlePositions[ i * 3 ]; |
| 176 | + positions[ vertexpos ++ ] = particlePositions[ i * 3 + 1 ]; |
| 177 | + positions[ vertexpos ++ ] = particlePositions[ i * 3 + 2 ]; |
| 178 | + |
| 179 | + positions[ vertexpos ++ ] = particlePositions[ j * 3 ]; |
| 180 | + positions[ vertexpos ++ ] = particlePositions[ j * 3 + 1 ]; |
| 181 | + positions[ vertexpos ++ ] = particlePositions[ j * 3 + 2 ]; |
| 182 | + |
| 183 | + colors[ colorpos ++ ] = alpha; |
| 184 | + colors[ colorpos ++ ] = alpha; |
| 185 | + colors[ colorpos ++ ] = alpha; |
| 186 | + |
| 187 | + colors[ colorpos ++ ] = alpha; |
| 188 | + colors[ colorpos ++ ] = alpha; |
| 189 | + colors[ colorpos ++ ] = alpha; |
| 190 | + |
| 191 | + numConnected ++; |
| 192 | + |
| 193 | + } |
| 194 | + |
| 195 | + } |
| 196 | + |
| 197 | + } |
| 198 | + |
| 199 | + |
| 200 | + linesMesh.geometry.setDrawRange( 0, numConnected * 2 ); |
| 201 | + linesMesh.geometry.attributes.position.needsUpdate = true; |
| 202 | + linesMesh.geometry.attributes.color.needsUpdate = true; |
| 203 | + |
| 204 | + pointCloud.geometry.attributes.position.needsUpdate = true; |
| 205 | + |
| 206 | + requestAnimationFrame( animate ); |
| 207 | + render(); |
| 208 | + |
| 209 | + } |
| 210 | + |
| 211 | + function render() { |
| 212 | + |
| 213 | + var time = Date.now() * 0.001; |
| 214 | + |
| 215 | + group.rotation.y = time * 0.5; |
| 216 | + renderer.render( scene, camera ); |
| 217 | + |
| 218 | + } |
| 219 | + |
| 220 | + init(); |
| 221 | + animate(); |
| 222 | + </script> |
| 223 | +<py-script> |
| 224 | +from pyodide import create_proxy, to_js |
| 225 | +from js import window |
| 226 | +from js import Math |
| 227 | +from js import THREE |
| 228 | +from js import performance |
| 229 | +from pyodide import to_js |
| 230 | +from js import Object, Float32Array |
| 231 | + |
| 232 | +</py-script> |
| 233 | + </body> |
| 234 | +</html> |
0 commit comments