Jump to content

How to export mesh instances and mesh clones from Unity ?


dsman
 Share

Recommended Posts

If there are too many similar meshes in my scene, what do I do in unity such that when I export the scene to Babylon, those meshes are instances of one of them and not remain individual meshes in Babylon? 

Also if there many similar meshes in my scene, which does require separate materials, what do I do in Unity such that when I export the scene to Babylon, those meshes become clones of one of them and not remain individual meshes in Babylon?

@MackeyK24

Link to comment
Share on other sites

If you place several meshes in the scene, there will be several meshes in the output scene (Unity As Well) weather you have separate material instances for each mesh or not.

In the Toolkit by default, materials are SHARED, meaning that only one single 'JSON' exported Babylon Material per material name (Unless lightmap is used, then it gets an internal instance because of the different material.lighptmapTexture and coordinates info used for baked shadow of THAT game object with the same material name). If you need a specific object to use a TOTALLY separate material. Just duplicate the material and give another name and use the dup material as an "Material Instance'... Remember you want has FEW as possible actual materials used in the GPU... Every separate material on a mesh cost you a draw call.

Im sure you know all of this, just pointing that out.

My toolkit does a few features to help out... if all these meshes that similar are static, they don't move... just put them on the 'Babylon Static' layer in Unity Layers.

My static layer will group all static meshes by material and combine them together and used the the merged materials... I also have texture atlas baking tools in there to. You could bake ALL those meshes (or groups of meshes) into a texture atlas. that would put all those meshes on a single material with its UV coordinates adjusted to offset to the texture in the atlas... You can take a WHOLE scene of geometry like 'candyland'... Bake them all into a 4096 texture atlas then all your candyland props will be one a single material even though its over 200 separate meshes.

Then put all those candyland props on the 'Babylon Statics' layer in Unity Editor. (Uncheck static mesh vertex limit in Toolkit Exporter Default Settings)

if all you had where the candyland environment meshes all combined into a single static mesh using the texture atlas materials and it will only cost 1 single draw call

 If you really need 'Runtime Instances'... Put the meshes (including any sub mesh hierarchies with script components and everything) on the 'Babylon Prefab' layer in Unity Editor...  This will make that mesh a PREFAB that is used just like UNITY prefabs at runtime. You can make MATSER meshes at desgin time... script em up with any components and functionality and make em prefabs:

Here is an Example Enemy Ship setup as a prefab with the "Make Instance' Option checked (Prefabs are fully cloned copies... using the make instance clones the mesh as an MeshInstance... so cant change materials on instanced clones like you can a fully cloned copy)

then in would use code to instantiate your prefabs at whatever position and rotation you like.

5a56a0194715f_ScreenShot2018-01-10at1_19_20PM.thumb.png.e2ade90ae974e2aff59cc0df6361dbc2.png

 

In code the Enemy ship gets instantiated:

private spawnPlayerHazard():void {
    if (this.gameOverFlag === true) return;
    this.spawnTimeIndex = this.manager.time;
    if (SpaceController.LoadState === true || SpaceController.WaitState === true) return;
    
    // Spawn hazard prefab
    this.spawnCount++;
    this.hazardIndex++;
    var enemyRatio:number = BABYLON.Scalar.Clamp(SpaceController.EnemyRatio, 1, 3) + 3;
    var randomMark:number = (this.spawnCount <= 3) ? 3 : enemyRatio;
    var randomIndex:number = Math.round(BABYLON.Scalar.RandomRange(1, randomMark));
    var hazardPrefab:string = (randomIndex >= 4) ? "Enemy" : "Asteroid_" + randomIndex.toString();
    var spawnPosition:BABYLON.Vector3 = new BABYLON.Vector3(BABYLON.Scalar.RandomRange(-this.spawnValues.x, this.spawnValues.x), this.spawnValues.y, this.spawnValues.z);
    this.manager.instantiatePrefab(hazardPrefab, "Hazard_" + hazardPrefab + "_" + this.hazardIndex.toString(), spawnPosition, BABYLON.Vector3.Zero());
    if (this.hazardIndex >= Number.MAX_VALUE) this.hazardIndex = 0;
    
    // Validate spawning waves
    if (this.spawnCount >= SpaceController.HazardCountMark) {
        this.spawnCount = 0;
        this.allowSpawn = false;
        if (SpaceController.GameLevel < 3) {
            // Wait for next wave
            SpaceController.WaitState = true;
            this.manager.delay(()=>{ this.checkWaitStatus(); }, 1000);
        } else {
            // Wait for you win
            SpaceController.WinState = true;
            this.manager.delay(()=>{ this.checkWinStatus(); }, 1000);
        }
    }
}

Note the:  

 this.manager.instantiatePrefab() 

 I am using that to instance my enemy ship or asteroid

My enemy ship the mesh is just visual sugar that is a child of the enemy ship object. its just along for the ride so you have something to see. But it is setup so that when you create a prefab clone copy of the enemy ship called 'Enemy' the actual mesh 'Enemy_Ship' is set to create as a babylon instanced mesh because will not and cannot change the material for that instance. That is part of how i keep the whole space shooter demo under 20 draw calls for all the meshes and laser bolt instances and particle system explosions 

5a56a2fcbb4eb_ScreenShot2018-01-10at1_27_07PM.thumb.png.2ac5b531b77a725a217c0769f7789081.png

I hope that helps... I will try to make a video about using my 'babylon art tools' to help you doo all the stuff i said above :)

Check out my Space Shooter Demo ... Still working on particle systems and explosions :)

 

Link to comment
Share on other sites

@MackeyK24   You are awesome. While I started to read the answer, a thought came to mind what if there was a good video explaining mesh optimization (combining meshes and creating texture atlas, and resuing meshes as instance )? And then at the end I read you too are thinking about a video. Yes, a video would be great. 

Anyway, we will try with what you said and try to optimize both mesh count and reuse of mesh using instances. 

The Babylon toolkit for Unity is clearly a much much better replacement for Babylon Editor. Can't thank you enough for creating this. 

Link to comment
Share on other sites

  • 10 months later...

The prefab system is the core tech behind this kind of thing. You have a couple ways of doing this type of thing using prefabs.

1... Manually create instances from prefab meshes

2... Use the built in Tree ? system I made to support Terrains (not complete because I could never get anyone to help me write the terrain shader all the way)

I’m sorry, I have company visiting me this week. But when I get a chance I’ll try make a quick video showing the Terrain and Tree  Painting so you can use the Editor to scatter trees over the terrain . Including Tree LOD’s

Link to comment
Share on other sites

3 hours ago, MackeyK24 said:

The prefab system is the core tech behind this kind of thing. You have a couple ways of doing this type of thing using prefabs.

1... Manually create instances from prefab meshes

2... Use the built in Tree ? system I made to support Terrains (not complete because I could never get anyone to help me write the terrain shader all the way)

I’m sorry, I have company visiting me this week. But when I get a chance I’ll try make a quick video showing the Terrain and Tree  Painting so you can use the Editor to scatter trees over the terrain . Including Tree LOD’s

Thanks, I think I realized how to optimize trees using build-in layers. But the video will always be useful to users. You made a amazing and powerful toolkit!  The only problem is that there is little documentation and examples, and users need to figure out in many things by yourself.

But now I have more questions on another topic: http://www.html5gamedevs.com/topic/41340-textures-for-terrain-painting-in-unity/

 

Link to comment
Share on other sites

This is another area that I repeatedly asked someone to help... Terrain exporting and the shader need to render... I have implemented how Unity does terrain splatmapping but could never really get results I was looking for... so that part is just stuck there in limbo....

FYI... Advance Character Animation and State Machines are I. The same limbo because again ... I can’t get NO ONE to actually help with the code for Calaculate Input Weights (2D) needed to fully implement blend trees... I already did 98% of all the coding for this stuff but can’t get anyone (everyone I asked is too busy to help with my project or just plain and simply refused to help... I even offered to pay several people out of my own pocket... but they all basically told me to screw myself, they won’t help with the Math or coding) 

Also got some issues with Particle Systems but I’m afraid I will get no help there either

Anyways... that’s were my toolkit development is... in limbo.

but I will try to make an Non-official video of how i do the whole terrain, instancing and LOD stuff to PAINT your trees on the Terrain and export everything 

Link to comment
Share on other sites

Yo @Sebavan ... I REALLY NEED HELP Calculating Input Weights for 2D Blend Trees (I dont understand the game math used for Polar Gradient Bands create by Rune ... The Unity Locomotion Guy...)

If you PLEASE contact me so we can talk... I think i still get your cell, you can text me so we can hook up and i can show you all the details. 

I really need this to close up and create my Game mechanics stuff (Which most is based of my implementation of the Unity Mechanim Animation State Machine System)

Secondary would be the Shader... @NasimiAsl needs to help with that :)

 

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