|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <title>PlayCanvas Mini Stats</title> |
| 5 | + <meta charset="utf-8"> |
| 6 | + <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> |
| 7 | + <link rel="icon" type="image/png" href="../playcanvas-favicon.png" /> |
| 8 | + <script src="../../build/playcanvas.prf.js"></script> |
| 9 | + <script src="../../build/playcanvas-extras.js"></script> |
| 10 | + <style> |
| 11 | + body { |
| 12 | + margin: 0; |
| 13 | + overflow-y: hidden; |
| 14 | + } |
| 15 | + </style> |
| 16 | +</head> |
| 17 | + |
| 18 | +<body> |
| 19 | + <!-- The canvas element --> |
| 20 | + <canvas id="application-canvas"></canvas> |
| 21 | + |
| 22 | + <!-- The script --> |
| 23 | + <script> |
| 24 | + var canvas = document.getElementById("application-canvas"); |
| 25 | + |
| 26 | + // Create the application and start the update loop |
| 27 | + var app = new pc.Application(canvas); |
| 28 | + app.start(); |
| 29 | + |
| 30 | + // Set the canvas to fill the window and automatically change resolution to be the same as the canvas size |
| 31 | + app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW); |
| 32 | + app.setCanvasResolution(pc.RESOLUTION_AUTO); |
| 33 | + |
| 34 | + window.addEventListener("resize", function () { |
| 35 | + app.resizeCanvas(canvas.width, canvas.height); |
| 36 | + }); |
| 37 | + |
| 38 | + // set up options for mini-stats, start with the default options |
| 39 | + var options = pcx.MiniStats.getDefaultOptions(); |
| 40 | + |
| 41 | + // configure sizes |
| 42 | + options.sizes = [ |
| 43 | + { width: 128, height: 16, spacing: 0, graphs: false }, |
| 44 | + { width: 256, height: 32, spacing: 2, graphs: true }, |
| 45 | + { width: 500, height: 64, spacing: 2, graphs: true } |
| 46 | + ]; |
| 47 | + |
| 48 | + // when the application starts, use the largest size |
| 49 | + options.startSizeIndex = 2; |
| 50 | + |
| 51 | + // display additional counters |
| 52 | + // Note: for most of thees to report values, either debug or profiling engine build needs to be used. |
| 53 | + options.stats = [ |
| 54 | + |
| 55 | + // frame update time in ms |
| 56 | + { |
| 57 | + name: "Update", |
| 58 | + stats: ["frame.updateTime"], |
| 59 | + decimalPlaces: 1, |
| 60 | + unitsName: "ms", |
| 61 | + watermark: 33 |
| 62 | + }, |
| 63 | + |
| 64 | + // total number of draw calls |
| 65 | + { |
| 66 | + name: "DrawCalls", |
| 67 | + stats: ["drawCalls.total"], |
| 68 | + watermark: 2000 |
| 69 | + }, |
| 70 | + |
| 71 | + // total number of triangles, in 1000s |
| 72 | + { |
| 73 | + name: "triCount", |
| 74 | + stats: ["frame.triangles"], |
| 75 | + decimalPlaces: 1, |
| 76 | + multiplier: 1 / 1000, |
| 77 | + unitsName: "k", |
| 78 | + watermark: 500 |
| 79 | + }, |
| 80 | + |
| 81 | + // number of materials used in a frame |
| 82 | + { |
| 83 | + name: "materials", |
| 84 | + stats: ["frame.materials"], |
| 85 | + watermark: 2000 |
| 86 | + }, |
| 87 | + |
| 88 | + // frame time it took to do frustum culling |
| 89 | + { |
| 90 | + name: "cull", |
| 91 | + stats: ["frame.cullTime"], |
| 92 | + decimalPlaces: 1, |
| 93 | + watermark: 1, |
| 94 | + unitsName: "ms", |
| 95 | + }, |
| 96 | + |
| 97 | + // used VRAM, displayed using 2 colors - red for textures, green for geometry |
| 98 | + { |
| 99 | + name: "VRAM", |
| 100 | + stats: ["vram.tex", "vram.geom"], |
| 101 | + decimalPlaces: 1, |
| 102 | + multiplier: 1 / (1024 * 1024), |
| 103 | + unitsName: "MB", |
| 104 | + watermark: 100 |
| 105 | + }, |
| 106 | + |
| 107 | + // frames per second |
| 108 | + { |
| 109 | + name: "FPS", |
| 110 | + stats: ["frame.fps"], |
| 111 | + watermark: 60 |
| 112 | + }, |
| 113 | + |
| 114 | + // delta time |
| 115 | + { |
| 116 | + name: "Frame", |
| 117 | + stats: ["frame.ms"], |
| 118 | + decimalPlaces: 1, |
| 119 | + unitsName: "ms", |
| 120 | + watermark: 33 |
| 121 | + } |
| 122 | + ]; |
| 123 | + |
| 124 | + // create mini-stats system |
| 125 | + var miniStats = new pcx.MiniStats(app, options); |
| 126 | + |
| 127 | + // add directional lights to the scene |
| 128 | + var light = new pc.Entity(); |
| 129 | + light.addComponent("light", { |
| 130 | + type: "directional" |
| 131 | + }); |
| 132 | + app.root.addChild(light); |
| 133 | + light.setLocalEulerAngles(45, 30, 0); |
| 134 | + |
| 135 | + // Create an entity with a camera component |
| 136 | + var camera = new pc.Entity(); |
| 137 | + camera.addComponent("camera", { |
| 138 | + clearColor: new pc.Color(0.1, 0.1, 0.1) |
| 139 | + }); |
| 140 | + app.root.addChild(camera); |
| 141 | + camera.setLocalPosition(20, 10, 10); |
| 142 | + camera.lookAt(pc.Vec3.ZERO); |
| 143 | + |
| 144 | + // helper function to create a primitive with shape type, position, scale |
| 145 | + function createPrimitive(primitiveType, position, scale) { |
| 146 | + |
| 147 | + // create material of random color |
| 148 | + var material = new pc.StandardMaterial(); |
| 149 | + material.diffuse = new pc.Color(Math.random(), Math.random(), Math.random()); |
| 150 | + material.update(); |
| 151 | + |
| 152 | + // create primitive |
| 153 | + var primitive = new pc.Entity(); |
| 154 | + primitive.addComponent('model', { |
| 155 | + type: primitiveType |
| 156 | + }); |
| 157 | + primitive.model.material = material; |
| 158 | + |
| 159 | + // set position and scale |
| 160 | + primitive.setLocalPosition(position); |
| 161 | + primitive.setLocalScale(scale); |
| 162 | + |
| 163 | + return primitive; |
| 164 | + } |
| 165 | + |
| 166 | + // list of all created engine resources |
| 167 | + var entities = []; |
| 168 | + var vertexBuffers = []; |
| 169 | + var textures = []; |
| 170 | + |
| 171 | + // update function called every frame |
| 172 | + var adding = true; |
| 173 | + var step = 10, max = 2000; |
| 174 | + var entity, vertexBuffer, texture; |
| 175 | + app.on("update", function (dt) { |
| 176 | + |
| 177 | + // execute some tasks multiple times per frame |
| 178 | + for (var i = 0; i < step; i++) { |
| 179 | + |
| 180 | + // allocating resouces |
| 181 | + if (adding) { |
| 182 | + |
| 183 | + // add entity (they used shared geometry internally, and we create individual material for each) |
| 184 | + var shape = Math.random() < 0.5 ? "box" : "sphere"; |
| 185 | + var position = new pc.Vec3(Math.random() * 10, Math.random() * 10, Math.random() * 10); |
| 186 | + var scale = 0.5 + Math.random(); |
| 187 | + entity = createPrimitive(shape, position, new pc.Vec3(scale, scale, scale)); |
| 188 | + entities.push(entity); |
| 189 | + app.root.addChild(entity); |
| 190 | + |
| 191 | + // if allocation reached the max limit, switch to removing mode |
| 192 | + if (entities.length >= max) { |
| 193 | + adding = false; |
| 194 | + } |
| 195 | + |
| 196 | + // add vertex buffer |
| 197 | + var vertexCount = 500; |
| 198 | + var data = new Float32Array(vertexCount * 16); |
| 199 | + vertexBuffer = new pc.VertexBuffer(app.graphicsDevice, pc.VertexFormat.defaultInstancingFormat, vertexCount, pc.BUFFER_STATIC, data); |
| 200 | + vertexBuffers.push(vertexBuffer); |
| 201 | + |
| 202 | + // allocate texture |
| 203 | + var texture = new pc.Texture(app.graphicsDevice, { |
| 204 | + width: 64, |
| 205 | + height: 64, |
| 206 | + format: pc.PIXELFORMAT_R8_G8_B8, |
| 207 | + autoMipmap: false |
| 208 | + }); |
| 209 | + textures.push(texture); |
| 210 | + |
| 211 | + // ensure texture is uploaded (actual VRAM is allocated) |
| 212 | + var dest = texture.lock(); |
| 213 | + texture.unlock(); |
| 214 | + app.graphicsDevice.setTexture(texture, 0); |
| 215 | + |
| 216 | + } else { // de-allocating resources |
| 217 | + |
| 218 | + if (entities.length > 0) { |
| 219 | + |
| 220 | + // desotry entities |
| 221 | + entity = entities[entities.length - 1]; |
| 222 | + entity.destroy(); |
| 223 | + entities.length = entities.length - 1; |
| 224 | + |
| 225 | + // destroy vertex buffer |
| 226 | + vertexBuffer = vertexBuffers[vertexBuffers.length - 1]; |
| 227 | + vertexBuffer.destroy(); |
| 228 | + vertexBuffers.length = vertexBuffers.length - 1; |
| 229 | + |
| 230 | + // destroy texture |
| 231 | + texture = textures[textures.length - 1]; |
| 232 | + texture.destroy(); |
| 233 | + textures.length = textures.length - 1; |
| 234 | + |
| 235 | + } else { |
| 236 | + adding = true; |
| 237 | + } |
| 238 | + } |
| 239 | + } |
| 240 | + }); |
| 241 | + </script> |
| 242 | +</body> |
| 243 | +</html> |
0 commit comments