1
0

Menubar.Add.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. import * as THREE from 'three';
  2. import { UIPanel, UIRow } from './libs/ui.js';
  3. import { AddObjectCommand } from './commands/AddObjectCommand.js';
  4. import { FontLoader } from 'three/addons/loaders/FontLoader.js';
  5. import { TextGeometry } from 'three/addons/geometries/TextGeometry.js';
  6. function MenubarAdd( editor ) {
  7. const strings = editor.strings;
  8. const container = new UIPanel();
  9. container.setClass( 'menu' );
  10. const title = new UIPanel();
  11. title.setClass( 'title' );
  12. title.setTextContent( strings.getKey( 'menubar/add' ) );
  13. container.add( title );
  14. const options = new UIPanel();
  15. options.setClass( 'options' );
  16. container.add( options );
  17. // Group
  18. let option = new UIRow();
  19. option.setClass( 'option' );
  20. option.setTextContent( strings.getKey( 'menubar/add/group' ) );
  21. option.onClick( function () {
  22. const mesh = new THREE.Group();
  23. mesh.name = 'Group';
  24. editor.execute( new AddObjectCommand( editor, mesh ) );
  25. } );
  26. options.add( option );
  27. // Mesh
  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. // Mesh / Box
  44. option = new UIRow();
  45. option.setClass( 'option' );
  46. option.setTextContent( strings.getKey( 'menubar/add/mesh/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. // Mesh / Capsule
  55. option = new UIRow();
  56. option.setClass( 'option' );
  57. option.setTextContent( strings.getKey( 'menubar/add/mesh/capsule' ) );
  58. option.onClick( function () {
  59. const geometry = new THREE.CapsuleGeometry( 1, 1, 4, 8, 1 );
  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. // Mesh / Circle
  67. option = new UIRow();
  68. option.setClass( 'option' );
  69. option.setTextContent( strings.getKey( 'menubar/add/mesh/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. // Mesh / Cylinder
  78. option = new UIRow();
  79. option.setClass( 'option' );
  80. option.setTextContent( strings.getKey( 'menubar/add/mesh/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. // Mesh / Dodecahedron
  89. option = new UIRow();
  90. option.setClass( 'option' );
  91. option.setTextContent( strings.getKey( 'menubar/add/mesh/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. // Mesh / Icosahedron
  100. option = new UIRow();
  101. option.setClass( 'option' );
  102. option.setTextContent( strings.getKey( 'menubar/add/mesh/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. // Mesh / Lathe
  111. option = new UIRow();
  112. option.setClass( 'option' );
  113. option.setTextContent( strings.getKey( 'menubar/add/mesh/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. // Mesh / Octahedron
  122. option = new UIRow();
  123. option.setClass( 'option' );
  124. option.setTextContent( strings.getKey( 'menubar/add/mesh/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. // Mesh / Plane
  133. option = new UIRow();
  134. option.setClass( 'option' );
  135. option.setTextContent( strings.getKey( 'menubar/add/mesh/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. // Mesh / Ring
  145. option = new UIRow();
  146. option.setClass( 'option' );
  147. option.setTextContent( strings.getKey( 'menubar/add/mesh/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. // Mesh / Sphere
  156. option = new UIRow();
  157. option.setClass( 'option' );
  158. option.setTextContent( strings.getKey( 'menubar/add/mesh/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. // Mesh / Sprite
  167. option = new UIRow();
  168. option.setClass( 'option' );
  169. option.setTextContent( strings.getKey( 'menubar/add/mesh/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. // Mesh / Tetrahedron
  177. option = new UIRow();
  178. option.setClass( 'option' );
  179. option.setTextContent( strings.getKey( 'menubar/add/mesh/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. // Mesh / Text
  188. option = new UIRow();
  189. option.setClass( 'option' );
  190. option.setTextContent( strings.getKey( 'menubar/add/text' ) );
  191. option.onClick( function () {
  192. const loader = new FontLoader();
  193. loader.load( '../examples/fonts/helvetiker_bold.typeface.json', function ( font ) {
  194. const text = 'THREE.JS';
  195. const geometry = new TextGeometry( text, {
  196. text: text,
  197. font,
  198. size: 1,
  199. depth: 0.5,
  200. curveSegments: 4,
  201. bevelEnabled: false,
  202. bevelThickness: 0.1,
  203. bevelSize: 0.01,
  204. bevelOffset: 0,
  205. bevelSegments: 3
  206. } );
  207. const mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  208. mesh.name = 'Text';
  209. editor.execute( new AddObjectCommand( editor, mesh ) );
  210. } );
  211. } );
  212. meshSubmenu.add( option );
  213. // Mesh / Torus
  214. option = new UIRow();
  215. option.setClass( 'option' );
  216. option.setTextContent( strings.getKey( 'menubar/add/mesh/torus' ) );
  217. option.onClick( function () {
  218. const geometry = new THREE.TorusGeometry( 1, 0.4, 12, 48, Math.PI * 2 );
  219. const mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  220. mesh.name = 'Torus';
  221. editor.execute( new AddObjectCommand( editor, mesh ) );
  222. } );
  223. meshSubmenu.add( option );
  224. // Mesh / TorusKnot
  225. option = new UIRow();
  226. option.setClass( 'option' );
  227. option.setTextContent( strings.getKey( 'menubar/add/mesh/torusknot' ) );
  228. option.onClick( function () {
  229. const geometry = new THREE.TorusKnotGeometry( 1, 0.4, 64, 8, 2, 3 );
  230. const mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  231. mesh.name = 'TorusKnot';
  232. editor.execute( new AddObjectCommand( editor, mesh ) );
  233. } );
  234. meshSubmenu.add( option );
  235. // Mesh / Tube
  236. option = new UIRow();
  237. option.setClass( 'option' );
  238. option.setTextContent( strings.getKey( 'menubar/add/mesh/tube' ) );
  239. option.onClick( function () {
  240. const path = new THREE.CatmullRomCurve3( [
  241. new THREE.Vector3( 2, 2, - 2 ),
  242. new THREE.Vector3( 2, - 2, - 0.6666666666666667 ),
  243. new THREE.Vector3( - 2, - 2, 0.6666666666666667 ),
  244. new THREE.Vector3( - 2, 2, 2 )
  245. ] );
  246. const geometry = new THREE.TubeGeometry( path, 64, 1, 8, false );
  247. const mesh = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  248. mesh.name = 'Tube';
  249. editor.execute( new AddObjectCommand( editor, mesh ) );
  250. } );
  251. meshSubmenu.add( option );
  252. // Light
  253. const lightSubmenuTitle = new UIRow().setTextContent( strings.getKey( 'menubar/add/light' ) ).addClass( 'option' ).addClass( 'submenu-title' );
  254. lightSubmenuTitle.onMouseOver( function () {
  255. const { top, right } = lightSubmenuTitle.dom.getBoundingClientRect();
  256. const { paddingTop } = getComputedStyle( this.dom );
  257. lightSubmenu.setLeft( right + 'px' );
  258. lightSubmenu.setTop( top - parseFloat( paddingTop ) + 'px' );
  259. lightSubmenu.setStyle( 'max-height', [ `calc( 100vh - ${top}px )` ] );
  260. lightSubmenu.setDisplay( 'block' );
  261. } );
  262. lightSubmenuTitle.onMouseOut( function () {
  263. lightSubmenu.setDisplay( 'none' );
  264. } );
  265. options.add( lightSubmenuTitle );
  266. const lightSubmenu = new UIPanel().setPosition( 'fixed' ).addClass( 'options' ).setDisplay( 'none' );
  267. lightSubmenuTitle.add( lightSubmenu );
  268. // Light / Ambient
  269. option = new UIRow();
  270. option.setClass( 'option' );
  271. option.setTextContent( strings.getKey( 'menubar/add/light/ambient' ) );
  272. option.onClick( function () {
  273. const color = 0x222222;
  274. const light = new THREE.AmbientLight( color );
  275. light.name = 'AmbientLight';
  276. editor.execute( new AddObjectCommand( editor, light ) );
  277. } );
  278. lightSubmenu.add( option );
  279. // Light / Directional
  280. option = new UIRow();
  281. option.setClass( 'option' );
  282. option.setTextContent( strings.getKey( 'menubar/add/light/directional' ) );
  283. option.onClick( function () {
  284. const color = 0xffffff;
  285. const intensity = 1;
  286. const light = new THREE.DirectionalLight( color, intensity );
  287. light.name = 'DirectionalLight';
  288. light.target.name = 'DirectionalLight Target';
  289. light.position.set( 5, 10, 7.5 );
  290. editor.execute( new AddObjectCommand( editor, light ) );
  291. } );
  292. lightSubmenu.add( option );
  293. // Light / Hemisphere
  294. option = new UIRow();
  295. option.setClass( 'option' );
  296. option.setTextContent( strings.getKey( 'menubar/add/light/hemisphere' ) );
  297. option.onClick( function () {
  298. const skyColor = 0x00aaff;
  299. const groundColor = 0xffaa00;
  300. const intensity = 1;
  301. const light = new THREE.HemisphereLight( skyColor, groundColor, intensity );
  302. light.name = 'HemisphereLight';
  303. light.position.set( 0, 10, 0 );
  304. editor.execute( new AddObjectCommand( editor, light ) );
  305. } );
  306. lightSubmenu.add( option );
  307. // Light / Point
  308. option = new UIRow();
  309. option.setClass( 'option' );
  310. option.setTextContent( strings.getKey( 'menubar/add/light/point' ) );
  311. option.onClick( function () {
  312. const color = 0xffffff;
  313. const intensity = 1;
  314. const distance = 0;
  315. const light = new THREE.PointLight( color, intensity, distance );
  316. light.name = 'PointLight';
  317. editor.execute( new AddObjectCommand( editor, light ) );
  318. } );
  319. lightSubmenu.add( option );
  320. // Light / Spot
  321. option = new UIRow();
  322. option.setClass( 'option' );
  323. option.setTextContent( strings.getKey( 'menubar/add/light/spot' ) );
  324. option.onClick( function () {
  325. const color = 0xffffff;
  326. const intensity = 1;
  327. const distance = 0;
  328. const angle = Math.PI * 0.1;
  329. const penumbra = 0;
  330. const light = new THREE.SpotLight( color, intensity, distance, angle, penumbra );
  331. light.name = 'SpotLight';
  332. light.target.name = 'SpotLight Target';
  333. light.position.set( 5, 10, 7.5 );
  334. editor.execute( new AddObjectCommand( editor, light ) );
  335. } );
  336. lightSubmenu.add( option );
  337. // Camera
  338. const cameraSubmenuTitle = new UIRow().setTextContent( strings.getKey( 'menubar/add/camera' ) ).addClass( 'option' ).addClass( 'submenu-title' );
  339. cameraSubmenuTitle.onMouseOver( function () {
  340. const { top, right } = cameraSubmenuTitle.dom.getBoundingClientRect();
  341. const { paddingTop } = getComputedStyle( this.dom );
  342. cameraSubmenu.setLeft( right + 'px' );
  343. cameraSubmenu.setTop( top - parseFloat( paddingTop ) + 'px' );
  344. cameraSubmenu.setStyle( 'max-height', [ `calc( 100vh - ${top}px )` ] );
  345. cameraSubmenu.setDisplay( 'block' );
  346. } );
  347. cameraSubmenuTitle.onMouseOut( function () {
  348. cameraSubmenu.setDisplay( 'none' );
  349. } );
  350. options.add( cameraSubmenuTitle );
  351. const cameraSubmenu = new UIPanel().setPosition( 'fixed' ).addClass( 'options' ).setDisplay( 'none' );
  352. cameraSubmenuTitle.add( cameraSubmenu );
  353. // Camera / Orthographic
  354. option = new UIRow();
  355. option.setClass( 'option' );
  356. option.setTextContent( strings.getKey( 'menubar/add/camera/orthographic' ) );
  357. option.onClick( function () {
  358. const aspect = editor.camera.aspect;
  359. const camera = new THREE.OrthographicCamera( - aspect, aspect );
  360. camera.name = 'OrthographicCamera';
  361. editor.execute( new AddObjectCommand( editor, camera ) );
  362. } );
  363. cameraSubmenu.add( option );
  364. // Camera / Perspective
  365. option = new UIRow();
  366. option.setClass( 'option' );
  367. option.setTextContent( strings.getKey( 'menubar/add/camera/perspective' ) );
  368. option.onClick( function () {
  369. const camera = new THREE.PerspectiveCamera();
  370. camera.name = 'PerspectiveCamera';
  371. editor.execute( new AddObjectCommand( editor, camera ) );
  372. } );
  373. cameraSubmenu.add( option );
  374. return container;
  375. }
  376. export { MenubarAdd };
粤ICP备19079148号