def _approx_derivative(easing_name, t, easing_args, offsets): delta = 1e-10 t0 = max(0, t - delta) t1 = min(1, t + delta) y0 = ngl.easing_evaluate(easing_name, t0, easing_args, offsets) y1 = ngl.easing_evaluate(easing_name, t1, easing_args, offsets) return (y1 - y0) / (t1 - t0)
def anim_forward_api(nb_points=7): scale = 1.0 / float(nb_points) ret = [] times = [i * scale for i in range(nb_points + 1)] for easing in _easing_list: easing_name, easing_args = _easing_split(easing) for offsets in _offsets: values = [ngl.easing_evaluate(easing_name, t, easing_args, offsets) for t in times] ret.append((easing_name, values)) return ret
def _get_easing_node(cfg, easing, curve_zoom, color_program, nb_points=128): text_vratio = 1 / 8. graph_hpad_ratio = 1 / 16. area_size = 2.0 width, height = area_size, area_size text_height = text_vratio * height pad_height = graph_hpad_ratio * height # Colors hue = random.uniform(0, 0.6) color = list(colorsys.hls_to_rgb(hue, 0.6, 1.0)) + [1] ucolor = ngl.UniformVec4(value=color) graph_bg_ucolor = ngl.UniformVec4(value=(.15, .15, .15, 1)) normed_graph_bg_ucolor = ngl.UniformVec4(value=(0, 0, 0, 1)) line_ucolor = ngl.UniformVec4(value=(1, 1, 1, .4)) # Text legend text = ngl.Text(text=easing, fg_color=color, padding=3, bg_color=(0, 0, 0, 1), box_corner=(-width / 2., height / 2. - text_height, 0), box_width=(width, 0, 0), box_height=(0, text_height, 0), label='%s legend' % easing) # Graph drawing area (where the curve may overflow) graph_size = area_size - text_height - pad_height * 2 graph_block = _block(graph_size, graph_size, color_program, corner=(-graph_size / 2, -(graph_size + text_height) / 2, 0), color=graph_bg_ucolor) # Normed area of the graph normed_graph_size = graph_size * curve_zoom normed_graph_block = _block(normed_graph_size, normed_graph_size, color_program, corner=(-normed_graph_size / 2, -(normed_graph_size + text_height) / 2, 0), color=normed_graph_bg_ucolor) # Curve easing_name, easing_args = _easing_split(easing) curve_scale_factor = graph_size / area_size * curve_zoom vertices_data = array.array('f') for i in range(nb_points + 1): t = i / float(nb_points) v = ngl.easing_evaluate(easing_name, t, easing_args) x = curve_scale_factor * (t * width - width / 2.) y = curve_scale_factor * (v * height - height / 2.) y -= text_height / 2. vertices_data.extend([x, y, 0]) vertices = ngl.BufferVec3(data=vertices_data) geometry = ngl.Geometry(vertices, topology='line_strip') curve = ngl.Render(geometry, color_program, label='%s curve' % easing) curve.update_uniforms(color=ucolor) # Value cursor y = 2 / 3. * pad_height x = y * math.sqrt(3) cursor_geometry = ngl.Triangle((-x, y, 0), (0, 0, 0), (-x, -y, 0)) cursor = ngl.Render(cursor_geometry, color_program, label='%s cursor' % easing) cursor.update_uniforms(color=ucolor) # Horizontal value line hline_data = array.array('f', (0, 0, 0, graph_size, 0, 0)) hline_vertices = ngl.BufferVec3(data=hline_data) hline_geometry = ngl.Geometry(hline_vertices, topology='line_strip') hline = ngl.Render(hline_geometry, color_program, label='%s value line' % easing) hline.update_uniforms(color=line_ucolor) # Value animation (cursor + h-line) value_x = -graph_size / 2. value_y = (-text_height - normed_graph_size) / 2. value_animkf = ( ngl.AnimKeyFrameVec3(0, (value_x, value_y, 0)), ngl.AnimKeyFrameVec3(cfg.duration, (value_x, value_y + normed_graph_size, 0), easing_name, easing_args), ) value_anim = ngl.Group(children=(hline, cursor)) value_anim = ngl.Translate(value_anim, anim=ngl.AnimatedVec3(value_animkf), label='%s value anim' % easing) # Vertical time line vline_data = array.array('f', (0, 0, 0, 0, graph_size, 0)) vline_vertices = ngl.BufferVec3(data=vline_data) vline_geometry = ngl.Geometry(vline_vertices, topology='line_strip') vline = ngl.Render(vline_geometry, color_program, label='%s time line' % easing) vline.update_uniforms(color=line_ucolor) # Time animation (v-line only) time_x = -normed_graph_size / 2. time_y = (-text_height - graph_size) / 2. time_animkf = (ngl.AnimKeyFrameVec3(0, (time_x, time_y, 0)), ngl.AnimKeyFrameVec3( cfg.duration, (time_x + normed_graph_size, time_y, 0))) time_anim = ngl.Translate(vline, anim=ngl.AnimatedVec3(time_animkf), label='%s time anim' % easing) group = ngl.Group(label='%s block' % easing) group.add_children(text, graph_block, normed_graph_block, curve, value_anim, time_anim) return group