Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

4562 рядки
175KB

  1. /*
  2. * KNWebGLObjects.js
  3. * Keynote HTML Player
  4. *
  5. * Created by Tungwei Cheng
  6. * Copyright (c) 2016-2019 Apple Inc. All rights reserved.
  7. */
  8. var kShaderUniformGravity = "Gravity";
  9. var kShaderUniformMaskTexture = "MaskTexture";
  10. var kShaderUniformNoiseAmount = "NoiseAmount";
  11. var kShaderUniformNoiseMax = "NoiseMax";
  12. var kShaderUniformNoiseSeed = "NoiseSeed";
  13. var kShaderUniformParticleBurstTiming = "ParticleBurstTiming";
  14. var kShaderUniformPreviousParticleBurstTiming = "PreviousParticleBurstTiming";
  15. var kShaderUniformPreviousPercent = "PreviousPercent";
  16. var kShaderUniformShouldSparkle = "ShouldSparkle";
  17. var kShaderUniformSparklePeriod = "SparklePeriod";
  18. var kShaderUniformSparkleStartTime = "SparkleStartTime";
  19. var kShaderUniformStartScale = "StartScale";
  20. var kShimmerUniformParticleScalePercent = "ParticleScalePercent";
  21. var kShimmerUniformRotationMatrix = "RotationMatrix";
  22. var KNSparkleMaxParticleLife = 0.667;
  23. var KNWebGLRenderer = Class.create({
  24. initialize: function(params) {
  25. var canvas = this.canvas = params.canvas;
  26. this.canvasId = params.canvasId;
  27. this.textureAssets = params.textureAssets;
  28. this.durationMax = params.overallEndTime * 1000;
  29. this.glPrograms = [];
  30. // to be used in request animation frame
  31. this.elapsed = 0;
  32. // attempt to create webgl context
  33. var gl = this.gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
  34. // if webgl is not supported then set noGL to true
  35. if (!gl) {
  36. this.noGL = true;
  37. return;
  38. }
  39. // indicate if the animation has started for this renderer
  40. this.animationStarted = false;
  41. gl.viewportWidth = canvas.width;
  42. gl.viewportHeight = canvas.height;
  43. // create default project matrix
  44. this.initMVPMatrix();
  45. // initialize core animation wrapper
  46. this.coreAnimationWrapper = new KNWebGLCoreAnimationWrapper(gl);
  47. },
  48. initMVPMatrix: function() {
  49. var gl = this.gl;
  50. var w = gl.viewportWidth;
  51. var h = gl.viewportHeight;
  52. var fovradians = 20 * (Math.PI / 180);
  53. var backupDistance = h / (2 * Math.tan(fovradians / 2));
  54. var frontclipping = backupDistance - (w * 1.5);
  55. var backclipping = backupDistance + (w * 15.0);
  56. // create default ortho and proj matrices
  57. this.slideProjectionMatrix = WebGraphics.makePerspectiveMatrix4(20, w / h, Math.max(1, frontclipping), backclipping);
  58. var translate = WebGraphics.translateMatrix4(WebGraphics.createMatrix4(), -w / 2, -h / 2, -backupDistance);
  59. this.slideProjectionMatrix = WebGraphics.multiplyMatrix4(this.slideProjectionMatrix, translate);
  60. this.slideOrthoMatrix = WebGraphics.makeOrthoMatrix4(0, w, 0, h, -1, 1);
  61. },
  62. setupTexture: function(effect) {
  63. var textures = [];
  64. this.textureInfoFromEffect(effect.kpfLayer, effect.name, {"pointX": 0, "pointY": 0}, effect.baseLayer.initialState.opacity, textures);
  65. for (var i = 0, length = textures.length; i < length; i++) {
  66. var textureId = textures[i].textureId;
  67. var image = this.textureAssets[textureId];
  68. textures[i].texture = KNWebGLUtil.createTexture(this.gl, image);
  69. var toTextureId = textures[i].toTextureId;
  70. if (toTextureId) {
  71. var toTextureImage = this.textureAssets[toTextureId];
  72. textures[i].toTexture = KNWebGLUtil.createTexture(this.gl, toTextureImage);
  73. }
  74. }
  75. return textures;
  76. },
  77. textureInfoFromEffect: function(kpfLayer, name, offset, parentOpacity, textures) {
  78. var textureInfo = {};
  79. textureInfo.offset = {
  80. "pointX": offset.pointX + kpfLayer.bounds.offset.pointX,
  81. "pointY": offset.pointY + kpfLayer.bounds.offset.pointY
  82. };
  83. textureInfo.parentOpacity = parentOpacity * kpfLayer.initialState.opacity;
  84. if (kpfLayer.textureId) {
  85. textureInfo.textureId = kpfLayer.textureId;
  86. textureInfo.width = kpfLayer.bounds.width;
  87. textureInfo.height = kpfLayer.bounds.height;
  88. textureInfo.initialState = kpfLayer.initialState;
  89. textureInfo.hasHighlightedBulletAnimation = kpfLayer.hasHighlightedBulletAnimation;
  90. textureInfo.texturedRectangle = kpfLayer.texturedRectangle;
  91. // search the animations within group for contents animation
  92. var groupAnimations = kpfLayer.animations;
  93. if (groupAnimations && groupAnimations.length > 0) {
  94. var groupAnimation = groupAnimations[0];
  95. if (groupAnimation.property === "contents") {
  96. textureInfo.toTextureId = groupAnimation.to.texture;
  97. } else if (!groupAnimation.property) {
  98. var animations = groupAnimation.animations;
  99. if (animations) {
  100. for (var i = 0, length = animations.length; i < length; i++) {
  101. var animation = animations[i];
  102. if (animation.property === "contents") {
  103. textureInfo.toTextureId = animation.to.texture;
  104. break;
  105. }
  106. }
  107. }
  108. }
  109. }
  110. textureInfo.animations = groupAnimations;
  111. textureInfo.textureRect = {
  112. origin: {
  113. x: textureInfo.offset.pointX,
  114. y: textureInfo.offset.pointY
  115. },
  116. size: {
  117. width: textureInfo.width,
  118. height: textureInfo.height
  119. }
  120. };
  121. textures.push(textureInfo);
  122. } else {
  123. for (var i = 0, length = kpfLayer.layers.length; i < length; i++) {
  124. this.textureInfoFromEffect(kpfLayer.layers[i], name, textureInfo.offset, textureInfo.parentOpacity, textures);
  125. }
  126. }
  127. },
  128. draw: function(effect) {
  129. var params = {
  130. effect: effect,
  131. textures: this.setupTexture(effect)
  132. };
  133. var effectType = effect.type;
  134. var program;
  135. if (effectType === "transition") {
  136. switch (effect.name) {
  137. case "apple:wipe-iris":
  138. program = new KNWebGLTransitionIris(this, params);
  139. break;
  140. case "com.apple.iWork.Keynote.BUKTwist":
  141. program = new KNWebGLTransitionTwist(this, params);
  142. break;
  143. case "com.apple.iWork.Keynote.KLNColorPlanes":
  144. program = new KNWebGLTransitionColorPlanes(this, params);
  145. break;
  146. case "com.apple.iWork.Keynote.BUKFlop":
  147. program = new KNWebGLTransitionFlop(this, params);
  148. break;
  149. case "com.apple.iWork.Keynote.KLNConfetti":
  150. program = new KNWebGLTransitionConfetti(this, params);
  151. break;
  152. case "apple:magic-move-implied-motion-path":
  153. program = new KNWebGLTransitionMagicMove(this, params);
  154. break;
  155. case "apple:ca-text-shimmer":
  156. program = new KNWebGLTransitionShimmer(this, params);
  157. break;
  158. case "apple:ca-text-sparkle":
  159. program = new KNWebGLTransitionSparkle(this, params);
  160. break;
  161. default:
  162. // fallback to dissolve
  163. program = new KNWebGLDissolve(this, params);
  164. break;
  165. }
  166. } else if (effectType === "buildIn" || effectType === "buildOut") {
  167. switch (effect.name) {
  168. case "apple:wipe-iris":
  169. program = new KNWebGLBuildIris(this, params);
  170. break;
  171. case "com.apple.iWork.Keynote.BUKAnvil":
  172. program = new KNWebGLBuildAnvil(this, params);
  173. break;
  174. case "com.apple.iWork.Keynote.KLNFlame":
  175. program = new KNWebGLBuildFlame(this, params);
  176. break;
  177. case "com.apple.iWork.Keynote.KNFireworks":
  178. program = new KNWebGLBuildFireworks(this, params);
  179. break;
  180. case "com.apple.iWork.Keynote.KLNConfetti":
  181. program = new KNWebGLBuildConfetti(this, params);
  182. break;
  183. case "com.apple.iWork.Keynote.KLNDiffuse":
  184. program = new KNWebGLBuildDiffuse(this, params);
  185. break;
  186. case "com.apple.iWork.Keynote.KLNShimmer":
  187. program = new KNWebGLBuildShimmer(this, params);
  188. break;
  189. case "com.apple.iWork.Keynote.KLNSparkle":
  190. program = new KNWebGLBuildSparkle(this, params);
  191. break;
  192. default:
  193. // fallback to dissolve
  194. program = new KNWebGLDissolve(this, params);
  195. break;
  196. }
  197. } else if (effectType === "smartBuild") {
  198. switch (effect.name) {
  199. case "apple:gallery-dissolve":
  200. program = new KNWebGLContents(this, params);
  201. break;
  202. default:
  203. // fallback to dissolve
  204. program = new KNWebGLDissolve(this, params);
  205. break;
  206. }
  207. }
  208. // remove existing gl program for the same object when new program is rendered such as build in by highlighted paragraph
  209. this.removeProgram(effect.objectID);
  210. // push new gl program into the array
  211. this.glPrograms.push(program);
  212. },
  213. animate: function() {
  214. // compute time difference
  215. var time = new Date();
  216. var difference = 0;
  217. if (this.time) {
  218. var mseconds = time.getTime();
  219. difference = mseconds - this.time;
  220. this.time = mseconds;
  221. } else {
  222. difference = 0;
  223. this.time = time.getTime();
  224. }
  225. this.elapsed += difference;
  226. var glPrograms = this.glPrograms;
  227. var length = glPrograms.length;
  228. if (this.elapsed <= this.durationMax) {
  229. // set up the frame for the next drawing operation, only if there is time left in the animation
  230. this.animationRequest = window.requestAnimFrame(this.animate.bind(this));
  231. } else {
  232. // set gl program to isCompleted when there is no overall event time left
  233. for (var i = 0; i < length; i++) {
  234. var program = glPrograms[i];
  235. program.isCompleted = true;
  236. }
  237. }
  238. // clear the buffers before animation frame
  239. var gl = this.gl;
  240. gl.clearColor(0, 0, 0, 0);
  241. gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  242. for (var i = 0; i < length; i++) {
  243. var program = glPrograms[i];
  244. program.drawFrame(difference, this.elapsed, program.duration);
  245. }
  246. },
  247. removeProgram: function(objectID) {
  248. var glPrograms = this.glPrograms;
  249. var glProgramLength = glPrograms.length;
  250. // remove gl program for the same objectID from the array
  251. while (glProgramLength--) {
  252. var glProgram = glPrograms[glProgramLength];
  253. if (glProgram.effect.objectID === objectID) {
  254. glPrograms.splice(glProgramLength, 1);
  255. }
  256. }
  257. },
  258. resize: function(viewport) {
  259. var gl = this.gl;
  260. var viewportWidth = viewport.width;
  261. var viewportHeight = viewport.height;
  262. if (gl.viewportWidth !== viewportWidth || gl.viewportHeight !== viewportHeight) {
  263. gl.viewport(0, 0, viewportWidth, viewportHeight);
  264. gl.viewportWidth = viewportWidth;
  265. gl.viewportHeight = viewportHeight;
  266. }
  267. }
  268. });
  269. var KNWebGLProgram = Class.create({
  270. initialize: function(renderer, programData) {
  271. // reference to the renderer
  272. this.renderer = renderer;
  273. // reference to gl context
  274. this.gl = renderer.gl;
  275. // specify textures
  276. this.textures = programData.textures;
  277. // reference to the effect object
  278. var effect = this.effect = programData.effect;
  279. // specify the effect type
  280. var type = this.type = effect.type;
  281. // specific the direction from the effect
  282. this.direction = effect.attributes ? effect.attributes.direction : null;
  283. // specify the duration from the effect
  284. this.duration = effect.duration * 1000;
  285. // boolean to indicate if the effect is a build out
  286. this.buildOut = type === "buildOut";
  287. // boolean to indicate if the effect is a build in
  288. this.buildIn = type === "buildIn";
  289. // create a shader program container object
  290. this.program = {};
  291. // indicate if the effect is completed
  292. this.isCompleted = false;
  293. // setup program data
  294. if (programData.programNames) {
  295. this.setupProgram(programData);
  296. }
  297. },
  298. setupProgram: function(programData) {
  299. var gl = this.gl;
  300. for (var i = 0, length = programData.programNames.length; i < length; i++) {
  301. var programName = programData.programNames[i];
  302. this.program[programName] = KNWebGLUtil.setupProgram(gl, programName);
  303. }
  304. // enable blend function
  305. gl.enable(gl.BLEND);
  306. gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
  307. }
  308. });
  309. var KNWebGLContents = Class.create(KNWebGLProgram, {
  310. initialize: function($super, renderer, params) {
  311. this.programData = {
  312. name: "contents",
  313. effect: params.effect,
  314. textures: params.textures
  315. };
  316. $super(renderer, this.programData);
  317. // initialize percent finish based on effect type
  318. this.percentfinished = 0;
  319. // setup requirements
  320. this.animationWillBeginWithContext();
  321. },
  322. animationWillBeginWithContext: function() {
  323. var renderer = this.renderer;
  324. var gl = this.gl;
  325. var textureRect = this.textures[0].textureRect;
  326. var vertexRect = CGRectMake(0, 0, textureRect.size.width, textureRect.size.height);
  327. var meshSize = CGSizeMake(2, 2);
  328. // init contents shader and data buffer
  329. var contentsShader = this.contentsShader = new TSDGLShader(gl);
  330. contentsShader.initWithContentsShader();
  331. // contents shader set methods
  332. contentsShader.setMat4WithTransform3D(renderer.slideProjectionMatrix, kTSDGLShaderUniformMVPMatrix);
  333. // outgoing Texture
  334. contentsShader.setGLint(0, kTSDGLShaderUniformTexture2);
  335. // incoming Texture
  336. contentsShader.setGLint(1, kTSDGLShaderUniformTexture);
  337. // init contents data buffer
  338. var contentsDataBuffer = this.contentsDataBuffer = new TSDGLDataBuffer(gl);
  339. contentsDataBuffer.initWithVertexRect(vertexRect, TSDRectUnit, meshSize, false, false);
  340. },
  341. drawFrame: function(difference, elapsed, duration) {
  342. var renderer = this.renderer;
  343. var gl = this.gl;
  344. var percentfinished = this.percentfinished;
  345. percentfinished += difference / duration;
  346. if (percentfinished >= 1) {
  347. percentfinished = 1;
  348. this.isCompleted = true;
  349. }
  350. this.percentfinished = percentfinished;
  351. // draw contents using glsl mix
  352. this.p_drawContents(percentfinished);
  353. },
  354. p_drawContents: function(percent) {
  355. var gl = this.gl;
  356. var textures = this.textures;
  357. var incomingTexture = textures[0].texture;
  358. var outgoingTexture = textures[1].texture;
  359. // calculate the mix factor in ease in and ease out fashion
  360. var mixFactor = TSUSineMap(percent);
  361. if (percent >= 1) {
  362. mixFactor = 1.0;
  363. }
  364. gl.activeTexture(gl.TEXTURE1);
  365. gl.bindTexture(gl.TEXTURE_2D, incomingTexture);
  366. gl.activeTexture(gl.TEXTURE0);
  367. gl.bindTexture(gl.TEXTURE_2D, outgoingTexture);
  368. this.contentsShader.setGLFloat(mixFactor, "mixFactor");
  369. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  370. this.contentsDataBuffer.drawWithShader(this.contentsShader, true);
  371. }
  372. });
  373. var KNWebGLDrawable = Class.create(KNWebGLProgram, {
  374. initialize: function($super, renderer, params) {
  375. this.programData = {
  376. name: "WebDrawable",
  377. programNames:["defaultTextureAndOpacity"],
  378. effect: params.effect,
  379. textures: params.textures
  380. };
  381. $super(renderer, this.programData);
  382. this.Opacity = 1.0;
  383. // setup web drawable requirements
  384. this.animationWillBeginWithContext();
  385. },
  386. animationWillBeginWithContext: function() {
  387. var renderer = this.renderer;
  388. var gl = this.gl;
  389. var program = this.program["defaultTextureAndOpacity"];
  390. var uniforms = program.uniforms;
  391. var attribs = program.attribs;
  392. var textureInfo = this.textures[0];
  393. gl.useProgram(program.shaderProgram);
  394. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  395. // create WebGLBuffer object for texture coordinates
  396. var textureCoordinateBuffer = this.textureCoordinateBuffer = gl.createBuffer();
  397. var textureCoordinates = this.textureCoordinates = [
  398. 0.0, 0.0,
  399. 0.0, 1.0,
  400. 1.0, 0.0,
  401. 1.0, 1.0,
  402. ];
  403. // bind WebGLBuffer object to gl.ARRAY_BUFFER target
  404. gl.bindBuffer(gl.ARRAY_BUFFER, textureCoordinateBuffer);
  405. // send vertex data to this bound buffer
  406. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoordinates), gl.STATIC_DRAW);
  407. // create WebGLBuffer object for position coordinates
  408. var positionBuffer = this.positionBuffer = gl.createBuffer();
  409. var boxPosition = this.boxPosition = [
  410. 0.0, 0.0, 0.0,
  411. 0.0, textureInfo.height, 0.0,
  412. textureInfo.width, 0.0, 0.0,
  413. textureInfo.width, textureInfo.height, 0.0
  414. ];
  415. // bind WebGLBuffer object to gl.ARRAY_BUFFER target
  416. gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
  417. // send vertex data to this bound buffer
  418. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(boxPosition), gl.STATIC_DRAW);
  419. // move the MVPMatrix to appropriate offset
  420. this.MVPMatrix = WebGraphics.translateMatrix4(renderer.slideProjectionMatrix, textureInfo.offset.pointX, gl.viewportHeight - textureInfo.offset.pointY - textureInfo.height, 0);
  421. },
  422. drawFrame: function() {
  423. var renderer = this.renderer;
  424. var gl = this.gl;
  425. var program = this.program["defaultTextureAndOpacity"];
  426. var uniforms = program.uniforms;
  427. var attribs = program.attribs;
  428. var textures = this.textures;
  429. var texture = textures[0].texture;
  430. gl.useProgram(program.shaderProgram);
  431. // bind WebGLBuffer object to gl.ARRAY_BUFFER target
  432. gl.bindBuffer(gl.ARRAY_BUFFER, this.textureCoordinateBuffer);
  433. // assigns the WebGLBuffer object currently bound to the gl.ARRAY_BUFFER target to a vertex attribute index
  434. gl.vertexAttribPointer(attribs["TexCoord"], 2, gl.FLOAT, false, 0, 0);
  435. // call enableVertexAttribArray, otherwise it won't draw
  436. gl.enableVertexAttribArray(attribs["TexCoord"]);
  437. // bind WebGLBuffer object to gl.ARRAY_BUFFER target
  438. gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
  439. // assigns the WebGLBuffer object currently bound to the gl.ARRAY_BUFFER target to a vertex attribute index
  440. gl.vertexAttribPointer(attribs["Position"], 3, gl.FLOAT, false, 0, 0);
  441. // call enableVertexAttribArray, otherwise it won't draw
  442. gl.enableVertexAttribArray(attribs["Position"]);
  443. // set MVPMatrix
  444. gl.uniformMatrix4fv(uniforms["MVPMatrix"], false, this.MVPMatrix);
  445. // set Opacity
  446. gl.uniform1f(uniforms["Opacity"], this.Opacity);
  447. // set sampler2D Texture in fragment shader to have the value 0, so it matches the texture unit gl.TEXTURE0
  448. gl.activeTexture(gl.TEXTURE0);
  449. gl.uniform1i(uniforms["Texture"], 0);
  450. // bind the texture to texture unit gl.TEXTURE0
  451. gl.bindTexture(gl.TEXTURE_2D, texture);
  452. gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
  453. }
  454. });
  455. var KNWebGLFramebufferDrawable = Class.create(KNWebGLProgram, {
  456. initialize: function($super, renderer, params) {
  457. var gl = renderer.gl;
  458. var frameRect = this.frameRect = params.frameRect;
  459. var texture = this.texture = this.createFramebufferTexture(gl, frameRect);
  460. this.buffer = this.createFramebuffer(gl, texture);
  461. var textureInfo = {
  462. width: frameRect.size.width,
  463. height: frameRect.size.height,
  464. offset: {pointX: 0, pointY: 0},
  465. texture: texture
  466. };
  467. this.programData = {
  468. name: "FramebufferDrawable",
  469. programNames:["defaultTexture"],
  470. effect: params.effect,
  471. textures: [textureInfo]
  472. };
  473. $super(renderer, this.programData);
  474. this.drawableFrame = params.drawableFrame;
  475. // setup web drawable requirements
  476. this.animationWillBeginWithContext();
  477. },
  478. createFramebufferTexture: function(gl, rect) {
  479. var texture = gl.createTexture();
  480. // bind texture
  481. gl.bindTexture(gl.TEXTURE_2D, texture);
  482. gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
  483. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
  484. // setup texture parameters
  485. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  486. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  487. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  488. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  489. // specify the texture size for memory allocation
  490. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, rect.size.width, rect.size.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
  491. // unbind texture
  492. gl.bindTexture(gl.TEXTURE_2D, null);
  493. return texture;
  494. },
  495. createFramebuffer: function(gl, texture) {
  496. var buffer = gl.createFramebuffer();
  497. //bind framebuffer to texture
  498. gl.bindFramebuffer(gl.FRAMEBUFFER, buffer);
  499. gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
  500. return buffer;
  501. },
  502. animationWillBeginWithContext: function() {
  503. var renderer = this.renderer;
  504. var gl = this.gl;
  505. var program = this.program["defaultTexture"];
  506. var uniforms = program.uniforms;
  507. var attribs = program.attribs;
  508. var textureInfo = this.textures[0];
  509. gl.useProgram(program.shaderProgram);
  510. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  511. // create WebGLBuffer object for texture coordinates
  512. var textureCoordinateBuffer = this.textureCoordinateBuffer = gl.createBuffer();
  513. var textureCoordinates = this.textureCoordinates = [
  514. 0.0, 1.0,
  515. 0.0, 0.0,
  516. 1.0, 1.0,
  517. 1.0, 0.0,
  518. ];
  519. // bind WebGLBuffer object to gl.ARRAY_BUFFER target
  520. gl.bindBuffer(gl.ARRAY_BUFFER, textureCoordinateBuffer);
  521. // send vertex data to this bound buffer
  522. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoordinates), gl.STATIC_DRAW);
  523. // create WebGLBuffer object for position coordinates
  524. var positionBuffer = this.positionBuffer = gl.createBuffer();
  525. var boxPosition = this.boxPosition = [
  526. 0.0, 0.0, 0.0,
  527. 0.0, textureInfo.height, 0.0,
  528. textureInfo.width, 0.0, 0.0,
  529. textureInfo.width, textureInfo.height, 0.0
  530. ];
  531. // bind WebGLBuffer object to gl.ARRAY_BUFFER target
  532. gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
  533. // send vertex data to this bound buffer
  534. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(boxPosition), gl.STATIC_DRAW);
  535. // move the MVPMatrix to appropriate offset
  536. this.MVPMatrix = WebGraphics.translateMatrix4(renderer.slideProjectionMatrix, textureInfo.offset.pointX, gl.viewportHeight - textureInfo.offset.pointY - textureInfo.height, 0);
  537. },
  538. drawFrame: function() {
  539. var renderer = this.renderer;
  540. var gl = this.gl;
  541. var program = this.program["defaultTexture"];
  542. var uniforms = program.uniforms;
  543. var attribs = program.attribs;
  544. var textures = this.textures;
  545. var texture = textures[0].texture;
  546. gl.useProgram(program.shaderProgram);
  547. // bind WebGLBuffer object to gl.ARRAY_BUFFER target
  548. gl.bindBuffer(gl.ARRAY_BUFFER, this.textureCoordinateBuffer);
  549. // assigns the WebGLBuffer object currently bound to the gl.ARRAY_BUFFER target to a vertex attribute index
  550. gl.vertexAttribPointer(attribs["TexCoord"], 2, gl.FLOAT, false, 0, 0);
  551. // call enableVertexAttribArray, otherwise it won't draw
  552. gl.enableVertexAttribArray(attribs["TexCoord"]);
  553. // bind WebGLBuffer object to gl.ARRAY_BUFFER target
  554. gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
  555. // assigns the WebGLBuffer object currently bound to the gl.ARRAY_BUFFER target to a vertex attribute index
  556. gl.vertexAttribPointer(attribs["Position"], 3, gl.FLOAT, false, 0, 0);
  557. // call enableVertexAttribArray, otherwise it won't draw
  558. gl.enableVertexAttribArray(attribs["Position"]);
  559. // set MVPMatrix
  560. gl.uniformMatrix4fv(uniforms["MVPMatrix"], false, this.MVPMatrix);
  561. // set sampler2D Texture in fragment shader to have the value 0, so it matches the texture unit gl.TEXTURE0
  562. gl.activeTexture(gl.TEXTURE0);
  563. gl.uniform1i(uniforms["Texture"], 0);
  564. // bind the texture to texture unit gl.TEXTURE0
  565. gl.bindTexture(gl.TEXTURE_2D, texture);
  566. gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
  567. }
  568. });
  569. var KNWebGLDissolve = Class.create(KNWebGLProgram, {
  570. initialize: function($super, renderer, params) {
  571. this.programData = {
  572. name: "dissolve",
  573. programNames:["defaultTextureAndOpacity"],
  574. effect: params.effect,
  575. textures: params.textures
  576. };
  577. $super(renderer, this.programData);
  578. // initialize percent finish based on effect type
  579. this.percentfinished = 0;
  580. // setup requirements
  581. this.animationWillBeginWithContext();
  582. },
  583. animationWillBeginWithContext: function() {
  584. var renderer = this.renderer;
  585. var gl = this.gl;
  586. var program = this.program["defaultTextureAndOpacity"];
  587. var uniforms = program.uniforms;
  588. var attribs = program.attribs;
  589. var textureInfo = this.textures[0];
  590. gl.useProgram(program.shaderProgram);
  591. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  592. // create WebGLBuffer object for texture coordinates
  593. var textureCoordinateBuffer = this.textureCoordinateBuffer = gl.createBuffer();
  594. var textureCoordinates = this.textureCoordinates = [
  595. 0.0, 0.0,
  596. 0.0, 1.0,
  597. 1.0, 0.0,
  598. 1.0, 1.0,
  599. ];
  600. // bind WebGLBuffer object to gl.ARRAY_BUFFER target
  601. gl.bindBuffer(gl.ARRAY_BUFFER, textureCoordinateBuffer);
  602. // send vertex data to this bound buffer
  603. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoordinates), gl.STATIC_DRAW);
  604. // create WebGLBuffer object for position coordinates
  605. var positionBuffer = this.positionBuffer = gl.createBuffer();
  606. var boxPosition = this.boxPosition = [
  607. 0.0, 0.0, 0.0,
  608. 0.0, textureInfo.height, 0.0,
  609. textureInfo.width, 0.0, 0.0,
  610. textureInfo.width, textureInfo.height, 0.0
  611. ];
  612. // bind WebGLBuffer object to gl.ARRAY_BUFFER target
  613. gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
  614. // send vertex data to this bound buffer
  615. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(boxPosition), gl.STATIC_DRAW);
  616. this.MVPMatrix = WebGraphics.translateMatrix4(renderer.slideProjectionMatrix, textureInfo.offset.pointX, gl.viewportHeight - (textureInfo.offset.pointY + textureInfo.height), 0);
  617. this.drawFrame(0, 0, 4);
  618. },
  619. drawFrame: function(difference, elapsed, duration) {
  620. var percentfinished = this.percentfinished;
  621. percentfinished += difference / duration;
  622. percentfinished > 1 ? percentfinished = 1 : 0;
  623. var percentAlpha = TSUSineMap(percentfinished);
  624. if (percentfinished === 1) {
  625. percentAlpha = 1.0;
  626. }
  627. if (this.buildOut) {
  628. percentAlpha = 1 - percentAlpha;
  629. }
  630. this.percentfinished = percentfinished;
  631. this.percentAlpha = percentAlpha;
  632. this.draw();
  633. },
  634. draw: function() {
  635. var renderer = this.renderer;
  636. var gl = this.gl;
  637. var program = this.program["defaultTextureAndOpacity"];
  638. var uniforms = program.uniforms;
  639. var attribs = program.attribs;
  640. var textures = this.textures;
  641. var texture = textures[0].texture;
  642. var outgoingTexture;
  643. if (textures.length > 1) {
  644. outgoingTexture = textures[1].texture;
  645. }
  646. // use this program
  647. gl.useProgram(program.shaderProgram);
  648. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  649. // bind WebGLBuffer object to gl.ARRAY_BUFFER target
  650. gl.bindBuffer(gl.ARRAY_BUFFER, this.textureCoordinateBuffer);
  651. // send vertex data to this bound buffer
  652. gl.vertexAttribPointer(attribs["TexCoord"], 2, gl.FLOAT, false, 0, 0);
  653. // call enableVertexAttribArray, otherwise it won't draw
  654. gl.enableVertexAttribArray(attribs["TexCoord"]);
  655. // bind WebGLBuffer object to gl.ARRAY_BUFFER target
  656. gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
  657. // send vertex data to this bound buffer
  658. gl.vertexAttribPointer(attribs["Position"], 3, gl.FLOAT, false, 0, 0);
  659. // call enableVertexAttribArray, otherwise it won't draw
  660. gl.enableVertexAttribArray(attribs["Position"]);
  661. // set MVPMatrix
  662. gl.uniformMatrix4fv(uniforms["MVPMatrix"], false, this.MVPMatrix);
  663. // set sampler2D Texture in fragment shader to have the value 0, so it matches the texture unit gl.TEXTURE0
  664. gl.activeTexture(gl.TEXTURE0);
  665. gl.uniform1i(uniforms["Texture"], 0);
  666. // bind the texture to texture unit gl.TEXTURE0
  667. if (outgoingTexture) {
  668. gl.bindTexture(gl.TEXTURE_2D, outgoingTexture);
  669. gl.uniform1f(uniforms["Opacity"], 1.0);
  670. gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
  671. }
  672. gl.bindTexture(gl.TEXTURE_2D, texture);
  673. gl.uniform1f(uniforms["Opacity"], this.percentAlpha);
  674. gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
  675. }
  676. });
  677. var KNWebGLTransitionIris = Class.create(KNWebGLProgram, {
  678. initialize: function($super, renderer, params) {
  679. this.programData = {
  680. name: "apple:wipe-iris",
  681. programNames: ["iris"],
  682. effect: params.effect,
  683. textures: params.textures
  684. };
  685. $super(renderer, this.programData);
  686. // determine the type and direction
  687. var direction = this.direction;
  688. var directionOut = direction === KNDirection.kKNDirectionOut;
  689. var buildOut = this.buildOut;
  690. if ((buildOut && directionOut) || (!buildOut && !directionOut)) {
  691. this.mix = 0.0;
  692. this.percentfinished = 1.0;
  693. } else {
  694. this.mix = 1.0;
  695. this.percentfinished = 0.0;
  696. }
  697. this.percentAlpha = 0.0;
  698. // setup requirements
  699. this.animationWillBeginWithContext();
  700. },
  701. animationWillBeginWithContext: function() {
  702. var renderer = this.renderer;
  703. var gl = this.gl;
  704. var program = this.program["iris"];
  705. var attribs = program.attribs;
  706. var uniforms = program.uniforms;
  707. var textureInfo = this.textures[0];
  708. gl.useProgram(program.shaderProgram);
  709. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  710. // initial scale uniform
  711. this.scale = textureInfo.width/textureInfo.height;
  712. // create buffers
  713. var textureCoordinatesBuffer = this.textureCoordinatesBuffer = gl.createBuffer();
  714. var textureCoordinates = this.textureCoordinates = [
  715. 0.0, 0.0,
  716. 0.0, 1.0,
  717. 1.0, 0.0,
  718. 1.0, 1.0,
  719. ];
  720. gl.bindBuffer(gl.ARRAY_BUFFER, textureCoordinatesBuffer);
  721. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoordinates), gl.STATIC_DRAW);
  722. var positionBuffer = this.positionBuffer = gl.createBuffer();
  723. var boxPosition = this.boxPosition = [
  724. 0.0, 0.0, 0.0,
  725. 0.0, textureInfo.height, 0.0,
  726. textureInfo.width, 0.0, 0.0,
  727. textureInfo.width, textureInfo.height, 0.0
  728. ];
  729. gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
  730. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(boxPosition), gl.STATIC_DRAW);
  731. this.MVPMatrix = WebGraphics.translateMatrix4(renderer.slideProjectionMatrix, textureInfo.offset.pointX, gl.viewportHeight - (textureInfo.offset.pointY + textureInfo.height), 0);
  732. this.drawFrame(0, 0, 4);
  733. },
  734. drawFrame: function(difference, elapsed, duration) {
  735. // determine the type and direction
  736. var buildOut = this.buildOut;
  737. var directionOut = this.direction === KNDirection.kKNDirectionOut;
  738. var percentfinished = this.percentfinished;
  739. if ((buildOut && directionOut) || (!buildOut && !directionOut)) {
  740. percentfinished -= difference / duration;
  741. percentfinished < 0 ? percentfinished = 0 : 0;
  742. } else {
  743. percentfinished += difference / duration;
  744. percentfinished > 1 ? percentfinished = 1 : 0;
  745. }
  746. var percentAlpha = TSUSineMap(percentfinished);
  747. if (percentfinished === 1) {
  748. percentAlpha = 1.0;
  749. }
  750. if (buildOut) {
  751. percentAlpha = 1 - percentAlpha;
  752. }
  753. this.percentAlpha = percentAlpha;
  754. this.percentfinished = percentfinished;
  755. this.draw();
  756. },
  757. draw: function() {
  758. var renderer = this.renderer;
  759. var gl = this.gl;
  760. var program = this.program["iris"];
  761. var attribs = program.attribs;
  762. var uniforms = program.uniforms;
  763. var textures = this.textures;
  764. var texture = textures[0].texture;
  765. var textureInfo = textures[0];
  766. var outgoingTexture;
  767. var scale = this.scale;
  768. if (textures.length > 1) {
  769. outgoingTexture = textures[1].texture;
  770. }
  771. gl.useProgram(program.shaderProgram);
  772. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  773. // setup attributes
  774. gl.bindBuffer(gl.ARRAY_BUFFER, this.textureCoordinatesBuffer);
  775. gl.vertexAttribPointer(attribs["TexCoord"], 2, gl.FLOAT, false, 0, 0);
  776. gl.enableVertexAttribArray(attribs["TexCoord"]);
  777. gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
  778. gl.vertexAttribPointer(attribs["Position"], 3, gl.FLOAT, false, 0, 0);
  779. gl.enableVertexAttribArray(attribs["Position"]);
  780. // setup uniforms and textures
  781. gl.uniformMatrix4fv(uniforms["MVPMatrix"], false, this.MVPMatrix);
  782. gl.activeTexture(gl.TEXTURE0);
  783. gl.uniform1i(uniforms["Texture"], 0);
  784. // set Opacity
  785. gl.uniform1f(uniforms["Opacity"], 1);
  786. // bg texture
  787. if (outgoingTexture) {
  788. gl.bindTexture(gl.TEXTURE_2D, outgoingTexture);
  789. gl.uniform1f(uniforms["PercentForAlpha"], 0.0);
  790. gl.uniform1f(uniforms["Scale"], scale);
  791. gl.uniform1f(uniforms["Mix"], 0.0);
  792. gl.drawArrays(gl.TRIANGLE_STRIP, 0 , 4);
  793. }
  794. //fg texture
  795. gl.bindTexture(gl.TEXTURE_2D, texture);
  796. gl.uniform1f(uniforms["PercentForAlpha"], this.percentAlpha);
  797. gl.uniform1f(uniforms["Scale"], scale);
  798. gl.uniform1f(uniforms["Mix"], this.mix);
  799. gl.drawArrays(gl.TRIANGLE_STRIP, 0 , 4);
  800. }
  801. });
  802. var KNWebGLBuildIris = Class.create(KNWebGLProgram, {
  803. initialize: function($super, renderer, params) {
  804. var effect = params.effect;
  805. this.programData = {
  806. name: "apple:wipe-iris",
  807. programNames: ["iris"],
  808. effect: effect,
  809. textures: params.textures
  810. };
  811. $super(renderer, this.programData);
  812. // determine the type and direction
  813. var direction = this.direction;
  814. var directionOut = direction === KNDirection.kKNDirectionOut;
  815. var buildOut = this.buildOut;
  816. if ((buildOut && directionOut) || (!buildOut && !directionOut)) {
  817. this.mix = 0.0;
  818. this.percentfinished = 1.0;
  819. } else {
  820. this.mix = 1.0;
  821. this.percentfinished = 0.0;
  822. }
  823. this.percentAlpha = 0.0;
  824. // create drawable object for drawing static texture
  825. this.drawableObjects = [];
  826. for (var i = 0, length = this.textures.length; i < length; i++) {
  827. var texture = params.textures[i];
  828. var drawableParams = {
  829. effect: effect,
  830. textures: [texture]
  831. };
  832. var drawableObject = new KNWebGLDrawable(renderer, drawableParams);
  833. this.drawableObjects.push(drawableObject);
  834. }
  835. // set parent opacity from CA baseLayer
  836. this.parentOpacity = effect.baseLayer.initialState.opacity;
  837. // setup requirements
  838. this.animationWillBeginWithContext();
  839. },
  840. animationWillBeginWithContext: function() {
  841. var renderer = this.renderer;
  842. var gl = this.gl;
  843. var program = this.program["iris"];
  844. var attribs = program.attribs;
  845. var uniforms = program.uniforms;
  846. gl.useProgram(program.shaderProgram);
  847. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  848. // setup attributes
  849. var textureCoordinatesBuffer = gl.createBuffer();
  850. var textureCoordinates = [
  851. 0.0, 0.0,
  852. 0.0, 1.0,
  853. 1.0, 0.0,
  854. 1.0, 1.0,
  855. ];
  856. gl.bindBuffer(gl.ARRAY_BUFFER, textureCoordinatesBuffer);
  857. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoordinates), gl.STATIC_DRAW);
  858. var viewportWidth = gl.viewportWidth;
  859. var viewportHeight = gl.viewportHeight;
  860. this.irisSystems = [];
  861. for (var i = 0, length = this.textures.length; i < length; i++) {
  862. var textureInfo = this.textures[i];
  863. var width = textureInfo.width;
  864. var height = textureInfo.height;
  865. // initial scale uniform
  866. var scale = textureInfo.width/textureInfo.height;
  867. var positionBuffer = gl.createBuffer();
  868. var boxPosition = [
  869. 0.0, 0.0, 0.0,
  870. 0.0, textureInfo.height, 0.0,
  871. textureInfo.width, 0.0, 0.0,
  872. textureInfo.width, textureInfo.height, 0.0
  873. ];
  874. gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
  875. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(boxPosition), gl.STATIC_DRAW);
  876. var MVPMatrix = WebGraphics.translateMatrix4(renderer.slideProjectionMatrix, textureInfo.offset.pointX, gl.viewportHeight - (textureInfo.offset.pointY + textureInfo.height), 0);
  877. this.irisSystems[i] = {
  878. textureCoordinatesBuffer: textureCoordinatesBuffer,
  879. positionBuffer: positionBuffer,
  880. MVPMatrix: MVPMatrix,
  881. scale: scale
  882. };
  883. }
  884. },
  885. drawFrame: function(difference, elapsed, duration) {
  886. var renderer = this.renderer;
  887. var gl = this.gl;
  888. // determine the type and direction
  889. var buildOut = this.buildOut;
  890. var directionOut = this.direction === KNDirection.kKNDirectionOut;
  891. var percentfinished = this.percentfinished;
  892. if ((buildOut && directionOut) || (!buildOut && !directionOut)) {
  893. percentfinished -= difference / duration;
  894. if (percentfinished <= 0) {
  895. percentfinished = 0;
  896. this.isCompleted = true;
  897. }
  898. } else {
  899. percentfinished += difference / duration;
  900. if (percentfinished >= 1) {
  901. percentfinished = 1;
  902. this.isCompleted = true;
  903. }
  904. }
  905. var percentAlpha = TSUSineMap(percentfinished);
  906. if (percentfinished === 1) {
  907. percentAlpha = 1.0;
  908. }
  909. if (buildOut) {
  910. percentAlpha = 1 - percentAlpha;
  911. }
  912. this.percentAlpha = percentAlpha;
  913. this.percentfinished = percentfinished;
  914. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  915. for (var i = 0, length = this.textures.length; i < length; i++) {
  916. var textureInfo = this.textures[i];
  917. var initialState = textureInfo.initialState;
  918. var animations = textureInfo.animations;
  919. if (textureInfo.hasHighlightedBulletAnimation) {
  920. if (!initialState.hidden) {
  921. var opacity;
  922. if (animations.length > 0 && animations[0].property === "opacity") {
  923. var opacityFrom = animations[0].from.scalar;
  924. var opacityTo = animations[0].to.scalar;
  925. var diff = opacityTo - opacityFrom;
  926. if (buildOut) {
  927. opacity = opacityFrom + diff * (1 - this.percentfinished);
  928. } else {
  929. opacity = opacityFrom + diff * this.percentfinished;
  930. }
  931. } else {
  932. opacity = textureInfo.initialState.opacity;
  933. }
  934. this.drawableObjects[i].Opacity = this.parentOpacity * opacity;
  935. this.drawableObjects[i].drawFrame();
  936. }
  937. } else if (textureInfo.animations.length > 0) {
  938. if (this.isCompleted) {
  939. if (!buildOut) {
  940. // if completed, just draw its texture object for better performance
  941. this.drawableObjects[i].Opacity = this.parentOpacity * textureInfo.initialState.opacity;
  942. this.drawableObjects[i].drawFrame();
  943. }
  944. continue;
  945. }
  946. var program = this.program["iris"];
  947. var attribs = program.attribs;
  948. var uniforms = program.uniforms;
  949. var irisSystem = this.irisSystems[i];
  950. var scale = irisSystem.scale;
  951. gl.useProgram(program.shaderProgram);
  952. var textureCoordinatesBuffer = irisSystem.textureCoordinatesBuffer;
  953. gl.bindBuffer(gl.ARRAY_BUFFER, textureCoordinatesBuffer);
  954. gl.vertexAttribPointer(attribs["TexCoord"], 2, gl.FLOAT, false, 0, 0);
  955. gl.enableVertexAttribArray(attribs["TexCoord"]);
  956. var positionBuffer = irisSystem.positionBuffer;
  957. gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
  958. gl.vertexAttribPointer(attribs["Position"], 3, gl.FLOAT, false, 0, 0);
  959. gl.enableVertexAttribArray(attribs["Position"]);
  960. var MVPMatrix = irisSystem.MVPMatrix;
  961. gl.uniformMatrix4fv(uniforms["MVPMatrix"], false, MVPMatrix);
  962. gl.activeTexture(gl.TEXTURE0);
  963. gl.uniform1i(uniforms["Texture"], 0);
  964. // set Opacity
  965. gl.uniform1f(uniforms["Opacity"], this.parentOpacity * textureInfo.initialState.opacity);
  966. //fg texture
  967. gl.bindTexture(gl.TEXTURE_2D, textureInfo.texture);
  968. gl.uniform1f(uniforms["PercentForAlpha"], this.percentAlpha);
  969. gl.uniform1f(uniforms["Scale"], scale);
  970. gl.uniform1f(uniforms["Mix"], this.mix);
  971. gl.drawArrays(gl.TRIANGLE_STRIP, 0 , 4);
  972. } else {
  973. if (!textureInfo.initialState.hidden) {
  974. this.drawableObjects[i].Opacity = this.parentOpacity * textureInfo.initialState.opacity;
  975. this.drawableObjects[i].drawFrame();
  976. }
  977. }
  978. }
  979. }
  980. });
  981. var KNWebGLTransitionTwist = Class.create(KNWebGLProgram, {
  982. initialize: function($super, renderer, params) {
  983. this.programData = {
  984. name: "com.apple.iWork.Keynote.BUKTwist",
  985. programNames:["twist"],
  986. effect: params.effect,
  987. textures: params.textures
  988. };
  989. $super(renderer, this.programData);
  990. var gl = this.gl;
  991. this.direction = this.effect.attributes.direction;
  992. this.percentfinished = 0.0;
  993. var mNumPoints = this.mNumPoints = 24;
  994. var dx = gl.viewportWidth / (mNumPoints - 1);
  995. var dy = gl.viewportHeight / (mNumPoints - 1);
  996. var fractionOfUnitLength = 1 / (mNumPoints - 1);
  997. var x, y;
  998. var TexCoords = this.TexCoords = [];
  999. var PositionCoords = this.PositionCoords = [];
  1000. var NormalCoords = this.NormalCoords = [];
  1001. for (y = 0; y < mNumPoints; y++) {
  1002. for (x = 0; x < mNumPoints; x++) {
  1003. var index = y * mNumPoints + x;
  1004. PositionCoords[index * 3] = x * dx;
  1005. PositionCoords[index * 3 + 1] = y * dy;
  1006. PositionCoords[index * 3 + 2] = 0;
  1007. TexCoords.push(x * fractionOfUnitLength);
  1008. TexCoords.push(y * fractionOfUnitLength);
  1009. NormalCoords.push(0);
  1010. NormalCoords.push(0);
  1011. NormalCoords.push(-1);
  1012. }
  1013. }
  1014. var index = 0;
  1015. var elementArray = this.elementArray = [];
  1016. for (y = 0; y < mNumPoints - 1; y++) {
  1017. for (x = 0; x < mNumPoints; x++) {
  1018. elementArray[index++] = (y) * (mNumPoints) + x;
  1019. elementArray[index++] = (y + 1) * (mNumPoints) + x;
  1020. }
  1021. }
  1022. // setup requirements
  1023. this.animationWillBeginWithContext();
  1024. },
  1025. animationWillBeginWithContext: function() {
  1026. var renderer = this.renderer;
  1027. var gl = this.gl;
  1028. var program = this.program["twist"];
  1029. var uniforms = program.uniforms;
  1030. var attribs = program.attribs;
  1031. gl.enable(gl.CULL_FACE);
  1032. this.buffers = {};
  1033. this.buffers["TexCoord"] = gl.createBuffer();
  1034. gl.bindBuffer(gl.ARRAY_BUFFER, this.buffers["TexCoord"]);
  1035. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.TexCoords), gl.STATIC_DRAW);
  1036. gl.vertexAttribPointer(attribs["TexCoord"], 2, gl.FLOAT, false, 0, 0);
  1037. gl.enableVertexAttribArray(attribs["TexCoord"]);
  1038. this.buffers["Position"] = gl.createBuffer();
  1039. gl.bindBuffer(gl.ARRAY_BUFFER, this.buffers["Position"]);
  1040. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.PositionCoords), gl.DYNAMIC_DRAW);
  1041. gl.vertexAttribPointer(attribs["Position"], 3, gl.FLOAT, false, 0, 0);
  1042. gl.enableVertexAttribArray(attribs["Position"]);
  1043. this.buffers["Normal"] = gl.createBuffer();
  1044. gl.bindBuffer(gl.ARRAY_BUFFER, this.buffers["Normal"]);
  1045. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.NormalCoords), gl.DYNAMIC_DRAW);
  1046. gl.vertexAttribPointer(attribs["Normal"], 3, gl.FLOAT, false, 0, 0);
  1047. gl.enableVertexAttribArray(attribs["Normal"]);
  1048. this.MVPMatrix = renderer.slideProjectionMatrix;
  1049. gl.uniformMatrix4fv(uniforms["MVPMatrix"], false, this.MVPMatrix);
  1050. this.AffineTransform = new Matrix3();
  1051. this.AffineTransform.affineScale(1.0, -1.0);
  1052. this.AffineTransform.affineTranslate(0.0, 1.0);
  1053. this.AffineIdentity = new Matrix3();
  1054. this.elementIndicesBuffer = gl.createBuffer();
  1055. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.elementIndicesBuffer);
  1056. gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(this.elementArray), gl.STATIC_DRAW);
  1057. gl.activeTexture(gl.TEXTURE0);
  1058. gl.uniform1i(uniforms["Texture"], 0);
  1059. this.drawFrame(0, 0, 4);
  1060. },
  1061. drawFrame: function(difference, elapsed, duration) {
  1062. var gl = this.gl;
  1063. var program = this.program["twist"];
  1064. var attribs = program.attribs;
  1065. var percentfinished = this.percentfinished;
  1066. percentfinished += difference / duration;
  1067. percentfinished > 1 ? percentfinished = 1 : 0;
  1068. this.specularcolor = TSUSineMap(percentfinished * 2) * 0.5;
  1069. var y, x;
  1070. var height = gl.viewportHeight / 2.0;
  1071. var mNumPoints = this.mNumPoints;
  1072. var TexCoords = this.TexCoords;
  1073. var PositionCoords = this.PositionCoords;
  1074. var NormalCoords = this.NormalCoords;
  1075. for (y = 0; y < mNumPoints; y++) {
  1076. for (x = 0; x < mNumPoints; x++) {
  1077. var index = y * mNumPoints + x;
  1078. var start = {};
  1079. start.x = TexCoords[index * 2];
  1080. start.y = TexCoords[index * 2 + 1];
  1081. var angle = -Math.PI * TwistFX(this.direction === KNDirection.kKNDirectionLeftToRight ? start.x : (1 - start.x), percentfinished);
  1082. var result = {};
  1083. result.y = (height - (height * (1 - start.y * 2) * Math.cos(angle)));
  1084. result.z = (height * (1 - start.y * 2) * Math.sin(angle));
  1085. PositionCoords[index * 3 + 1] = result.y;
  1086. PositionCoords[index * 3 + 2] = result.z;
  1087. }
  1088. }
  1089. for (y = 0; y < mNumPoints; y++) {
  1090. for (x = 0; x < mNumPoints; x++) {
  1091. var finalNormal = new vector3();
  1092. var index = y * mNumPoints + x;
  1093. for (var q = 0; q < 4; q++) {
  1094. var q1x = 0, q1y = 0, q2x = 0, q2y = 0;
  1095. switch (q) {
  1096. case 0:
  1097. q1x = 1;
  1098. q2y = 1;
  1099. break;
  1100. case 1:
  1101. q1y = 1;
  1102. q2x = -1;
  1103. break;
  1104. case 2:
  1105. q1x = -1;
  1106. q2y = -1;
  1107. break;
  1108. case 3:
  1109. q1y = -1;
  1110. q2x = 1;
  1111. default:
  1112. break;
  1113. }
  1114. if ((x + q1x) < 0 || (x + q2x) < 0 || (y + q1y) < 0 || (y + q2y) < 0
  1115. || x + q1x >= mNumPoints || x + q2x >= mNumPoints || y + q1y >= mNumPoints || y + q2y >= mNumPoints) {
  1116. continue;
  1117. }
  1118. var thisV = new vector3([PositionCoords[index * 3], PositionCoords[index * 3 + 1], PositionCoords[index * 3 + 2] ]);
  1119. var nextV = new vector3([PositionCoords[((y + q1y) * mNumPoints + (x + q1x)) * 3], PositionCoords[((y + q1y) * mNumPoints + (x + q1x)) * 3 + 1], PositionCoords[((y + q1y) * mNumPoints + (x + q1x)) * 3 + 2] ]);
  1120. var prevV = new vector3([PositionCoords[(((y + q2y) * mNumPoints) + (x + q2x)) * 3], PositionCoords[(((y + q2y) * mNumPoints) + (x + q2x)) * 3 + 1], PositionCoords[(((y + q2y) * mNumPoints) + (x + q2x)) * 3 + 2] ]);
  1121. nextV.subtract(thisV);
  1122. prevV.subtract(thisV);
  1123. nextV.cross(prevV); // cross gives you the normal
  1124. finalNormal.add(nextV);
  1125. }
  1126. finalNormal.normalize();
  1127. finalNormal.scale(-1.0);
  1128. finalNormal = finalNormal.getArray();
  1129. NormalCoords[index * 3] = finalNormal[0];
  1130. NormalCoords[index * 3 + 1] = finalNormal[1];
  1131. NormalCoords[index * 3 + 2] = finalNormal[2];
  1132. }
  1133. }
  1134. gl.bindBuffer(gl.ARRAY_BUFFER, this.buffers["Position"]);
  1135. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(PositionCoords), gl.DYNAMIC_DRAW);
  1136. gl.vertexAttribPointer(attribs["Position"], 3, gl.FLOAT, false, 0, 0);
  1137. gl.bindBuffer(gl.ARRAY_BUFFER, this.buffers["Normal"]);
  1138. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(NormalCoords), gl.DYNAMIC_DRAW);
  1139. gl.vertexAttribPointer(attribs["Normal"], 3, gl.FLOAT, false, 0, 0);
  1140. this.percentfinished = percentfinished;
  1141. this.draw();
  1142. },
  1143. draw: function() {
  1144. var renderer = this.renderer;
  1145. var gl = this.gl;
  1146. var program = this.program["twist"];
  1147. var uniforms = program.uniforms;
  1148. var textures = this.textures;
  1149. var texture = textures[0].texture;
  1150. var outgoingTexture = textures[1].texture;
  1151. var mNumPoints = this.mNumPoints;
  1152. var specularcolor = this.specularcolor;
  1153. var AffineTransform = this.AffineTransform.getColumnMajorFloat32Array();
  1154. var AffineIdentity = this.AffineIdentity.getColumnMajorFloat32Array();
  1155. var elementIndicesBuffer = this.elementIndicesBuffer;
  1156. if (!specularcolor) {
  1157. specularcolor = 0;
  1158. }
  1159. gl.uniform1f(uniforms["SpecularColor"], specularcolor);
  1160. if (this.percentfinished < 0.5) {
  1161. gl.cullFace(gl.BACK);
  1162. gl.bindTexture(gl.TEXTURE_2D, texture);
  1163. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elementIndicesBuffer);
  1164. gl.uniformMatrix3fv(uniforms["TextureMatrix"], false, AffineTransform);
  1165. gl.uniform1f(uniforms["FlipNormals"], 1.0);
  1166. // draw
  1167. for (y = 0; y < mNumPoints - 1; y++) {
  1168. gl.drawElements(gl.TRIANGLE_STRIP, mNumPoints * 2, gl.UNSIGNED_SHORT, y * mNumPoints * 2 * (2));
  1169. }
  1170. // ANIMATE OVERLAY
  1171. gl.cullFace(gl.FRONT);
  1172. gl.bindTexture(gl.TEXTURE_2D, outgoingTexture);
  1173. gl.uniformMatrix3fv(uniforms["TextureMatrix"], false, AffineIdentity);
  1174. gl.uniform1f(uniforms["FlipNormals"], -1.0);
  1175. for (y = 0; y < mNumPoints - 1; y++) {
  1176. gl.drawElements(gl.TRIANGLE_STRIP, mNumPoints * 2, gl.UNSIGNED_SHORT, y * mNumPoints * 2 * (2));
  1177. }
  1178. } else {
  1179. gl.cullFace(gl.FRONT);
  1180. gl.bindTexture(gl.TEXTURE_2D, outgoingTexture);
  1181. gl.uniformMatrix3fv(uniforms["TextureMatrix"], false, AffineIdentity);
  1182. gl.uniform1f(uniforms["FlipNormals"], -1.0);
  1183. for (y = 0; y < mNumPoints - 1; y++) {
  1184. gl.drawElements(gl.TRIANGLE_STRIP, mNumPoints * 2, gl.UNSIGNED_SHORT, y * mNumPoints * 2 * (2));
  1185. }
  1186. gl.cullFace(gl.BACK);
  1187. gl.bindTexture(gl.TEXTURE_2D, texture);
  1188. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elementIndicesBuffer);
  1189. gl.uniformMatrix3fv(uniforms["TextureMatrix"], false, AffineTransform);
  1190. gl.uniform1f(uniforms["SpecularColor"], specularcolor);
  1191. gl.uniform1f(uniforms["FlipNormals"], 1.0);
  1192. // draw
  1193. for (y = 0; y < mNumPoints - 1; y++) {
  1194. gl.drawElements(gl.TRIANGLE_STRIP, mNumPoints * 2, gl.UNSIGNED_SHORT, y * mNumPoints * 2 * (2));
  1195. }
  1196. }
  1197. }
  1198. });
  1199. var KNWebGLTransitionColorPlanes = Class.create(KNWebGLProgram, {
  1200. initialize: function($super, renderer, params) {
  1201. this.programData = {
  1202. name: "com.apple.iWork.Keynote.KLNColorPlanes",
  1203. programNames:["colorPlanes"],
  1204. effect: params.effect,
  1205. textures: params.textures
  1206. };
  1207. $super(renderer, this.programData);
  1208. var direction = this.effect.attributes.direction;
  1209. if (direction !== KNDirection.kKNDirectionLeftToRight && direction !== KNDirection.kKNDirectionRightToLeft && direction !== KNDirection.kKNDirectionTopToBottom && direction !== KNDirection.kKNDirectionBottomToTop) {
  1210. // default direction to left to right if not specified
  1211. direction = KNDirection.kKNDirectionLeftToRight
  1212. }
  1213. this.direction = direction;
  1214. this.mNumColors = 3;
  1215. this.percentfinished = 0.0;
  1216. // setup requirements
  1217. this.animationWillBeginWithContext();
  1218. },
  1219. animationWillBeginWithContext: function() {
  1220. var renderer = this.renderer;
  1221. var gl = this.gl;
  1222. var program = this.program["colorPlanes"];
  1223. var uniforms = program.uniforms;
  1224. var attribs = program.attribs;
  1225. var textureInfo = this.textures[0];
  1226. gl.disable(gl.CULL_FACE);
  1227. gl.blendFunc(gl.ONE, gl.ONE);
  1228. var buffers = this.buffers = {};
  1229. buffers["TexCoord"] = gl.createBuffer();
  1230. gl.bindBuffer(gl.ARRAY_BUFFER, buffers["TexCoord"]);
  1231. var TexCoords = this.TexCoords = [
  1232. 0.0, 0.0,
  1233. 0.0, 1.0,
  1234. 1.0, 0.0,
  1235. 1.0, 1.0,
  1236. ];
  1237. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(TexCoords), gl.STATIC_DRAW);
  1238. gl.vertexAttribPointer(attribs["TexCoord"], 2, gl.FLOAT, false, 0,0);
  1239. gl.enableVertexAttribArray(attribs["TexCoord"]);
  1240. buffers["Position"] = gl.createBuffer();
  1241. gl.bindBuffer(gl.ARRAY_BUFFER, buffers["Position"]);
  1242. var PositionCoords = this.PositionCoords = [
  1243. 0.0, 0.0, 0.0,
  1244. 0.0, textureInfo.height, 0.0,
  1245. textureInfo.width, 0.0, 0.0,
  1246. textureInfo.width, textureInfo.height, 0.0
  1247. ];
  1248. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(PositionCoords), gl.STATIC_DRAW);
  1249. gl.vertexAttribPointer(attribs["Position"], 3, gl.FLOAT, false, 0,0);
  1250. gl.enableVertexAttribArray(attribs["Position"]);
  1251. this.MVPMatrix = renderer.slideProjectionMatrix;
  1252. gl.uniformMatrix4fv(uniforms["MVPMatrix"], false, this.MVPMatrix);
  1253. gl.activeTexture(gl.TEXTURE0);
  1254. gl.uniform1i(uniforms["Texture"], 0);
  1255. this.drawFrame(0, 0, 4);
  1256. },
  1257. drawFrame: function(difference, elapsed, duration) {
  1258. var renderer = this.renderer;
  1259. var gl = this.gl;
  1260. var program = this.program["colorPlanes"];
  1261. var uniforms = program.uniforms;
  1262. var attribs = program.attribs;
  1263. var textures = this.textures;
  1264. var textureInfo = textures[0];
  1265. var outgoingTextureInfo = textures[1];
  1266. this.percentfinished += difference / duration;
  1267. this.percentfinished > 1 ? this.percentfinished = 1 : 0;
  1268. var percent = this.percentfinished;
  1269. var direction = this.direction;
  1270. var planeSeparation = 0.25;
  1271. var cameraPullBack = 1.0;
  1272. var clockwise = (direction == KNDirection.kKNDirectionRightToLeft || direction == KNDirection.kKNDirectionBottomToTop);
  1273. var yAxis = (direction == KNDirection.kKNDirectionLeftToRight || direction == KNDirection.kKNDirectionRightToLeft);
  1274. var percentInvSq = 1-(1-percent)*(1-percent);
  1275. var cameraAmount = yAxis ? textureInfo.width : textureInfo.height;
  1276. var uCurve = TSUSineMap(percent * 2.0);
  1277. var planeOffset = uCurve * cameraAmount * planeSeparation;
  1278. var zOffset = Math.sin(-percentInvSq*2.*Math.PI);
  1279. zOffset *= percentInvSq * cameraAmount * cameraPullBack;
  1280. if (percent < 0.5) {
  1281. gl.bindTexture(gl.TEXTURE_2D, outgoingTextureInfo.texture);
  1282. gl.uniform2fv(uniforms["FlipTexCoords"], new Float32Array([0,0]));
  1283. } else {
  1284. gl.bindTexture(gl.TEXTURE_2D, textureInfo.texture);
  1285. if (direction == KNDirection.kKNDirectionTopToBottom || direction == KNDirection.kKNDirectionBottomToTop) {
  1286. gl.uniform2fv(uniforms["FlipTexCoords"], new Float32Array([0,1]));
  1287. } else {
  1288. gl.uniform2fv(uniforms["FlipTexCoords"], new Float32Array([1,0]));
  1289. }
  1290. }
  1291. for (var iHue = 0, mNumColors = this.mNumColors; iHue < mNumColors; iHue++) {
  1292. var thisHue = iHue/mNumColors;
  1293. // setup color mask
  1294. var color = WebGraphics.colorWithHSBA(thisHue, 1, 1, 1/mNumColors);
  1295. gl.uniform4fv(uniforms["ColorMask"], new Float32Array([color.red, color.green, color.blue, color.alpha]));
  1296. var angle = (Math.PI/180.0) * (180.0 * (TSUSineMap(percent)));
  1297. var mvpMatrix = WebGraphics.translateMatrix4(this.MVPMatrix, textureInfo.width/2, textureInfo.height/2, zOffset);
  1298. mvpMatrix = WebGraphics.rotateMatrix4AboutXYZ(mvpMatrix, angle, (clockwise ? -1 : 1) * (yAxis ? 0 : 1), (clockwise ? -1 : 1) * (yAxis ? 1 : 0), 0);
  1299. mvpMatrix = WebGraphics.translateMatrix4(mvpMatrix, -textureInfo.width/2, -textureInfo.height/2, planeOffset*(iHue-1));
  1300. gl.uniformMatrix4fv(uniforms["MVPMatrix"], false, mvpMatrix);
  1301. gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
  1302. }
  1303. }
  1304. });
  1305. var KNWebGLTransitionFlop = Class.create(KNWebGLProgram, {
  1306. initialize: function($super, renderer, params) {
  1307. this.programData = {
  1308. name: "com.apple.iWork.Keynote.BUKFlop",
  1309. programNames:["flop", "defaultTexture"],
  1310. effect: params.effect,
  1311. textures: params.textures
  1312. };
  1313. $super(renderer, this.programData);
  1314. var direction = this.effect.attributes.direction;
  1315. if (direction !== KNDirection.kKNDirectionLeftToRight && direction !== KNDirection.kKNDirectionRightToLeft && direction !== KNDirection.kKNDirectionTopToBottom && direction !== KNDirection.kKNDirectionBottomToTop) {
  1316. // default direction to left to right if not specified
  1317. direction = KNDirection.kKNDirectionLeftToRight
  1318. }
  1319. this.direction = direction;
  1320. this.percentfinished = 0.0;
  1321. var elementArray = this.elementArray = [];
  1322. var gl = this.gl;
  1323. var texWidth = gl.viewportWidth;
  1324. var texHeight = gl.viewportHeight;
  1325. var width = texWidth
  1326. var height = texHeight;
  1327. if (direction === KNDirection.kKNDirectionTopToBottom || direction === KNDirection.kKNDirectionBottomToTop) {
  1328. height *= 0.5;
  1329. } else {
  1330. width *= 0.5;
  1331. }
  1332. var mNumPoints = this.mNumPoints = 8;
  1333. var index = 0;
  1334. for (y = 0; y < mNumPoints - 1; y++) {
  1335. for (x = 0; x < mNumPoints; x++) {
  1336. elementArray[index++] = (y + 0) * (mNumPoints) + x;
  1337. elementArray[index++] = (y + 1) * (mNumPoints) + x;
  1338. }
  1339. }
  1340. var dx = width / (mNumPoints - 1);
  1341. var dy = height / (mNumPoints - 1);
  1342. var yOffset = (direction == KNDirection.kKNDirectionTopToBottom) ? height : yOffset = 0;
  1343. var xOffset = (direction == KNDirection.kKNDirectionRightToLeft) ? width : xOffset = 0;
  1344. var attributeBufferData = this.attributeBufferData = {
  1345. Position: [],
  1346. TexCoords: [],
  1347. Normal: [],
  1348. ShadowPosition: [],
  1349. ShadowTexCoord: [],
  1350. PreviousPosition: [],
  1351. PreviousTexCoords: [],
  1352. PreviousNormal: []
  1353. };
  1354. for (var y = 0; y < mNumPoints; y++) {
  1355. for (var x = 0; x < mNumPoints; x++) {
  1356. index = y * mNumPoints + x;
  1357. KNWebGLUtil.setPoint3DAtIndexForAttribute(WebGraphics.makePoint3D(x * dx + xOffset, y * dy, 0), index, attributeBufferData["Position"]);
  1358. KNWebGLUtil.setPoint2DAtIndexForAttribute(WebGraphics.makePoint((x * dx + xOffset) / texWidth, (y * dy + yOffset) / texHeight), index, attributeBufferData["TexCoords"]);
  1359. KNWebGLUtil.setPoint3DAtIndexForAttribute(WebGraphics.makePoint3D(0, 0, 1), index, attributeBufferData["Normal"]);
  1360. }
  1361. }
  1362. // setup requirements
  1363. this.animationWillBeginWithContext();
  1364. },
  1365. animationWillBeginWithContext: function() {
  1366. var renderer = this.renderer;
  1367. var gl = this.gl;
  1368. var program = this.program["flop"];
  1369. var attribs = program.attribs;
  1370. var uniforms = program.uniforms;
  1371. var basicProgram = this.program["defaultTexture"];
  1372. var MVPMatrix = this.MVPMatrix = renderer.slideProjectionMatrix;
  1373. var width = gl.viewportWidth;
  1374. var height = gl.viewportHeight;
  1375. var direction = this.direction;
  1376. if (direction === KNDirection.kKNDirectionTopToBottom || direction === KNDirection.kKNDirectionBottomToTop) {
  1377. height *= 0.5;
  1378. } else {
  1379. width *= 0.5;
  1380. }
  1381. var textureCoordinates = [
  1382. 0.0, 0.0,
  1383. 0.0, 0.5,
  1384. 1.0, 0.0,
  1385. 1.0, 0.5,
  1386. ];
  1387. var boxPosition = [
  1388. 0.0, 0.0, 0.0,
  1389. 0.0, height, 0.0,
  1390. width,0.0, 0.0,
  1391. width, height, 0.0,
  1392. ];
  1393. var textureCoordinates2 = [
  1394. 0.0, 0.5,
  1395. 0.0, 1.0,
  1396. 1.0, 0.5,
  1397. 1.0, 1.0,
  1398. ];
  1399. var boxPosition2 = [
  1400. 0.0, height, 0.0,
  1401. 0.0, height*2, 0.0,
  1402. width, height, 0.0,
  1403. width, height*2, 0.0,
  1404. ];
  1405. // use this program and enable vertex attrib array
  1406. KNWebGLUtil.enableAttribs(gl, program);
  1407. var attributeBufferData = this.attributeBufferData;
  1408. var buffers = this.buffers = {};
  1409. var Coordinates = this.Coordinates = {};
  1410. buffers["TexCoord"] = gl.createBuffer();
  1411. gl.bindBuffer(gl.ARRAY_BUFFER, buffers["TexCoord"]);
  1412. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(attributeBufferData["TexCoords"]), gl.DYNAMIC_DRAW);
  1413. gl.vertexAttribPointer(attribs["TexCoord"], 2, gl.FLOAT, false, 0,0);
  1414. buffers["Position"] = gl.createBuffer();
  1415. gl.bindBuffer(gl.ARRAY_BUFFER, buffers["Position"]);
  1416. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(attributeBufferData["Position"]), gl.DYNAMIC_DRAW);
  1417. gl.vertexAttribPointer(attribs["Position"], 3, gl.FLOAT, false, 0,0);
  1418. buffers["Normal"] = gl.createBuffer();
  1419. gl.bindBuffer(gl.ARRAY_BUFFER, buffers["Normal"]);
  1420. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(attributeBufferData["Normal"]), gl.DYNAMIC_DRAW);
  1421. gl.vertexAttribPointer(attribs["Normal"], 3, gl.FLOAT, false, 0,0);
  1422. gl.uniformMatrix4fv(uniforms["MVPMatrix"], false, MVPMatrix);
  1423. var AffineTransform = this.AffineTransform = new Matrix3();
  1424. if (direction === KNDirection.kKNDirectionTopToBottom) {
  1425. AffineTransform.affineScale(1.0, -1.0);
  1426. AffineTransform.affineTranslate(0.0,1.0);
  1427. } else if (direction == KNDirection.kKNDirectionBottomToTop) {
  1428. AffineTransform.affineScale(1.0, -1.0);
  1429. AffineTransform.affineTranslate(0.0,1.0);
  1430. textureCoordinates = [
  1431. 0.0, 0.5,
  1432. 0.0, 1.0,
  1433. 1.0, 0.5,
  1434. 1.0, 1.0,
  1435. ];
  1436. textureCoordinates2 = [
  1437. 0.0, 0.0,
  1438. 0.0, 0.5,
  1439. 1.0, 0.0,
  1440. 1.0, 0.5,
  1441. ];
  1442. boxPosition = [
  1443. 0.0, height, 0.0,
  1444. 0.0, height*2, 0.0,
  1445. width,height, 0.0,
  1446. width, height*2, 0.0,
  1447. ];
  1448. boxPosition2 = [
  1449. 0, 0, 0.0,
  1450. 0, height, 0.0,
  1451. width, 0, 0.0,
  1452. width, height, 0.0,
  1453. ];
  1454. } else if (direction == KNDirection.kKNDirectionRightToLeft) {
  1455. AffineTransform.affineScale(-1.0, 1.0);
  1456. AffineTransform.affineTranslate(1.0, 0.0);
  1457. textureCoordinates = [
  1458. 0.0, 0.0,
  1459. 0.0, 1.0,
  1460. 0.5, 0.0,
  1461. 0.5, 1.0,
  1462. ];
  1463. textureCoordinates2 = [
  1464. 0.5, 0.0,
  1465. 0.5, 1.0,
  1466. 1.0, 0.0,
  1467. 1.0, 1.0,
  1468. ];
  1469. boxPosition2 = [
  1470. width, 0, 0.0,
  1471. width, height, 0.0,
  1472. width*2, 0, 0.0,
  1473. width*2, height, 0.0,
  1474. ];
  1475. } else if (direction === KNDirection.kKNDirectionLeftToRight) {
  1476. AffineTransform.affineScale(-1.0, 1.0);
  1477. AffineTransform.affineTranslate(1.0, 0.0);
  1478. boxPosition = [
  1479. width, 0, 0.0,
  1480. width, height, 0.0,
  1481. width*2,0, 0.0,
  1482. width*2, height, 0.0,
  1483. ];
  1484. textureCoordinates = [
  1485. 0.5, 0.0,
  1486. 0.5, 1.0,
  1487. 1.0, 0.0,
  1488. 1.0, 1.0,
  1489. ];
  1490. textureCoordinates2 = [
  1491. 0.0, 0.0,
  1492. 0.0, 1.0,
  1493. 0.5, 0.0,
  1494. 0.5, 1.0,
  1495. ];
  1496. boxPosition2 = [
  1497. 0, 0, 0.0,
  1498. 0, height, 0.0,
  1499. width, 0, 0.0,
  1500. width, height, 0.0,
  1501. ];
  1502. }
  1503. this.AffineIdentity = new Matrix3();
  1504. this.elementIndicesBuffer = gl.createBuffer();
  1505. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.elementIndicesBuffer);
  1506. gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(this.elementArray), gl.STATIC_DRAW);
  1507. //setup second program
  1508. Coordinates["DefaultTexture"] = textureCoordinates;
  1509. Coordinates["DefaultTexture2"] = textureCoordinates2;
  1510. Coordinates["DefaultPosition"] = boxPosition;
  1511. Coordinates["DefaultPosition2"] = boxPosition2;
  1512. // use this program and enable vertex attrib array
  1513. KNWebGLUtil.enableAttribs(gl, basicProgram);
  1514. //setup VBO and FTB
  1515. buffers["TextureCoordinates"] = gl.createBuffer();
  1516. buffers["PositionCoordinates"] = gl.createBuffer();
  1517. gl.bindBuffer(gl.ARRAY_BUFFER, buffers["TextureCoordinates"]);
  1518. gl.bindBuffer(gl.ARRAY_BUFFER, buffers["PositionCoordinates"]);
  1519. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoordinates), gl.DYNAMIC_DRAW);
  1520. gl.vertexAttribPointer(basicProgram.attribs["TexCoord"], 2, gl.FLOAT, false, 0, 0);
  1521. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(boxPosition), gl.DYNAMIC_DRAW);
  1522. gl.vertexAttribPointer(basicProgram.attribs["Position"], 3, gl.FLOAT, false, 0, 0);
  1523. gl.uniform1i(basicProgram.uniforms["Texture"], 0);
  1524. gl.uniformMatrix4fv(basicProgram.uniforms["MVPMatrix"], false, MVPMatrix);
  1525. // switch back to main program with animation
  1526. gl.useProgram(program.shaderProgram);
  1527. gl.activeTexture(gl.TEXTURE0);
  1528. gl.uniform1i(program.uniforms["Texture"], 0);
  1529. this.drawFrame(0, 0, 4);
  1530. },
  1531. drawFrame: function(difference, elapsed, duration) {
  1532. this.percentfinished += difference / duration;
  1533. this.percentfinished > 1 ? this.percentfinished = 1 : 0;
  1534. this.updateFlopWithPercent();
  1535. this.draw();
  1536. },
  1537. updateFlopWithPercent: function() {
  1538. var gl = this.gl;
  1539. var direction = this.direction;
  1540. var texWidth = gl.viewportWidth;
  1541. var texHeight = gl.viewportHeight;
  1542. var thetaA = this.percentfinished * Math.PI;
  1543. var thetaB = this.percentfinished * this.percentfinished * this.percentfinished * Math.PI;
  1544. var height = texHeight / 2.0;
  1545. var width = texWidth / 2.0;
  1546. var location = 0.0;
  1547. var mNumPoints = this.mNumPoints;
  1548. var attributeBufferData = this.attributeBufferData;
  1549. for (var y = 0; y < mNumPoints; y++) {
  1550. for(var x = 0; x < mNumPoints; x++) {
  1551. var index = y * mNumPoints + x;
  1552. var start = KNWebGLUtil.getPoint2DForArrayAtIndex(attributeBufferData["TexCoords"], index);
  1553. start.x *= texWidth;
  1554. start.y *= texHeight;
  1555. if (direction === KNDirection.kKNDirectionBottomToTop) {
  1556. location = start.y / height;
  1557. } else if (direction === KNDirection.kKNDirectionTopToBottom) {
  1558. location = (height*2 - start.y) / height;
  1559. } else if (direction === KNDirection.kKNDirectionLeftToRight) {
  1560. location = start.x / width;
  1561. } else {
  1562. location = (width*2 - start.x) / width;
  1563. }
  1564. var angle = location*thetaA + (1-location) * thetaB;
  1565. if (direction === KNDirection.kKNDirectionLeftToRight || direction === KNDirection.kKNDirectionTopToBottom) {
  1566. angle *= -1;
  1567. }
  1568. var sinAngle = Math.sin(angle);
  1569. var cosAngle = Math.cos(angle);
  1570. var startPosition = KNWebGLUtil.getPoint3DForArrayAtIndex(attributeBufferData["Position"], index);
  1571. var startNormal = KNWebGLUtil.getPoint3DForArrayAtIndex(attributeBufferData["Normal"], index);
  1572. if (direction === KNDirection.kKNDirectionTopToBottom || direction === KNDirection.kKNDirectionBottomToTop) {
  1573. var thisPosition = WebGraphics.makePoint3D(startPosition.x, height - (height - start.y) * cosAngle, (height - start.y) * sinAngle);
  1574. KNWebGLUtil.setPoint3DAtIndexForAttribute(thisPosition, index, attributeBufferData["Position"]);
  1575. var thisNormal = WebGraphics.makePoint3D(startNormal.x, -sinAngle, cosAngle);
  1576. KNWebGLUtil.setPoint3DAtIndexForAttribute(thisNormal, index, attributeBufferData["Normal"]);
  1577. } else {
  1578. var thisPosition = WebGraphics.makePoint3D(width - (width - start.x) * cosAngle, startPosition.y, -(width - start.x) * sinAngle);
  1579. KNWebGLUtil.setPoint3DAtIndexForAttribute(thisPosition, index, attributeBufferData["Position"]);
  1580. var thisNormal = WebGraphics.makePoint3D(-sinAngle, startNormal.y, cosAngle);
  1581. KNWebGLUtil.setPoint3DAtIndexForAttribute(thisNormal, index, attributeBufferData["Normal"]);
  1582. }
  1583. }
  1584. }
  1585. },
  1586. draw: function() {
  1587. var renderer = this.renderer;
  1588. var gl = this.gl;
  1589. var program = this.program["flop"];
  1590. var basicProgram = this.program["defaultTexture"];
  1591. var textures = this.textures;
  1592. var outgoingTexture = textures[1].texture;
  1593. var incomingTexture = textures[0].texture;
  1594. gl.useProgram(basicProgram.shaderProgram);
  1595. gl.disable(gl.CULL_FACE);
  1596. gl.bindTexture(gl.TEXTURE_2D, outgoingTexture);
  1597. var mNumPoints = this.mNumPoints;
  1598. var buffers = this.buffers;
  1599. var Coordinates = this.Coordinates;
  1600. var attributeBufferData = this.attributeBufferData;
  1601. KNWebGLUtil.bindDynamicBufferWithData(gl, basicProgram.attribs["Position"], buffers["PositionCoordinates"], Coordinates["DefaultPosition"], 3);
  1602. KNWebGLUtil.bindDynamicBufferWithData(gl, basicProgram.attribs["TexCoord"], buffers["TextureCoordinates"], Coordinates["DefaultTexture"], 2);
  1603. gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
  1604. gl.useProgram(basicProgram.shaderProgram);
  1605. gl.disable(gl.CULL_FACE);
  1606. gl.bindTexture(gl.TEXTURE_2D, incomingTexture);
  1607. KNWebGLUtil.bindDynamicBufferWithData(gl, basicProgram.attribs["Position"], buffers["PositionCoordinates"], Coordinates["DefaultPosition2"], 3);
  1608. KNWebGLUtil.bindDynamicBufferWithData(gl, basicProgram.attribs["TexCoord"], buffers["TextureCoordinates"], Coordinates["DefaultTexture2"], 2);
  1609. gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
  1610. gl.enable(gl.CULL_FACE);
  1611. //ANIMATE OVERLAY
  1612. gl.useProgram(program.shaderProgram);
  1613. gl.bindBuffer(gl.ARRAY_BUFFER, buffers["Position"]);
  1614. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(attributeBufferData["Position"]), gl.DYNAMIC_DRAW);
  1615. gl.vertexAttribPointer(program.attribs["Position"], 3, gl.FLOAT, false, 0,0);
  1616. gl.bindBuffer(gl.ARRAY_BUFFER, buffers["Normal"]);
  1617. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(attributeBufferData["Normal"]), gl.DYNAMIC_DRAW);
  1618. gl.vertexAttribPointer(program.attribs["Normal"], 3, gl.FLOAT, false, 0,0);
  1619. gl.bindBuffer(gl.ARRAY_BUFFER, buffers["TexCoord"]);
  1620. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(attributeBufferData["TexCoords"]), gl.DYNAMIC_DRAW);
  1621. gl.vertexAttribPointer(program.attribs["TexCoord"], 2, gl.FLOAT, false, 0,0);
  1622. gl.cullFace(gl.BACK);
  1623. gl.bindTexture(gl.TEXTURE_2D, incomingTexture);
  1624. gl.uniformMatrix3fv(program.uniforms["TextureMatrix"], false, this.AffineTransform.getColumnMajorFloat32Array());
  1625. gl.uniform1f(program.uniforms["FlipNormals"], -1.0);
  1626. for (var y = 0; y< mNumPoints-1; y++) {
  1627. gl.drawElements(gl.TRIANGLE_STRIP, mNumPoints*2, gl.UNSIGNED_SHORT, y*mNumPoints*2*(2));
  1628. }
  1629. gl.bindTexture(gl.TEXTURE_2D, outgoingTexture);
  1630. gl.cullFace(gl.FRONT);
  1631. gl.uniformMatrix3fv(program.uniforms["TextureMatrix"], false, this.AffineIdentity.getColumnMajorFloat32Array());
  1632. gl.uniform1f(program.uniforms["FlipNormals"], 1.0);
  1633. for (var y = 0; y < mNumPoints-1; y++) {
  1634. gl.drawElements(gl.TRIANGLE_STRIP, mNumPoints*2, gl.UNSIGNED_SHORT, y*mNumPoints*2*(2));
  1635. }
  1636. }
  1637. });
  1638. var KNWebGLBuildAnvil = Class.create(KNWebGLProgram, {
  1639. initialize: function($super, renderer, params) {
  1640. var effect = params.effect;
  1641. this.programData = {
  1642. name: "com.apple.iWork.Keynote.BUKAnvil",
  1643. programNames: ["anvilsmoke", "anvilspeck"],
  1644. effect: effect,
  1645. textures: params.textures
  1646. };
  1647. $super(renderer, this.programData);
  1648. var gl = this.gl;
  1649. // bind required textures from base64 image source
  1650. this.smokeTexture = KNWebGLUtil.bindTextureWithImage(gl, smokeImage);
  1651. this.speckTexture = KNWebGLUtil.bindTextureWithImage(gl, speckImage);
  1652. // initialize percent finish
  1653. this.percentfinished = 0;
  1654. // create drawable object for drawing static texture
  1655. this.drawableObjects = [];
  1656. for (var i = 0, length = this.textures.length; i < length; i++) {
  1657. var texture = params.textures[i];
  1658. var drawableParams = {
  1659. effect: effect,
  1660. textures: [texture]
  1661. };
  1662. var drawableObject = new KNWebGLDrawable(renderer, drawableParams);
  1663. this.drawableObjects.push(drawableObject);
  1664. }
  1665. this.objectY = 1;
  1666. // set parent opacity from CA baseLayer
  1667. this.parentOpacity = effect.baseLayer.initialState.opacity;
  1668. // setup requirements
  1669. this.animationWillBeginWithContext();
  1670. },
  1671. animationWillBeginWithContext: function() {
  1672. var renderer = this.renderer;
  1673. var gl = this.gl;
  1674. this.smokeSystems = [];
  1675. this.speckSystems = [];
  1676. for (var i = 0, length = this.textures.length; i < length; i++) {
  1677. var textureInfo = this.textures[i];
  1678. var width = textureInfo.width;
  1679. var height = textureInfo.height;
  1680. var viewportWidth = gl.viewportWidth;
  1681. var viewportHeight = gl.viewportHeight;
  1682. var numParticles = 300;
  1683. var smokeSystem = new KNWebGLBuildAnvilSmokeSystem(
  1684. renderer,
  1685. this.program["anvilsmoke"],
  1686. {"width": width, "height": height},
  1687. {"width": viewportWidth, "height": viewportHeight},
  1688. this.duration,
  1689. {"width": numParticles, "height": 1},
  1690. {"width": kParticleSize, "height": kParticleSize},
  1691. this.smokeTexture);
  1692. numParticles = 40;
  1693. var speckSystem = new KNWebGLBuildAnvilSpeckSystem(
  1694. renderer,
  1695. this.program["anvilspeck"],
  1696. {"width": width, "height": height},
  1697. {"width": viewportWidth, "height": viewportHeight},
  1698. this.duration,
  1699. {"width": numParticles, "height": 1},
  1700. {"width": kParticleSize, "height": kParticleSize},
  1701. this.speckTexture);
  1702. this.smokeSystems.push(smokeSystem);
  1703. this.speckSystems.push(speckSystem);
  1704. }
  1705. },
  1706. drawFrame: function(difference, elapsed, duration) {
  1707. var renderer = this.renderer;
  1708. var gl = this.gl;
  1709. this.percentfinished += difference / duration;
  1710. if (this.percentfinished >= 1) {
  1711. this.percentfinished = 1;
  1712. this.isCompleted = true;
  1713. }
  1714. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  1715. for (var i = 0, length = this.textures.length; i < length; i++) {
  1716. var textureInfo = this.textures[i];
  1717. var initialState = textureInfo.initialState;
  1718. var animations = textureInfo.animations;
  1719. if (textureInfo.hasHighlightedBulletAnimation) {
  1720. if (!initialState.hidden) {
  1721. var opacity;
  1722. if (animations.length > 0 && animations[0].property === "opacity") {
  1723. var opacityFrom = animations[0].from.scalar;
  1724. var opacityTo = animations[0].to.scalar;
  1725. var diff = opacityTo - opacityFrom;
  1726. opacity = opacityFrom + diff * this.percentfinished;
  1727. } else {
  1728. opacity = textureInfo.initialState.opacity;
  1729. }
  1730. this.drawableObjects[i].Opacity = this.parentOpacity * opacity;
  1731. this.drawableObjects[i].drawFrame();
  1732. }
  1733. } else if (textureInfo.animations.length > 0) {
  1734. if (this.isCompleted) {
  1735. // if completed, just draw its texture object for better performance
  1736. this.drawableObjects[i].Opacity = this.parentOpacity * textureInfo.initialState.opacity;
  1737. this.drawableObjects[i].drawFrame();
  1738. continue;
  1739. }
  1740. var width = textureInfo.width;
  1741. var height = textureInfo.height;
  1742. var offsetX = textureInfo.offset.pointX;
  1743. var offsetY = textureInfo.offset.pointY;
  1744. var viewportWidth = gl.viewportWidth;
  1745. var viewportHeight = gl.viewportHeight;
  1746. duration /= 1000;
  1747. var kObjectSmashDuration = Math.min(0.20, duration * 0.4);
  1748. var kCameraShakeDuration = Math.min(0.25, duration * 0.5);
  1749. var cameraShakePoints = this.cameraShakePointsWithRandomGenerator();
  1750. var cameraShakePercent = (this.percentfinished * duration - kObjectSmashDuration) / kCameraShakeDuration;
  1751. var shakePoint = WebGraphics.makePoint(0, 0);
  1752. if (0 < cameraShakePercent && cameraShakePercent < 1) {
  1753. var minIndex = Math.floor(cameraShakePercent * kNumCameraShakePoints);
  1754. var maxIndex = Math.ceil(WebGraphics.clamp(cameraShakePercent * kNumCameraShakePoints, 0, cameraShakePoints.length - 1));
  1755. var minPoint = cameraShakePoints[minIndex];
  1756. var maxPoint = cameraShakePoints[maxIndex];
  1757. var cameraLerp = cameraShakePercent * kNumCameraShakePoints - minIndex;
  1758. shakePoint = WebGraphics.makePoint(
  1759. WebGraphics.mix(minPoint.x, maxPoint.x, cameraLerp),
  1760. WebGraphics.mix(minPoint.y, maxPoint.y, cameraLerp));
  1761. }
  1762. var objectSmashPercent = WebGraphics.clamp((this.percentfinished * duration) / kObjectSmashDuration, 0, 1);
  1763. var smokepercent = WebGraphics.clamp(((this.percentfinished * duration) - kObjectSmashDuration) / (duration - kObjectSmashDuration), 0, 1);
  1764. var percent = this.percentfinished;
  1765. // calculations for the camera shake
  1766. this.objectY = offsetY + height;
  1767. this.objectY *= (1.0 - objectSmashPercent * objectSmashPercent);
  1768. // draw the texture
  1769. this.drawableObjects[i].MVPMatrix = WebGraphics.translateMatrix4(
  1770. renderer.slideOrthoMatrix,
  1771. offsetX + (shakePoint.x * viewportWidth),
  1772. viewportHeight - offsetY - height + this.objectY + (shakePoint.y * viewportHeight),
  1773. 0);
  1774. this.drawableObjects[i].Opacity = this.parentOpacity * textureInfo.initialState.opacity;
  1775. this.drawableObjects[i].drawFrame();
  1776. // draw smoke
  1777. var MVPMatrix = WebGraphics.translateMatrix4(renderer.slideProjectionMatrix, offsetX, viewportHeight - (offsetY + (height + 16)) * (1 - (smokepercent * smokepercent * 0.02)), 0);
  1778. var smokeSystem = this.smokeSystems[i];
  1779. smokeSystem.setMVPMatrix(MVPMatrix);
  1780. smokeSystem.drawFrame(smokepercent, 1 - (smokepercent * smokepercent));
  1781. // draw specks
  1782. if (smokepercent < 0.50) {
  1783. MVPMatrix = WebGraphics.translateMatrix4(renderer.slideOrthoMatrix, offsetX, viewportHeight - (offsetY + height + 16), 0);
  1784. var speckSystem = this.speckSystems[i];
  1785. speckSystem.setMVPMatrix(MVPMatrix);
  1786. speckSystem.drawFrame(smokepercent, WebGraphics.clamp(1 - WebGraphics.sineMap(smokepercent) * 2, 0, 1));
  1787. }
  1788. } else {
  1789. if (!textureInfo.initialState.hidden) {
  1790. this.drawableObjects[i].Opacity = this.parentOpacity * textureInfo.initialState.opacity;
  1791. this.drawableObjects[i].drawFrame();
  1792. }
  1793. }
  1794. }
  1795. },
  1796. cameraShakePointsWithRandomGenerator: function() {
  1797. var cameraShakePoints = [];
  1798. var globalScale = 0.025;
  1799. for (var i = 0; i < kNumCameraShakePoints; i++) {
  1800. var scale = 1 - (i / kNumCameraShakePoints);
  1801. scale *= scale;
  1802. var thisPoint = WebGraphics.makePoint(
  1803. WebGraphics.randomBetween(-1, 1) * globalScale * scale * 0.4, Math.pow(-1, i) * globalScale * scale);
  1804. cameraShakePoints[i] = thisPoint;
  1805. }
  1806. return cameraShakePoints;
  1807. }
  1808. });
  1809. var KNWebGLBuildFlame = Class.create(KNWebGLProgram, {
  1810. initialize: function($super, renderer, params) {
  1811. this.programData = {
  1812. name: "com.apple.iWork.Keynote.KLNFlame",
  1813. programNames: ["flame"],
  1814. effect: params.effect,
  1815. textures: params.textures
  1816. };
  1817. $super(renderer, this.programData);
  1818. var gl = this.gl;
  1819. // bind required textures from base64 image source
  1820. this.flameTexture = KNWebGLUtil.bindTextureWithImage(gl, flameImage);
  1821. // initialize percent finish
  1822. this.percentfinished = 0;
  1823. // create drawable object for drawing static texture
  1824. this.drawableObjects = [];
  1825. // create framebuffer drawable object array for drawing flame
  1826. this.framebufferDrawableObjects = [];
  1827. this.slideSize = {"width": gl.viewportWidth, "height": gl.viewportHeight};
  1828. var effect = this.effect;
  1829. for (var i = 0, length = this.textures.length; i < length; i++) {
  1830. var texture = params.textures[i];
  1831. var drawableParams = {
  1832. effect: effect,
  1833. textures: [texture]
  1834. };
  1835. var drawableObject = new KNWebGLDrawable(renderer, drawableParams);
  1836. this.drawableObjects.push(drawableObject);
  1837. var drawableFrame = {
  1838. "size": {
  1839. "width": texture.width,
  1840. "height": texture.height
  1841. },
  1842. "origin": {
  1843. "x": texture.offset.pointX,
  1844. "y": texture.offset.pointY
  1845. }
  1846. };
  1847. var frameRect = this.frameOfEffectWithFrame(drawableFrame);
  1848. var framebufferParams = {
  1849. effect: effect,
  1850. textures: [],
  1851. drawableFrame: drawableFrame,
  1852. frameRect: frameRect
  1853. };
  1854. var framebufferDrawable = new KNWebGLFramebufferDrawable(renderer, framebufferParams);
  1855. // push the framebufferDrawable to the array
  1856. this.framebufferDrawableObjects.push(framebufferDrawable);
  1857. }
  1858. // set parent opacity from CA baseLayer
  1859. this.parentOpacity = effect.baseLayer.initialState.opacity;
  1860. // setup requirements
  1861. this.animationWillBeginWithContext();
  1862. },
  1863. frameOfEffectWithFrame: function(drawableFrame) {
  1864. var objSize = drawableFrame.size;
  1865. var slideSize = this.slideSize;
  1866. // the larger the object, the less we have to inflate its size
  1867. var widthAdjust = (1.2 - Math.min(1.0, Math.sqrt(objSize.width / slideSize.width))) + 1.0;
  1868. var heightAdjust = (1.25 - Math.min(1.0, Math.sqrt(objSize.height / slideSize.height))) + 1.0;
  1869. var viewSize = {
  1870. "width": Math.round(objSize.width * widthAdjust),
  1871. "height": Math.round(objSize.height * heightAdjust)
  1872. };
  1873. if (objSize.width / objSize.height < 1.0) {
  1874. // for really skinny objects, make sure GL View is more squarish
  1875. viewSize.width = Math.max(viewSize.width, (objSize.width + objSize.height));
  1876. }
  1877. var rect = {
  1878. "size": viewSize,
  1879. "origin": {
  1880. "x": drawableFrame.origin.x + (objSize.width - viewSize.width) / 2,
  1881. "y": drawableFrame.origin.y + (objSize.height - viewSize.height) / 2
  1882. }
  1883. };
  1884. // Now move the FBO up a bit so only 25% of extra space is on the bottom
  1885. rect.origin.y -= (rect.size.height - drawableFrame.size.height) * 0.25;
  1886. var gl = this.gl;
  1887. var slideRect = {
  1888. "origin": {
  1889. "x": 0,
  1890. "y": 0
  1891. },
  1892. "size": {
  1893. "width": gl.viewportWidth,
  1894. "height": gl.viewportHeight
  1895. }
  1896. };
  1897. var mFrameRect = CGRectIntersection(rect, slideRect);
  1898. mFrameRect = CGRectIntegral(mFrameRect);
  1899. return mFrameRect;
  1900. },
  1901. p_orthoTransformWithScale: function(scale, offset, mFrameRect) {
  1902. var size = {
  1903. "width": mFrameRect.size.width * scale,
  1904. "height": mFrameRect.size.height * scale
  1905. };
  1906. var ortho = WebGraphics.makeOrthoMatrix4(0, size.width, 0, size.height, -1, 1);
  1907. var result = WebGraphics.translateMatrix4(ortho, offset.x, -offset.y, 0);
  1908. return result;
  1909. },
  1910. animationWillBeginWithContext: function() {
  1911. var renderer = this.renderer;
  1912. var gl = this.gl;
  1913. var duration = this.duration / 1000;
  1914. this.flameSystems = [];
  1915. for (var i = 0, length = this.textures.length; i < length; i++) {
  1916. var textureInfo = this.textures[i];
  1917. var width = textureInfo.width;
  1918. var height = textureInfo.height;
  1919. var viewportWidth = gl.viewportWidth;
  1920. var viewportHeight = gl.viewportHeight;
  1921. var framebufferDrawable = this.framebufferDrawableObjects[i];
  1922. var mFrameRect = framebufferDrawable.frameRect
  1923. var mDrawableFrame = framebufferDrawable.drawableFrame;
  1924. var orthoOffset = {
  1925. "x": textureInfo.offset.pointX - mFrameRect.origin.x,
  1926. "y": textureInfo.offset.pointY + height - (mFrameRect.origin.y + mFrameRect.size.height)
  1927. };
  1928. var bottomPadding = mDrawableFrame.origin.y - mFrameRect.origin.y;
  1929. var topPadding = mFrameRect.origin.y + mFrameRect.size.height - (mDrawableFrame.origin.y + mDrawableFrame.size.height);
  1930. orthoOffset.y += (topPadding - bottomPadding);
  1931. framebufferDrawable.MVPMatrix = this.p_orthoTransformWithScale(1.0, orthoOffset, mFrameRect);
  1932. var ratio = width / height;
  1933. var numParticles = Math.round(ratio * 150);
  1934. numParticles *= (duration + Math.max(0, 1.0 - duration / 2));
  1935. // We updated actualSize, so need to update the max speed in the shader
  1936. var flameSystem = new KNWebGLBuildFlameSystem(
  1937. renderer,
  1938. this.program["flame"],
  1939. {"width": width, "height": height},
  1940. {"width": viewportWidth, "height": viewportHeight},
  1941. Math.max(2, this.duration),
  1942. numParticles,
  1943. this.flameTexture
  1944. );
  1945. flameSystem.p_setupParticleDataWithTexture(textureInfo);
  1946. this.flameSystems.push(flameSystem);
  1947. }
  1948. },
  1949. drawFrame: function(difference, elapsed, duration) {
  1950. var renderer = this.renderer;
  1951. var gl = this.gl;
  1952. var program = this.program["flame"];
  1953. var uniforms = program.uniforms;
  1954. var buildOut = this.buildOut;
  1955. var percentfinished = this.percentfinished;
  1956. percentfinished += difference / duration;
  1957. if (percentfinished >= 1) {
  1958. percentfinished = 1;
  1959. this.isCompleted = true;
  1960. }
  1961. this.percentfinished = percentfinished;
  1962. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  1963. for (var i = 0, length = this.textures.length; i < length; i++) {
  1964. var textureInfo = this.textures[i];
  1965. var initialState = textureInfo.initialState;
  1966. var animations = textureInfo.animations;
  1967. if (textureInfo.hasHighlightedBulletAnimation) {
  1968. if (!initialState.hidden) {
  1969. var opacity;
  1970. if (animations.length > 0 && animations[0].property === "opacity") {
  1971. var opacityFrom = animations[0].from.scalar;
  1972. var opacityTo = animations[0].to.scalar;
  1973. var diff = opacityTo - opacityFrom;
  1974. opacity = opacityFrom + diff * this.percentfinished;
  1975. } else {
  1976. opacity = textureInfo.initialState.opacity;
  1977. }
  1978. this.drawableObjects[i].Opacity = this.parentOpacity * opacity;
  1979. this.drawableObjects[i].drawFrame();
  1980. }
  1981. } else if (textureInfo.animations.length > 0) {
  1982. if (this.isCompleted) {
  1983. if (!buildOut) {
  1984. // if completed, just draw its texture object for better performance
  1985. this.drawableObjects[i].Opacity = this.parentOpacity * textureInfo.initialState.opacity;
  1986. this.drawableObjects[i].drawFrame();
  1987. }
  1988. continue;
  1989. }
  1990. var width = textureInfo.width;
  1991. var height = textureInfo.height;
  1992. var offsetX = textureInfo.offset.pointX;
  1993. var offsetY = textureInfo.offset.pointY;
  1994. var viewportWidth = gl.viewportWidth;
  1995. var viewportHeight = gl.viewportHeight;
  1996. duration /= 1000;
  1997. var percent = percentfinished;
  1998. if (buildOut) {
  1999. percent = 1.0 - percent;
  2000. }
  2001. var minCutoff = buildOut ? 0.25 : 0.5;
  2002. var cutoff = Math.min(minCutoff, 1.0 / duration);
  2003. if (percent > cutoff) {
  2004. var newPercent = (percent - cutoff) / (1 - cutoff);
  2005. var alpha = TSUSineMap(Math.min(1.0, 2 * newPercent));
  2006. alpha *= this.parentOpacity * textureInfo.initialState.opacity;
  2007. var drawable = this.drawableObjects[i];
  2008. drawable.Opacity = alpha;
  2009. drawable.drawFrame();
  2010. }
  2011. var framebufferDrawable = this.framebufferDrawableObjects[i];
  2012. var mDrawableFrame = framebufferDrawable.drawableFrame;
  2013. var mFrameRect = framebufferDrawable.frameRect;
  2014. var orthoOffset = {
  2015. "x": textureInfo.offset.pointX - mFrameRect.origin.x,
  2016. "y": textureInfo.offset.pointY + height - (mFrameRect.origin.y + mFrameRect.size.height)
  2017. };
  2018. var bottomPadding = mDrawableFrame.origin.y - mFrameRect.origin.y;
  2019. var topPadding = mFrameRect.origin.y + mFrameRect.size.height - (mDrawableFrame.origin.y + mDrawableFrame.size.height);
  2020. orthoOffset.y += (topPadding - bottomPadding);
  2021. // this is slightly different implementation because we do not scale up and down in web
  2022. var MVPMatrix = this.p_orthoTransformWithScale(1, orthoOffset, mFrameRect);
  2023. // change viewport to match the frame buffer size
  2024. gl.viewport(0, 0, mFrameRect.size.width, mFrameRect.size.height);
  2025. //bind framebuffer
  2026. gl.bindFramebuffer(gl.FRAMEBUFFER, framebufferDrawable.buffer);
  2027. //now render the scene
  2028. gl.clear(gl.COLOR_BUFFER_BIT);
  2029. gl.enable(gl.BLEND);
  2030. gl.blendFunc(gl.SRC_ALPHA, gl.ONE);
  2031. var flameOpacity = (percentfinished == 0.0 || percentfinished == 1.0 ? 0.0 : 1.0);
  2032. // bind framebuffer texture
  2033. gl.bindTexture(gl.TEXTURE_2D, framebufferDrawable.texture);
  2034. var flameSystem = this.flameSystems[i];
  2035. flameSystem.setMVPMatrix(MVPMatrix);
  2036. gl.uniform1f(uniforms["SpeedMax"], flameSystem._speedMax);
  2037. flameSystem.drawFrame(percentfinished, flameOpacity);
  2038. // unbind the framebuffer
  2039. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  2040. // unbind the texture
  2041. gl.bindTexture(gl.TEXTURE_2D, null);
  2042. // change viewport back to original size
  2043. gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
  2044. // send result to framebuffer
  2045. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  2046. framebufferDrawable.MVPMatrix = WebGraphics.translateMatrix4(renderer.slideProjectionMatrix, mFrameRect.origin.x, gl.viewportHeight - (mFrameRect.origin.y + mFrameRect.size.height), 0);
  2047. framebufferDrawable.drawFrame();
  2048. } else {
  2049. if (!textureInfo.initialState.hidden) {
  2050. this.drawableObjects[i].Opacity = this.parentOpacity * textureInfo.initialState.opacity;
  2051. this.drawableObjects[i].drawFrame();
  2052. }
  2053. }
  2054. }
  2055. }
  2056. });
  2057. var KNWebGLTransitionConfetti = Class.create(KNWebGLProgram, {
  2058. initialize: function($super, renderer, params) {
  2059. this.programData = {
  2060. name: "com.apple.iWork.Keynote.KLNConfetti",
  2061. programNames: ["confetti", "defaultTexture"],
  2062. effect: params.effect,
  2063. textures: params.textures
  2064. };
  2065. $super(renderer, this.programData);
  2066. this.useGravity = this.direction === KNDirection.kKNDirectionGravity ? true : false;
  2067. this.percentfinished = 0.0;
  2068. this.animationWillBeginWithContext();
  2069. },
  2070. animationWillBeginWithContext: function(){
  2071. var renderer = this.renderer;
  2072. var gl = this.gl;
  2073. var textures = this.textures;
  2074. var textureInfo = textures[0];
  2075. var width = textureInfo.width;
  2076. var height = textureInfo.height;
  2077. var viewportWidth = gl.viewportWidth;
  2078. var viewportHeight = gl.viewportHeight;
  2079. var numParticles = 10000;
  2080. gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
  2081. // create a confetti system
  2082. this.confettiSystem = new KNWebGLBuildConfettiSystem(
  2083. renderer,
  2084. this.program["confetti"],
  2085. {"width": width, "height": height},
  2086. {"width": viewportWidth, "height": viewportHeight},
  2087. this.duration,
  2088. numParticles,
  2089. textures[1].texture);
  2090. this.confettiSystem.setMVPMatrix(renderer.slideProjectionMatrix);
  2091. // use default texture shader program for incoming slide
  2092. var program = this.program["defaultTexture"];
  2093. // enable attribs before binding and set the program to use.
  2094. KNWebGLUtil.enableAttribs(gl, program);
  2095. var textureCoordinates = [
  2096. 0.0, 0.0,
  2097. 0.0, 1.0,
  2098. 1.0, 0.0,
  2099. 1.0, 1.0,
  2100. ];
  2101. var boxPosition = [
  2102. 0.0, 0.0, -1.0,
  2103. 0.0, viewportHeight, -1.0,
  2104. viewportWidth, 0.0, -1.0,
  2105. viewportWidth, viewportHeight, -1.0,
  2106. ];
  2107. // setup VBO and FTB
  2108. this.textureCoordinatesBuffer = gl.createBuffer();
  2109. gl.bindBuffer(gl.ARRAY_BUFFER, this.textureCoordinatesBuffer);
  2110. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoordinates), gl.STATIC_DRAW);
  2111. gl.vertexAttribPointer(program.attribs["TexCoord"], 2, gl.FLOAT, false, 0, 0);
  2112. this.positionBuffer = gl.createBuffer();
  2113. KNWebGLUtil.bindDynamicBufferWithData(gl, program.attribs["Position"], this.positionBuffer, boxPosition, 3);
  2114. gl.uniformMatrix4fv(program.uniforms["MVPMatrix"], false, renderer.slideOrthoMatrix);
  2115. gl.activeTexture(gl.TEXTURE0);
  2116. gl.uniform1i(program.uniforms["Texture"], 0);
  2117. this.drawFrame(0, 0, 4);
  2118. },
  2119. drawFrame: function(difference, elapsed, duration) {
  2120. var gl = this.gl;
  2121. var viewportWidth = gl.viewportWidth;
  2122. var viewportHeight = gl.viewportHeight;
  2123. var percentfinished = this.percentfinished;
  2124. percentfinished += difference / duration;
  2125. if (percentfinished > 1) {
  2126. percentfinished = 1;
  2127. this.isCompleted = true;
  2128. }
  2129. var percent = this.percentfinished = percentfinished;
  2130. var revPercent = 1 - percent;
  2131. var myPercent = 1 - revPercent*revPercent*revPercent;
  2132. myPercent = myPercent*(1-percent*percent) + (1-revPercent*revPercent)*(percent*percent) + percent;
  2133. myPercent *= 0.5;
  2134. myPercent*= myPercent;
  2135. var scale= 0.75 + (1 - Math.pow(revPercent,4)) * 0.25;
  2136. var quadShaderMVPMatrix = WebGraphics.translateMatrix4(this.renderer.slideProjectionMatrix, viewportWidth / 2, viewportHeight / 2, 0);
  2137. quadShaderMVPMatrix = WebGraphics.scaleMatrix4(quadShaderMVPMatrix, scale, scale, 1);
  2138. quadShaderMVPMatrix = WebGraphics.translateMatrix4(quadShaderMVPMatrix, -viewportWidth / 2, -viewportHeight / 2, 0);
  2139. // draw the incoming slide
  2140. var program = this.program["defaultTexture"];
  2141. gl.useProgram(program.shaderProgram);
  2142. gl.uniformMatrix4fv(program.uniforms["MVPMatrix"], false, quadShaderMVPMatrix);
  2143. this.draw();
  2144. //draw the confetti system frame
  2145. var finalPercent = 1 - percent;
  2146. finalPercent = WebGraphics.clamp(finalPercent, 0, 1);
  2147. myPercent = WebGraphics.clamp(myPercent, 0, 1);
  2148. if (this.useGravity) {
  2149. var ratio = 1;
  2150. var MVPMatrix = this.renderer.slideProjectionMatrix;
  2151. MVPMatrix = WebGraphics.translateMatrix4(MVPMatrix, 0, -viewportHeight * 2 * percent * percent * (1.0 - ratio * 0.5), 0);
  2152. this.confettiSystem.setMVPMatrix(MVPMatrix);
  2153. }
  2154. this.confettiSystem.drawFrame(myPercent, finalPercent);
  2155. },
  2156. draw: function() {
  2157. var gl = this.gl;
  2158. var program = this.program["defaultTexture"];
  2159. var attribs = program.attribs;
  2160. var viewportWidth = gl.viewportWidth;
  2161. var viewportHeight = gl.viewportHeight;
  2162. gl.useProgram(program.shaderProgram);
  2163. var textureCoordinates = [
  2164. 0.0, 0.0,
  2165. 0.0, 1.0,
  2166. 1.0, 0.0,
  2167. 1.0, 1.0,
  2168. ];
  2169. var boxPosition = [
  2170. 0.0, 0.0, -1.0,
  2171. 0.0, viewportHeight, -1.0,
  2172. viewportWidth, 0.0, -1.0,
  2173. viewportWidth, viewportHeight, -1.0,
  2174. ];
  2175. gl.bindBuffer(gl.ARRAY_BUFFER, this.textureCoordinatesBuffer);
  2176. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoordinates), gl.STATIC_DRAW);
  2177. gl.vertexAttribPointer(attribs["TexCoord"], 2, gl.FLOAT, false, 0, 0);
  2178. KNWebGLUtil.bindDynamicBufferWithData(gl, attribs["Position"], this.positionBuffer, boxPosition, 3);
  2179. // bind incoming texture
  2180. gl.bindTexture(gl.TEXTURE_2D, this.textures[0].texture);
  2181. gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
  2182. }
  2183. });
  2184. var KNWebGLBuildConfetti = Class.create(KNWebGLProgram, {
  2185. initialize: function($super, renderer, params) {
  2186. var effect = params.effect;
  2187. this.programData = {
  2188. name: "com.apple.iWork.Keynote.KLNConfetti",
  2189. programNames: ["confetti"],
  2190. effect: effect,
  2191. textures: params.textures
  2192. };
  2193. $super(renderer, this.programData);
  2194. this.useGravity = this.direction === KNDirection.kKNDirectionGravity ? true : false;
  2195. this.percentfinished = 0.0;
  2196. // create drawable object for drawing static texture
  2197. this.drawableObjects = [];
  2198. for (var i = 0, length = this.textures.length; i < length; i++) {
  2199. var texture = params.textures[i];
  2200. var drawableParams = {
  2201. effect: effect,
  2202. textures: [texture]
  2203. };
  2204. var drawableObject = new KNWebGLDrawable(renderer, drawableParams);
  2205. this.drawableObjects.push(drawableObject);
  2206. }
  2207. // set parent opacity from CA baseLayer
  2208. this.parentOpacity = effect.baseLayer.initialState.opacity;
  2209. // setup requirements
  2210. this.animationWillBeginWithContext();
  2211. },
  2212. animationWillBeginWithContext: function() {
  2213. var renderer = this.renderer;
  2214. var gl = this.gl;
  2215. var viewportWidth = gl.viewportWidth;
  2216. var viewportHeight = gl.viewportHeight;
  2217. this.confettiSystems = [];
  2218. for (var i = 0, length = this.textures.length; i < length; i++) {
  2219. var textureInfo = this.textures[i];
  2220. var width = textureInfo.width;
  2221. var height = textureInfo.height;
  2222. var ratio = (height / viewportHeight * width / viewportWidth);
  2223. ratio = Math.sqrt(Math.sqrt(ratio));
  2224. var numParticles = Math.round(ratio * 10000);
  2225. // create a confetti system
  2226. var confettiSystem = new KNWebGLBuildConfettiSystem(
  2227. renderer,
  2228. this.program["confetti"],
  2229. {"width": width, "height": height},
  2230. {"width": viewportWidth, "height": viewportHeight},
  2231. this.duration,
  2232. numParticles,
  2233. textureInfo.texture);
  2234. // set ratio so we don't need to recalculate during draw frame
  2235. confettiSystem.ratio = ratio;
  2236. this.confettiSystems.push(confettiSystem);
  2237. }
  2238. },
  2239. drawFrame: function(difference, elapsed, duration) {
  2240. var renderer = this.renderer;
  2241. var gl = this.gl;
  2242. var viewportWidth = gl.viewportWidth;
  2243. var viewportHeight = gl.viewportHeight;
  2244. // determine the type and direction
  2245. var buildIn = this.buildIn;
  2246. var buildOut = this.buildOut;
  2247. var percentfinished = this.percentfinished;
  2248. percentfinished += difference / duration;
  2249. if (percentfinished > 1) {
  2250. percentfinished = 1;
  2251. this.isCompleted = true;
  2252. }
  2253. this.percentfinished = percentfinished;
  2254. for (var i = 0, length = this.textures.length; i < length; i++) {
  2255. var textureInfo = this.textures[i];
  2256. var initialState = textureInfo.initialState;
  2257. var animations = textureInfo.animations;
  2258. if (textureInfo.hasHighlightedBulletAnimation) {
  2259. if (!initialState.hidden) {
  2260. var opacity;
  2261. if (animations.length > 0 && animations[0].property === "opacity") {
  2262. var opacityFrom = animations[0].from.scalar;
  2263. var opacityTo = animations[0].to.scalar;
  2264. var diff = opacityTo - opacityFrom;
  2265. opacity = opacityFrom + diff * percentfinished;
  2266. } else {
  2267. opacity = textureInfo.initialState.opacity;
  2268. }
  2269. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  2270. this.drawableObjects[i].Opacity = this.parentOpacity * opacity;
  2271. this.drawableObjects[i].drawFrame();
  2272. }
  2273. } else if (textureInfo.animations.length > 0) {
  2274. if (this.isCompleted) {
  2275. if (buildIn) {
  2276. // if completed, just draw its texture object for better performance
  2277. this.drawableObjects[i].Opacity = this.parentOpacity * textureInfo.initialState.opacity;
  2278. this.drawableObjects[i].drawFrame();
  2279. }
  2280. continue;
  2281. }
  2282. var width = textureInfo.width;
  2283. var height = textureInfo.height;
  2284. var percent = buildIn ? 1 - percentfinished : percentfinished;
  2285. var revPercent = 1 - percent;
  2286. var myPercent = 1 - revPercent * revPercent * revPercent;
  2287. myPercent = myPercent * (1 - percent * percent) + (1 - revPercent * revPercent) * (percent * percent) + percent;
  2288. myPercent *= 0.5;
  2289. if (buildIn) {
  2290. myPercent *= myPercent;
  2291. }
  2292. //draw the confetti system frame
  2293. var confettiSystem = this.confettiSystems[i];
  2294. var MVPMatrix = WebGraphics.translateMatrix4(renderer.slideProjectionMatrix, textureInfo.offset.pointX, viewportHeight - (textureInfo.offset.pointY + height), 0);
  2295. var finalPercent = 1 - percent;
  2296. finalPercent = WebGraphics.clamp(finalPercent, 0, 1);
  2297. myPercent = WebGraphics.clamp(myPercent, 0, 1);
  2298. if (this.useGravity) {
  2299. var ratio = confettiSystem.ratio;
  2300. MVPMatrix = WebGraphics.translateMatrix4(MVPMatrix, 0, -viewportHeight * 2 * percent * percent * (1.0 - ratio * 0.5), 0);
  2301. }
  2302. gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
  2303. confettiSystem.setMVPMatrix(MVPMatrix);
  2304. confettiSystem.drawFrame(myPercent, finalPercent);
  2305. } else {
  2306. if (!textureInfo.initialState.hidden) {
  2307. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  2308. this.drawableObjects[i].Opacity = this.parentOpacity * textureInfo.initialState.opacity;
  2309. this.drawableObjects[i].drawFrame();
  2310. }
  2311. }
  2312. }
  2313. }
  2314. });
  2315. var KNWebGLBuildDiffuse = Class.create(KNWebGLProgram, {
  2316. initialize: function($super, renderer, params) {
  2317. var effect = params.effect;
  2318. this.programData = {
  2319. name: "com.apple.iWork.Keynote.KLNDiffuse",
  2320. programNames: ["diffuse"],
  2321. effect: effect,
  2322. textures: params.textures
  2323. };
  2324. $super(renderer, this.programData);
  2325. this.percentfinished = 0.0;
  2326. // create drawable object for drawing static texture
  2327. this.drawableObjects = [];
  2328. for (var i = 0, length = this.textures.length; i < length; i++) {
  2329. var texture = params.textures[i];
  2330. var drawableParams = {
  2331. effect: effect,
  2332. textures: [texture]
  2333. };
  2334. var drawableObject = new KNWebGLDrawable(renderer, drawableParams);
  2335. this.drawableObjects.push(drawableObject);
  2336. }
  2337. // set parent opacity from CA baseLayer
  2338. this.parentOpacity = effect.baseLayer.initialState.opacity;
  2339. // setup requirements
  2340. this.animationWillBeginWithContext();
  2341. },
  2342. animationWillBeginWithContext: function() {
  2343. var renderer = this.renderer;
  2344. var gl = this.gl;
  2345. var viewportWidth = gl.viewportWidth;
  2346. var viewportHeight = gl.viewportHeight;
  2347. this.diffuseSystems = [];
  2348. for (var i = 0, length = this.textures.length; i < length; i++) {
  2349. var textureInfo = this.textures[i];
  2350. var width = textureInfo.width;
  2351. var height = textureInfo.height;
  2352. var ratio = (height / viewportHeight * width / viewportWidth);
  2353. ratio = Math.sqrt(Math.sqrt(ratio));
  2354. var numParticles = Math.round(ratio * 4000);
  2355. // create a confetti system
  2356. var diffuseSystem = new KNWebGLBuildDiffuseSystem(
  2357. renderer,
  2358. this.program["diffuse"],
  2359. {"width": width, "height": height},
  2360. {"width": viewportWidth, "height": viewportHeight},
  2361. this.duration,
  2362. numParticles,
  2363. textureInfo.texture,
  2364. this.direction === KNDirection.kKNDirectionRightToLeft);
  2365. this.diffuseSystems.push(diffuseSystem);
  2366. }
  2367. },
  2368. drawFrame: function(difference, elapsed, duration) {
  2369. var renderer = this.renderer;
  2370. var gl = this.gl;
  2371. var viewportWidth = gl.viewportWidth;
  2372. var viewportHeight = gl.viewportHeight;
  2373. var percentfinished = this.percentfinished;
  2374. percentfinished += difference / duration;
  2375. if (percentfinished > 1) {
  2376. percentfinished = 1;
  2377. this.isCompleted = true;
  2378. }
  2379. this.percentfinished = percentfinished;
  2380. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  2381. for (var i = 0, length = this.textures.length; i < length; i++) {
  2382. var textureInfo = this.textures[i];
  2383. var initialState = textureInfo.initialState;
  2384. var animations = textureInfo.animations;
  2385. if (textureInfo.hasHighlightedBulletAnimation) {
  2386. if (!initialState.hidden) {
  2387. var opacity;
  2388. if (animations.length > 0 && animations[0].property === "opacity") {
  2389. var opacityFrom = animations[0].from.scalar;
  2390. var opacityTo = animations[0].to.scalar;
  2391. var diff = opacityTo - opacityFrom;
  2392. opacity = opacityFrom + diff * percentfinished;
  2393. } else {
  2394. opacity = textureInfo.initialState.opacity;
  2395. }
  2396. this.drawableObjects[i].Opacity = this.parentOpacity * opacity;
  2397. this.drawableObjects[i].drawFrame();
  2398. }
  2399. } else if (textureInfo.animations.length > 0) {
  2400. var width = textureInfo.width;
  2401. var height = textureInfo.height;
  2402. var offsetX = textureInfo.offset.pointX;
  2403. var offsetY = textureInfo.offset.pointY;
  2404. //draw the diffuse system frame
  2405. var diffuseSystem = this.diffuseSystems[i];
  2406. var MVPMatrix = WebGraphics.translateMatrix4(renderer.slideProjectionMatrix, offsetX, viewportHeight - (offsetY + height), 0);
  2407. diffuseSystem.setMVPMatrix(MVPMatrix);
  2408. diffuseSystem.drawFrame(this.percentfinished, 1.0);
  2409. } else {
  2410. if (!textureInfo.initialState.hidden) {
  2411. this.drawableObjects[i].Opacity = this.parentOpacity * textureInfo.initialState.opacity;
  2412. this.drawableObjects[i].drawFrame();
  2413. }
  2414. }
  2415. }
  2416. }
  2417. });
  2418. var KNWebGLBuildFireworks = Class.create(KNWebGLProgram, {
  2419. initialize: function($super, renderer, params) {
  2420. this.programData = {
  2421. name: "com.apple.iWork.Keynote.KNFireworks",
  2422. programNames: ["fireworks"],
  2423. effect: params.effect,
  2424. textures: params.textures
  2425. };
  2426. $super(renderer, this.programData);
  2427. var gl = this.gl;
  2428. // animation parameter group
  2429. this.animParameterGroup = new KNAnimParameterGroup("Fireworks");
  2430. // bind required textures from base64 image source
  2431. this.fireworksTexture = KNWebGLUtil.bindTextureWithImage(gl, fireworksImage);
  2432. this.fireworksCenterBurstTexture = KNWebGLUtil.bindTextureWithImage(gl, fireworksCenterBurstImage);
  2433. // initialize percent finish
  2434. this.percentfinished = 0;
  2435. this.prevpercentfinished = 0;
  2436. // create drawable object for drawing static texture
  2437. this.drawableObjects = [];
  2438. // frame rect for all firework systems
  2439. this.frameRect = this.frameOfEffectWithFrame();
  2440. this.slideSize = {"width": gl.viewportWidth, "height": gl.viewportHeight};
  2441. var effect = this.effect;
  2442. for (var i = 0, length = this.textures.length; i < length; i++) {
  2443. var texture = params.textures[i];
  2444. var drawableParams = {
  2445. effect: effect,
  2446. textures: [texture]
  2447. };
  2448. var drawableObject = new KNWebGLDrawable(renderer, drawableParams);
  2449. // push drawable object to drawableObjects array
  2450. this.drawableObjects.push(drawableObject);
  2451. }
  2452. // set parent opacity from CA baseLayer
  2453. this.parentOpacity = effect.baseLayer.initialState.opacity;
  2454. // setup requirements
  2455. this.animationWillBeginWithContext();
  2456. },
  2457. frameOfEffectWithFrame: function() {
  2458. var gl = this.gl;
  2459. var slideRect = {
  2460. "origin": {
  2461. "x": 0,
  2462. "y": 0
  2463. },
  2464. "size": {
  2465. "width": gl.viewportWidth,
  2466. "height": gl.viewportHeight
  2467. }
  2468. };
  2469. return slideRect;
  2470. },
  2471. p_orthoTransformWithScale: function(scale, offset, mFrameRect) {
  2472. var size = {
  2473. "width": mFrameRect.size.width * scale,
  2474. "height": mFrameRect.size.height * scale
  2475. };
  2476. var ortho = WebGraphics.makeOrthoMatrix4(0, size.width, 0, size.height, -1, 1);
  2477. var result = WebGraphics.translateMatrix4(ortho, offset.x, -offset.y, 0);
  2478. return result;
  2479. },
  2480. p_setupFBOWithSize: function(size) {
  2481. this.framebuffer = new TSDGLFrameBuffer(this.gl, size, 2);
  2482. },
  2483. p_fireworksSystemsForTR: function(textureInfo) {
  2484. var renderer = this.renderer;
  2485. var gl = this.gl;
  2486. var viewportWidth = gl.viewportWidth;
  2487. var viewportHeight = gl.viewportHeight;
  2488. var duration = this.duration / 1000;
  2489. var parameterGroup = this.animParameterGroup;
  2490. var numFireworks = duration * parameterGroup.doubleForKey("FireworksCount");
  2491. // At least 2 fireworks!
  2492. numFireworks = Math.max(2, numFireworks);
  2493. var systems = [];
  2494. var startOnLeftIndex = 0;
  2495. var startOnRightIndex = 1;
  2496. var startImmediatelyIndex = parseInt(WebGraphics.randomBetween(0, numFireworks - 1));
  2497. for (var i = 0; i < numFireworks; i++) {
  2498. var numParticles = parameterGroup.doubleForKey("ParticleCount");
  2499. var minSlideSide = Math.min(viewportWidth, viewportHeight);
  2500. var fireworkSpan = minSlideSide * WebGraphics.doubleBetween(parameterGroup.doubleForKey("FireworkSizeMin"), parameterGroup.doubleForKey("FireworkSizeMax"));
  2501. var particleSystem = new KNWebGLBuildFireworksSystem(
  2502. renderer,
  2503. this.program["fireworks"],
  2504. {"width": textureInfo.width, "height": textureInfo.height},
  2505. {"width": viewportWidth, "height": viewportHeight},
  2506. this.duration,
  2507. {"width": numParticles, "height": 1},
  2508. {"width": 1, "height": 1},
  2509. this.fireworksTexture
  2510. );
  2511. var randomSize = WebGraphics.makeSize(parameterGroup.doubleForKey("ParticleSizeMin"), parameterGroup.doubleForKey("ParticleSizeMax"));
  2512. randomSize.width = randomSize.width * minSlideSide / 100;
  2513. randomSize.height = randomSize.height * minSlideSide / 100;
  2514. particleSystem.randomParticleSizeMinMax = randomSize;
  2515. particleSystem.maxDistance = fireworkSpan;
  2516. particleSystem.colorRandomness = parameterGroup.doubleForKey("ParticleColorRandomness");
  2517. particleSystem.lifeSpanMinDuration = parameterGroup.doubleForKey("ParticleLifeSpanMinDuration");
  2518. particleSystem.randomParticleSpeedMinMax = WebGraphics.makePoint(parameterGroup.doubleForKey("FireworkSpeedMin"), parameterGroup.doubleForKey("FireworkSpeedMax"));
  2519. if (i % 2 === 0) {
  2520. // 1/2 of particles start in left half
  2521. particleSystem.fireworkStartingPositionX = WebGraphics.randomBetween(0, 0.5);
  2522. } else if (i % 2 === 1) {
  2523. // 1/2 of particles start in right half
  2524. particleSystem.fireworkStartingPositionX = WebGraphics.randomBetween(0.5, 1);
  2525. }
  2526. if (i === startOnLeftIndex) {
  2527. // Make sure at least one burst is all the way on the left side
  2528. particleSystem.fireworkStartingPositionX = 0;
  2529. }
  2530. if (i === startOnRightIndex) {
  2531. // Make sure at least one burst is all the way on the right side
  2532. particleSystem.fireworkStartingPositionX = 1;
  2533. }
  2534. // Lifespan/duration of firework
  2535. var randomDuration = WebGraphics.randomBetween(parameterGroup.doubleForKey("FireworkDurationMin"), parameterGroup.doubleForKey("FireworkDurationMax"));
  2536. randomDuration /= duration;
  2537. var startTime = WebGraphics.randomBetween(0, 1.0 - randomDuration);
  2538. if (i === startImmediatelyIndex) {
  2539. // Make sure ONE of the fireworks starts right away!
  2540. startTime = 0;
  2541. }
  2542. startTime = Math.max(startTime, 0.001);
  2543. particleSystem.lifeSpan = {
  2544. "start": startTime,
  2545. "duration": randomDuration
  2546. };
  2547. particleSystem.setupWithTexture(textureInfo);
  2548. systems.push(particleSystem);
  2549. }
  2550. return systems;
  2551. },
  2552. animationWillBeginWithContext: function() {
  2553. var renderer = this.renderer;
  2554. var gl = this.gl;
  2555. var parameterGroup = this.animParameterGroup;
  2556. var centerBurstVertexRect = CGRectMake(0, 0, 512, 512);
  2557. var vertexRect = CGRectMake(0, 0, this.slideSize.width, this.slideSize.height);
  2558. var textureRect = CGRectMake(0, 0, 1, 1);
  2559. var meshSize = CGSizeMake(2, 2);
  2560. var mFrameRect = this.frameRect;
  2561. this.fireworksSystems = [];
  2562. for (var i = 0, length = this.textures.length; i < length; i++) {
  2563. var texture = this.textures[i];
  2564. var orthoOffset = {
  2565. "x": texture.offset.pointX - mFrameRect.origin.x,
  2566. "y": texture.offset.pointY + texture.height - (mFrameRect.origin.y + mFrameRect.size.height)
  2567. };
  2568. var baseOrthoTransform = WebGraphics.makeOrthoMatrix4(0, mFrameRect.size.width, 0, mFrameRect.size.height, -1, 1);
  2569. var baseTransform = WebGraphics.translateMatrix4(baseOrthoTransform, orthoOffset.x, -orthoOffset.y, 0);
  2570. // init object shader and data buffer
  2571. var objectShader = new TSDGLShader(gl);
  2572. objectShader.initWithDefaultTextureAndOpacityShader();
  2573. // object shader set methods
  2574. objectShader.setMat4WithTransform3D(baseTransform, kTSDGLShaderUniformMVPMatrix);
  2575. objectShader.setGLint(0, kTSDGLShaderUniformTexture);
  2576. // init object data buffer
  2577. var objectTextureRect = texture.textureRect;
  2578. var objectVertexRect = CGRectMake(0, 0, objectTextureRect.size.width, objectTextureRect.size.height);
  2579. var objectDataBuffer = new TSDGLDataBuffer(gl);
  2580. objectDataBuffer.initWithVertexRect(objectVertexRect, TSDRectUnit, meshSize, false, false);
  2581. // Set up shaders for particle systems
  2582. var fireworksMVP = renderer.slideProjectionMatrix;
  2583. fireworksMVP = WebGraphics.translateMatrix4(fireworksMVP, orthoOffset.x, -orthoOffset.y, 0);
  2584. var fireworksSystems = this.p_fireworksSystemsForTR(texture);
  2585. // set up FBO
  2586. this.p_setupFBOWithSize(mFrameRect.size);
  2587. var fboShader = this.fboShader = new TSDGLShader(gl);
  2588. fboShader.initWithShaderFileNames("fireworkstrails", "fireworkstrails");
  2589. fboShader.setMat4WithTransform3D(baseOrthoTransform, kTSDGLShaderUniformMVPMatrix);
  2590. fboShader.setGLint(0, kTSDGLShaderUniformTexture);
  2591. var fboDataBuffer = this.fboDataBuffer = new TSDGLDataBuffer(gl);
  2592. fboDataBuffer.initWithVertexRect(CGRectMake(0, 0, mFrameRect.size.width, mFrameRect.size.height), TSDRectUnit, meshSize, false, false);
  2593. var centerBurstShader = this.centerBurstShader = new TSDGLShader(gl);
  2594. centerBurstShader.initWithDefaultTextureAndOpacityShader();
  2595. centerBurstShader.setGLFloat(1.0, kTSDGLShaderUniformOpacity);
  2596. var centerBurstDataBuffer = this.centerBurstDataBuffer = new TSDGLDataBuffer(gl);
  2597. centerBurstDataBuffer.initWithVertexRect(centerBurstVertexRect, TSDRectUnit, meshSize, false, false);
  2598. var _bloomEffect = this._bloomEffect = new TSDGLBloomEffect(gl);
  2599. _bloomEffect.initWithEffectSize(mFrameRect.size, parameterGroup.doubleForKey("BloomBlurScale"));
  2600. var fireworksSystem = {
  2601. "_baseOrthoTransform": baseOrthoTransform,
  2602. "_baseTransform": baseTransform,
  2603. "objectShader": objectShader,
  2604. "objectDataBuffer": objectDataBuffer,
  2605. "fireworksMVP": fireworksMVP,
  2606. "systems": fireworksSystems
  2607. };
  2608. this.fireworksSystems.push(fireworksSystem);
  2609. gl.clearColor(0.0, 0.0, 0.0, 0.0);
  2610. gl.enable(gl.BLEND);
  2611. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  2612. gl.disable(gl.DEPTH_TEST);
  2613. }
  2614. },
  2615. drawFrame: function(difference, elapsed, duration) {
  2616. var renderer = this.renderer;
  2617. var gl = this.gl;
  2618. var program = this.program["fireworks"];
  2619. var uniforms = program.uniforms;
  2620. var buildOut = this.buildOut;
  2621. var percentfinished = this.percentfinished;
  2622. var parameterGroup = this.animParameterGroup;
  2623. var noiseAmount = parameterGroup.doubleForKey("ParticleTrailsDitherAmount");
  2624. var noiseMax = parameterGroup.doubleForKey("ParticleTrailsDitherMax");
  2625. var bloomAmount = parameterGroup.doubleForKey("BloomPower");
  2626. percentfinished += difference / duration;
  2627. if (percentfinished >= 1) {
  2628. percentfinished = 1;
  2629. this.isCompleted = true;
  2630. }
  2631. this.percentfinished = percentfinished;
  2632. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  2633. for (var i = 0, length = this.textures.length; i < length; i++) {
  2634. var textureInfo = this.textures[i];
  2635. var initialState = textureInfo.initialState;
  2636. var animations = textureInfo.animations;
  2637. if (textureInfo.hasHighlightedBulletAnimation) {
  2638. if (!initialState.hidden) {
  2639. var opacity;
  2640. if (animations.length > 0 && animations[0].property === "opacity") {
  2641. var opacityFrom = animations[0].from.scalar;
  2642. var opacityTo = animations[0].to.scalar;
  2643. var diff = opacityTo - opacityFrom;
  2644. opacity = opacityFrom + diff * this.percentfinished;
  2645. } else {
  2646. opacity = textureInfo.initialState.opacity;
  2647. }
  2648. this.drawableObjects[i].Opacity = this.parentOpacity * opacity;
  2649. this.drawableObjects[i].drawFrame();
  2650. }
  2651. } else if (textureInfo.animations.length > 0) {
  2652. if (this.isCompleted) {
  2653. if (!buildOut) {
  2654. // if completed, just draw its texture object for better performance
  2655. this.drawableObjects[i].Opacity = this.parentOpacity * textureInfo.initialState.opacity;
  2656. this.drawableObjects[i].drawFrame();
  2657. }
  2658. continue;
  2659. }
  2660. var width = textureInfo.width;
  2661. var height = textureInfo.height;
  2662. var offsetX = textureInfo.offset.pointX;
  2663. var offsetY = textureInfo.offset.pointY;
  2664. var viewportWidth = gl.viewportWidth;
  2665. var viewportHeight = gl.viewportHeight;
  2666. duration /= 1000;
  2667. var percent = percentfinished;
  2668. var currentGLFramebuffer = TSDGLFrameBuffer.currentGLFramebuffer(gl);
  2669. var fireworksSystem = this.fireworksSystems[i];
  2670. var objectShader = fireworksSystem.objectShader;
  2671. var objectDataBuffer = fireworksSystem.objectDataBuffer;
  2672. // Draw the actual object
  2673. this.p_drawObject(percent, textureInfo, objectShader, objectDataBuffer);
  2674. // Draw particles into FBO to save trails
  2675. var framebuffer = this.framebuffer;
  2676. var fboShader = this.fboShader;
  2677. var fboDataBuffer = this.fboDataBuffer;
  2678. var previousFBOTexture = framebuffer.currentGLTexture();
  2679. framebuffer.setCurrentTextureToNext();
  2680. framebuffer.bindFramebuffer();
  2681. // clear current framebuffer texture
  2682. gl.clear(gl.COLOR_BUFFER_BIT);
  2683. // change viewport to match the frame buffer size
  2684. gl.viewport(0, 0, framebuffer.size.width, framebuffer.size.height);
  2685. // First, draw existing trails, but faded out a bit
  2686. // bind previous framebuffer texture so we can take the content and draw into current one
  2687. gl.bindTexture(gl.TEXTURE_2D, previousFBOTexture);
  2688. var minDuration = parameterGroup.doubleForKey("FireworkDurationMin") / duration;
  2689. minDuration = Math.min(minDuration / 2.0, 1.0);
  2690. var trailsFadePercent = WebGraphics.clamp((percentfinished - minDuration) / (1.0 - minDuration), 0, 1);
  2691. var trailsFadeOut = 1.0 - WebGraphics.mix(parameterGroup.doubleForKey("TrailsFadeOutMin"), parameterGroup.doubleForKey("TrailsFadeOutMax"), Math.pow(trailsFadePercent, 2));
  2692. fboShader.setGLFloat(trailsFadeOut, kTSDGLShaderUniformOpacity);
  2693. fboShader.setGLFloat(noiseAmount, kShaderUniformNoiseAmount);
  2694. fboShader.setGLFloat(noiseMax, kShaderUniformNoiseMax);
  2695. var noiseSeed = WebGraphics.makePoint(WebGraphics.randomBetween(0, 1), WebGraphics.randomBetween(0, 1));
  2696. fboShader.setPoint2D(noiseSeed, kShaderUniformNoiseSeed);
  2697. fboDataBuffer.drawWithShader(this.fboShader, true);
  2698. // Draw center burst
  2699. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  2700. // need to use fireworks program before drawing particle system
  2701. gl.useProgram(program.shaderProgram);
  2702. var gravity = parameterGroup.doubleForKey("Gravity");
  2703. gravity *= Math.min(viewportWidth, viewportHeight) * 0.001;
  2704. gravity *= duration; // acceleration is per second!
  2705. gl.uniform1f(uniforms["Gravity"], gravity);
  2706. var minSlideSide = Math.min(viewportWidth, viewportHeight);
  2707. var startScale = minSlideSide * parameterGroup.doubleForKey("ParticleSizeStart") / 100;
  2708. gl.uniform1f(uniforms["StartScale"], startScale);
  2709. gl.uniform1f(uniforms["SparklePeriod"], parameterGroup.doubleForKey("SparklePeriod"));
  2710. // draw particle system with percent
  2711. this.drawParticleSystemsWithPercent(percentfinished, false, 1.0, fireworksSystem);
  2712. // change viewport back to original
  2713. gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
  2714. // done drawing particle into FBO so unbind the framebuffer
  2715. framebuffer.unbindFramebufferAndBindGLFramebuffer(currentGLFramebuffer);
  2716. // Draw particle trails
  2717. var maxDuration = parameterGroup.doubleForKey("FireworkDurationMax");
  2718. maxDuration = Math.min(maxDuration, 0.999);
  2719. var particleOpacityPercent = WebGraphics.clamp((percentfinished - maxDuration) / (1.0 - maxDuration), 0, 1);
  2720. var particleSystemOpacity = 1.0 - parameterGroup.doubleForAnimationCurve("ParticleTransparency", particleOpacityPercent);
  2721. // apply bloom effect
  2722. this._bloomEffect.bindFramebuffer();
  2723. gl.clear(gl.COLOR_BUFFER_BIT);
  2724. // draw to bloom effect's _colorFramebuffer
  2725. fboShader.setGLFloat(particleSystemOpacity, kTSDGLShaderUniformOpacity);
  2726. fboShader.setGLFloat(0, kShaderUniformNoiseAmount);
  2727. gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
  2728. // bind to current framebuffer texture
  2729. gl.bindTexture(gl.TEXTURE_2D, framebuffer.currentGLTexture());
  2730. // draw trails FBO to bloom effect FBO
  2731. fboDataBuffer.drawWithShader(fboShader, true);
  2732. // draw new sparkles into bloom effect FBO
  2733. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  2734. // need to use the program before drawing fireworks particle system
  2735. gl.useProgram(program.shaderProgram);
  2736. this.drawParticleSystemsWithPercent(percentfinished, true, particleSystemOpacity, fireworksSystem);
  2737. // unbind bloom effect framebuffer and bind to default drawing buffer
  2738. this._bloomEffect.unbindFramebufferAndBindGLFramebuffer(currentGLFramebuffer);
  2739. // additive blend mode
  2740. gl.blendFunc(gl.ONE, gl.ONE);
  2741. this._bloomEffect.drawBloomEffectWithMVPMatrix(fireworksSystem._baseOrthoTransform, bloomAmount, currentGLFramebuffer);
  2742. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  2743. } else {
  2744. if (!textureInfo.initialState.hidden) {
  2745. this.drawableObjects[i].Opacity = this.parentOpacity * textureInfo.initialState.opacity;
  2746. this.drawableObjects[i].drawFrame();
  2747. }
  2748. }
  2749. }
  2750. this.prevpercentfinished = this.percentfinished;
  2751. },
  2752. p_drawObject: function(percent, textureInfo, objectShader, objectDataBuffer) {
  2753. var gl = this.gl;
  2754. var parameterGroup = this.animParameterGroup;
  2755. var beginTime = parameterGroup.doubleForKey("TextOpacityBeginTime");
  2756. var endTime = parameterGroup.doubleForKey("TextOpacityEndTime");
  2757. percent = WebGraphics.clamp((percent - beginTime) / (endTime - beginTime), 0, 1);
  2758. var opacity = this.parentOpacity * textureInfo.initialState.opacity;
  2759. opacity *= parameterGroup.doubleForAnimationCurve("TextOpacityTiming", percent);
  2760. objectShader.setGLFloat(opacity, kTSDGLShaderUniformOpacity);
  2761. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  2762. gl.bindTexture(gl.TEXTURE_2D, textureInfo.texture);
  2763. objectDataBuffer.drawWithShader(objectShader, true);
  2764. },
  2765. drawParticleSystemsWithPercent: function(percent, shouldDrawSparkles, particleSystemOpacity, fireworksSystem) {
  2766. var renderer = this.renderer;
  2767. var gl = this.gl;
  2768. var program = this.program["fireworks"];
  2769. var uniforms = program.uniforms;
  2770. var parameterGroup = this.animParameterGroup;
  2771. var systems = fireworksSystem.systems;
  2772. var baseTransform = fireworksSystem._baseTransform;
  2773. var MVPMatrix = fireworksSystem.fireworksMVP;
  2774. // need to use fireworks program before drawing particle system
  2775. gl.useProgram(program.shaderProgram);
  2776. gl.uniform1f(uniforms["ShouldSparkle"], shouldDrawSparkles ? 1 : 0);
  2777. for (var i = 0, length = systems.length; i < length; i++) {
  2778. var particleSystem = systems[i];
  2779. var lifeSpan = particleSystem.lifeSpan;
  2780. var systemPercent = (percent - lifeSpan.start) / lifeSpan.duration;
  2781. if (systemPercent <= 0 || systemPercent >= 1) {
  2782. continue;
  2783. }
  2784. var systemPercent = WebGraphics.clamp(systemPercent, 0, 1);
  2785. var prevSystemPercent = (this.prevpercentfinished - lifeSpan.start) / lifeSpan.duration;
  2786. prevSystemPercent = WebGraphics.clamp(prevSystemPercent, systemPercent / 2, 1);
  2787. var opacity = particleSystemOpacity;
  2788. if (shouldDrawSparkles) {
  2789. opacity = 1.0 - parameterGroup.doubleForAnimationCurve("ParticleTransparency", systemPercent);
  2790. }
  2791. // Also send in previous particle burst timing so we can blur in direction of burst velocity and avoid strobing
  2792. var prevParticleBurstTiming = parameterGroup.doubleForAnimationCurve("ParticleBurstTiming", prevSystemPercent);
  2793. var particleBurstTiming = parameterGroup.doubleForAnimationCurve("ParticleBurstTiming", systemPercent);
  2794. gl.uniform1f(uniforms["ParticleBurstTiming"], particleBurstTiming);
  2795. gl.uniform1f(uniforms["PreviousParticleBurstTiming"], prevParticleBurstTiming);
  2796. gl.uniform1f(uniforms["PreviousPercent"], prevSystemPercent);
  2797. if (!shouldDrawSparkles) {
  2798. // Draw big center burst once at very first frame of Firework... the FBO fading will handle persisting it for a bit
  2799. if (!particleSystem.didDrawCenterBurst) {
  2800. gl.bindTexture(gl.TEXTURE_2D, this.fireworksCenterBurstTexture);
  2801. // Scale is percent of slide size
  2802. var scale = gl.viewportHeight / 512;
  2803. scale *= WebGraphics.randomBetween(parameterGroup.doubleForKey("CenterBurstScaleMin"), parameterGroup.doubleForKey("CenterBurstScaleMax"));
  2804. var center = particleSystem._startingPoint;
  2805. var t = WebGraphics.translateMatrix4(baseTransform, center.x, center.y, 0);
  2806. var centerAdjust = WebGraphics.makePoint(-(512 / 2.0 * scale), -(512 / 2.0 * scale));
  2807. t = WebGraphics.translateMatrix4(t, centerAdjust.x, centerAdjust.y, 0);
  2808. t = WebGraphics.scaleMatrix4(t, scale, scale, 1);
  2809. this.centerBurstShader.setGLFloat(parameterGroup.doubleForKey("CenterBurstOpacity"), kTSDGLShaderUniformOpacity);
  2810. this.centerBurstShader.setMat4WithTransform3D(t, kTSDGLShaderUniformMVPMatrix);
  2811. gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
  2812. this.centerBurstDataBuffer.drawWithShader(this.centerBurstShader, true);
  2813. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  2814. particleSystem.didDrawCenterBurst = true;
  2815. }
  2816. }
  2817. // need to use fireworks program before drawing particle system
  2818. gl.useProgram(program.shaderProgram);
  2819. particleSystem.setMVPMatrix(MVPMatrix);
  2820. particleSystem.drawFrame(systemPercent, opacity);
  2821. }
  2822. }
  2823. });
  2824. var KNWebGLBuildShimmer = Class.create(KNWebGLProgram, {
  2825. initialize: function($super, renderer, params) {
  2826. var effect = params.effect;
  2827. this.programData = {
  2828. name: "com.apple.iWork.Keynote.KLNShimmer",
  2829. programNames: ["shimmerObject", "shimmerParticle"],
  2830. effect: effect,
  2831. textures: params.textures
  2832. };
  2833. $super(renderer, this.programData);
  2834. var gl = this.gl;
  2835. this.percentfinished = 0.0;
  2836. // create drawable object for drawing static texture
  2837. this.drawableObjects = [];
  2838. this.slideOrigin = {"x": 0, "y": 0};
  2839. this.slideSize = {"width": gl.viewportWidth, "height": gl.viewportHeight};
  2840. this.slideRect = {
  2841. "origin": this.slideOrigin,
  2842. "size": this.slideSize
  2843. };
  2844. for (var i = 0, length = this.textures.length; i < length; i++) {
  2845. var texture = params.textures[i];
  2846. var drawableFrame = texture.textureRect;
  2847. var drawableParams = {
  2848. effect: effect,
  2849. textures: [texture]
  2850. };
  2851. var frameRect = this.frameOfEffectWithFrame(drawableFrame);
  2852. var drawableObject = new KNWebGLDrawable(renderer, drawableParams);
  2853. drawableObject.frameRect = frameRect;
  2854. this.drawableObjects.push(drawableObject);
  2855. }
  2856. // set parent opacity from CA baseLayer
  2857. this.parentOpacity = effect.baseLayer.initialState.opacity;
  2858. // setup requirements
  2859. this.animationWillBeginWithContext();
  2860. },
  2861. frameOfEffectWithFrame: function(drawableFrame) {
  2862. var gl = this.gl;
  2863. var minPt = {
  2864. "x": CGRectGetMinX(drawableFrame),
  2865. "y": CGRectGetMinY(drawableFrame)
  2866. };
  2867. var maxPt = {
  2868. "x": CGRectGetMaxX(drawableFrame),
  2869. "y": CGRectGetMaxY(drawableFrame)
  2870. };
  2871. var extraPadding = Math.max(drawableFrame.size.width, drawableFrame.size.height);
  2872. extraPadding = Math.max(extraPadding, this.slideSize.height / 3.0);
  2873. minPt.y -= extraPadding;
  2874. maxPt.y += extraPadding;
  2875. minPt.x -= extraPadding;
  2876. maxPt.x += extraPadding;
  2877. var frameRect = TSDRectWithPoints(minPt, maxPt);
  2878. frameRect = CGRectIntersection(frameRect, this.slideRect);
  2879. frameRect = CGRectIntegral(frameRect);
  2880. return frameRect;
  2881. },
  2882. animationWillBeginWithContext: function() {
  2883. var renderer = this.renderer;
  2884. // initialize a shimmer effect object for each texture rectangle
  2885. this.shimmerEffects = [];
  2886. var program = this.program;
  2887. var slideRect = this.slideRect;
  2888. var duration = this.duration;
  2889. var direction = this.direction;
  2890. var type = this.type;
  2891. var parentOpacity = this.parentOpacity;
  2892. for (var i = 0, length = this.textures.length; i < length; i++) {
  2893. var texture = this.textures[i];
  2894. var tr = this.textures[i].textureRect;
  2895. var frameRect = this.drawableObjects[i].frameRect;
  2896. var orthoOffset = {
  2897. "x": texture.offset.pointX - frameRect.origin.x,
  2898. "y": texture.offset.pointY + texture.height - (frameRect.origin.y + frameRect.size.height)
  2899. };
  2900. var baseOrthoTransform = WebGraphics.makeOrthoMatrix4(0, frameRect.size.width, 0, frameRect.size.height, -1, 1);
  2901. var baseTransform = WebGraphics.translateMatrix4(baseOrthoTransform, orthoOffset.x, -orthoOffset.y, 0);
  2902. var shimmerEffect = new KNWebGLBuildShimmerEffect(
  2903. renderer,
  2904. program,
  2905. slideRect,
  2906. texture,
  2907. frameRect,
  2908. baseTransform,
  2909. duration,
  2910. direction,
  2911. type,
  2912. parentOpacity
  2913. );
  2914. this.shimmerEffects.push(shimmerEffect);
  2915. }
  2916. },
  2917. drawFrame: function(difference, elapsed, duration) {
  2918. var renderer = this.renderer;
  2919. var gl = this.gl;
  2920. var viewportWidth = gl.viewportWidth;
  2921. var viewportHeight = gl.viewportHeight;
  2922. var percentfinished = this.percentfinished;
  2923. percentfinished += difference / duration;
  2924. if (percentfinished > 1) {
  2925. percentfinished = 1;
  2926. this.isCompleted = true;
  2927. }
  2928. this.percentfinished = percentfinished;
  2929. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  2930. for (var i = 0, length = this.textures.length; i < length; i++) {
  2931. var textureInfo = this.textures[i];
  2932. var initialState = textureInfo.initialState;
  2933. var animations = textureInfo.animations;
  2934. if (textureInfo.hasHighlightedBulletAnimation) {
  2935. if (!initialState.hidden) {
  2936. var opacity;
  2937. if (animations.length > 0 && animations[0].property === "opacity") {
  2938. var opacityFrom = animations[0].from.scalar;
  2939. var opacityTo = animations[0].to.scalar;
  2940. var diff = opacityTo - opacityFrom;
  2941. opacity = opacityFrom + diff * percentfinished;
  2942. } else {
  2943. opacity = textureInfo.initialState.opacity;
  2944. }
  2945. this.drawableObjects[i].Opacity = this.parentOpacity * opacity;
  2946. this.drawableObjects[i].drawFrame();
  2947. }
  2948. } else if (textureInfo.animations.length > 0) {
  2949. if (this.isCompleted) {
  2950. if (this.buildIn) {
  2951. // if completed, just draw its texture object for better performance
  2952. this.drawableObjects[i].Opacity = this.parentOpacity * textureInfo.initialState.opacity;
  2953. this.drawableObjects[i].drawFrame();
  2954. }
  2955. continue;
  2956. }
  2957. var width = textureInfo.width;
  2958. var height = textureInfo.height;
  2959. var offsetX = textureInfo.offset.pointX;
  2960. var offsetY = textureInfo.offset.pointY;
  2961. //draw shimmer effect
  2962. var shimmerEffect = this.shimmerEffects[i];
  2963. shimmerEffect.renderEffectAtPercent(this.percentfinished);
  2964. } else {
  2965. if (!textureInfo.initialState.hidden) {
  2966. this.drawableObjects[i].Opacity = this.parentOpacity * textureInfo.initialState.opacity;
  2967. this.drawableObjects[i].drawFrame();
  2968. }
  2969. }
  2970. }
  2971. }
  2972. });
  2973. var KNWebGLBuildShimmerEffect = Class.create({
  2974. initialize: function(renderer, program, slideRect, texture, destinationRect, translate, duration, direction, buildType, parentOpacity) {
  2975. this.renderer = renderer;
  2976. this.gl = renderer.gl;
  2977. this.program = program;
  2978. this._slideRect = slideRect;
  2979. this._texture = texture;
  2980. this._destinationRect = destinationRect;
  2981. this._translate = translate;
  2982. this._duration = duration;
  2983. this._direction = direction;
  2984. this._buildType = buildType;
  2985. this._baseTransform = new Float32Array(16);
  2986. this._isSetup = false;
  2987. this.parentOpacity = parentOpacity;
  2988. // bind shimmer texture
  2989. this.shimmerTexture = KNWebGLUtil.bindTextureWithImage(this.gl, shimmerImage);
  2990. this.setupEffectIfNecessary();
  2991. },
  2992. setupEffectIfNecessary: function() {
  2993. if (this._isSetup) {
  2994. return;
  2995. }
  2996. var gl = this.gl;
  2997. var texture = this._texture;
  2998. var meshSize = CGSizeMake(2, 2);
  2999. var mFrameRect = {
  3000. "origin": {
  3001. "x": 0,
  3002. "y": 0
  3003. },
  3004. "size": {
  3005. "width": gl.viewportWidth,
  3006. "height": gl.viewportHeight
  3007. }
  3008. };
  3009. var orthoOffset = {
  3010. "x": texture.offset.pointX - mFrameRect.origin.x,
  3011. "y": texture.offset.pointY + texture.height - (mFrameRect.origin.y + mFrameRect.size.height)
  3012. };
  3013. var baseOrthoTransform = WebGraphics.makeOrthoMatrix4(0, mFrameRect.size.width, 0, mFrameRect.size.height, -1, 1);
  3014. var baseTransform = this.baseTransform = WebGraphics.translateMatrix4(baseOrthoTransform, orthoOffset.x, -orthoOffset.y, 0);
  3015. this._objectSystem = this.objectSystemForTR(this._texture, this._slideRect, this._duration);
  3016. this._objectSystem.setMVPMatrix(this.baseTransform);
  3017. // Set up particle particle system
  3018. if (this._objectSystem.shouldDraw) {
  3019. // Only set up the particles if we will actually draw this particle system!
  3020. this._particleSystem = this.particleSystemForTR(this._texture, this._slideRect, this._duration);
  3021. this._particleSystem.setMVPMatrix(this.baseTransform);
  3022. }
  3023. this._isSetup = true;
  3024. },
  3025. p_numberOfParticlesForTR: function(tr, slideRect, duration) {
  3026. var destRect = this._destinationRect;
  3027. var slideSize = slideRect.size;
  3028. var slideRatio = (destRect.size.width / slideSize.width * destRect.size.height / slideSize.height);
  3029. var texRatio = (tr.size.width / destRect.size.width * tr.size.height / destRect.size.height);
  3030. // create as many particles as possible without hitting our vertex limit
  3031. var numParticles = parseInt(Math.min((slideRatio * texRatio * 2000), 3276));
  3032. return numParticles;
  3033. },
  3034. objectSystemForTR: function(texture, slideRect, duration) {
  3035. var tr = texture.textureRect;
  3036. var numParticles = this.p_numberOfParticlesForTR(tr, slideRect, duration);
  3037. var particleSystem = new KNWebGLBuildShimmerObjectSystem(
  3038. this.renderer,
  3039. this.program["shimmerObject"],
  3040. {"width": tr.size.width, "height": tr.size.height},
  3041. {"width": slideRect.size.width, "height": slideRect.size.height},
  3042. duration,
  3043. numParticles,
  3044. texture.texture,
  3045. this._direction
  3046. );
  3047. return particleSystem;
  3048. },
  3049. particleSystemForTR: function(texture, slideRect, duration) {
  3050. var tr = texture.textureRect;
  3051. // Extra sparkles at end
  3052. var extraParticles = this.p_numberOfParticlesForTR(tr, slideRect, duration);
  3053. extraParticles = Math.max(2, extraParticles / 40);
  3054. // Add in sparkles to match object's particles
  3055. var objectSystemParticleCount = this._objectSystem.particleCount;
  3056. var numParticles = objectSystemParticleCount;
  3057. numParticles += extraParticles;
  3058. numParticles = Math.min(numParticles, 3276);
  3059. var particleSystem = new KNWebGLBuildShimmerParticleSystem(
  3060. this.renderer,
  3061. this.program["shimmerParticle"],
  3062. {"width": tr.size.width, "height": tr.size.height},
  3063. {"width": slideRect.size.width, "height": slideRect.size.height},
  3064. duration,
  3065. CGSizeMake(numParticles, 1),
  3066. this._objectSystem.particleSize,
  3067. this._objectSystem,
  3068. this.shimmerTexture,
  3069. this._direction
  3070. );
  3071. return particleSystem;
  3072. },
  3073. p_drawObject: function(percent, textureInfo, objectShader, objectDataBuffer) {
  3074. var gl = this.gl;
  3075. var opacity = this.parentOpacity * textureInfo.initialState.opacity;
  3076. opacity = opacity * TSUSineMap(percent);
  3077. objectShader.setGLFloat(opacity, kTSDGLShaderUniformOpacity);
  3078. gl.bindTexture(gl.TEXTURE_2D, textureInfo.texture);
  3079. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  3080. objectDataBuffer.drawWithShader(objectShader, true);
  3081. },
  3082. renderEffectAtPercent: function(percent) {
  3083. var gl = this.gl;
  3084. var texture = this._texture;
  3085. if (this._buildType === "buildOut") {
  3086. percent = 1.0 - percent;
  3087. }
  3088. var accelPercent = (1 - percent) * (1 - percent);
  3089. var isClockwise = this._buildType === "buildIn";
  3090. var rotation = (TSUReverseSquare(percent) * this._duration/1000 + percent) * Math.PI/2;
  3091. if (!isClockwise) {
  3092. rotation *= -1.0;
  3093. }
  3094. // Draw main object as pieces
  3095. var objectOpacitySpan = WebGraphics.makePoint(0.2, 0.4);
  3096. var objectOpacity = (percent - objectOpacitySpan.x) / objectOpacitySpan.y;
  3097. objectOpacity = WebGraphics.clamp(objectOpacity, 0.0, 1.0);
  3098. objectOpacity = TSUSineMap(objectOpacity);
  3099. var opacity = this.parentOpacity * texture.initialState.opacity;
  3100. objectOpacity *= opacity;
  3101. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  3102. // need to use the program before drawing the particle system
  3103. gl.useProgram(this.program["shimmerObject"].shaderProgram);
  3104. // set MVP Matrix for object system
  3105. this._objectSystem.setMVPMatrix(this.baseTransform);
  3106. this._objectSystem.drawGLSLWithPercent(accelPercent, objectOpacity, rotation, isClockwise, texture.texture);
  3107. // Draw shimmers
  3108. // need to use the program before drawing the particle system
  3109. gl.useProgram(this.program["shimmerParticle"].shaderProgram);
  3110. this._particleSystem.setMVPMatrix(this.baseTransform);
  3111. this._particleSystem.drawGLSLWithPercent(accelPercent, opacity * 0.5, rotation, isClockwise, this.shimmerTexture);
  3112. }
  3113. });
  3114. var KNWebGLBuildSparkle = Class.create(KNWebGLProgram, {
  3115. initialize: function($super, renderer, params) {
  3116. var effect = params.effect;
  3117. this.programData = {
  3118. name: "com.apple.iWork.Keynote.KLNSparkle",
  3119. programNames: ["sparkle"],
  3120. effect: effect,
  3121. textures: params.textures
  3122. };
  3123. $super(renderer, this.programData);
  3124. var gl = this.gl;
  3125. this.percentfinished = 0.0;
  3126. // create drawable object for drawing static texture
  3127. this.drawableObjects = [];
  3128. this.slideOrigin = {"x": 0, "y": 0};
  3129. this.slideSize = {"width": gl.viewportWidth, "height": gl.viewportHeight};
  3130. this.slideRect = {
  3131. "origin": this.slideOrigin,
  3132. "size": this.slideSize
  3133. };
  3134. for (var i = 0, length = this.textures.length; i < length; i++) {
  3135. var texture = params.textures[i];
  3136. var drawableFrame = texture.textureRect;
  3137. var drawableParams = {
  3138. effect: effect,
  3139. textures: [texture]
  3140. };
  3141. var frameRect = this.frameOfEffectWithFrame(drawableFrame);
  3142. var drawableObject = new KNWebGLDrawable(renderer, drawableParams);
  3143. drawableObject.frameRect = frameRect;
  3144. this.drawableObjects.push(drawableObject);
  3145. }
  3146. // set parent opacity from CA baseLayer
  3147. this.parentOpacity = effect.baseLayer.initialState.opacity;
  3148. // setup requirements
  3149. this.animationWillBeginWithContext();
  3150. },
  3151. frameOfEffectWithFrame: function(drawableFrame) {
  3152. var minPt = WebGraphics.makePoint(CGRectGetMinX(drawableFrame), CGRectGetMinY(drawableFrame));
  3153. var maxPt = WebGraphics.makePoint(CGRectGetMaxX(drawableFrame), CGRectGetMaxY(drawableFrame));
  3154. var extraPadding = Math.max(drawableFrame.size.width, drawableFrame.size.height);
  3155. // Make sure the width is large enough to deal with floating point precision errors in proj matrix
  3156. // (Otherwise very small text will look blurry)
  3157. extraPadding = Math.max(extraPadding, 128);
  3158. minPt.y = Math.max(CGRectGetMinY(this.slideRect), minPt.y - extraPadding);
  3159. maxPt.y = Math.min(CGRectGetMaxY(this.slideRect), maxPt.y + extraPadding);
  3160. minPt.x = Math.max(CGRectGetMinX(this.slideRect), minPt.x - extraPadding);
  3161. maxPt.x = Math.min(CGRectGetMaxX(this.slideRect), maxPt.x + extraPadding);
  3162. var frameRect = TSDRectWithPoints(minPt, maxPt);
  3163. frameRect = CGRectIntegral(frameRect);
  3164. return frameRect;
  3165. },
  3166. animationWillBeginWithContext: function() {
  3167. var renderer = this.renderer;
  3168. // initialize a shimmer effect object for each texture rectangle
  3169. this.sparkleEffects = [];
  3170. var program = this.program;
  3171. var slideRect = this.slideRect;
  3172. var duration = this.duration;
  3173. var direction = this.direction;
  3174. var type = this.type;
  3175. var parentOpacity = this.parentOpacity;
  3176. for (var i = 0, length = this.textures.length; i < length; i++) {
  3177. var texture = this.textures[i];
  3178. var direction = this.direction;
  3179. var tr = this.textures[i].textureRect;
  3180. var frameRect = this.drawableObjects[i].frameRect;
  3181. var orthoOffset = {
  3182. "x": texture.offset.pointX - frameRect.origin.x,
  3183. "y": texture.offset.pointY + texture.height - (frameRect.origin.y + frameRect.size.height)
  3184. };
  3185. var baseOrthoTransform = WebGraphics.makeOrthoMatrix4(0, frameRect.size.width, 0, frameRect.size.height, -1, 1);
  3186. var baseTransform = WebGraphics.translateMatrix4(baseOrthoTransform, orthoOffset.x, -orthoOffset.y, 0);
  3187. var sparkleEffect = new KNWebGLBuildSparkleEffect(
  3188. renderer,
  3189. program,
  3190. slideRect,
  3191. texture,
  3192. frameRect,
  3193. baseTransform,
  3194. duration,
  3195. direction,
  3196. type,
  3197. parentOpacity
  3198. );
  3199. this.sparkleEffects.push(sparkleEffect);
  3200. }
  3201. },
  3202. drawFrame: function(difference, elapsed, duration) {
  3203. var renderer = this.renderer;
  3204. var gl = this.gl;
  3205. var viewportWidth = gl.viewportWidth;
  3206. var viewportHeight = gl.viewportHeight;
  3207. // determine the type and direction
  3208. var buildIn = this.buildIn;
  3209. var buildOut = this.buildOut;
  3210. var percentfinished = this.percentfinished;
  3211. percentfinished += difference / duration;
  3212. if (percentfinished > 1) {
  3213. percentfinished = 1;
  3214. this.isCompleted = true;
  3215. }
  3216. this.percentfinished = percentfinished;
  3217. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  3218. for (var i = 0, length = this.textures.length; i < length; i++) {
  3219. var textureInfo = this.textures[i];
  3220. var initialState = textureInfo.initialState;
  3221. var animations = textureInfo.animations;
  3222. if (textureInfo.hasHighlightedBulletAnimation) {
  3223. if (!initialState.hidden) {
  3224. var opacity;
  3225. if (animations.length > 0 && animations[0].property === "opacity") {
  3226. var opacityFrom = animations[0].from.scalar;
  3227. var opacityTo = animations[0].to.scalar;
  3228. var diff = opacityTo - opacityFrom;
  3229. opacity = opacityFrom + diff * percentfinished;
  3230. } else {
  3231. opacity = textureInfo.initialState.opacity;
  3232. }
  3233. this.drawableObjects[i].Opacity = this.parentOpacity * opacity;
  3234. this.drawableObjects[i].drawFrame();
  3235. }
  3236. } else if (textureInfo.animations.length > 0) {
  3237. if (this.isCompleted) {
  3238. if (buildIn) {
  3239. // if completed, just draw its texture object for better performance
  3240. this.drawableObjects[i].Opacity = this.parentOpacity * textureInfo.initialState.opacity;
  3241. this.drawableObjects[i].drawFrame();
  3242. }
  3243. continue;
  3244. }
  3245. //draw shimmer effect
  3246. var sparkleEffect = this.sparkleEffects[i];
  3247. sparkleEffect.renderEffectAtPercent(this.percentfinished);
  3248. } else {
  3249. if (!textureInfo.initialState.hidden) {
  3250. this.drawableObjects[i].Opacity = this.parentOpacity * textureInfo.initialState.opacity;
  3251. this.drawableObjects[i].drawFrame();
  3252. }
  3253. }
  3254. }
  3255. }
  3256. });
  3257. var KNWebGLBuildSparkleEffect = Class.create({
  3258. initialize: function(renderer, program, slideRect, texture, destinationRect, translate, duration, direction, buildType, parentOpacity) {
  3259. this.renderer = renderer;
  3260. this.gl = renderer.gl;
  3261. this.program = program;
  3262. this._slideRect = slideRect;
  3263. this._texture = texture;
  3264. this._destinationRect = destinationRect;
  3265. this._translate = translate;
  3266. this._duration = duration;
  3267. this._direction = direction;
  3268. this._buildType = buildType;
  3269. this._baseTransform = new Float32Array(16);
  3270. this._isSetup = false;
  3271. this.parentOpacity = parentOpacity;
  3272. // bind shimmer texture
  3273. this.sparkleTexture = KNWebGLUtil.bindTextureWithImage(this.gl, sparkleImage);
  3274. this.setupEffectIfNecessary();
  3275. },
  3276. setupEffectIfNecessary: function() {
  3277. if (this._isSetup) {
  3278. return;
  3279. }
  3280. var gl = this.gl;
  3281. var texture = this._texture;
  3282. var meshSize = CGSizeMake(2, 2);
  3283. var mFrameRect = {
  3284. "origin": {
  3285. "x": 0,
  3286. "y": 0
  3287. },
  3288. "size": {
  3289. "width": gl.viewportWidth,
  3290. "height": gl.viewportHeight
  3291. }
  3292. };
  3293. var orthoOffset = {
  3294. "x": texture.offset.pointX - mFrameRect.origin.x,
  3295. "y": texture.offset.pointY + texture.height - (mFrameRect.origin.y + mFrameRect.size.height)
  3296. };
  3297. var baseOrthoTransform = WebGraphics.makeOrthoMatrix4(0, mFrameRect.size.width, 0, mFrameRect.size.height, -1, 1);
  3298. var baseTransform = this.baseTransform = WebGraphics.translateMatrix4(baseOrthoTransform, orthoOffset.x, -orthoOffset.y, 0);
  3299. // init object shader and data buffer
  3300. var objectShader = this._objectShader = new TSDGLShader(gl);
  3301. objectShader.initWithDefaultTextureAndOpacityShader();
  3302. // object shader set methods
  3303. objectShader.setMat4WithTransform3D(baseTransform, kTSDGLShaderUniformMVPMatrix);
  3304. objectShader.setGLint(0, kTSDGLShaderUniformTexture);
  3305. // new data buffer attributes
  3306. var objectPositionAttribute = new TSDGLDataBufferAttribute(kTSDGLShaderAttributePosition, GL_STREAM_DRAW, GL_FLOAT, false, 2);
  3307. var objectTexCoordAttribute = new TSDGLDataBufferAttribute(kTSDGLShaderAttributeTexCoord, GL_STREAM_DRAW, GL_FLOAT, false, 2);
  3308. // init object data buffer
  3309. var objectDataBuffer = this._objectDataBuffer = new TSDGLDataBuffer(gl);
  3310. objectDataBuffer.newDataBufferWithVertexAttributes([objectPositionAttribute, objectTexCoordAttribute] , meshSize, true);
  3311. // Set up sparkle particle system
  3312. this.sparkleSystem = this.sparkleSystemForTR(this._texture, this._slideRect, this._duration);
  3313. this.sparkleSystem.setMVPMatrix(baseTransform);
  3314. this.sparkleSystem.setColor(new Float32Array([1, 1, 1, 1]));
  3315. this._isSetup = true;
  3316. },
  3317. p_numberOfParticlesForTR: function(tr, slideRect, duration) {
  3318. var destRect = this._destinationRect;
  3319. var slideSize = slideRect.size;
  3320. var slideRatio = (destRect.size.width / slideSize.width * destRect.size.height / slideSize.height);
  3321. var texRatio = (tr.size.width / destRect.size.width * tr.size.height / destRect.size.height);
  3322. // create as many particles as possible without hitting our vertex limit
  3323. var numParticles = parseInt(Math.min((slideRatio * texRatio * 2000), 3276));
  3324. return numParticles;
  3325. },
  3326. sparkleSystemForTR: function(texture, slideRect, duration) {
  3327. var tr = texture.textureRect;
  3328. var slideSize = this._slideRect.size;
  3329. var boundingRect = this._destinationRect;
  3330. var slideRatio = Math.min(boundingRect.size.width, slideSize.width) / slideSize.width * Math.min(boundingRect.size.height, slideSize.height) / slideSize.height;
  3331. var numParticles = parseInt(((2 - Math.sqrt(slideRatio)) / 2) * 1500 * this._duration / 1000);
  3332. var sparkleSystem = new KNWebGLBuildSparkleSystem(
  3333. this.renderer,
  3334. this.program["sparkle"],
  3335. {"width": tr.size.width, "height": tr.size.height},
  3336. {"width": slideRect.size.width, "height": slideRect.size.height},
  3337. duration,
  3338. CGSizeMake(numParticles, 1),
  3339. {"width": 128, "height": 128},
  3340. this.sparkleTexture,
  3341. this._direction
  3342. );
  3343. return sparkleSystem;
  3344. },
  3345. p_drawObject: function(percent, textureInfo, objectShader, objectDataBuffer) {
  3346. var gl = this.gl;
  3347. var opacity = this.parentOpacity * textureInfo.initialState.opacity;
  3348. opacity = opacity * TSUSineMap(percent);
  3349. objectShader.setGLFloat(opacity, kTSDGLShaderUniformOpacity);
  3350. gl.bindTexture(gl.TEXTURE_2D, textureInfo.texture);
  3351. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  3352. objectDataBuffer.drawWithShader(objectShader, true);
  3353. },
  3354. renderEffectAtPercent: function(percent) {
  3355. var gl = this.gl;
  3356. var texture = this._texture;
  3357. var direction = this._direction;
  3358. var tr = texture.textureRect;
  3359. var isReverse = (direction == KNDirection.kKNDirectionRightToLeft || direction == KNDirection.kKNDirectionTopToBottom);
  3360. var isHorizontal = (direction == KNDirection.kKNDirectionRightToLeft || direction == KNDirection.kKNDirectionLeftToRight);
  3361. var mvpMatrix = this._translate;
  3362. var alpha = this.parentOpacity * texture.initialState.opacity;
  3363. // CONSTANTS
  3364. var duration = this._duration / 1000;
  3365. var blurWidth = 0.2 / duration;
  3366. var width = tr.size.width;
  3367. var height = tr.size.height;
  3368. // =========
  3369. // FIRST, we draw the original image fading out
  3370. var particleTiming = KNSparkleMaxParticleLife / Math.max(0.75, duration);
  3371. var opaqueWidth = percent / (1. - particleTiming);
  3372. var xStart = 0, yStart = 0, xEnd = 0, yEnd = 0, xTexStart = 0, yTexStart = 0, xTexEnd = 0, yTexEnd = 0;
  3373. if (this._buildType == "buildOut") {
  3374. opaqueWidth -= blurWidth;
  3375. xStart = (isHorizontal) ? ((isReverse) ? 0 : width) : 0;
  3376. yStart = (isHorizontal) ? 0 : ((isReverse) ? 0 : height);
  3377. xEnd = (isHorizontal) ? ((isReverse) ? width - (width * WebGraphics.clamp(opaqueWidth, 0, 1)) : width * WebGraphics.clamp(opaqueWidth, 0, 1)) : width;
  3378. yEnd = (isHorizontal) ? height : ((isReverse) ? height - (height * WebGraphics.clamp(opaqueWidth, 0, 1)) : (height * WebGraphics.clamp(opaqueWidth, 0, 1)));
  3379. xTexStart = (isHorizontal) ? ((isReverse) ? 0 : 1) : 0;
  3380. yTexStart = (isHorizontal) ? 0 : ((isReverse) ? 0 : 1);
  3381. xTexEnd = (isHorizontal) ? ((isReverse) ? 1 - (1 * WebGraphics.clamp(opaqueWidth, 0, 1)) : (1 * WebGraphics.clamp(opaqueWidth, 0, 1))) : 1;
  3382. yTexEnd = (isHorizontal) ? 1 : ((isReverse) ? 1 - (1 * WebGraphics.clamp(opaqueWidth, 0, 1)) : (1 * WebGraphics.clamp(opaqueWidth, 0, 1)));
  3383. } else {
  3384. opaqueWidth -= blurWidth;
  3385. xStart = (isHorizontal) ? ((isReverse) ? width : 0) : 0;
  3386. yStart = (isHorizontal) ? 0 : ((isReverse) ? height : 0);
  3387. xEnd = (isHorizontal) ? ((isReverse) ? width - (width * WebGraphics.clamp(opaqueWidth, 0, 1)) : width * WebGraphics.clamp(opaqueWidth, 0, 1)) : width;
  3388. yEnd = (isHorizontal) ? height : ((isReverse) ? height - (height * WebGraphics.clamp(opaqueWidth, 0, 1)) : height * WebGraphics.clamp(opaqueWidth, 0, 1));
  3389. xTexStart = (isHorizontal) ? ((isReverse) ? 1 : 0) : 0;
  3390. yTexStart = (isHorizontal) ? 0 : ((isReverse) ? 1 : 0);
  3391. xTexEnd = (isHorizontal) ? ((isReverse) ? 1 - (1 * WebGraphics.clamp(opaqueWidth, 0, 1)) : 1 * WebGraphics.clamp(opaqueWidth, 0, 1)) : 1;
  3392. yTexEnd = (isHorizontal) ? 1 : ((isReverse) ? 1 - (1 * WebGraphics.clamp(opaqueWidth, 0, 1)) : 1 * WebGraphics.clamp(opaqueWidth, 0, 1));
  3393. }
  3394. gl.bindTexture(gl.TEXTURE_2D, texture.texture);
  3395. this._objectShader.setGLFloat(alpha, kTSDGLShaderUniformOpacity);
  3396. // update data buffer position and text coord
  3397. var objectDataBuffer = this._objectDataBuffer;
  3398. var objectPositionAttribute = objectDataBuffer.vertexAttributeNamed(kTSDGLShaderAttributePosition);
  3399. var objectTexCoordAttribute = objectDataBuffer.vertexAttributeNamed(kTSDGLShaderAttributeTexCoord);
  3400. objectDataBuffer.setGLPoint2D(WebGraphics.makePoint(xStart, yStart), objectPositionAttribute, 0);
  3401. objectDataBuffer.setGLPoint2D(WebGraphics.makePoint(xEnd, yStart), objectPositionAttribute, 1);
  3402. objectDataBuffer.setGLPoint2D(WebGraphics.makePoint(xStart, yEnd), objectPositionAttribute, 2);
  3403. objectDataBuffer.setGLPoint2D(WebGraphics.makePoint(xEnd, yEnd), objectPositionAttribute, 3);
  3404. objectDataBuffer.setGLPoint2D(WebGraphics.makePoint(xTexStart, yTexStart), objectTexCoordAttribute, 0);
  3405. objectDataBuffer.setGLPoint2D(WebGraphics.makePoint(xTexEnd, yTexStart), objectTexCoordAttribute, 1);
  3406. objectDataBuffer.setGLPoint2D(WebGraphics.makePoint(xTexStart, yTexEnd), objectTexCoordAttribute, 2);
  3407. objectDataBuffer.setGLPoint2D(WebGraphics.makePoint(xTexEnd, yTexEnd), objectTexCoordAttribute, 3);
  3408. objectDataBuffer.drawWithShader(this._objectShader, true);
  3409. gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  3410. // need to use the program before drawing the particle system
  3411. gl.useProgram(this.program["sparkle"].shaderProgram);
  3412. this.sparkleSystem.setMVPMatrix(this.baseTransform);
  3413. this.sparkleSystem.drawFrame(percent, 1.0);
  3414. }
  3415. });
  3416. var KNWebGLTransitionMagicMove = Class.create(KNWebGLProgram, {
  3417. initialize: function($super, renderer, params) {
  3418. // initialize default program data for core animation wrapper program
  3419. this.coreAnimationWrapperProgram = new KNWebGLCoreAnimationWrapperProgram(params);
  3420. // create WebGL program using core animation wrapper program data
  3421. $super(renderer, this.coreAnimationWrapperProgram.data);
  3422. var gl = this.gl;
  3423. this.percentfinished = 0.0;
  3424. // create drawable object for drawing the texture
  3425. this.drawableObjects = [];
  3426. this.slideOrigin = {"x": 0, "y": 0};
  3427. this.slideSize = {"width": gl.viewportWidth, "height": gl.viewportHeight};
  3428. this.slideRect = {
  3429. "origin": this.slideOrigin,
  3430. "size": this.slideSize
  3431. };
  3432. this.frameRect = this.slideRect;
  3433. var effect = params.effect;
  3434. // set parent opacity from CA baseLayer
  3435. this.parentOpacity = effect.baseLayer.initialState.opacity;
  3436. // setup web drawable requirements
  3437. this.animationWillBeginWithContext();
  3438. },
  3439. animationWillBeginWithContext: function() {
  3440. var renderer = this.renderer;
  3441. // initialize a core animation wrapper based effect object for each texture rectangle
  3442. this.coreAnimationWrapperBasedEffects = [];
  3443. var program = this.program;
  3444. var slideRect = this.slideRect;
  3445. var duration = this.duration;
  3446. var direction = this.direction;
  3447. var buildType = this.type;
  3448. var parentOpacity = this.parentOpacity;
  3449. var parameterGroupName = this.parameterGroupName;
  3450. for (var i = 0, length = this.textures.length; i < length; i++) {
  3451. var texture = this.textures[i];
  3452. var direction = this.direction;
  3453. var tr = this.textures[i].textureRect;
  3454. var frameRect = this.frameRect;
  3455. var orthoOffset = {
  3456. "x": texture.offset.pointX - frameRect.origin.x,
  3457. "y": texture.offset.pointY + texture.height - (frameRect.origin.y + frameRect.size.height)
  3458. };
  3459. var baseOrthoTransform = WebGraphics.makeOrthoMatrix4(0, frameRect.size.width, 0, frameRect.size.height, -1, 1);
  3460. var baseTransform = WebGraphics.translateMatrix4(baseOrthoTransform, orthoOffset.x, -orthoOffset.y, 0);
  3461. var coreAnimationWrapperBasedEffect = new KNWebGLCoreAnimationWrapperBasedEffect(
  3462. renderer,
  3463. program,
  3464. slideRect,
  3465. texture,
  3466. frameRect,
  3467. baseTransform,
  3468. duration,
  3469. direction,
  3470. buildType,
  3471. parentOpacity
  3472. );
  3473. // push each effect into effect dictionary
  3474. this.coreAnimationWrapperBasedEffects.push(coreAnimationWrapperBasedEffect);
  3475. }
  3476. },
  3477. drawFrame: function(difference, elapsed, duration) {
  3478. var coreAnimationWrapperBasedEffects = this.coreAnimationWrapperBasedEffects;
  3479. for (var i = 0, length = coreAnimationWrapperBasedEffects.length; i < length; i++) {
  3480. coreAnimationWrapperBasedEffects[i].drawFrame(difference, elapsed, duration);
  3481. }
  3482. }
  3483. });
  3484. var KNWebGLTransitionContentAware = Class.create(KNWebGLProgram, {
  3485. initialize: function($super, renderer, params) {
  3486. // initialize default program data for core animation wrapper program
  3487. this.coreAnimationWrapperProgram = new KNWebGLCoreAnimationWrapperProgram(params);
  3488. this.params = params;
  3489. // create WebGL program using core animation wrapper program data
  3490. $super(renderer, this.coreAnimationWrapperProgram.data);
  3491. var gl = this.gl;
  3492. this.percentfinished = 0.0;
  3493. this.slideOrigin = {"x": 0, "y": 0};
  3494. this.slideSize = {"width": gl.viewportWidth, "height": gl.viewportHeight};
  3495. this.slideRect = {
  3496. "origin": this.slideOrigin,
  3497. "size": this.slideSize
  3498. };
  3499. // set frameRect to slideRect for content aware transition
  3500. this.frameRect = this.slideRect;
  3501. var effect = params.effect;
  3502. // set parent opacity from CA baseLayer
  3503. this.parentOpacity = effect.baseLayer.initialState.opacity;
  3504. // setup web drawable requirements
  3505. this.animationWillBeginWithContext();
  3506. },
  3507. animationWillBeginWithContext: function() {
  3508. var renderer = this.renderer;
  3509. // effect array object to include both text effects and CA wrapper based objects
  3510. this.contentAwareEffects = [];
  3511. var program = this.program;
  3512. var slideRect = this.slideRect;
  3513. var duration = this.duration;
  3514. var direction = this.direction;
  3515. var buildType = this.type;
  3516. var parentOpacity = this.parentOpacity;
  3517. var parameterGroupName = this.parameterGroupName;
  3518. for (var i = 0, length = this.textures.length; i < length; i++) {
  3519. var texture = this.textures[i];
  3520. var direction = this.direction;
  3521. var tr = this.textures[i].textureRect;
  3522. var frameRect = this.frameRect;
  3523. var orthoOffset = {
  3524. "x": texture.offset.pointX - frameRect.origin.x,
  3525. "y": texture.offset.pointY + texture.height - (frameRect.origin.y + frameRect.size.height)
  3526. };
  3527. var baseOrthoTransform = WebGraphics.makeOrthoMatrix4(0, frameRect.size.width, 0, frameRect.size.height, -1, 1);
  3528. var baseTransform = WebGraphics.translateMatrix4(baseOrthoTransform, orthoOffset.x, -orthoOffset.y, 0);
  3529. // make sure the effect only work on text type or shape object
  3530. var texturedRectangle = texture.texturedRectangle;
  3531. var textureType = texturedRectangle.textureType;
  3532. var isShapeObject = (textureType === TSDTextureType.Object && texturedRectangle.shapePath) ? true : false;
  3533. if (textureType === TSDTextureType.Text || isShapeObject) {
  3534. var params = this.params;
  3535. var effect = params.effect;
  3536. // set this texture for text effect
  3537. params.textures = [texture];
  3538. // use hidden animations to find out the correct build type
  3539. var groupAnimations = texture.animations;
  3540. var program;
  3541. if (groupAnimations && groupAnimations.length > 0) {
  3542. var animations = groupAnimations[0].animations;
  3543. for (var j = 0, animationLength = animations.length; j < animationLength; j++) {
  3544. var animation = animations[j];
  3545. if (animation.property === "hidden") {
  3546. effect.type = animation.to.scalar ? "buildOut" : "buildIn";
  3547. break;
  3548. }
  3549. }
  3550. }
  3551. switch (effect.name) {
  3552. case "apple:ca-text-shimmer":
  3553. program = new KNWebGLBuildShimmer(renderer, params);
  3554. break;
  3555. case "apple:ca-text-sparkle":
  3556. program = new KNWebGLBuildSparkle(renderer, params);
  3557. break;
  3558. default:
  3559. program = new KNWebGLDissolve(renderer, params);
  3560. break;
  3561. }
  3562. // push each text effect into effect dictionary
  3563. this.contentAwareEffects.push(program);
  3564. } else {
  3565. var coreAnimationWrapperBasedEffect = new KNWebGLCoreAnimationWrapperBasedEffect(
  3566. renderer,
  3567. program,
  3568. slideRect,
  3569. texture,
  3570. frameRect,
  3571. baseTransform,
  3572. duration,
  3573. direction,
  3574. buildType,
  3575. parentOpacity
  3576. );
  3577. // push each CA effect into effect dictionary
  3578. this.contentAwareEffects.push(coreAnimationWrapperBasedEffect);
  3579. }
  3580. }
  3581. },
  3582. drawFrame: function(difference, elapsed, duration) {
  3583. var contentAwareEffects = this.contentAwareEffects;
  3584. for (var i = 0, length = contentAwareEffects.length; i < length; i++) {
  3585. var contentAwareEffect = contentAwareEffects[i];
  3586. contentAwareEffect.drawFrame(difference, elapsed, duration);
  3587. }
  3588. }
  3589. });
  3590. var KNWebGLTransitionShimmer = Class.create(KNWebGLTransitionContentAware, {
  3591. initialize: function($super, renderer, params) {
  3592. // Set up Shimmer as content aware transition
  3593. $super(renderer, params);
  3594. }
  3595. });
  3596. var KNWebGLTransitionSparkle = Class.create(KNWebGLTransitionContentAware, {
  3597. initialize: function($super, renderer, params) {
  3598. // Set up Sparkle as content aware transition
  3599. $super(renderer, params);
  3600. }
  3601. });