|
15 | 15 | <script>
|
16 | 16 | var group;
|
17 | 17 | var container;
|
18 |
| - var particlesData = []; |
19 | 18 | var camera, scene, renderer, obc;
|
20 | 19 | var positions, colors;
|
21 | 20 | var particles;
|
22 | 21 | var pointCloud;
|
23 | 22 | var particlePositions;
|
24 | 23 | var linesMesh;
|
25 | 24 |
|
| 25 | + var particlesData = []; |
26 | 26 | var maxParticleCount = 1000;
|
27 | 27 | var particleCount = 500;
|
28 | 28 | var r = 800;
|
|
37 | 37 | particleCount: 500
|
38 | 38 | };
|
39 | 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 |
| - |
| 40 | + function init(container, scene, group, camera, helper, segments, positions, colors, pMaterial, particles, particlePositions) { |
94 | 41 | particles.setDrawRange( 0, particleCount );
|
95 | 42 | particles.setAttribute( 'position', new THREE.BufferAttribute( particlePositions, 3 ).setUsage( THREE.DynamicDrawUsage ) );
|
96 | 43 |
|
|
122 | 69 | renderer.outputEncoding = THREE.sRGBEncoding;
|
123 | 70 |
|
124 | 71 | container.appendChild( renderer.domElement );
|
| 72 | + renderer.render( scene, camera ); |
125 | 73 | }
|
126 | 74 |
|
127 | 75 | function animate() {
|
|
203 | 151 |
|
204 | 152 | pointCloud.geometry.attributes.position.needsUpdate = true;
|
205 | 153 |
|
206 |
| - requestAnimationFrame( animate ); |
207 | 154 | render();
|
208 | 155 |
|
209 | 156 | }
|
|
213 | 160 | var time = Date.now() * 0.001;
|
214 | 161 |
|
215 | 162 | group.rotation.y = time * 0.5;
|
216 |
| - renderer.render( scene, camera ); |
217 | 163 |
|
218 |
| - } |
219 | 164 |
|
220 |
| - init(); |
221 |
| - animate(); |
| 165 | + } |
222 | 166 | </script>
|
223 | 167 | <py-script>
|
224 | 168 | from pyodide import create_proxy, to_js
|
|
228 | 172 | from js import performance
|
229 | 173 | from pyodide import to_js
|
230 | 174 | from js import Object, Float32Array
|
| 175 | +from js import init, animate, render |
| 176 | +from js import group, container, camera, scene, renderer, obc, positions, colors, particles, pointCloud, particlePositions, linesMesh |
| 177 | + |
| 178 | +particlesData = []; |
| 179 | +maxParticleCount = 1000; |
| 180 | +particleCount = 500; |
| 181 | +r = 800; |
| 182 | +rHalf = r / 2; |
| 183 | + |
| 184 | +container = document.getElementById( 'container' ); |
| 185 | + |
| 186 | +camera = THREE.PerspectiveCamera.new( 45, window.innerWidth / window.innerHeight, 1, 4000 ); |
| 187 | +camera.position.z = 1750; |
| 188 | + |
| 189 | +scene = THREE.Scene.new(); |
| 190 | +group = THREE.Group.new(); |
| 191 | + |
| 192 | +scene.add( group ); |
| 193 | + |
| 194 | +helper = THREE.BoxHelper.new( THREE.Mesh.new( THREE.BoxGeometry.new( r, r, r ) ) ); |
| 195 | +helper.material.color.setHex( 0x101010 ); |
| 196 | +helper.material.blending = THREE.AdditiveBlending; |
| 197 | +helper.material.transparent = True; |
| 198 | +group.add( helper ); |
| 199 | + |
| 200 | +segments = maxParticleCount * maxParticleCount; |
| 201 | + |
| 202 | +positions = Float32Array.new( segments * 3 ); |
| 203 | +colors = Float32Array.new( segments * 3 ); |
| 204 | + |
| 205 | +points_perms = {"color": 0xFFFFFF,"size": 3,"blending": THREE.AdditiveBlending,"transparent": True,"sizeAttenuation": False} |
| 206 | +points_perms = Object.fromEntries(to_js(points_perms)) |
| 207 | + |
| 208 | +pMaterial = THREE.PointsMaterial.new(points_perms); |
| 209 | + |
| 210 | +particles = THREE.BufferGeometry.new(); |
| 211 | +particlePositions = Float32Array.new( maxParticleCount * 3 ); |
| 212 | + |
| 213 | +i = 0 |
| 214 | +while i < len(particlePositions): |
| 215 | + x = Math.random() * r - r / 2; |
| 216 | + y = Math.random() * r - r / 2; |
| 217 | + z = Math.random() * r - r / 2; |
| 218 | + |
| 219 | + particlePositions[ i * 3 ] = x; |
| 220 | + particlePositions[ i * 3 + 1 ] = y; |
| 221 | + particlePositions[ i * 3 + 2 ] = z; |
| 222 | + |
| 223 | + particlesData.append( {"velocity": THREE.Vector3.new( - 1 + Math.random() * 2, - 1 + Math.random() * 2, - 1 + Math.random() * 2 ), "numConnections": 0} ); |
| 224 | + i += 1 |
231 | 225 |
|
| 226 | +init(container, scene, group, camera, helper, segments, positions, colors, pMaterial, particles, particlePositions) |
232 | 227 | </py-script>
|
233 | 228 | </body>
|
234 | 229 | </html>
|
0 commit comments