EditorView.as 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. // Urho3D editor view & camera functions
  2. Node@ cameraNode;
  3. Camera@ camera;
  4. enum EditMode
  5. {
  6. EDIT_MOVE = 0,
  7. EDIT_ROTATE,
  8. EDIT_SCALE,
  9. EDIT_SELECT
  10. }
  11. enum AxisMode
  12. {
  13. AXIS_WORLD = 0,
  14. AXIS_LOCAL
  15. }
  16. Text@ editorModeText;
  17. Text@ renderStatsText;
  18. Text@ cameraPosText;
  19. EditMode editMode = EDIT_MOVE;
  20. AxisMode axisMode = AXIS_WORLD;
  21. FillMode fillMode = FILL_SOLID;
  22. float cameraBaseSpeed = 10;
  23. float cameraBaseRotationSpeed = 0.2;
  24. float cameraShiftSpeedMultiplier = 5;
  25. float cameraYaw = 0;
  26. float cameraPitch = 0;
  27. float newNodeDistance = 20;
  28. float moveStep = 0.5;
  29. float rotateStep = 5;
  30. float scaleStep = 0.1;
  31. bool moveSnap = false;
  32. bool rotateSnap = false;
  33. bool scaleSnap = false;
  34. bool renderingDebug = false;
  35. bool physicsDebug = false;
  36. bool octreeDebug = false;
  37. int pickMode = PICK_GEOMETRIES;
  38. Array<int> pickModeDrawableFlags = {
  39. DRAWABLE_GEOMETRY,
  40. DRAWABLE_LIGHT,
  41. DRAWABLE_ZONE
  42. };
  43. Array<String> editModeText = {
  44. "Move",
  45. "Rotate",
  46. "Scale",
  47. "Select"
  48. };
  49. Array<String> axisModeText = {
  50. "World",
  51. "Local"
  52. };
  53. Array<String> pickModeText = {
  54. "Geometries",
  55. "Lights",
  56. "Zones",
  57. "Rigidbodies"
  58. };
  59. Array<String> fillModeText = {
  60. "Solid",
  61. "Wire",
  62. "Point"
  63. };
  64. void CreateCamera()
  65. {
  66. // Note: the camera will not be bound into the scene, so that it is not listed, and does not get deleted
  67. cameraNode = Node();
  68. camera = cameraNode.CreateComponent("Camera");
  69. audio.listener = cameraNode.CreateComponent("SoundListener");
  70. ResetCamera();
  71. renderer.viewports[0] = Viewport(editorScene, camera);
  72. SubscribeToEvent("PostRenderUpdate", "HandlePostRenderUpdate");
  73. SubscribeToEvent("UIMouseClick", "ViewMouseClick");
  74. }
  75. void ResetCamera()
  76. {
  77. cameraNode.position = Vector3(0, 10, 0);
  78. cameraNode.rotation = Quaternion();
  79. cameraPitch = 0;
  80. cameraYaw = 0;
  81. }
  82. void CreateStatsBar()
  83. {
  84. Font@ font = cache.GetResource("Font", "Fonts/Anonymous Pro.ttf");
  85. editorModeText = Text();
  86. ui.root.AddChild(editorModeText);
  87. renderStatsText = Text();
  88. ui.root.AddChild(renderStatsText);
  89. cameraPosText = Text();
  90. ui.root.AddChild(cameraPosText);
  91. if (ui.root.width >= 1200)
  92. {
  93. SetupStatsBarText(editorModeText, font, 0, 24, HA_LEFT, VA_TOP);
  94. SetupStatsBarText(renderStatsText, font, 0, 24, HA_RIGHT, VA_TOP);
  95. }
  96. else
  97. {
  98. SetupStatsBarText(editorModeText, font, 0, 24, HA_LEFT, VA_TOP);
  99. SetupStatsBarText(renderStatsText, font, 0, 36, HA_LEFT, VA_TOP);
  100. }
  101. SetupStatsBarText(cameraPosText, font, 0, 0, HA_LEFT, VA_BOTTOM);
  102. }
  103. void SetupStatsBarText(Text@ text, Font@ font, int x, int y, HorizontalAlignment hAlign, VerticalAlignment vAlign)
  104. {
  105. text.position = IntVector2(x, y);
  106. text.horizontalAlignment = hAlign;
  107. text.verticalAlignment = vAlign;
  108. text.SetFont(font, 11);
  109. text.color = Color(1, 1, 0);
  110. text.priority = -100;
  111. }
  112. void UpdateStats(float timeStep)
  113. {
  114. editorModeText.text = String(
  115. "Mode: " + editModeText[editMode] +
  116. " Axis: " + axisModeText[axisMode] +
  117. " Pick: " + pickModeText[pickMode] +
  118. " Fill: " + fillModeText[fillMode] +
  119. " Updates: " + (runUpdate ? "Running" : "Paused"));
  120. renderStatsText.text = String(
  121. "Tris: " + renderer.numPrimitives +
  122. " Batches: " + renderer.numBatches +
  123. " Lights: " + renderer.numLights[true] +
  124. " Shadowmaps: " + renderer.numShadowMaps[true] +
  125. " Occluders: " + renderer.numOccluders[true]);
  126. Vector3 cameraPos = cameraNode.position;
  127. String xText(cameraPos.x);
  128. String yText(cameraPos.y);
  129. String zText(cameraPos.z);
  130. xText.Resize(8);
  131. yText.Resize(8);
  132. zText.Resize(8);
  133. cameraPosText.text = String(
  134. "Pos: " + xText + " " + yText + " " + zText);
  135. editorModeText.size = editorModeText.minSize;
  136. renderStatsText.size = renderStatsText.minSize;
  137. cameraPosText.size = cameraPosText.minSize;
  138. }
  139. void UpdateView(float timeStep)
  140. {
  141. // Move camera
  142. if (ui.focusElement is null && !input.keyDown[KEY_LCTRL])
  143. {
  144. float speedMultiplier = 1.0;
  145. if (input.keyDown[KEY_LSHIFT])
  146. speedMultiplier = cameraShiftSpeedMultiplier;
  147. if (input.keyDown['W'] || input.keyDown[KEY_UP])
  148. {
  149. cameraNode.TranslateRelative(Vector3(0, 0, cameraBaseSpeed) * timeStep * speedMultiplier);
  150. HideUI();
  151. }
  152. if (input.keyDown['S'] || input.keyDown[KEY_DOWN])
  153. {
  154. cameraNode.TranslateRelative(Vector3(0, 0, -cameraBaseSpeed) * timeStep * speedMultiplier);
  155. HideUI();
  156. }
  157. if (input.keyDown['A'] || input.keyDown[KEY_LEFT])
  158. {
  159. cameraNode.TranslateRelative(Vector3(-cameraBaseSpeed, 0, 0) * timeStep * speedMultiplier);
  160. HideUI();
  161. }
  162. if (input.keyDown['D'] || input.keyDown[KEY_RIGHT])
  163. {
  164. cameraNode.TranslateRelative(Vector3(cameraBaseSpeed, 0, 0) * timeStep * speedMultiplier);
  165. HideUI();
  166. }
  167. }
  168. // Rotate camera
  169. if (input.mouseButtonDown[MOUSEB_RIGHT])
  170. {
  171. IntVector2 mouseMove = input.mouseMove;
  172. if (mouseMove.x != 0 || mouseMove.y != 0)
  173. {
  174. cameraYaw += mouseMove.x * cameraBaseRotationSpeed;
  175. cameraPitch += mouseMove.y * cameraBaseRotationSpeed;
  176. if (cameraPitch < -90.0)
  177. cameraPitch = -90.0;
  178. if (cameraPitch > 90.0)
  179. cameraPitch = 90.0;
  180. cameraNode.rotation = Quaternion(cameraPitch, cameraYaw, 0);
  181. HideUI();
  182. }
  183. }
  184. // Move/rotate/scale object
  185. if (!editNodes.empty && editMode != EDIT_SELECT && ui.focusElement is null && input.keyDown[KEY_LCTRL])
  186. {
  187. Vector3 adjust(0, 0, 0);
  188. if (input.keyDown[KEY_UP])
  189. adjust.z = 1;
  190. if (input.keyDown[KEY_DOWN])
  191. adjust.z = -1;
  192. if (input.keyDown[KEY_LEFT])
  193. adjust.x = -1;
  194. if (input.keyDown[KEY_RIGHT])
  195. adjust.x = 1;
  196. if (input.keyDown[KEY_PAGEUP])
  197. adjust.y = 1;
  198. if (input.keyDown[KEY_PAGEDOWN])
  199. adjust.y = -1;
  200. if (editMode == EDIT_SCALE)
  201. {
  202. if (input.keyDown[KEY_ADD])
  203. adjust = Vector3(1, 1, 1);
  204. if (input.keyDown[KEY_SUBTRACT])
  205. adjust = Vector3(-1, -1, -1);
  206. }
  207. if (adjust == Vector3(0, 0, 0))
  208. return;
  209. bool moved = false;
  210. adjust *= timeStep * 10;
  211. switch (editMode)
  212. {
  213. case EDIT_MOVE:
  214. if (!moveSnap)
  215. moved = MoveNodes(adjust * moveStep);
  216. break;
  217. case EDIT_ROTATE:
  218. if (!rotateSnap)
  219. {
  220. Vector3 rotAdjust;
  221. rotAdjust.x = adjust.z * rotateStep;
  222. rotAdjust.y = adjust.x * rotateStep;
  223. rotAdjust.z = adjust.y * rotateStep;
  224. moved = RotateNodes(rotAdjust);
  225. }
  226. break;
  227. case EDIT_SCALE:
  228. if (!scaleSnap)
  229. moved = ScaleNodes(adjust * scaleStep);
  230. break;
  231. }
  232. if (moved)
  233. UpdateNodeAttributes();
  234. }
  235. }
  236. void SteppedObjectManipulation(int key)
  237. {
  238. if (editNodes.empty || editMode == EDIT_SELECT)
  239. return;
  240. // Do not react in non-snapped mode, because that is handled in frame update
  241. if (editMode == EDIT_MOVE && !moveSnap)
  242. return;
  243. if (editMode == EDIT_ROTATE && !rotateSnap)
  244. return;
  245. if (editMode == EDIT_SCALE && !scaleSnap)
  246. return;
  247. Vector3 adjust(0, 0, 0);
  248. if (key == KEY_UP)
  249. adjust.z = 1;
  250. if (key == KEY_DOWN)
  251. adjust.z = -1;
  252. if (key == KEY_LEFT)
  253. adjust.x = -1;
  254. if (key == KEY_RIGHT)
  255. adjust.x = 1;
  256. if (key == KEY_PAGEUP)
  257. adjust.y = 1;
  258. if (key == KEY_PAGEDOWN)
  259. adjust.y = -1;
  260. if (editMode == EDIT_SCALE)
  261. {
  262. if (key == KEY_ADD)
  263. adjust = Vector3(1, 1, 1);
  264. if (key == KEY_SUBTRACT)
  265. adjust = Vector3(-1, -1, -1);
  266. }
  267. if (adjust == Vector3(0, 0, 0))
  268. return;
  269. bool moved = false;
  270. switch (editMode)
  271. {
  272. case EDIT_MOVE:
  273. moved = MoveNodes(adjust);
  274. break;
  275. case EDIT_ROTATE:
  276. {
  277. Vector3 rotAdjust;
  278. rotAdjust.x = adjust.z * rotateStep;
  279. rotAdjust.y = adjust.x * rotateStep;
  280. rotAdjust.z = adjust.y * rotateStep;
  281. moved = RotateNodes(rotAdjust);
  282. }
  283. break;
  284. case EDIT_SCALE:
  285. moved = ScaleNodes(adjust * scaleStep);
  286. break;
  287. }
  288. if (moved)
  289. UpdateNodeAttributes();
  290. }
  291. void HandlePostRenderUpdate()
  292. {
  293. DebugRenderer@ debug = editorScene.debugRenderer;
  294. if (debug is null)
  295. return;
  296. // Visualize the currently selected nodes as their local axes + the first drawable component
  297. for (uint i = 0; i < selectedNodes.length; ++i)
  298. {
  299. Node@ node = selectedNodes[i];
  300. debug.AddNode(node, 1.0, false);
  301. for (uint j = 0; j < node.numComponents; ++j)
  302. {
  303. Drawable@ drawable = cast<Drawable>(node.components[j]);
  304. if (drawable !is null)
  305. {
  306. drawable.DrawDebugGeometry(debug, false);
  307. break;
  308. }
  309. }
  310. }
  311. // Visualize the currently selected components
  312. for (uint i = 0; i < selectedComponents.length; ++i)
  313. selectedComponents[i].DrawDebugGeometry(debug, false);
  314. if (renderingDebug)
  315. renderer.DrawDebugGeometry(false);
  316. if (physicsDebug && editorScene.physicsWorld !is null)
  317. editorScene.physicsWorld.DrawDebugGeometry(true);
  318. if (octreeDebug && editorScene.octree !is null)
  319. editorScene.octree.DrawDebugGeometry(true);
  320. ViewRaycast(false);
  321. }
  322. void ViewMouseClick()
  323. {
  324. ViewRaycast(true);
  325. }
  326. void ViewRaycast(bool mouseClick)
  327. {
  328. DebugRenderer@ debug = editorScene.debugRenderer;
  329. IntVector2 pos = ui.cursorPosition;
  330. Component@ selected;
  331. // Do not raycast / change selection if hovering over an UI element or the gizmo
  332. if (ui.GetElementAt(pos) !is null || IsGizmoSelected())
  333. return;
  334. Ray cameraRay = camera.GetScreenRay(float(pos.x) / graphics.width, float(pos.y) / graphics.height);
  335. if (pickMode != PICK_RIGIDBODIES)
  336. {
  337. if (editorScene.octree is null)
  338. return;
  339. RayQueryResult result = editorScene.octree.RaycastSingle(cameraRay, RAY_TRIANGLE, camera.farClip,
  340. pickModeDrawableFlags[pickMode], 0x7fffffff);
  341. if (result.drawable !is null)
  342. {
  343. Drawable@ drawable = result.drawable;
  344. // If selecting a terrain patch, select the parent terrain instead
  345. if (drawable.typeName != "TerrainPatch")
  346. {
  347. selected = drawable;
  348. if (debug !is null)
  349. {
  350. debug.AddNode(drawable.node, 1.0, false);
  351. drawable.DrawDebugGeometry(debug, false);
  352. }
  353. }
  354. else if (drawable.node.parent !is null)
  355. selected = drawable.node.parent.GetComponent("Terrain");
  356. }
  357. }
  358. else
  359. {
  360. if (editorScene.physicsWorld is null)
  361. return;
  362. // If we are not running the actual physics update, refresh collisions before raycasting
  363. if (!runUpdate)
  364. editorScene.physicsWorld.UpdateCollisions();
  365. PhysicsRaycastResult result = editorScene.physicsWorld.RaycastSingle(cameraRay, camera.farClip);
  366. if (result.body !is null)
  367. {
  368. RigidBody@ body = result.body;
  369. if (debug !is null)
  370. {
  371. debug.AddNode(body.node, 1.0, false);
  372. body.DrawDebugGeometry(debug, false);
  373. }
  374. selected = body;
  375. }
  376. }
  377. if (mouseClick && input.mouseButtonPress[MOUSEB_LEFT])
  378. {
  379. bool multiselect = input.qualifierDown[QUAL_CTRL];
  380. if (selected !is null)
  381. {
  382. if (input.qualifierDown[QUAL_SHIFT])
  383. {
  384. // If we are selecting components, but have nodes in existing selection, do not multiselect to prevent confusion
  385. if (!selectedNodes.empty)
  386. multiselect = false;
  387. SelectComponent(selected, multiselect);
  388. }
  389. else
  390. {
  391. // If we are selecting nodes, but have components in existing selection, do not multiselect to prevent confusion
  392. if (!selectedComponents.empty)
  393. multiselect = false;
  394. SelectNode(selected.node, multiselect);
  395. }
  396. }
  397. else
  398. {
  399. // If clicked on emptiness in non-multiselect mode, clear the selection
  400. if (!multiselect)
  401. SelectComponent(null, false);
  402. }
  403. }
  404. }
  405. Vector3 GetNewNodePosition()
  406. {
  407. return cameraNode.position + cameraNode.worldRotation * Vector3(0, 0, newNodeDistance);
  408. }
  409. int GetShadowResolution()
  410. {
  411. if (!renderer.drawShadows)
  412. return 0;
  413. int level = 1;
  414. int res = renderer.shadowMapSize;
  415. while (res > 512)
  416. {
  417. res >>= 1;
  418. ++level;
  419. }
  420. if (level > 3)
  421. level = 3;
  422. return level;
  423. }
  424. void SetShadowResolution(int level)
  425. {
  426. if (level <= 0)
  427. {
  428. renderer.drawShadows = false;
  429. return;
  430. }
  431. else
  432. {
  433. renderer.drawShadows = true;
  434. renderer.shadowMapSize = 256 << level;
  435. }
  436. }
  437. void ToggleRenderingDebug()
  438. {
  439. renderingDebug = !renderingDebug;
  440. }
  441. void TogglePhysicsDebug()
  442. {
  443. physicsDebug = !physicsDebug;
  444. }
  445. void ToggleOctreeDebug()
  446. {
  447. octreeDebug = !octreeDebug;
  448. }
  449. void ToggleUpdate()
  450. {
  451. runUpdate = !runUpdate;
  452. }