-- GENERATED by tools/love.sh from the Luau source - do not edit local function __iter(t, ...) if type(t) ~= 'table' or (getmetatable(t) and getmetatable(t).__call) then return t, ... end local n, i, k = #t, 0, nil return function() if i < n then i = i + 1 return i, t[i] end repeat k = next(t, k) until k == nil or type(k) ~= 'number' or k < 1 or k > n or k % 1 ~= 0 if k ~= nil then return k, t[k] end end end table.create = table.create or function(n, v) local t = {} if v ~= nil then for i = 1, n do t[i] = v end end return t end table.clone = table.clone or function(t) local c = {} for k, v in pairs(t) do c[k] = v end return setmetatable(c, getmetatable(t)) end math.clamp = math.clamp or function(x, a, b) return math.max(a, math.min(b, x)) end math.sign = math.sign or function(x) return x > 0 and 1 or (x < 0 and -1 or 0) end math.round = math.round or function(x) return x >= 0 and math.floor(x + 0.5) or math.ceil(x - 0.5) end -- ImPlot3D demo (Luau port of implot3d_demo.cpp). Optional: ImPlot3D.lua never calls into this file. -- Usage: require(path.to.ImPlot3DDemo)(G, ImPlot3D) -- defines ImPlot3D.ShowDemoWindow(p_open) -> new p_open, -- ImPlot3D.ShowAllDemos(), ImPlot3D.ShowStyleEditor(ref), ImPlot3D.ShowAboutWindow(p_open) -> new p_open -- [Port] Lua has no function-level 'static': they are fields of the file-level table S (S.FunctionName.x). -- [Port] Demo* functions live in the table D (keeps the file chunk under the 200-locals limit). -- [Port] Specs are plain tables of ImPlot3DSpec fields; arrays are 1-based; Stride/Offset are in elements. return function(G, ImPlot3D) local ImGui, ImVec2, ImVec4 = G.ImGui, G.ImVec2, G.ImVec4 local IM_COL32 = G.IM_COL32 local IM_PI = math.pi local P = ImPlot3D.ImPlot3DPoint local ImAxis3D = ImPlot3D.ImAxis3D local Flags = ImPlot3D.ImPlot3DFlags local AxisFlags = ImPlot3D.ImPlot3DAxisFlags local Marker = ImPlot3D.ImPlot3DMarker local Cond = ImPlot3D.ImPlot3DCond local Colormap = ImPlot3D.ImPlot3DColormap local LineFlags = ImPlot3D.ImPlot3DLineFlags local TriFlags = ImPlot3D.ImPlot3DTriangleFlags local QuadFlags = ImPlot3D.ImPlot3DQuadFlags local SurfFlags = ImPlot3D.ImPlot3DSurfaceFlags local MeshFlags = ImPlot3D.ImPlot3DMeshFlags local Meshes = ImPlot3D.Meshes local band, bor = bit32.band, bit32.bor local _ local S = {} local D = {} -- [Port] IMGUI_DEMO_MARKER: forwarded to ImGui.DemoMarker when available (no __LINE__ in Lua: line is 0) local function IMGUI_DEMO_MARKER(section) if ImGui.DemoMarker then ImGui.DemoMarker("implot3d_demo.cpp", 0, section) end end -- [Port] CHECKBOX_FLAG(flags, flag) macro: label is the upstream flag name; returns the new flags local function CHECKBOX_FLAG(flags, name, flag) local _, v = ImGui.CheckboxFlags(name, flags, flag) return v end local function HelpMarker(desc) ImGui.TextDisabled("(?)") if ImGui.BeginItemTooltip() then ImGui.PushTextWrapPos(ImGui.GetFontSize() * 35.0) ImGui.TextUnformatted(desc) ImGui.PopTextWrapPos() ImGui.EndTooltip() end end -- [Port] rand()/RAND_MAX -> deterministic LCG so srand(0) sections repeat every frame like upstream local seed = 0 local function srand(s) seed = s end local function rand01() seed = (seed * 1103515245 + 12345) % 2147483648 return seed / 2147483647 end -- [Port] &arr[base] pointer arithmetic -> read-only proxy (0-based base) local function Ptr(arr, base) return setmetatable({}, { __index = function(_, i) return arr[i + base] end }) end -- Utility structure for realtime plot local function ScrollingBuffer(max_size) return { MaxSize = max_size or 2000, Offset = 0, Data = {} } end local function AddPoint(b, x) if #b.Data < b.MaxSize then table.insert(b.Data, x) else b.Data[b.Offset + 1] = x b.Offset = (b.Offset + 1) % b.MaxSize end end -- Custom axis formatter that adds metric prefixes (G, M, k, m, u, n) -- [Port] ImPlot3DFormatter is function(value, data) -> string local metric_v = { 1000000000, 1000000, 1000, 1, 0.001, 0.000001, 0.000000001 } local metric_p = { "G", "M", "k", "", "m", "u", "n" } local function MetricFormatter(value, unit) if value == 0 then return "0 " .. unit end for i = 1, 7 do if math.abs(value) >= metric_v[i] then return string.format("%g %s%s", value / metric_v[i], metric_p[i], unit) end end return string.format("%g %s%s", value / metric_v[7], metric_p[7], unit) end local function HSV_U32(h, s, v) return G.ImColor.HSV(h, s, v):U32() end --------------------------------------------------------------------------- -- [SECTION] Plots --------------------------------------------------------------------------- S.DemoLinePlots = { xs1 = {}, ys1 = {}, zs1 = {}, xs2 = {}, ys2 = {}, zs2 = {} } function D.DemoLinePlots() IMGUI_DEMO_MARKER("Plots/Line Plots") local s = S.DemoLinePlots local t = ImGui.GetTime() for i = 1, 1001 do local x = (i - 1) * 0.001 s.xs1[i], s.ys1[i], s.zs1[i] = x, 0.5 + 0.5 * math.cos(50 * (x + t / 10)), 0.5 + 0.5 * math.sin(50 * (x + t / 10)) end for i = 1, 20 do local x = (i - 1) / 19.0 s.xs2[i], s.ys2[i], s.zs2[i] = x, x * x, x * x * x end if ImPlot3D.BeginPlot("Line Plots") then ImPlot3D.SetupAxes("x", "y", "z") ImPlot3D.PlotLine("f(x)", s.xs1, s.ys1, s.zs1, 1001) ImPlot3D.PlotLine("g(x)", s.xs2, s.ys2, s.zs2, 20, { Marker = Marker.Circle, Flags = LineFlags.Segments }) ImPlot3D.EndPlot() end end S.DemoScatterPlots = { xs1 = {}, ys1 = {}, zs1 = {}, xs2 = {}, ys2 = {}, zs2 = {} } function D.DemoScatterPlots() IMGUI_DEMO_MARKER("Plots/Scatter Plots") local s = S.DemoScatterPlots srand(0) for i = 1, 100 do local x = (i - 1) * 0.01 s.xs1[i], s.ys1[i], s.zs1[i] = x, x + 0.1 * rand01(), x + 0.1 * rand01() end for i = 1, 50 do s.xs2[i], s.ys2[i], s.zs2[i] = 0.25 + 0.2 * rand01(), 0.50 + 0.2 * rand01(), 0.75 + 0.2 * rand01() end if ImPlot3D.BeginPlot("Scatter Plots") then ImPlot3D.PlotScatter("Data 1", s.xs1, s.ys1, s.zs1, 100) local c = ImPlot3D.GetColormapColor(1) ImPlot3D.PlotScatter("Data 2", s.xs2, s.ys2, s.zs2, 50, { Marker = Marker.Square, MarkerSize = 6, MarkerLineColor = c, MarkerFillColor = c, FillAlpha = 0.25 }) ImPlot3D.EndPlot() end end -- Pyramid: 6 triangles (4 sides + 2 base), 18 vertices local function Pyramid() local cx, cy = { -0.5, 0.5, 0.5, -0.5 }, { -0.5, -0.5, 0.5, 0.5 } local xs, ys, zs = {}, {}, {} local function V(x, y, z) table.insert(xs, x); table.insert(ys, y); table.insert(zs, z) end local function C(i) V(cx[i + 1], cy[i + 1], 0) end for k = 0, 3 do V(0, 0, 1); C(k); C((k + 1) % 4) end -- sides: apex, corner k, corner k+1 C(0); C(1); C(2) -- base C(0); C(2); C(3) return xs, ys, zs end S.DemoTrianglePlots = { flags = TriFlags.None } function D.DemoTrianglePlots() IMGUI_DEMO_MARKER("Plots/Triangle Plots") local s = S.DemoTrianglePlots if s.xs == nil then s.xs, s.ys, s.zs = Pyramid() end s.flags = CHECKBOX_FLAG(s.flags, "ImPlot3DTriangleFlags_NoLines", TriFlags.NoLines) s.flags = CHECKBOX_FLAG(s.flags, "ImPlot3DTriangleFlags_NoFill", TriFlags.NoFill) s.flags = CHECKBOX_FLAG(s.flags, "ImPlot3DTriangleFlags_NoMarkers", TriFlags.NoMarkers) if ImPlot3D.BeginPlot("Triangle Plots") then ImPlot3D.SetupAxesLimits(-1, 1, -1, 1, -0.5, 1.5) ImPlot3D.PlotTriangle("Pyramid", s.xs, s.ys, s.zs, 6 * 3, { FillColor = ImPlot3D.GetColormapColor(0), LineColor = ImPlot3D.GetColormapColor(1), Marker = Marker.Square, MarkerSize = 3, Flags = s.flags, }) ImPlot3D.EndPlot() end end -- Cube faces as quads: +x, -x, +y, -y, +z, -z (4 vertices each) -- stylua: ignore local cube_quads = { xs = { 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1 }, ys = { -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, 1, 1, -1, -1, 1, 1 }, zs = { -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1 }, } S.DemoQuadPlots = { flags = QuadFlags.None, colorX = ImVec4(0.8, 0.2, 0.2, 0.8), colorY = ImVec4(0.2, 0.8, 0.2, 0.8), colorZ = ImVec4(0.2, 0.2, 0.8, 0.8) } function D.DemoQuadPlots() IMGUI_DEMO_MARKER("Plots/Quad Plots") local s, q = S.DemoQuadPlots, cube_quads s.flags = CHECKBOX_FLAG(s.flags, "ImPlot3DQuadFlags_NoLines", QuadFlags.NoLines) s.flags = CHECKBOX_FLAG(s.flags, "ImPlot3DQuadFlags_NoFill", QuadFlags.NoFill) s.flags = CHECKBOX_FLAG(s.flags, "ImPlot3DQuadFlags_NoMarkers", QuadFlags.NoMarkers) if ImPlot3D.BeginPlot("Quad Plots") then ImPlot3D.SetupAxesLimits(-1.5, 1.5, -1.5, 1.5, -1.5, 1.5) for k, e in __iter({ { "X", s.colorX }, { "Y", s.colorY }, { "Z", s.colorZ } }) do local base = (k - 1) * 8 -- [Port] &xs[base] ImPlot3D.PlotQuad(e[1], Ptr(q.xs, base), Ptr(q.ys, base), Ptr(q.zs, base), 8, { Marker = Marker.Square, MarkerSize = 3, Flags = s.flags, FillColor = e[2], LineColor = e[2] }) end ImPlot3D.EndPlot() end end local surface_colormaps = { "Viridis", "Plasma", "Hot", "Cool", "Pink", "Jet", "Twilight", "RdBu", "BrBG", "PiYG", "Spectral", "Greys" } S.DemoSurfacePlots = { xs = {}, ys = {}, zs = {}, custom_colors = {}, t = 0, selected_fill = 1, solid_color = ImVec4(0.8, 0.8, 0.2, 0.6), sel_colormap = 5, custom_range = false, range_min = -1.0, range_max = 1.0, flags = SurfFlags.NoMarkers } function D.DemoSurfacePlots() IMGUI_DEMO_MARKER("Plots/Surface Plots") local s = S.DemoSurfacePlots local N = 20 s.t =s.t+ ImGui.GetIO().DeltaTime -- Define the range for X and Y local min_val, max_val = -1.0, 1.0 local step = (max_val - min_val) / (N - 1) for i = 0, N - 1 do for j = 0, N - 1 do local idx = i * N + j + 1 local x, y = min_val + j * step, min_val + i * step -- X constant along rows, Y along columns local z = math.sin(2 * s.t + math.sqrt(x * x + y * y)) -- z = sin(2t + sqrt(x^2 + y^2)) s.xs[idx], s.ys[idx], s.zs[idx] = x, y, z -- Custom per-point color: R=x, G=y, B=z (each remapped from [-1,1] to [0,1]) s.custom_colors[idx] = IM_COL32(math.floor((x + 1) * 0.5 * 255), math.floor((y + 1) * 0.5 * 255), math.floor((z + 1) * 0.5 * 255), 255) end end -- Choose fill color ImGui.Text("Fill color") ImGui.Indent() _, s.selected_fill = ImGui.RadioButton("Solid", s.selected_fill, 0) if s.selected_fill == 0 then ImGui.SameLine() ImGui.ColorEdit4("##SurfaceSolidColor", s.solid_color) end _, s.selected_fill = ImGui.RadioButton("Colormap", s.selected_fill, 1) if s.selected_fill == 1 then ImGui.SameLine() s.sel_colormap = ImGui.Combo("##SurfaceColormap", s.sel_colormap, surface_colormaps) end _, s.selected_fill = ImGui.RadioButton("Custom Per-Point", s.selected_fill, 2) if s.selected_fill == 2 then ImGui.SameLine(); ImGui.TextDisabled("R=x, G=y, B=z") end ImGui.Unindent() -- Choose range (only applies to Colormap mode) if s.selected_fill ~= 1 then ImGui.BeginDisabled() end _, s.custom_range = ImGui.Checkbox("Custom range", s.custom_range) ImGui.Indent() if not s.custom_range then ImGui.BeginDisabled() end s.range_min = ImGui.SliderFloat("Range min", s.range_min, -1.0, s.range_max - 0.01) s.range_max = ImGui.SliderFloat("Range max", s.range_max, s.range_min + 0.01, 1.0) if not s.custom_range then ImGui.EndDisabled() end ImGui.Unindent() if s.selected_fill ~= 1 then ImGui.EndDisabled() end -- Select flags s.flags = CHECKBOX_FLAG(s.flags, "ImPlot3DSurfaceFlags_NoLines", SurfFlags.NoLines) s.flags = CHECKBOX_FLAG(s.flags, "ImPlot3DSurfaceFlags_NoFill", SurfFlags.NoFill) s.flags = CHECKBOX_FLAG(s.flags, "ImPlot3DSurfaceFlags_NoMarkers", SurfFlags.NoMarkers) if s.selected_fill == 1 then ImPlot3D.PushColormap(surface_colormaps[s.sel_colormap + 1]) end if ImPlot3D.BeginPlot("Surface Plots", ImVec2(-1, 0), Flags.NoClip) then ImPlot3D.SetupAxesLimits(-1, 1, -1, 1, -1.5, 1.5) local spec = { FillAlpha = 0.8, Flags = s.flags, Marker = Marker.Square, LineColor = ImPlot3D.GetColormapColor(1) } if s.selected_fill == 0 then spec.FillColor = s.solid_color elseif s.selected_fill == 2 then spec.FillColors = s.custom_colors end local lo, hi = 0.0, 0.0 if s.custom_range then lo, hi = s.range_min, s.range_max end ImPlot3D.PlotSurface("Wave Surface", s.xs, s.ys, s.zs, N, N, lo, hi, spec) ImPlot3D.EndPlot() end if s.selected_fill == 1 then ImPlot3D.PopColormap() end end S.DemoMeshPlots = { mesh_id = 0, line_color = ImVec4(0.5, 0.5, 0.2, 0.6), fill_color = ImVec4(0.8, 0.8, 0.2, 0.6), marker_color = ImVec4(0.5, 0.5, 0.2, 0.6), flags = MeshFlags.NoMarkers } function D.DemoMeshPlots() IMGUI_DEMO_MARKER("Plots/Mesh Plots") local s = S.DemoMeshPlots s.mesh_id = ImGui.Combo("Mesh", s.mesh_id, "Duck\0Sphere\0Cube\0\0") ImGui.ColorEdit4("Line Color##Mesh", s.line_color) ImGui.ColorEdit4("Fill Color##Mesh", s.fill_color) ImGui.ColorEdit4("Marker Color##Mesh", s.marker_color) s.flags = CHECKBOX_FLAG(s.flags, "ImPlot3DMeshFlags_NoLines", MeshFlags.NoLines) s.flags = CHECKBOX_FLAG(s.flags, "ImPlot3DMeshFlags_NoFill", MeshFlags.NoFill) s.flags = CHECKBOX_FLAG(s.flags, "ImPlot3DMeshFlags_NoMarkers", MeshFlags.NoMarkers) if ImPlot3D.BeginPlot("Mesh Plots") then ImPlot3D.SetupAxesLimits(-1, 1, -1, 1, -1, 1) local spec = { Flags = s.flags, FillColor = s.fill_color, LineColor = s.line_color, Marker = Marker.Square, MarkerSize = 3.0, MarkerLineColor = s.marker_color, MarkerFillColor = s.marker_color } -- [Port] ImPlot3D.Meshes keep upstream's 0-based indices local name = ({ [0] = "Duck", "Sphere", "Cube" })[s.mesh_id] local m = Meshes[name] ImPlot3D.PlotMesh(name, m.VtxXs, m.VtxYs, m.VtxZs, m.Idx, m.VtxCount, m.IdxCount, spec) ImPlot3D.EndPlot() end end -- [Port] ImPlot3DPoint/ImVec2 statics -> 1-based arrays for SliderFloat3/SliderFloat2; ImVec4 tints edited in place S.DemoImagePlots = { tint1 = ImVec4(1, 1, 1, 1), tint2 = ImVec4(1, 1, 1, 1), center1 = { 0, 0, 1 }, axis_u1 = { 1, 0, 0 }, axis_v1 = { 0, 1, 0 }, uv0_1 = { 0, 0 }, uv1_1 = { 1, 1 }, p0 = { -1, -1, 0 }, p1 = { 1, -1, 0 }, p2 = { 1, 1, 0 }, p3 = { -1, 1, 0 }, uv0 = { 0, 0 }, uv1 = { 1, 0 }, uv2 = { 1, 1 }, uv3 = { 0, 1 } } function D.DemoImagePlots() IMGUI_DEMO_MARKER("Plots/Image Plots") local s = S.DemoImagePlots ImGui.BulletText("Below we are displaying the font texture, which is the only texture we have\naccess to in this demo.") ImGui.BulletText("Use the 'ImTextureID' type as storage to pass pointers or identifiers to your\nown texture data.") ImGui.BulletText("See ImGui Wiki page 'Image Loading and Displaying Examples'.") ImGui.Dummy(ImVec2(0, 10)) if ImGui.TreeNodeEx("Image 1 Controls: Center + Axes") then ImGui.SliderFloat3("Center", s.center1, -2, 2, "%.1f") ImGui.SliderFloat3("Axis U", s.axis_u1, -2, 2, "%.1f") ImGui.SliderFloat3("Axis V", s.axis_v1, -2, 2, "%.1f") ImGui.SliderFloat2("UV0", s.uv0_1, 0, 1, "%.2f") ImGui.SliderFloat2("UV1", s.uv1_1, 0, 1, "%.2f") ImGui.ColorEdit4("Tint", s.tint1) ImGui.TreePop() end if ImGui.TreeNodeEx("Image 2 Controls: Full Quad") then ImGui.SliderFloat3("P0", s.p0, -2, 2, "%.1f") ImGui.SliderFloat3("P1", s.p1, -2, 2, "%.1f") ImGui.SliderFloat3("P2", s.p2, -2, 2, "%.1f") ImGui.SliderFloat3("P3", s.p3, -2, 2, "%.1f") ImGui.SliderFloat2("UV0", s.uv0, 0, 1, "%.2f") ImGui.SliderFloat2("UV1", s.uv1, 0, 1, "%.2f") ImGui.SliderFloat2("UV2", s.uv2, 0, 1, "%.2f") ImGui.SliderFloat2("UV3", s.uv3, 0, 1, "%.2f") ImGui.ColorEdit4("Tint##2", s.tint2) ImGui.TreePop() end if ImPlot3D.BeginPlot("Image Plot", ImVec2(-1, 0), Flags.NoClip) then -- We use the font atlas texture for this demo; your own loaded textures work just as well. local tex = ImGui.GetIO().Fonts.TexRef local function p3(a) return P(a[1], a[2], a[3]) end local function v2(a) return ImVec2(a[1], a[2]) end ImPlot3D.PlotImage("Image 1", tex, p3(s.center1), p3(s.axis_u1), p3(s.axis_v1), v2(s.uv0_1), v2(s.uv1_1), s.tint1) ImPlot3D.PlotImage("Image 2", tex, p3(s.p0), p3(s.p1), p3(s.p2), p3(s.p3), v2(s.uv0), v2(s.uv1), v2(s.uv2), v2(s.uv3), s.tint2) ImPlot3D.EndPlot() end end S.DemoRealtimePlots = { sdata1 = ScrollingBuffer(), sdata2 = ScrollingBuffer(), sdata3 = ScrollingBuffer(), flags = AxisFlags.NoTickLabels, t = 0.0, last_t = -1.0 } function D.DemoRealtimePlots() IMGUI_DEMO_MARKER("Plots/Realtime Plots") local s = S.DemoRealtimePlots ImGui.BulletText("Move your mouse to change the data!") if ImPlot3D.BeginPlot("Scrolling Plot") then -- Pool mouse data every 10 ms s.t =s.t+ ImGui.GetIO().DeltaTime if s.t - s.last_t > 0.01 then s.last_t = s.t local mouse = ImGui.GetMousePos() if math.abs(mouse.x) < 1e4 and math.abs(mouse.y) < 1e4 then local pos, size = ImPlot3D.GetFramePos(), ImPlot3D.GetFrameSize() AddPoint(s.sdata1, s.t) AddPoint(s.sdata2, mouse.x - (pos.x + size.x / 2)) AddPoint(s.sdata3, mouse.y - (pos.y + size.y / 2)) end end ImPlot3D.SetupAxes("Time", "Mouse X", "Mouse Y", s.flags, s.flags, s.flags) ImPlot3D.SetupAxisLimits(ImAxis3D.X, s.t - 10.0, s.t, Cond.Always) ImPlot3D.SetupAxisLimits(ImAxis3D.Y, -400, 400, Cond.Once) ImPlot3D.SetupAxisLimits(ImAxis3D.Z, -400, 400, Cond.Once) ImPlot3D.PlotLine("Mouse", s.sdata1.Data, s.sdata2.Data, s.sdata3.Data, #s.sdata1.Data, { Offset = s.sdata1.Offset }) ImPlot3D.EndPlot() end end local plot_flag_list = { { "NoTitle", "Hide plot title" }, { "NoLegend", "Hide plot legend" }, { "NoMouseText", "Hide mouse position in plot coordinates" }, { "NoClip", "Disable 3D box clipping" }, { "NoMenus", "The user will not be able to open context menus" }, { "Equal", "X, Y, and Z axes will be constrained to have the same units/pixel" }, { "NoRotate", "Lock rotation interaction" }, { "NoPan", "Lock panning/translation interaction" }, { "NoZoom", "Lock zooming interaction" }, { "NoInputs", "Disable all user inputs" }, } S.DemoPlotFlags = { flags = Flags.None, x = {}, y = {}, z = {} } for i = 0, 99 do local t = i * 0.1 S.DemoPlotFlags.x[i + 1], S.DemoPlotFlags.y[i + 1], S.DemoPlotFlags.z[i + 1] = 3.0 * math.cos(t), 3.0 * math.sin(t), t - 5.0 end local corner_xs, corner_ys, corner_zs = { -10, 10, -10, 10, -10, 10, -10, 10 }, { -10, -10, 10, 10, -10, -10, 10, 10 }, { -5, -5, -5, -5, 5, 5, 5, 5 } function D.DemoPlotFlags() IMGUI_DEMO_MARKER("Plots/Plot Flags") local s = S.DemoPlotFlags -- [Port] the repeated CHECKBOX_FLAG + "(?)" tooltip blocks folded into a loop for _, f in __iter(plot_flag_list) do s.flags = CHECKBOX_FLAG(s.flags, "ImPlot3DFlags_" .. f[1], Flags[f[1] ]) ImGui.SameLine() ImGui.TextDisabled("(?)") if ImGui.IsItemHovered() then ImGui.SetTooltip(f[2]) end end if ImPlot3D.BeginPlot("Plot Flags Demo", ImVec2(-1, 0), s.flags) then ImPlot3D.SetupAxes("X-axis", "Y-axis", "Z-axis") ImPlot3D.SetupAxesLimits(-10, 10, -10, 10, -5, 5) ImPlot3D.PlotLine("Helix", s.x, s.y, s.z, 100) -- Add some scatter points to show equal scaling effect ImPlot3D.PlotScatter("Cube corners", corner_xs, corner_ys, corner_zs, 8) ImPlot3D.EndPlot() end end S.DemoOffsetAndStride = { offset = 0, data = {} } function D.DemoOffsetAndStride() IMGUI_DEMO_MARKER("Plots/Offset and Stride") local s = S.DemoOffsetAndStride local k_spirals, k_points_per = 11, 50 local data = s.data for p = 0, k_points_per - 1 do for sp = 0, k_spirals - 1 do local r = sp / (k_spirals - 1) * 0.2 + 0.2 local theta = p / k_points_per * 6.28 local base = p * 3 * k_spirals + 3 * sp + 1 data[base], data[base + 1], data[base + 2] = 0.5 + r * math.cos(theta), 0.5 + r * math.sin(theta), 0.5 + 0.5 * math.sin(2.0 * theta) end end ImGui.BulletText("Offsetting is useful for realtime plots (see above) and circular buffers.") ImGui.BulletText("Striding is useful for interleaved data (e.g. audio) or plotting structs.") ImGui.BulletText("Here, all spiral data is stored in a single interleaved buffer:") ImGui.BulletText("[s0.x0 s0.y0 s0.z0 ... sn.x0 sn.y0 sn.z0 s0.x1 s0.y1 s0.z1 ... sn.x1 sn.y1 sn.z1 ... sn.xm sn.ym sn.zm]") ImGui.BulletText("The offset value indicates which spiral point index is considered the first.") ImGui.BulletText("Offsets can be negative and/or larger than the actual data count.") s.offset = ImGui.SliderInt("Offset", s.offset, -2 * k_points_per, 2 * k_points_per) if ImPlot3D.BeginPlot("##strideoffset", ImVec2(-1, 0)) then ImPlot3D.PushColormap(Colormap.Jet) for sp = 0, k_spirals - 1 do -- [Port] &data[s*3+k] -> Ptr proxy; Stride is in elements (3*k_spirals doubles) ImPlot3D.PlotLine(string.format("Spiral %d", sp), Ptr(data, sp * 3), Ptr(data, sp * 3 + 1), Ptr(data, sp * 3 + 2), k_points_per, { Offset = s.offset, Stride = 3 * k_spirals }) end ImPlot3D.EndPlot() ImPlot3D.PopColormap() end end local Location, LegendFlags = ImPlot3D.ImPlot3DLocation, ImPlot3D.ImPlot3DLegendFlags S.DemoLegendOptions = { loc = Location.East, flags = 0, num_dummy_items = 25, t = 0, xs1 = {}, ys1 = {}, zs1 = {}, xs2 = {}, ys2 = {}, zs2 = {}, xs3 = {}, ys3 = {}, zs3 = {} } local legend_flag_list = { { "Horizontal", "Legend entries will be displayed horizontally" }, { "NoButtons", "Legend icons will not function as hide/show buttons" }, { "NoHighlightItem", "Plot items will not be highlighted when their legend entry is hovered" }, } function D.DemoLegendOptions() IMGUI_DEMO_MARKER("Plots/Legend Options") local s = S.DemoLegendOptions for i, l in __iter({ "North", "South", "West", "East" }) do if i > 1 then ImGui.SameLine() end _, s.loc = ImGui.CheckboxFlags(l, s.loc, Location[l]) end for _, f in __iter(legend_flag_list) do s.flags = CHECKBOX_FLAG(s.flags, "ImPlot3DLegendFlags_" .. f[1], LegendFlags[f[1] ]) ImGui.SameLine() ImGui.TextDisabled("(?)") if ImGui.IsItemHovered() then ImGui.SetTooltip(f[2]) end end local style = ImPlot3D.GetStyle() ImGui.SliderFloat2("LegendPadding", style.LegendPadding, 0.0, 20.0, "%.0f") ImGui.SliderFloat2("LegendInnerPadding", style.LegendInnerPadding, 0.0, 10.0, "%.0f") ImGui.SliderFloat2("LegendSpacing", style.LegendSpacing, 0.0, 5.0, "%.0f") s.num_dummy_items = ImGui.SliderInt("Num Dummy Items (Demo Scrolling)", s.num_dummy_items, 0, 100) if ImPlot3D.BeginPlot("Legend Options Demo", ImVec2(-1, 0)) then ImPlot3D.SetupAxes("X-Axis", "Y-Axis", "Z-Axis") ImPlot3D.SetupAxesLimits(-1, 1, -1, 1, -1, 1) ImPlot3D.SetupLegend(s.loc, s.flags) -- Generate some 3D line data s.t =s.t+ ImGui.GetIO().DeltaTime * 0.5 local count = 50 for i = 1, count do local phase = (i - 1) * 0.1 + s.t s.xs1[i], s.ys1[i], s.zs1[i] = 0.8 * math.cos(phase), 0.8 * math.sin(phase), 0.5 * math.sin(phase * 2) s.xs2[i], s.ys2[i], s.zs2[i] = 0.6 * math.cos(phase + 1.0), 0.6 * math.sin(phase + 1.0), -0.3 * math.cos(phase * 1.5) s.xs3[i], s.ys3[i], s.zs3[i] = 0.4 * math.sin(phase), 0.4 * math.cos(phase), 0.7 * math.cos(phase * 0.8) end ImPlot3D.PlotLine("Helix A", s.xs1, s.ys1, s.zs1, count) ImPlot3D.PlotLine("Helix B##IDText", s.xs2, s.ys2, s.zs2, count) -- Text after ## used for ID only ImPlot3D.PlotLine("##NotListed", s.xs3, s.ys3, s.zs3, count) -- Plotted, but not added to legend -- Add dummy items to demonstrate legend scrolling (scroll the mouse wheel over the legend) for i = 0, s.num_dummy_items - 1 do ImPlot3D.PlotDummy(string.format("Item %03d", i)) end ImPlot3D.EndPlot() end end S.DemoMarkersAndText = {} function D.DemoMarkersAndText() IMGUI_DEMO_MARKER("Plots/Markers and Text") local s = S.DemoMarkersAndText if s.mk_size == nil then s.mk_size, s.mk_weight = ImPlot3D.GetStyle().MarkerSize, ImPlot3D.GetStyle().LineWeight end s.mk_size = ImGui.DragFloat("Marker Size", s.mk_size, 0.1, 2.0, 10.0, "%.2f px") s.mk_weight = ImGui.DragFloat("Marker Weight", s.mk_weight, 0.05, 0.5, 3.0, "%.2f px") if ImPlot3D.BeginPlot("##MarkerStyles", ImVec2(-1, 0), Flags.CanvasOnly) then local nd = AxisFlags.NoDecorations ImPlot3D.SetupAxes(nil, nil, nil, nd, nd, nd) local COUNT = Marker.COUNT ImPlot3D.SetupAxesLimits(-0.5, 1.5, -0.5, 1.5, 0, COUNT + 1) local xs, ys, zs = { 0, 0 }, { 0, 0 }, { COUNT, COUNT + 1 } -- Filled markers for m = 0, COUNT - 1 do xs[2] = xs[1] + math.cos(zs[1] / COUNT * 2 * IM_PI) * 0.5 ys[2] = ys[1] + math.sin(zs[1] / COUNT * 2 * IM_PI) * 0.5 ImGui.PushID(m) ImPlot3D.PlotLine("##Filled", xs, ys, zs, 2, { Marker = m, MarkerSize = s.mk_size, LineWeight = s.mk_weight }) ImGui.PopID() zs[1]=zs[1]- 1; zs[2]=zs[2]- 1 end xs[1], ys[1], zs[1] = 1, 1, COUNT zs[2] = zs[1] + 1 -- Open markers for m = 0, COUNT - 1 do xs[2] = xs[1] + math.cos(zs[1] / COUNT * 2 * IM_PI) * 0.5 ys[2] = ys[1] - math.sin(zs[1] / COUNT * 2 * IM_PI) * 0.5 ImGui.PushID(m) ImPlot3D.PlotLine("##Open", xs, ys, zs, 2, { Marker = m, MarkerSize = s.mk_size, LineWeight = s.mk_weight, FillColor = ImVec4(0, 0, 0, 0) }) ImGui.PopID() zs[1]=zs[1]- 1; zs[2]=zs[2]- 1 end ImPlot3D.PlotText("Filled Markers", 0.0, 0.0, 6.0) ImPlot3D.PlotText("Open Markers", 1.0, 1.0, 6.0) ImPlot3D.PushStyleColor(ImPlot3D.ImPlot3DCol.InlayText, ImVec4(1, 0, 1, 1)) ImPlot3D.PlotText("Rotated Text", 0.5, 0.5, 6.0, IM_PI / 4, ImVec2(0, 0)) ImPlot3D.PopStyleColor() ImPlot3D.EndPlot() end end S.DemoNaNValues = { include_nan = true, flags = 0 } function D.DemoNaNValues() IMGUI_DEMO_MARKER("Plots/NaN Values") local s = S.DemoNaNValues local data1, data2, data3 = { 0.0, 0.25, 0.5, 0.75, 1.0 }, { 0.0, 0.25, 0.5, 0.75, 1.0 }, { 0.0, 0.25, 0.5, 0.75, 1.0 } if s.include_nan then data1[3] = 0 / 0 end _, s.include_nan = ImGui.Checkbox("Include NaN", s.include_nan) ImGui.SameLine() _, s.flags = ImGui.CheckboxFlags("Skip NaN", s.flags, LineFlags.SkipNaN) if ImPlot3D.BeginPlot("##NaNValues") then ImPlot3D.PlotLine("Line", data1, data2, data3, 5, { Flags = s.flags, Marker = Marker.Square }) ImPlot3D.EndPlot() end end -- Cube [0,1]^3 as quads (+x, -x, +y, -y, +z, -z) -- stylua: ignore local unit_quads = { xs = { 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0 }, ys = { 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1 }, zs = { 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0 }, } -- Gouraud-shaded duck: per-vertex normals -> point light + ambient, mapped to index-buffer slots local function BuildDuckColors() local m = Meshes.Duck local X, Y, Z, idx = m.VtxXs, m.VtxYs, m.VtxZs, m.Idx local nx, ny, nz = table.create(m.VtxCount, 0), table.create(m.VtxCount, 0), table.create(m.VtxCount, 0) -- Accumulate area-weighted face normals into each vertex for i = 1, m.IdxCount, 3 do local a, b, c = idx[i] + 1, idx[i + 1] + 1, idx[i + 2] + 1 local ux, uy, uz = X[b] - X[a], Y[b] - Y[a], Z[b] - Z[a] local vx, vy, vz = X[c] - X[a], Y[c] - Y[a], Z[c] - Z[a] local fx, fy, fz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx for _, v in __iter({ a, b, c }) do nx[v]=nx[v]+ fx; ny[v]=ny[v]+ fy; nz[v]=nz[v]+ fz end end local function norm(x, y, z) local len = math.sqrt(x * x + y * y + z * z) if len > 1e-8 then return x / len, y / len, z / len end return 0, 0, 1 end -- Lighting: yellow duck (1, 0.85, 0.1), warm white light (1, 0.95, 0.8) at (2, 2, 3), dim warm ambient (0.15, 0.12, 0.03) local vtx_colors = {} for v = 1, m.VtxCount do local ax, ay, az = norm(nx[v], ny[v], nz[v]) local lx, ly, lz = norm(2 - X[v], 2 - Y[v], 3 - Z[v]) local diff = math.max(0, ax * lx + ay * ly + az * lz) local r = math.min(1, 0.15 + 1.0 * 1.0 * diff) local g = math.min(1, 0.12 + 0.85 * 0.95 * diff) local b = math.min(1, 0.03 + 0.1 * 0.8 * diff) vtx_colors[v] = IM_COL32(math.floor(r * 255), math.floor(g * 255), math.floor(b * 255), 255) end local out = {} for i = 1, m.IdxCount do out[i] = vtx_colors[idx[i] + 1] end return out end S.Demo_PerIndexColors = { xs1 = {}, ys1 = {}, zs1 = {}, colors1 = {}, xs2 = {}, ys2 = {}, zs2 = {}, colors2 = {} } function D.Demo_PerIndexColors() IMGUI_DEMO_MARKER("Plots/Per-Index Colors") local s = S.Demo_PerIndexColors local Viridis = Colormap.Viridis local function cm(t, c) return ImGui.GetColorU32(ImPlot3D.SampleColormap(t, c)) end -- Colorful Lines local t = ImGui.GetTime() for i = 1, 1001 do local x = (i - 1) * 0.001 s.xs1[i], s.ys1[i], s.zs1[i] = x, 0.5 + 0.5 * math.sin(50 * (x + t / 10)), 0.5 + 0.5 * math.cos(50 * (x + t / 10)) s.colors1[i] = HSV_U32((i - 1) / 1000.0, 0.8, 0.9) -- Rainbow colors end for i = 1, 20 do local x = (i - 1) / 19.0 s.xs2[i], s.ys2[i], s.zs2[i] = x, x * x, x * x * x s.colors2[i] = cm((i - 1) / 19.0, Viridis) -- Colormap colors (Viridis) end if ImPlot3D.BeginPlot("Colorful Lines") then ImPlot3D.PlotLine("f(x)", s.xs1, s.ys1, s.zs1, 1001, { LineColors = s.colors1 }) ImPlot3D.PlotLine("g(x)", s.xs2, s.ys2, s.zs2, 20, { Marker = Marker.Circle, Flags = LineFlags.Segments, LineColors = s.colors2, MarkerFillColors = s.colors2, MarkerLineColors = s.colors2 }) ImPlot3D.EndPlot() end -- Colorful Scatter if s.sc == nil then srand(0) local sc = { xs1 = {}, ys1 = {}, zs1 = {}, fill1 = {}, line1 = {}, sizes1 = {}, xs2 = {}, ys2 = {}, zs2 = {}, colors2 = {}, sizes2 = {} } for i = 1, 100 do local x = (i - 1) * 0.01 sc.xs1[i], sc.ys1[i], sc.zs1[i] = x, x + 0.1 * rand01(), x + 0.1 * rand01() local hue = (i - 1) / 99.0 -- Rainbow hue colors sc.fill1[i], sc.line1[i] = HSV_U32(hue, 0.8, 0.9), HSV_U32(hue, 0.9, 0.7) sc.sizes1[i] = 2.0 + 4.0 * rand01() -- Random sizes between 2 and 6 end for i = 1, 50 do sc.xs2[i], sc.ys2[i], sc.zs2[i] = 0.25 + 0.2 * rand01(), 0.50 + 0.2 * rand01(), 0.75 + 0.2 * rand01() sc.colors2[i] = cm((i - 1) / 49.0, Viridis) sc.sizes2[i] = 2.0 + 4.0 * rand01() end -- Colorful Triangles (pyramid, Hot colormap sampled by z) / Colorful Quads (R=x, G=y, B=z) sc.tx, sc.ty, sc.tz = Pyramid() sc.tri_colors, sc.quad_colors = {}, {} for i = 1, 18 do sc.tri_colors[i] = cm(0.5 * sc.tz[i], Colormap.Hot) end local q = unit_quads for i = 1, 24 do sc.quad_colors[i] = IM_COL32(q.xs[i] * 255, q.ys[i] * 255, q.zs[i] * 255, 255) end sc.duck_fill_colors = BuildDuckColors() s.sc = sc end local sc = s.sc if ImPlot3D.BeginPlot("Colorful Scatter") then ImPlot3D.PlotScatter("Data 1", sc.xs1, sc.ys1, sc.zs1, 100, { MarkerFillColors = sc.fill1, MarkerLineColors = sc.line1, MarkerSizes = sc.sizes1 }) ImPlot3D.PlotScatter("Data 2", sc.xs2, sc.ys2, sc.zs2, 50, { Marker = Marker.Square, MarkerFillColors = sc.colors2, MarkerLineColors = sc.colors2, MarkerSizes = sc.sizes2, FillAlpha = 0.5 }) ImPlot3D.EndPlot() end if ImPlot3D.BeginPlot("Colorful Triangles") then ImPlot3D.SetupAxesLimits(-1, 1, -1, 1, -0.5, 1.5) ImPlot3D.PlotTriangle("Pyramid", sc.tx, sc.ty, sc.tz, 18, { FillColors = sc.tri_colors, FillAlpha = 0.8 }) ImPlot3D.EndPlot() end if ImPlot3D.BeginPlot("Colorful Quads") then ImPlot3D.SetupAxesLimits(-0.5, 1.5, -0.5, 1.5, -0.5, 1.5) ImPlot3D.PlotQuad("Cube", unit_quads.xs, unit_quads.ys, unit_quads.zs, 24, { FillColors = sc.quad_colors, FillAlpha = 0.8 }) ImPlot3D.EndPlot() end if ImPlot3D.BeginPlot("Gouraud Duck") then ImPlot3D.SetupAxesLimits(-1, 1, -1, 1, -1, 1) local m = Meshes.Duck ImPlot3D.PlotMesh("Duck", m.VtxXs, m.VtxYs, m.VtxZs, m.Idx, m.VtxCount, m.IdxCount, { FillColors = sc.duck_fill_colors, Flags = MeshFlags.NoLines }) ImPlot3D.EndPlot() end end --------------------------------------------------------------------------- -- [SECTION] Axes --------------------------------------------------------------------------- S.DemoBoxScale = { scale = { 1.0, 1.0, 1.0 }, xs = {}, ys = {}, zs = {} } function D.DemoBoxScale() IMGUI_DEMO_MARKER("Axes/Box Scale") local s = S.DemoBoxScale local N = 100 for i = 1, N do local t = (i - 1) / (N - 1) s.xs[i], s.ys[i], s.zs[i] = math.sin(t * 2 * IM_PI), math.cos(t * 4 * IM_PI), t * 2 - 1 end ImGui.SliderFloat3("Box Scale", s.scale, 0.1, 2.0, "%.2f") if ImPlot3D.BeginPlot("##BoxScale") then ImPlot3D.SetupBoxScale(s.scale[1], s.scale[2], s.scale[3]) ImPlot3D.PlotLine("3D Curve", s.xs, s.ys, s.zs, N) ImPlot3D.EndPlot() end end S.DemoBoxRotation = { elevation = 45.0, azimuth = -135.0, animate = false, init_elevation = 45.0, init_azimuth = -135.0 } function D.DemoBoxRotation() IMGUI_DEMO_MARKER("Axes/Box Rotation") local s = S.DemoBoxRotation local origin, axis = { 0.0, 0.0 }, { 0.0, 1.0 } ImGui.Text("Rotation") local c1, c2 s.elevation, c1 = ImGui.SliderFloat("Elevation", s.elevation, -90.0, 90.0, "%.1f degrees") s.azimuth, c2 = ImGui.SliderFloat("Azimuth", s.azimuth, -180.0, 180.0, "%.1f degrees") local changed = c1 or c2 _, s.animate = ImGui.Checkbox("Animate", s.animate) ImGui.Text("Initial Rotation") ImGui.SameLine() HelpMarker("The rotation will be reset to the initial rotation when you double right-click") s.init_elevation = ImGui.SliderFloat("Initial Elevation", s.init_elevation, -90.0, 90.0, "%.1f degrees") s.init_azimuth = ImGui.SliderFloat("Initial Azimuth", s.init_azimuth, -180.0, 180.0, "%.1f degrees") if ImPlot3D.BeginPlot("##BoxRotation") then ImPlot3D.SetupAxesLimits(-1, 1, -1, 1, -1, 1, Cond.Always) ImPlot3D.SetupBoxInitialRotation(s.init_elevation, s.init_azimuth) if changed then ImPlot3D.SetupBoxRotation(s.elevation, s.azimuth, s.animate, Cond.Always) end ImPlot3D.PlotLine("X-Axis", axis, origin, origin, 2, { LineColor = ImVec4(0.8, 0.2, 0.2, 1) }) ImPlot3D.PlotLine("Y-Axis", origin, axis, origin, 2, { LineColor = ImVec4(0.2, 0.8, 0.2, 1) }) ImPlot3D.PlotLine("Z-Axis", origin, origin, axis, 2, { LineColor = ImVec4(0.2, 0.2, 0.8, 1) }) ImPlot3D.EndPlot() end end local Scale = ImPlot3D.ImPlot3DScale S.Demo_LogScale = { xs = {}, ys1 = {}, ys2 = {}, ys3 = {}, zs = {} } for i = 1, 1001 do local s, x = S.Demo_LogScale, (i - 1) * 0.1 s.xs[i], s.ys1[i], s.ys2[i], s.ys3[i], s.zs[i] = x, math.sin(x) + 1, math.log(x), 10 ^ x, 0.0 end function D.Demo_LogScale() IMGUI_DEMO_MARKER("Axes/Log Scale") local s = S.Demo_LogScale if ImPlot3D.BeginPlot("Log Plot 3D", ImVec2(-1, 0)) then ImPlot3D.SetupAxisScale(ImAxis3D.X, Scale.Log10) ImPlot3D.SetupAxesLimits(0.1, 100, 0, 10, -1, 1) ImPlot3D.PlotLine("f(x) = x", s.xs, s.xs, s.zs, 1001) ImPlot3D.PlotLine("f(x) = sin(x)+1", s.xs, s.ys1, s.zs, 1001) ImPlot3D.PlotLine("f(x) = log(x)", s.xs, s.ys2, s.zs, 1001) ImPlot3D.PlotLine("f(x) = 10^x", s.xs, s.ys3, s.zs, 21) ImPlot3D.EndPlot() end end S.Demo_SymmetricLogScale = { xs = {}, ys1 = {}, ys2 = {}, zs = {} } for i = 1, 1001 do local s, x = S.Demo_SymmetricLogScale, (i - 1) * 0.1 - 50 s.xs[i], s.ys1[i], s.ys2[i], s.zs[i] = x, math.sin(x), (i - 1) * 0.002 - 1, 0.0 end function D.Demo_SymmetricLogScale() IMGUI_DEMO_MARKER("Axes/Symmetric Log Scale") local s = S.Demo_SymmetricLogScale if ImPlot3D.BeginPlot("SymLog Plot", ImVec2(-1, 0)) then ImPlot3D.SetupAxisScale(ImAxis3D.X, Scale.SymLog) ImPlot3D.PlotLine("f(x) = a*x+b", s.xs, s.ys2, s.zs, 1001) ImPlot3D.PlotLine("f(x) = sin(x)", s.xs, s.ys1, s.zs, 1001) ImPlot3D.EndPlot() end end S.DemoTickLabels = { custom_fmt = true, custom_ticks = false, custom_labels = true } local letters_ticks, letters_labels = { 0.0, 0.2, 0.4, 0.6, 0.8, 1.0 }, { "A", "B", "C", "D", "E", "F" } function D.DemoTickLabels() IMGUI_DEMO_MARKER("Axes/Tick Labels") local s = S.DemoTickLabels _, s.custom_fmt = ImGui.Checkbox("Show Custom Format", s.custom_fmt) ImGui.SameLine() _, s.custom_ticks = ImGui.Checkbox("Show Custom Ticks", s.custom_ticks) if s.custom_ticks then ImGui.SameLine() _, s.custom_labels = ImGui.Checkbox("Show Custom Labels", s.custom_labels) end if ImPlot3D.BeginPlot("##Ticks") then ImPlot3D.SetupAxesLimits(2, 5, 0, 1, 0, 1000) if s.custom_fmt then ImPlot3D.SetupAxisFormat(ImAxis3D.Y, MetricFormatter, "Hz") ImPlot3D.SetupAxisFormat(ImAxis3D.Z, MetricFormatter, "m") end if s.custom_ticks then local labels = (s.custom_labels and{letters_labels }or{nil })[1] ImPlot3D.SetupAxisTicks(ImAxis3D.X, { 3.14 }, 1, s.custom_labels and{ "PI" } or nil, true) ImPlot3D.SetupAxisTicks(ImAxis3D.Y, letters_ticks, 6, labels, false) ImPlot3D.SetupAxisTicks(ImAxis3D.Z, 0, 1000, 6, labels, false) end ImPlot3D.EndPlot() end end S.DemoAxisConstraints = { limits = { -10, 10 }, zoom = { 1, 20 }, flags = 0 } function D.DemoAxisConstraints() IMGUI_DEMO_MARKER("Axes/Axis Constraints") local s = S.DemoAxisConstraints ImGui.DragFloat2("Limits Constraints", s.limits, 0.01) ImGui.DragFloat2("Zoom Constraints", s.zoom, 0.01) s.flags = CHECKBOX_FLAG(s.flags, "ImPlot3DAxisFlags_PanStretch", AxisFlags.PanStretch) if ImPlot3D.BeginPlot("##AxisConstraints", ImVec2(-1, 0)) then ImPlot3D.SetupAxes("X", "Y", "Z", s.flags, s.flags, s.flags) ImPlot3D.SetupAxesLimits(-1, 1, -1, 1, -1, 1) for a = 0, 2 do ImPlot3D.SetupAxisLimitsConstraints(a, s.limits[1], s.limits[2]) ImPlot3D.SetupAxisZoomConstraints(a, s.zoom[1], s.zoom[2]) end ImPlot3D.EndPlot() end end S.DemoEqualAxes = { flags = Flags.Equal, cx = {}, cy = {}, cz = {}, hx = {}, hy = {}, hz = {} } for i = 0, 359 do local s, angle = S.DemoEqualAxes, i * 2 * IM_PI / 359.0 s.cx[i + 1], s.cy[i + 1], s.cz[i + 1] = math.cos(angle), math.sin(angle), 0 -- Circle in XY plane at Z=0 s.hx[i + 1], s.hy[i + 1], s.hz[i + 1] = 0.5 * math.cos(angle), 0.5 * math.sin(angle), i / 359.0 * 2.0 - 1.0 -- Helix end local square_xs, square_ys, square_zs = { -0.5, 0.5, 0.5, -0.5, -0.5 }, { -0.5, -0.5, 0.5, 0.5, -0.5 }, { -0.5, -0.5, -0.5, -0.5, -0.5 } function D.DemoEqualAxes() IMGUI_DEMO_MARKER("Axes/Equal Axes") local s = S.DemoEqualAxes ImGui.BulletText("Equal constraint applies to all three axes (X, Y, Z)") ImGui.BulletText("When enabled, the axes maintain the same units/pixel ratio") s.flags = CHECKBOX_FLAG(s.flags, "ImPlot3DFlags_Equal", Flags.Equal) if ImPlot3D.BeginPlot("##EqualAxes", ImVec2(-1, 0), s.flags) then ImPlot3D.SetupAxes("X-Axis", "Y-Axis", "Z-Axis") ImPlot3D.PlotLine("Circle", s.cx, s.cy, s.cz, 360) ImPlot3D.PlotLine("Helix", s.hx, s.hy, s.hz, 360) ImPlot3D.PlotLine("Square", square_xs, square_ys, square_zs, 5) ImPlot3D.EndPlot() end end S.DemoAutoFittingData = { xflags = AxisFlags.None, yflags = AxisFlags.None, zflags = AxisFlags.AutoFit, x = {}, y = {}, z = {} } for i = 0, 100 do local s = S.DemoAutoFittingData s.x[i + 1], s.y[i + 1], s.z[i + 1] = i * 0.1, i * 0.1, 1 + math.sin(i / 10.0) end function D.DemoAutoFittingData() IMGUI_DEMO_MARKER("Axes/Auto-Fitting Data") local s = S.DemoAutoFittingData ImGui.BulletText("Axes can be configured to auto-fit to data extents.") ImGui.BulletText("Try panning and zooming to see the axes adjust.") ImGui.BulletText("Disable AutoFit on an axis to fix its range.") for _, a in __iter({ "x", "y", "z" }) do ImGui.TextUnformatted(string.upper(a) .. ": ") ImGui.SameLine() _, s[a .. "flags"] = ImGui.CheckboxFlags("ImPlot3DAxisFlags_AutoFit##" .. string.upper(a), s[a .. "flags"], AxisFlags.AutoFit) end if ImPlot3D.BeginPlot("##AutoFitting") then ImPlot3D.SetupAxes("X-Axis", "Y-Axis", "Z-Axis", s.xflags, s.yflags, s.zflags) ImPlot3D.PlotLine("Wave", s.x, s.y, s.z, 101) ImPlot3D.EndPlot() end end --------------------------------------------------------------------------- -- [SECTION] Tools --------------------------------------------------------------------------- local Plane = ImPlot3D.ImPlane3D S.DemoMousePicking = { points = {}, rays = {}, selected_plane = Plane.XY, mask_plane = true } function D.DemoMousePicking() IMGUI_DEMO_MARKER("Tools/Mouse Picking") local s = S.DemoMousePicking ImGui.BulletText("Click anywhere in the plot to place points/rays.") _, s.selected_plane = ImGui.RadioButton("XY-Plane", s.selected_plane, Plane.XY) ImGui.SameLine() _, s.selected_plane = ImGui.RadioButton("XZ-Plane", s.selected_plane, Plane.XZ) ImGui.SameLine() _, s.selected_plane = ImGui.RadioButton("YZ-Plane", s.selected_plane, Plane.YZ) _, s.mask_plane = ImGui.Checkbox("Mask Plane", s.mask_plane) if ImGui.Button("Clear") then table.clear(s.points) table.clear(s.rays) end if ImPlot3D.BeginPlot("Mouse Picking", ImVec2(-1, 0), Flags.NoClip) then ImPlot3D.SetupAxes("X-Axis", "Y-Axis", "Z-Axis") ImPlot3D.SetupAxesLimits(-1, 1, -1, 1, -1, 1) -- Get mouse position and convert to ray / point on the selected plane local mouse_pos = ImGui.GetMousePos() local ray = ImPlot3D.PixelsToPlotRay(mouse_pos) local point = ImPlot3D.PixelsToPlotPlane(mouse_pos, s.selected_plane, s.mask_plane) -- Show intersection point if ImGui.IsItemHovered() and not point:IsNaN() then ImPlot3D.PlotScatter("##Intersection", { point.x }, { point.y }, { point.z }, 1, { Marker = Marker.Circle, MarkerSize = 5, FillColor = ImVec4(1, 1, 0, 1) }) end -- Add point/ray on click if ImGui.IsItemHovered() and ImGui.IsMouseClicked(0) and not point:IsNaN() then table.insert(s.points, point) table.insert(s.rays, ray) end -- [Port] ImVector + Stride = sizeof(ImPlot3DPoint) -> split x/y/z arrays if #s.points > 0 then local xs, ys, zs = {}, {}, {} for i, p in __iter(s.points) do xs[i], ys[i], zs[i] = p.x, p.y, p.z end ImPlot3D.PlotScatter("Placed Points", xs, ys, zs, #xs, { Marker = Marker.Circle, MarkerSize = 3 }) -- Ray from added point to the camera local rx, ry, rz = {}, {}, {} for i, p in __iter(s.points) do local d = s.rays[i].Direction table.insert(rx, p.x); table.insert(ry, p.y); table.insert(rz, p.z) table.insert(rx, p.x - d.x); table.insert(ry, p.y - d.y); table.insert(rz, p.z - d.z) end ImPlot3D.PlotLine("Placed Rays", rx, ry, rz, #rx, { Flags = LineFlags.Segments }) end ImPlot3D.EndPlot() end end --------------------------------------------------------------------------- -- [SECTION] Custom --------------------------------------------------------------------------- -- [Port] ImPlot3DStyle copy assignment (deep, into dst so GetStyle() references stay valid) local function CopyStyle(dst, src) for k, v in __iter(src) do if k == "Colors" then dst.Colors = dst.Colors or {} for i, c in __iter(v) do dst.Colors[i] = ImVec4(c.x, c.y, c.z, c.w) end elseif type(v) == "table" then dst[k] = ImVec2(v.x, v.y) else dst[k] = v end end return dst end -- Example for Custom Styles section (MyImPlot3D::StyleSeaborn) local function StyleSeaborn() local style = ImPlot3D.GetStyle() local colors, Col = style.Colors, ImPlot3D.ImPlot3DCol colors[Col.FrameBg] = ImVec4(1.00, 1.00, 1.00, 1.00) colors[Col.PlotBg] = ImVec4(0.92, 0.92, 0.95, 1.00) colors[Col.PlotBorder] = ImVec4(0.00, 0.00, 0.00, 0.00) colors[Col.LegendBg] = ImVec4(0.92, 0.92, 0.95, 1.00) colors[Col.LegendBorder] = ImVec4(0.80, 0.81, 0.85, 1.00) colors[Col.LegendText] = ImVec4(0.00, 0.00, 0.00, 1.00) colors[Col.TitleText] = ImVec4(0.00, 0.00, 0.00, 1.00) colors[Col.InlayText] = ImVec4(0.00, 0.00, 0.00, 1.00) colors[Col.AxisText] = ImVec4(0.00, 0.00, 0.00, 1.00) colors[Col.AxisGrid] = ImVec4(1.00, 1.00, 1.00, 1.00) style.LineWeight = 1.5 style.Marker = Marker.None style.MarkerSize = 4 style.FillAlpha = 1.0 style.PlotPadding = ImVec2(12, 12) style.LabelPadding = ImVec2(5, 5) style.LegendPadding = ImVec2(5, 5) style.PlotMinSize = ImVec2(300, 225) end ImPlot3D.StyleSeaborn = StyleSeaborn local seaborn = { xs = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, ys = table.create(10, 0), lin = { 8, 8, 9, 7, 8, 8, 8, 9, 7, 8 }, dot = { 7, 6, 6, 7, 8, 5, 6, 5, 8, 7 } } function D.DemoCustomStyles() IMGUI_DEMO_MARKER("Custom/Custom Styles") ImPlot3D.PushColormap(Colormap.Deep) -- normally you wouldn't change the entire style each frame local backup = CopyStyle({}, ImPlot3D.GetStyle()) StyleSeaborn() if ImPlot3D.BeginPlot("Seaborn Style") then ImPlot3D.SetupAxes("X-axis", "Y-axis", "Z-axis") ImPlot3D.SetupAxesLimits(-0.5, 9.5, -0.5, 0.5, 0, 10) ImPlot3D.NextColormapColor() -- Skip blue ImPlot3D.PlotLine("Line", seaborn.xs, seaborn.ys, seaborn.lin, 10) ImPlot3D.NextColormapColor() -- Skip green ImPlot3D.PlotScatter("Scatter", seaborn.xs, seaborn.ys, seaborn.dot, 10) ImPlot3D.EndPlot() end CopyStyle(ImPlot3D.GetStyle(), backup) ImPlot3D.PopColormap() end local box_corners = { P(0, 0, 0), P(1, 0, 0), P(1, 1, 0), P(0, 1, 0), P(0, 0, 1), P(1, 0, 1), P(1, 1, 1), P(0, 1, 1) } function D.DemoCustomRendering() IMGUI_DEMO_MARKER("Custom/Custom Rendering") if ImPlot3D.BeginPlot("##CustomRend") then ImPlot3D.SetupAxesLimits(-0.1, 1.1, -0.1, 1.1, -0.1, 1.1) local dl = ImPlot3D.GetPlotDrawList() -- Draw circle local cntr = ImPlot3D.PlotToPixels(P(0.5, 0.5, 0.5)) dl:AddCircleFilled(cntr, 20, IM_COL32(255, 255, 0, 255), 20) -- Draw box local px = {} for i = 1, 8 do px[i] = ImPlot3D.PlotToPixels(box_corners[i]) end local col = IM_COL32(128, 0, 255, 255) for i = 1, 4 do local n = i % 4 + 1 dl:AddLine(px[i], px[n], col) dl:AddLine(px[i + 4], px[n + 4], col) dl:AddLine(px[i], px[i + 4], col) end ImPlot3D.EndPlot() end end S.DemoCustomOverlay = {} local gizmo_axes = { P(1, 0, 0), P(0, 1, 0), P(0, 0, 1) } local gizmo_colors = { IM_COL32(200, 50, 50, 255), IM_COL32(50, 200, 50, 255), IM_COL32(50, 50, 200, 255) } local gizmo_labels = { "X", "Y", "Z" } function D.DemoCustomOverlay() IMGUI_DEMO_MARKER("Custom/Custom Overlay") local s = S.DemoCustomOverlay ImGui.BulletText("Demonstrates custom 2D overlays using GetPlotRectPos/GetPlotRectSize.") ImGui.BulletText("Shows mouse tooltip, line to closest point, and orientation gizmo.") if s.xs == nil then srand(0) s.xs, s.ys, s.zs = {}, {}, {} for i = 1, 50 do s.xs[i], s.ys[i], s.zs[i] = rand01(), rand01(), rand01() end end if ImPlot3D.BeginPlot("##CustomOverlay", ImVec2(-1, 0)) then ImPlot3D.SetupAxes("X-Axis", "Y-Axis", "Z-Axis") ImPlot3D.SetupAxesLimits(0, 1, 0, 1, 0, 1) ImPlot3D.PlotScatter("Data", s.xs, s.ys, s.zs, 50) local draw_list = ImPlot3D.GetPlotDrawList() local mouse_pos = ImGui.GetMousePos() local plot_pos, plot_size = ImPlot3D.GetPlotRectPos(), ImPlot3D.GetPlotRectSize() if ImGui.IsItemHovered() then -- Find closest point to mouse in screen space local closest_idx, min_dist_sq, closest_px = -1, 1e10, nil for i = 1, 50 do local pp = ImPlot3D.PlotToPixels(s.xs[i], s.ys[i], s.zs[i]) local dx, dy = pp.x - mouse_pos.x, pp.y - mouse_pos.y if dx * dx + dy * dy < min_dist_sq then min_dist_sq, closest_idx, closest_px = dx * dx + dy * dy, i, pp end end if closest_idx >= 1 then draw_list:AddLine(mouse_pos, closest_px, IM_COL32(255, 255, 0, 255), 2.0) ImGui.BeginTooltip() ImGui.Text("Mouse: (%.1f, %.1f)", mouse_pos.x, mouse_pos.y) ImGui.Text("Closest Point #%d", closest_idx - 1) ImGui.Text("Position: (%.3f, %.3f, %.3f)", s.xs[closest_idx], s.ys[closest_idx], s.zs[closest_idx]) ImGui.Text("Distance: %.1f px", math.sqrt(min_dist_sq)) ImGui.EndTooltip() end end -- Draw orientation gizmo in bottom-right corner local plot = ImPlot3D.internal.GetCurrentPlot() if plot then local gc = ImVec2(plot_pos.x + plot_size.x - 50, plot_pos.y + plot_size.y - 50) local gizmo_size = 30.0 draw_list:AddCircleFilled(gc, gizmo_size + 5, IM_COL32(0, 0, 0, 100)) for i = 1, 3 do local rotated = plot.Rotation * gizmo_axes[i] -- Rotate axis by quaternion -- Project to 2D gizmo space (simple orthographic projection, Y flipped for screen coords) local axis_end = ImVec2(gc.x + rotated.x * gizmo_size, gc.y - rotated.y * gizmo_size) draw_list:AddLine(gc, axis_end, gizmo_colors[i], 2.0) draw_list:AddCircleFilled(axis_end, 4.0, gizmo_colors[i]) draw_list:AddText(ImVec2(axis_end.x + 8, axis_end.y - 8), gizmo_colors[i], gizmo_labels[i]) end end ImPlot3D.EndPlot() end end S.DemoCustomPerPointStyle = { marker_size = 4.0, cmap = Colormap.Viridis } local torus_labels = { "Height-colored", "Radial-colored", "Angular-colored" } local torus_legend_colors = { ImVec4(1, 0, 0, 1), ImVec4(0, 1, 0, 1), ImVec4(0, 0, 1, 1) } function D.DemoCustomPerPointStyle() IMGUI_DEMO_MARKER("Custom/Custom Per-Point Style") local s = S.DemoCustomPerPointStyle ImGui.BulletText("Demonstrates per-point coloring using colormap sampling.") ImGui.BulletText("A different color is sampled for each point.") ImGui.BulletText("All points share the same label for a single legend entry.") s.marker_size = ImGui.SliderFloat("Marker Size", s.marker_size, 2.0, 10.0) if ImGui.BeginCombo("Colormap", ImPlot3D.GetColormapName(s.cmap)) then local cd = ImPlot3D.GetCurrentContext().ColormapData for i = 0, ImPlot3D.GetColormapCount() - 1 do -- Only show continuous colormaps if not cd:IsQual(i) and ImGui.Selectable(ImPlot3D.GetColormapName(i), s.cmap == i) then s.cmap = i end end ImGui.EndCombo() end -- Generate three stacked toruses with different color patterns (x, y, z, t per point) if s.tori == nil then s.tori = {} local R, r = 0.6, 0.2 -- Major / minor radius for torus = 1, 3 do local z_offset = (3 - torus) * 0.6 local d = { xs = {}, ys = {}, zs = {}, ts = {}, colors = {} } for i = 0, 19 do local u = i / 20 * 2 * IM_PI for j = 0, 19 do local v = j / 20 * 2 * IM_PI local z = r * math.sin(v) + z_offset local t if torus == 1 then t = (z - (z_offset - r)) / (2 * r) -- Color by height elseif torus == 2 then t = (math.cos(v) + 1) / 2 -- Color by radial distance from tube center else t = (math.cos(u) + 1) / 2 -- Angular pattern around the major circle end table.insert(d.xs, (R + r * math.cos(v)) * math.cos(u)) table.insert(d.ys, (R + r * math.cos(v)) * math.sin(u)) table.insert(d.zs, z) table.insert(d.ts, t) end end s.tori[torus] = d end end for _, d in __iter(s.tori) do for i = 1, 400 do d.colors[i] = ImGui.ColorConvertFloat4ToU32(ImPlot3D.SampleColormap(d.ts[i], s.cmap)) end end if ImPlot3D.BeginPlot("##PerPointStyle", ImVec2(-1, 0)) then ImPlot3D.SetupAxes("X", "Y", "Z") ImPlot3D.SetupAxesLimits(-1, 1, -1, 1, -0.5, 1.5) for torus = 1, 3 do local d = s.tori[torus] ImPlot3D.PlotScatter(torus_labels[torus], d.xs, d.ys, d.zs, 400, { Marker = Marker.Circle, MarkerSize = s.marker_size, MarkerFillColors = d.colors, MarkerLineColors = d.colors }) -- Override legend color with PlotDummy local c = torus_legend_colors[torus] ImPlot3D.PlotDummy(torus_labels[torus], { Marker = Marker.Circle, MarkerSize = s.marker_size, MarkerFillColor = c, MarkerLineColor = c }) end ImPlot3D.EndPlot() end end --------------------------------------------------------------------------- -- [SECTION] Config --------------------------------------------------------------------------- S.DemoConfig = {} function D.DemoConfig() IMGUI_DEMO_MARKER("Config") local s = S.DemoConfig ImGui.ShowFontSelector("Font") ImGui.ShowStyleSelector("ImGui Style") ImPlot3D.ShowStyleSelector("ImPlot3D Style") ImPlot3D.ShowColormapSelector("ImPlot3D Colormap") ImGui.Separator() -- Preview plot with 3D spirals if ImPlot3D.BeginPlot("Preview", ImVec2(-1, 0)) then if s.xs == nil then s.xs, s.ys, s.zs = {}, {}, {} for i = 1, 10 do s.xs[i], s.ys[i], s.zs[i] = {}, {}, {} for j = 1, 50 do local angle = (j - 1) / 49 * 4 * IM_PI local radius = 0.3 + (i - 1) * 0.05 s.xs[i][j], s.ys[i][j], s.zs[i][j] = radius * math.cos(angle), radius * math.sin(angle), (i - 1) / 9 end end end for i = 1, 10 do ImGui.PushID(i - 1) ImPlot3D.PlotLine("##Spiral", s.xs[i], s.ys[i], s.zs[i], 50) ImGui.PopID() end ImPlot3D.EndPlot() end end --------------------------------------------------------------------------- -- [SECTION] Demo Window --------------------------------------------------------------------------- local help_sections = { { "Translation", { "Left-click drag to translate.", "If over axis, only that axis will translate.", "If over plane, only that plane will translate.", "If outside plot area, translate in the view plane." } }, { "Zoom", { "Scroll or middle-click drag to zoom.", "If over axis, only that axis will zoom.", "If over plane, only that plane will zoom.", "If outside plot area, zoom the entire plot." } }, { "Rotation", { "Right-click drag to rotate.", "To reset rotation, double right-click outside plot area.", "To rotate to plane, double right-click when over the plane." } }, { "Fit data", { "Double left-click to fit.", "If over axis, fit data to axis.", "If over plane, fit data to plane.", "If outside plot area, fit data to plot." } }, { "Context Menus", { "Right-click outside plot area to show full context menu.", "Right-click over legend to show legend context menu.", "Right-click over axis to show axis context menu.", "Right-click over plane to show plane context menu." } }, } function D.DemoHelp() IMGUI_DEMO_MARKER("Help") ImGui.SeparatorText("ABOUT THIS DEMO:") ImGui.BulletText("The other tabs are demonstrating many aspects of the library.") ImGui.SeparatorText("PROGRAMMER GUIDE:") ImGui.BulletText("See the ShowDemoWindow() code in ImPlot3DDemo.lua. <- you are here!") ImGui.BulletText("See comments in ImPlot3DDemo.lua.") ImGui.BulletText("See example application in examples/ folder.") ImGui.SeparatorText("USER GUIDE:") for _, sec in __iter(help_sections) do ImGui.BulletText(sec[1]) ImGui.Indent() for _, line in __iter(sec[2]) do ImGui.BulletText(line) end ImGui.Unindent() end ImGui.BulletText("Click legend label icons to show/hide plot items.") end local function DemoHeader(label, demo) if ImGui.TreeNodeEx(label) then demo() ImGui.TreePop() end end local demo_tabs = { { "Plots", { "Plot Types", { "Line Plots", D.DemoLinePlots }, { "Scatter Plots", D.DemoScatterPlots }, { "Triangle Plots", D.DemoTrianglePlots }, { "Quad Plots", D.DemoQuadPlots }, { "Surface Plots", D.DemoSurfacePlots }, { "Mesh Plots", D.DemoMeshPlots }, { "Realtime Plots", D.DemoRealtimePlots }, { "Image Plots", D.DemoImagePlots }, "Plot Options", { "Plot Flags", D.DemoPlotFlags }, { "Offset and Stride", D.DemoOffsetAndStride }, { "Legend Options", D.DemoLegendOptions }, { "Markers and Text", D.DemoMarkersAndText }, { "NaN Values", D.DemoNaNValues }, { "Per-Index Colors", D.Demo_PerIndexColors } } }, { "Axes", { { "Box Scale", D.DemoBoxScale }, { "Box Rotation", D.DemoBoxRotation }, { "Log Scale", D.Demo_LogScale }, { "Symmetric Log Scale", D.Demo_SymmetricLogScale }, { "Tick Labels", D.DemoTickLabels }, { "Axis Constraints", D.DemoAxisConstraints }, { "Equal Axes", D.DemoEqualAxes }, { "Auto-Fitting Data", D.DemoAutoFittingData } } }, { "Tools", { { "Mouse Picking", D.DemoMousePicking } } }, { "Custom", { { "Custom Styles", D.DemoCustomStyles }, { "Custom Rendering", D.DemoCustomRendering }, { "Custom Overlay", D.DemoCustomOverlay }, { "Custom Per-Point Style", D.DemoCustomPerPointStyle } } }, { "Config", D.DemoConfig }, { "Help", D.DemoHelp }, } function ImPlot3D.ShowAllDemos() ImGui.Text("ImPlot3D says olá! (%s) (%d)", ImPlot3D.IMPLOT3D_VERSION, ImPlot3D.IMPLOT3D_VERSION_NUM) ImGui.Spacing() if ImGui.BeginTabBar("ImPlot3DDemoTabs") then for _, tab in __iter(demo_tabs) do if ImGui.BeginTabItem(tab[1]) then if type(tab[2]) == "function" then tab[2]() else for _, e in __iter(tab[2]) do if type(e) == "string" then ImGui.SeparatorText(e) else DemoHeader(e[1], e[2]) end end end ImGui.EndTabItem() end end ImGui.EndTabBar() end end S.ShowDemoWindow = { show_implot3d_metrics = false, show_implot3d_style_editor = false, show_implot3d_about = false, show_imgui_metrics = false, show_imgui_style_editor = false, show_imgui_demo = false } -- [Port] p_open: true/false/nil (nil = no close button). Returns the new p_open value. function ImPlot3D.ShowDemoWindow(p_open) local s = S.ShowDemoWindow if s.show_implot3d_metrics then s.show_implot3d_metrics = ImPlot3D.ShowMetricsWindow(s.show_implot3d_metrics) end if s.show_implot3d_style_editor then s.show_implot3d_style_editor = ImGui.Begin("Style Editor (ImPlot3D)", s.show_implot3d_style_editor) ImPlot3D.ShowStyleEditor() ImGui.End() end if s.show_implot3d_about then s.show_implot3d_about = ImPlot3D.ShowAboutWindow(s.show_implot3d_about) end if s.show_imgui_style_editor then s.show_imgui_style_editor = ImGui.Begin("Style Editor (ImGui)", s.show_imgui_style_editor) ImGui.ShowStyleEditor() ImGui.End() end if s.show_imgui_metrics then s.show_imgui_metrics = ImGui.ShowMetricsWindow(s.show_imgui_metrics) end if s.show_imgui_demo then s.show_imgui_demo = ImGui.ShowDemoWindow(s.show_imgui_demo) end ImGui.SetNextWindowPos(ImVec2(100, 100), G.ImGuiCond.FirstUseEver) ImGui.SetNextWindowSize(ImVec2(600, 750), G.ImGuiCond.FirstUseEver) p_open = ImGui.Begin("ImPlot3D Demo", p_open, G.ImGuiWindowFlags.MenuBar) if ImGui.BeginMenuBar() then if ImGui.BeginMenu("Tools") then _, s.show_implot3d_metrics = ImGui.MenuItem("Metrics", nil, s.show_implot3d_metrics) _, s.show_implot3d_style_editor = ImGui.MenuItem("Style Editor", nil, s.show_implot3d_style_editor) _, s.show_implot3d_about = ImGui.MenuItem("About ImPlot3D", nil, s.show_implot3d_about) ImGui.Separator() _, s.show_imgui_metrics = ImGui.MenuItem("ImGui Metrics", nil, s.show_imgui_metrics) _, s.show_imgui_style_editor = ImGui.MenuItem("ImGui Style Editor", nil, s.show_imgui_style_editor) _, s.show_imgui_demo = ImGui.MenuItem("ImGui Demo", nil, s.show_imgui_demo) ImGui.EndMenu() end ImGui.EndMenuBar() end ImPlot3D.ShowAllDemos() ImGui.End() return p_open end --------------------------------------------------------------------------- -- [SECTION] Style Editor --------------------------------------------------------------------------- local internal = ImPlot3D.internal local ImGuiCol, CEF = G.ImGuiCol, G.ImGuiColorEditFlags -- ColormapButton(label, size_arg, cmap) -> pressed function ImPlot3D.ColormapButton(label, size_arg, cmap) local style = ImGui.GetStyle() local Window = ImGui.GetCurrentWindow() if Window.SkipItems then return false end local gp = ImPlot3D.GetCurrentContext() cmap = internal.ResolveColormap(gp, cmap) local keys, off = gp.ColormapData:GetKeys(cmap) local pos = Window.DC.CursorPos local label_size = ImGui.CalcTextSize(label, nil, true) local size = ImGui.CalcItemSize(size_arg, label_size.x + style.FramePadding.x * 2.0, label_size.y + style.FramePadding.y * 2.0) local rect = G.ImRect(pos.x, pos.y, pos.x + size.x, pos.y + size.y) internal.RenderColorBar(keys, off, gp.ColormapData:GetKeyCount(cmap), ImGui.GetWindowDrawList(), rect, false, false, not gp.ColormapData:IsQual(cmap)) local text = internal.CalcTextColor(gp.ColormapData:LerpTable(cmap, style.ButtonTextAlign.x)) ImGui.PushStyleColor(ImGuiCol.Button, G.IM_COL32_BLACK_TRANS) ImGui.PushStyleColor(ImGuiCol.ButtonHovered, ImVec4(1, 1, 1, 0.1)) ImGui.PushStyleColor(ImGuiCol.ButtonActive, ImVec4(1, 1, 1, 0.2)) ImGui.PushStyleColor(ImGuiCol.Text, text) ImGui.PushStyleVar(G.ImGuiStyleVar.FrameRounding, 0) local pressed = ImGui.Button(label, size) ImGui.PopStyleColor(4) ImGui.PopStyleVar(1) return pressed end local function ColorsDiffer(a, b) return a.x ~= b.x or a.y ~= b.y or a.z ~= b.z or a.w ~= b.w end local function V4(c) return ImVec4(c.x, c.y, c.z, c.w) end local SE = { init = true, flash_color_time = 0.5, flash_color_idx = nil, flash_color_backup = nil, output_dest = 0, output_only_modified = true, alpha_flags = CEF.AlphaPreviewHalf, cmap_output_dest = 0, edit = false, qual = true } local function StyleEditorColors(style, ref) local COUNT = ImPlot3D.ImPlot3DCol.COUNT if ImGui.Button("Export") then if SE.output_dest == 0 then ImGui.LogToClipboard() else ImGui.LogToTTY() end ImGui.LogText("ImVec4* colors = ImPlot3D::GetStyle().Colors;\n") for i = 0, COUNT - 1 do local col, name = style.Colors[i], internal.GetStyleColorName(i) if not SE.output_only_modified or ColorsDiffer(col, ref.Colors[i]) then ImGui.LogText("colors[ImPlot3DCol_%s]%s= ImVec4(%.2ff, %.2ff, %.2ff, %.2ff);\n", name, string.rep(" ", math.max(0, 15 - #name)), col.x, col.y, col.z, col.w) end end ImGui.LogFinish() end ImGui.SameLine(); ImGui.SetNextItemWidth(120) SE.output_dest = ImGui.Combo("##output_type", SE.output_dest, "To Clipboard\0To TTY\0") ImGui.SameLine() _, SE.output_only_modified = ImGui.Checkbox("Only Modified Colors", SE.output_only_modified) if SE.filter == nil then SE.filter = G.ImGuiTextFilter() end ImGui.SetNextItemWidth(ImGui.GetFontSize() * 16) SE.filter:Draw("Filter colors") for _, e in __iter({ { "Opaque", CEF.AlphaOpaque }, { "Alpha", CEF.None }, { "Both", CEF.AlphaPreviewHalf } }) do if ImGui.RadioButtonEx(e[1], SE.alpha_flags == e[2]) then SE.alpha_flags = e[2] end ImGui.SameLine() end HelpMarker("In the color list:\nLeft-click on color square to open color picker,\nRight-click to open edit options menu.") ImGui.Separator() for i = 0, COUNT - 1 do local name = internal.GetStyleColorName(i) if SE.filter:PassFilter(name) then ImGui.PushID(i) -- Flash color if ImGui.Button("?") then if SE.flash_color_idx ~= nil then style.Colors[SE.flash_color_idx] = SE.flash_color_backup end SE.flash_color_time, SE.flash_color_idx, SE.flash_color_backup = 0.5, i, V4(style.Colors[i]) end ImGui.SetItemTooltip("Flash given color to identify places where it is used.") ImGui.SameLine() -- Handle auto color selection local is_auto = internal.IsColorAuto(style.Colors[i]) if is_auto then ImGui.BeginDisabled() end if ImGui.Button("Auto") then style.Colors[i] = ImPlot3D.IMPLOT3D_AUTO_COL() end if is_auto then ImGui.EndDisabled() end -- Color selection ImGui.SameLine() if ImGui.ColorEdit4("##Color", style.Colors[i], bor(CEF.NoInputs, SE.alpha_flags)) then if style.Colors[i].w == -1 then style.Colors[i].w = 1 end end -- Save/Revert buttons if color changed if ColorsDiffer(style.Colors[i], ref.Colors[i]) then ImGui.SameLine() if ImGui.Button("Save") then ref.Colors[i] = V4(style.Colors[i]) end ImGui.SameLine() if ImGui.Button("Revert") then style.Colors[i] = V4(ref.Colors[i]) end end ImGui.SameLine() ImGui.TextUnformatted(name) ImGui.PopID() end end end local function StyleEditorColormaps(gp) if ImGui.Button("Export", ImVec2(75, 0)) then if SE.cmap_output_dest == 0 then ImGui.LogToClipboard() else ImGui.LogToTTY() end local size = ImPlot3D.GetColormapSize() local name = ImPlot3D.GetColormapName(gp.Style.Colormap) ImGui.LogText("static const ImU32 %s_Data[%d] = {\n", name, size) for i = 0, size - 1 do ImGui.LogText(" %u%s\n", internal.GetColormapColorU32(i, gp.Style.Colormap), i == size - 1 and"" or",") end ImGui.LogText("};\nImPlotColormap %s = ImPlot::AddColormap(\"%s\", %s_Data, %d);", name, name, name, size) ImGui.LogFinish() end ImGui.SameLine(); ImGui.SetNextItemWidth(120) SE.cmap_output_dest = ImGui.Combo("##output_type", SE.cmap_output_dest, "To Clipboard\0To TTY\0") ImGui.SameLine() _, SE.edit = ImGui.Checkbox("Edit Mode", SE.edit) -- built-in/added ImGui.Separator() local cd = gp.ColormapData for i = 0, cd.Count - 1 do ImGui.PushID(i) local size = cd:GetKeyCount(i) local selected = i == gp.Style.Colormap if not selected then ImGui.PushStyleVar(G.ImGuiStyleVar.Alpha, 0.25) end if ImGui.Button(ImPlot3D.GetColormapName(i), ImVec2(100, 0)) then gp.Style.Colormap = i internal.BustItemCache() end if not selected then ImGui.PopStyleVar() end ImGui.SameLine() ImGui.BeginGroup() if SE.edit then for c = 0, size - 1 do ImGui.PushID(c) local col4 = ImGui.ColorConvertU32ToFloat4(cd:GetKeyColor(i, c)) if ImGui.ColorEdit4("", col4, CEF.NoInputs) then cd:SetKeyColor(i, c, ImGui.ColorConvertFloat4ToU32(col4)) internal.BustItemCache() end if (c + 1) % 12 ~= 0 and c ~= size - 1 then ImGui.SameLine() end ImGui.PopID() end elseif ImPlot3D.ColormapButton("##", ImVec2(-1, 0), i) then SE.edit = true end ImGui.EndGroup() ImGui.PopID() end if SE.custom == nil then SE.custom = { ImVec4(1, 0, 0, 1), ImVec4(0, 1, 0, 1), ImVec4(0, 0, 1, 1) } SE.name = ImGui.NewTextBuffer("MyColormap", 16) end local custom = SE.custom ImGui.Separator() ImGui.BeginGroup() local half = (100 - ImGui.GetStyle().ItemSpacing.x) / 2 if ImGui.Button("+", ImVec2(half, 0)) then table.insert(custom, ImVec4(0, 0, 0, 1)) end ImGui.SameLine() if ImGui.Button("-", ImVec2(half, 0)) and #custom > 2 then table.remove(custom) end ImGui.SetNextItemWidth(100) ImGui.InputText("##Name", SE.name, 16, G.ImGuiInputTextFlags.CharsNoBlank) _, SE.qual = ImGui.Checkbox("Qualitative", SE.qual) local name = ImGui.TextBufferToString(SE.name) if ImGui.Button("Add", ImVec2(100, 0)) and cd:GetIndex(name) == -1 then ImPlot3D.AddColormap(name, custom, #custom, SE.qual) end ImGui.EndGroup() ImGui.SameLine() ImGui.BeginGroup() for c = 1, #custom do ImGui.PushID(c - 1) ImGui.ColorEdit4("##Col1", custom[c], CEF.NoInputs) if c % 12 ~= 0 then ImGui.SameLine() end ImGui.PopID() end ImGui.EndGroup() end -- [Port] ImPlot3DStyle* ref -> optional style table (defaults to internal saved copy) function ImPlot3D.ShowStyleEditor(ref) local gp = ImPlot3D.GetCurrentContext() -- Handle style internal storage local style = ImPlot3D.GetStyle() if SE.init and ref == nil then SE.ref_saved_style = CopyStyle({}, style) end SE.init = false ref = ref or SE.ref_saved_style -- Handle flash style color if SE.flash_color_idx ~= nil then local color = style.Colors[SE.flash_color_idx] color.x, color.y, color.z = ImGui.ColorConvertHSVtoRGB(math.cos(SE.flash_color_time * 6.0) * 0.5 + 0.5, 0.5, 0.5) color.w = 1.0 -- Decrease timer until zero, then restore the backup color SE.flash_color_time =SE.flash_color_time- ImGui.GetIO().DeltaTime if SE.flash_color_time <= 0.0 then style.Colors[SE.flash_color_idx] = SE.flash_color_backup SE.flash_color_idx, SE.flash_color_time = nil, 0.5 end end -- Style selector if ImPlot3D.ShowStyleSelector("Colors##Selector") then CopyStyle(SE.ref_saved_style, style) end -- Save/Revert button if ImGui.Button("Save Ref") then CopyStyle(ref, style) CopyStyle(SE.ref_saved_style, style) end ImGui.SameLine() if ImGui.Button("Revert Ref") then CopyStyle(style, ref) end ImGui.SameLine() HelpMarker("Save/Revert in local non-persistent storage. Default Colors definition are not affected. " .. "Use \"Export\" below to save them somewhere.") ImGui.Separator() if ImGui.BeginTabBar("##Tabs", G.ImGuiTabBarFlags.None) then if ImGui.BeginTabItem("Variables") then ImGui.Text("Item Styling") style.LineWeight = ImGui.SliderFloat("LineWeight", style.LineWeight, 0.0, 5.0, "%.1f") style.MarkerSize = ImGui.SliderFloat("MarkerSize", style.MarkerSize, 2.0, 10.0, "%.1f") style.FillAlpha = ImGui.SliderFloat("FillAlpha", style.FillAlpha, 0.0, 1.0, "%.2f") ImGui.Text("Plot Styling") ImGui.SliderFloat2("PlotDefaultSize", style.PlotDefaultSize, 0.0, 1000, "%.0f") ImGui.SliderFloat2("PlotMinSize", style.PlotMinSize, 0.0, 300, "%.0f") ImGui.SliderFloat2("PlotPadding", style.PlotPadding, 0.0, 20.0, "%.0f") ImGui.SliderFloat2("LabelPadding", style.LabelPadding, 0.0, 20.0, "%.0f") style.ViewScaleFactor = ImGui.SliderFloat("ViewScaleFactor", style.ViewScaleFactor, 0.1, 2.0, "%.2f") ImGui.Text("Legend Styling") ImGui.SliderFloat2("LegendPadding", style.LegendPadding, 0.0, 20.0, "%.0f") ImGui.SliderFloat2("LegendInnerPadding", style.LegendInnerPadding, 0.0, 10.0, "%.0f") ImGui.SliderFloat2("LegendSpacing", style.LegendSpacing, 0.0, 5.0, "%.0f") ImGui.EndTabItem() end if ImGui.BeginTabItem("Colors") then StyleEditorColors(style, ref) ImGui.EndTabItem() end if ImGui.BeginTabItem("Colormaps") then StyleEditorColormaps(gp) ImGui.EndTabItem() end ImGui.EndTabBar() end end S.ShowAboutWindow = { show_config_info = false } -- [Port] bool* p_open -> returns the new open state function ImPlot3D.ShowAboutWindow(p_open) local s = S.ShowAboutWindow local visible p_open, visible = ImGui.Begin("About ImPlot3D", p_open, G.ImGuiWindowFlags.AlwaysAutoResize) if not visible then ImGui.End() return p_open end ImGui.Text("ImPlot3D %s (%d)", ImPlot3D.IMPLOT3D_VERSION, ImPlot3D.IMPLOT3D_VERSION_NUM) ImGui.TextLinkOpenURL("Homepage", "https://github.com/brenocq/implot3d") ImGui.SameLine() ImGui.TextLinkOpenURL("Q&A", "https://github.com/brenocq/implot3d/discussions/categories/q-a") ImGui.SameLine() ImGui.TextLinkOpenURL("Releases", "https://github.com/brenocq/implot3d/releases") ImGui.SameLine() ImGui.TextLinkOpenURL("Sponsors", "https://github.com/sponsors/brenocq") ImGui.Separator() ImGui.Text("(c) 2024-2025 Breno Cunha Queiroz") ImGui.Text("Developed by Breno Cunha Queiroz and all ImPlot3D contributors.") ImGui.Text("ImPlot3D is licensed under the MIT License.") ImGui.Text("If your company uses ImPlot3D, please consider sponsoring the project.") _, s.show_config_info = ImGui.Checkbox("Config/Build Information", s.show_config_info) if s.show_config_info then local copy_to_clipboard = ImGui.Button("Copy to clipboard") ImGui.BeginChild(ImGui.GetID("cfg_infos"), ImVec2(0, ImGui.GetTextLineHeightWithSpacing() * 18), G.ImGuiChildFlags.FrameStyle) if copy_to_clipboard then ImGui.LogToClipboard() ImGui.LogText("```\n") end ImGui.Text("ImPlot3D %s (%d)", ImPlot3D.IMPLOT3D_VERSION, ImPlot3D.IMPLOT3D_VERSION_NUM) ImGui.Separator() -- [Port] C++ sizeof/compiler defines -> Luau runtime info ImGui.Text("Luau port (imgui-luau), _VERSION: %s", _VERSION) if copy_to_clipboard then ImGui.LogText("```\n") ImGui.LogFinish() end ImGui.EndChild() end ImGui.End() return p_open end return ImPlot3D.ShowDemoWindow end