1 /**
  2  * @fileOverview plugins for tQuery.core to help creation of object
  3 */
  4 
  5 
  6 //////////////////////////////////////////////////////////////////////////////////
  7 //										//
  8 //////////////////////////////////////////////////////////////////////////////////
  9 
 10 /**
 11  * Create tQuery.World
 12 */
 13 tQuery.registerStatic('createWorld', function(opts){
 14 	return new tQuery.World(opts);
 15 });
 16 
 17 /**
 18  * Create tQuery.World
 19 */
 20 tQuery.registerStatic('createObject3D', function(){
 21 	var object3d	= new THREE.Object3D();
 22 	return tQuery(object3d);
 23 });
 24 
 25 
 26 /**
 27  * Create tQuery.loop
 28  * 
 29  * @param {tQuery.World} world the world to display (optional)
 30  * @function
 31 */
 32 tQuery.registerStatic('createLoop', function(world){
 33 	return new tQuery.Loop(world);
 34 });
 35 
 36 
 37 tQuery.registerStatic('createDirectionalLight', function(){
 38 	var tLight	= new THREE.DirectionalLight();
 39 	return new tQuery.DirectionalLight([tLight]);
 40 });
 41 
 42 tQuery.registerStatic('createSpotLight', function(){
 43 	var tLight	= new THREE.SpotLight();
 44 	return new tQuery.SpotLight([tLight]);
 45 });
 46 
 47 tQuery.registerStatic('createPointLight', function(){
 48 	var tLight	= new THREE.PointLight();
 49 	return new tQuery.PointLight([tLight]);
 50 });
 51 
 52 tQuery.registerStatic('createAmbientLight', function(){
 53 	var tLight	= new THREE.AmbientLight();
 54 	return new tQuery.AmbientLight([tLight]);
 55 });
 56 
 57 
 58 //////////////////////////////////////////////////////////////////////////////////
 59 //										//
 60 //////////////////////////////////////////////////////////////////////////////////
 61 
 62 /**
 63  * contains the default material to use when create tQuery.Object3D
 64  * 
 65  * @fieldOf tQuery
 66  * @name defaultObject3DMaterial
 67 */
 68 tQuery.registerStatic('defaultObject3DMaterial', new THREE.MeshNormalMaterial());
 69 
 70 tQuery.Geometry.prototype.toMesh	= function(material){
 71 	var meshes	= [];
 72 	this.each(function(tGeometry){
 73 		// handle paramters
 74 		material	= material || tQuery.defaultObject3DMaterial;
 75 		// create the THREE.Mesh
 76 		var mesh	= new THREE.Mesh(tGeometry, material)
 77 		// return it
 78 		meshes.push(mesh);
 79 	});
 80 	return new tQuery.Mesh(meshes);
 81 };
 82 
 83 
 84 /**
 85  * Create a cube
 86  * 
 87  * @returns {tQuery.Object3D} a tQuery.Object3D containing it
 88 */
 89 tQuery.registerStatic('createCube', function(){
 90 	var ctor	= THREE.CubeGeometry;
 91 	var dflGeometry	= [1, 1, 1];
 92 	return this._createMesh(ctor, dflGeometry, arguments)
 93 });
 94 
 95 tQuery.registerStatic('createTorus', function(){
 96 	var ctor	= THREE.TorusGeometry;
 97 	var dflGeometry	= [0.5-0.15, 0.15];
 98 	return this._createMesh(ctor, dflGeometry, arguments)
 99 });
100 
101 tQuery.registerStatic('createVector3', function(x, y, z){
102 	return new THREE.Vector3(x, y, z);
103 });
104 
105 tQuery.registerStatic('createSphere', function(){
106 	var ctor	= THREE.SphereGeometry;
107 	var dflGeometry	= [0.5, 32, 16];
108 	return this._createMesh(ctor, dflGeometry, arguments)
109 });
110 
111 tQuery.registerStatic('createPlane', function(){
112 	var ctor	= THREE.PlaneGeometry;
113 	var dflGeometry	= [1, 1, 16, 16];
114 	return this._createMesh(ctor, dflGeometry, arguments)
115 });
116 
117 tQuery.registerStatic('createCylinder', function(){
118 	var ctor	= THREE.CylinderGeometry;
119 	var dflGeometry	= [0.5, 0.5, 1, 16, 4];
120 	return this._createMesh(ctor, dflGeometry, arguments)
121 });
122 
123 tQuery.registerStatic('_createMesh', function(ctor, dflGeometry, args)
124 {
125 	// convert args to array if it is instanceof Arguments
126 	// FIXME if( args instanceof Arguments )
127 	args	= Array.prototype.slice.call( args );
128 	
129 	// init the material
130 	var material	= tQuery.defaultObject3DMaterial;
131 	// if the last arguments is a material, use it
132 	if( args.length && args[args.length-1] instanceof THREE.Material ){
133 		material	= args.pop();
134 	}
135 	
136 	// ugly trick to get .apply() to work 
137 	var createFn	= function(ctor, a0, a1, a2, a3, a4, a5, a6, a7){
138 		console.assert(arguments.length <= 9);
139 		//console.log("createFn", arguments)
140 		return new ctor(a0,a1,a2,a3,a4,a5,a6,a7);
141 	}
142 	if( args.length === 0 )	args	= dflGeometry.slice();
143 	args.unshift(ctor);
144 	var geometry	= createFn.apply(this, args);
145 
146 	// set the geometry.dynamic by default
147 	geometry.dynamic= true;
148 	// create the THREE.Mesh
149 	var mesh	= new THREE.Mesh(geometry, material)
150 	// return it
151 	return tQuery(mesh);
152 });
153 
154 //////////////////////////////////////////////////////////////////////////////////
155 //										//
156 //////////////////////////////////////////////////////////////////////////////////
157 
158 tQuery.registerStatic('createAxis', function(){
159 	var axis	= new THREE.AxisHelper();
160 	axis.scale.multiplyScalar(1/100);
161 	return tQuery(axis);
162 });
163