Menubar.Add.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. options.add( new UIHorizontalRule() );
  212. // AmbientLight
  213. option = new UIRow();
  214. option.setClass( 'option' );
  215. option.setTextContent( strings.getKey( 'menubar/add/ambientlight' ) );
  216. option.onClick( function () {
  217. const color = 0x222222;
  218. const light = new THREE.AmbientLight( color );
  219. light.name = 'AmbientLight';
  220. editor.execute( new AddObjectCommand( editor, light ) );
  221. } );
  222. options.add( option );
  223. // DirectionalLight
  224. option = new UIRow();
  225. option.setClass( 'option' );
  226. option.setTextContent( strings.getKey( 'menubar/add/directionallight' ) );
  227. option.onClick( function () {
  228. const color = 0xffffff;
  229. const intensity = 1;
  230. const light = new THREE.DirectionalLight( color, intensity );
  231. light.name = 'DirectionalLight';
  232. light.target.name = 'DirectionalLight Target';
  233. light.position.set( 5, 10, 7.5 );
  234. editor.execute( new AddObjectCommand( editor, light ) );
  235. } );
  236. options.add( option );
  237. // HemisphereLight
  238. option = new UIRow();
  239. option.setClass( 'option' );
  240. option.setTextContent( strings.getKey( 'menubar/add/hemispherelight' ) );
  241. option.onClick( function () {
  242. const skyColor = 0x00aaff;
  243. const groundColor = 0xffaa00;
  244. const intensity = 1;
  245. const light = new THREE.HemisphereLight( skyColor, groundColor, intensity );
  246. light.name = 'HemisphereLight';
  247. light.position.set( 0, 10, 0 );
  248. editor.execute( new AddObjectCommand( editor, light ) );
  249. } );
  250. options.add( option );
  251. // PointLight
  252. option = new UIRow();
  253. option.setClass( 'option' );
  254. option.setTextContent( strings.getKey( 'menubar/add/pointlight' ) );
  255. option.onClick( function () {
  256. const color = 0xffffff;
  257. const intensity = 1;
  258. const distance = 0;
  259. const light = new THREE.PointLight( color, intensity, distance );
  260. light.name = 'PointLight';
  261. editor.execute( new AddObjectCommand( editor, light ) );
  262. } );
  263. options.add( option );
  264. // SpotLight
  265. option = new UIRow();
  266. option.setClass( 'option' );
  267. option.setTextContent( strings.getKey( 'menubar/add/spotlight' ) );
  268. option.onClick( function () {
  269. const color = 0xffffff;
  270. const intensity = 1;
  271. const distance = 0;
  272. const angle = Math.PI * 0.1;
  273. const penumbra = 0;
  274. const light = new THREE.SpotLight( color, intensity, distance, angle, penumbra );
  275. light.name = 'SpotLight';
  276. light.target.name = 'SpotLight Target';
  277. light.position.set( 5, 10, 7.5 );
  278. editor.execute( new AddObjectCommand( editor, light ) );
  279. } );
  280. options.add( option );
  281. //
  282. options.add( new UIHorizontalRule() );
  283. // OrthographicCamera
  284. option = new UIRow();
  285. option.setClass( 'option' );
  286. option.setTextContent( strings.getKey( 'menubar/add/orthographiccamera' ) );
  287. option.onClick( function () {
  288. const aspect = editor.camera.aspect;
  289. const camera = new THREE.OrthographicCamera( - aspect, aspect );
  290. camera.name = 'OrthographicCamera';
  291. editor.execute( new AddObjectCommand( editor, camera ) );
  292. } );
  293. options.add( option );
  294. // PerspectiveCamera
  295. option = new UIRow();
  296. option.setClass( 'option' );
  297. option.setTextContent( strings.getKey( 'menubar/add/perspectivecamera' ) );
  298. option.onClick( function () {
  299. const camera = new THREE.PerspectiveCamera();
  300. camera.name = 'PerspectiveCamera';
  301. editor.execute( new AddObjectCommand( editor, camera ) );
  302. } );
  303. options.add( option );
  304. return container;
  305. }
  306. export { MenubarAdd };