-
Notifications
You must be signed in to change notification settings - Fork 514
/
video.ts
318 lines (266 loc) · 8.25 KB
/
video.ts
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*{# Copyright (c) 2012 Turbulenz Limited #}*/
/*
* @title: Video playback
* @description:
* This sample shows how to play a video into a texture.
*/
/*{{ javascript("jslib/observer.js") }}*/
/*{{ javascript("jslib/requesthandler.js") }}*/
/*{{ javascript("jslib/utilities.js") }}*/
/*{{ javascript("jslib/services/turbulenzservices.js") }}*/
/*{{ javascript("jslib/services/turbulenzbridge.js") }}*/
/*{{ javascript("jslib/services/gamesession.js") }}*/
/*{{ javascript("jslib/services/mappingtable.js") }}*/
/*global TurbulenzEngine: true */
/*global TurbulenzServices: false */
/*global RequestHandler: false */
TurbulenzEngine.onload = function onloadFn()
{
var graphicsDevice = TurbulenzEngine.createGraphicsDevice({});
// IE detection while WebGL implementation is incomplete
if (graphicsDevice && graphicsDevice.renderer === "Internet Explorer")
{
window.alert("The video sample is not supported on Internet Explorer");
return;
}
var soundDevice = TurbulenzEngine.createSoundDevice({});
var mathDevice = TurbulenzEngine.createMathDevice({});
var requestHandler = RequestHandler.create({});
var video;
var videoPosition = -1;
var shader, technique;
var texture;
var clearColor = mathDevice.v4Build(0, 0, 0, 1);
var clipSpace = mathDevice.v4Build(1, -1, 0, 0);
var videoColor = mathDevice.v4Build(1, 1, 1, 1);
var primitive = graphicsDevice.PRIMITIVE_TRIANGLE_STRIP;
var semantics = graphicsDevice.createSemantics(['POSITION', 'TEXCOORD0']);
var vertexBuffer = graphicsDevice.createVertexBuffer({
numVertices: 4,
attributes: [graphicsDevice.VERTEXFORMAT_FLOAT2,
graphicsDevice.VERTEXFORMAT_FLOAT2],
dynamic: false,
data: [
-1.0, 1.0, 0.0, 1.0,
1.0, 1.0, 1.0, 1.0,
-1.0, -1.0, 0.0, 0.0,
1.0, -1.0, 1.0, 0.0
]
});
var source = soundDevice.createGlobalSource({
looping: true
});
var sound;
var assetsToLoad = 3;
function mappingTableReceived(mappingTable)
{
var videoURL;
if (graphicsDevice.isSupported("FILEFORMAT_WEBM"))
{
videoURL = mappingTable.getURL("videos/turbulenzanimation.webm");
}
else
{
videoURL = mappingTable.getURL("videos/turbulenzanimation.mp4");
}
graphicsDevice.createVideo({
src: videoURL,
looping: true,
onload: function (v)
{
if (v)
{
video = v;
assetsToLoad -= 1;
}
else
{
window.alert("Failed to load video!");
}
}
});
var soundURL;
if (soundDevice.isSupported("FILEFORMAT_OGG"))
{
soundURL = mappingTable.getURL("sounds/turbulenzanimation.ogg");
}
else
{
soundURL = mappingTable.getURL("sounds/turbulenzanimation.mp3");
}
soundDevice.createSound({
src: soundURL,
onload : function (s)
{
if (s)
{
sound = s;
assetsToLoad -= 1;
}
else
{
window.alert('Failed to load sound!');
}
}
});
function shaderLoaded(shaderText)
{
if (shaderText)
{
var shaderParameters = JSON.parse(shaderText);
shader = graphicsDevice.createShader(shaderParameters);
technique = shader.getTechnique("video");
assetsToLoad -= 1;
}
else
{
window.alert("Failed to load shader!");
}
}
requestHandler.request({
src: mappingTable.getURL("shaders/video.cgfx"),
onload: shaderLoaded
});
}
var gameSession;
function sessionCreated()
{
TurbulenzServices.createMappingTable(
requestHandler,
gameSession,
mappingTableReceived
);
}
gameSession = TurbulenzServices.createGameSession(requestHandler, sessionCreated);
//==========================================================================
// Main loop.
//==========================================================================
var fpsElement = document.getElementById("fpscounter");
var lastFPS = "";
var nextUpdate = 0;
function displayPerformance()
{
var currentTime = TurbulenzEngine.time;
if (currentTime > nextUpdate)
{
nextUpdate = (currentTime + 0.1);
var fpsText = (graphicsDevice.fps).toFixed(2);
if (lastFPS !== fpsText)
{
lastFPS = fpsText;
fpsElement.innerHTML = fpsText + " fps";
}
}
}
function mainLoop()
{
soundDevice.update();
if (graphicsDevice.beginFrame())
{
var deviceWidth = graphicsDevice.width;
var deviceHeight = graphicsDevice.height;
var aspectRatio = (deviceWidth / deviceHeight);
var videoWidth = video.width;
var videoHeight = video.height;
var videoAspectRatio = (videoWidth / videoHeight);
var x, y;
if (aspectRatio < videoAspectRatio)
{
x = 1;
y = aspectRatio / videoAspectRatio;
}
else //if (aspectRatio >= videoAspectRatio)
{
x = videoAspectRatio / aspectRatio;
y = 1;
}
var currentVideoPosition = video.tell;
if (currentVideoPosition &&
videoPosition !== currentVideoPosition)
{
if (currentVideoPosition < videoPosition)
{
// looped, sync
source.seek(videoPosition);
}
videoPosition = currentVideoPosition;
texture.setData(video);
}
graphicsDevice.clear(clearColor);
graphicsDevice.setTechnique(technique);
technique.texture = texture;
technique.clipSpace = mathDevice.v4Build(x, -y, 0, 0, clipSpace);
technique.color = videoColor;
graphicsDevice.setStream(vertexBuffer, semantics);
graphicsDevice.draw(primitive, 4);
graphicsDevice.endFrame();
if (fpsElement)
{
displayPerformance();
}
}
}
var intervalID;
function loadingLoop()
{
if (assetsToLoad === 0)
{
TurbulenzEngine.clearInterval(intervalID);
source.play(sound);
video.play();
texture = graphicsDevice.createTexture({
width: video.width,
height: video.height,
mipmaps: false,
format: 'R8G8B8',
dynamic: true,
data: video
});
videoPosition = video.tell;
intervalID = TurbulenzEngine.setInterval(mainLoop, 1000 / 60);
}
}
intervalID = TurbulenzEngine.setInterval(loadingLoop, 100);
// Create a scene destroy callback to run when the window is closed
TurbulenzEngine.onunload = function destroyScene()
{
TurbulenzEngine.clearInterval(intervalID);
if (texture)
{
texture.destroy();
texture = null;
}
if (shader)
{
shader.destroy();
technique = null;
shader = null;
}
if (video)
{
video.destroy();
video = null;
}
if (vertexBuffer)
{
vertexBuffer.destroy();
vertexBuffer = null;
}
if (source)
{
source.destroy();
source = null;
}
if (sound)
{
sound.destroy();
sound = null;
}
fpsElement = null;
if (gameSession)
{
gameSession.destroy();
gameSession = null;
}
};
};