particles.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. * Copyright 2011-2025 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include "common.h"
  6. #include "bgfx_utils.h"
  7. #include <entry/cmd.h>
  8. #include <entry/input.h>
  9. #include <camera.h>
  10. #include <debugdraw/debugdraw.h>
  11. #include <imgui/imgui.h>
  12. #include <bx/rng.h>
  13. #include <bx/easing.h>
  14. #include <ps/particle_system.h>
  15. namespace
  16. {
  17. static const char* s_shapeNames[] =
  18. {
  19. "Sphere",
  20. "Hemisphere",
  21. "Circle",
  22. "Disc",
  23. "Rect",
  24. };
  25. static const char* s_directionName[] =
  26. {
  27. "Up",
  28. "Outward",
  29. };
  30. static const char* s_easeFuncName[] =
  31. {
  32. "Linear",
  33. "Step",
  34. "SmoothStep",
  35. "InQuad",
  36. "OutQuad",
  37. "InOutQuad",
  38. "OutInQuad",
  39. "InCubic",
  40. "OutCubic",
  41. "InOutCubic",
  42. "OutInCubic",
  43. "InQuart",
  44. "OutQuart",
  45. "InOutQuart",
  46. "OutInQuart",
  47. "InQuint",
  48. "OutQuint",
  49. "InOutQuint",
  50. "OutInQuint",
  51. "InSine",
  52. "OutSine",
  53. "InOutSine",
  54. "OutInSine",
  55. "InExpo",
  56. "OutExpo",
  57. "InOutExpo",
  58. "OutInExpo",
  59. "InCirc",
  60. "OutCirc",
  61. "InOutCirc",
  62. "OutInCirc",
  63. "InElastic",
  64. "OutElastic",
  65. "InOutElastic",
  66. "OutInElastic",
  67. "InBack",
  68. "OutBack",
  69. "InOutBack",
  70. "OutInBack",
  71. "InBounce",
  72. "OutBounce",
  73. "InOutBounce",
  74. "OutInBounce",
  75. };
  76. static_assert(BX_COUNTOF(s_easeFuncName) == bx::Easing::Count);
  77. struct Emitter
  78. {
  79. EmitterUniforms m_uniforms;
  80. EmitterHandle m_handle;
  81. EmitterShape::Enum m_shape;
  82. EmitterDirection::Enum m_direction;
  83. void create()
  84. {
  85. m_shape = EmitterShape::Sphere;
  86. m_direction = EmitterDirection::Outward;
  87. m_handle = psCreateEmitter(m_shape, m_direction, 1024);
  88. m_uniforms.reset();
  89. }
  90. void destroy()
  91. {
  92. psDestroyEmitter(m_handle);
  93. }
  94. void update()
  95. {
  96. psUpdateEmitter(m_handle, &m_uniforms);
  97. }
  98. void imgui()
  99. {
  100. // if (ImGui::CollapsingHeader("General") )
  101. {
  102. if (ImGui::Combo("Shape", (int*)&m_shape, s_shapeNames, BX_COUNTOF(s_shapeNames) )
  103. || ImGui::Combo("Direction", (int*)&m_direction, s_directionName, BX_COUNTOF(s_directionName) ) )
  104. {
  105. psDestroyEmitter(m_handle);
  106. m_handle = psCreateEmitter(m_shape, m_direction, 1024);
  107. }
  108. ImGui::SliderInt("particles / s", (int*)&m_uniforms.m_particlesPerSecond, 0, 1024);
  109. ImGui::SliderFloat("Gravity scale"
  110. , &m_uniforms.m_gravityScale
  111. , -2.0f
  112. , 2.0f
  113. );
  114. ImGui::RangeSliderFloat("Life span"
  115. , &m_uniforms.m_lifeSpan[0]
  116. , &m_uniforms.m_lifeSpan[1]
  117. , 0.1f
  118. , 5.0f
  119. );
  120. if (ImGui::Button("Reset") )
  121. {
  122. psUpdateEmitter(m_handle);
  123. }
  124. }
  125. if (ImGui::CollapsingHeader("Position and scale") )
  126. {
  127. ImGui::Combo("Position Ease", (int*)&m_uniforms.m_easePos, s_easeFuncName, BX_COUNTOF(s_easeFuncName) );
  128. ImGui::RangeSliderFloat("Start offset"
  129. , &m_uniforms.m_offsetStart[0]
  130. , &m_uniforms.m_offsetStart[1]
  131. , 0.0f
  132. , 10.0f
  133. );
  134. ImGui::RangeSliderFloat("End offset"
  135. , &m_uniforms.m_offsetEnd[0]
  136. , &m_uniforms.m_offsetEnd[1]
  137. , 0.0f
  138. , 10.0f
  139. );
  140. ImGui::Text("Scale:");
  141. ImGui::Combo("Scale Ease", (int*)&m_uniforms.m_easeScale, s_easeFuncName, BX_COUNTOF(s_easeFuncName) );
  142. ImGui::RangeSliderFloat("Scale Start"
  143. , &m_uniforms.m_scaleStart[0]
  144. , &m_uniforms.m_scaleStart[1]
  145. , 0.0f
  146. , 3.0f
  147. );
  148. ImGui::RangeSliderFloat("Scale End"
  149. , &m_uniforms.m_scaleEnd[0]
  150. , &m_uniforms.m_scaleEnd[1]
  151. , 0.0f
  152. , 3.0f
  153. );
  154. }
  155. if (ImGui::CollapsingHeader("Blending and color") )
  156. {
  157. ImGui::Combo("Blend Ease", (int*)&m_uniforms.m_easeBlend, s_easeFuncName, BX_COUNTOF(s_easeFuncName) );
  158. ImGui::RangeSliderFloat("Blend Start"
  159. , &m_uniforms.m_blendStart[0]
  160. , &m_uniforms.m_blendStart[1]
  161. , 0.0f
  162. , 1.0f
  163. );
  164. ImGui::RangeSliderFloat("Blend End"
  165. , &m_uniforms.m_blendEnd[0]
  166. , &m_uniforms.m_blendEnd[1]
  167. , 0.0f
  168. , 1.0f
  169. );
  170. ImGui::Text("Color:");
  171. ImGui::Combo("RGBA Ease", (int*)&m_uniforms.m_easeRgba, s_easeFuncName, BX_COUNTOF(s_easeFuncName) );
  172. ImGui::ColorWheel("RGBA0", &m_uniforms.m_rgba[0], 0.3f);
  173. ImGui::ColorWheel("RGBA1", &m_uniforms.m_rgba[1], 0.3f);
  174. ImGui::ColorWheel("RGBA2", &m_uniforms.m_rgba[2], 0.3f);
  175. ImGui::ColorWheel("RGBA3", &m_uniforms.m_rgba[3], 0.3f);
  176. ImGui::ColorWheel("RGBA4", &m_uniforms.m_rgba[4], 0.3f);
  177. }
  178. }
  179. void gizmo(const float* _view, const float* _proj)
  180. {
  181. float mtx[16];
  182. float scale[3] = { 1.0f, 1.0f, 1.0f };
  183. ImGuizmo::RecomposeMatrixFromComponents(m_uniforms.m_position, m_uniforms.m_angle, scale, mtx);
  184. ImGuiIO& io = ImGui::GetIO();
  185. ImGuizmo::SetRect(0, 0, io.DisplaySize.x, io.DisplaySize.y);
  186. ImGuizmo::Manipulate(
  187. _view
  188. , _proj
  189. , ImGuizmo::TRANSLATE
  190. , ImGuizmo::LOCAL
  191. , mtx
  192. );
  193. ImGuizmo::DecomposeMatrixToComponents(mtx, m_uniforms.m_position, m_uniforms.m_angle, scale);
  194. }
  195. };
  196. class ExampleParticles : public entry::AppI
  197. {
  198. public:
  199. ExampleParticles(const char* _name, const char* _description, const char* _url)
  200. : entry::AppI(_name, _description, _url)
  201. {
  202. }
  203. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  204. {
  205. Args args(_argc, _argv);
  206. m_width = _width;
  207. m_height = _height;
  208. m_debug = BGFX_DEBUG_NONE;
  209. m_reset = BGFX_RESET_VSYNC;
  210. bgfx::Init init;
  211. init.type = args.m_type;
  212. init.vendorId = args.m_pciId;
  213. init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  214. init.platformData.ndt = entry::getNativeDisplayHandle();
  215. init.platformData.type = entry::getNativeWindowHandleType();
  216. init.resolution.width = m_width;
  217. init.resolution.height = m_height;
  218. init.resolution.reset = m_reset;
  219. bgfx::init(init);
  220. // Enable m_debug text.
  221. bgfx::setDebug(m_debug);
  222. // Set view 0 clear state.
  223. bgfx::setViewClear(0
  224. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  225. , 0x202020ff
  226. , 1.0f
  227. , 0
  228. );
  229. ddInit();
  230. psInit();
  231. bimg::ImageContainer* image = imageLoad(
  232. "textures/particle.ktx"
  233. , bgfx::TextureFormat::BGRA8
  234. );
  235. EmitterSpriteHandle sprite = psCreateSprite(
  236. uint16_t(image->m_width)
  237. , uint16_t(image->m_height)
  238. , image->m_data
  239. );
  240. bimg::imageFree(image);
  241. for (uint32_t ii = 0; ii < BX_COUNTOF(m_emitter); ++ii)
  242. {
  243. m_emitter[ii].create();
  244. m_emitter[ii].m_uniforms.m_handle = sprite;
  245. m_emitter[ii].update();
  246. }
  247. imguiCreate();
  248. cameraCreate();
  249. cameraSetPosition({ 0.0f, 2.0f, -12.0f });
  250. cameraSetVerticalAngle(0.0f);
  251. m_timeOffset = bx::getHPCounter();
  252. }
  253. virtual int shutdown() override
  254. {
  255. for (uint32_t ii = 0; ii < BX_COUNTOF(m_emitter); ++ii)
  256. {
  257. m_emitter[ii].destroy();
  258. }
  259. psShutdown();
  260. ddShutdown();
  261. imguiDestroy();
  262. cameraDestroy();
  263. // Shutdown bgfx.
  264. bgfx::shutdown();
  265. return 0;
  266. }
  267. bool update() override
  268. {
  269. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  270. {
  271. // Set view 0 default viewport.
  272. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  273. bgfx::touch(0);
  274. int64_t now = bx::getHPCounter() - m_timeOffset;
  275. static int64_t last = now;
  276. const int64_t frameTime = now - last;
  277. last = now;
  278. const double freq = double(bx::getHPFrequency() );
  279. const float deltaTime = float(frameTime/freq);
  280. cameraUpdate(deltaTime, m_mouseState, ImGui::MouseOverArea() );
  281. float view[16];
  282. cameraGetViewMtx(view);
  283. float proj[16];
  284. // Set view and projection matrix for view 0.
  285. {
  286. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  287. bgfx::setViewTransform(0, view, proj);
  288. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  289. }
  290. imguiBeginFrame(
  291. m_mouseState.m_mx
  292. , m_mouseState.m_my
  293. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  294. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  295. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  296. , m_mouseState.m_mz
  297. , uint16_t(m_width)
  298. , uint16_t(m_height)
  299. );
  300. showExampleDialog(this);
  301. ImGui::SetNextWindowPos(
  302. ImVec2(m_width - m_width / 4.0f - 10.0f, 10.0f)
  303. , ImGuiCond_FirstUseEver
  304. );
  305. ImGui::SetNextWindowSize(
  306. ImVec2(m_width / 4.0f, m_height - 20.0f)
  307. , ImGuiCond_FirstUseEver
  308. );
  309. ImGui::Begin("Settings"
  310. , NULL
  311. );
  312. static float timeScale = 1.0f;
  313. ImGui::SliderFloat("Time scale"
  314. , &timeScale
  315. , 0.0f
  316. , 1.0f
  317. );
  318. static bool showBounds;
  319. ImGui::Checkbox("Show bounds", &showBounds);
  320. ImGui::Text("Emitter:");
  321. static int currentEmitter = 0;
  322. for (uint32_t ii = 0; ii < BX_COUNTOF(m_emitter); ++ii)
  323. {
  324. ImGui::SameLine();
  325. char name[16];
  326. bx::snprintf(name, BX_COUNTOF(name), "%d", ii);
  327. ImGui::RadioButton(name, &currentEmitter, ii);
  328. }
  329. m_emitter[currentEmitter].imgui();
  330. ImGui::End();
  331. m_emitter[currentEmitter].gizmo(view, proj);
  332. imguiEndFrame();
  333. DebugDrawEncoder dde;
  334. dde.begin(0);
  335. dde.drawGrid(Axis::Y, { 0.0f, 0.0f, 0.0f });
  336. const bx::Vec3 eye = cameraGetPosition();
  337. m_emitter[currentEmitter].update();
  338. psUpdate(deltaTime * timeScale);
  339. psRender(0, view, eye);
  340. if (showBounds)
  341. {
  342. bx::Aabb aabb;
  343. psGetAabb(m_emitter[currentEmitter].m_handle, aabb);
  344. dde.push();
  345. dde.setWireframe(true);
  346. dde.setColor(0xff0000ff);
  347. dde.draw(aabb);
  348. dde.pop();
  349. }
  350. dde.end();
  351. // Advance to next frame. Rendering thread will be kicked to
  352. // process submitted rendering primitives.
  353. bgfx::frame();
  354. return true;
  355. }
  356. return false;
  357. }
  358. entry::MouseState m_mouseState;
  359. int64_t m_timeOffset;
  360. uint32_t m_width;
  361. uint32_t m_height;
  362. uint32_t m_debug;
  363. uint32_t m_reset;
  364. Emitter m_emitter[4];
  365. };
  366. } // namespace
  367. ENTRY_IMPLEMENT_MAIN(
  368. ExampleParticles
  369. , "32-particles"
  370. , "Particles."
  371. , "https://bkaradzic.github.io/bgfx/examples.html#particles"
  372. );