Jump to content

Hard time with attachToBone


Recommended Posts

Hello everyone, long time no see!

So i'v ran to an issue where i'm trying to attach meshes to bones from another import.

I'v this torso which is built from 2 meshes and this torso assembly holds the skeleton. Then i'v another model and when i'm trying to place the import directly to head bone for example the model flips around Z axis and goes somewhere it doesn't really belong.

I'v attached an image which hopefully explains more than my english does :D What i'm doing wrong? any ideas?

- best regards, eljuko


Link to comment
Share on other sites

I think the fact of using getMeshByName followed attachToBone fact hard time. 

The engine must search the mesh before attaching.

This can be faster:

var mesh = scene.getMeshByName("industrial_body_bottom");
mesh.attachToBone(actor.skeleton.bones[actor.skeleton.getBoneIndexByName("boneName")], actor);


If you could have the mesh directly without using getMeshByName. it would go faster still.
as the bone, if you know the bone index, it will be faster than using getBoneIndexByName ().

The skeleton also, if you could get it faster than going through scene.skeleton [0] I guess it would go quicker.

and there is a second parameter which is attacheToBone the mesh or this is the Skeleton can also accelerate.

mesh.attachToBone(actor.skeleton.bones[0], actor);

If you must use getMeshByName, use it to load the game and you can then use in-game, but it will be already in memory, thus faster to use.

Link to comment
Share on other sites

Thanks for the answer, i doubt that's the issue quite yet. I'm currently at the state where i'v just imported the models and i just want to know how to attach the meshes to bones in right locations without errors :D optimization will become handy a bit later, and this is much more clear to me thanks to you :) 

Link to comment
Share on other sites

No one? Ok, lets put it this way, is there something wrong in the workflow or in import snippet? now i get error that HEAD is undefined :E Please note this is really simplified.

Maybe i'm missing something important.


	var canvas = document.getElementById("renderCanvas");

	var engine = new BABYLON.Engine(canvas, true);

	var createScene = function () {
		var scene = new BABYLON.Scene(engine);
		scene.clearColor = new BABYLON.Color3( 23 / 255, 20 / 255, 14/255);
		scene.fogMode = BABYLON.Scene.FOGMODE_LINEAR;;
		scene.fogColor = new BABYLON.Color3( 23 / 255, 20 / 255, 14/255);
		scene.fogDensity = 1;
		scene.fogStart = 5.0;
		scene.fogEnd = 30.0;

		var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(2, 3, -9), scene);
		camera.setTarget(new BABYLON.Vector3(0,3,0));

		var light = new BABYLON.DirectionalLight("dir1", new BABYLON.Vector3(-1, -2, -1), scene);
		light.position = new BABYLON.Vector3(20, 40, 20);
		light.intensity = 1;

		var light2 = new BABYLON.SpotLight("spot1", new BABYLON.Vector3(30, 40, 20), new BABYLON.Vector3(-1, -2, -1), 1.1, 16, scene);
		light2.intensity = 0.5;

		var shadowGenerator = new BABYLON.ShadowGenerator(2048, light);

		var TORSO;
		var HEAD;

		BABYLON.SceneLoader.ImportMesh("", "assets/models/sample/", "TORSO_INDUSTRIAL.babylon", scene, function (newMeshes, particleSystems, skeletons){
			TORSO = newMeshes;
			SKELETOR = skeletons[0];
			for(i = 0; i < TORSO.length; i++){
				TORSO[i].material.specularPower = 0;
				TORSO[i].material.specularColor = new BABYLON.Color3(0, 0, 0);
				TORSO[i].material.diffuseColor = new BABYLON.Color3(1, 1, 1);
				TORSO[i].material.emissiveColor = new BABYLON.Color3(1, 1, 1);
				TORSO[i].material.wireframe = true;


		BABYLON.SceneLoader.ImportMesh("", "assets/models/sample/", "HEAD_INDUSTRIAL.babylon", scene, function (newMesh){
			HEAD = newMesh[0];
			HEAD.material.specularPower = 0;
			HEAD.material.specularColor = new BABYLON.Color3(0, 0, 0);
			HEAD.material.diffuseColor = new BABYLON.Color3(1, 1, 1);
			HEAD.material.emissiveColor = new BABYLON.Color3(1, 1, 1);


		shadowGenerator.useVarianceShadowMap = true;
		var material1 = new BABYLON.StandardMaterial("texture1", scene);
		material1.diffuseTexture = new BABYLON.Texture("assets/models/sample/sand.png", scene);
		material1.diffuseTexture.uScale = 500.0;
		material1.diffuseTexture.vScale = 500.0;

		var plane = BABYLON.Mesh.CreatePlane("plane", 1000.0, scene);
		plane.rotation.x += Math.PI / 2;
		plane.material = material1;
		plane.receiveShadows = true;

		return scene;

	var scene = createScene();

		//Do stuff

	window.addEventListener("resize", function () {


Link to comment
Share on other sites

That's because you call attachToBone before the object Import HEAD and SKELETOR is loaded.

try :

scene.executeWhenReady(function () {


use a variable to check that everything is loaded and the rendering loop run attachToBone making sure it runs only once.
But I would prefer the first method with executeWhenReady

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.

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.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...