|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <title>cannon.js - performance tests</title> |
| 5 | + <meta charset="utf-8" /> |
| 6 | + <link rel="stylesheet" href="css/style.css" type="text/css" /> |
| 7 | + <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0" /> |
| 8 | + </head> |
| 9 | + <body> |
| 10 | + <script type="module"> |
| 11 | + import * as CANNON from '../dist/cannon-es.js' |
| 12 | + import { Demo } from './js/Demo.js' |
| 13 | + |
| 14 | + /** |
| 15 | + * Test the performance limits with a lot of stuff happening all at the same time. |
| 16 | + * The simulation should not slow down but rather skip ahead when introducing jank. |
| 17 | + * When there are too many object to simulate, the simulation will slow down but still run at a stable framerate. |
| 18 | + */ |
| 19 | + const demo = new Demo() |
| 20 | + |
| 21 | + let rafId |
| 22 | + |
| 23 | + demo.addScene('200 boxes', () => { |
| 24 | + setupFallingBoxes({ N: 200 }) |
| 25 | + }) |
| 26 | + |
| 27 | + demo.addScene('500 boxes', () => { |
| 28 | + setupFallingBoxes({ N: 500 }) |
| 29 | + }) |
| 30 | + |
| 31 | + demo.addScene('100 boxes + 32ms jank', () => { |
| 32 | + setupFallingBoxes({ N: 100, JANK: 32 }) |
| 33 | + }) |
| 34 | + |
| 35 | + demo.addScene('100 boxes + 48ms jank', () => { |
| 36 | + setupFallingBoxes({ N: 100, JANK: 48 }) |
| 37 | + }) |
| 38 | + |
| 39 | + demo.addScene('100 boxes + 64ms jank', () => { |
| 40 | + setupFallingBoxes({ N: 100, JANK: 64 }) |
| 41 | + }) |
| 42 | + |
| 43 | + demo.addScene('100 boxes + 128ms jank', () => { |
| 44 | + setupFallingBoxes({ N: 100, JANK: 128 }) |
| 45 | + }) |
| 46 | + |
| 47 | + function setupFallingBoxes({ N, JANK }) { |
| 48 | + if (rafId) cancelAnimationFrame(rafId) |
| 49 | + |
| 50 | + const world = setupWorld(demo) |
| 51 | + |
| 52 | + const size = 0.25 |
| 53 | + const mass = 1 |
| 54 | + |
| 55 | + const boxShape = new CANNON.Box(new CANNON.Vec3(size, size, size)) |
| 56 | + |
| 57 | + const boxes = [] |
| 58 | + for (let i = 0; i < N; i++) { |
| 59 | + // start with random positions |
| 60 | + const position = new CANNON.Vec3( |
| 61 | + (Math.random() * 2 - 1) * 2.5, |
| 62 | + Math.random() * 10, |
| 63 | + (Math.random() * 2 - 1) * 2.5 |
| 64 | + ) |
| 65 | + |
| 66 | + const boxBody = new CANNON.Body({ |
| 67 | + position, |
| 68 | + mass, |
| 69 | + }) |
| 70 | + boxBody.addShape(boxShape) |
| 71 | + world.addBody(boxBody) |
| 72 | + // demo.addVisual(boxBody) |
| 73 | + boxes.push(boxBody) |
| 74 | + } |
| 75 | + |
| 76 | + // Use instancing so three.js doesn't get in the way |
| 77 | + // of performance measuring |
| 78 | + demo.addVisualInstanced(boxes) |
| 79 | + |
| 80 | + function animate(ms) { |
| 81 | + rafId = requestAnimationFrame(animate) |
| 82 | + |
| 83 | + const time = ms / 1000 |
| 84 | + |
| 85 | + const index = Math.floor(Math.random() * N) |
| 86 | + |
| 87 | + boxes[index].position.set(0, Math.random() * 10, 0) |
| 88 | + |
| 89 | + if (JANK) { |
| 90 | + blockThread(JANK) |
| 91 | + } |
| 92 | + } |
| 93 | + rafId = requestAnimationFrame(animate) |
| 94 | + } |
| 95 | + |
| 96 | + demo.start() |
| 97 | + |
| 98 | + function setupWorld(demo) { |
| 99 | + const world = demo.getWorld() |
| 100 | + world.gravity.set(0, -50, 0) |
| 101 | + // world.broadphase = new CANNON.SAPBroadphase(world) |
| 102 | + |
| 103 | + // world.solver.iterations = 20; |
| 104 | + // world.solver.tolerance = 0.001; |
| 105 | + // world.allowSleep = true; |
| 106 | + |
| 107 | + // Static ground plane |
| 108 | + const groundShape = new CANNON.Plane() |
| 109 | + const groundBody = new CANNON.Body({ mass: 0 }) |
| 110 | + groundBody.addShape(groundShape) |
| 111 | + groundBody.quaternion.setFromEuler(-Math.PI / 2, 0, 0) |
| 112 | + world.addBody(groundBody) |
| 113 | + demo.addVisual(groundBody) |
| 114 | + |
| 115 | + return world |
| 116 | + } |
| 117 | + |
| 118 | + // Block the javascript thread for N milliseconds |
| 119 | + function blockThread(milliseconds = 0) { |
| 120 | + const start = performance.now() |
| 121 | + while (performance.now() < start + milliseconds) {} |
| 122 | + } |
| 123 | + </script> |
| 124 | + </body> |
| 125 | +</html> |
0 commit comments