forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraycast.html
More file actions
192 lines (157 loc) · 6.1 KB
/
raycast.html
File metadata and controls
192 lines (157 loc) · 6.1 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<!DOCTYPE html>
<html>
<head>
<title>PlayCanvas Raycast</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>
<script src="../wasm-loader.js"></script>
<style>
body {
margin: 0;
overflow-y: hidden;
}
</style>
</head>
<body>
<!-- The canvas element -->
<canvas id="application-canvas"></canvas>
<!-- The script -->
<script>
if (wasmSupported()) {
loadWasmModuleAsync('Ammo', '../lib/ammo/ammo.wasm.js', '../lib/ammo/ammo.wasm.wasm', demo);
} else {
loadWasmModuleAsync('Ammo', '../lib/ammo/ammo.js', '', demo);
}
function demo() {
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);
app.scene.ambientLight = new pc.Color(0.2, 0.2, 0.2);
function createMaterial(color) {
var material = new pc.StandardMaterial();
material.diffuse = color;
material.update();
return material;
}
// Create a couple of materials
var red = createMaterial(new pc.Color(1, 0, 0));
var green = createMaterial(new pc.Color(0, 1, 0));
// Create light
var light = new pc.Entity();
light.addComponent("light", {
type: "directional"
});
app.root.addChild(light);
light.setEulerAngles(45, 30, 0);
// Create camera
var camera = new pc.Entity();
camera.addComponent("camera", {
clearColor: new pc.Color(0.5, 0.5, 0.8)
});
app.root.addChild(camera);
camera.setPosition(5, 0, 15);
function createPhysicalShape(type, material, x, y, z) {
var e = new pc.Entity();
// Have to set the position of the entity before adding the static rigidbody
// component because static bodies cannot be moved after creation
app.root.addChild(e);
e.setPosition(x, y, z);
e.addComponent("model", {
type: type,
material: material
});
e.addComponent("rigidbody", {
type: "static"
});
e.addComponent("collision", {
type: type,
height: type === 'capsule' ? 2 : 1
});
return e;
}
// Create two rows of physical geometric shapes
var types = ['box', 'capsule', 'cone', 'cylinder', 'sphere'];
types.forEach(function (type, idx) {
createPhysicalShape(type, green, idx * 2 + 1, 2, 0);
});
types.forEach(function (type, idx) {
createPhysicalShape(type, green, idx * 2 + 1, -2, 0);
});
// Allocate some colors
var white = new pc.Color(1, 1, 1);
var blue = new pc.Color(0, 0, 1);
// Allocate some vectors
var start = new pc.Vec3();
var end = new pc.Vec3();
var temp = new pc.Vec3();
// Set an update function on the application's update event
var time = 0;
app.on("update", function (dt) {
time += dt;
// Reset all shapes to green
app.root.findComponents('model').forEach(function (model) {
model.material = green;
});
y = 2 + Math.sin(time);
start.set(0, y, 0);
end.set(10, y, 0);
// Render the ray used in the raycast
app.renderLine(start, end, white);
var result = app.systems.rigidbody.raycastFirst(start, end);
if (result) {
result.entity.model.material = red;
// Render the normal on the surface from the hit point
temp.copy(result.normal).scale(0.3).add(result.point);
app.renderLine(result.point, temp, blue);
}
y = -2 + Math.sin(time);
start.set(0, y, 0);
end.set(10, y, 0);
// Render the ray used in the raycast
app.renderLine(start, end, white);
var results = app.systems.rigidbody.raycastAll(start, end);
results.forEach(function (result) {
result.entity.model.material = red;
// Render the normal on the surface from the hit point
temp.copy(result.normal).scale(0.3).add(result.point);
app.renderLine(result.point, temp, blue);
}, this);
});
var createText = function (fontAsset, message, x, y, z, rot) {
// Create a text element-based entity
var text = new pc.Entity();
text.addComponent("element", {
anchor: [0.5, 0.5, 0.5, 0.5],
fontAsset: fontAsset,
fontSize: 0.5,
pivot: [0, 0.5],
text: message,
type: pc.ELEMENTTYPE_TEXT
});
text.setLocalPosition(x, y, z);
text.setLocalEulerAngles(0, 0, rot);
app.root.addChild(text);
};
// Load a font
var fontAsset = new pc.Asset('arial.json', "font", { url: "../assets/fonts/arial.json" });
fontAsset.on('load', function () {
createText(fontAsset, 'raycastFirst', 0.5, 3.75, 0, 0);
createText(fontAsset, 'raycastAll', 0.5, -0.25, 0, 0);
});
app.assets.add(fontAsset);
app.assets.load(fontAsset);
}
</script>
</body>
</html>