Menubar.Add.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. import * as THREE from '../../build/three.module.js';
  2. import { UIPanel, UIRow, UIHorizontalRule } from './libs/ui.js';
  3. import { AddObjectCommand } from './commands/AddObjectCommand.js';
  4. function MenubarAdd( editor ) {
  5. var strings = editor.strings;
  6. var container = new UIPanel();
  7. container.setClass( 'menu' );
  8. var title = new UIPanel();
  9. title.setClass( 'title' );
  10. title.setTextContent( strings.getKey( 'menubar/add' ) );
  11. container.add( title );
  12. var options = new UIPanel();
  13. options.setClass( 'options' );
  14. container.add( options );
  15. // Group
  16. var option = new UIRow();
  17. option.setClass( 'option' );
  18. option.setTextContent( strings.getKey( 'menubar/add/group' ) );
  19. option.onClick( function () {
  20. var mesh = new THREE.Group();
  21. mesh.name = 'Group';
  22. editor.execute( new AddObjectCommand( editor, mesh ) );
  23. } );
  24. options.add( option );
  25. //
  26. options.add( new UIHorizontalRule() );
  27. // Box
  28. var option = new UIRow();
  29. option.setClass( 'option' );
  30. option.setTextContent( strings.getKey( 'menubar/add/box' ) );
  31. option.onClick( function () {
  32. var geometry = new THREE.BoxGeometry( 1, 1, 1, 1, 1, 1 );
  33. var mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  34. mesh.name = 'Box';
  35. editor.execute( new AddObjectCommand( editor, mesh ) );
  36. } );
  37. options.add( option );
  38. // Circle
  39. var option = new UIRow();
  40. option.setClass( 'option' );
  41. option.setTextContent( strings.getKey( 'menubar/add/circle' ) );
  42. option.onClick( function () {
  43. var geometry = new THREE.CircleGeometry( 1, 8, 0, Math.PI * 2 );
  44. var mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  45. mesh.name = 'Circle';
  46. editor.execute( new AddObjectCommand( editor, mesh ) );
  47. } );
  48. options.add( option );
  49. // Cylinder
  50. var option = new UIRow();
  51. option.setClass( 'option' );
  52. option.setTextContent( strings.getKey( 'menubar/add/cylinder' ) );
  53. option.onClick( function () {
  54. var geometry = new THREE.CylinderGeometry( 1, 1, 1, 8, 1, false, 0, Math.PI * 2 );
  55. var mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  56. mesh.name = 'Cylinder';
  57. editor.execute( new AddObjectCommand( editor, mesh ) );
  58. } );
  59. options.add( option );
  60. // Dodecahedron
  61. var option = new UIRow();
  62. option.setClass( 'option' );
  63. option.setTextContent( strings.getKey( 'menubar/add/dodecahedron' ) );
  64. option.onClick( function () {
  65. var geometry = new THREE.DodecahedronGeometry( 1, 0 );
  66. var mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  67. mesh.name = 'Dodecahedron';
  68. editor.execute( new AddObjectCommand( editor, mesh ) );
  69. } );
  70. options.add( option );
  71. // Icosahedron
  72. var option = new UIRow();
  73. option.setClass( 'option' );
  74. option.setTextContent( strings.getKey( 'menubar/add/icosahedron' ) );
  75. option.onClick( function () {
  76. var geometry = new THREE.IcosahedronGeometry( 1, 0 );
  77. var mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  78. mesh.name = 'Icosahedron';
  79. editor.execute( new AddObjectCommand( editor, mesh ) );
  80. } );
  81. options.add( option );
  82. // Lathe
  83. var option = new UIRow();
  84. option.setClass( 'option' );
  85. option.setTextContent( strings.getKey( 'menubar/add/lathe' ) );
  86. option.onClick( function () {
  87. var geometry = new THREE.LatheGeometry();
  88. var mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial( { side: THREE.DoubleSide } ) );
  89. mesh.name = 'Lathe';
  90. editor.execute( new AddObjectCommand( editor, mesh ) );
  91. } );
  92. options.add( option );
  93. // Octahedron
  94. var option = new UIRow();
  95. option.setClass( 'option' );
  96. option.setTextContent( strings.getKey( 'menubar/add/octahedron' ) );
  97. option.onClick( function () {
  98. var geometry = new THREE.OctahedronGeometry( 1, 0 );
  99. var mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  100. mesh.name = 'Octahedron';
  101. editor.execute( new AddObjectCommand( editor, mesh ) );
  102. } );
  103. options.add( option );
  104. // Plane
  105. var option = new UIRow();
  106. option.setClass( 'option' );
  107. option.setTextContent( strings.getKey( 'menubar/add/plane' ) );
  108. option.onClick( function () {
  109. var geometry = new THREE.PlaneGeometry( 1, 1, 1, 1 );
  110. var material = new THREE.MeshStandardMaterial();
  111. var mesh = new THREE.Mesh( geometry, material );
  112. mesh.name = 'Plane';
  113. editor.execute( new AddObjectCommand( editor, mesh ) );
  114. } );
  115. options.add( option );
  116. // Ring
  117. var option = new UIRow();
  118. option.setClass( 'option' );
  119. option.setTextContent( strings.getKey( 'menubar/add/ring' ) );
  120. option.onClick( function () {
  121. var geometry = new THREE.RingGeometry( 0.5, 1, 8, 1, 0, Math.PI * 2 );
  122. var mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  123. mesh.name = 'Ring';
  124. editor.execute( new AddObjectCommand( editor, mesh ) );
  125. } );
  126. options.add( option );
  127. // Sphere
  128. var option = new UIRow();
  129. option.setClass( 'option' );
  130. option.setTextContent( strings.getKey( 'menubar/add/sphere' ) );
  131. option.onClick( function () {
  132. var geometry = new THREE.SphereGeometry( 1, 32, 16, 0, Math.PI * 2, 0, Math.PI );
  133. var mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  134. mesh.name = 'Sphere';
  135. editor.execute( new AddObjectCommand( editor, mesh ) );
  136. } );
  137. options.add( option );
  138. // Sprite
  139. var option = new UIRow();
  140. option.setClass( 'option' );
  141. option.setTextContent( strings.getKey( 'menubar/add/sprite' ) );
  142. option.onClick( function () {
  143. var sprite = new THREE.Sprite( new THREE.SpriteMaterial() );
  144. sprite.name = 'Sprite';
  145. editor.execute( new AddObjectCommand( editor, sprite ) );
  146. } );
  147. options.add( option );
  148. // Tetrahedron
  149. var option = new UIRow();
  150. option.setClass( 'option' );
  151. option.setTextContent( strings.getKey( 'menubar/add/tetrahedron' ) );
  152. option.onClick( function () {
  153. var geometry = new THREE.TetrahedronGeometry( 1, 0 );
  154. var mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  155. mesh.name = 'Tetrahedron';
  156. editor.execute( new AddObjectCommand( editor, mesh ) );
  157. } );
  158. options.add( option );
  159. // Torus
  160. var option = new UIRow();
  161. option.setClass( 'option' );
  162. option.setTextContent( strings.getKey( 'menubar/add/torus' ) );
  163. option.onClick( function () {
  164. var geometry = new THREE.TorusGeometry( 1, 0.4, 8, 6, Math.PI * 2 );
  165. var mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  166. mesh.name = 'Torus';
  167. editor.execute( new AddObjectCommand( editor, mesh ) );
  168. } );
  169. options.add( option );
  170. // TorusKnot
  171. var option = new UIRow();
  172. option.setClass( 'option' );
  173. option.setTextContent( strings.getKey( 'menubar/add/torusknot' ) );
  174. option.onClick( function () {
  175. var geometry = new THREE.TorusKnotGeometry( 1, 0.4, 64, 8, 2, 3 );
  176. var mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  177. mesh.name = 'TorusKnot';
  178. editor.execute( new AddObjectCommand( editor, mesh ) );
  179. } );
  180. options.add( option );
  181. // Tube
  182. var option = new UIRow();
  183. option.setClass( 'option' );
  184. option.setTextContent( strings.getKey( 'menubar/add/tube' ) );
  185. option.onClick( function () {
  186. var path = new THREE.CatmullRomCurve3( [
  187. new THREE.Vector3( 2, 2, - 2 ),
  188. new THREE.Vector3( 2, - 2, - 0.6666666666666667 ),
  189. new THREE.Vector3( - 2, - 2, 0.6666666666666667 ),
  190. new THREE.Vector3( - 2, 2, 2 )
  191. ] );
  192. var geometry = new THREE.TubeGeometry( path, 64, 1, 8, false );
  193. var mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  194. mesh.name = 'Tube';
  195. editor.execute( new AddObjectCommand( editor, mesh ) );
  196. } );
  197. options.add( option );
  198. /*
  199. // Teapot
  200. var option = new UIRow();
  201. option.setClass( 'option' );
  202. option.setTextContent( 'Teapot' );
  203. option.onClick( function () {
  204. var size = 50;
  205. var segments = 10;
  206. var bottom = true;
  207. var lid = true;
  208. var body = true;
  209. var fitLid = false;
  210. var blinnScale = true;
  211. var material = new THREE.MeshStandardMaterial();
  212. var geometry = new TeapotGeometry( size, segments, bottom, lid, body, fitLid, blinnScale );
  213. var mesh = new THREE.Mesh( geometry, material );
  214. mesh.name = 'Teapot';
  215. editor.addObject( mesh );
  216. editor.select( mesh );
  217. } );
  218. options.add( option );
  219. */
  220. //
  221. options.add( new UIHorizontalRule() );
  222. // AmbientLight
  223. var option = new UIRow();
  224. option.setClass( 'option' );
  225. option.setTextContent( strings.getKey( 'menubar/add/ambientlight' ) );
  226. option.onClick( function () {
  227. var color = 0x222222;
  228. var light = new THREE.AmbientLight( color );
  229. light.name = 'AmbientLight';
  230. editor.execute( new AddObjectCommand( editor, light ) );
  231. } );
  232. options.add( option );
  233. // DirectionalLight
  234. var option = new UIRow();
  235. option.setClass( 'option' );
  236. option.setTextContent( strings.getKey( 'menubar/add/directionallight' ) );
  237. option.onClick( function () {
  238. var color = 0xffffff;
  239. var intensity = 1;
  240. var light = new THREE.DirectionalLight( color, intensity );
  241. light.name = 'DirectionalLight';
  242. light.target.name = 'DirectionalLight Target';
  243. light.position.set( 5, 10, 7.5 );
  244. editor.execute( new AddObjectCommand( editor, light ) );
  245. } );
  246. options.add( option );
  247. // HemisphereLight
  248. var option = new UIRow();
  249. option.setClass( 'option' );
  250. option.setTextContent( strings.getKey( 'menubar/add/hemispherelight' ) );
  251. option.onClick( function () {
  252. var skyColor = 0x00aaff;
  253. var groundColor = 0xffaa00;
  254. var intensity = 1;
  255. var light = new THREE.HemisphereLight( skyColor, groundColor, intensity );
  256. light.name = 'HemisphereLight';
  257. light.position.set( 0, 10, 0 );
  258. editor.execute( new AddObjectCommand( editor, light ) );
  259. } );
  260. options.add( option );
  261. // PointLight
  262. var option = new UIRow();
  263. option.setClass( 'option' );
  264. option.setTextContent( strings.getKey( 'menubar/add/pointlight' ) );
  265. option.onClick( function () {
  266. var color = 0xffffff;
  267. var intensity = 1;
  268. var distance = 0;
  269. var light = new THREE.PointLight( color, intensity, distance );
  270. light.name = 'PointLight';
  271. editor.execute( new AddObjectCommand( editor, light ) );
  272. } );
  273. options.add( option );
  274. // SpotLight
  275. var option = new UIRow();
  276. option.setClass( 'option' );
  277. option.setTextContent( strings.getKey( 'menubar/add/spotlight' ) );
  278. option.onClick( function () {
  279. var color = 0xffffff;
  280. var intensity = 1;
  281. var distance = 0;
  282. var angle = Math.PI * 0.1;
  283. var penumbra = 0;
  284. var light = new THREE.SpotLight( color, intensity, distance, angle, penumbra );
  285. light.name = 'SpotLight';
  286. light.target.name = 'SpotLight Target';
  287. light.position.set( 5, 10, 7.5 );
  288. editor.execute( new AddObjectCommand( editor, light ) );
  289. } );
  290. options.add( option );
  291. //
  292. options.add( new UIHorizontalRule() );
  293. // OrthographicCamera
  294. var option = new UIRow();
  295. option.setClass( 'option' );
  296. option.setTextContent( strings.getKey( 'menubar/add/orthographiccamera' ) );
  297. option.onClick( function () {
  298. var aspect = editor.camera.aspect;
  299. var camera = new THREE.OrthographicCamera( - aspect, aspect );
  300. camera.name = 'OrthographicCamera';
  301. editor.execute( new AddObjectCommand( editor, camera ) );
  302. } );
  303. options.add( option );
  304. // PerspectiveCamera
  305. var option = new UIRow();
  306. option.setClass( 'option' );
  307. option.setTextContent( strings.getKey( 'menubar/add/perspectivecamera' ) );
  308. option.onClick( function () {
  309. var camera = new THREE.PerspectiveCamera();
  310. camera.name = 'PerspectiveCamera';
  311. editor.execute( new AddObjectCommand( editor, camera ) );
  312. } );
  313. options.add( option );
  314. return container;
  315. }
  316. export { MenubarAdd };