-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.js
208 lines (177 loc) · 5.95 KB
/
sample.js
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
var PC_Wrapper = PC_Wrapper || (function() {
var container = document.querySelector('#container');
var canvas = document.createElement('CANVAS');
var assetUrls = [
'ui-circle-default.png',
'ui-slider_top-default.png',
'white1x1-default.png',
'input-default.js',
'ui-default.js'
];
init();
function init() {
canvas.style.width = '100%';
canvas.style.height = '100%';
container.appendChild(canvas);
pc.app = new pc.Application(canvas, { });
pc.app.start();
// fill the available space at full resolution
pc.app.setCanvasFillMode(pc.FILLMODE_NONE);
pc.app.setCanvasResolution(pc.RESOLUTION_AUTO);
pc.app.mouse = new pc.Mouse(canvas);
pc.app.touch = new pc.TouchDevice(canvas);
// ensure canvas is resized when window changes size
window.addEventListener('resize', function() {
pc.app.resizeCanvas();
});
loadAssets(function() {
var cameraEntity = drawStage();
cameraEntity.addComponent('script');
cameraEntity.script.create('orbitCamera');
cameraEntity.script.create('touchInput');
cameraEntity.script.create('mouseInput');
drawUI();
});
}
function loadAssets(callback) {
var cnt = 0;
assetUrls.forEach(function(url) {
pc.app.assets.loadFromUrl(url, /.js$/.test(url) ? 'script' : 'texture', function(err, asset) {
if (++cnt >= assetUrls.length) {
callback();
}
});
});
}
function drawStage() {
var cameraEntity = new pc.Entity('camera');
cameraEntity.addComponent('camera', {
clearColor: new pc.Color(1,1,1)
});
var light = new pc.Entity('light');
light.addComponent('light', {
type: 'spot',
color: new pc.Color(1, 1, 1)
});
light.setPosition(0, 0.5, -0.5);
light.setEulerAngles(-45, 0, 0);
pc.app.root.addChild(light);
pc.app.root.addChild(cameraEntity);
cameraEntity.setPosition(0, 0, 2);
return cameraEntity;
}
function drawUI() {
console.log('UIControl draw!');
// parent 2d screen
var screenEntity = new pc.Entity('ui-screen');
screenEntity.enabled = true;
screenEntity.addComponent('screen', {
screenSpace: true, // for 2d screen
scaleMode: pc.SCALEMODE_BLEND,
scaleBlend: 0.5,
referenceResolution: new pc.Vec2(1280, 720)
});
// sliders group
var uiSliders = new pc.Entity('ui-sliders');
uiSliders.addComponent('element', {
type: pc.ELEMENTTYPE_GROUP,
anchor: new pc.Vec4(1,0,1,0),
pivot: new pc.Vec2(1,0),
width: 64,
height: 400,
margin: new pc.Vec4(-139,250,75,-650)
});
uiSliders.setLocalPosition(-75,250,0);
// divider
var uiDivider = new pc.Entity('ui-divider');
uiDivider.addComponent('element', {
type: pc.ELEMENTTYPE_IMAGE,
anchor: new pc.Vec4(0.5,0.5,0.5,0.5),
pivot: new pc.Vec2(0.5,0.5),
width: 40,
height: 2,
margin: new pc.Vec4(-20,-1,-20,-1),
texture: pc.app.assets.find('white1x1-default.png').resource
});
uiSliders.addChild(uiDivider);
// top circle
var uiTopCircle = new pc.Entity('ui-top-circle');
uiTopCircle.addComponent('element', {
type: pc.ELEMENTTYPE_IMAGE,
anchor: new pc.Vec4(0.5,1,0.5,1),
pivot: new pc.Vec2(0.5,1),
width: 64,
height: 64,
margin: new pc.Vec4(-32,-64,-32,0),
texture: pc.app.assets.find('ui-circle-default.png').resource
});
uiTopCircle.setLocalScale(0.8,0.8,0.8);
uiSliders.addChild(uiTopCircle);
// bot circle
var uiBotCircle = new pc.Entity('ui-bot-circle');
uiBotCircle.addComponent('element', {
type: pc.ELEMENTTYPE_IMAGE,
anchor: new pc.Vec4(0.5,0,0.5,0),
pivot: new pc.Vec2(0.5,0),
width: 64,
height: 64,
margin: new pc.Vec4(-32,0,-32,-64),
texture: pc.app.assets.find('ui-circle-default.png').resource
});
uiBotCircle.setLocalScale(0.8,0.8,0.8);
uiSliders.addChild(uiBotCircle);
// slider guide
var uiSliderGuide = new pc.Entity('ui-slider-guide');
uiSliderGuide.addComponent('element', {
type: pc.ELEMENTTYPE_IMAGE,
anchor: new pc.Vec4(0.5,0.5,0.5,0.5),
pivot: new pc.Vec2(0.5,0.5),
width: 3,
height: 300,
margin: new pc.Vec4(-1.5,-150,-1.5,-150),
texture: pc.app.assets.find('white1x1-default.png').resource
});
uiSliders.addChild(uiSliderGuide);
// top slider
var uiTopSlider = new pc.Entity('ui-top-slider');
uiTopSlider.addComponent('element', {
type: pc.ELEMENTTYPE_IMAGE,
useInput: true,
anchor: new pc.Vec4(0.5,1,0.5,1),
pivot: new pc.Vec2(0.5,1),
width: 64,
height: 64,
margin: new pc.Vec4(-32,-64,-32,0),
rect: new pc.Vec4(0,0,1,1),
texture: pc.app.assets.find('ui-slider_top-default.png').resource
});
uiTopSlider.addComponent('script');
uiTopSlider.script.create('slider');
uiTopSlider.setLocalScale(0.8,0.8,0.8);
uiSliders.addChild(uiTopSlider);
// bottom slider
var uiBotSlider = new pc.Entity('ui-bot-slider');
uiBotSlider.addComponent('element', {
type: pc.ELEMENTTYPE_IMAGE,
useInput: true,
anchor: new pc.Vec4(0.5,1,0.5,1),
pivot: new pc.Vec2(0.5,1),
width: 64,
height: 64,
margin: new pc.Vec4(-32,-412,-32,348),
rect: new pc.Vec4(0,0,1,1),
texture: pc.app.assets.find('ui-slider_top-default.png').resource
});
uiBotSlider.addComponent('script');
uiBotSlider.script.create('slider');
uiBotSlider.setLocalPosition(0,-348.8,0);
uiBotSlider.setLocalScale(0.8,0.8,0.8);
uiSliders.addChild(uiBotSlider);
screenEntity.addChild(uiSliders);
pc.app.root.addChild(screenEntity);
}
return {
pc: pc
}
}());
//# sourceURL=sample.js