plugin for unity 3D

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Re: plugin for unity 3D

Postby Sweenie » Wed Jun 15, 2016 3:26 am

The callback seems to work fine...

By the way, you should be able to simplify the code like this...

Replace this...
Code: Select all
DrawFaceDelegateCallback drawCallback = new DrawFaceDelegateCallback(DrawFace);
m_shape.DebugRender(drawCallback);


with this
Code: Select all
m_shape.DebugRender(DrawFace);


C# will know the DrawFace function has the same signature as the Delegate and handle it automatically.
Sweenie
 
Posts: 503
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: plugin for unity 3D

Postby Sweenie » Wed Jun 15, 2016 3:41 am

to me is silly that swig does not have a way to map array of data other than IntPtr. and we have to use that Marshal.Copy stuff, but that is good enough.


That is why I still think that we should use the "unsafe" method.
As I mentioned before, unsafe is a misleading name, it simply means pointers are allowed in C#. Since we are using interops against a c++ dll we already are in the "unsafe" zone so I don't see any problems with using pointers.

Recently Unity has added a few new methods for RayCast and collision called "NonAlloc" versions, for example, Physics.RaycastNonAlloc
They were having performance problems with the Garbage collector due to array allocations.
I bet they are using unsafe methods themselves for this. I think it's necessary to get good performance.

Here is some example code...
https://msdn.microsoft.com/en-us/library/f58wzh21.aspx
Sweenie
 
Posts: 503
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: plugin for unity 3D

Postby Julio Jerez » Wed Jun 15, 2016 8:42 am

yes I see what you mean about the memory, but I believe we can work around that problem witout sacrificing performance more than it need to,

This is how the function look now
Code: Select all
      public void DrawFace(IntPtr vertexDataPtr, int vertexCount)
        {
            float[] polygon = new float[vertexCount * 3];

            Marshal.Copy(vertexDataPtr, polygon, 0, vertexCount * 3);
            int i0 = vertexCount - 1;
            for (int i1 = 0; i1 < vertexCount; i1++)
            {
                Vector3 pA = new Vector3(polygon[i0 * 3], polygon[i0 * 3 + 1], polygon[i0 * 3 + 2]);
                Vector3 pB = new Vector3(polygon[i1 * 3], polygon[i1 * 3 + 1], polygon[i1 * 3 + 2]);
                Gizmos.DrawLine(pA, pB);
                i0 = i1;
            }
        }


I can see how line float[] polygon = new float[vertexCount * 3];
can stress the garbage collector manager, but I have an idea how we solve fix that later.

for these lines
    Vector3 pA = new Vector3(polygon[i0 * 3], polygon[i0 * 3 + 1], polygon[i0 * 3 + 2]);
    Vector3 pB = new Vector3(polygon[i1 * 3], polygon[i1 * 3 + 1], polygon[i1 * 3 + 2]);
I was my impression that C# and also java recognize these king of objects and get the memory from the stack instead of from the garbage collector.
In fact is a couple of book I read about compiler design, it say that that one of the weakness of language with automatic garbage collision, that in eh end they need to special case some objected like primitives and use stack memory so that the do not pollute the garbage collector.

I do not know, but it will * is those line use the GC to make a point and in each iteration.

Any way now I see how to add editing capability to the shapes.
we need to add a gizmos that we can select for rotation, positioning and resizing.
also adding a rest bottom so that the shape adopt the size of the mesh bounding box.
Julio Jerez
Moderator
Moderator
 
Posts: 12478
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: plugin for unity 3D

Postby Julio Jerez » Wed Jun 15, 2016 8:56 am

ok now it look like this

Code: Select all
        public void DrawFace(IntPtr vertexDataPtr, int vertexCount)
        {
            Marshal.Copy(vertexDataPtr, m_debugDisplayVertexBuffer, 0, vertexCount * 3);
            int i0 = vertexCount - 1;
            for (int i1 = 0; i1 < vertexCount; i1++)
            {
                m_lineP0.x = m_debugDisplayVertexBuffer[i0 * 3 + 0];
                m_lineP0.y = m_debugDisplayVertexBuffer[i0 * 3 + 1];
                m_lineP0.z = m_debugDisplayVertexBuffer[i0 * 3 + 2];
                m_lineP1.x = m_debugDisplayVertexBuffer[i1 * 3 + 0];
                m_lineP1.y = m_debugDisplayVertexBuffer[i1 * 3 + 1];
                m_lineP1.z = m_debugDisplayVertexBuffer[i1 * 3 + 2];
                Gizmos.DrawLine(m_lineP0, m_lineP1);
                i0 = i1;
            }
        }


zero memory allocation, the only ugly part is that
Code: Select all
Marshal.Copy(vertexDataPtr, m_debugDisplayVertexBuffer, 0, vertexCount * 3);


but I think we can live with that, unless this is doing some hidden memory allocation shenanigan. but do not think so, I think is just find the memory pointer to copy the data.

anyway now on to see how to add editing at editor time, before moving on to use these object for rigid bodies.
Julio Jerez
Moderator
Moderator
 
Posts: 12478
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: plugin for unity 3D

Postby Julio Jerez » Wed Jun 15, 2016 2:13 pm

ok now I am try to add more interesting thing.
The first thong I found is that it is no trivial to get the dimensions of a game object, I was thinking to read the bounds and use that to first initialize the collision primitive parameter, but the first problem is that primitive do no have mesh, and there are meny different type of meshes, so I guess will have to leave the way it is now.

the second problem I found is how to keep is sync with other components, for example if you scale a gameObject by change its scale in the Transform, we have three options.
1- leave to the user to do I manually (I do not like that)
2-remove the scale form the collision and inherit the scale from the transform, this seem easy but is also problematic
3-inherit the scale for the Transform as if the collision was a child of he transform, this is my favorite way by could get complex with parent child game objects collision shape

I am going for option three, since we have all the support in the matrix for support full scale.
but I do not know if Unity supports it proper general scales. I assume it does.

but even before that I guess the next immediate step is to add Custom property editor, so that we can get feedback when we edit properties in the editor .
Julio Jerez
Moderator
Moderator
 
Posts: 12478
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: plugin for unity 3D

Postby Julio Jerez » Wed Jun 15, 2016 2:56 pm

k here if come th eprobems

I am try to add a custom editor like this, from here
http://unity3d.com/learn/tutorials/topi ... -inspector

Code: Select all
[
using UnityEngine;
using System.Collections;
using UnityEditor;


namespace NewtonPlugin
{
    [CustomEditor(typeof(NewtonCollider))]
    public class LevelScriptEditor : Editor
    {

        //    public class NewtonColliderEditor : Editor
        //    {
        //    }
    }
}
 


but not matter how I write I get these error
:\Users\jjerez\Downloads\NewtonUnityPlugin\NewtonPlugin\NewtonCollider.cs(11,37,11,43): error CS0246: The type or namespace name 'Editor' could not be found (are you missing a using directive or an assembly reference?)
2>C:\Users\jjerez\Downloads\NewtonUnityPlugin\NewtonPlugin\NewtonCollider.cs(10,2,10,14): error CS0246: The type or namespace name 'CustomEditorAttribute' could not be found (are you missing a using directive or an assembly reference?)
2>C:\Users\jjerez\Downloads\NewtonUnityPlugin\NewtonPlugin\NewtonCollider.cs(10,2,10,14): error CS0246: The type or namespace name 'CustomEditor' could not be found (are you missing a using directive or an assembly reference?)
2>C:\Users\jjerez\Downloads\NewtonUnityPlugin\NewtonPlugin\NewtonCollider.cs(10,22,10,34): error CS0432: Alias 'NewtonPlugin' not found


no sure why it does not see the editor base class, could it be that the free version do no allow custom editor inspectors?

some time when I watch those videos I get the impression that they are for a much older version of the engine.
Julio Jerez
Moderator
Moderator
 
Posts: 12478
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: plugin for unity 3D

Postby Julio Jerez » Wed Jun 15, 2016 3:19 pm

just trying to derive from editor gives this error
error CS0246: The type or namespace name 'Editor' could not be found (are you missing a using directive or an assembly reference?)

It seems its missing path, but I do not know how to tell visual studio where to find that class declaration.
Julio Jerez
Moderator
Moderator
 
Posts: 12478
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: plugin for unity 3D

Postby Sweenie » Wed Jun 15, 2016 4:59 pm

I think the editor stuff has it own assembly(dll). You need to add it as a reference. I think it's called UnityEditor.dll
However there might be problems mixing Editor and Engine components in the same managed plugin. When publishing the game Unity won't allow editor stuff, so you might have to create another managed plugin for editor components. There exist compiler directives for this but I don't think they work in an externally created plugin.

EDIT:
Check in the Unity install folder under Unity\Editor\Data\Managed\
There you will find the UnityEditor.dll (copy it over to the project folder and reference it)
Sweenie
 
Posts: 503
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: plugin for unity 3D

Postby Julio Jerez » Thu Jun 16, 2016 10:22 am

boy this is a steep learning curve.
http://unity3d.com/learn/tutorials/topi ... -inspector

according to this video, it seems that custom script must be their own special folder by the name Editor.

my interpretation Is that we crate a separate DLL, and place there, since no other DLL depend on editor script, any project that do no add them will simple no use them, so there should be no conflict if my interpretation is correct.

On that assumption I added a new C# project name NewtonPluginScript and I added the fist script.
It compile find, and copy the dll to the editor folder.
It does not do anything, and I do no know if it even work.

Maybe you can sync and review it, and tell me is my assumption is right before moving on.

Edit:
I just run it and for some reason Unity tell me that I change the project, and simple delete the demo.
is not a big deal, but I did no know that unity keep history of what was in a project.

anyway, the new folder and DLL do show up in the editor, that no to say is working but at least is there. I will recreate the demo and see what happens. if is works I expect that NewtonCollider component to no display anything since public override void OnInspectorGUI() does no do anything.

anyway I hope it words.
Julio Jerez
Moderator
Moderator
 
Posts: 12478
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: plugin for unity 3D

Postby speps » Thu Jun 16, 2016 10:23 am

I'm glad you're working on this Julio! I did a Unity plugin more than a year ago, started by hand. The automated approach is much better :)

For your script error, editor scripts need to be under an "Editor" folder. It doesn't matter where this folder is, as long as it's called Editor and contains your script.

EDIT: you should just start without an external DLL for now, I just tried it on a personal project and it's not ideal when you're prototyping.
speps
 
Posts: 6
Joined: Thu Dec 19, 2013 4:23 pm

Re: plugin for unity 3D

Postby Julio Jerez » Thu Jun 16, 2016 10:34 am

speps wrote:EDIT: you should just start without an external DLL for now, I just tried it on a personal project and it's not ideal when you're prototyping.

what does this mean?


I reload the scene and the dll is there, but is no doing anything, I believe there some else that nee to ne done in the editor do that it gets activated, but I dot no know what.
I am going to what the video again. I remember the dude said some related to that.
Julio Jerez
Moderator
Moderator
 
Posts: 12478
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: plugin for unity 3D

Postby Julio Jerez » Thu Jun 16, 2016 10:40 am

I do not know why is no called, maybe some is still wrong.
I hope that when the say editor script need to be under the Editor folder, that the do no mean the .cs file,
Julio Jerez
Moderator
Moderator
 
Posts: 12478
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: plugin for unity 3D

Postby Julio Jerez » Thu Jun 16, 2016 11:16 am

Eureka it works.

I got two mistakes,
-I was trying to control the NewtonCollider and I have to control NewtonBoxCollider
-name space got me again, I still do no know why C# let you write the name withpout the name space and do no issue an error, but them unity does no recognize the name. I am tenting to remove the name spaces all together, we use Newton as prefix so the name space is not adding any extra clarification

Now I will complete the edit script so that when the Parameter of the object are edited the collision shape get updated to reflect the change.
after a very slow start it start to take off now :D
Julio Jerez
Moderator
Moderator
 
Posts: 12478
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: plugin for unity 3D

Postby Julio Jerez » Thu Jun 16, 2016 1:31 pm

Ok now the collision shape inherit the scale from the Transform of the game object and it own transform.

in the example the transform scale is 50.0, 0.5, 50.0
and the collision shape is 1, 1, 1
the visual shape is (50.0 * 1, 0.5 * 1, 50.0 * 1)

now I will make that when any of the field is edited in the editor, the collision shape reflect that change. Is this goes ok then I will do the same for the transform offset.

the goal is the user can manipulate the collision at will, and a the same time the collision do no have to be resized if the use change the gameobject transform.

I am loving this now :D
if this goes well we can use Unity as the Engine Dev environment to try cool stuff, which is something bee looking for a while.

edit:
that was easy, now if we edit the transform scale field, the collision is updated to reflect the change
there is one error, which is that newton does not accept negative of zero scale, I have to add some clamping, but that should be easy.

There is one more little thing left to figure out, the control when it take over by the custom script, lose some functionality, like refreshing, I assume that the there parameter on the script to force to refresh, but I do not see any example. Not a big problem, but it would be nice to have identical to the normal interface.
Julio Jerez
Moderator
Moderator
 
Posts: 12478
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: plugin for unity 3D

Postby Julio Jerez » Thu Jun 16, 2016 2:44 pm

ah I see in order to refresh the changes the callback has to end by calling
Code: Select all
EditorUtility.SetDirty(target);


that seems unnecessary, but that's how the do it, It seem that the unity UI has a lot of redundancy.
I am sure they has reasons for that, but boy it makes the user spend a great deal of time figuring out stiff.

Well the scale stuff now works the way I was envisioning, I will now complete the position and rotation. and then add few more primitives.
Julio Jerez
Moderator
Moderator
 
Posts: 12478
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

PreviousNext

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron