Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImPlot support? #2

Closed
bitsauce opened this issue Jun 20, 2021 · 15 comments
Closed

ImPlot support? #2

bitsauce opened this issue Jun 20, 2021 · 15 comments
Assignees
Labels
enhancement New feature or request

Comments

@bitsauce
Copy link

Hi there!

Good work on this repo btw! It's so nice to have ImGui working in Unity without a hassle.

One question I have is, have you considered adding ImPlot to this package?

I actually compiled cimgui, cimplot and ImPlot.NET from scratch yesterday and added it to uimgui, but the end result was just having Unity crash when I try to use any ImPlot functions in Unity. I'm guessing the problem is that my compiler environment is not set up properly (compiler flags, etc.). Or maybe ImPlot won't work since it uses GPU acceleration?

Anyway, I was just curious if adding ImPlot is something you would consider for this repo?
Thanks

@psydack
Copy link
Owner

psydack commented Jun 21, 2021

Hello!

Yes, I'm trying to compile ImGui.NET libs like implot, imnodes, etc, but seems to be very unstable and crash often even on ImGui.NET.
I can't tell what's wrong, but when I have anything I will push it.

So, yeah, I'm planning to add the ImPlot.

Thank you.

@psydack psydack added the enhancement New feature or request label Jun 21, 2021
@psydack psydack self-assigned this Jun 21, 2021
@Vivian-A
Copy link

Vivian-A commented Jun 28, 2021

It's possible to do, check out https://github.com/TillAlex/ImGui.NET-nativebuild/tree/additional_libraries . I use it all the time inside of unity. It's a bit of a pain to set up, but I can send you the package I made to make it work inside of unity if need be.

@psydack
Copy link
Owner

psydack commented Jun 28, 2021

It will be great. I'm trying to recompile each library with the most stable version but it's not working. Thank you =)

@Vivian-A
Copy link

Hm, unfortunately I don't have them running on the most recent version... Sorry about that! Though, it seems that ImGuiNET/ImGui.NET#218 (comment) will help quite a bit!

@psydack
Copy link
Owner

psydack commented Jun 29, 2021

I'm following that but isn't work yet. I will try a little more before bothering them. Thanks.

@viliwonka
Copy link

I got Implot working in Unity.

@viliwonka
Copy link

Packages.zip

Files for current package (including dlls)

@viliwonka
Copy link

viliwonka commented Jun 29, 2021

Example code, hopefully all this will be sufficient.

using ImGuiNET;
using ImPlotNET;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

    public class ExponentialDecayWindow : ImguiWindow {

        [Range(1f, 200f)]
        public int TicksPerSecond = 30;

        [Range(0.25f, 100f)]
        public float TotalTime = 10f;

        public float HalfLife = 1f;

        public float InitialValue = 10f;

        float decay;

        protected override void Awake() {

            base.Awake();

            WindowName = "Exponential Decay";
        }

        float Continious(float t) {

            return InitialValue * Mathf.Exp(-decay * t);
        }

        float DiscreteEuler(float value, float dt) {

            return -decay * value * dt;
        }

        float[] continiousArr ;
        float[] discreteArr   ;
        float[] timeArr       ;


        protected override void OnLayout() {

            if(Expanded = ImGui.Begin(WindowName, ref Open)) {

                // calculate decay from half life
                decay = Mathf.Log(2) / HalfLife;

                ImGui.DragInt("TicksPerSecond", ref TicksPerSecond, 10f, 5, 1000);
                ImGui.DragFloat("TotalTime", ref TotalTime, 0.5f, 0.1f, 100f);

                ImGui.DragFloat("Half Life", ref HalfLife, 0.05f, 0.001f, 10f);
                ImGui.DragFloat("Initial Value", ref InitialValue, 0.5f, -10f, 10f);

                float t = 0f;
                float dt = 1f / TicksPerSecond;

                int samples = Mathf.RoundToInt(TotalTime / dt);

                if(continiousArr == null || continiousArr.Length != samples) {
                    continiousArr = new float[samples];
                    discreteArr = new float[samples];
                    timeArr = new float[samples];
                }


                continiousArr[0] = InitialValue;
                discreteArr[0] = InitialValue;
                timeArr[0] = 0f;
                for(int i = 1; i < samples; i++) {

                    t += dt;
                    timeArr[i] = t;

                    continiousArr[i] = Continious(t);
                    discreteArr[i] = discreteArr[i - 1] + DiscreteEuler(discreteArr[i - 1], dt);

                }

                if(ImPlot.BeginPlot("Plot", "time", "value")) {

                    //ImPlot.SetNextPlotTicksX(ref time[0], 10000);
                    ImPlot.PlotLine("continious", ref timeArr[0], ref continiousArr[0], continiousArr.Length);
                    ImPlot.PlotLine("discrete", ref timeArr[0], ref discreteArr[0], discreteArr.Length);


                    ImPlot.EndPlot();
                }

                ImGui.End();
            }
        }

    }

@viliwonka
Copy link

Main modifications was in ImGuiUnityContext.cs with addition of ImPlot context that is "nested inside".

@psydack
Copy link
Owner

psydack commented Jun 30, 2021

I managed to work with the most recent commit, but I wondering if I wait they release the new version - probably 1.84 - or if I make a branch to 1.82 with implot, etc without docking support, etc. The last option has some impact to rewrite a lot of code and that imply to not have freetype too. =/

@viliwonka
Copy link

My version I uploaded has docking support, works with ImPlot too (since it is part of window anyway).

As for rewriting Imgui.NET, in my experience just consists of changing vector2/3 to unity's vector2/3 afaik, but I believe u had more work done in it.

@psydack
Copy link
Owner

psydack commented Jul 4, 2021

Had a more code to rewrite because I choose to work with Freetype and WCHAR. But we're having some progress.
image

=)

If someone want to test use this branch: https://github.com/psydack/uimgui/tree/feature/add-support-implot-imnodes-imguizmos

@psydack
Copy link
Owner

psydack commented Jul 5, 2021

Now you can use imnodes and imguizmo.
Sample for imnodes:

private void OnLayout(UImGui uImGui)
{
	if (ImGui.Begin("ImNodes window"))
	{
		imnodes.BeginNodeEditor();
		imnodes.BeginNode(1);

		imnodes.BeginNodeTitleBar();
		ImGui.TextUnformatted("simple node :)");
		imnodes.EndNodeTitleBar();

		imnodes.BeginInputAttribute(2);
		ImGui.Text("input");
		imnodes.EndInputAttribute();

		imnodes.BeginOutputAttribute(3);
		ImGui.Indent(40);
		ImGui.Text("output");
		imnodes.EndOutputAttribute();

		imnodes.EndNode();
		imnodes.EndNodeEditor();
		ImGui.End();
	}
}

I'm studying the best way to deploy and distribute these tools as addons.
Thank you, everyone.

@psydack psydack closed this as completed Jul 5, 2021
@viliwonka
Copy link

Cool :D How is with imnodes support, afaik it requires tons of libraries (sdl ?)

@psydack
Copy link
Owner

psydack commented Jul 5, 2021

It didn't require any lib to run but cimgui at least. If you want to see it what changes, check this comparison: main...feature/add-support-implot-imnodes-imguizmos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants