Browse Source

make sure page renders
even when babylon could not load

Raanan Weber 5 years ago
parent
commit
c7b393bef7
2 changed files with 34 additions and 5 deletions
  1. 26 1
      Playground/js/index.js
  2. 8 4
      Playground/js/main.js

+ 26 - 1
Playground/js/index.js

@@ -20,4 +20,29 @@
         this.main.initialize();
     }
 }
-index = new Index();
+index = new Index();
+
+// defeinsive, in case there was an error loading babylon.js
+// This is done so that search bots will still be able to render the page, even when babylon had a problem while downloading
+if (!window.BABYLON) {
+    window.BABYLON = {
+        Vector3: function () {},
+        Vector2: function () {},
+        Mesh: function () {},
+        Matrix: function () {},
+        GLTF2: {
+            GLTFLoader: {
+                RegisterExtension: function () {}
+            }
+        },
+        SceneLoader: {
+            OnPluginActivatedObservable: {
+                add: function () {}
+            }
+        }
+    }
+    BABYLON.Vector3.Up = function () {};
+    BABYLON.Vector3.Zero = function () {};
+    BABYLON.Vector2.Zero = function () {};
+    BABYLON.Matrix.Zero = function () {};
+}

+ 8 - 4
Playground/js/main.js

@@ -33,7 +33,7 @@ compileAndRun = function (parent, fpsLabel) {
         parent.menuPG.hideWaitDiv();
         globalParent = parent;
 
-        if (!BABYLON.Engine.isSupported()) {
+        if (typeof BABYLON === 'undefined' || !BABYLON.Engine || !BABYLON.Engine.isSupported()) {
             parent.utils.showError("Your browser does not support WebGL. Please, try to update it, or install a compatible one.", null);
             return;
         }
@@ -44,7 +44,7 @@ compileAndRun = function (parent, fpsLabel) {
             readOnly: false
         });
 
-        if (BABYLON.Engine.LastCreatedScene && BABYLON.Engine.LastCreatedScene.debugLayer && BABYLON.Engine.LastCreatedScene.debugLayer.isVisible()) {
+        if (typeof BABYLON !== 'undefined' && BABYLON.Engine && BABYLON.Engine.LastCreatedScene && BABYLON.Engine.LastCreatedScene.debugLayer && BABYLON.Engine.LastCreatedScene.debugLayer.isVisible()) {
             showInspector = true;
         }
 
@@ -220,7 +220,9 @@ class Main {
     constructor(parent) {
         this.parent = parent;
 
-        BABYLON.Engine.ShadersRepository = "/src/Shaders/";
+        if (typeof BABYLON !== 'undefined') {
+            BABYLON.Engine.ShadersRepository = "/src/Shaders/";
+        }
         this.snippetV3Url = "https://snippet.babylonjs.com"
         this.currentSnippetToken;
         this.currentSnippetTitle = null;
@@ -283,7 +285,9 @@ class Main {
         var handleGetZip = () => this.parent.zipTool.getZip(engine);
 
         // Display BJS version
-        if (BABYLON) this.parent.utils.setToMultipleID("mainTitle", "innerHTML", "v" + BABYLON.Engine.Version);
+        if (typeof BABYLON !== 'undefined' && BABYLON.Engine) {
+            this.parent.utils.setToMultipleID("mainTitle", "innerHTML", "v" + BABYLON.Engine.Version);
+        }
         // Run
         this.parent.utils.setToMultipleID("runButton", "click", handleRun);
         // New