babylon.videoTexture.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.VideoTexture = function (name, urls, size, scene, generateMipMaps) {
  5. this._scene = scene;
  6. this._scene.textures.push(this);
  7. this.name = name;
  8. this.wrapU = BABYLON.Texture.WRAP_ADDRESSMODE;
  9. this.wrapV = BABYLON.Texture.WRAP_ADDRESSMODE;
  10. this._texture = scene.getEngine().createDynamicTexture(size, size, generateMipMaps);
  11. var textureSize = this.getSize();
  12. this.video = document.createElement("video");
  13. this.video.width = textureSize.width;
  14. this.video.height = textureSize.height;
  15. this.video.autoplay = false;
  16. this.video.loop = true;
  17. this.video.preload = true;
  18. this._autoLaunch = true;
  19. var that = this;
  20. this.video.addEventListener("canplaythrough", function () {
  21. if (that._texture) {
  22. that._texture.isReady = true;
  23. }
  24. });
  25. urls.forEach(function (url) {
  26. var source = document.createElement("source");
  27. source.src = url;
  28. that.video.appendChild(source);
  29. });
  30. this._lastUpdate = new Date();
  31. };
  32. BABYLON.VideoTexture.prototype = Object.create(BABYLON.Texture.prototype);
  33. BABYLON.VideoTexture.prototype._update = function () {
  34. if (this._autoLaunch) {
  35. this._autoLaunch = false;
  36. this.video.play();
  37. }
  38. var now = new Date();
  39. if (now - this._lastUpdate < 15) {
  40. return false;
  41. }
  42. this._lastUpdate = now;
  43. this._scene.getEngine().updateVideoTexture(this._texture, this.video);
  44. return true;
  45. };
  46. })();