|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover"> |
| 6 | + <title>PlayCanvas Web Components - Physics Cluster</title> |
| 7 | + <script type="importmap"> |
| 8 | + { |
| 9 | + "imports": { |
| 10 | + "playcanvas": "../node_modules/playcanvas/build/playcanvas.mjs" |
| 11 | + } |
| 12 | + } |
| 13 | + </script> |
| 14 | + <script type="module" src="../dist/pwc.mjs"></script> |
| 15 | + <link rel="stylesheet" href="css/example.css"> |
| 16 | + </head> |
| 17 | + <body> |
| 18 | + <pc-app> |
| 19 | + <!-- Modules --> |
| 20 | + <pc-module name="Ammo" glue="modules/ammo/ammo.wasm.js" wasm="modules/ammo/ammo.wasm.wasm" fallback="modules/ammo/ammo.js"></pc-module> |
| 21 | + <!-- Assets --> |
| 22 | + <pc-asset src="assets/skies/octagon-lamps-photo-studio-2k.hdr" id="studio"></pc-asset> |
| 23 | + <pc-asset src="assets/scripts/gravity.mjs"></pc-asset> |
| 24 | + <pc-asset src="assets/scripts/physical-pointer.mjs"></pc-asset> |
| 25 | + <pc-material id="hotpink" diffuse="hotpink"></pc-material> |
| 26 | + <pc-material id="mediumseagreen" diffuse="mediumseagreen"></pc-material> |
| 27 | + <!-- Scene --> |
| 28 | + <pc-scene gravity="0 0 0"> |
| 29 | + <!-- Sky --> |
| 30 | + <pc-sky asset="studio" type="none" lighting></pc-sky> |
| 31 | + <!-- Camera --> |
| 32 | + <pc-entity name="camera" position="0 0 5"> |
| 33 | + <pc-camera clear-color="#cccccc" tonemap="neutral"></pc-camera> |
| 34 | + </pc-entity> |
| 35 | + <!-- Physical Sphere Template --> |
| 36 | + <template id="sphere-template"> |
| 37 | + <pc-entity> |
| 38 | + <pc-render type="sphere" material="mediumseagreen"></pc-render> |
| 39 | + <pc-collision type="sphere"></pc-collision> |
| 40 | + <pc-rigidbody type="dynamic" angular-damping="0.9" linear-damping="0.9" restitution="0.9"></pc-rigidbody> |
| 41 | + <pc-scripts> |
| 42 | + <pc-script name="gravity"></pc-script> |
| 43 | + </pc-scripts> |
| 44 | + </pc-entity> |
| 45 | + </template> |
| 46 | + <!-- Physical Pointer --> |
| 47 | + <pc-entity name="pointer" position="0 0 0"> |
| 48 | + <pc-entity scale="0.5 0.5 0.5"> |
| 49 | + <pc-render type="sphere"></pc-render> |
| 50 | + </pc-entity> |
| 51 | + <pc-light type="omni" cast-shadows></pc-light> |
| 52 | + <pc-collision type="sphere" radius="0.25"></pc-collision> |
| 53 | + <pc-rigidbody type="kinematic" restitution="0.9"></pc-rigidbody> |
| 54 | + <pc-scripts> |
| 55 | + <pc-script name="physicalPointer"></pc-script> |
| 56 | + </pc-scripts> |
| 57 | + </pc-entity> |
| 58 | + </pc-scene> |
| 59 | + </pc-app> |
| 60 | + <script> |
| 61 | + document.addEventListener('DOMContentLoaded', () => { |
| 62 | + const scene = document.querySelector('pc-scene'); |
| 63 | + const sphereTemplate = document.getElementById('sphere-template'); |
| 64 | + |
| 65 | + // Clone and append 40 spheres |
| 66 | + for (let i = 0; i < 40; i++) { |
| 67 | + // Clone the sphere template |
| 68 | + const clone = document.importNode(sphereTemplate.content, true); |
| 69 | + const sphereEntity = clone.querySelector('pc-entity'); |
| 70 | + |
| 71 | + // Set a random start position |
| 72 | + const x = (Math.random() - 0.5) * 10; |
| 73 | + const y = (Math.random() - 0.5) * 10; |
| 74 | + const z = (Math.random() - 0.5) * 10; |
| 75 | + sphereEntity.setAttribute('position', `${x} ${y} ${z}`); |
| 76 | + |
| 77 | + // Append the clone to the scene |
| 78 | + scene.appendChild(clone); |
| 79 | + } |
| 80 | + }); |
| 81 | + </script> |
| 82 | + <script type="module" src="js/example.mjs"></script> |
| 83 | + </body> |
| 84 | +</html> |
0 commit comments