mirror of
https://github.com/3b1b/manim.git
synced 2025-07-31 22:13:30 +08:00
Refactor in terms of a ShaderWrapper object
This commit is contained in:
@ -22,7 +22,7 @@ from manimlib.utils.simple_functions import get_parameters
|
|||||||
from manimlib.utils.space_ops import angle_of_vector
|
from manimlib.utils.space_ops import angle_of_vector
|
||||||
from manimlib.utils.space_ops import get_norm
|
from manimlib.utils.space_ops import get_norm
|
||||||
from manimlib.utils.space_ops import rotation_matrix_transpose
|
from manimlib.utils.space_ops import rotation_matrix_transpose
|
||||||
from manimlib.utils.shaders import ShaderWrapper
|
from manimlib.shader_wrapper import ShaderWrapper
|
||||||
|
|
||||||
|
|
||||||
# TODO: Explain array_attrs
|
# TODO: Explain array_attrs
|
||||||
|
@ -44,6 +44,7 @@ class ParametricSurface(Mobject):
|
|||||||
self.uv_func = uv_func
|
self.uv_func = uv_func
|
||||||
self.compute_triangle_indices()
|
self.compute_triangle_indices()
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
|
self.sort_faces_back_to_front()
|
||||||
|
|
||||||
def init_points(self):
|
def init_points(self):
|
||||||
dim = self.dim
|
dim = self.dim
|
||||||
@ -124,7 +125,7 @@ class ParametricSurface(Mobject):
|
|||||||
self.rgbas = interpolate(mobject1.rgbas, mobject2.rgbas, alpha)
|
self.rgbas = interpolate(mobject1.rgbas, mobject2.rgbas, alpha)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def pointwise_become_partial(self, smobject, a, b):
|
def pointwise_become_partial(self, smobject, a, b, axis=1):
|
||||||
assert(isinstance(smobject, ParametricSurface))
|
assert(isinstance(smobject, ParametricSurface))
|
||||||
self.points[:] = smobject.points[:]
|
self.points[:] = smobject.points[:]
|
||||||
if a <= 0 and b >= 1:
|
if a <= 0 and b >= 1:
|
||||||
@ -132,25 +133,38 @@ class ParametricSurface(Mobject):
|
|||||||
|
|
||||||
nu, nv = smobject.resolution
|
nu, nv = smobject.resolution
|
||||||
self.points[:] = np.vstack([
|
self.points[:] = np.vstack([
|
||||||
self.get_partial_points_array(arr, a, b, (nu, nv, 3))
|
self.get_partial_points_array(arr, a, b, (nu, nv, 3), axis=axis)
|
||||||
for arr in self.get_surface_points_and_nudged_points()
|
for arr in self.get_surface_points_and_nudged_points()
|
||||||
])
|
])
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def get_partial_points_array(self, points, a, b, resolution):
|
def get_partial_points_array(self, points, a, b, resolution, axis):
|
||||||
nu, nv = resolution[:2]
|
nu, nv = resolution[:2]
|
||||||
points = points.reshape(resolution)
|
points = points.reshape(resolution)
|
||||||
lower_index, lower_residue = integer_interpolate(0, nv - 1, a)
|
max_index = resolution[axis] - 1
|
||||||
upper_index, upper_residue = integer_interpolate(0, nv - 1, b)
|
lower_index, lower_residue = integer_interpolate(0, max_index, a)
|
||||||
tuples = [
|
upper_index, upper_residue = integer_interpolate(0, max_index, b)
|
||||||
(points[:, :lower_index], lower_index, lower_residue),
|
if axis == 0:
|
||||||
(points[:, upper_index:], upper_index, upper_residue),
|
points[:lower_index] = interpolate(points[lower_index], points[lower_index + 1], lower_residue)
|
||||||
]
|
points[upper_index:] = interpolate(points[upper_index], points[upper_index + 1], upper_residue)
|
||||||
for to_change, index, residue in tuples:
|
else:
|
||||||
col = interpolate(points[:, index], points[:, index + 1], residue)
|
tuples = [
|
||||||
to_change[:] = np.tile(col, to_change.shape[1]).reshape(to_change.shape)
|
(points[:, :lower_index], lower_index, lower_residue),
|
||||||
|
(points[:, upper_index:], upper_index, upper_residue),
|
||||||
|
]
|
||||||
|
for to_change, index, residue in tuples:
|
||||||
|
col = interpolate(points[:, index], points[:, index + 1], residue)
|
||||||
|
to_change[:] = col.reshape((nu, 1, *resolution[2:]))
|
||||||
return points.reshape((nu * nv, *resolution[2:]))
|
return points.reshape((nu * nv, *resolution[2:]))
|
||||||
|
|
||||||
|
def sort_faces_back_to_front(self, vect=OUT):
|
||||||
|
tri_is = self.triangle_indices
|
||||||
|
indices = list(range(len(tri_is) // 3))
|
||||||
|
indices.sort(key=lambda i: np.dot(self.points[tri_is[3 * i]], vect))
|
||||||
|
for k in range(3):
|
||||||
|
tri_is[k::3] = tri_is[k::3][indices]
|
||||||
|
return self
|
||||||
|
|
||||||
# For shaders
|
# For shaders
|
||||||
def get_shader_data(self):
|
def get_shader_data(self):
|
||||||
s_points, du_points, dv_points = self.get_surface_points_and_nudged_points()
|
s_points, du_points, dv_points = self.get_surface_points_and_nudged_points()
|
||||||
@ -241,13 +255,13 @@ class TexturedSurface(ParametricSurface):
|
|||||||
sm.set_opacity(opacity, family)
|
sm.set_opacity(opacity, family)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def pointwise_become_partial(self, tsmobject, a, b):
|
def pointwise_become_partial(self, tsmobject, a, b, axis=1):
|
||||||
super().pointwise_become_partial(tsmobject, a, b)
|
super().pointwise_become_partial(tsmobject, a, b, axis)
|
||||||
self.im_coords[:] = tsmobject.im_coords
|
self.im_coords[:] = tsmobject.im_coords
|
||||||
if a <= 0 and b >= 1:
|
if a <= 0 and b >= 1:
|
||||||
return self
|
return self
|
||||||
nu, nv = tsmobject.resolution
|
nu, nv = tsmobject.resolution
|
||||||
self.im_coords[:] = self.get_partial_points_array(self.im_coords, a, b, (nu, nv, 2))
|
self.im_coords[:] = self.get_partial_points_array(self.im_coords, a, b, (nu, nv, 2), axis)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def get_shader_uniforms(self):
|
def get_shader_uniforms(self):
|
||||||
|
@ -6,7 +6,6 @@ uniform float is_fixed_in_frame;
|
|||||||
in vec3 point;
|
in vec3 point;
|
||||||
in vec3 unit_normal;
|
in vec3 unit_normal;
|
||||||
in vec4 color;
|
in vec4 color;
|
||||||
// in float fill_all; // Either 0 or 1
|
|
||||||
in float vert_index;
|
in float vert_index;
|
||||||
|
|
||||||
out vec3 bp; // Bezier control point
|
out vec3 bp; // Bezier control point
|
||||||
|
Reference in New Issue
Block a user