Exemplo n.º 1
0
def generate_cube_with_effect():
    cube = actor.cube(np.array([[0, 0, 0]]))
    shader_to_actor(cube, "vertex", impl_code=vertex_impl,
                    decl_code=vertex_dec, block="valuepass")
    shader_to_actor(cube, "fragment", impl_code=frag_impl,
                    decl_code=frag_dec, block="light")
    return cube
Exemplo n.º 2
0
    def __init__(self,
                 directions,
                 indices,
                 values=None,
                 affine=None,
                 colors=None,
                 lookup_colormap=None,
                 linewidth=1):
        if affine is not None:
            w_pos = apply_affine(affine, np.asarray(indices).T)

        valid_dirs = directions[indices]

        num_dirs = len(np.nonzero(np.abs(valid_dirs).max(axis=-1) > 0)[0])

        pnts_per_line = 2

        points_array = np.empty((num_dirs * pnts_per_line, 3))
        centers_array = np.empty_like(points_array, dtype=int)
        diffs_array = np.empty_like(points_array)
        line_count = 0
        for idx, center in enumerate(zip(indices[0], indices[1], indices[2])):
            if affine is None:
                xyz = np.asarray(center)
            else:
                xyz = w_pos[idx, :]
            valid_peaks = np.nonzero(
                np.abs(valid_dirs[idx, :, :]).max(axis=-1) > 0.)[0]
            for direction in valid_peaks:
                if values is not None:
                    pv = values[center][direction]
                else:
                    pv = 1.
                point_i = directions[center][direction] * pv + xyz
                point_e = -directions[center][direction] * pv + xyz
                diff = point_e - point_i
                points_array[line_count * pnts_per_line, :] = point_e
                points_array[line_count * pnts_per_line + 1, :] = point_i
                centers_array[line_count * pnts_per_line, :] = center
                centers_array[line_count * pnts_per_line + 1, :] = center
                diffs_array[line_count * pnts_per_line, :] = diff
                diffs_array[line_count * pnts_per_line + 1, :] = diff
                line_count += 1

        vtk_points = numpy_to_vtk_points(points_array)

        vtk_cells = _points_to_vtk_cells(points_array)

        colors_tuple = _peaks_colors_from_points(points_array, colors=colors)
        vtk_colors, colors_are_scalars, self.__global_opacity = colors_tuple

        poly_data = vtk.vtkPolyData()
        poly_data.SetPoints(vtk_points)
        poly_data.SetLines(vtk_cells)
        poly_data.GetPointData().SetScalars(vtk_colors)

        self.__mapper = vtk.vtkPolyDataMapper()
        self.__mapper.SetInputData(poly_data)
        self.__mapper.ScalarVisibilityOn()
        self.__mapper.SetScalarModeToUsePointFieldData()
        self.__mapper.SelectColorArray('colors')
        self.__mapper.Update()

        self.SetMapper(self.__mapper)

        attribute_to_actor(self, centers_array, 'center')
        attribute_to_actor(self, diffs_array, 'diff')

        vs_dec_code = load('peak_dec.vert')
        vs_impl_code = load('peak_impl.vert')
        fs_dec_code = load('peak_dec.frag')
        fs_impl_code = load('peak_impl.frag')

        shader_to_actor(self,
                        'vertex',
                        decl_code=vs_dec_code,
                        impl_code=vs_impl_code)
        shader_to_actor(self, 'fragment', decl_code=fs_dec_code)
        shader_to_actor(self,
                        'fragment',
                        impl_code=fs_impl_code,
                        block='light')

        # Color scale with a lookup table
        if colors_are_scalars:
            if lookup_colormap is None:
                lookup_colormap = colormap_lookup_table()

            self.__mapper.SetLookupTable(lookup_colormap)
            self.__mapper.UseLookupTableScalarRangeOn()
            self.__mapper.Update()

        self.__lw = linewidth
        self.GetProperty().SetLineWidth(self.__lw)

        if self.__global_opacity >= 0:
            self.GetProperty().SetOpacity(self.__global_opacity)

        self.__min_centers = np.min(indices, axis=1)
        self.__max_centers = np.max(indices, axis=1)

        self.__is_range = True
        self.__low_ranges = self.__min_centers
        self.__high_ranges = self.__max_centers
        self.__cross_section = self.__high_ranges // 2

        self.__mapper.AddObserver(vtk.vtkCommand.UpdateShaderEvent,
                                  self.__display_peaks_vtk_callback)
Exemplo n.º 3
0
def manifest_principled(actor,
                        subsurface=0,
                        subsurface_color=[0, 0, 0],
                        metallic=0,
                        specular=0,
                        specular_tint=0,
                        roughness=0,
                        anisotropic=0,
                        anisotropic_direction=[0, 1, .5],
                        sheen=0,
                        sheen_tint=0,
                        clearcoat=0,
                        clearcoat_gloss=0):
    """Apply the Principled Shading properties to the selected actor.

    Parameters
    ----------
    actor : actor
    subsurface : float, optional
        Subsurface scattering computation value. Values must be between 0.0 and
        1.0.
    subsurface_color : list, optional
        Subsurface scattering RGB color where R, G and B should be in the range
        [0, 1].
    metallic : float, optional
        Metallic or non-metallic (dielectric) shading computation value. Values
        must be between 0.0 and 1.0.
    specular : float, optional
        Specular lighting coefficient. Value must be between 0.0 and 1.0.
    specular_tint : float, optional
        Specular tint coefficient value. Values must be between 0.0 and 1.0.
    roughness : float, optional
        Parameter used to specify how glossy the actor should be. Values must
        be between 0.0 and 1.0.
    anisotropic : float, optional
        Anisotropy coefficient. Values must be between 0.0 and 1.0.
    anisotropic_direction : list, optional
        Anisotropy direction where X, Y and Z should be in the range [-1, 1].
    sheen : float, optional
        Sheen coefficient. Values must be between 0.0 and 1.0.
    sheen_tint : float, optional
        Sheen tint coefficient value. Values must be between 0.0 and 1.0.
    clearcoat : float, optional
        Clearcoat coefficient. Values must be between 0.0 and 1.0.
    clearcoat_gloss : float, optional
        Clearcoat gloss coefficient value. Values must be between 0.0 and 1.0.

    Returns
    -------
    principled_params : dict
        Dictionary containing the Principled Shading parameters.

    """

    try:
        prop = actor.GetProperty()

        principled_params = {
            'subsurface': subsurface,
            'subsurface_color': subsurface_color,
            'metallic': metallic,
            'specular': specular,
            'specular_tint': specular_tint,
            'roughness': roughness,
            'anisotropic': anisotropic,
            'anisotropic_direction': anisotropic_direction,
            'sheen': sheen,
            'sheen_tint': sheen_tint,
            'clearcoat': clearcoat,
            'clearcoat_gloss': clearcoat_gloss
        }

        prop.SetSpecular(specular)

        @calldata_type(VTK_OBJECT)
        def uniforms_callback(_caller, _event, calldata=None):
            if calldata is not None:
                calldata.SetUniformf('subsurface',
                                     principled_params['subsurface'])
                calldata.SetUniformf('metallic', principled_params['metallic'])
                calldata.SetUniformf('specularTint',
                                     principled_params['specular_tint'])
                calldata.SetUniformf('roughness',
                                     principled_params['roughness'])
                calldata.SetUniformf('anisotropic',
                                     principled_params['anisotropic'])
                calldata.SetUniformf('sheen', principled_params['sheen'])
                calldata.SetUniformf('sheenTint',
                                     principled_params['sheen_tint'])
                calldata.SetUniformf('clearcoat',
                                     principled_params['clearcoat'])
                calldata.SetUniformf('clearcoatGloss',
                                     principled_params['clearcoat_gloss'])

                calldata.SetUniform3f('subsurfaceColor',
                                      principled_params['subsurface_color'])
                calldata.SetUniform3f(
                    'anisotropicDirection',
                    principled_params['anisotropic_direction'])

        add_shader_callback(actor, uniforms_callback)

        fs_dec_code = import_fury_shader('bxdf_dec.frag')
        fs_impl_code = import_fury_shader('bxdf_impl.frag')

        shader_to_actor(actor, 'fragment', decl_code=fs_dec_code)
        shader_to_actor(actor,
                        'fragment',
                        impl_code=fs_impl_code,
                        block='light')
        return principled_params
    except AttributeError:
        warnings.warn('Actor does not have the attribute property. This '
                      'material will not be applied.')
        return None
Exemplo n.º 4
0
fragment_shader_code_decl = \
    """
    uniform float time;
    varying vec4 myVertexVC;
    """

fragment_shader_code_impl = \
    """
    vec2 iResolution = vec2(1024,720);
    vec2 uv = myVertexVC.xy/iResolution;
    vec3 col = 0.5 + 0.5 * cos((time/30) + uv.xyx + vec3(0, 2, 4));
    fragOutput0 = vec4(col, fragOutput0.a);
    """

shader_to_actor(utah,
                "vertex",
                impl_code=vertex_shader_code_impl,
                decl_code=vertex_shader_code_decl)
shader_to_actor(utah, "fragment", decl_code=fragment_shader_code_decl)
shader_to_actor(utah,
                "fragment",
                impl_code=fragment_shader_code_impl,
                block="light")

###############################################################################
# Let's create a scene.

scene = window.Scene()

global timer
timer = 0