def _create_points_material(self): """ Define custom raw shader for point cloud to allow to RGBA color format. """ return p3.ShaderMaterial( vertexShader=''' precision highp float; attribute vec4 rgba_color; varying vec3 mypos; varying vec4 vColor; varying vec4 projected; float xDelta, yDelta, zDelta, delta; void main(){ vColor = rgba_color; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); // use the delta between the point position and camera position for size xDelta = (position[0]-cameraPosition[0]) * (position[0]-cameraPosition[0]); yDelta = (position[1]-cameraPosition[1]) * (position[1]-cameraPosition[1]); zDelta = (position[2]-cameraPosition[2]) * (position[2]-cameraPosition[2]); delta = pow(xDelta + yDelta + zDelta, 0.5); gl_PointSize = %f / delta; } ''' % (580.0 * self.pixel_size, ), # the value of 580 is from trial and error fragmentShader=''' precision highp float; varying vec4 vColor; void main() { gl_FragColor = vColor; } ''', vertexColors='VertexColors', transparent=True, depthTest=True)
def loadShaderMaterial(name): uniforms, lights = None, True vs, fs = None, None if (name == 'vector_field'): uniforms = dict( arrowAlignment=dict(value=-1.0), **pythreejs.UniformsLib['lights'], **pythreejs.UniformsLib['common'] ) vs = open('{}/{}.vert'.format(directory, name), 'r').read() fs = open('{}/{}.frag'.format(directory, name), 'r').read() else: raise Exception('Unknown shader : ' + name) mat = pythreejs.ShaderMaterial( uniforms=uniforms, vertexShader=vs, fragmentShader=fs, side='DoubleSide', lights=lights) # Attach an updateUniforms for more easily changing the uniforms of a shader material def updateUniforms(sm, **kwargs): u = dict(**sm.uniforms) u.update({k: dict(value=v) for k, v in kwargs.items()}) sm.uniforms = u sm.needsUpdate = True mat.updateUniforms = MethodType(updateUniforms, mat) return mat
def get_voxelgrid_pythreejs(xyz, colors): vertices = np.array(np.meshgrid([-0.5, 0.5], [-0.5, 0.5], [-0.5, 0.5]), dtype=np.float32).T.reshape(-1, 3) faces = np.array( [ [0, 3, 2], [0, 1, 3], # front [1, 7, 3], [1, 5, 7], # right [5, 6, 7], [5, 4, 6], # back [4, 2, 6], [4, 0, 2], # left [2, 7, 6], [2, 3, 7], # top [4, 1, 0], [4, 5, 1] ], # bottom dtype=np.uint32) colors = pythreejs.InstancedBufferAttribute(array=colors, meshPerAttribute=3) offsets = pythreejs.InstancedBufferAttribute(array=xyz, meshPerAttribute=3) instanced_geometry = pythreejs.InstancedBufferGeometry( attributes={ "position": pythreejs.BufferAttribute(array=vertices), "index": pythreejs.BufferAttribute(array=faces.ravel()), "offset": offsets, "color": colors, }) material = pythreejs.ShaderMaterial(vertexShader=''' precision highp float; attribute vec3 offset; varying vec3 vPosition; varying vec4 vColor; void main(){ vPosition = position + offset; vColor = vec4( color, 1 ); gl_Position = projectionMatrix * modelViewMatrix * vec4( vPosition, 1.0 ); } ''', fragmentShader=''' precision highp float; varying vec4 vColor; void main() { gl_FragColor = vec4( vColor ); } ''', vertexColors='VertexColors', transparent=False) return pythreejs.Mesh(instanced_geometry, material, frustumCulled=False)
def _default_line_material(self): return pythreejs.ShaderMaterial()
def _default_material(self): return pythreejs.ShaderMaterial(side=pythreejs.enums.Side.DoubleSide)
def __init__(self, U, grid, render_size, color_map, title, bounding_box=([0, 0], [1, 1]), codim=2, vmin=None, vmax=None): assert grid.reference_element in (triangle, square) assert grid.dim == 2 assert codim in (0, 2) self.layout = Layout(min_width=str(render_size[0]), min_height=str(render_size[1]), margin='0px 0px 0px 20px ') self.grid = grid self.codim = codim self.vmin, self.vmax = vmin, vmax subentities, coordinates, self.entity_map = flatten_grid(grid) if grid.reference_element == triangle: if codim == 2: vertices = np.zeros((len(coordinates), 3)) vertices[:, :-1] = coordinates indices = subentities else: vertices = np.zeros((len(subentities) * 3, 3)) VERTEX_POS = coordinates[subentities] vertices[:, 0:2] = VERTEX_POS.reshape((-1, 2)) indices = np.arange(len(subentities) * 3, dtype=np.uint32) else: if codim == 2: vertices = np.zeros((len(coordinates), 3)) vertices[:, :-1] = coordinates indices = np.vstack( (subentities[:, 0:3], subentities[:, [0, 2, 3]])) else: num_entities = len(subentities) vertices = np.zeros((num_entities * 6, 3)) VERTEX_POS = coordinates[subentities] vertices[0:num_entities * 3, 0:2] = VERTEX_POS[:, 0:3, :].reshape((-1, 2)) vertices[num_entities * 3:, 0:2] = VERTEX_POS[:, [0, 2, 3], :].reshape((-1, 2)) indices = np.arange(len(subentities) * 6, dtype=np.uint32) max_tex_size = 512 cm = color_map(np.linspace(0, 1, max_tex_size)).astype(np.float32) cm.resize((max_tex_size, 1, 4)) color_map = p3js.DataTexture(cm, format='RGBAFormat', width=max_tex_size, height=1, type='FloatType') uniforms = dict(colormap={'value': color_map, 'type': 'sampler2D'}, ) self.material = p3js.ShaderMaterial( vertexShader=RENDER_VERTEX_SHADER, fragmentShader=RENDER_FRAGMENT_SHADER, uniforms=uniforms, ) self.buffer_vertices = p3js.BufferAttribute(vertices.astype( np.float32), normalized=False) self.buffer_faces = p3js.BufferAttribute(indices.astype( np.uint32).ravel(), normalized=False) self.meshes = [] self._setup_scene(bounding_box, render_size) if config.is_nbconvert(): # need to ensure all data is loaded before cell execution is over class LoadDummy: def done(self): return True self._load_data(U) self.load = LoadDummy() else: self.load = asyncio.ensure_future(self._async_load_data(U)) self._last_idx = None super().__init__(children=[ self.renderer, ])