Sign in to follow this  

save babylon mesh as new babylon file

Recommended Posts

Hi guys. I have a little task that I can't complet it

Let me explain.

I have a sphere with 3 bones. Using bones and a slider I modify the sphere geometry, I can scale, translate or rotate the bones, It works like a morph.

After deform the mesh I want to save it as new babylon file, without bones, but keep deform geometry, not original.

In this scope I update positions, normals, compute normals, but all time the new babylon file is saved like original, and not deform.

I have to update anything else?

Or I have to save the skeleton too? ....If yes, than I have a problem, because I want to add an animation to the new mesh, that I think will overwrite the skeleton.


Here are 2 picture that show my problem


first sphere is the original, and second is sphere saved.


And I make a playground.


Any opinion is welcome. Thanks

Share this post

Link to post
Share on other sites

I did it. :)


with this piece of code

        var positionsData = mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);	var normalsData = mesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);	var matricesIndicesData = mesh.getVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind);	var matricesWeightsData = mesh.getVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind);	var skeletonMatrices = skeleton.getTransformMatrices();	var tempVector3 = BABYLON.Vector3.Zero();	var finalMatrix = new BABYLON.Matrix();	var tempMatrix = new BABYLON.Matrix();		for (var index = 0; index < positionsData.length; index += 3) {		var index4 = (index / 3) * 4;		var matricesWeight0 = matricesWeightsData[index4];		var matricesWeight1 = matricesWeightsData[index4 + 1];		var matricesWeight2 = matricesWeightsData[index4 + 2];		var matricesWeight3 = matricesWeightsData[index4 + 3];		if (matricesWeight0 > 0) {			BABYLON.Matrix.FromFloat32ArrayToRefScaled(skeletonMatrices, matricesIndicesData[index4] * 16, matricesWeight0, tempMatrix);			finalMatrix.addToSelf(tempMatrix);		}		if (matricesWeight1 > 0) {			BABYLON.Matrix.FromFloat32ArrayToRefScaled(skeletonMatrices, matricesIndicesData[index4 + 1] * 16, matricesWeight1, tempMatrix);			finalMatrix.addToSelf(tempMatrix);		}		if (matricesWeight2 > 0) {			BABYLON.Matrix.FromFloat32ArrayToRefScaled(skeletonMatrices, matricesIndicesData[index4 + 2] * 16, matricesWeight2, tempMatrix);			finalMatrix.addToSelf(tempMatrix);		}		if (matricesWeight3 > 0) {			BABYLON.Matrix.FromFloat32ArrayToRefScaled(skeletonMatrices, matricesIndicesData[index4 + 3] * 16, matricesWeight3, tempMatrix);			finalMatrix.addToSelf(tempMatrix);		}		BABYLON.Vector3.TransformCoordinatesFromFloatsToRef(positionsData[index], positionsData[index + 1], positionsData[index + 2], finalMatrix, tempVector3);		tempVector3.toArray(positionsData, index);		BABYLON.Vector3.TransformNormalFromFloatsToRef(normalsData[index], normalsData[index + 1], normalsData[index + 2], finalMatrix, tempVector3);		tempVector3.toArray(normalsData, index); 		finalMatrix.reset();	}	mesh.updateVerticesData(BABYLON.VertexBuffer.PositionKind, positionsData);	mesh.updateVerticesData(BABYLON.VertexBuffer.NormalKind, normalsData);  

 instead of simple update

    var positions = mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind)	mesh.updateVerticesData(BABYLON.VertexBuffer.PositionKind, positions, false, false)    var indices = mesh.getIndices();	var normals = mesh.getVerticesData(BABYLON.VertexBuffer.NormalKind); 	BABYLON.VertexData.ComputeNormals(positions, indices, normals);	mesh.updateVerticesData(BABYLON.VertexBuffer.NormalKind, normals, false, false);

Share this post

Link to post
Share on other sites

Wow! This is exactly what I was looking for.

I created a scene with different single meshes consisting of cubes. Because there are many of them and all have the same geometry and purpose I merged them all together.

This drastically pushed my perfomance.


Now I used the "doDownload" function from bulisors playground example to save the merged meshes to a *.babylon file.

Everything works fine, export and import again.

The only problem I have is, that the import of the file imports everything as single parts, they aren't merged anymore.


How can I solve the problem?



Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.