Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - mixd

Pages: [1] 2
1
NGUI 3 Support / Re: SpringDampen Performance optimization
« on: July 10, 2013, 02:01:50 PM »
Great -- should be the same as the original.

All I did was take the integral of that loop.

(Probably the first thing I've ever used calculus for in real life)

Also, the implementation is exactly the same for the Vector2 version, but with Vector2 instead of Vector3 I just didn't post it here.

2
NGUI 3 Support / SpringDampen Performance optimization
« on: July 05, 2013, 11:15:25 PM »
Noticed this was starting to eat up some CPU cycles and the current implementation seemed odd. This is roughly 4x faster than the current method (rough benchmark).

Original Way:
  1. static public Vector3 SpringDampen (ref Vector3 velocity, float strength, float deltaTime)
  2.         {
  3.                 // Dampening factor applied each millisecond
  4.                 if (deltaTime > 1f) deltaTime = 1f;
  5.                 float dampeningFactor = 1f - strength * 0.001f;
  6.                 int ms = Mathf.RoundToInt(deltaTime * 1000f);
  7.                 Vector3 offset = Vector3.zero;
  8.  
  9.                 // Apply the offset for each millisecond
  10.                 for (int i = 0; i < ms; ++i)
  11.                 {
  12.                         // Mimic 60 FPS the editor runs at
  13.                         offset += velocity * 0.06f;
  14.                         velocity *= dampeningFactor;
  15.                 }
  16.                 return offset;
  17.         }


Faster Way (Use raw math instead of a loop)
  1. static public Vector3 SpringDampen (ref Vector3 velocity, float strength, float deltaTime)
  2.         {
  3.                 // Dampening factor applied each millisecond
  4.                 if (deltaTime > 1f) deltaTime = 1f;
  5.                 float dampeningFactor = 1f - strength * 0.001f;
  6.                 int ms = Mathf.RoundToInt(deltaTime * 1000f);
  7.  
  8.                 float totalDampening = Mathf.Pow(dampeningFactor, ms);
  9.                 Vector3 vTotal = velocity * ((totalDampening - 1f) / Mathf.Log(dampeningFactor));
  10.                 velocity = velocity * totalDampening;
  11.                
  12.                 return vTotal * .06f;
  13.         }

3
NGUI 3 Support / Re: Animation within a UIPanel and performance
« on: February 21, 2013, 07:23:37 PM »
Hmm. Have you looked into Futile? It's really in a different realm of performance. Yet effectively renders the same way as NGUI. See absolutely no reason why NGUI couldn't also be this fast. Gotta be a core difference somewhere. We are talking many orders of magnitude, especially when there are only a few objects moving on a large mesh.

4
NGUI 3 Support / Re: GDC 2013
« on: February 19, 2013, 02:26:07 PM »
I will be there, but mainly because my office is right next to it

5
NGUI 3 Support / Re: Animation within a UIPanel and performance
« on: February 18, 2013, 03:18:52 PM »
Aren -- was hoping to get your take on this, and wonder if NGUI might someday re-implement its core drawing functions to have performance comparable to something like Futile? Would love to be able to use NGUI as my sole 2D library in all projects! (Like for an RTS or TD game or something)

6
NGUI 3 Support / Re: Animation within a UIPanel and performance
« on: February 18, 2013, 01:50:16 AM »
Try Futile:
https://github.com/MattRix/Futile

Doesn't seem to differ much from NGUI in terms of the core technique they use to render things, so it seems completely possible to free NGUI of this limitation someday.

Check it out -- very cool engine. I'm finding that NGUI really isn't suited for 2D games due to UpdateTransforms, which is unfortunate because it is always nicer to use a single library.

With Futile, I can get 15,000 2D objects (60,000 verts) on the screen all at once, all animating at the same time. Not that you'd need that many objects in a single game view, but point is, it's fast -- just not as convenient for simple UI. (Forgot to mention this is on an iPhone 4s)

7
Nice! Thanks Aren -- always happy to contribute

8
NGUI 3 Support / Re: Draggable Panel "Smooth Drag" Improvement Suggestion
« on: February 15, 2013, 01:39:33 AM »
Great thanks for looking into the bug, let me know if it is a problem with the code I posted.  So far I have not run into any issues on my app.

Also, here is more code (I realize the old UIDraggableCamera script needed an update as well)

UIDraggableCamera.cs, in Press function, add anywhere:
  1. if(!isPressed)
  2.         _dragStarted = false;

and at the start of the Drag function, add:
  1. if(!_dragStarted) {
  2.         _dragStarted = true;
  3.         return;
  4. }

9
Currently while dragging a panel, the drag does not start until the drag threshold is reached. This is fine, however results in a "jump" when the drag starts.

If you observe standard iOS behavior, there is no jump. Anyway here is the code to fix it so dragging panels have no jumpiness. Note that the drag threshold is still preserved as it should be.

In UIDraggablePanel.cs add the following lines:
  1. Vector2 _startOffset;
  2. bool _startOffsetSet = false;

Then in the Drag function, after UICamera.currentTouch.clickNotification = UICamera.ClickNotification.BasedOnDelta; Add the following:
  1. if(!_startOffsetSet)
  2. {
  3.         _startOffset = UICamera.currentTouch.totalDelta;
  4.         _startOffsetSet = true;
  5. }

Then also in the Drag function, update this line and move it below the code you just added (just added - _startOffset to it)
  1. Ray ray = UICamera.currentCamera.ScreenPointToRay(UICamera.currentTouch.pos - _startOffset);

Then in the OnPress function in the "else" block, add the following:
  1. _startOffsetSet = false;


Overall looks like this:
  1. public void Press (bool pressed)
  2.         {
  3.                 if (enabled && NGUITools.GetActive(gameObject))
  4.                 {
  5.                         if (!pressed && mDragID == UICamera.currentTouchID) mDragID = -10;
  6.  
  7.                         mCalculatedBounds = false;
  8.                         mShouldMove = shouldMove;
  9.                         if (!mShouldMove) return;
  10.                         mPressed = pressed;
  11.  
  12.                         if (pressed)
  13.                         {
  14.                                 // Remove all momentum on press
  15.                                 mMomentum = Vector3.zero;
  16.                                 mScroll = 0f;
  17.  
  18.                                 // Disable the spring movement
  19.                                 DisableSpring();
  20.  
  21.                                 // Remember the hit position
  22.                                 mLastPos = UICamera.lastHit.point;
  23.  
  24.                                 // Create the plane to drag along
  25.                                 mPlane = new Plane(mTrans.rotation * Vector3.back, mLastPos);
  26.                         }
  27.                         else
  28.                         {
  29.                                 _startOffsetSet = false;
  30.                                 if (restrictWithinPanel && mPanel.clipping != UIDrawCall.Clipping.None && dragEffect == DragEffect.MomentumAndSpring)
  31.                                 {
  32.                                         RestrictWithinBounds(false);
  33.                                 }
  34.                                 if (onDragFinished != null) onDragFinished();
  35.                         }
  36.                 }
  37.         }
  38.  
  39.         /// <summary>
  40.         /// Drag the object along the plane.
  41.         /// </summary>
  42.  
  43.        
  44.         Vector2 _startOffset;
  45.         bool _startOffsetSet = false;
  46.        
  47.         public void Drag (Vector2 delta)
  48.         {
  49.                 if (enabled && NGUITools.GetActive(gameObject) && mShouldMove)
  50.                 {
  51.                         if (mDragID == -10) mDragID = UICamera.currentTouchID;
  52.                         UICamera.currentTouch.clickNotification = UICamera.ClickNotification.BasedOnDelta;
  53.                        
  54.                         if(!_startOffsetSet)
  55.                         {
  56.                                 _startOffset = UICamera.currentTouch.totalDelta;
  57.                                 _startOffsetSet = true;
  58.                         }
  59.  
  60.                         Ray ray = UICamera.currentCamera.ScreenPointToRay(UICamera.currentTouch.pos - _startOffset);
  61.                         float dist = 0f;
  62.                        

10
Thanks, that did the trick. Am I understanding correctly that the number of messages being sent by NGUI is now doubled if the event also has a non-generic receiver? Not sure if this really matters though.

11
Finally updated to the new version. I get what you did, but it does not really work for a number of reasons:

1. OnPress should never be triggered unless the user actually pressed on something (treating OnPress differently breaks existing UI)
2. Breaks all scrolling lists, try scrolling a list, then running into an outside button - the list abruptly stops scrolling, or vice versa if dragging from an outer button onto a scrolling list
3. Does not solve the functionality of simply knowing when a click is canceled without writing lots of special-case code for an event that should be built in.
4. Does not solve the functionality of knowing when the user has "touched" an object on mobile devices (i.e. sweeping around their finger to collect gold coins) currently this only works if they first pressed down on something, not just arbitrarily moving their finger. Again, seems like pretty basic functionality.

Simply speaking, something like "OnHover" should also work on mobile devices and would solve all of these problems. Not sure why you don't allow hover events on mobile. Sure, there is no mouse pointer, but you could simply treat the users finger as the same thing as the mouse pointer.

Overall NGUI is a great library, and the code is good enough that it's easy to splice these things in there, just wish it was more based around UI standards comparable to UIKit or something. I like your approach of trying to make solutions as integrated as possible, however in this case it didn't really work. If I'm somehow not understanding something, it would be great to know how I am supposed to accomplish the above functionality without editing the library or writing lots of outside code.

12
NGUI 3 Support / Re: OnHover when Mouse Button pressed
« on: November 30, 2012, 12:48:29 AM »
If I understand you correctly I would not advise doing this.  You shouldn't trigger a click event on an object that the user did not originally click.  This could be highly error prone and frustrating for users.

13
Not exactly just for that use case, however it does solve that problem... Here is a simple example:

In the existing NGUI:
1. Click down on a button but do not release the click
2. The button updates to show the user that they clicked on it correctly
3. Without releasing your finger, slide your finger/mouse off the button.
4. Nothing changes.

On a touch screen this is particularly bad, because the user can't see the position of their finger. The user should always know what will happen when they lift their finger, because they may want to "cancel" the click by sliding off, or may become frustrated that their click didn't work after accidentally sliding off a button before lifting their finger.

The OnClickDisabled event allows me to make the button appear in a "deselected" state BEFORE the user lifts their finger. It seems like a subtle edge-case, however it makes a world of difference in how a UI feels. You'll see this behavior in almost all professional UIs, such as windows, mac, iOS.

14
NGUI 3 Support / Re: OnHover when Mouse Button pressed
« on: November 29, 2012, 03:21:22 PM »
If I am understanding what you are asking for... so the user clicks down on a button, then hovers "off" the button, then hovers back "on" the button while it is pressed, and you'd like to know exactly when these events happen.

Instead of "hover" I called it OnClickEnabled and OnClickDisabled:
http://www.tasharen.com/forum/index.php?topic=2416.0

Hope this is what you were looking for.

15
NGUI 3 Support / Re: best way to toggle HUD?
« on: November 29, 2012, 02:55:05 PM »
If you need to switch really fast between the channels, just tween them in/out of view. If not, probably best to deactivate so they aren't sitting in memory.

If they are all sharing the same sprite sheet, there is almost no cost to keeping them active assuming they aren't doing expensive operations every frame.

Pages: [1] 2