babylon.cubeTexture.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. module BABYLON {
  2. export class CubeTexture extends BaseTexture {
  3. public url: string;
  4. public coordinatesMode = Texture.CUBIC_MODE;
  5. private _noMipmap: boolean;
  6. private _files: string[];
  7. private _extensions: string[];
  8. private _textureMatrix: Matrix;
  9. private _format: number;
  10. public static CreateFromImages(files: string[], scene: Scene, noMipmap?: boolean) {
  11. return new CubeTexture("", scene, null, noMipmap, files);
  12. }
  13. constructor(rootUrl: string, scene: Scene, extensions?: string[], noMipmap?: boolean, files?: string[], onLoad: () => void = null, onError: () => void = null, format: number = Engine.TEXTUREFORMAT_RGBA) {
  14. super(scene);
  15. this.name = rootUrl;
  16. this.url = rootUrl;
  17. this._noMipmap = noMipmap;
  18. this.hasAlpha = false;
  19. this._format = format;
  20. if (!rootUrl && !files) {
  21. return;
  22. }
  23. this._texture = this._getFromCache(rootUrl, noMipmap);
  24. if (!files) {
  25. if (!extensions) {
  26. extensions = ["_px.jpg", "_py.jpg", "_pz.jpg", "_nx.jpg", "_ny.jpg", "_nz.jpg"];
  27. }
  28. files = [];
  29. for (var index = 0; index < extensions.length; index++) {
  30. files.push(rootUrl + extensions[index]);
  31. }
  32. this._extensions = extensions;
  33. }
  34. this._files = files;
  35. if (!this._texture) {
  36. if (!scene.useDelayedTextureLoading) {
  37. this._texture = scene.getEngine().createCubeTexture(rootUrl, scene, files, noMipmap, onLoad, onError, this._format);
  38. } else {
  39. this.delayLoadState = Engine.DELAYLOADSTATE_NOTLOADED;
  40. }
  41. } else if (onLoad) {
  42. if (this._texture.isReady) {
  43. Tools.SetImmediate(() => onLoad());
  44. } else {
  45. this._texture.onLoadedCallbacks.push(onLoad);
  46. }
  47. }
  48. this.isCube = true;
  49. this._textureMatrix = Matrix.Identity();
  50. }
  51. // Methods
  52. public delayLoad(): void {
  53. if (this.delayLoadState !== Engine.DELAYLOADSTATE_NOTLOADED) {
  54. return;
  55. }
  56. this.delayLoadState = Engine.DELAYLOADSTATE_LOADED;
  57. this._texture = this._getFromCache(this.url, this._noMipmap);
  58. if (!this._texture) {
  59. this._texture = this.getScene().getEngine().createCubeTexture(this.url, this.getScene(), this._files, this._noMipmap, this._format);
  60. }
  61. }
  62. public getReflectionTextureMatrix(): Matrix {
  63. return this._textureMatrix;
  64. }
  65. public static Parse(parsedTexture: any, scene: Scene, rootUrl: string): CubeTexture {
  66. var texture = SerializationHelper.Parse(() => {
  67. return new BABYLON.CubeTexture(rootUrl + parsedTexture.name, scene, parsedTexture.extensions);
  68. }, parsedTexture, scene);
  69. // Animations
  70. if (parsedTexture.animations) {
  71. for (var animationIndex = 0; animationIndex < parsedTexture.animations.length; animationIndex++) {
  72. var parsedAnimation = parsedTexture.animations[animationIndex];
  73. texture.animations.push(Animation.Parse(parsedAnimation));
  74. }
  75. }
  76. return texture;
  77. }
  78. public clone(): CubeTexture {
  79. return SerializationHelper.Clone(() => {
  80. return new CubeTexture(this.url, this.getScene(), this._extensions, this._noMipmap, this._files);
  81. }, this);
  82. }
  83. }
  84. }