/**
 * @author mikael emtinger / http://gomo.se/
 * @author alteredq / http://alteredqualia.com/
 * @author ikerr / http://verold.com
 */

THREE.SkinnedMesh = function ( geometry, material, useVertexTexture ) {

	THREE.Mesh.call( this, geometry, material );

	this.type = 'SkinnedMesh';

	this.bindMode = "attached";
	this.bindMatrix = new THREE.Matrix4();
	this.bindMatrixInverse = new THREE.Matrix4();

	// init bones

	// TODO: remove bone creation as there is no reason (other than
	// convenience) for THREE.SkinnedMesh to do this.

	var bones = [];

	if ( this.geometry && this.geometry.bones !== undefined ) {

		var bone, gbone;

		for ( var b = 0, bl = this.geometry.bones.length; b < bl; ++ b ) {

			gbone = this.geometry.bones[ b ];

			bone = new THREE.Bone( this );
			bones.push( bone );

			bone.name = gbone.name;
			bone.position.fromArray( gbone.pos );
			bone.quaternion.fromArray( gbone.rotq );
			if ( gbone.scl !== undefined ) bone.scale.fromArray( gbone.scl );

		}

		for ( var b = 0, bl = this.geometry.bones.length; b < bl; ++ b ) {

			gbone = this.geometry.bones[ b ];

			if ( gbone.parent !== - 1 && gbone.parent !== null ) {

				bones[ gbone.parent ].add( bones[ b ] );

			} else {

				this.add( bones[ b ] );

			}

		}

	}

	this.normalizeSkinWeights();

	this.updateMatrixWorld( true );
	this.bind( new THREE.Skeleton( bones, undefined, useVertexTexture ), this.matrixWorld );

};


THREE.SkinnedMesh.prototype = Object.create( THREE.Mesh.prototype );
THREE.SkinnedMesh.prototype.constructor = THREE.SkinnedMesh;

THREE.SkinnedMesh.prototype.bind = function( skeleton, bindMatrix ) {

	this.skeleton = skeleton;

	if ( bindMatrix === undefined ) {

		this.updateMatrixWorld( true );

		this.skeleton.calculateInverses();

		bindMatrix = this.matrixWorld;

	}

	this.bindMatrix.copy( bindMatrix );
	this.bindMatrixInverse.getInverse( bindMatrix );

};

THREE.SkinnedMesh.prototype.pose = function () {

	this.skeleton.pose();

};

THREE.SkinnedMesh.prototype.normalizeSkinWeights = function () {

	if ( this.geometry instanceof THREE.Geometry ) {

		for ( var i = 0; i < this.geometry.skinIndices.length; i ++ ) {

			var sw = this.geometry.skinWeights[ i ];

			var scale = 1.0 / sw.lengthManhattan();

			if ( scale !== Infinity ) {

				sw.multiplyScalar( scale );

			} else {

				sw.set( 1 ); // this will be normalized by the shader anyway

			}

		}

	} else {

		// skinning weights assumed to be normalized for THREE.BufferGeometry

	}

};

THREE.SkinnedMesh.prototype.updateMatrixWorld = function( force ) {

	THREE.Mesh.prototype.updateMatrixWorld.call( this, true );

	if ( this.bindMode === "attached" ) {

		this.bindMatrixInverse.getInverse( this.matrixWorld );

	} else if ( this.bindMode === "detached" ) {

		this.bindMatrixInverse.getInverse( this.bindMatrix );

	} else {

		console.warn( 'THREE.SkinnedMesh unrecognized bindMode: ' + this.bindMode );

	}

};

THREE.SkinnedMesh.prototype.clone = function() {

	return new this.constructor( this.geometry, this.material, this.useVertexTexture ).copy( this );

};

@@ -2123,14 +2123,14 @@
 skin
-Indice
+Weight
 s.le
@@ -2320,81 +2320,486 @@
 %09%09%09%09
-sw.set( 1 ); // this will be normalized by the shader anyway%0A%0A%09%09%09%7D%0A%0A%09%09%7D%0A%0A
+// do nothing%0A%0A%09%09%09%7D%0A%0A%09%09%7D%0A%0A%09%7D else if ( this.geometry instanceof THREE.BufferGeometry ) %7B%0A%0A%09%09var vec = new THREE.Vector4();%0A%0A%09%09var skinWeight = this.geometry.attributes.skinWeight;%0A%0A%09%09for ( var i = 0; i %3C skinWeight.count; i ++ ) %7B%0A%0A%09%09%09vec.x = skinWeight.getX( i );%0A%09%09%09vec.y = skinWeight.getY( i );%0A%09%09%09vec.z = skinWeight.getZ( i );%0A%09%09%09vec.w = skinWeight.getW( i );%0A%0A%09%09%09var scale = 1.0 / vec.lengthManhattan();%0A%0A%09%09%09if ( scale !== Infinity ) %7B%0A%0A%09%09%09%09vec.multiplyScalar( scale );%0A%0A%09%09
 %09%7D e
@@ -2809,79 +2809,91 @@
  %7B%0A%0A
+%09%09
 %09%09// 
-skinning weights assumed to be normalized for THREE.BufferGeometry
+do nothing%0A%0A%09%09%09%7D%0A%0A%09%09%09skinWeight.setXYZ( i, vec.x, vec.y, vec.z, vec.w )%0A%0A%09%09%7D
 %0A%0A%09%7D

support weight normalization for BufferGeometry