提交 8c06add8 编写于 作者: A alteredq

Editor: refactored scene exporter to handle properly hierarchies.

上级 c828dbbd
...@@ -14,8 +14,6 @@ THREE.SceneExporter.prototype = { ...@@ -14,8 +14,6 @@ THREE.SceneExporter.prototype = {
var rotation = Vector3String( scene.rotation ); var rotation = Vector3String( scene.rotation );
var scale = Vector3String( scene.scale ); var scale = Vector3String( scene.scale );
// todo: extract all scene elements
var nobjects = 0; var nobjects = 0;
var ngeometries = 0; var ngeometries = 0;
var nmaterials = 0; var nmaterials = 0;
...@@ -30,8 +28,6 @@ THREE.SceneExporter.prototype = { ...@@ -30,8 +28,6 @@ THREE.SceneExporter.prototype = {
var materialsMap = {}; var materialsMap = {};
var texturesMap = {}; var texturesMap = {};
// todo: make object creation properly recursive
var checkTexture = function ( map ) { var checkTexture = function ( map ) {
if ( ! map ) return; if ( ! map ) return;
...@@ -46,51 +42,83 @@ THREE.SceneExporter.prototype = { ...@@ -46,51 +42,83 @@ THREE.SceneExporter.prototype = {
}; };
scene.traverse( function ( node ) { var linesArray = [];
if ( node instanceof THREE.Mesh ) { function createObjectsList( object, pad ) {
objectsArray.push( ObjectString( node ) ); for ( var i = 0; i < object.children.length; i ++ ) {
nobjects += 1;
if ( ! ( node.geometry.id in geometriesMap ) ) { var node = object.children[ i ];
geometriesMap[ node.geometry.id ] = true; if ( node instanceof THREE.Mesh ) {
geometriesArray.push( GeometryString( node.geometry ) );
ngeometries += 1;
} linesArray.push( MeshString( node, pad ) );
nobjects += 1;
if ( ! ( node.geometry.id in geometriesMap ) ) {
geometriesMap[ node.geometry.id ] = true;
geometriesArray.push( GeometryString( node.geometry ) );
ngeometries += 1;
}
if ( ! ( node.material.id in materialsMap ) ) {
materialsMap[ node.material.id ] = true;
materialsArray.push( MaterialString( node.material ) );
nmaterials += 1;
checkTexture( node.material.map );
checkTexture( node.material.envMap );
checkTexture( node.material.lightMap );
checkTexture( node.material.specularMap );
checkTexture( node.material.bumpMap );
checkTexture( node.material.normalMap );
if ( ! ( node.material.id in materialsMap ) ) { }
materialsMap[ node.material.id ] = true; } else if ( node instanceof THREE.Light ) {
materialsArray.push( MaterialString( node.material ) );
nmaterials += 1;
checkTexture( node.material.map ); linesArray.push( LightString( node, pad ) );
checkTexture( node.material.envMap ); nobjects += 1;
checkTexture( node.material.lightMap );
checkTexture( node.material.specularMap ); } else if ( node instanceof THREE.Camera ) {
checkTexture( node.material.bumpMap );
checkTexture( node.material.normalMap ); linesArray.push( CameraString( node, pad ) );
nobjects += 1;
} else if ( node instanceof THREE.Object3D ) {
linesArray.push( ObjectString( node, pad ) );
nobjects += 1;
} }
} else if ( node instanceof THREE.Light ) { if ( node.children.length > 0 ) {
objectsArray.push( LightString( node ) ); linesArray.push( PaddingString( pad + 1 ) + '\t\t"children" : {' );
nobjects += 1;
} else if ( node instanceof THREE.Camera ) { }
createObjectsList( node, pad + 2 );
objectsArray.push( CameraString( node ) ); if ( node.children.length > 0 ) {
nobjects += 1;
linesArray.push( PaddingString( pad + 1 ) + "\t\t}" );
}
linesArray.push( PaddingString( pad ) + "\t\t}" + ( i < object.children.length - 1 ? ",\n" : "" ) );
} }
} ); }
createObjectsList( scene, 0 );
var objects = linesArray.join( "\n" );
var objects = generateMultiLineString( objectsArray, ",\n\n\t" );
var geometries = generateMultiLineString( geometriesArray, ",\n\n\t" ); var geometries = generateMultiLineString( geometriesArray, ",\n\n\t" );
var materials = generateMultiLineString( materialsArray, ",\n\n\t" ); var materials = generateMultiLineString( materialsArray, ",\n\n\t" );
var textures = generateMultiLineString( texturesArray, ",\n\n\t" ); var textures = generateMultiLineString( texturesArray, ",\n\n\t" );
...@@ -142,75 +170,81 @@ THREE.SceneExporter.prototype = { ...@@ -142,75 +170,81 @@ THREE.SceneExporter.prototype = {
} }
function PaddingString( n ) {
var output = "";
for ( var i = 0; i < n; i ++ ) output += "\t";
return output;
}
// //
function LightString( l ) { function LightString( o, n ) {
if ( l instanceof THREE.AmbientLight ) { if ( o instanceof THREE.AmbientLight ) {
var output = [ var output = [
'\t' + LabelString( getObjectName( l ) ) + ' : {', '\t\t' + LabelString( getObjectName( o ) ) + ' : {',
' "type" : AmbientLight,', ' "type" : AmbientLight,',
' "color" : ' + l.color.getHex(), ' "color" : ' + o.color.getHex() + ( o.children.length ? ',' : '' )
'}'
]; ];
} else if ( l instanceof THREE.DirectionalLight ) { } else if ( o instanceof THREE.DirectionalLight ) {
var output = [ var output = [
'\t' + LabelString( getObjectName( l ) ) + ' : {', '\t\t' + LabelString( getObjectName( o ) ) + ' : {',
' "type" : DirectionalLight,', ' "type" : DirectionalLight,',
' "color" : ' + l.color.getHex() + ',', ' "color" : ' + o.color.getHex() + ',',
' "intensity" : ' + l.intensity + ',', ' "intensity" : ' + o.intensity + ',',
' "direction" : ' + Vector3String( l.position ), ' "direction" : ' + Vector3String( o.position ) + ( o.children.length ? ',' : '' )
'}'
]; ];
} else if ( l instanceof THREE.PointLight ) { } else if ( o instanceof THREE.PointLight ) {
var output = [ var output = [
'\t' + LabelString( getObjectName( l ) ) + ' : {', '\t\t' + LabelString( getObjectName( o ) ) + ' : {',
' "type" : PointLight,', ' "type" : PointLight,',
' "color" : ' + l.color.getHex() + ',', ' "color" : ' + o.color.getHex() + ',',
' "intensity" : ' + l.intensity + ',', ' "intensity" : ' + o.intensity + ',',
' "position" : ' + Vector3String( l.position ) + ',', ' "position" : ' + Vector3String( o.position ) + ',',
' "distance" : ' + l.distance, ' "distance" : ' + o.distance + ( o.children.length ? ',' : '' )
'}'
]; ];
} else if ( l instanceof THREE.SpotLight ) { } else if ( o instanceof THREE.SpotLight ) {
var output = [ var output = [
'\t' + LabelString( getObjectName( l ) ) + ' : {', '\t\t' + LabelString( getObjectName( o ) ) + ' : {',
' "type" : SpotLight,', ' "type" : SpotLight,',
' "color" : ' + l.color.getHex() + ',', ' "color" : ' + o.color.getHex() + ',',
' "intensity" : ' + l.intensity + ',', ' "intensity" : ' + o.intensity + ',',
' "position" : ' + Vector3String( l.position ) + ',', ' "position" : ' + Vector3String( o.position ) + ',',
' "distance" : ' + l.distance + ',', ' "distance" : ' + o.distance + ',',
' "angle" : ' + l.angle + ',', ' "angle" : ' + o.angle + ',',
' "exponent" : ' + l.exponent, ' "exponent" : ' + o.exponent + ( o.children.length ? ',' : '' )
'}'
]; ];
} else if ( l instanceof THREE.HemisphereLight ) { } else if ( o instanceof THREE.HemisphereLight ) {
var output = [ var output = [
'\t' + LabelString( getObjectName( l ) ) + ' : {', '\t\t' + LabelString( getObjectName( o ) ) + ' : {',
' "type" : HemisphereLight,', ' "type" : HemisphereLight,',
' "skyColor" : ' + l.color.getHex() + ',', ' "skyColor" : ' + o.color.getHex() + ',',
' "groundColor" : ' + l.groundColor.getHex() + ',', ' "groundColor" : ' + o.groundColor.getHex() + ',',
' "intensity" : ' + l.intensity + ',', ' "intensity" : ' + o.intensity + ',',
' "position" : ' + Vector3String( l.position ), ' "position" : ' + Vector3String( o.position ) + ( o.children.length ? ',' : '' )
'}'
]; ];
...@@ -220,41 +254,39 @@ THREE.SceneExporter.prototype = { ...@@ -220,41 +254,39 @@ THREE.SceneExporter.prototype = {
} }
return output.join( '\n\t\t' ); return generateMultiLineString( output, '\n\t\t', n );
} }
function CameraString( c ) { function CameraString( o, n ) {
if ( c instanceof THREE.PerspectiveCamera ) { if ( o instanceof THREE.PerspectiveCamera ) {
var output = [ var output = [
'\t' + LabelString( getObjectName( c ) ) + ' : {', '\t\t' + LabelString( getObjectName( o ) ) + ' : {',
' "type" : PerspectiveCamera,', ' "type" : PerspectiveCamera,',
' "fov": ' + c.fov + ',', ' "fov": ' + o.fov + ',',
' "aspect": ' + c.aspect + ',', ' "aspect": ' + o.aspect + ',',
' "near": ' + c.near + ',', ' "near": ' + o.near + ',',
' "far": ' + c.far + ',', ' "far": ' + o.far + ',',
' "position" : ' + Vector3String( c.position ), ' "position" : ' + Vector3String( o.position ) + ( o.children.length ? ',' : '' )
'}'
]; ];
} else if ( c instanceof THREE.OrthographicCamera ) { } else if ( o instanceof THREE.OrthographicCamera ) {
var output = [ var output = [
'\t' + LabelString( getObjectName( c ) ) + ' : {', '\t\t' + LabelString( getObjectName( o ) ) + ' : {',
' "type" : OrthographicCamera,', ' "type" : OrthographicCamera,',
' "left": ' + c.left + ',', ' "left": ' + o.left + ',',
' "right": ' + c.right + ',', ' "right": ' + o.right + ',',
' "top": ' + c.top + ',', ' "top": ' + o.top + ',',
' "bottom": ' + c.bottom + ',', ' "bottom": ' + o.bottom + ',',
' "near": ' + c.near + ',', ' "near": ' + o.near + ',',
' "far": ' + c.far + ',', ' "far": ' + o.far + ',',
' "position" : ' + Vector3String( c.position ), ' "position" : ' + Vector3String( o.position ) + ( o.children.length ? ',' : '' )
'}'
]; ];
...@@ -264,29 +296,46 @@ THREE.SceneExporter.prototype = { ...@@ -264,29 +296,46 @@ THREE.SceneExporter.prototype = {
} }
return output.join( '\n\t\t' ); return generateMultiLineString( output, '\n\t\t', n );
}
function ObjectString( o, n ) {
var output = [
'\t\t' + LabelString( getObjectName( o ) ) + ' : {',
' "position" : ' + Vector3String( o.position ) + ',',
' "rotation" : ' + Vector3String( o.rotation ) + ',',
' "scale" : ' + Vector3String( o.scale ) + ',',
' "visible" : ' + o.visible + ( o.children.length ? ',' : '' )
];
return generateMultiLineString( output, '\n\t\t', n );
} }
function ObjectString( o ) { function MeshString( o, n ) {
var output = [ var output = [
'\t' + LabelString( getObjectName( o ) ) + ' : {', '\t\t' + LabelString( getObjectName( o ) ) + ' : {',
' "geometry" : ' + LabelString( getGeometryName( o.geometry ) ) + ',', ' "geometry" : ' + LabelString( getGeometryName( o.geometry ) ) + ',',
' "materials": [ ' + LabelString( getMaterialName( o.material ) ) + ' ],', ' "materials": [ ' + LabelString( getMaterialName( o.material ) ) + ' ],',
' "position" : ' + Vector3String( o.position ) + ',', ' "position" : ' + Vector3String( o.position ) + ',',
' "rotation" : ' + Vector3String( o.rotation ) + ',', ' "rotation" : ' + Vector3String( o.rotation ) + ',',
' "scale" : ' + Vector3String( o.scale ) + ',', ' "scale" : ' + Vector3String( o.scale ) + ',',
' "visible" : ' + o.visible, ' "visible" : ' + o.visible + ( o.children.length ? ',' : '' )
'}'
]; ];
return output.join( '\n\t\t' ); return generateMultiLineString( output, '\n\t\t', n );
} }
//
function GeometryString( g ) { function GeometryString( g ) {
if ( g instanceof THREE.SphereGeometry ) { if ( g instanceof THREE.SphereGeometry ) {
...@@ -298,7 +347,7 @@ THREE.SceneExporter.prototype = { ...@@ -298,7 +347,7 @@ THREE.SceneExporter.prototype = {
' "radius" : ' + g.radius + ',', ' "radius" : ' + g.radius + ',',
' "widthSegments" : ' + g.widthSegments + ',', ' "widthSegments" : ' + g.widthSegments + ',',
' "heightSegments" : ' + g.heightSegments + ',', ' "heightSegments" : ' + g.heightSegments + ',',
'}', '}'
]; ];
...@@ -314,7 +363,7 @@ THREE.SceneExporter.prototype = { ...@@ -314,7 +363,7 @@ THREE.SceneExporter.prototype = {
' "widthSegments" : ' + g.widthSegments + ',', ' "widthSegments" : ' + g.widthSegments + ',',
' "heightSegments" : ' + g.heightSegments + ',', ' "heightSegments" : ' + g.heightSegments + ',',
' "depthSegments" : ' + g.depthSegments + ',', ' "depthSegments" : ' + g.depthSegments + ',',
'}', '}'
]; ];
...@@ -328,7 +377,7 @@ THREE.SceneExporter.prototype = { ...@@ -328,7 +377,7 @@ THREE.SceneExporter.prototype = {
' "height" : ' + g.height + ',', ' "height" : ' + g.height + ',',
' "widthSegments" : ' + g.widthSegments + ',', ' "widthSegments" : ' + g.widthSegments + ',',
' "heightSegments" : ' + g.heightSegments + ',', ' "heightSegments" : ' + g.heightSegments + ',',
'}', '}'
]; ];
...@@ -341,7 +390,7 @@ THREE.SceneExporter.prototype = { ...@@ -341,7 +390,7 @@ THREE.SceneExporter.prototype = {
'\t' + LabelString( getGeometryName( g ) ) + ': {', '\t' + LabelString( getGeometryName( g ) ) + ': {',
' "type" : ' + LabelString( g.sourceType ) + ',', ' "type" : ' + LabelString( g.sourceType ) + ',',
' "url" : ' + LabelString( g.sourceFile ), ' "url" : ' + LabelString( g.sourceFile ),
'}', '}'
]; ];
...@@ -383,7 +432,7 @@ THREE.SceneExporter.prototype = { ...@@ -383,7 +432,7 @@ THREE.SceneExporter.prototype = {
' "wireframe" : ' + m.wireframe + ',', ' "wireframe" : ' + m.wireframe + ',',
' "wireframeLinewidth" : ' + m.wireframeLinewidth, ' "wireframeLinewidth" : ' + m.wireframeLinewidth,
' }', ' }',
'}', '}'
]; ];
...@@ -410,7 +459,7 @@ THREE.SceneExporter.prototype = { ...@@ -410,7 +459,7 @@ THREE.SceneExporter.prototype = {
' "wireframe" : ' + m.wireframe + ',', ' "wireframe" : ' + m.wireframe + ',',
' "wireframeLinewidth" : ' + m.wireframeLinewidth, ' "wireframeLinewidth" : ' + m.wireframeLinewidth,
' }', ' }',
'}', '}'
]; ];
...@@ -441,7 +490,7 @@ THREE.SceneExporter.prototype = { ...@@ -441,7 +490,7 @@ THREE.SceneExporter.prototype = {
' "wireframe" : ' + m.wireframe + ',', ' "wireframe" : ' + m.wireframe + ',',
' "wireframeLinewidth" : ' + m.wireframeLinewidth, ' "wireframeLinewidth" : ' + m.wireframeLinewidth,
' }', ' }',
'}', '}'
]; ];
...@@ -457,7 +506,7 @@ THREE.SceneExporter.prototype = { ...@@ -457,7 +506,7 @@ THREE.SceneExporter.prototype = {
' "wireframe" : ' + m.wireframe + ',', ' "wireframe" : ' + m.wireframe + ',',
' "wireframeLinewidth" : ' + m.wireframeLinewidth, ' "wireframeLinewidth" : ' + m.wireframeLinewidth,
' }', ' }',
'}', '}'
]; ];
...@@ -473,7 +522,7 @@ THREE.SceneExporter.prototype = { ...@@ -473,7 +522,7 @@ THREE.SceneExporter.prototype = {
' "wireframe" : ' + m.wireframe + ',', ' "wireframe" : ' + m.wireframe + ',',
' "wireframeLinewidth" : ' + m.wireframeLinewidth, ' "wireframeLinewidth" : ' + m.wireframeLinewidth,
' }', ' }',
'}', '}'
]; ];
...@@ -484,7 +533,7 @@ THREE.SceneExporter.prototype = { ...@@ -484,7 +533,7 @@ THREE.SceneExporter.prototype = {
'\t' + LabelString( getMaterialName( m ) ) + ': {', '\t' + LabelString( getMaterialName( m ) ) + ': {',
' "type" : "MeshFaceMaterial",', ' "type" : "MeshFaceMaterial",',
' "parameters" : {}', ' "parameters" : {}',
'}', '}'
]; ];
...@@ -509,7 +558,7 @@ THREE.SceneExporter.prototype = { ...@@ -509,7 +558,7 @@ THREE.SceneExporter.prototype = {
' "magFilter" : ' + NumConstantString( t.magFilter ) + ',', ' "magFilter" : ' + NumConstantString( t.magFilter ) + ',',
' "minFilter" : ' + NumConstantString( t.minFilter ) + ',', ' "minFilter" : ' + NumConstantString( t.minFilter ) + ',',
' "anisotropy" : ' + t.anisotropy, ' "anisotropy" : ' + t.anisotropy,
'}', '}'
]; ];
...@@ -519,13 +568,20 @@ THREE.SceneExporter.prototype = { ...@@ -519,13 +568,20 @@ THREE.SceneExporter.prototype = {
// //
function generateMultiLineString( lines, separator ) { function generateMultiLineString( lines, separator, padding ) {
var cleanLines = []; var cleanLines = [];
for ( var i = 0; i < lines.length; i ++ ) { for ( var i = 0; i < lines.length; i ++ ) {
if ( lines[ i ] ) cleanLines.push( lines[ i ] ); var line = lines[ i ];
if ( line ) {
if ( padding ) line = PaddingString( padding ) + line;
cleanLines.push( line );
}
} }
...@@ -576,7 +632,7 @@ THREE.SceneExporter.prototype = { ...@@ -576,7 +632,7 @@ THREE.SceneExporter.prototype = {
' "objects" :', ' "objects" :',
' {', ' {',
'\t' + objects, objects,
' },', ' },',
'', '',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册