Menubar.Add.js 12 KB

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