Skip to content

dannybanno/imgui-particle-background

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 

Repository files navigation

Particle background for imgui

always liked the look of these sort of background so thought id have a go at making one and think it has came out pretty good

Usage

Initializes the particles setting the x and y to random and sets the velocity which will be used later to set the position

  static bool initialized = false;
	if (!initialized)
	{
		for (int i = 0; i < numParticles; ++i)
		{
			particlePositions[i] = ImVec2(
				ImGui::GetWindowPos().x + ImGui::GetWindowSize().x * static_cast<float>(rand()) / RAND_MAX,
				ImGui::GetWindowPos().y + ImGui::GetWindowSize().y * static_cast<float>(rand()) / RAND_MAX
			);

			particleVelocities[i] = ImVec2(
				static_cast<float>((rand() % 11) - 5),
				static_cast<float>((rand() % 11) - 5)
			);

		}

		initialized = true;
	}

Draws the lines to the cursor and particles depending on the distance

  ImVec2 cursorPos = ImGui::GetIO().MousePos;
	for (int i = 0; i < numParticles; ++i)
	{
		//draw lines to particles
		for (int j = i + 1; j < numParticles; ++j)
		{
			float distance = std::hypotf(particlePositions[j].x - particlePositions[i].x, particlePositions[j].y - particlePositions[i].y);
			float opacity = 1.0f - (distance / 55.0f);  // opacity cahnge

			if (opacity > 0.0f)
			{
				ImU32 lineColor = ImGui::GetColorU32(ImVec4(1.0f, 1.0f, 1.0f, opacity));
				drawList->AddLine(particlePositions[i], particlePositions[j], lineColor);
			}
		}

		//draw lines to cursor
		float distanceToCursor = std::hypotf(cursorPos.x - particlePositions[i].x, cursorPos.y - particlePositions[i].y);
		float opacityToCursor = 1.0f - (distanceToCursor / 52.0f);  // Adjust the divisor to control the opacity change

		if (opacityToCursor > 0.0f)
		{
			ImU32 lineColorToCursor = ImGui::GetColorU32(ImVec4(1.0f, 1.0f, 1.0f, opacityToCursor));
			drawList->AddLine(cursorPos, particlePositions[i], lineColorToCursor);
		}
	}

Updates adn renders the particles setting the positions, keeping them in bound of the window and setting the colour

float deltaTime = ImGui::GetIO().DeltaTime;
	for (int i = 0; i < numParticles; ++i)
	{
		particlePositions[i].x += particleVelocities[i].x * deltaTime;
		particlePositions[i].y += particleVelocities[i].y * deltaTime;

		// Stay in window
		if (particlePositions[i].x < ImGui::GetWindowPos().x)
			particlePositions[i].x = ImGui::GetWindowPos().x + ImGui::GetWindowSize().x;
		else if (particlePositions[i].x > ImGui::GetWindowPos().x + ImGui::GetWindowSize().x)
			particlePositions[i].x = ImGui::GetWindowPos().x;

		if (particlePositions[i].y < ImGui::GetWindowPos().y)
			particlePositions[i].y = ImGui::GetWindowPos().y + ImGui::GetWindowSize().y;
		else if (particlePositions[i].y > ImGui::GetWindowPos().y + ImGui::GetWindowSize().y)
			particlePositions[i].y = ImGui::GetWindowPos().y;

		ImU32 particleColour = ImGui::ColorConvertFloat4ToU32(settings::particleColour);

		//render particles behind components
		drawList->AddCircleFilled(particlePositions[i], 1.5f, particleColour);
	}
credits to cazzwastaken for the base of the imgui: https://github.com/cazzwastaken/borderless-imgui-window