-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathtransform-rotate.example.mjs
More file actions
164 lines (143 loc) · 4.99 KB
/
transform-rotate.example.mjs
File metadata and controls
164 lines (143 loc) · 4.99 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
// @config E2E_TEST
import { data } from 'examples/observer';
import { deviceType, fileImport, rootPath } from 'examples/utils';
import * as pc from 'playcanvas';
const { Grid } = await fileImport(`${rootPath}/static/scripts/esm/grid.mjs`);
const canvas = /** @type {HTMLCanvasElement} */ (document.getElementById('application-canvas'));
window.focus();
const gfxOptions = {
deviceTypes: [deviceType],
glslangUrl: `${rootPath}/static/lib/glslang/glslang.js`,
twgslUrl: `${rootPath}/static/lib/twgsl/twgsl.js`
};
const device = await pc.createGraphicsDevice(canvas, gfxOptions);
device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
const createOptions = new pc.AppOptions();
createOptions.graphicsDevice = device;
createOptions.mouse = new pc.Mouse(document.body);
createOptions.keyboard = new pc.Keyboard(window);
createOptions.componentSystems = [
pc.RenderComponentSystem,
pc.CameraComponentSystem,
pc.LightComponentSystem,
pc.ScriptComponentSystem
];
createOptions.resourceHandlers = [pc.TextureHandler, pc.ContainerHandler, pc.ScriptHandler, pc.FontHandler];
const app = new pc.AppBase(canvas);
app.init(createOptions);
// 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);
// load assets
const assets = {
script: new pc.Asset('script', 'script', { url: `${rootPath}/static/scripts/camera/orbit-camera.js` }),
font: new pc.Asset('font', 'font', { url: `${rootPath}/static/assets/fonts/courier.json` })
};
/**
* @param {pc.Asset[] | number[]} assetList - The asset list.
* @param {pc.AssetRegistry} assetRegistry - The asset registry.
* @returns {Promise<void>} The promise.
*/
function loadAssets(assetList, assetRegistry) {
return new Promise((resolve) => {
const assetListLoader = new pc.AssetListLoader(assetList, assetRegistry);
assetListLoader.load(resolve);
});
}
await loadAssets(Object.values(assets), app.assets);
app.start();
// scene settings
app.scene.ambientLight = new pc.Color(0.2, 0.2, 0.2);
// create entities
const box = new pc.Entity('box');
box.addComponent('render', {
type: 'box'
});
app.root.addChild(box);
// create camera entity
const camera = new pc.Entity('camera');
camera.addComponent('camera', {
clearColor: new pc.Color(0.1, 0.1, 0.1),
farClip: 1000
});
camera.addComponent('script');
const orbitCamera = camera.script.create('orbitCamera');
camera.script.create('orbitCameraInputMouse');
camera.script.create('orbitCameraInputTouch');
camera.setPosition(1, 1, 1);
app.root.addChild(camera);
orbitCamera.distance = 5 * camera.camera.aspectRatio;
data.set('camera', {
proj: camera.camera.projection + 1,
fov: camera.camera.fov
});
// create light entity
const light = new pc.Entity('light');
light.addComponent('light');
app.root.addChild(light);
light.setEulerAngles(0, 0, -60);
// create gizmo
const layer = pc.Gizmo.createLayer(app);
const gizmo = new pc.RotateGizmo(camera.camera, layer);
gizmo.attach(box);
data.set('gizmo', {
size: gizmo.size,
snapIncrement: gizmo.snapIncrement,
orbitRotation: gizmo.orbitRotation,
xAxisColor: Object.values(gizmo.xAxisColor),
yAxisColor: Object.values(gizmo.yAxisColor),
zAxisColor: Object.values(gizmo.zAxisColor),
colorAlpha: gizmo.colorAlpha,
shading: gizmo.shading,
coordSpace: gizmo.coordSpace,
ringTolerance: gizmo.ringTolerance,
xyzTubeRadius: gizmo.xyzTubeRadius,
xyzRingRadius: gizmo.xyzRingRadius,
faceTubeRadius: gizmo.faceTubeRadius,
faceRingRadius: gizmo.faceRingRadius
});
// create grid
const gridEntity = new pc.Entity('grid');
gridEntity.setLocalScale(4, 1, 4);
app.root.addChild(gridEntity);
gridEntity.addComponent('script');
gridEntity.script.create(Grid);
// controls hook
const tmpC = new pc.Color();
data.on('*:set', (/** @type {string} */ path, /** @type {any} */ value) => {
const [category, key] = path.split('.');
switch (category) {
case 'camera':
switch (key) {
case 'proj':
camera.camera.projection = value - 1;
break;
case 'fov':
camera.camera.fov = value;
break;
}
return;
case 'gizmo':
// @ts-ignore
if (gizmo[key] instanceof pc.Color) {
// @ts-ignore
gizmo[key] = tmpC.set(...value);
return;
}
// @ts-ignore
gizmo[key] = value;
}
});
// ensure canvas is resized when window changes size + keep gizmo size consistent to canvas size
const resize = () => {
app.resizeCanvas();
const bounds = canvas.getBoundingClientRect();
const dim = camera.camera.horizontalFov ? bounds.width : bounds.height;
data.set('gizmo.size', 1024 / dim);
};
window.addEventListener('resize', resize);
resize();
app.on('destroy', () => {
window.removeEventListener('resize', resize);
});
export { app };