babylon.videoTexture.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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[], scene: Scene, generateMipMaps = false, invertY = false, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE) {
  7. super(null, scene, !generateMipMaps, invertY);
  8. this.name = name;
  9. this.video = document.createElement("video");
  10. this.video.autoplay = false;
  11. this.video.loop = true;
  12. this.video.addEventListener("canplaythrough", () => {
  13. if (Tools.IsExponantOfTwo(this.video.videoWidth) && Tools.IsExponantOfTwo(this.video.videoHeight)) {
  14. this.wrapU = BABYLON.Texture.WRAP_ADDRESSMODE;
  15. this.wrapV = BABYLON.Texture.WRAP_ADDRESSMODE;
  16. } else {
  17. this.wrapU = BABYLON.Texture.CLAMP_ADDRESSMODE;
  18. this.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE;
  19. generateMipMaps = false;
  20. }
  21. this._texture = scene.getEngine().createDynamicTexture(this.video.videoWidth, this.video.videoHeight, generateMipMaps, samplingMode, false);
  22. this._texture.isReady = true;
  23. });
  24. urls.forEach(url => {
  25. //Backwards-compatibility for typescript 1. from 1.3 it should say "SOURCE". see here - https://github.com/Microsoft/TypeScript/issues/1850
  26. var source = <HTMLSourceElement> 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 || this.video.readyState !== this.video.HAVE_ENOUGH_DATA) {
  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. }