SceneView3D.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. // Portions Copyright (c) 2008-2015 the Urho3D project.
  2. //
  3. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include <Atomic/IO/Log.h>
  24. #include <Atomic/Core/CoreEvents.h>
  25. #include <Atomic/Scene/SceneEvents.h>
  26. #include <Atomic/Scene/Scene.h>
  27. #include <Atomic/Scene/PrefabComponent.h>
  28. #include <Atomic/Graphics/Camera.h>
  29. #include <Atomic/Graphics/Graphics.h>
  30. #include <Atomic/Graphics/DebugRenderer.h>
  31. #include <Atomic/Graphics/Viewport.h>
  32. #include <Atomic/Graphics/Octree.h>
  33. #include <Atomic/Graphics/Material.h>
  34. #include <Atomic/Atomic3D/Model.h>
  35. #include <Atomic/Atomic3D/StaticModel.h>
  36. #include <Atomic/Atomic3D/AnimatedModel.h>
  37. #include <Atomic/Atomic3D/AnimationController.h>
  38. #include <Atomic/Input/Input.h>
  39. #include <Atomic/IO/FileSystem.h>
  40. #include <Atomic/Resource/ResourceCache.h>
  41. #include <Atomic/Resource/XMLFile.h>
  42. #include <Atomic/Physics/PhysicsWorld.h>
  43. #include <Atomic/UI/UI.h>
  44. #include <Atomic/UI/UIEvents.h>
  45. #include <Atomic/Resource/ResourceEvents.h>
  46. #include <ToolCore/Assets/Asset.h>
  47. #include <ToolCore/Assets/AssetDatabase.h>
  48. #include "../../EditorMode/AEEditorEvents.h"
  49. #include "SceneView3D.h"
  50. #include "SceneEditor3D.h"
  51. #include "SceneEditor3DEvents.h"
  52. #include "SceneSelection.h"
  53. using namespace ToolCore;
  54. namespace AtomicEditor
  55. {
  56. SceneView3D ::SceneView3D(Context* context, SceneEditor3D *sceneEditor) :
  57. UISceneView(context),
  58. yaw_(0.0f),
  59. pitch_(0.0f),
  60. mouseLeftDown_(false),
  61. mouseMoved_(false),
  62. enabled_(true),
  63. cameraMove_(false),
  64. cameraMoveSpeed_(20.0f)
  65. {
  66. sceneEditor_ = sceneEditor;
  67. ResourceCache* cache = GetSubsystem<ResourceCache>();
  68. scene_ = sceneEditor->GetScene();
  69. debugRenderer_ = scene_->GetComponent<DebugRenderer>();
  70. if (debugRenderer_.Null())
  71. {
  72. debugRenderer_ = scene_->CreateComponent<DebugRenderer>();
  73. debugRenderer_->SetTemporary(true);
  74. }
  75. octree_ = scene_->GetComponent<Octree>();
  76. if (octree_.Null())
  77. {
  78. LOGWARNING("Scene without an octree loaded");
  79. octree_ = scene_->CreateComponent<Octree>();
  80. }
  81. cameraNode_ = scene_->CreateChild("__atomic_sceneview3d_camera");
  82. cameraNode_->SetTemporary(true);
  83. camera_ = cameraNode_->CreateComponent<Camera>();
  84. debugRenderer_ = scene_->GetComponent<DebugRenderer>();
  85. assert(debugRenderer_.NotNull());
  86. octree_ = scene_->GetComponent<Octree>();
  87. assert(octree_.NotNull());
  88. cameraNode_->SetPosition(Vector3(0, 0, -10));
  89. SetView(scene_, camera_);
  90. SetAutoUpdate(false);
  91. SubscribeToEvent(E_UPDATE, HANDLER(SceneView3D, HandleUpdate));
  92. SubscribeToEvent(E_POSTRENDERUPDATE, HANDLER(SceneView3D, HandlePostRenderUpdate));
  93. SubscribeToEvent(E_MOUSEMOVE, HANDLER(SceneView3D,HandleMouseMove));
  94. SubscribeToEvent(this, E_DRAGENTERWIDGET, HANDLER(SceneView3D, HandleDragEnterWidget));
  95. SubscribeToEvent(this, E_DRAGEXITWIDGET, HANDLER(SceneView3D, HandleDragExitWidget));
  96. SubscribeToEvent(this, E_DRAGENDED, HANDLER(SceneView3D, HandleDragEnded));
  97. SubscribeToEvent(E_UIUNHANDLEDSHORTCUT, HANDLER(SceneView3D, HandleUIUnhandledShortcut));
  98. SubscribeToEvent(E_UIWIDGETFOCUSESCAPED, HANDLER(SceneView3D, HandleUIWidgetFocusEscaped));
  99. SetIsFocusable(true);
  100. }
  101. SceneView3D::~SceneView3D()
  102. {
  103. }
  104. void SceneView3D::Enable()
  105. {
  106. if (enabled_)
  107. return;
  108. enabled_ = true;
  109. }
  110. void SceneView3D::Disable()
  111. {
  112. if (!enabled_)
  113. return;
  114. enabled_ = false;
  115. }
  116. bool SceneView3D::GetOrbitting()
  117. {
  118. Input* input = GetSubsystem<Input>();
  119. return framedBBox_.defined_ && MouseInView() && input->GetKeyDown(KEY_ALT) && input->GetMouseButtonDown(MOUSEB_LEFT);
  120. }
  121. bool SceneView3D::GetZooming()
  122. {
  123. Input* input = GetSubsystem<Input>();
  124. return MouseInView() && input->GetMouseMoveWheel() && !input->GetMouseButtonDown(MOUSEB_RIGHT);
  125. }
  126. bool SceneView3D::GetChangingCameraSpeed()
  127. {
  128. Input* input = GetSubsystem<Input>();
  129. return MouseInView() && input->GetMouseMoveWheel() && input->GetMouseButtonDown(MOUSEB_RIGHT);
  130. }
  131. void SceneView3D::CheckCameraSpeedBounds()
  132. {
  133. const float MAX_CAMERA_SPEED = 80.0f;
  134. const float MIN_CAMERA_SPEED = 2.0f;
  135. if (cameraMoveSpeed_ >= MAX_CAMERA_SPEED)
  136. {
  137. cameraMoveSpeed_ = MAX_CAMERA_SPEED;
  138. }
  139. if (cameraMoveSpeed_ <= MIN_CAMERA_SPEED)
  140. cameraMoveSpeed_ = MIN_CAMERA_SPEED;
  141. }
  142. void SceneView3D::MoveCamera(float timeStep)
  143. {
  144. // Mouse sensitivity as degrees per pixel
  145. const float MOUSE_SENSITIVITY = 0.2f;
  146. // Tempo at which mouse speed increases using mousewheel
  147. const float CAMERA_MOVE_TEMPO = 5.0f;
  148. // Tempo used when zooming in and out
  149. const float ZOOM_TEMPO = 0.6f;
  150. if (!enabled_ && !GetFocus())
  151. return;
  152. Input* input = GetSubsystem<Input>();
  153. bool shiftDown = false;
  154. if (input->GetKeyDown(KEY_LSHIFT) || input->GetKeyDown(KEY_RSHIFT))
  155. shiftDown = true;
  156. bool mouseInView = MouseInView();
  157. bool orbitting = GetOrbitting();
  158. bool zooming = GetZooming();
  159. bool changingCameraSpeed = GetChangingCameraSpeed();
  160. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  161. if ((mouseInView && input->GetMouseButtonDown(MOUSEB_RIGHT)) || orbitting)
  162. {
  163. SetFocus();
  164. IntVector2 mouseMove = input->GetMouseMove();
  165. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  166. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  167. pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  168. }
  169. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  170. Quaternion q(pitch_, yaw_, 0.0f);
  171. if (!zooming || !changingCameraSpeed)
  172. cameraNode_->SetRotation(q);
  173. if (orbitting)
  174. {
  175. zooming = false;
  176. BoundingBox bbox;
  177. sceneEditor_->GetSelection()->GetBounds(bbox);
  178. if (bbox.defined_)
  179. {
  180. Vector3 centerPoint = bbox.Center();
  181. Vector3 d = cameraNode_->GetWorldPosition() - centerPoint;
  182. cameraNode_->SetWorldPosition(centerPoint - q * Vector3(0.0, 0.0, d.Length()));
  183. }
  184. }
  185. if (zooming)
  186. {
  187. orbitting = false;
  188. Ray ray = GetCameraRay();
  189. Vector3 wpos = cameraNode_->GetWorldPosition();
  190. wpos += ray.direction_ * (float(input->GetMouseMoveWheel()) * ZOOM_TEMPO);
  191. cameraNode_->SetWorldPosition(wpos);
  192. }
  193. if (changingCameraSpeed)
  194. {
  195. int mouseWheel = input->GetMouseMoveWheel();
  196. // Apple decided to change the direction of mousewheel input to match touch devices
  197. #ifdef ATOMIC_PLATFORM_OSX
  198. mouseWheel = -mouseWheel;
  199. #endif
  200. if (mouseWheel)
  201. cameraMoveSpeed_ += mouseWheel * CAMERA_MOVE_TEMPO;
  202. CheckCameraSpeedBounds();
  203. }
  204. #ifdef ATOMIC_PLATFORM_WINDOWS
  205. bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
  206. #else
  207. bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
  208. #endif
  209. if (!orbitting && mouseInView && !superdown && input->GetMouseButtonDown(MOUSEB_RIGHT)) {
  210. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  211. // Use the Translate() function (default local space) to move relative to the node's orientation.
  212. if (input->GetKeyDown(KEY_W))
  213. {
  214. SetFocus();
  215. cameraNode_->Translate(Vector3::FORWARD * cameraMoveSpeed_ * timeStep);
  216. }
  217. if (input->GetKeyDown(KEY_S))
  218. {
  219. SetFocus();
  220. cameraNode_->Translate(Vector3::BACK * cameraMoveSpeed_ * timeStep);
  221. }
  222. if (input->GetKeyDown(KEY_A))
  223. {
  224. SetFocus();
  225. cameraNode_->Translate(Vector3::LEFT * cameraMoveSpeed_ * timeStep);
  226. }
  227. if (input->GetKeyDown(KEY_D))
  228. {
  229. SetFocus();
  230. cameraNode_->Translate(Vector3::RIGHT * cameraMoveSpeed_ * timeStep);
  231. }
  232. if (input->GetKeyDown(KEY_E))
  233. {
  234. SetFocus();
  235. cameraNode_->Translate(Vector3::UP * cameraMoveSpeed_ * timeStep);
  236. }
  237. if (input->GetKeyDown(KEY_Q))
  238. {
  239. SetFocus();
  240. cameraNode_->Translate(Vector3::DOWN * cameraMoveSpeed_ * timeStep);
  241. }
  242. }
  243. if (cameraMove_)
  244. {
  245. cameraMoveTime_ += timeStep * 3.0f;
  246. if (cameraMoveTime_ > 1.0f)
  247. {
  248. cameraMove_ = false;
  249. cameraMoveTime_ = 1.0f;
  250. }
  251. Vector3 pos = cameraMoveStart_.Lerp(cameraMoveTarget_, cameraMoveTime_);
  252. cameraNode_->SetWorldPosition(pos);
  253. }
  254. }
  255. Ray SceneView3D::GetCameraRay()
  256. {
  257. Ray camRay;
  258. Input* input = GetSubsystem<Input>();
  259. IntVector2 cpos = input->GetMousePosition();
  260. IntRect rect = GetRect();
  261. if (!rect.Width() || !rect.Height())
  262. return camRay;
  263. int x = rect.left_;
  264. int y = rect.top_;
  265. GetInternalWidget()->ConvertToRoot(x, y);
  266. return camera_->GetScreenRay(float(cpos.x_ - x) / rect.Width(),
  267. float(cpos.y_ - y) / rect.Height());
  268. }
  269. bool SceneView3D::MouseInView()
  270. {
  271. if (!GetInternalWidget())
  272. return false;
  273. if (!TBWidget::hovered_widget || TBWidget::hovered_widget->GetDelegate() != this)
  274. return false;
  275. Input* input = GetSubsystem<Input>();
  276. IntVector2 pos = input->GetMousePosition();
  277. IntRect rect = GetRect();
  278. GetInternalWidget()->ConvertToRoot(rect.left_, rect.top_);
  279. GetInternalWidget()->ConvertToRoot(rect.right_, rect.bottom_);
  280. return rect.IsInside(pos);
  281. }
  282. void SceneView3D::HandleUIUnhandledShortcut(StringHash eventType, VariantMap& eventData)
  283. {
  284. if (!enabled_)
  285. return;
  286. unsigned id = eventData[UIUnhandledShortcut::P_REFID].GetUInt();
  287. if (id == TBIDC("undo"))
  288. sceneEditor_->Undo();
  289. else if (id == TBIDC("redo"))
  290. sceneEditor_->Redo();
  291. else if (id == TBIDC("copy"))
  292. sceneEditor_->Copy();
  293. else if (id == TBIDC("cut"))
  294. sceneEditor_->Cut();
  295. else if (id == TBIDC("paste"))
  296. sceneEditor_->Paste();
  297. return;
  298. }
  299. void SceneView3D::HandleUIWidgetFocusEscaped(StringHash eventType, VariantMap& eventData)
  300. {
  301. if (!enabled_)
  302. return;
  303. SetFocus();
  304. }
  305. void SceneView3D::HandlePostRenderUpdate(StringHash eventType, VariantMap& eventData)
  306. {
  307. if (!MouseInView() || GetOrbitting())
  308. return;
  309. Input* input = GetSubsystem<Input>();
  310. mouseLeftDown_ = false;
  311. bool shiftDown = input->GetKeyDown(KEY_LSHIFT) || input->GetKeyDown(KEY_RSHIFT);
  312. if (input->GetMouseButtonPress(MOUSEB_LEFT))
  313. {
  314. SetFocus();
  315. if (!mouseMoved_ && !sceneEditor_->GetGizmo()->Selected())
  316. {
  317. Ray camRay = GetCameraRay();
  318. PODVector<RayQueryResult> result;
  319. RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_GEOMETRY, 0x7fffffff);
  320. octree_->RaycastSingle(query);
  321. if (query.result_.Size())
  322. {
  323. const RayQueryResult& r = result[0];
  324. if (r.drawable_)
  325. {
  326. VariantMap neventData;
  327. Node* node = r.drawable_->GetNode();
  328. // if temporary, this is a prefab
  329. // TODO: if we use temporary for other stuff
  330. // fix this to look for prefab
  331. if (node->IsTemporary())
  332. node = node->GetParent();
  333. if (sceneEditor_->GetSelection()->Contains(node) && shiftDown)
  334. {
  335. sceneEditor_->GetSelection()->RemoveNode(node);
  336. }
  337. else
  338. {
  339. sceneEditor_->GetSelection()->AddNode(node, !shiftDown);
  340. }
  341. }
  342. }
  343. else
  344. {
  345. sceneEditor_->GetSelection()->Clear();
  346. }
  347. }
  348. mouseMoved_ = false;
  349. }
  350. else if (!input->GetMouseButtonDown(MOUSEB_LEFT))
  351. {
  352. Ray camRay = GetCameraRay();
  353. PODVector<RayQueryResult> result;
  354. mouseMoved_ = false;
  355. /*
  356. Array<int> pickModeDrawableFlags = {
  357. DRAWABLE_GEOMETRY,
  358. DRAWABLE_LIGHT,
  359. DRAWABLE_ZONE
  360. };
  361. */
  362. RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_GEOMETRY, 0x7fffffff);
  363. octree_->RaycastSingle(query);
  364. if (query.result_.Size())
  365. {
  366. const RayQueryResult& r = result[0];
  367. if (r.drawable_)
  368. {
  369. debugRenderer_->AddNode(r.drawable_->GetNode(), 1.0, false);
  370. r.drawable_->DrawDebugGeometry(debugRenderer_, false);
  371. }
  372. }
  373. }
  374. else
  375. {
  376. mouseLeftDown_ = true;
  377. if (Abs(input->GetMouseMoveX() > 3 || input->GetMouseMoveY() > 3))
  378. {
  379. mouseMoved_ = true;
  380. }
  381. }
  382. }
  383. bool SceneView3D::OnEvent(const TBWidgetEvent &ev)
  384. {
  385. if (ev.type == EVENT_TYPE_SHORTCUT)
  386. {
  387. if (ev.ref_id == TBIDC("close"))
  388. return false;
  389. }
  390. if (ev.type == EVENT_TYPE_KEY_UP)
  391. {
  392. if (ev.special_key == TB_KEY_ESC)
  393. {
  394. sceneEditor_->GetSelection()->Clear();
  395. }
  396. }
  397. return sceneEditor_->OnEvent(ev);
  398. }
  399. void SceneView3D::HandleUpdate(StringHash eventType, VariantMap& eventData)
  400. {
  401. // parent is the contentRoot for our tab, when tab isn't active it will not be visible
  402. if (!GetParent() || GetParent()->GetVisibility() != UI_WIDGET_VISIBILITY_VISIBLE)
  403. {
  404. Disable();
  405. return;
  406. }
  407. Enable();
  408. // Timestep parameter is same no matter what event is being listened to
  409. float timeStep = eventData[Update::P_TIMESTEP].GetFloat();
  410. MoveCamera(timeStep);
  411. QueueUpdate();
  412. if (preloadResourceScene_.NotNull())
  413. {
  414. if (preloadResourceScene_->GetAsyncProgress() == 1.0f)
  415. {
  416. ResourceCache* cache = GetSubsystem<ResourceCache>();
  417. XMLFile* xml = cache->GetResource<XMLFile>(dragAssetGUID_);
  418. if (dragNode_.NotNull())
  419. {
  420. dragNode_->LoadXML(xml->GetRoot());
  421. UpdateDragNode(0, 0);
  422. AnimationController* controller = dragNode_->GetComponent<AnimationController>();
  423. if (controller)
  424. {
  425. controller->PlayExclusive("Idle", 0, true);
  426. dragNode_->GetScene()->SetUpdateEnabled(true);
  427. }
  428. }
  429. preloadResourceScene_ = 0;
  430. dragAssetGUID_ = "";
  431. }
  432. }
  433. }
  434. void SceneView3D::UpdateDragNode(int mouseX, int mouseY)
  435. {
  436. if (dragNode_.Null())
  437. return;
  438. Ray ray = GetCameraRay();
  439. Vector3 pos = ray.origin_;
  440. pos += ray.direction_ * 10;
  441. dragNode_->SetWorldPosition(pos);
  442. }
  443. void SceneView3D::HandleMouseMove(StringHash eventType, VariantMap& eventData)
  444. {
  445. if (dragNode_.Null())
  446. return;
  447. Input* input = GetSubsystem<Input>();
  448. if (!input->IsMouseVisible())
  449. return;
  450. using namespace MouseMove;
  451. int x = eventData[P_X].GetInt();
  452. int y = eventData[P_Y].GetInt();
  453. UpdateDragNode(x, y);
  454. }
  455. void SceneView3D::HandleDragEnterWidget(StringHash eventType, VariantMap& eventData)
  456. {
  457. using namespace DragEnterWidget;
  458. UIWidget* widget = static_cast<UIWidget*>(eventData[P_WIDGET].GetPtr());
  459. if (widget != this)
  460. return;
  461. UIDragObject* dragObject = static_cast<UIDragObject*>(eventData[P_DRAGOBJECT].GetPtr());
  462. Object* object = dragObject->GetObject();
  463. if (!object)
  464. return;
  465. if (object->GetType() == Asset::GetTypeStatic())
  466. {
  467. Asset* asset = (Asset*) object;
  468. dragNode_ = asset->InstantiateNode(scene_, asset->GetName());
  469. if (dragNode_.NotNull())
  470. {
  471. Input* input = GetSubsystem<Input>();
  472. IntVector2 pos = input->GetMousePosition();
  473. UpdateDragNode(pos.x_, pos.y_);
  474. }
  475. //LOGINFOF("Dropped %s : %s on SceneView3D", asset->GetPath().CString(), asset->GetGUID().CString());
  476. }
  477. }
  478. void SceneView3D::HandleDragExitWidget(StringHash eventType, VariantMap& eventData)
  479. {
  480. if (preloadResourceScene_.NotNull())
  481. {
  482. preloadResourceScene_->StopAsyncLoading();
  483. preloadResourceScene_ = 0;
  484. }
  485. if (dragNode_.NotNull())
  486. {
  487. scene_->RemoveChild(dragNode_);
  488. }
  489. dragAssetGUID_ = "";
  490. dragNode_ = 0;
  491. }
  492. void SceneView3D::HandleDragEnded(StringHash eventType, VariantMap& eventData)
  493. {
  494. using namespace DragEnded;
  495. UIDragObject* dragObject = static_cast<UIDragObject*>(eventData[P_DRAGOBJECT].GetPtr());
  496. if (dragNode_.NotNull())
  497. {
  498. VariantMap nodeCreatedEvent;
  499. nodeCreatedEvent[SceneEditNodeCreated::P_NODE] = dragNode_;
  500. scene_->SendEvent(E_SCENEEDITNODECREATED, nodeCreatedEvent);
  501. }
  502. if (dragObject && dragObject->GetObject()->GetType() == ToolCore::Asset::GetTypeStatic())
  503. {
  504. Asset* asset = (ToolCore::Asset*) dragObject->GetObject();
  505. if (asset->GetImporterTypeName() == "MaterialImporter") {
  506. Material* material = GetSubsystem<ResourceCache>()->GetResource<Material>(asset->GetPath());
  507. if (material) {
  508. material = material;
  509. Ray camRay = GetCameraRay();
  510. PODVector<RayQueryResult> result;
  511. RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_GEOMETRY, 0x7fffffff);
  512. octree_->RaycastSingle(query);
  513. if (query.result_.Size())
  514. {
  515. const RayQueryResult& r = result[0];
  516. if (r.drawable_ && (r.drawable_->GetType() == StaticModel::GetTypeStatic() || r.drawable_->GetType() == AnimatedModel::GetTypeStatic()))
  517. {
  518. ((StaticModel*)r.drawable_)->SetMaterial(material);
  519. }
  520. }
  521. }
  522. }
  523. }
  524. dragAssetGUID_ = "";
  525. dragNode_ = 0;
  526. }
  527. void SceneView3D::FrameSelection()
  528. {
  529. BoundingBox bbox;
  530. sceneEditor_->GetSelection()->GetBounds(bbox);
  531. if (!bbox.defined_)
  532. return;
  533. Sphere sphere(bbox);
  534. if (sphere.radius_ < .01f || sphere.radius_ > 512)
  535. return;
  536. framedBBox_ = bbox;
  537. cameraMoveStart_ = cameraNode_->GetWorldPosition();
  538. cameraMoveTarget_ = bbox.Center() - (cameraNode_->GetWorldDirection() * sphere.radius_ * 3);
  539. cameraMoveTime_ = 0.0f;
  540. cameraMove_ = true;
  541. }
  542. }