babylon.videoTexture.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. module BABYLON {
  2. export class VideoTexture extends Texture {
  3. public video: HTMLVideoElement;
  4. private _autoLaunch = true;
  5. private _lastUpdate: number;
  6. constructor(name: string, urls: string[], size: any, scene: Scene, generateMipMaps: boolean, invertY: boolean, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE) {
  7. super(null, scene, !generateMipMaps, invertY);
  8. this.name = name;
  9. this.wrapU = BABYLON.Texture.WRAP_ADDRESSMODE;
  10. this.wrapV = BABYLON.Texture.WRAP_ADDRESSMODE;
  11. var requiredWidth = size.width || size;
  12. var requiredHeight = size.height || size;
  13. this._texture = scene.getEngine().createDynamicTexture(requiredWidth, requiredHeight, generateMipMaps, samplingMode);
  14. var textureSize = this.getSize();
  15. this.video = document.createElement("video");
  16. this.video.width = textureSize.width;
  17. this.video.height = textureSize.height;
  18. this.video.autoplay = false;
  19. this.video.loop = true;
  20. this.video.addEventListener("canplaythrough", () => {
  21. if (this._texture) {
  22. this._texture.isReady = true;
  23. }
  24. });
  25. urls.forEach(url => {
  26. var source = document.createElement("source");
  27. source.src = url;
  28. this.video.appendChild(source);
  29. });
  30. this._lastUpdate = Tools.Now;
  31. }
  32. public update(): boolean {
  33. if (this._autoLaunch) {
  34. this._autoLaunch = false;
  35. this.video.play();
  36. }
  37. var now = Tools.Now;
  38. if (now - this._lastUpdate < 15) {
  39. return false;
  40. }
  41. this._lastUpdate = now;
  42. this.getScene().getEngine().updateVideoTexture(this._texture, this.video, this._invertY);
  43. return true;
  44. }
  45. }
  46. }