Sidebar.Properties.Geometry.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. Sidebar.Properties.Geometry = function ( signals ) {
  2. var container = new UI.Panel();
  3. container.setDisplay( 'none' );
  4. container.add( new UI.Text().setText( 'GEOMETRY' ).setColor( '#666' ) );
  5. var button = new UI.Button( 'absolute' ).setRight( '0px' ).setText( 'Export' ).onClick( exportGeometry );
  6. button.download = 'test.js';
  7. container.add( button );
  8. container.add( new UI.Break(), new UI.Break() );
  9. container.add( new UI.Text().setText( 'Name' ).setColor( '#666' ) );
  10. var geometryName = new UI.Text( 'absolute' ).setLeft( '90px' ).setColor( '#444' ).setFontSize( '12px' );
  11. container.add( geometryName );
  12. container.add( new UI.HorizontalRule() );
  13. container.add( new UI.Text().setText( 'Class' ).setColor( '#666' ) );
  14. var geometryClass = new UI.Text( 'absolute' ).setLeft( '90px' ).setColor( '#444' ).setFontSize( '12px' );
  15. container.add( geometryClass );
  16. container.add( new UI.HorizontalRule() );
  17. container.add( new UI.Text().setText( 'Vertices' ).setColor( '#666' ) );
  18. var verticesCount = new UI.Text( 'absolute' ).setLeft( '90px' ).setColor( '#444' ).setFontSize( '12px' );
  19. container.add( verticesCount );
  20. container.add( new UI.HorizontalRule() );
  21. container.add( new UI.Text().setText( 'Faces' ).setColor( '#666' ) );
  22. var facesCount = new UI.Text( 'absolute' ).setLeft( '90px' ).setColor( '#444' ).setFontSize( '12px' );
  23. container.add( facesCount );
  24. container.add( new UI.Break(), new UI.Break(), new UI.Break() );
  25. //
  26. var selected = null;
  27. signals.objectSelected.add( function ( object ) {
  28. if ( object && object.geometry ) {
  29. selected = object.geometry;
  30. container.setDisplay( 'block' );
  31. geometryName.setText( object.geometry.name );
  32. geometryClass.setText( getGeometryInstanceName( object.geometry ) );
  33. verticesCount.setText( object.geometry.vertices.length );
  34. facesCount.setText( object.geometry.faces.length );
  35. } else {
  36. selected = null;
  37. container.setDisplay( 'none' );
  38. }
  39. } );
  40. function getGeometryInstanceName( geometry ) {
  41. // TODO: Is there a way of doing this automatically?
  42. if ( geometry instanceof THREE.ConvexGeometry ) return "ConvexGeometry";
  43. if ( geometry instanceof THREE.CubeGeometry ) return "CubeGeometry";
  44. if ( geometry instanceof THREE.CylinderGeometry ) return "CylinderGeometry";
  45. if ( geometry instanceof THREE.ExtrudeGeometry ) return "ExtrudeGeometry";
  46. if ( geometry instanceof THREE.IcosahedronGeometry ) return "IcosahedronGeometry";
  47. if ( geometry instanceof THREE.LatheGeometry ) return "LatheGeometry";
  48. if ( geometry instanceof THREE.OctahedronGeometry ) return "OctahedronGeometry";
  49. if ( geometry instanceof THREE.ParametricGeometry ) return "ParametricGeometry";
  50. if ( geometry instanceof THREE.PlaneGeometry ) return "PlaneGeometry";
  51. if ( geometry instanceof THREE.PolyhedronGeometry ) return "PolyhedronGeometry";
  52. if ( geometry instanceof THREE.SphereGeometry ) return "SphereGeometry";
  53. if ( geometry instanceof THREE.TetrahedronGeometry ) return "TetrahedronGeometry";
  54. if ( geometry instanceof THREE.TextGeometry ) return "TextGeometry";
  55. if ( geometry instanceof THREE.TorusGeometry ) return "TorusGeometry";
  56. if ( geometry instanceof THREE.TorusKnotGeometry ) return "TorusKnotGeometry";
  57. if ( geometry instanceof THREE.TubeGeometry ) return "TubeGeometry";
  58. if ( geometry instanceof THREE.Geometry ) return "Geometry";
  59. }
  60. function exportGeometry() {
  61. var geometry = selected;
  62. var vertices = [];
  63. for ( var i = 0; i < geometry.vertices.length; i ++ ) {
  64. var vertex = geometry.vertices[ i ];
  65. vertices.push( vertex.x, vertex.y, vertex.z );
  66. }
  67. var faces = [];
  68. var uvs = [[]];
  69. var normals = [];
  70. for ( var i = 0; i < geometry.faces.length; i ++ ) {
  71. var face = geometry.faces[ i ];
  72. var isTriangle = face instanceof THREE.Face3;
  73. var hasMaterial = face.materialIndex !== undefined;
  74. var hasFaceUv = geometry.faceUvs[ 0 ][ i ] !== undefined;
  75. var hasFaceVertexUv = geometry.faceVertexUvs[ 0 ][ i ] !== undefined;
  76. var hasFaceNormal = face.normal.length() > 0;
  77. var hasFaceVertexNormal = face.vertexNormals[ 0 ] !== undefined;
  78. var hasFaceColor = face.color;
  79. var hasFaceVertexColor = face.vertexColors[ 0 ] !== undefined;
  80. var faceType = 0
  81. faceType = setBit( faceType, 0, ! isTriangle );
  82. /*
  83. faceType = setBit( faceType, 1, hasMaterial );
  84. faceType = setBit( faceType, 2, hasFaceUv );
  85. faceType = setBit( faceType, 3, hasFaceVertexUv );
  86. faceType = setBit( faceType, 4, hasFaceNormal );
  87. faceType = setBit( faceType, 5, hasFaceVertexNormal );
  88. faceType = setBit( faceType, 6, hasFaceColor );
  89. faceType = setBit( faceType, 7, hasFaceVertexColor );
  90. */
  91. faces.push( faceType );
  92. if ( isTriangle ) {
  93. faces.push( face.a, face.b, face.c );
  94. } else {
  95. faces.push( face.a, face.b, face.c, face.d );
  96. }
  97. if ( hasMaterial ) {
  98. faces.push( face.materialIndex );
  99. }
  100. if ( hasFaceUv ) {
  101. /*
  102. var uv = geometry.faceUvs[ 0 ][ i ];
  103. uvs[ 0 ].push( uv.u, uv.v );
  104. */
  105. }
  106. if ( hasFaceVertexUv ) {
  107. /*
  108. var uvs = geometry.faceVertexUvs[ 0 ][ i ];
  109. if ( isTriangle ) {
  110. faces.push(
  111. uvs[ 0 ].u, uvs[ 0 ].v,
  112. uvs[ 1 ].u, uvs[ 1 ].v,
  113. uvs[ 2 ].u, uvs[ 2 ].v
  114. );
  115. } else {
  116. faces.push(
  117. uvs[ 0 ].u, uvs[ 0 ].v,
  118. uvs[ 1 ].u, uvs[ 1 ].v,
  119. uvs[ 2 ].u, uvs[ 2 ].v,
  120. uvs[ 3 ].u, uvs[ 3 ].v
  121. );
  122. }
  123. */
  124. }
  125. if ( hasFaceNormal ) {
  126. /*
  127. var normal = face.normal;
  128. faces.push( normal.x, normal.y, normal.z );
  129. */
  130. }
  131. if ( hasFaceVertexNormal ) {
  132. /*
  133. var normals = face.vertexNormals;
  134. if ( isTriangle ) {
  135. faces.push(
  136. normals[ 0 ].u, normals[ 0 ].y, normals[ 0 ].z,
  137. normals[ 1 ].u, normals[ 1 ].y, normals[ 1 ].z,
  138. normals[ 2 ].u, normals[ 2 ].y, normals[ 2 ].z
  139. );
  140. } else {
  141. faces.push(
  142. normals[ 0 ].x, normals[ 0 ].y, normals[ 0 ].z,
  143. normals[ 1 ].x, normals[ 1 ].y, normals[ 1 ].z,
  144. normals[ 2 ].x, normals[ 2 ].y, normals[ 2 ].z,
  145. normals[ 3 ].x, normals[ 3 ].y, normals[ 3 ].z
  146. );
  147. }
  148. */
  149. }
  150. }
  151. function setBit( value, position, enabled ) {
  152. return enabled ? value | ( 1 << position ) : value & ( ~ ( 1 << position) );
  153. }
  154. //
  155. var output = [
  156. '{',
  157. ' "metadata": {',
  158. ' "formatVersion" : 3',
  159. ' },',
  160. ' "vertices": ' + JSON.stringify( vertices ) + ',',
  161. ' "normals": ' + JSON.stringify( normals ) + ',',
  162. ' "uvs": ' + JSON.stringify( uvs ) + ',',
  163. ' "faces": ' + JSON.stringify( faces ),
  164. '}'
  165. ].join( '\n' );
  166. var file = new BlobBuilder();
  167. file.append( output );
  168. var objectURL = URL.createObjectURL( file.getBlob( 'text/json' ) );
  169. var clickEvent = document.createEvent( 'MouseEvent' );
  170. clickEvent.initMouseEvent( 'click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null );
  171. var download = document.createElement( 'a' );
  172. download.href = objectURL;
  173. download.download = 'geometry.js';
  174. download.dispatchEvent( clickEvent );
  175. URL.revokeObjectURL( objectURL );
  176. }
  177. return container;
  178. }