Menubar.Add.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. import * as THREE from 'three';
  2. import { UIPanel, UIRow } from './libs/ui.js';
  3. import { AddObjectCommand } from './commands/AddObjectCommand.js';
  4. import { MultiCmdsCommand } from './commands/MultiCmdsCommand.js';
  5. import { FontLoader } from 'three/addons/loaders/FontLoader.js';
  6. import { TextGeometry } from 'three/addons/geometries/TextGeometry.js';
  7. function MenubarAdd( editor ) {
  8. const strings = editor.strings;
  9. const container = new UIPanel();
  10. container.setClass( 'menu' );
  11. const title = new UIPanel();
  12. title.setClass( 'title' );
  13. title.setTextContent( strings.getKey( 'menubar/add' ) );
  14. container.add( title );
  15. const options = new UIPanel();
  16. options.setClass( 'options' );
  17. container.add( options );
  18. // Group
  19. let option = new UIRow();
  20. option.setClass( 'option' );
  21. option.setTextContent( strings.getKey( 'menubar/add/group' ) );
  22. option.onClick( function () {
  23. const mesh = new THREE.Group();
  24. mesh.name = 'Group';
  25. editor.execute( new AddObjectCommand( editor, mesh ) );
  26. } );
  27. options.add( option );
  28. // Mesh
  29. const meshSubmenuTitle = new UIRow().setTextContent( strings.getKey( 'menubar/add/mesh' ) ).addClass( 'option' ).addClass( 'submenu-title' );
  30. meshSubmenuTitle.onMouseOver( function () {
  31. const { top, right } = meshSubmenuTitle.dom.getBoundingClientRect();
  32. const { paddingTop } = getComputedStyle( this.dom );
  33. meshSubmenu.setLeft( right + 'px' );
  34. meshSubmenu.setTop( top - parseFloat( paddingTop ) + 'px' );
  35. meshSubmenu.setStyle( 'max-height', [ `calc( 100vh - ${top}px )` ] );
  36. meshSubmenu.setDisplay( 'block' );
  37. } );
  38. meshSubmenuTitle.onMouseOut( function () {
  39. meshSubmenu.setDisplay( 'none' );
  40. } );
  41. options.add( meshSubmenuTitle );
  42. const meshSubmenu = new UIPanel().setPosition( 'fixed' ).addClass( 'options' ).setDisplay( 'none' );
  43. meshSubmenuTitle.add( meshSubmenu );
  44. // Mesh / Box
  45. option = new UIRow();
  46. option.setClass( 'option' );
  47. option.setTextContent( strings.getKey( 'menubar/add/mesh/box' ) );
  48. option.onClick( function () {
  49. const geometry = new THREE.BoxGeometry( 1, 1, 1, 1, 1, 1 );
  50. const mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  51. mesh.name = 'Box';
  52. editor.execute( new AddObjectCommand( editor, mesh ) );
  53. } );
  54. meshSubmenu.add( option );
  55. // Mesh / Capsule
  56. option = new UIRow();
  57. option.setClass( 'option' );
  58. option.setTextContent( strings.getKey( 'menubar/add/mesh/capsule' ) );
  59. option.onClick( function () {
  60. const geometry = new THREE.CapsuleGeometry( 1, 1, 4, 8, 1 );
  61. const material = new THREE.MeshStandardMaterial();
  62. const mesh = new THREE.Mesh( geometry, material );
  63. mesh.name = 'Capsule';
  64. editor.execute( new AddObjectCommand( editor, mesh ) );
  65. } );
  66. meshSubmenu.add( option );
  67. // Mesh / Circle
  68. option = new UIRow();
  69. option.setClass( 'option' );
  70. option.setTextContent( strings.getKey( 'menubar/add/mesh/circle' ) );
  71. option.onClick( function () {
  72. const geometry = new THREE.CircleGeometry( 1, 32, 0, Math.PI * 2 );
  73. const mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  74. mesh.name = 'Circle';
  75. editor.execute( new AddObjectCommand( editor, mesh ) );
  76. } );
  77. meshSubmenu.add( option );
  78. // Mesh / Cylinder
  79. option = new UIRow();
  80. option.setClass( 'option' );
  81. option.setTextContent( strings.getKey( 'menubar/add/mesh/cylinder' ) );
  82. option.onClick( function () {
  83. const geometry = new THREE.CylinderGeometry( 1, 1, 1, 32, 1, false, 0, Math.PI * 2 );
  84. const mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  85. mesh.name = 'Cylinder';
  86. editor.execute( new AddObjectCommand( editor, mesh ) );
  87. } );
  88. meshSubmenu.add( option );
  89. // Mesh / Dodecahedron
  90. option = new UIRow();
  91. option.setClass( 'option' );
  92. option.setTextContent( strings.getKey( 'menubar/add/mesh/dodecahedron' ) );
  93. option.onClick( function () {
  94. const geometry = new THREE.DodecahedronGeometry( 1, 0 );
  95. const mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  96. mesh.name = 'Dodecahedron';
  97. editor.execute( new AddObjectCommand( editor, mesh ) );
  98. } );
  99. meshSubmenu.add( option );
  100. // Mesh / Icosahedron
  101. option = new UIRow();
  102. option.setClass( 'option' );
  103. option.setTextContent( strings.getKey( 'menubar/add/mesh/icosahedron' ) );
  104. option.onClick( function () {
  105. const geometry = new THREE.IcosahedronGeometry( 1, 0 );
  106. const mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  107. mesh.name = 'Icosahedron';
  108. editor.execute( new AddObjectCommand( editor, mesh ) );
  109. } );
  110. meshSubmenu.add( option );
  111. // Mesh / Lathe
  112. option = new UIRow();
  113. option.setClass( 'option' );
  114. option.setTextContent( strings.getKey( 'menubar/add/mesh/lathe' ) );
  115. option.onClick( function () {
  116. const geometry = new THREE.LatheGeometry();
  117. const mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial( { side: THREE.DoubleSide } ) );
  118. mesh.name = 'Lathe';
  119. editor.execute( new AddObjectCommand( editor, mesh ) );
  120. } );
  121. meshSubmenu.add( option );
  122. // Mesh / Octahedron
  123. option = new UIRow();
  124. option.setClass( 'option' );
  125. option.setTextContent( strings.getKey( 'menubar/add/mesh/octahedron' ) );
  126. option.onClick( function () {
  127. const geometry = new THREE.OctahedronGeometry( 1, 0 );
  128. const mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  129. mesh.name = 'Octahedron';
  130. editor.execute( new AddObjectCommand( editor, mesh ) );
  131. } );
  132. meshSubmenu.add( option );
  133. // Mesh / Plane
  134. option = new UIRow();
  135. option.setClass( 'option' );
  136. option.setTextContent( strings.getKey( 'menubar/add/mesh/plane' ) );
  137. option.onClick( function () {
  138. const geometry = new THREE.PlaneGeometry( 1, 1, 1, 1 );
  139. const material = new THREE.MeshStandardMaterial();
  140. const mesh = new THREE.Mesh( geometry, material );
  141. mesh.name = 'Plane';
  142. editor.execute( new AddObjectCommand( editor, mesh ) );
  143. } );
  144. meshSubmenu.add( option );
  145. // Mesh / Ring
  146. option = new UIRow();
  147. option.setClass( 'option' );
  148. option.setTextContent( strings.getKey( 'menubar/add/mesh/ring' ) );
  149. option.onClick( function () {
  150. const geometry = new THREE.RingGeometry( 0.5, 1, 32, 1, 0, Math.PI * 2 );
  151. const mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  152. mesh.name = 'Ring';
  153. editor.execute( new AddObjectCommand( editor, mesh ) );
  154. } );
  155. meshSubmenu.add( option );
  156. // Mesh / Sphere
  157. option = new UIRow();
  158. option.setClass( 'option' );
  159. option.setTextContent( strings.getKey( 'menubar/add/mesh/sphere' ) );
  160. option.onClick( function () {
  161. const geometry = new THREE.SphereGeometry( 1, 32, 16, 0, Math.PI * 2, 0, Math.PI );
  162. const mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  163. mesh.name = 'Sphere';
  164. editor.execute( new AddObjectCommand( editor, mesh ) );
  165. } );
  166. meshSubmenu.add( option );
  167. // Mesh / Sprite
  168. option = new UIRow();
  169. option.setClass( 'option' );
  170. option.setTextContent( strings.getKey( 'menubar/add/mesh/sprite' ) );
  171. option.onClick( function () {
  172. const sprite = new THREE.Sprite( new THREE.SpriteMaterial() );
  173. sprite.name = 'Sprite';
  174. editor.execute( new AddObjectCommand( editor, sprite ) );
  175. } );
  176. meshSubmenu.add( option );
  177. // Mesh / Tetrahedron
  178. option = new UIRow();
  179. option.setClass( 'option' );
  180. option.setTextContent( strings.getKey( 'menubar/add/mesh/tetrahedron' ) );
  181. option.onClick( function () {
  182. const geometry = new THREE.TetrahedronGeometry( 1, 0 );
  183. const mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  184. mesh.name = 'Tetrahedron';
  185. editor.execute( new AddObjectCommand( editor, mesh ) );
  186. } );
  187. meshSubmenu.add( option );
  188. // Mesh / Text
  189. option = new UIRow();
  190. option.setClass( 'option' );
  191. option.setTextContent( strings.getKey( 'menubar/add/text' ) );
  192. option.onClick( function () {
  193. const loader = new FontLoader();
  194. loader.load( '../examples/fonts/helvetiker_bold.typeface.json', function ( font ) {
  195. const text = 'THREE.JS';
  196. const geometry = new TextGeometry( text, {
  197. text: text,
  198. font,
  199. size: 1,
  200. depth: 0.5,
  201. curveSegments: 4,
  202. bevelEnabled: false,
  203. bevelThickness: 0.1,
  204. bevelSize: 0.01,
  205. bevelOffset: 0,
  206. bevelSegments: 3
  207. } );
  208. const mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  209. mesh.name = 'Text';
  210. editor.execute( new AddObjectCommand( editor, mesh ) );
  211. } );
  212. } );
  213. meshSubmenu.add( option );
  214. // Mesh / Torus
  215. option = new UIRow();
  216. option.setClass( 'option' );
  217. option.setTextContent( strings.getKey( 'menubar/add/mesh/torus' ) );
  218. option.onClick( function () {
  219. const geometry = new THREE.TorusGeometry( 1, 0.4, 12, 48, Math.PI * 2 );
  220. const mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  221. mesh.name = 'Torus';
  222. editor.execute( new AddObjectCommand( editor, mesh ) );
  223. } );
  224. meshSubmenu.add( option );
  225. // Mesh / TorusKnot
  226. option = new UIRow();
  227. option.setClass( 'option' );
  228. option.setTextContent( strings.getKey( 'menubar/add/mesh/torusknot' ) );
  229. option.onClick( function () {
  230. const geometry = new THREE.TorusKnotGeometry( 1, 0.4, 64, 8, 2, 3 );
  231. const mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  232. mesh.name = 'TorusKnot';
  233. editor.execute( new AddObjectCommand( editor, mesh ) );
  234. } );
  235. meshSubmenu.add( option );
  236. // Mesh / Tube
  237. option = new UIRow();
  238. option.setClass( 'option' );
  239. option.setTextContent( strings.getKey( 'menubar/add/mesh/tube' ) );
  240. option.onClick( function () {
  241. const path = new THREE.CatmullRomCurve3( [
  242. new THREE.Vector3( 2, 2, - 2 ),
  243. new THREE.Vector3( 2, - 2, - 0.6666666666666667 ),
  244. new THREE.Vector3( - 2, - 2, 0.6666666666666667 ),
  245. new THREE.Vector3( - 2, 2, 2 )
  246. ] );
  247. const geometry = new THREE.TubeGeometry( path, 64, 1, 8, false );
  248. const mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  249. mesh.name = 'Tube';
  250. editor.execute( new AddObjectCommand( editor, mesh ) );
  251. } );
  252. meshSubmenu.add( option );
  253. // Light
  254. const lightSubmenuTitle = new UIRow().setTextContent( strings.getKey( 'menubar/add/light' ) ).addClass( 'option' ).addClass( 'submenu-title' );
  255. lightSubmenuTitle.onMouseOver( function () {
  256. const { top, right } = lightSubmenuTitle.dom.getBoundingClientRect();
  257. const { paddingTop } = getComputedStyle( this.dom );
  258. lightSubmenu.setLeft( right + 'px' );
  259. lightSubmenu.setTop( top - parseFloat( paddingTop ) + 'px' );
  260. lightSubmenu.setStyle( 'max-height', [ `calc( 100vh - ${top}px )` ] );
  261. lightSubmenu.setDisplay( 'block' );
  262. } );
  263. lightSubmenuTitle.onMouseOut( function () {
  264. lightSubmenu.setDisplay( 'none' );
  265. } );
  266. options.add( lightSubmenuTitle );
  267. const lightSubmenu = new UIPanel().setPosition( 'fixed' ).addClass( 'options' ).setDisplay( 'none' );
  268. lightSubmenuTitle.add( lightSubmenu );
  269. // Light / Ambient
  270. option = new UIRow();
  271. option.setClass( 'option' );
  272. option.setTextContent( strings.getKey( 'menubar/add/light/ambient' ) );
  273. option.onClick( function () {
  274. const color = 0x222222;
  275. const light = new THREE.AmbientLight( color );
  276. light.name = 'AmbientLight';
  277. editor.execute( new AddObjectCommand( editor, light ) );
  278. } );
  279. lightSubmenu.add( option );
  280. // Light / Directional
  281. option = new UIRow();
  282. option.setClass( 'option' );
  283. option.setTextContent( strings.getKey( 'menubar/add/light/directional' ) );
  284. option.onClick( function () {
  285. const color = 0xffffff;
  286. const intensity = 1;
  287. const light = new THREE.DirectionalLight( color, intensity );
  288. light.name = 'DirectionalLight';
  289. light.target.name = 'DirectionalLight Target';
  290. light.position.set( 5, 10, 7.5 );
  291. editor.execute( new MultiCmdsCommand( editor, [
  292. new AddObjectCommand( editor, light.target ),
  293. new AddObjectCommand( editor, light )
  294. ] ) );
  295. } );
  296. lightSubmenu.add( option );
  297. // Light / Hemisphere
  298. option = new UIRow();
  299. option.setClass( 'option' );
  300. option.setTextContent( strings.getKey( 'menubar/add/light/hemisphere' ) );
  301. option.onClick( function () {
  302. const skyColor = 0x00aaff;
  303. const groundColor = 0xffaa00;
  304. const intensity = 1;
  305. const light = new THREE.HemisphereLight( skyColor, groundColor, intensity );
  306. light.name = 'HemisphereLight';
  307. light.position.set( 0, 10, 0 );
  308. editor.execute( new AddObjectCommand( editor, light ) );
  309. } );
  310. lightSubmenu.add( option );
  311. // Light / Point
  312. option = new UIRow();
  313. option.setClass( 'option' );
  314. option.setTextContent( strings.getKey( 'menubar/add/light/point' ) );
  315. option.onClick( function () {
  316. const color = 0xffffff;
  317. const intensity = 1;
  318. const distance = 0;
  319. const light = new THREE.PointLight( color, intensity, distance );
  320. light.name = 'PointLight';
  321. editor.execute( new AddObjectCommand( editor, light ) );
  322. } );
  323. lightSubmenu.add( option );
  324. // Light / Spot
  325. option = new UIRow();
  326. option.setClass( 'option' );
  327. option.setTextContent( strings.getKey( 'menubar/add/light/spot' ) );
  328. option.onClick( function () {
  329. const color = 0xffffff;
  330. const intensity = 1;
  331. const distance = 0;
  332. const angle = Math.PI * 0.1;
  333. const penumbra = 0;
  334. const light = new THREE.SpotLight( color, intensity, distance, angle, penumbra );
  335. light.name = 'SpotLight';
  336. light.target.name = 'SpotLight Target';
  337. light.position.set( 5, 10, 7.5 );
  338. editor.execute( new MultiCmdsCommand( editor, [
  339. new AddObjectCommand( editor, light.target ),
  340. new AddObjectCommand( editor, light )
  341. ] ) );
  342. } );
  343. lightSubmenu.add( option );
  344. // Camera
  345. const cameraSubmenuTitle = new UIRow().setTextContent( strings.getKey( 'menubar/add/camera' ) ).addClass( 'option' ).addClass( 'submenu-title' );
  346. cameraSubmenuTitle.onMouseOver( function () {
  347. const { top, right } = cameraSubmenuTitle.dom.getBoundingClientRect();
  348. const { paddingTop } = getComputedStyle( this.dom );
  349. cameraSubmenu.setLeft( right + 'px' );
  350. cameraSubmenu.setTop( top - parseFloat( paddingTop ) + 'px' );
  351. cameraSubmenu.setStyle( 'max-height', [ `calc( 100vh - ${top}px )` ] );
  352. cameraSubmenu.setDisplay( 'block' );
  353. } );
  354. cameraSubmenuTitle.onMouseOut( function () {
  355. cameraSubmenu.setDisplay( 'none' );
  356. } );
  357. options.add( cameraSubmenuTitle );
  358. const cameraSubmenu = new UIPanel().setPosition( 'fixed' ).addClass( 'options' ).setDisplay( 'none' );
  359. cameraSubmenuTitle.add( cameraSubmenu );
  360. // Camera / Orthographic
  361. option = new UIRow();
  362. option.setClass( 'option' );
  363. option.setTextContent( strings.getKey( 'menubar/add/camera/orthographic' ) );
  364. option.onClick( function () {
  365. const aspect = editor.camera.isPerspectiveCamera
  366. ? editor.camera.aspect
  367. : ( editor.camera.right - editor.camera.left ) / ( editor.camera.top - editor.camera.bottom );
  368. const camera = new THREE.OrthographicCamera( - aspect, aspect );
  369. camera.name = 'OrthographicCamera';
  370. editor.execute( new AddObjectCommand( editor, camera ) );
  371. } );
  372. cameraSubmenu.add( option );
  373. // Camera / Perspective
  374. option = new UIRow();
  375. option.setClass( 'option' );
  376. option.setTextContent( strings.getKey( 'menubar/add/camera/perspective' ) );
  377. option.onClick( function () {
  378. const camera = new THREE.PerspectiveCamera();
  379. camera.name = 'PerspectiveCamera';
  380. editor.execute( new AddObjectCommand( editor, camera ) );
  381. } );
  382. cameraSubmenu.add( option );
  383. return container;
  384. }
  385. export { MenubarAdd };
粤ICP备19079148号