Browse Source

Add support for cameras

Fernando Serrano 8 years ago
parent
commit
1f2c1d86a9
2 changed files with 55 additions and 1 deletions
  1. 4 1
      examples/gltf_exporter.html
  2. 51 0
      examples/js/exporters/GLTFExporter.js

+ 4 - 1
examples/gltf_exporter.html

@@ -142,12 +142,15 @@
 				object2.position.set( 0, 0, 50 );
 				object2.rotation.set( 0, 45, 0 );
 				object2.name = "SubCube";
-				object.add(object2);
+				object.add( object2 );
 
 				scene.add( object );
 
 				scene.add( camera );
 
+				var cameraOrtho = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, - 10, 10 );
+				scene.add( cameraOrtho );
+
 /*
 				object = new THREE.Mesh( new THREE.CircleGeometry( 50, 20, 0, Math.PI * 2 ), material );
 				object.position.set( 0, 0, 0 );

+ 51 - 0
examples/js/exporters/GLTFExporter.js

@@ -403,6 +403,55 @@ THREE.GLTFExporter.prototype = {
 			return outputJSON.meshes.length - 1;
 		}
 
+		/**
+		 * Process camera
+		 * @param  {THREE.Camera} camera Camera to process
+		 * @return {Integer}      Index of the processed mesh in the "camera" array
+		 */
+		function processCamera( camera ) {
+			if ( !outputJSON.cameras ) {
+				outputJSON.cameras = [];
+			}
+
+			var isOrtho = camera instanceof THREE.OrthographicCamera;
+
+			var gltfCamera = {
+				type: isOrtho ? 'orthographic' : 'perspective'
+			};
+
+			if ( isOrtho ) {
+
+				gltfCamera.orthographic = {
+
+					xmag: camera.right * 2,
+					ymag: camera.top * 2,
+					zfar: camera.far,
+					znear: camera.near
+
+				}
+
+			} else {
+
+				gltfCamera.perspective = {
+
+					aspectRatio: camera.aspect,
+					yfov: THREE.Math.degToRad( camera.fov ) / camera.aspect,
+					zfar: camera.far,
+					znear: camera.near
+
+				};
+
+			}
+
+			if ( camera.name ) {
+				gltfCamera.name = camera.type;
+			}
+
+			outputJSON.cameras.push( gltfCamera );
+
+			return outputJSON.cameras.length - 1;
+		}
+
 		/**
 		 * Process Object3D node
 		 * @param  {THREE.Object3D} node Object3D to processNode
@@ -446,6 +495,8 @@ THREE.GLTFExporter.prototype = {
 
 			if ( object instanceof THREE.Mesh ) {
 				gltfNode.mesh = processMesh( object );
+			} else if ( object instanceof THREE.Camera ) {
+				gltfNode.camera = processCamera( object );
 			}
 
 			if ( object.children.length > 0 ) {