Jump to content

Loading Asynchronous Objects into array for later use?


Aerion
 Share

Recommended Posts

Am I correct in thinking that the loading or use of meshes will be user controlled? If this is true I have only just realised this as this is not clear in the posts. If so this changes how you look at the problem. Previously I could not understand how you could mesh a file asynchronously and then use it in the main program without knowing it had been loaded by using a onSuccess or a callback. If it is user controlled then at any time the user can see what is in the library and make use of them when they appear in the library.

Also if the intention was always for the user to control when the intended function would be applied to the mesh then it would have been much clearer to state this in your first post. The more you explain the intended context and use the easier it is to help.

If the intention is

Load meshes asynchronously into a library of meshes.

User views library.

User can apply functions to meshes that appear in library.

Then perhaps my earlier attempt at a modelling user interface may help you.

Have a look at http://cubees.github.io/

Source at https://github.com/Cubees/Cubees.github.io

Link to comment
Share on other sites

DING DING DING @JohnK ! You hit the NAIL on the head! YOU TOO win a box of chocolates! <3

The user uses the library, and are able to apply functions using a SIMPLE array variable to call WHICHEVER mesh they so desire. Such as mesh [ 0 ] OR mesh [ 1 ] OR both if they use a for loop.

They are then able to control the object array that is being returned  with ANY other function that they'd like as long as the meshes load correctly & are stored in the mesh "library" correctly. They can do like :: 

var LIBRARY = [ ]; LIBRARY [ 'water' ] = LoadEntity ( 'meshes/water.obj', OTHER_INIT_VARIABLES_HERE . . . ); LIBRARY [ 'grassplane' ] = LoadEntity ( 'meshes/grass.obj', OTHER_INIT_VARIABLES_HERE . . . ); THEN in the LOOP :: for ( var i = 0; i <= LIBRARY.length; i++ ) { /* Load the WATER plane with index 0 */ if ( i === 0 ) { UpdateEntity ( scene, i, xpos, ypos, zpos, OTHER_UPDATE_VARIABLES_HERE . . . ); } /*Load the GRASS plane with index 1*/ if ( i === 1 ) { UpdateEntity ( scene, i, xpos, ypos, zpos, OTHER_UPDATE_VARIABLES_HERE . . . ); }

Thank You folks! <3

 

Link to comment
Share on other sites

First, you are the only one I have seen, saying that a solved tag got added to your topic.  Maybe change your password, or log out if you share your machine.  Seems unlikely, but is a valid explanation why you are the only one. If it still happens, then you can be sure it is something you are doing.

For having an array of all your meshes, so that you can reference one by index when changing a property, you could just use scene.meshes.  It is an array.

  1. Import Mesh
  2. assign var skullIdx from looping thru scene.meshes till you match
  3. scene.meshes[skullIdx].position.y = 3;

What I think what you are really trying to do given that 'Asynchronous' is in the title of the topic, is do all net retrievals (geometry & textures) in advance, but not actually put up a mesh until needed.  Right?  If not, ignore the rest.

ImportMesh has the side effect of actually creating something.  I have an entire work flow, originating in Blender, which does this.  How geometry gets to your machine is the fact that it is exported in a javascript file.  You can even dynamically Download a .JS file without a script tag in your html.  Appending it to the document does nothing by itself.  Each exported .JS file also has a matReadAhead(), which gets all of the data for the textures in advance as well without tying up the UI thread.

When you actually want something, say var mesh = new module.MyMesh("name", scene);

Here is a scene where none of the .JS files for any of the busts are statically linked.  You of course need to wait for the first one to transmit, but the others are already local by the time to clicked the "Next Model" button.

Link to comment
Share on other sites

That's correct. I'm basically just trying to be able to do var mesh = [ ]; "mesh [ 0 ] = new module.MyMesh ( 'model1', scene ); mesh [ 1 ] = new module.MyMesh ( 'model2', scene ); ", in which I can use "mesh [ 0 ]", "mesh [ 1 ]", etc... in other functions.

Link to comment
Share on other sites

Weekends are always slow. Perhaps people think you have been given sufficient information for you to have another go. 

If looking at the library is user controlled then the user needs to click something to display current library contents. 

So async load library.

Main code button for user to click

Onclick display current library contents.

You seem to have enough ability to try this.

Link to comment
Share on other sites

24 minutes ago, Mythros said:

I just want to be able to load a model and use that loaded model's variable in other functions. that's it.

In which case just call these functions inside the onload function.

Link to comment
Share on other sites

The PG in your original post https://www.babylonjs-playground.com/#1QJUDF#3 line 44

function ( newMeshes )

this is the onload (or onsuccess) function that is called when

BABYLON.SceneLoader.ImportMesh

has loaded all the meshes from the given file.

newMeshes is defacto the library of meshes so

function(newMeshes) {
  for(var i = 0; i < newMeshes.length; i++) {
     //apply my functions to newMeshes(i)
  }
}

 

Link to comment
Share on other sites

i did try it the way you suggested. I JUST want the variable myMesh [ ] to hold the information of the meshes for use in other functions. how hard is that to understand?

I showed you EXACTLY what I need the code to accomplish inside of the Playground.

First, at the top of the file, define "myMesh" as a global object :: 

	var myMesh = [ ];
	

Then, in the createScene ( ) function :: 

	myMesh [ 0 ] = LoadEntity ( entityname1, entitylocation1, entityfile1 );
	myMesh [ 1 ] = LoadEntity ( entityname2, entitylocation2, entityfile2 );
	

Then in the UPDATE function OR OTHER functions, I want to be able to still have access to that CERTAIN mesh or those CERTAIN meshes ( by using a for loop as opposed to an assigned index ) :: 

	myMesh [ 0 ].rotation.y -= 0.01;
	myMesh [ 1 ].rotation.x += 0.01;
	

OR

	for ( var i = 0; i <= myMesh.length; i++ )

        {

            if ( i === 0 )

            {

                myMesh [ i ].rotation.x -= 0.01;

                myMesh [ i ].rotation.y += 0.01;

            }

            if ( i === 1 )

            {

                myMesh [ i ].rotation.y -= 0.01;

                myMesh [ i ].rotation.z += 0.01;

            }

        }

--------------

Thank You! <3

 

 

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...