SceneView3D.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. if (input->GetMouseMoveWheel() > 0)
  196. cameraMoveSpeed_ += input->GetMouseMoveWheel() * CAMERA_MOVE_TEMPO;
  197. else
  198. if (input->GetMouseMoveWheel() < 0)
  199. cameraMoveSpeed_ += input->GetMouseMoveWheel() * CAMERA_MOVE_TEMPO;
  200. CheckCameraSpeedBounds();
  201. }
  202. #ifdef ATOMIC_PLATFORM_WINDOWS
  203. bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
  204. #else
  205. bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
  206. #endif
  207. if (!orbitting && mouseInView && !superdown && input->GetMouseButtonDown(MOUSEB_RIGHT)) {
  208. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  209. // Use the Translate() function (default local space) to move relative to the node's orientation.
  210. if (input->GetKeyDown(KEY_W))
  211. {
  212. SetFocus();
  213. cameraNode_->Translate(Vector3::FORWARD * cameraMoveSpeed_ * timeStep);
  214. }
  215. if (input->GetKeyDown(KEY_S))
  216. {
  217. SetFocus();
  218. cameraNode_->Translate(Vector3::BACK * cameraMoveSpeed_ * timeStep);
  219. }
  220. if (input->GetKeyDown(KEY_A))
  221. {
  222. SetFocus();
  223. cameraNode_->Translate(Vector3::LEFT * cameraMoveSpeed_ * timeStep);
  224. }
  225. if (input->GetKeyDown(KEY_D))
  226. {
  227. SetFocus();
  228. cameraNode_->Translate(Vector3::RIGHT * cameraMoveSpeed_ * timeStep);
  229. }
  230. if (input->GetKeyDown(KEY_E))
  231. {
  232. SetFocus();
  233. cameraNode_->Translate(Vector3::UP * cameraMoveSpeed_ * timeStep);
  234. }
  235. if (input->GetKeyDown(KEY_Q))
  236. {
  237. SetFocus();
  238. cameraNode_->Translate(Vector3::DOWN * cameraMoveSpeed_ * timeStep);
  239. }
  240. }
  241. if (cameraMove_)
  242. {
  243. cameraMoveTime_ += timeStep * 3.0f;
  244. if (cameraMoveTime_ > 1.0f)
  245. {
  246. cameraMove_ = false;
  247. cameraMoveTime_ = 1.0f;
  248. }
  249. Vector3 pos = cameraMoveStart_.Lerp(cameraMoveTarget_, cameraMoveTime_);
  250. cameraNode_->SetWorldPosition(pos);
  251. }
  252. }
  253. Ray SceneView3D::GetCameraRay()
  254. {
  255. Ray camRay;
  256. Input* input = GetSubsystem<Input>();
  257. IntVector2 cpos = input->GetMousePosition();
  258. IntRect rect = GetRect();
  259. if (!rect.Width() || !rect.Height())
  260. return camRay;
  261. int x = rect.left_;
  262. int y = rect.top_;
  263. GetInternalWidget()->ConvertToRoot(x, y);
  264. return camera_->GetScreenRay(float(cpos.x_ - x) / rect.Width(),
  265. float(cpos.y_ - y) / rect.Height());
  266. }
  267. bool SceneView3D::MouseInView()
  268. {
  269. if (!GetInternalWidget())
  270. return false;
  271. if (!TBWidget::hovered_widget || TBWidget::hovered_widget->GetDelegate() != this)
  272. return false;
  273. Input* input = GetSubsystem<Input>();
  274. IntVector2 pos = input->GetMousePosition();
  275. IntRect rect = GetRect();
  276. GetInternalWidget()->ConvertToRoot(rect.left_, rect.top_);
  277. GetInternalWidget()->ConvertToRoot(rect.right_, rect.bottom_);
  278. return rect.IsInside(pos);
  279. }
  280. void SceneView3D::HandleUIUnhandledShortcut(StringHash eventType, VariantMap& eventData)
  281. {
  282. if (!enabled_)
  283. return;
  284. unsigned id = eventData[UIUnhandledShortcut::P_REFID].GetUInt();
  285. if (id == TBIDC("undo"))
  286. sceneEditor_->Undo();
  287. else if (id == TBIDC("redo"))
  288. sceneEditor_->Redo();
  289. else if (id == TBIDC("copy"))
  290. sceneEditor_->Copy();
  291. else if (id == TBIDC("cut"))
  292. sceneEditor_->Cut();
  293. else if (id == TBIDC("paste"))
  294. sceneEditor_->Paste();
  295. return;
  296. }
  297. void SceneView3D::HandleUIWidgetFocusEscaped(StringHash eventType, VariantMap& eventData)
  298. {
  299. if (!enabled_)
  300. return;
  301. SetFocus();
  302. }
  303. void SceneView3D::HandlePostRenderUpdate(StringHash eventType, VariantMap& eventData)
  304. {
  305. if (!MouseInView() || GetOrbitting())
  306. return;
  307. Input* input = GetSubsystem<Input>();
  308. mouseLeftDown_ = false;
  309. bool shiftDown = input->GetKeyDown(KEY_LSHIFT) || input->GetKeyDown(KEY_RSHIFT);
  310. if (input->GetMouseButtonPress(MOUSEB_LEFT))
  311. {
  312. SetFocus();
  313. if (!mouseMoved_ && !sceneEditor_->GetGizmo()->Selected())
  314. {
  315. Ray camRay = GetCameraRay();
  316. PODVector<RayQueryResult> result;
  317. RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_GEOMETRY, 0x7fffffff);
  318. octree_->RaycastSingle(query);
  319. if (query.result_.Size())
  320. {
  321. const RayQueryResult& r = result[0];
  322. if (r.drawable_)
  323. {
  324. VariantMap neventData;
  325. Node* node = r.drawable_->GetNode();
  326. // if temporary, this is a prefab
  327. // TODO: if we use temporary for other stuff
  328. // fix this to look for prefab
  329. if (node->IsTemporary())
  330. node = node->GetParent();
  331. if (sceneEditor_->GetSelection()->Contains(node) && shiftDown)
  332. {
  333. sceneEditor_->GetSelection()->RemoveNode(node);
  334. }
  335. else
  336. {
  337. sceneEditor_->GetSelection()->AddNode(node, !shiftDown);
  338. }
  339. }
  340. }
  341. else
  342. {
  343. sceneEditor_->GetSelection()->Clear();
  344. }
  345. }
  346. mouseMoved_ = false;
  347. }
  348. else if (!input->GetMouseButtonDown(MOUSEB_LEFT))
  349. {
  350. Ray camRay = GetCameraRay();
  351. PODVector<RayQueryResult> result;
  352. mouseMoved_ = false;
  353. /*
  354. Array<int> pickModeDrawableFlags = {
  355. DRAWABLE_GEOMETRY,
  356. DRAWABLE_LIGHT,
  357. DRAWABLE_ZONE
  358. };
  359. */
  360. RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_GEOMETRY, 0x7fffffff);
  361. octree_->RaycastSingle(query);
  362. if (query.result_.Size())
  363. {
  364. const RayQueryResult& r = result[0];
  365. if (r.drawable_)
  366. {
  367. debugRenderer_->AddNode(r.drawable_->GetNode(), 1.0, false);
  368. r.drawable_->DrawDebugGeometry(debugRenderer_, false);
  369. }
  370. }
  371. }
  372. else
  373. {
  374. mouseLeftDown_ = true;
  375. if (Abs(input->GetMouseMoveX() > 3 || input->GetMouseMoveY() > 3))
  376. {
  377. mouseMoved_ = true;
  378. }
  379. }
  380. }
  381. bool SceneView3D::OnEvent(const TBWidgetEvent &ev)
  382. {
  383. if (ev.type == EVENT_TYPE_SHORTCUT)
  384. {
  385. if (ev.ref_id == TBIDC("close"))
  386. return false;
  387. }
  388. if (ev.type == EVENT_TYPE_KEY_UP)
  389. {
  390. if (ev.special_key == TB_KEY_ESC)
  391. {
  392. sceneEditor_->GetSelection()->Clear();
  393. }
  394. }
  395. return sceneEditor_->OnEvent(ev);
  396. }
  397. void SceneView3D::HandleUpdate(StringHash eventType, VariantMap& eventData)
  398. {
  399. // parent is the contentRoot for our tab, when tab isn't active it will not be visible
  400. if (!GetParent() || GetParent()->GetVisibility() != UI_WIDGET_VISIBILITY_VISIBLE)
  401. {
  402. Disable();
  403. return;
  404. }
  405. Enable();
  406. // Timestep parameter is same no matter what event is being listened to
  407. float timeStep = eventData[Update::P_TIMESTEP].GetFloat();
  408. MoveCamera(timeStep);
  409. QueueUpdate();
  410. if (preloadResourceScene_.NotNull())
  411. {
  412. if (preloadResourceScene_->GetAsyncProgress() == 1.0f)
  413. {
  414. ResourceCache* cache = GetSubsystem<ResourceCache>();
  415. XMLFile* xml = cache->GetResource<XMLFile>(dragAssetGUID_);
  416. if (dragNode_.NotNull())
  417. {
  418. dragNode_->LoadXML(xml->GetRoot());
  419. UpdateDragNode(0, 0);
  420. AnimationController* controller = dragNode_->GetComponent<AnimationController>();
  421. if (controller)
  422. {
  423. controller->PlayExclusive("Idle", 0, true);
  424. dragNode_->GetScene()->SetUpdateEnabled(true);
  425. }
  426. }
  427. preloadResourceScene_ = 0;
  428. dragAssetGUID_ = "";
  429. }
  430. }
  431. }
  432. void SceneView3D::UpdateDragNode(int mouseX, int mouseY)
  433. {
  434. if (dragNode_.Null())
  435. return;
  436. Ray ray = GetCameraRay();
  437. Vector3 pos = ray.origin_;
  438. pos += ray.direction_ * 10;
  439. dragNode_->SetWorldPosition(pos);
  440. }
  441. void SceneView3D::HandleMouseMove(StringHash eventType, VariantMap& eventData)
  442. {
  443. if (dragNode_.Null())
  444. return;
  445. Input* input = GetSubsystem<Input>();
  446. if (!input->IsMouseVisible())
  447. return;
  448. using namespace MouseMove;
  449. int x = eventData[P_X].GetInt();
  450. int y = eventData[P_Y].GetInt();
  451. UpdateDragNode(x, y);
  452. }
  453. void SceneView3D::HandleDragEnterWidget(StringHash eventType, VariantMap& eventData)
  454. {
  455. using namespace DragEnterWidget;
  456. UIWidget* widget = static_cast<UIWidget*>(eventData[P_WIDGET].GetPtr());
  457. if (widget != this)
  458. return;
  459. UIDragObject* dragObject = static_cast<UIDragObject*>(eventData[P_DRAGOBJECT].GetPtr());
  460. Object* object = dragObject->GetObject();
  461. if (!object)
  462. return;
  463. if (object->GetType() == Asset::GetTypeStatic())
  464. {
  465. Asset* asset = (Asset*) object;
  466. dragNode_ = asset->InstantiateNode(scene_, asset->GetName());
  467. if (dragNode_.NotNull())
  468. {
  469. Input* input = GetSubsystem<Input>();
  470. IntVector2 pos = input->GetMousePosition();
  471. UpdateDragNode(pos.x_, pos.y_);
  472. }
  473. //LOGINFOF("Dropped %s : %s on SceneView3D", asset->GetPath().CString(), asset->GetGUID().CString());
  474. }
  475. }
  476. void SceneView3D::HandleDragExitWidget(StringHash eventType, VariantMap& eventData)
  477. {
  478. if (preloadResourceScene_.NotNull())
  479. {
  480. preloadResourceScene_->StopAsyncLoading();
  481. preloadResourceScene_ = 0;
  482. }
  483. if (dragNode_.NotNull())
  484. {
  485. scene_->RemoveChild(dragNode_);
  486. }
  487. dragAssetGUID_ = "";
  488. dragNode_ = 0;
  489. }
  490. void SceneView3D::HandleDragEnded(StringHash eventType, VariantMap& eventData)
  491. {
  492. using namespace DragEnded;
  493. UIDragObject* dragObject = static_cast<UIDragObject*>(eventData[P_DRAGOBJECT].GetPtr());
  494. if (dragNode_.NotNull())
  495. {
  496. VariantMap nodeCreatedEvent;
  497. nodeCreatedEvent[SceneEditNodeCreated::P_NODE] = dragNode_;
  498. scene_->SendEvent(E_SCENEEDITNODECREATED, nodeCreatedEvent);
  499. }
  500. if (dragObject && dragObject->GetObject()->GetType() == ToolCore::Asset::GetTypeStatic())
  501. {
  502. Asset* asset = (ToolCore::Asset*) dragObject->GetObject();
  503. if (asset->GetImporterTypeName() == "MaterialImporter") {
  504. Material* material = GetSubsystem<ResourceCache>()->GetResource<Material>(asset->GetPath());
  505. if (material) {
  506. material = material;
  507. Ray camRay = GetCameraRay();
  508. PODVector<RayQueryResult> result;
  509. RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_GEOMETRY, 0x7fffffff);
  510. octree_->RaycastSingle(query);
  511. if (query.result_.Size())
  512. {
  513. const RayQueryResult& r = result[0];
  514. if (r.drawable_ && (r.drawable_->GetType() == StaticModel::GetTypeStatic() || r.drawable_->GetType() == AnimatedModel::GetTypeStatic()))
  515. {
  516. ((StaticModel*)r.drawable_)->SetMaterial(material);
  517. }
  518. }
  519. }
  520. }
  521. }
  522. dragAssetGUID_ = "";
  523. dragNode_ = 0;
  524. }
  525. void SceneView3D::FrameSelection()
  526. {
  527. BoundingBox bbox;
  528. sceneEditor_->GetSelection()->GetBounds(bbox);
  529. if (!bbox.defined_)
  530. return;
  531. Sphere sphere(bbox);
  532. if (sphere.radius_ < .01f || sphere.radius_ > 512)
  533. return;
  534. framedBBox_ = bbox;
  535. cameraMoveStart_ = cameraNode_->GetWorldPosition();
  536. cameraMoveTarget_ = bbox.Center() - (cameraNode_->GetWorldDirection() * sphere.radius_ * 3);
  537. cameraMoveTime_ = 0.0f;
  538. cameraMove_ = true;
  539. }
  540. }