forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpositional.html
More file actions
149 lines (129 loc) · 4.89 KB
/
positional.html
File metadata and controls
149 lines (129 loc) · 4.89 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<!DOCTYPE html>
<html>
<head>
<title>PlayCanvas Positional Sound</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);
// 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 an Entity with a camera component
var camera = new pc.Entity();
camera.addComponent("camera", {
clearColor: new pc.Color(1, 0, 0)
});
camera.addComponent("audiolistener");
camera.rotateLocal(-30, 0, 0);
camera.translateLocal(0, 0, 5);
app.root.addChild(camera);
// Create an Entity for the ground
var material = new pc.StandardMaterial();
material.diffuse.set(0, 1, 0);
material.update();
var ground = new pc.Entity();
ground.addComponent("model", {
type: "box",
material: material
});
ground.setLocalScale(50, 1, 50);
ground.setLocalPosition(0, -0.5, 0);
app.root.addChild(ground);
// Create an entity with a light component
var light = new pc.Entity();
light.addComponent("light", {
type: "directional",
color: new pc.Color(1, 1, 1),
castShadows: true,
intensity: 10,
shadowBias: 0.2,
shadowDistance: 16,
normalOffsetBias: 0.05,
shadowResolution: 2048
});
light.setLocalEulerAngles(45, 30, 0);
app.root.addChild(light);
var requests = [{
url: "../assets/sounds/footsteps.mp3",
type: "audio"
}, {
url: "../assets/models/playbot/playbot.json",
type: "model"
}, {
url: "../assets/animations/playbot/playbot-run.json",
type: "animation"
}];
var count = requests.length;
var assets = [];
for (var i = 0; i < requests.length; i++) {
app.assets.loadFromUrl(requests[i].url, requests[i].type, function (err, asset) {
count--;
if (count === 0) {
app.start();
var gravel = app.assets.find("footsteps.mp3");
var model = app.assets.find("playbot.json");
var anim = app.assets.find("playbot-run.json");
// Create walking dude
var entity = new pc.Entity();
// add sound component
entity.addComponent('sound');
// add footsteps slot
entity.sound.addSlot('footsteps', {
asset: gravel,
pitch: 1.7,
loop: true,
autoPlay: true
});
// add model
entity.addComponent("model", {
type: "asset",
asset: model,
castShadows: true
});
// add animation
entity.addComponent("animation", {
assets: [anim],
speed: 0.8
});
// add entity in the hierarchy
app.root.addChild(entity);
var pos;
var angle = 135;
var radius = 3;
var height = 0;// 1.1;
app.on("update", function (dt) {
angle += 30 * dt;
if (angle > 360) {
angle -= 360;
}
var pos = camera.getPosition();
entity.setLocalPosition(radius * Math.sin(angle * pc.math.DEG_TO_RAD), height, radius * Math.cos(angle * pc.math.DEG_TO_RAD));
entity.setLocalEulerAngles(0, angle + 90, 0);
});
}
});
}
</script>
</body>
</html>