Jump to content

[SOLVED] - Replace Mesh Vertex Data


MackeyK24
 Share

Recommended Posts

Can you NOT replace VertexData on a mesh?

I am creating a Empty Mesh in C# Unity (I save some metadata info about a box size). Thin in client code i create a box VertexData and apply to mesh... But i see nothing... When i try the same "ApplyBoxCollider" code a new BABYLON.Mesh it works fine.

My apply vertex function:

        private static applyBoxCollider(box:BABYLON.Mesh, options: { size?: number, width?: number, height?: number, depth?: number, faceUV?: Vector4[], faceColors?: Color4[], sideOrientation?: number, updatable?: boolean }, scene: Scene): void {
            options.sideOrientation = this.updateSideOrientation(options.sideOrientation, scene);
            box.sideOrientation = options.sideOrientation;
            var vertexData = VertexData.CreateBox(options);
            vertexData.applyToMesh(box, options.updatable);
        }

This WORKS:

var box:BABYLON.Mesh = new BABYLON.Mesh("SHIT", this._scene);
BABYLON.SceneManager.applyBoxCollider(box, boxOptions, this._scene);

This does NOT:

BABYLON.SceneManager.applyBoxCollider(existingBox, boxOptions, this._scene);

I was hoping to create an empty mesh on the C# and then create the actual collision mesh on the client side and simply RESET the vertex data on an already "Parented Mesh" with all its checkCollision stuff already setup... Is that Bad... Should I just Create the whole mesh on client side only and use New Mesh instead of trying to replace the underlying vertextdata ???

Thoughts Anyone :)

 

Link to comment
Share on other sites

Anyone?  Even me?  Coooool. 

"Come and listen to my story 'bout a mesh named 'SHIT'... a vert-swappin node... tryin' to make of mess of it."  (Beverly Hillbillies theme, etc, ahem)

BABYLON.SceneManager?  That's a terrible name.  ;)  10,000 future youngsters will flock to that class (instead-of to Scene Class)... thinking they found a scene builder helper.  They won't know its a "loose mirror" of https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.html

Or is it so?  Anyway, I think (perhaps) do some new domains.  BABYLON.UnityTools.whatever, maybe.  And and and... you are doing a mesh-level action with BABYLON.SceneManager.applyBoxCollider(), so it should be done at BABYLON(.UnityTools).MeshManager.applyBoxCollider().

What a whiny off-topic jerk I am, huh?  :)  It's really cold outside here in Northern Michigan, now... and it makes people cranky.  :) 

Oh, yeah, the actual subject?  I bet you will find that vert swap-out speeds and make-new-mesh speeds... are pretty close to equal.  But, as @fenomas reminds us... "use the browser profiler".  I don't, but those who care about perf-testing, should, I hear. *shrug*  You're workin' like heck on something big over there, aren't ya Mack?  You're really active.  You're building a big bridge, aren't ya?  Good luck.

Link to comment
Share on other sites

@Wingnut First off... I want to make CLEAR i am NEVER calling ANYTHING about BabylonJS "SHIT" or "CRAP" or anything else bad... I LUV BabylonJS... Cant you tell... I spend ALOT of time working on my toolkit so i can make kool games (easily) with BabylonJS.

Now when I'm coding and things don't go right... I often make a "TEST" variable or something to try to track down the problem... I OFTEN call that tracking variable: "CRAP" OR "SHIT" OR "WTF" just to indicate to myself there is where something is wrong. THATS IT...

Just wanna be clear... I think that's the second or third time someone mentioned my use of "SHIT" or "CRAP" or "WTF"...

BTW... I also name my test variables things like "HOLYSHIT" that worked or "FXXX_YEAH" when things go really right :)

 

Link to comment
Share on other sites

 

I don't think the "GUYS" like me calling it "Unity" it was suggested to me maybe remove the unity stuff if it was NOT specific to Unity.  So all the Managed Scene Component API (Hence the name: SceneManager and SceneComponents) can be used WITH or WITHOUT unity to simply give you more of a component style API... Now we all know (who was around when i started this project) this api is HEAVILY BASED on the "Unity-Style" workflow. Thats Why I Made It :)

If you got a minute... Please take a look at my latest status to see whats the deal with Vertex Data... And my whole Colliders, Trigger Areas and Intersection Events... I am trying to get this toolkit out the door (and working right): 

U3D - BabylonJS Toolkit: Colliders, Trigger Areas and Intersection Events

@Deltakosh and @Wingnut Take a look when you get a chance :)

Link to comment
Share on other sites

As soon as i get a CAPSULE mesh... That the only thing left... I even got virtual joystick (the api is done... @davrous is gonna fix the native virtual joystick)...

I got one "// TODO:" block left... and that was the Capsule Mesh... Can you please help with a "VertexData.CreateCapsule" function and i will just start uploading the rest of the toolkit now... if i know 'The Man' @Deltakosh  is looking into Capsule Mesh :)

Take a look at that last video to see where I'm trying to use the Capsule Mesh and Exactly where i need that final piece of Help.

Link to comment
Share on other sites

@Deltakosh Quick Question... Can i take vertexData from an existing mesh and apply to another mesh... It seems your solution uses MESHES - Babylon.Mesh.CreateCylinder... I already have the empty mesh and using the VertexData.CreateXXX which return vertex data that i apply to the existing mesh.

It looks like you are using 1 cylinder and 2 spheres cut in half...  Can i create the cylinder and 2 sphere halves using VertextData.CreateXXX and then MERGE the vertex data onto an existing mesh.

Link to comment
Share on other sites

Hey @Deltakosh i am trying to use your solution ... But with vertex data... Ran into a problem... How do i set hemi1 and hemi2 position.y and hem1 rotation.x to move the caps to the top and bottom of the cylinder... Using vertexData ???

Here is what i got so far:

var cylinderData = BABYLON.VertexData.CreateCylinder( { height: cylinderSize, diameterTop: diameter, diameterBottom: diameter, tessellation: segments, subdivisions: 2 } );
var hemi1Data = BABYLON.VertexData.CreateSphere({diameter: hemiRadius*2, slice: 0.5, segments: segments, sideOrientation: BABYLON.Mesh.DOUBLESIDE});
var hemi2Data = BABYLON.VertexData.CreateSphere({diameter: hemiRadius*2, slice: 0.5, segments: segments, sideOrientation: BABYLON.Mesh.DOUBLESIDE});
            
//hemi1.rotation.x = Math.PI;
//hemi1.position.y = -halfCylinderSize;
//hemi2.position.y = halfCylinderSize;
            
hemi1Data.merge(cylinderData);
hemi1Data.merge(hemi2Data);
hemi1Data.applyToMesh(mx);

 

the vertex data.merge does not let take into account WHERE you wanna merge the at... or basically some what of offsetting it Y position for merging ???

Link to comment
Share on other sites

Well... This is my latest attempt... A BIT CHEEZY... But it works... Im gonna play with it some... I don't like that I'm Adding three meshes to the scene... Doing a merge which creates another mesh on the scene (at least it has option to dispose source meshes) and then, because i already have a setup mesh that i really just need to apply vertex data, i have ExtractVertexData from the merged mesh and apply is to my existing mesh... Like i said ... a bit cheesy :)

 

            var existingMesh:BABYLON.Mesh = new BABYLON.Mesh("capsulex", this.scene);

            var hemiRadius = .5; 
            var segments = 12;
            var diameter = 1;
            var height = 1;

            var radius = diameter / 2;
			var cylinderSize = height - (radius / 2.5);
			var halfCylinderSize = cylinderSize / 2;

            var cylinder = BABYLON.Mesh.CreateCylinder("wtf", cylinderSize, diameter, diameter, segments, 2, this.scene, false);            
            var hemi1 = BABYLON.MeshBuilder.CreateSphere("h1", {diameter: hemiRadius*2, slice: 0.5, segments: segments}, this.scene);
	        hemi1.rotation.x = Math.PI;
            hemi1.position.y = -halfCylinderSize;
            var hemi2 = BABYLON.MeshBuilder.CreateSphere("h1", {diameter: hemiRadius*2, slice: 0.5, segments: segments}, this.scene);
            hemi2.position.y = halfCylinderSize;
            
            var mx = BABYLON.Mesh.MergeMeshes([hemi1, cylinder, hemi2], true);
            BABYLON.VertexData.ExtractFromMesh(mx).applyToMesh(existingMesh);
            mx.dispose();

            existingMesh.position.x = 0;
            existingMesh.position.y = height;
            existingMesh.position.z = 0;

 

Ill keep working on a better solution though.

 

Link to comment
Share on other sites

18 hours ago, MackeyK24 said:

@Wingnut First off... I want to make CLEAR i am NEVER calling ANYTHING about BabylonJS "SHIT" or "CRAP" or anything else bad... I LUV BabylonJS... Cant you tell... I spend ALOT of time working on my toolkit so i can make kool games (easily) with BabylonJS.

Now when I'm coding and things don't go right... I often make a "TEST" variable or something to try to track down the problem... I OFTEN call that tracking variable: "CRAP" OR "SHIT" OR "WTF" just to indicate to myself there is where something is wrong. THATS IT...

Just wanna be clear... I think that's the second or third time someone mentioned my use of "SHIT" or "CRAP" or "WTF"...

BTW... I also name my test variables things like "HOLYSHIT" that worked or "FXXX_YEAH" when things go really right :)

 

ahhah I do the some thing... if a variable is giving me trouble it gets renamed to something vulgar... this made me crack up though!

Link to comment
Share on other sites

Thanks, I cant wait for the release.

With the current exporter I can export the scenes but the animations are not being exported for me maybe Im doing something wrong, I have been trying to replicate your work  @MackeyK24 on your  video (http://mackey.cloud/files/videos/u3dbabylonApi.mp4)

on the minute 19  you explain the Exporter metadata and serialization that you are using to get the rest of the properties from unity into Babylon format if I'm not mistaken;  I'm trying to get the animations that I wrote in C# to Babylon

here are my unity/babylon scenes , if you can please point me in the right direction I would appreciated other wise I will wait for the release.

Thanks for your work, i really hope i can contribute soon on this, but I still a newbie learning.

 

 

 

Link to comment
Share on other sites

21 hours ago, MackeyK24 said:

@Wingnut First off... I want to make CLEAR i am NEVER calling ANYTHING about BabylonJS "SHIT" or "CRAP" or anything else bad... I LUV BabylonJS... Cant you tell... I spend ALOT of time working on my toolkit so i can make kool games (easily) with BabylonJS.

Now when I'm coding and things don't go right... I often make a "TEST" variable or something to try to track down the problem... I OFTEN call that tracking variable: "CRAP" OR "SHIT" OR "WTF" just to indicate to myself there is where something is wrong. THATS IT...

Just wanna be clear... I think that's the second or third time someone mentioned my use of "SHIT" or "CRAP" or "WTF"...

BTW... I also name my test variables things like "HOLYSHIT" that worked or "FXXX_YEAH" when things go really right :)

 

:blink:

Link to comment
Share on other sites

22 hours ago, MackeyK24 said:

@Wingnut First off... I want to make CLEAR i am NEVER calling ANYTHING about BabylonJS "SHIT" or "CRAP" or anything else bad... I LUV BabylonJS... Cant you tell... I spend ALOT of time working on my toolkit so i can make kool games (easily) with BabylonJS.

Now when I'm coding and things don't go right... I often make a "TEST" variable or something to try to track down the problem... I OFTEN call that tracking variable: "CRAP" OR "SHIT" OR "WTF" just to indicate to myself there is where something is wrong. THATS IT...

Just wanna be clear... I think that's the second or third time someone mentioned my use of "SHIT" or "CRAP" or "WTF"...

BTW... I also name my test variables things like "HOLYSHIT" that worked or "FXXX_YEAH" when things go really right :)

 

Search all the GIFs

I don't really care how you name your variables.  I'm good as long as I can understand your logic.

Link to comment
Share on other sites

1 minute ago, MackeyK24 said:

@MrVR ... I think i may have the same issue as you (I still use the original skeletal animation exporter code). I am ALMOST finished cleaning up my mesh collision system... Then i will PR the toolkit... We can all as a community (using the newer exporting codebase from the toolkit) take a look at the animation export part :)

 

Sure @MackeyK24 I will, I saw your videos like three time already so I think I have a pretty good Idea how all works, thanks again man for that awesome work, I cant wait to put my hands on your code., if you need any type of help you can PM me, I have lots of spare time and Im willing to put it here

Link to comment
Share on other sites

Yo @Deltakosh Thanks Sooooo much for putting me on the right path for creating capsule... Like i said i am TOTALLY SHITTY ( And I really mean shitty this time :)) at making up the numbers to create raw geometry myself... but if i can understand what i gotta do programmatically... Im kool.

Now i didn't like merging AND REALLY DONT like dirty y scene just to put together pieces of a capsule... My OCD couldn't handle that :)

BUT... you should me what i should be looking for... So check it out... Create and capsule with pure vertices, uvs and triangles... After playing with your solution... I spent HOURS looking thru the internet on Procedural Geometry Creation... Tweaked some stuff out a bit.. This is what i cam up with... Check it out yo:

 

        public static Mesh GenerateCapsuleMesh(float height = 2f, float radius = 0.5f, int segments = 24) 
        {
            Mesh result = new Mesh();
            // Make segments an even number
            if ( segments % 2 != 0 ) {
                segments ++;
            }
            // Extra vertex on the seam
            int points = segments + 1;
            // Calculate points around a circle
            float[] pX = new float[ points ];
            float[] pZ = new float[ points ];
            float[] pY = new float[ points ];
            float[] pR = new float[ points ];
            float calcH = 0f;
            float calcV = 0f;
            for ( int i = 0; i < points; i ++ ) {
                pX[ i ] = Mathf.Sin( calcH * Mathf.Deg2Rad ); 
                pZ[ i ] = Mathf.Cos( calcH * Mathf.Deg2Rad );
                pY[ i ] = Mathf.Cos( calcV * Mathf.Deg2Rad ); 
                pR[ i ] = Mathf.Sin( calcV * Mathf.Deg2Rad );

                calcH += 360f / (float)segments;
                calcV += 180f / (float)segments;
            }
            // Vertices and UVs
            Vector3[] vertices = new Vector3[ points * ( points + 1 ) ];
            Vector2[] uvs = new Vector2[ vertices.Length ];
            int ind = 0;
            // Y-Offset is half the height minus the diameter
            float yOff = ( height - ( radius * 2f ) ) * 0.5f;
            if ( yOff < 0 ) yOff = 0;
            // UV Calculations
            float stepX = 1f / ( (float)(points - 1) );
            float uvX, uvY;
            // Top Hemisphere
            int top = Mathf.CeilToInt( (float)points * 0.5f );
            for ( int y = 0; y < top; y ++ ) {
                for ( int x = 0; x < points; x ++ ) {
                    vertices[ ind ] = new Vector3( pX[ x ] * pR[ y ], pY[ y ], pZ[ x ] * pR[ y ] ) * radius;
                    vertices[ ind ].y = yOff + vertices[ ind ].y;
                    
                    uvX = 1f - ( stepX * (float)x );
                    uvY = ( vertices[ ind ].y + ( height * 0.5f ) ) / height;
                    uvs[ ind ] = new Vector2( uvX, uvY );
                    
                    ind ++;
                }
            }
            // Bottom Hemisphere
            int btm = Mathf.FloorToInt( (float)points * 0.5f );
            for ( int y = btm; y < points; y ++ ) {
                for ( int x = 0; x < points; x ++ ) {
                    vertices[ ind ] = new Vector3( pX[ x ] * pR[ y ], pY[ y ], pZ[ x ] * pR[ y ] ) * radius;
                    vertices[ ind ].y = -yOff + vertices[ ind ].y;
                    
                    uvX = 1f - ( stepX * (float)x );
                    uvY = ( vertices[ ind ].y + ( height * 0.5f ) ) / height;
                    uvs[ ind ] = new Vector2( uvX, uvY );
                    
                    ind ++;
                }
            }
            // Triangles
            int[] triangles = new int[ ( segments * (segments + 1) * 2 * 3 ) ];
            for ( int y = 0, t = 0; y < segments + 1; y ++ ) {
                for ( int x = 0; x < segments; x ++, t += 6 ) {
                    triangles[ t + 0 ] = ( (y + 0) * ( segments + 1 ) ) + x + 0;
                    triangles[ t + 1 ] = ( (y + 1) * ( segments + 1 ) ) + x + 0;
                    triangles[ t + 2 ] = ( (y + 1) * ( segments + 1 ) ) + x + 1;
                    
                    triangles[ t + 3 ] = ( (y + 0) * ( segments + 1 ) ) + x + 1;
                    triangles[ t + 4 ] = ( (y + 0) * ( segments + 1 ) ) + x + 0;
                    triangles[ t + 5 ] = ( (y + 1) * ( segments + 1 ) ) + x + 1;
                }
            }
            // Mesh Result
            result.vertices = vertices;
            result.uv = uvs;
            result.triangles = triangles;
            result.RecalculateBounds();
            result.RecalculateNormals();
            return result;
        }

 

Nice and clean one swoop... Work great !!!

Now i decided to make this in C#... i am re-coding the vertex data creation part to stay in c#... First off.. All geometry will already be setup and ready to go just using the normal scene deserialization... And Second... NO NEED to scene.getMeshesByTags() during parse ... making scene loading even faster :)

When i get some time, ill port this function to a VertexData.CreateCapsule and PR it for the rest of babylon js users who would like to be a Mesh.CreateCapsule like Mesh.CreateBox :)

 

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