forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatching-dynamic.html
More file actions
115 lines (94 loc) · 3.98 KB
/
Copy pathbatching-dynamic.html
File metadata and controls
115 lines (94 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<!DOCTYPE html>
<html>
<head>
<title>PlayCanvas Batching Dynamic</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link rel="icon" type="image/png" href="../playcanvas-favicon.png" />
<script src="../../build/playcanvas.js"></script>
<script src="../../build/playcanvas-extras.js"></script>
<style>
body {
margin: 0;
overflow-y: hidden;
}
</style>
</head>
<body>
<!-- The canvas element -->
<canvas id="application-canvas"></canvas>
<!-- The script -->
<script>
var canvas = document.getElementById("application-canvas");
// Create the application and start the update loop
var app = new pc.Application(canvas);
app.start();
// Set the canvas to fill the window and automatically change resolution to be the same as the canvas size
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
app.setCanvasResolution(pc.RESOLUTION_AUTO);
window.addEventListener("resize", function () {
app.resizeCanvas(canvas.width, canvas.height);
});
var miniStats = new pcx.MiniStats(app);
// create two material
var material1 = new pc.StandardMaterial();
material1.diffuse = new pc.Color(1, 1, 0);
material1.update();
var material2 = new pc.StandardMaterial();
material2.diffuse = new pc.Color(0, 1, 1);
material2.update();
// create a single BatchGroup. Make it dynamic to allow batched meshes to be freely moved every frame.
var batchGroup = pc.app.batcher.addGroup("Meshes", true, 100);
// create various primitive instances using one of the two materials
var numInstances = 500;
var shapes = ["box", "cone", "cylinder", "sphere", "capsule"];
var entities = [];
var i;
for (i = 0; i < numInstances; i++) {
// random shape
var shapeName = shapes[Math.floor(Math.random() * shapes.length)];
var entity = new pc.Entity();
// create render component
var component = entity.addComponent("render", {
type: shapeName,
material: Math.random() < 0.5 ? material1 : material2,
// add it to the batchGroup - this instructs engine to try and render these meshes in a small number of draw calls.
// there will be at least 2 draw calls, one for each material
batchGroupId: batchGroup.id
});
// add entity for rendering
app.root.addChild(entity);
// keep in the list to adjust positions each frame
entities.push(entity);
}
// Create an entity with a directional light component
var light = new pc.Entity();
light.addComponent("light", {
type: "directional"
});
app.root.addChild(light);
light.setLocalEulerAngles(45, 30, 0);
// Create an entity with a camera component
var camera = new pc.Entity();
camera.addComponent("camera", {
clearColor: new pc.Color(0.3, 0.3, 0.3)
});
app.root.addChild(camera);
// Set an update function on the app's update event
var time = 0;
app.on("update", function (dt) {
time += dt;
// move all entities along orbits
for (i = 0; i < entities.length; i++) {
var radius = 5 + 20.0 * i / numInstances;
var speed = i / numInstances;
entities[i].setLocalPosition(radius * Math.sin(i + time * speed), radius * Math.cos(i + time * speed), radius * Math.cos(i + 2 * time * speed));
entities[i].lookAt(pc.Vec3.ZERO);
}
// orbit camera around
camera.setLocalPosition(100 * Math.sin(time), 0, 100 * Math.cos(time));
camera.lookAt(pc.Vec3.ZERO);
});
</script>
</body>
</html>