Menubar.Add.js 12 KB

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