Very simple mover script pretty laggy in Rhino 6?

Hi,

This script produces a very simple mover/agent/particle that randomly travels within two-dimensional bounds.
I think that it used to work great back in Rhino 5, but seems very laggy in version 6. The mover seems to slow down when approaching the scene centre (?) and the other GH components start to recompute like crazy. There is nothing particular evaluated in this region though.

Here’s the GHPython script:

import Rhino.Geometry as rg
import Grasshopper as gh
import random
import math


class Mover:
    def __init__(self, pos, vel):
        """Inits this mover."""
        self.pos = pos
        self.vel = self.contruct_random_vector3d()
        
    def update(self):
        """Updates this mover."""
        self.check_bounds()
        self.pos += self.vel
        
    def contruct_random_vector3d(self):
        """Returns a random three-dimensional unit vector."""
        vec = rg.Vector3d.YAxis
        angles = [math.radians(a) for a in range(15, 345+1, 30)]
        angle = random.choice(angles)
        vec.Rotate(angle, rg.Vector3d.ZAxis)
        return vec
    
    def check_bounds(self):
        next_pos = self.pos + self.vel
        if next_pos.X > 100 or next_pos.X < -100:
            self.vel *= -1
        if next_pos.Y > 100 or next_pos.Y < -100:
            self.vel *= -1
    
###############################################################################    

def update_component():
    """Updates this component, similar to using a Grasshopper timer."""
    def call_back(e):
        ghenv.Component.ExpireSolution(False)
    # Get this Grasshopper document
    gh_doc = ghenv.Component.OnPingDocument()
    # Schedule this component to expire
    gh_doc.ScheduleSolution(1, gh.Kernel.GH_Document.GH_ScheduleDelegate(call_back))

###############################################################################

if Run:
    mv.update()
    a = mv.pos
    b = mv.vel
    update_component()
else:
    mv = Mover(Position, Velocity)

Is there any issue with the update_component function in Grasshopper 1.x?
Or am I simply overlooking something?

simple_mover.gh (8.0 KB)

Edit:
Changed the category to Scripting. Might be more appropirate for this question.

Does nobody have an idea?

Afraid not, but I imagine this has more to do with all the changes to the rendering pipeline in Rhino 6. In general, I too have been feeling like Rhino 6 sometimes feel more sluggish when opening older Grasshopper files. Especially dynamic ones such as your example (and Kangaroo ones). It might also be related to Rhino now supporting high resolution screen perhaps :man_shrugging:

1 Like

Thanks, Anders. It’s weird how it’s switching from running fluently to sluggishly, mainly when approaching the origin, although the iteration-based processing stays exactly the same.
Could you please test the file on your system and check whether you encounter the same behaviour?

I just did, and though it’s slightly more sluggish in Rhino 6, it’s not to the same degree as what you’re seeing. Could it be the anti-aliasing that occurs when your agent goes into the grid that causes the lag?

3 Likes

Anders, you are a GH god! Thank you!!

When I switch the viewport grid off, the lag disappears nearly entirely! Very weird…

How do you even know about this kind of stuff!? :slight_smile:

I am not knowledgeable in this area, but this might be something that @stevebaer knows more about.
Thanks guys for figuring this out!

1 Like

Video games, I guess :nerd_face:

Which is also where the resolution conjecture comes from (i.e. cranking up resolution has an inherent performance cost).

1 Like

Here’s the definition running on my system. There is no discernible lag from anti-aliasing (I even turn it off at one point), but oddly if I pan (at around 30 seconds in), the movement speeds up:

I assume this has something to do with dynamic redraw settings and using the OpenGl pipeline. If I set the display pipeline to not use OpenGl (i.e. Windows), the movement speed seems to be consistent when panning.

I’ve also noticed that there is no lag, when either the Grid, Grid Axes, or World Axes are turned on. However, the sluggish behaviour returns, when all three are active.
Panning also speeds up the “simulation” for me.

I’m using Rhino for Mac which uses OpenGL by default, with antialiasing set to 4x. There’s no way to turn this off.

Also @dan might be interested to take a look at this! :slight_smile:

2 Likes