Jump to content

SceneLoader.Load should be depreciated


JCPalmer
 Share

Recommended Posts

I am kind of tired of people using the inferior SceneLoader.Load() over SceneLoader.Append() and hearing about all their avoidable problems.  Every time I mention Append(), I get at most a "well, I'll think about trying it". Append() came about because @Vousk-prod. wanted to have multiple .babylon files, and was doing all these back flips.  I looked at the source, and saw all I had to do is change the name of the Load() function to Append, switch the engine arg for a scene arg, then make a 1 line wrapper for Load.

public static Load(rootUrl: string, sceneFilename: any, engine: Engine, onsuccess?: (scene: Scene) => void, progressCallBack?: any, onerror?: (scene: Scene) => void): void {
    SceneLoader.Append(rootUrl, sceneFilename, new Scene(engine), onsuccess, progressCallBack, onerror);
}

/**
* Append a scene
* @param rootUrl a string that defines the root url for scene and resources
* @param sceneFilename a string that defines the name of the scene file. can start with "data:" following by the stringified version of the scene
* @param scene is the instance of BABYLON.Scene to append to
*/
public static Append(rootUrl: string, sceneFilename: any, scene: Scene, onsuccess?: (scene: Scene) => void, progressCallBack?: any, onerror?: (scene: Scene) => void): void {
    ...
}

So when you call Load(),  you are Appending.  Creating a scene outside of the Load call means you do not need to add your render loop in the callback.  All the scope problems for your scene variable are also gone.  Having more than one .babylon is just 2 simple Appends.

Callbacks are also stupid, and do not scale.  As soon as you have more than 1 .babylon, they become a nightmare.  Just do all your Append()'s & ImportMesh()'s, then have all your post load code below inside a blocking executeWhenReady function.

var canvas = document.getElementById("renderCanvas");
var engine = new BABYLON.Engine(canvas, true);
        
var scene = new BABYLON.Scene(engine);
BABYLON.SceneLoader.Append("./", "scene.babylon", scene);
BABYLON.SceneLoader.ImportMesh(["moe","larry","curly"], "./", "meshes.babylon", scene);
scene.executeWhenReady(function () {
    // Attach camera to canvas inputs
    scene.activeCamera.attachControl(canvas);

    // Once the scene is loaded, register a render loop
    engine.runRenderLoop(function() {
       scene.render();
    });
    // any post load code grabbing all stuff using scene lookups
    var skeleton = scene.getSkeletonByName("myskelname");
    var material = scene.getMaterialByName("mymaterial");
    var camera   = scene.getCameraByName("polaroid");
    var bone     = scene.getBoneByName("funny");
    var light    = scene.getLightByName("spot");
    var node     = scene.getNodeByName("node");
    var mesh     = scene.getMeshByName("cube");
    var sound    = scene.getSoundByName("talking");
    var flare    = scene.getLensFlareSystemByName("flare");
});

Problems with callbacks for ImportMesh that reference the mesh as mesh[0] are also fixed.  Usually, the problem is either [0] is the wrong mesh or there wasn't anything actually loaded.  Specifying by name in a executeWhenReady should either get the right mesh, or drive home the problem that nothing was done quicker.  Might be a good idea to add optional logging capability to Append (and therefore Load) and ImportMesh to list what actually got done.

Load must die!

Link to comment
Share on other sites

I agree with that. I find this more convenient than Append Load.

Perhaps a name change as:
- NewScene() (Load() current)
- AddScene() (Append() current)

would be good

or replace Append() on the Load() function.

Link to comment
Share on other sites

Adding a depreciated message to the browser console was what I wanted.  This is consistent with other discouraged features.  I just threw that "load must die" crack in at the end as sort of a call to action.  I thought DK was referring to name changes dad72 added.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...