#include "FlyTrace.h" #include #include namespace ava2 { FlyTrace::FlyTrace() = default; FlyTrace::~FlyTrace() = default; void FlyTrace::Create(uint32_t color, float tailLength, float tailSize, bool rectShape) { m_color = color; m_tailLength = tailLength > 0.01f ? tailLength : 0.25f; m_tailSize = tailSize > 0.1f ? tailSize : 10.0f; m_rectShape = rectShape; m_history.clear(); } void FlyTrace::Clear() { m_history.clear(); } void FlyTrace::UpdateNewPosition(float x, float y, float z, float currentTime) { TraceNode node; node.time = currentTime; node.pos[0] = x; node.pos[1] = y; node.pos[2] = z; m_history.push_front(node); while (!m_history.empty() && (currentTime - m_history.back().time) > m_tailLength) { m_history.pop_back(); } } void FlyTrace::BuildDrawCommands(const float* viewMatrix, const float* projMatrix, float currentTime, std::vector& outDraws) { if (m_history.size() < 2) return; Render3DDraw draw; draw.lighting = false; draw.z_enable = true; draw.z_write = false; draw.cull_mode = 1; // D3DCULL_NONE draw.alpha_blend = true; draw.blend_mode = 1; // Additive (D3DBLEND_ONE) for (int m = 0; m < 16; ++m) { draw.view[m] = viewMatrix[m]; draw.proj[m] = projMatrix[m]; draw.world[m] = (m % 5 == 0) ? 1.0f : 0.0f; } float camDir[3] = {viewMatrix[2], viewMatrix[6], viewMatrix[10]}; float camLen = std::sqrt(camDir[0]*camDir[0] + camDir[1]*camDir[1] + camDir[2]*camDir[2]); if (camLen > 1e-5f) { camDir[0] /= camLen; camDir[1] /= camLen; camDir[2] /= camLen; } uint32_t vertexOffset = 0; for (size_t i = 0; i + 1 < m_history.size(); ++i) { const auto& n0 = m_history[i]; const auto& n1 = m_history[i + 1]; float age0 = currentTime - n0.time; float age1 = currentTime - n1.time; float rate0 = std::clamp(1.0f - age0 / m_tailLength, 0.0f, 1.0f); float rate1 = std::clamp(1.0f - age1 / m_tailLength, 0.0f, 1.0f); float s0 = m_rectShape ? m_tailSize : m_tailSize * rate0; float s1 = m_rectShape ? m_tailSize : m_tailSize * rate1; // Ribbon segment direction float seg[3] = {n1.pos[0] - n0.pos[0], n1.pos[1] - n0.pos[1], n1.pos[2] - n0.pos[2]}; float segLen = std::sqrt(seg[0]*seg[0] + seg[1]*seg[1] + seg[2]*seg[2]); if (segLen < 1e-4f) continue; // Normal perpendicular to segment and camera look float norm[3] = { seg[1] * camDir[2] - seg[2] * camDir[1], seg[2] * camDir[0] - seg[0] * camDir[2], seg[0] * camDir[1] - seg[1] * camDir[0] }; float normLen = std::sqrt(norm[0]*norm[0] + norm[1]*norm[1] + norm[2]*norm[2]); if (normLen > 1e-5f) { norm[0] /= normLen; norm[1] /= normLen; norm[2] /= normLen; } // Vertices for this quad segment float v0[3] = {n0.pos[0] + norm[0] * s0, n0.pos[1] + norm[1] * s0, n0.pos[2] + norm[2] * s0}; float v1[3] = {n0.pos[0] - norm[0] * s0, n0.pos[1] - norm[1] * s0, n0.pos[2] - norm[2] * s0}; float v2[3] = {n1.pos[0] - norm[0] * s1, n1.pos[1] - norm[1] * s1, n1.pos[2] - norm[2] * s1}; float v3[3] = {n1.pos[0] + norm[0] * s1, n1.pos[1] + norm[1] * s1, n1.pos[2] + norm[2] * s1}; uint8_t baseA = (m_color >> 24) & 0xFF; uint8_t baseR = (m_color >> 16) & 0xFF; uint8_t baseG = (m_color >> 8) & 0xFF; uint8_t baseB = m_color & 0xFF; uint8_t a0 = static_cast(baseA * rate0); uint8_t a1 = static_cast(baseA * rate1); uint32_t diff0 = (static_cast(a0) << 24) | (static_cast(baseR) << 16) | (static_cast(baseG) << 8) | baseB; uint32_t diff1 = (static_cast(a1) << 24) | (static_cast(baseR) << 16) | (static_cast(baseG) << 8) | baseB; draw.positions.insert(draw.positions.end(), {v0[0], v0[1], v0[2]}); draw.positions.insert(draw.positions.end(), {v1[0], v1[1], v1[2]}); draw.positions.insert(draw.positions.end(), {v2[0], v2[1], v2[2]}); draw.positions.insert(draw.positions.end(), {v3[0], v3[1], v3[2]}); draw.diffuse.insert(draw.diffuse.end(), {diff0, diff0, diff1, diff1}); draw.uv0.insert(draw.uv0.end(), {0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f}); draw.indices.push_back(vertexOffset + 0); draw.indices.push_back(vertexOffset + 1); draw.indices.push_back(vertexOffset + 2); draw.indices.push_back(vertexOffset + 0); draw.indices.push_back(vertexOffset + 2); draw.indices.push_back(vertexOffset + 3); vertexOffset += 4; } if (!draw.indices.empty()) { outDraws.push_back(std::move(draw)); } } } // namespace ava2