Thark

Extending Mesh Class & MeshAssetTask Questions

Recommended Posts

I'm stuck with loading my meshes. I'm new to all the 3D and modelling so any thoughts are highly appreciated.

Basically I want to extends Babylons default Mesh class called BaseMesh and add a 'load' function to it to get rid of clutter in my scene code. I'm extending this BaseMesh class for each model I have.

My BaseMesh class: (Note that all code examples are written in TypeScript)

// BaseMesh.ts
class BaseMesh /* extends BABLYON.Mesh */ {
    public readonly BASE_URL: string; // I want these to be static
    public readonly MODEL_URL: string; // I want these to be static
    public readonly NAME: string; // I want these to be static

    public body;

/*	constructor( scene ) {
		// I don't know what to do here
		super(
			this.NAME //name
			scene // scene
			null // parent
			? // source
		);
	}
*/
	public load( assetsManager: BABYLON.AssetsManager ) {
		return assetsManager.addMeshTask(
			this.NAME + ' task', // name
			"",
			this.BASE_URL,
			this.MODEL_URL
		);
	}

	public onLoaded( results ) {
		// I don't know what to do here
		this.body = results.loadedMeshes[0];
	}

    public update(): void {}
};

A model class would looks like this:

// Robot.ts
class Robot extends BaseMesh {
    public readonly BASE_URL: string = '/models/';
    public readonly MODEL_URL: string = 'robot.babylon';
    public readonly NAME: string = 'robotMesh';

    public update(): void {
        this.body.rotation.y += 0.03;
    }
}

In my code above I store the loadedMesh from the AssetsManager into this.body. But here is question 1: why is my model already showing on the scene when the meshtask has run? I'm only loading a mesh, I've not put anything about putting it on the scene.

Question 2: How do I extend my BaseMesh class from BABLYON.Mesh so that the result (loadedMeshes) of my load function is "the mesh itself" (instead of an attribute this.body). For example this would mean I could change my update function to 'this.rotation.y += 0.03;' and just generally makes more sense.
 

Question 3: I'm really confused about the relationship between my "code" and my "model/.babylon files". Is there any good documentation/tutorials about this? 
These questions range from:
- when is it healthy to split different parts of a model in different files
- do I apply textures in my code or do I do that in my .babylon file
- do I apply animations in my blender file or do I code them
- ...

This was a pain to type, if you have any questions please do ask :)

Thank you in advance!

Share this post


Link to post
Share on other sites

This doesn't answer your questions, but is the Robot really a Mesh?  I'd make the mesh a member variable in the robot class and then extend the robot from something like a GameObject or GameEntity class. 

Share this post


Link to post
Share on other sites
55 minutes ago, adam said:

This doesn't answer your questions, but is the Robot really a Mesh?  I'd make the mesh a member variable in the robot class and then extend the robot from something like a GameObject or GameEntity class. 

I see what you're going with but "Robot" is just an example here. Calling it RobotMesh would've maybe been better to make my point. 

Share this post


Link to post
Share on other sites
1 hour ago, adam said:

This thread might be helpful:

 

Thanks for the reply @adam! That does kind of answers question 1. I could use isVisible = false to hide the mesh when loaded.

	public onLoaded( results ) {
		this.body = results.loadedMeshes[0];
		this.body.isVisible = false;
	}

But that's still very strange and inconsistent behaviour.
I'm expecting my vertices, shapes, ... and all that to be returned and not an actual instance of a mesh. It doesn't do that for any other tasks (like loading a sprite).

Does this also not affect performance? If I load in a bunch of meshes this way, but 90% of them are 'invisible' would I get the same performance as if I only load the 10% that are visible?

Questions 2 and 3 are also still bugging me out.

Share this post


Link to post
Share on other sites
1 hour ago, Thark said:

Does this also not affect performance? If I load in a bunch of meshes this way, but 90% of them are 'invisible' would I get the same performance as if I only load the 10% that are visible?

Use mesh.setEnabled(false) will keep a mesh available but ignored by engine so no performance issues. Replace false by true to use again.

Share this post


Link to post
Share on other sites
16 hours ago, JohnK said:

Use mesh.setEnabled(false) will keep a mesh available but ignored by engine so no performance issues. Replace false by true to use again.

Thank you! That completely solves question 1! :D

Share this post


Link to post
Share on other sites

Hi, 

I am glad you have question 1 resolved :-) Let me try answering 2 and 3 (and then, maybe, change 1 as well).

2) This would require a bit of object casting or a bit of code rewrite. The Babylon loader load BABYLON.Mesh objects. You cannot define what class will be returned, it is set in the code. We can discuss (gladly) whether or not generics fit here, but for now we accept it here as a fact - using the default loader won't get you to where you want to be. So, what can be done?

  • The first thing you can do is use the loader (not inside your class, but externally) and cast the object return to your own class. This will require a bit of typescript patience, but since typescript is only type-safe at compile time, it will work for sure. 
  • The second thing you can do is extend the default babylon loaded and force it to return your own class instead of BABYLON.Mesh. 
  • The third (and IMHO the most recommended) - make BABYLON.Mesh a member of your Object-Class. Leave Babylon "alone", and use babylon's classes internally in your application. I think @adam's question is rather legitimate - is "Robot" really a mesh? or is it an object with functionality, that has a mesh (or 2 or N) attached to it?

3) A .babylon file is a collection of serialized Babylon nodes (lights, cameras, meshes) and a few other properties that can be set at the scene level. A .babylon file is usually used to define an entire scene. However, you can use it as a mesh-serializing object. Meaning - a .babylon file is actually a serialized mesh that can be loaded once again using the Babylon loader. A mesh can also be exported back to a .babylon file.

The relationship is actually set using the babylon loader. So if you extend it or change it (don't forget to load it correctly), you can change the relationship between the two.

Share this post


Link to post
Share on other sites
21 hours ago, RaananW said:

3) A .babylon file is a collection of serialized Babylon nodes (lights, cameras, meshes) and a few other properties that can be set at the scene level. A .babylon file is usually used to define an entire scene. However, you can use it as a mesh-serializing object. Meaning - a .babylon file is actually a serialized mesh that can be loaded once again using the Babylon loader. A mesh can also be exported back to a .babylon file.

This is not completely clear to me. My .babylon files are just meshes I created in blender, I didn't think this was for "complete" scenes. What is used to just import a mesh/model? Do I make make all my meshes in 1 blender project that I then export to a .babylonjs file?
I thought each mesh/model had its own blender project that was then exported and imported through the AssetsManager in my scene code.

I'm like really confused now about all of this and how I should do it :huh: haha
What is considered best practice?

Share this post


Link to post
Share on other sites

A .babylon file is actually a definition of an entire scene. You CAN use it as a serialization object ob single meshes and load them async / independently .

It is up to you - it depends on the connection between the meshes, and if you want to set the lights / camera / physics using a json file or using code.

About loading it - each file loader is registered using the SceneLoader . the default extension (.babylon) is registered automatically. If you wanta .json loader with the same structure of the .babylon file, you need to simply register it the way I showed you.

Share this post


Link to post
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...

  • Recently Browsing   0 members

    No registered users viewing this page.