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