瀏覽代碼

Add erroreventlistener and code branch, that does (re)load a texture when the caching returns a corrupt one

Dennis Dervisis 6 年之前
父節點
當前提交
57b75eaf04

+ 5 - 3
dist/preview release/what's new.md

@@ -8,7 +8,7 @@
 - Added [support for AmmoJS](https://doc.babylonjs.com/how_to/using_the_physics_engine) as a physics plugin (Composite objects, motors, joints) ([TrevorDev](https://github.com/TrevorDev))
   - Added support for soft bodies, which are 3D softbody, 2D cloth and 1D rope, in Ammo physics plugin. [Doc](https://doc.babylonjs.com/how_to/soft_bodies) ([JohnK](https://github.com/BabylonJSGuide))
   - Added support for [Convex Hull Impostor][https://github.com/kripken/ammo.js/blob/master/bullet/src/BulletCollision/CollisionShapes/btConvexHullShape.h] using Ammo.js plugin ([MackeyK24](https://github.com/mackeyk24))
-  - Added AmmoJSPlugin scene file loader [MackeyK24](https://github.com/mackeyk24))  
+  - Added AmmoJSPlugin scene file loader [MackeyK24](https://github.com/mackeyk24))
 - Added support for [WebXR](https://doc.babylonjs.com/how_to/webxr) ([TrevorDev](https://github.com/TrevorDev))
   - Add customAnimationFrameRequester to allow sessions to hook into engine's render loop ([TrevorDev](https://github.com/TrevorDev))
   - camera customDefaultRenderTarget to allow cameras to render to a custom render target (eg. xr framebuffer) instead of the canvas ([TrevorDev](https://github.com/TrevorDev))
@@ -131,7 +131,7 @@
 - Observables can now make observers top or bottom priority ([TrevorDev](https://github.com/TrevorDev))
 - Mesh outline no longer is shown through the mesh when it's transparent ([TrevorDev](https://github.com/TrevorDev))
 - DeviceOrientationCamera will no longer be modified by mouse input if the orientation sensor is active ([TrevorDev](https://github.com/TrevorDev))
-- Added LoadScriptAsync tools helper function [MackeyK24](https://github.com/mackeyk24))  
+- Added LoadScriptAsync tools helper function [MackeyK24](https://github.com/mackeyk24))
 - Added customShaderNameResolve to PBRMaterialBase to allow subclasses to specify custom shader information [MackeyK24](https://github.com/mackeyk24))
 - Added PBRCustomMaterial to material library to allow easy subclassing of PBR materials [MackeyK24](https://github.com/mackeyk24))
 - Added `auto-exposure` support in `StandardRenderingPipeline` when `HDR` is enabled ([julien-moreau](https://github.com/julien-moreau))
@@ -151,7 +151,7 @@
   - Skinned meshes now set an override mesh instead of reparenting to the `__root__` transform node
   - Loaded bones are linked with the transform node created for the corresponding glTF node
 - Add `EquiRectangularCubeTexture` class to enable the usage of browser-canvas supported images as `CubeTexture`'s ([Dennis Dervisis](https://github.com/ddervisis))
-- Add `EquiRectangularCubeTextureAssetTask` to be able to load `EquiRectangularCubeTextures`s via Asset Manager ([Dennis Dervisis](https://github.com/ddervisis))
+- Add `EquiRectangularCubeTextureAssetTask` to be able to load `EquiRectangularCubeTexture`s via Asset Manager ([Dennis Dervisis](https://github.com/ddervisis))
 
 ### glTF Serializer
 
@@ -218,6 +218,8 @@
 - Fix `mesh.visibility` not working properly when certain material properties are set that changes the interpretation of alpha (e.g. refraction, specular over alpha, etc.) ([bghgary](https://github.com/bghgary))
 - Fix material and texture leak when loading/removing GLTF/obj/babylon files with AssetContainer ([TrevorDev](https://github.com/TrevorDev))
 - Avoid exception when removing impostor during cannon world step ([TrevorDev](https://github.com/TrevorDev))
+- Fix code branch, that does not try to (re)load a `EquiRectangularCubeTexture`/`HDRCubeTexture` when the caching returns an empty or corrupt `InternalTexture` ([Dennis Dervisis](https://github.com/ddervisis))
+- Add error eventlistener (bubbling up the onError callback chain) in case an `EquiRectangularCubeTexture` can not be loaded, because of a wrong path or IO problems ([Dennis Dervisis](https://github.com/ddervisis))
 
 ### Core Engine
 - Fixed a bug with `mesh.alwaysSelectAsActiveMesh` preventing layerMask to be taken in account ([Deltakosh](https://github.com/deltakosh))

+ 12 - 2
src/Materials/Textures/equiRectangularCubeTexture.ts

@@ -4,6 +4,7 @@ import { BaseTexture } from './baseTexture';
 import { Texture } from './texture';
 import { Scene } from "../../scene";
 import { Nullable } from "../../types";
+import { Tools } from 'Misc/tools';
 
 /**
  * This represents a texture coming from an equirectangular image supported by the web browser canvas.
@@ -75,17 +76,23 @@ export class EquiRectangularCubeTexture extends BaseTexture {
 
         if (!this._texture) {
             if (!scene.useDelayedTextureLoading) {
-                this.loadImage(this.loadTexture.bind(this));
+                this.loadImage(this.loadTexture.bind(this), this._onError);
             } else {
                 this.delayLoadState = Engine.DELAYLOADSTATE_NOTLOADED;
             }
+        } else if (onLoad) {
+            if (this._texture.isReady) {
+                Tools.SetImmediate(() => onLoad());
+            } else {
+                this._texture.onLoadedObservable.add(onLoad);
+            }
         }
     }
 
     /**
      * Load the image data, by putting the image on a canvas and extracting its buffer.
      */
-    private loadImage(loadTextureCallback: () => void): void {
+    private loadImage(loadTextureCallback: () => void, onError: (message?: string, exception?: any) => void): void {
         const canvas = document.createElement('canvas');
         const image = new Image();
 
@@ -104,6 +111,9 @@ export class EquiRectangularCubeTexture extends BaseTexture {
             canvas.remove();
             loadTextureCallback();
         });
+        image.addEventListener('error', (error) => {
+            onError(`${this.getClassName()} could not be loaded`, error);
+        });
         image.src = this.url;
     }
 

+ 7 - 0
src/Materials/Textures/hdrCubeTexture.ts

@@ -10,6 +10,7 @@ import { _DepthCullingState, _StencilState, _AlphaState } from "../../States/ind
 import { HDRTools } from "../../Misc/HighDynamicRange/hdr";
 import { CubeMapToSphericalPolynomialTools } from "../../Misc/HighDynamicRange/cubemapToSphericalPolynomial";
 import { _TypeStore } from '../../Misc/typeStore';
+import { Tools } from 'Misc/tools';
 
 /**
  * This represents a texture coming from an HDR input.
@@ -140,6 +141,12 @@ export class HDRCubeTexture extends BaseTexture {
             } else {
                 this.delayLoadState = Engine.DELAYLOADSTATE_NOTLOADED;
             }
+        } else if (onLoad) {
+            if (this._texture.isReady) {
+                Tools.SetImmediate(() => onLoad());
+            } else {
+                this._texture.onLoadedObservable.add(onLoad);
+            }
         }
     }