def draw(): shader.bind() shader.uniform_float("outline_width", scene.custom_outline_width) shader.uniform_float("color", scene.custom_outline_color) shader.uniform_float("perspective_matrix", bpy.context.region_data.perspective_matrix) shader.uniform_float("matrix_world", bpy.context.active_object.matrix_world) bgl.glEnable(bgl.GL_CULL_FACE) bgl.glCullFace(bgl.GL_FRONT) bgl.glEnable(bgl.GL_DEPTH_TEST) batch.draw(shader) bgl.glDisable(bgl.GL_CULL_FACE) bgl.glCullFace(bgl.GL_BACK) bgl.glDisable(bgl.GL_DEPTH_TEST)
def glsl_draw(self): if GlslDrawObj.myinstance is None and GlslDrawObj.draw_func is None: glsl_draw_obj = GlslDrawObj() glsl_draw_obj.build_scene() else: glsl_draw_obj = GlslDrawObj.myinstance model_offset = Matrix.Translation((glsl_draw_obj.draw_x_offset, 0, 0)) light_pos = [ i + n for i, n in zip(glsl_draw_obj.light.location, [-glsl_draw_obj.draw_x_offset, 0, 0]) ] batches = glsl_draw_obj.batches depth_shader = glsl_draw_obj.depth_shader toon_shader = glsl_draw_obj.toon_shader offscreen = glsl_draw_obj.offscreen # need bone etc changed only update depth_matrix = None light = glsl_draw_obj.light light_lookat = light.rotation_euler.to_quaternion() @ Vector( (0, 0, -1)) # TODO このへん tar = light_lookat.normalized() up = light.rotation_euler.to_quaternion() @ Vector((0, 1, 0)) tmp_bound_len = Vector(glsl_draw_obj.bounding_center).length camera_bias = 0.2 loc = Vector([ glsl_draw_obj.bounding_center[i] + tar[i] * (tmp_bound_len + camera_bias) for i in range(3) ]) loc = model_offset @ loc v_matrix = lookat_cross(loc, tar, up) const_proj = 2 * max(glsl_draw_obj.bounding_size) / 2 p_matrix = ortho_proj_mat(-const_proj, const_proj, -const_proj, const_proj, -const_proj, const_proj) depth_matrix = v_matrix @ p_matrix # reuse in main shader depth_matrix.transpose() # region shader depth path with offscreen.bind(): bgl.glClearColor(10, 10, 10, 1) bgl.glClear(bgl.GL_COLOR_BUFFER_BIT | bgl.GL_DEPTH_BUFFER_BIT) for bat in batches: mat = bat[0] mat.update() depth_bat = bat[2] depth_shader.bind() bgl.glEnable(bgl.GL_BLEND) if mat.alpha_method == "TRANSPARENT": bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) bgl.glDepthMask(bgl.GL_TRUE) bgl.glEnable(bgl.GL_DEPTH_TEST) elif mat.alpha_method == "OPAQUE": bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ZERO) bgl.glDepthMask(bgl.GL_TRUE) bgl.glEnable(bgl.GL_DEPTH_TEST) elif mat.alpha_method == "CLIP": bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ZERO) bgl.glDepthMask(bgl.GL_TRUE) bgl.glEnable(bgl.GL_DEPTH_TEST) if mat.cull_mode == "BACK": bgl.glEnable(bgl.GL_CULL_FACE) bgl.glCullFace(bgl.GL_BACK) else: bgl.glDisable(bgl.GL_CULL_FACE) bgl.glEnable(bgl.GL_CULL_FACE) # そも輪郭線がの影は落ちる? bgl.glCullFace(bgl.GL_BACK) depth_shader.uniform_float("obj_matrix", model_offset) # obj.matrix_world) depth_shader.uniform_float("depthMVP", depth_matrix) depth_bat.draw(depth_shader) # endregion shader depth path # region shader main vp_mat = bpy.context.region_data.perspective_matrix projection_mat = bpy.context.region_data.window_matrix view_dir = bpy.context.region_data.view_matrix[2][:3] view_up = bpy.context.region_data.view_matrix[1][:3] normal_world_to_view_matrix = ( bpy.context.region_data.view_matrix.inverted_safe().transposed()) aspect = bpy.context.area.width / bpy.context.area.height for is_outline in [0, 1]: for bat in batches: toon_bat = bat[1] toon_shader.bind() mat = bat[0] if is_outline == 1 and mat.float_dic["OutlineWidthMode"] == 0: continue # mat.update() #already in depth path bgl.glEnable(bgl.GL_BLEND) bgl.glDepthMask(bgl.GL_TRUE) bgl.glEnable(bgl.GL_DEPTH_TEST) if mat.alpha_method == "TRANSPARENT": bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) elif mat.alpha_method == "OPAQUE": bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ZERO) elif mat.alpha_method == "CLIP": bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ZERO) if is_outline == 0: if mat.cull_mode == "BACK": bgl.glEnable(bgl.GL_CULL_FACE) bgl.glCullFace(bgl.GL_BACK) else: bgl.glDisable(bgl.GL_CULL_FACE) else: bgl.glEnable(bgl.GL_CULL_FACE) bgl.glCullFace(bgl.GL_BACK) toon_shader.uniform_float("obj_matrix", model_offset) # obj.matrix_world) toon_shader.uniform_float("projectionMatrix", projection_mat) toon_shader.uniform_float("viewProjectionMatrix", vp_mat) toon_shader.uniform_float("viewDirection", view_dir) toon_shader.uniform_float("viewUpDirection", view_up) toon_shader.uniform_float("normalWorldToViewMatrix", normal_world_to_view_matrix) toon_shader.uniform_float("depthMVP", depth_matrix) toon_shader.uniform_float("lightpos", light_pos) toon_shader.uniform_float("aspect", aspect) toon_shader.uniform_float("is_outline", is_outline) toon_shader.uniform_float("isDebug", 0.0) toon_shader.uniform_float( "is_cutout", 1.0 if mat.alpha_method == "CLIP" else 0.0) float_keys = [ "CutoffRate", "BumpScale", "ReceiveShadowRate", "ShadeShift", "ShadeToony", "RimLightingMix", "RimFresnelPower", "RimLift", "ShadingGradeRate", "LightColorAttenuation", "IndirectLightIntensity", "OutlineWidth", "OutlineScaleMaxDistance", "OutlineLightingMix", "UV_Scroll_X", "UV_Scroll_Y", "UV_Scroll_Rotation", "OutlineWidthMode", "OutlineColorMode", ] for k in float_keys: toon_shader.uniform_float(k, mat.float_dic[k]) for k, v in mat.vector_dic.items(): toon_shader.uniform_float(k, v) bgl.glActiveTexture(bgl.GL_TEXTURE0) bgl.glBindTexture(bgl.GL_TEXTURE_2D, offscreen.color_texture) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_S, bgl.GL_CLAMP_TO_EDGE) # TODO bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_T, bgl.GL_CLAMP_TO_EDGE) toon_shader.uniform_int("depth_image", 0) for i, k in enumerate(mat.texture_dic.keys()): bgl.glActiveTexture(bgl.GL_TEXTURE1 + i) texture = mat.texture_dic[k] bgl.glBindTexture(bgl.GL_TEXTURE_2D, texture.bindcode) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_S, bgl.GL_CLAMP_TO_EDGE) # TODO bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_T, bgl.GL_CLAMP_TO_EDGE) toon_shader.uniform_int(k, 1 + i) toon_bat.draw(toon_shader)
own['deltaori'] = own['oldori']-ori own['deltapos'] = own['oldpos']-pos own['deltaori'] = own['oldori']-ori own['deltapos'] = own['oldpos']-pos #orienting and positioning the reflection rendercamera rendercamera.orientation = ori - own['deltaori'] rendercamera.position = pos - own['deltapos'] #storing the old orientation and position of the camera own['oldori'] = ori own['oldpos'] = pos #culling front faces as the camera is scaled to -1 bgl.glCullFace(bgl.GL_FRONT) #plane equation normal = own.getAxisVect((0.0, 0.0, 1.0)) #plane normals Z=front D = -own.position.project(normal).magnitude #closest distance from center to plane V = (activecam.position-own.position).normalized().dot(normal) #VdotN to get frontface/backface #invert normals when backface if V<0: normal = -normal #making a clipping plane buffer plane = bgl.Buffer(bgl.GL_DOUBLE, [4], [-normal[0], -normal[1], -normal[2], -D+offset]) bgl.glClipPlane(bgl.GL_CLIP_PLANE0,plane)
def draw_stuff(): # print("draw_stuff") # FIXME this should use glPushAttrib from bgl import glColor3f, glVertex3f, glBegin, glEnd, GL_POLYGON, GL_LINES global draw_stuff_dirty, draw_stuff_objects ctx = bpy.context if not len(ctx.selected_objects) and not ctx.object: return if not bpy.context.window_manager.thug_show_face_collision_colors: return VERT_FLAG = FACE_FLAGS["mFD_VERT"] WALLRIDABLE_FLAG = FACE_FLAGS["mFD_WALL_RIDABLE"] TRIGGER_FLAG = FACE_FLAGS["mFD_TRIGGER"] NON_COLLIDABLE_FLAG = FACE_FLAGS["mFD_NON_COLLIDABLE"] bgl.glPushAttrib(bgl.GL_ALL_ATTRIB_BITS) try: _tmp_buf = bgl.Buffer(bgl.GL_FLOAT, 1) bgl.glGetFloatv(bgl.GL_POLYGON_OFFSET_FACTOR, _tmp_buf) old_offset_factor = _tmp_buf[0] bgl.glGetFloatv(bgl.GL_POLYGON_OFFSET_UNITS, _tmp_buf) old_offset_units = _tmp_buf[0] del _tmp_buf objects = set([ob.name for ob in ctx.selected_objects] if ctx.mode == "OBJECT" else [ctx.object.name]) if draw_stuff_objects != objects: draw_stuff_dirty = True # print("draw_stuff2") if not draw_stuff_dirty: bgl.glCallList(draw_stuff_display_list_id) bgl.glPolygonOffset(old_offset_factor, old_offset_units) bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glDisable(bgl.GL_CULL_FACE) return bm = None bgl.glNewList(draw_stuff_display_list_id, bgl.GL_COMPILE_AND_EXECUTE) try: bgl.glCullFace(bgl.GL_BACK) bgl.glEnable(bgl.GL_CULL_FACE) bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) #bgl.glEnable(bgl.GL_POLYGON_OFFSET_LINE) bgl.glPolygonOffset(-2, -2) bgl.glEnable(bgl.GL_BLEND) bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA); prefs = ctx.user_preferences.addons[ADDON_NAME].preferences bgl.glLineWidth(prefs.line_width) draw_stuff_objects = objects bdobs = {ob.name: ob for ob in bpy.data.objects} for ob in objects: ob = bdobs[ob] if not bm: bm = bmesh.new() if (ob and ob.type == "CURVE" and ob.data.splines and ob.data.splines[-1].points and ob.thug_path_type == "Rail" and ob.thug_rail_connects_to): connects_to = bdobs[ob.thug_rail_connects_to] if (connects_to and connects_to.type == "CURVE" and connects_to.data.splines and connects_to.data.splines[0].points): glBegin(GL_LINES) bgl.glColor4f(*prefs.rail_end_connection_color) v = ob.matrix_world * ob.data.splines[-1].points[-1].co.to_3d() glVertex3f(v[0], v[1], v[2]) v = connects_to.matrix_world * connects_to.data.splines[0].points[0].co.to_3d() glVertex3f(v[0], v[1], v[2]) glEnd() # Draw previews for area lights - tube/sphere lights and area lights if (ob and ob.type == 'LAMP'): if ob.data.thug_light_props.light_type == 'TUBE': if ob.data.thug_light_props.light_end_pos != (0, 0, 0): glBegin(GL_LINES) bgl.glColor4f(1.0, 0.75, 0.25, 1.0) glVertex3f(ob.location[0], ob.location[1], ob.location[2]) glVertex3f(ob.location[0] + ob.data.thug_light_props.light_end_pos[0], ob.location[1] + ob.data.thug_light_props.light_end_pos[1], ob.location[2] + ob.data.thug_light_props.light_end_pos[2]) glEnd() continue elif ob.data.thug_light_props.light_type == 'SPHERE': continue elif ob.data.thug_light_props.light_type == 'AREA': continue else: continue elif (ob and ob.type == 'EMPTY'): if ob.thug_empty_props.empty_type == 'LightVolume' or ob.thug_empty_props.empty_type == 'CubemapProbe': # Draw light volume bbox! bbox, bbox_min, bbox_max, bbox_mid = get_bbox_from_node(ob) # 50% alpha, 2 pixel width line bgl.glEnable(bgl.GL_BLEND) bgl.glColor4f(1.0, 0.0, 0.0, 0.5) bgl.glLineWidth(4) glBegin(bgl.GL_LINE_STRIP) bgl.glVertex3f(*bbox[0]) bgl.glVertex3f(*bbox[1]) bgl.glVertex3f(*bbox[2]) bgl.glVertex3f(*bbox[3]) bgl.glVertex3f(*bbox[0]) bgl.glVertex3f(*bbox[4]) bgl.glVertex3f(*bbox[5]) bgl.glVertex3f(*bbox[6]) bgl.glVertex3f(*bbox[7]) bgl.glVertex3f(*bbox[4]) bgl.glEnd() bgl.glBegin(bgl.GL_LINES) bgl.glVertex3f(*bbox[1]) bgl.glVertex3f(*bbox[5]) bgl.glVertex3f(*bbox[2]) bgl.glVertex3f(*bbox[6]) bgl.glVertex3f(*bbox[3]) bgl.glVertex3f(*bbox[7]) glEnd() if not ob or ob.type != "MESH": continue if ob.mode == "EDIT": bm.free() bm = bmesh.from_edit_mesh(ob.data).copy() else: bm.clear() if ob.modifiers: final_mesh = ob.to_mesh(bpy.context.scene, True, "PREVIEW") try: bm.from_mesh(final_mesh) finally: bpy.data.meshes.remove(final_mesh) else: bm.from_mesh(ob.data) arl = bm.edges.layers.int.get("thug_autorail") if arl: bgl.glColor4f(*prefs.autorail_edge_color) glBegin(GL_LINES) for edge in bm.edges: if edge[arl] == AUTORAIL_NONE: continue for vert in edge.verts: v = ob.matrix_world * vert.co glVertex3f(v[0], v[1], v[2]) glEnd() cfl = bm.faces.layers.int.get("collision_flags") flag_stuff = ((VERT_FLAG, prefs.vert_face_color), (WALLRIDABLE_FLAG, prefs.wallridable_face_color), (TRIGGER_FLAG, prefs.trigger_face_color), (NON_COLLIDABLE_FLAG, prefs.non_collidable_face_color)) if cfl: bmesh.ops.triangulate(bm, faces=bm.faces) for face in bm.faces: drawn_face = False if prefs.show_bad_face_colors: if (face[cfl] & (VERT_FLAG | WALLRIDABLE_FLAG | NON_COLLIDABLE_FLAG) not in (VERT_FLAG, WALLRIDABLE_FLAG, NON_COLLIDABLE_FLAG, 0)): bgl.glColor4f(*prefs.bad_face_color) glBegin(GL_POLYGON) for vert in face.verts: v = ob.matrix_world * vert.co glVertex3f(v[0], v[1], v[2]) glEnd() continue for face_flag, face_color in flag_stuff: if face[cfl] & face_flag and (not drawn_face or prefs.mix_face_colors): bgl.glColor4f(*face_color) drawn_face = True glBegin(GL_POLYGON) for vert in face.verts: v = ob.matrix_world * vert.co glVertex3f(v[0], v[1], v[2]) glEnd() finally: draw_stuff_dirty = False bgl.glEndList() if bm: bm.free() bgl.glPolygonOffset(old_offset_factor, old_offset_units) bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL) finally: bgl.glPopAttrib()
def PS_draw_bgl(self, context): if context.mode == 'EDIT_MESH': # context.active_object != None and context.active_object.select_get() and #start_time = time.time() props = context.preferences.addons[__package__].preferences settings = context.scene.ps_set_ theme = context.preferences.themes['Default'] vertex_size = theme.view_3d.vertex_size # Color VA_Col = props.v_alone_color[0], props.v_alone_color[ 1], props.v_alone_color[2], props.v_alone_color[3] VE_Col = props.VE_color[0], props.VE_color[1], props.VE_color[ 2], props.VE_color[3] F_Col = props.F_color[0], props.F_color[1], props.F_color[ 2], props.opacity sel_Col = props.select_color[0], props.select_color[ 1], props.select_color[2], 1.0 bgl.glEnable(bgl.GL_BLEND) bgl.glLineWidth(props.edge_width) bgl.glPointSize(vertex_size + props.verts_size) bgl.glCullFace(bgl.GL_BACK) if props.xray_ret == False: bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glEnable(bgl.GL_CULL_FACE) if props.line_smooth: bgl.glEnable(bgl.GL_LINE_SMOOTH) #bgl.glDepthRange(0, 0.99999) #bgl.glDepthFunc(600) bgl.glDepthMask(False) is_perspective = context.region_data.is_perspective if is_perspective: z_bias = props.z_bias / 350 else: z_bias = 1.0 tool_retopo = active_tool().idname in tools_ret # Retopology Tools if tool_retopo: shader = shader_uni else: shader = shader_sm shader.bind() view_mat = context.region_data.perspective_matrix shader.uniform_float("view_mat", view_mat) shader.uniform_float("Z_Bias", z_bias) shader.uniform_float("Z_Offset", props.z_offset) if props.use_mod_ret: depsgraph = context.evaluated_depsgraph_get() uniques = context.objects_in_mode_unique_data #uniques = context.selected_objects #uniques = context.objects_in_mode for obj in uniques: if props.use_mod_ret: if len(obj.modifiers) > 0: depsgraph.update() ob_eval = obj.evaluated_get(depsgraph) me = ob_eval.to_mesh() bm = bmesh.new() bm.from_mesh(me, face_normals=True, use_shape_key=False) bm.verts.ensure_lookup_table() bm.edges.ensure_lookup_table() bm.faces.ensure_lookup_table() else: bm = bmesh.from_edit_mesh(obj.data) if len(bm.verts) <= props.maxP_retop: # Если выбран инструмент ретопологии if tool_retopo: # все вертексы vCo = [obj.matrix_world @ v.co for v in bm.verts] vNm = [v.normal for v in bm.verts] # --- FACES if settings.draw_faces: loop_triangles = bm.calc_loop_triangles() faces_indices = [[ loop.vert.index for loop in looptris ] for looptris in loop_triangles] FACES = batch_for_shader(shader, 'TRIS', { "pos": vCo, 'nrm': vNm }, indices=faces_indices) shader.uniform_float("color", F_Col) FACES.draw(shader) # --- EDGES if settings.draw_edges: #edges_indices = [ [e.verts[0].index, e.verts[1].index] for e in bm.edges] edges_ind = [e.index for e in bm.edges] edges_cord = [ obj.matrix_world @ v.co for i in edges_ind for v in bm.edges[i].verts ] eNm = [ v.normal for i in edges_ind for v in bm.edges[i].verts ] EDGES = batch_for_shader(shader, 'LINES', { "pos": edges_cord, 'nrm': eNm }) shader.uniform_float("color", VE_Col) EDGES.draw(shader) # --- VERTS # только одиночные вертексы if settings.draw_verts: vCo_one = [ obj.matrix_world @ v.co for v in bm.verts if len(v.link_faces) < 1 ] #not v.is_manifold] (not v.is_manifold and v.is_wire) vCo_one_Nm = [ v.normal for v in bm.verts if len(v.link_faces) < 1 ] VERTS = batch_for_shader(shader, 'POINTS', { "pos": vCo_one, 'nrm': vCo_one_Nm }) shader.uniform_float("color", VA_Col) VERTS.draw(shader) # Если выбраны обычные инструменты else: # --- FACES vCo = [obj.matrix_world @ v.co for v in bm.verts] vNm = [v.normal for v in bm.verts] v_len = len(vCo) if settings.draw_faces: loop_triangles = bm.calc_loop_triangles() faces_indices = [[ loop.vert.index for loop in looptris ] for looptris in loop_triangles] face_col = [F_Col for i in range(v_len)] FACES = batch_for_shader(shader, 'TRIS', { "pos": vCo, "col": face_col, 'nrm': vNm }, indices=faces_indices) FACES.draw(shader) # --- EDGES if settings.draw_edges: edges_ind = [e.index for e in bm.edges] edges_cord = [ obj.matrix_world @ v.co for i in edges_ind for v in bm.edges[i].verts ] eNm = [ v.normal for i in edges_ind for v in bm.edges[i].verts ] edge_col = [VE_Col for i in range(len(edges_cord))] for i, edge in enumerate( bm.edges): # Окрашивание выделенных элементов if edge.select: ind = i * 2 ind2 = ind + 1 edge_col[ind] = sel_Col edge_col[ind2] = sel_Col #edges_indices = [ [e.verts[0].index, e.verts[1].index] for e in bm.edges] EDGES = batch_for_shader(shader, 'LINES', { "pos": edges_cord, "col": edge_col, 'nrm': eNm }) # , indices=edges_indices EDGES.draw(shader) # --- VERTS if settings.draw_verts: vert_col = [VE_Col for i in range(v_len)] for i, vert in enumerate( bm.verts): # Окрашивание выделенных элементов if len(vert.link_faces) < 1: vert_col[i] = VA_Col if vert.select: #face_col[i] = select_color_f vert_col[i] = sel_Col #edge_col[i] = sel_Col VERTS = batch_for_shader(shader, 'POINTS', { "pos": vCo, "col": vert_col, 'nrm': vNm }) if context.tool_settings.mesh_select_mode[0]: VERTS.draw(shader) if props.use_mod_ret: bm.free() """ if props.line_smooth: bgl.glDisable(bgl.GL_LINE_SMOOTH) """ """ bgl.glDisable(bgl.GL_DEPTH_TEST)
def check_draw_bgl(self, context): objs = context.selected_objects if len(objs) > 0: props = context.preferences.addons[__package__].preferences theme = context.preferences.themes['Default'] vertex_size = theme.view_3d.vertex_size bgl.glEnable(bgl.GL_BLEND) bgl.glLineWidth(props.edge_width + 1) bgl.glPointSize(vertex_size + 5) bgl.glCullFace(bgl.GL_BACK) if props.xray_che == False: bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glEnable(bgl.GL_CULL_FACE) if props.line_smooth: bgl.glEnable(bgl.GL_LINE_SMOOTH) bgl.glDepthRange(0, 0.9999) #bgl.glDepthFunc(600) bgl.glDepthMask(False) shader.bind() # COLOR #opacity_second = props.opacity + 0.1 ngone_col = props.ngone_col[0], props.ngone_col[1], props.ngone_col[ 2], props.ngone_col[3] tris_col = props.tris_col[0], props.tris_col[1], props.tris_col[ 2], props.tris_col[3] e_non_col = props.non_manifold_color[0], props.non_manifold_color[ 1], props.non_manifold_color[2], props.non_manifold_color[3] e_pole_col = props.e_pole_col[0], props.e_pole_col[ 1], props.e_pole_col[2], props.e_pole_col[3] n_pole_col = props.n_pole_col[0], props.n_pole_col[ 1], props.n_pole_col[2], props.n_pole_col[3] f_pole_col = props.f_pole_col[0], props.f_pole_col[ 1], props.f_pole_col[2], props.f_pole_col[3] v_bound_col = props.bound_col[0], props.bound_col[1], props.bound_col[ 2], props.bound_col[3] v_alone_col = props.v_alone_color[0], props.v_alone_color[ 1], props.v_alone_color[2], props.v_alone_color[3] custom_col = props.custom_col[0], props.custom_col[ 1], props.custom_col[2], props.custom_col[3] if props.use_mod_che: depsgraph = context.evaluated_depsgraph_get() for obj in objs: if obj.type == 'MESH': me = obj.data if len(me.polygons) < 50000: if context.mode == 'EDIT_MESH' and props.use_mod_che == False: bm = bmesh.from_edit_mesh(obj.data) else: if props.use_mod_che: if len(obj.modifiers) > 0: depsgraph.update() ob_eval = obj.evaluated_get(depsgraph) me = ob_eval.to_mesh() bm = bmesh.new() bm.from_mesh(me, face_normals=True, use_shape_key=False) bm.verts.ensure_lookup_table() bm.edges.ensure_lookup_table() bm.faces.ensure_lookup_table() # --- N-Gone if props.ngone: ngone = [] for n in bm.faces: if len(n.verts) > 4: ngone.append(n.index) #print("ngone",ngone) copy = bm.copy() copy.faces.ensure_lookup_table() edge_n = [ e for i in ngone for e in copy.faces[i].edges ] for e in copy.edges: if not e in edge_n: e.hide_set(True) bmesh.ops.triangulate(copy, faces=copy.faces[:]) v_index = [] ngone_co = [] for f in copy.faces: v_index.extend( [v.index for v in f.verts if not f.hide]) ngone_co.extend([ obj.matrix_world @ v.co for v in f.verts if not f.hide ]) copy.free() # TODO может быть удалить ? ngons_indices = [] ngons_indices.extend( list(range(0, len(v_index)))[v_i:v_i + 3] for v_i in range(0, len(v_index), 3)) NGONE = batch_for_shader(shader, 'TRIS', {"pos": ngone_co}, indices=ngons_indices) shader.uniform_float("color", ngone_col) NGONE.draw(shader) # --- Custom if props.custom_count: custom_faces = [] for n in bm.faces: if len(n.verts) == props.custom_count_verts: custom_faces.append(n.index) copy = bm.copy() copy.faces.ensure_lookup_table() edge_n = [ e for i in custom_faces for e in copy.faces[i].edges ] for e in copy.edges: if not e in edge_n: e.hide_set(True) bmesh.ops.triangulate(copy, faces=copy.faces[:]) v_index = [] custom_co = [] for f in copy.faces: v_index.extend( [v.index for v in f.verts if not f.hide]) custom_co.extend([ obj.matrix_world @ v.co for v in f.verts if not f.hide ]) copy.free() # TODO может быть удалить ? custom_faces_indices = [] custom_faces_indices.extend( list(range(0, len(v_index)))[v_i:v_i + 3] for v_i in range(0, len(v_index), 3)) CUSTOM = batch_for_shader(shader, 'TRIS', {"pos": custom_co}, indices=custom_faces_indices) shader.uniform_float("color", custom_col) CUSTOM.draw(shader) if props.tris: tris_co = [ obj.matrix_world @ v.co for f in bm.faces for v in f.verts if len(f.verts) == 3 ] TRIS = batch_for_shader(shader, 'TRIS', {"pos": tris_co}) shader.uniform_float("color", tris_col) TRIS.draw(shader) if props.non_manifold_check: e_non_i = [ e.index for e in bm.edges if not e.is_manifold ] e_non_co = [ obj.matrix_world @ v.co for i in e_non_i for v in bm.edges[i].verts ] EDGES_NON = batch_for_shader(shader, 'LINES', {"pos": e_non_co}) shader.uniform_float("color", e_non_col) EDGES_NON.draw(shader) if props.e_pole: e_pole_co = [ obj.matrix_world @ v.co for v in bm.verts if len(v.link_edges) == 5 ] E_POLE = batch_for_shader(shader, 'POINTS', {"pos": e_pole_co}) shader.uniform_float("color", e_pole_col) E_POLE.draw(shader) if props.n_pole: n_pole_co = [ obj.matrix_world @ v.co for v in bm.verts if len(v.link_edges) == 3 ] N_POLE = batch_for_shader(shader, 'POINTS', {"pos": n_pole_co}) shader.uniform_float("color", n_pole_col) N_POLE.draw(shader) if props.f_pole: f_pole_co = [ obj.matrix_world @ v.co for v in bm.verts if len(v.link_edges) > 5 ] F_POLE = batch_for_shader(shader, 'POINTS', {"pos": f_pole_co}) shader.uniform_float("color", f_pole_col) F_POLE.draw(shader) if props.v_bound: v_bound_co = [ obj.matrix_world @ v.co for v in bm.verts if v.is_boundary or not v.is_manifold ] V_BOUND = batch_for_shader(shader, 'POINTS', { "pos": v_bound_co, }) shader.uniform_float("color", v_bound_col) V_BOUND.draw(shader) if props.v_alone: v_alone_co = [ obj.matrix_world @ v.co for v in bm.verts if len(v.link_edges) < 1 ] V_ALONE = batch_for_shader(shader, 'POINTS', {"pos": v_alone_co}) shader.uniform_float("color", v_alone_col) V_ALONE.draw(shader) if props.use_mod_che: bm.free() if props.line_smooth: bgl.glDisable(bgl.GL_LINE_SMOOTH) bgl.glDisable(bgl.GL_DEPTH_TEST) bgl.glDisable(bgl.GL_CULL_FACE) bgl.glLineWidth(2) bgl.glPointSize(vertex_size) bgl.glDisable(bgl.GL_BLEND)
def perform(waterPlane,viewerCam,waterCam): """ Performs the reflection and refraction textures @param waterPlane Water plane game object @param viewerCam Active camera @param waterCam Auxiliar camera used by water. """ # REFLECTION # Reflect the camera respect to the water plane waterCam.lens = viewerCam.lens waterCam.projection_matrix = viewerCam.projection_matrix waterCam.position = viewerCam.position camToMirror = waterPlane.position - viewerCam.position waterCam.position[2] += 2.0*camToMirror[2] orientation = Matrix(viewerCam.orientation) orientation.transpose() orientation = orientation*Matrix.Rotation(radians(180),3,'Y')*Matrix.Scale(-1,3,Vector([1,0,0])) orientation.transpose() waterCam.orientation = orientation # Hide the water surface waterPlane.visible = False # Render the reflected scene bgl.glCullFace(bgl.GL_FRONT) clip = -1 if viewerCam.position[2] < 0.0: clip = 1 buffer = bgl.Buffer(bgl.GL_DOUBLE, [4], [0.0, 0.0, clip, waterPlane.position[2]+offset]) bgl.glClipPlane(bgl.GL_CLIP_PLANE0,buffer) bgl.glEnable(bgl.GL_CLIP_PLANE0) g.reflection.refresh(True) # Restore bgl.glCullFace(bgl.GL_BACK) bgl.glDisable(bgl.GL_CLIP_PLANE0) # waterPlane.visible = True # REFRACTION # Reflect the camera respect to the water plane waterCam.lens = viewerCam.lens waterCam.projection_matrix = viewerCam.projection_matrix waterCam.position = viewerCam.position waterCam.orientation = Matrix(viewerCam.orientation) # Hide the water surface # waterPlane.visible = False # Render the reflected scene clip = 1 if viewerCam.position[2] < 0.0: clip = -1 buffer = bgl.Buffer(bgl.GL_DOUBLE, [4], [0.0, 0.0, clip, waterPlane.position[2]+offset]) bgl.glClipPlane(bgl.GL_CLIP_PLANE1,buffer) bgl.glEnable(bgl.GL_CLIP_PLANE1) g.refraction.refresh(True) # Restore bgl.glDisable(bgl.GL_CLIP_PLANE1) waterPlane.visible = True
def perform(waterPlane, viewerCam, waterCam): """ Performs the reflection and refraction textures @param waterPlane Water plane game object @param viewerCam Active camera @param waterCam Auxiliar camera used by water. """ # REFLECTION # Reflect the camera respect to the water plane waterCam.lens = viewerCam.lens waterCam.projection_matrix = viewerCam.projection_matrix waterCam.position = viewerCam.position camToMirror = waterPlane.position - viewerCam.position waterCam.position[2] += 2.0 * camToMirror[2] orientation = Matrix(viewerCam.orientation) orientation.transpose() orientation = orientation * Matrix.Rotation( radians(180), 3, 'Y') * Matrix.Scale(-1, 3, Vector([1, 0, 0])) orientation.transpose() waterCam.orientation = orientation # Hide the water surface waterPlane.visible = False # Render the reflected scene bgl.glCullFace(bgl.GL_FRONT) clip = -1 if viewerCam.position[2] < 0.0: clip = 1 buffer = bgl.Buffer(bgl.GL_DOUBLE, [4], [0.0, 0.0, clip, waterPlane.position[2] + offset]) bgl.glClipPlane(bgl.GL_CLIP_PLANE0, buffer) bgl.glEnable(bgl.GL_CLIP_PLANE0) g.reflection.refresh(True) # Restore bgl.glCullFace(bgl.GL_BACK) bgl.glDisable(bgl.GL_CLIP_PLANE0) # waterPlane.visible = True # REFRACTION # Reflect the camera respect to the water plane waterCam.lens = viewerCam.lens waterCam.projection_matrix = viewerCam.projection_matrix waterCam.position = viewerCam.position waterCam.orientation = Matrix(viewerCam.orientation) # Hide the water surface # waterPlane.visible = False # Render the reflected scene clip = 1 if viewerCam.position[2] < 0.0: clip = -1 buffer = bgl.Buffer(bgl.GL_DOUBLE, [4], [0.0, 0.0, clip, waterPlane.position[2] + offset]) bgl.glClipPlane(bgl.GL_CLIP_PLANE1, buffer) bgl.glEnable(bgl.GL_CLIP_PLANE1) g.refraction.refresh(True) # Restore bgl.glDisable(bgl.GL_CLIP_PLANE1) waterPlane.visible = True
own['deltaori'] = own['oldori'] - ori own['deltapos'] = own['oldpos'] - pos own['deltaori'] = own['oldori'] - ori own['deltapos'] = own['oldpos'] - pos #orienting and positioning the reflection rendercamera watercamera.orientation = ori - own['deltaori'] watercamera.position = pos - own['deltapos'] #storing the old orientation and position of the camera own['oldori'] = ori own['oldpos'] = pos #culling front faces as the camera is scaled to -1 bgl.glCullFace(bgl.GL_FRONT) #plane equation normal = own.getAxisVect((0.0, 0.0, 1.0)) #plane normals Z=front D = -own.position.project( normal).magnitude #closest distance from center to plane V = (activecam.position - own.position).normalized().dot( normal) #VdotN to get frontface/backface #invert normals when backface if V < 0: normal = -normal cam['resettimer'] = 0 cam['randomtime'] = (rf() * 5) * 2.0 - 1.0
def mesh_draw_bgl(): if bpy.context.active_object != None: if bpy.context.active_object.select_get(): if bpy.context.object.type == 'MESH': #start_time = time.time() props = bpy.context.preferences.addons[__package__.split(".") [0]].preferences #settings = bpy.context.scene.polySource_set theme = bpy.context.preferences.themes['Default'] vertex_size = theme.view_3d.vertex_size bgl.glEnable(bgl.GL_BLEND) bgl.glLineWidth(props.edge_width) bgl.glPointSize(vertex_size + 5) bgl.glCullFace(bgl.GL_BACK) if props.xray == False: bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glEnable(bgl.GL_CULL_FACE) if props.line_smooth: bgl.glEnable(bgl.GL_LINE_SMOOTH) #bgl.glDepthRange(0, 0.99999) #bgl.glDepthFunc(600) bgl.glDepthMask(False) z_bias = (0.2 + props.z_bias) / 200 shader.bind() matrix = bpy.context.region_data.perspective_matrix shader.uniform_float("view_mat", matrix) shader.uniform_float("Z_Bias", z_bias) #uniques = bpy.context.objects_in_mode_unique_data uniques = bpy.context.selected_objects for obj in uniques: me = obj.data if bpy.context.mode != 'EDIT_MESH': mesh = bmesh.new() mesh.from_mesh(me) mesh.verts.ensure_lookup_table() mesh.edges.ensure_lookup_table() mesh.faces.ensure_lookup_table() elif bpy.context.mode == 'EDIT_MESH': mesh = bmesh.from_edit_mesh(me) if len(mesh.verts) > 1: #retopology mode if props.retopo_mode: # color g_vertex_color = props.vertex_color g_wire_edit_color = props.edge_color g_face_color = props.face_color #faces_ind = [f.index for f in mesh.faces] #faces_cord = [obj.matrix_world @ v.co for i in faces_ind for v in mesh.faces[i].verts] #faces_cord = [obj.matrix_world @ mesh.verts[v].co for i in faces_indices for v in i ] #rab """ face_ind_ngone = [] for face in mesh.faces: if len(face.verts) > 4: face_ind_ngone.append(face.index) """ """ ngone_cord = [] for f in faces_indices: for n_i in face_ind_ngone: if n_i == f.index: for v in f: ngone_cord.append(obj.matrix_world @ mesh.verts[v].co) """ """ edges_indices = [] for i in edges_ind: verts = [] for v in mesh.edges[i].verts: verts.append(v.index) edges_indices.append(verts) """ """ for face in mesh.faces: if face.select: for i, looptris in enumerate(loop_triangles): for loop in looptris: if loop.face == face: ind = i * 3 ind2 = i * 3 + 1 ind3 = ind2 + 1 face_col[ind] = select_color_f #face_col[ind2] = select_color_f #face_col[ind3] = select_color_f """ ################################################################################################################## vertex_co = [ obj.matrix_world @ v.co for v in mesh.verts ] select_color_i = (props.select_items_color.r, props.select_items_color.g, props.select_items_color.b, 1.0) loop_triangles = mesh.calc_loop_triangles() faces_indices = [[ loop.vert.index for loop in looptris ] for looptris in loop_triangles] face_col = [(g_face_color[0], g_face_color[1], g_face_color[2], props.opacity) for _ in range(len(mesh.verts))] FACES = batch_for_shader(shader, 'TRIS', { "position": vertex_co, "colorize": face_col }, indices=faces_indices) FACES.draw(shader) if bpy.context.mode == 'EDIT_MESH': edges_ind = [e.index for e in mesh.edges] edges_cord = [ obj.matrix_world @ v.co for i in edges_ind for v in mesh.edges[i].verts ] edge_col = [ (g_wire_edit_color.r, g_wire_edit_color.g, g_wire_edit_color.b, 1.0) for _ in range(len(edges_cord)) ] vert_col = [ (g_vertex_color.r, g_vertex_color.g, g_vertex_color.b, 1.0) for _ in range(len(mesh.verts)) ] for i, vert in enumerate(mesh.verts): if vert.select: #face_col[i] = select_color_f vert_col[i] = select_color_i for i, edge in enumerate(mesh.edges): if edge.select: ind = i * 2 ind2 = ind + 1 edge_col[ind] = select_color_i edge_col[ind2] = select_color_i EDGES = batch_for_shader( shader, 'LINES', { "position": edges_cord, "colorize": edge_col }) VERTS = batch_for_shader( shader, 'POINTS', { "position": vertex_co, "colorize": vert_col }) EDGES.draw(shader) if bpy.context.tool_settings.mesh_select_mode[ 0]: VERTS.draw(shader) opacity_second = props.opacity + 0.1 # check if props.ngone: ngone = [] for n in mesh.faces: if len(n.verts) > 4: ngone.append(n.index) #print("ngone",ngone) copy = mesh.copy() copy.faces.ensure_lookup_table() edge_n = [ e for i in ngone for e in copy.faces[i].edges ] for e in copy.edges: if not e in edge_n: e.hide_set(True) bmesh.ops.triangulate(copy, faces=copy.faces[:]) v_index = [] ngone_co = [] for f in copy.faces: v_index.extend( [v.index for v in f.verts if not f.hide]) ngone_co.extend([ obj.matrix_world @ v.co for v in f.verts if not f.hide ]) copy.free() #maybe delete ngons_indices = [] ngons_indices.extend( list(range(0, len(v_index)))[v_i:v_i + 3] for v_i in range(0, len(v_index), 3)) #print("ngons_indices",ngons_indices) ngone_col = [(props.ngone_col.r, props.ngone_col.g, props.ngone_col.b, opacity_second) for _ in range(len(ngone_co))] NGONE = batch_for_shader(shader, 'TRIS', { "position": ngone_co, "colorize": ngone_col }, indices=ngons_indices) NGONE.draw(shader) if props.tris: tris_co = [ obj.matrix_world @ v.co for f in mesh.faces for v in f.verts if len(f.verts) == 3 ] tris_col = [(props.tris_col.r, props.tris_col.g, props.tris_col.b, opacity_second) for _ in range(len(tris_co))] TRIS = batch_for_shader(shader, 'TRIS', { "position": tris_co, "colorize": tris_col }) TRIS.draw(shader) if props.non_manifold_check: e_non_i = [ e.index for e in mesh.edges if not e.is_manifold ] e_non_co = [ obj.matrix_world @ v.co for i in e_non_i for v in mesh.edges[i].verts ] e_non_col = [ (props.non_manifold_color.r, props.non_manifold_color.g, props.non_manifold_color.b, opacity_second) for _ in range(len(e_non_co)) ] EDGES_NON = batch_for_shader( shader, 'LINES', { "position": e_non_co, "colorize": e_non_col }) EDGES_NON.draw(shader) if props.e_pole: e_pole_co = [ obj.matrix_world @ v.co for v in mesh.verts if len(v.link_edges) == 5 ] e_pole_col = [ (props.e_pole_col.r, props.e_pole_col.g, props.e_pole_col.b, opacity_second) for _ in range(len(e_pole_co)) ] E_POLE = batch_for_shader(shader, 'POINTS', { "position": e_pole_co, "colorize": e_pole_col }) E_POLE.draw(shader) if props.n_pole: n_pole_co = [ obj.matrix_world @ v.co for v in mesh.verts if len(v.link_edges) == 3 ] n_pole_col = [ (props.n_pole_col.r, props.n_pole_col.g, props.n_pole_col.b, opacity_second) for _ in range(len(n_pole_co)) ] N_POLE = batch_for_shader(shader, 'POINTS', { "position": n_pole_co, "colorize": n_pole_col }) N_POLE.draw(shader) if props.f_pole: f_pole_co = [ obj.matrix_world @ v.co for v in mesh.verts if len(v.link_edges) > 5 ] f_pole_col = [ (props.f_pole_col.r, props.f_pole_col.g, props.f_pole_col.b, opacity_second) for _ in range(len(f_pole_co)) ] F_POLE = batch_for_shader(shader, 'POINTS', { "position": f_pole_co, "colorize": f_pole_col }) F_POLE.draw(shader) if props.v_bound: v_bound_co = [ obj.matrix_world @ v.co for v in mesh.verts if v.is_boundary and v.is_manifold ] v_bound_col = [ (props.bound_col.r, props.bound_col.g, props.bound_col.b, opacity_second) for _ in range(len(v_bound_co)) ] V_BOUND = batch_for_shader(shader, 'POINTS', { "position": v_bound_co, "colorize": v_bound_col }) V_BOUND.draw(shader) if props.v_alone: v_alone_co = [ obj.matrix_world @ v.co for v in mesh.verts if not v.is_manifold ] v_alone_col = [ (props.non_manifold_color.r, props.non_manifold_color.g, props.non_manifold_color.b, opacity_second) for _ in range(len(v_alone_co)) ] V_ALONE = batch_for_shader(shader, 'POINTS', { "position": v_alone_co, "colorize": v_alone_col }) V_ALONE.draw(shader) if props.line_smooth: bgl.glDisable(bgl.GL_LINE_SMOOTH) bgl.glDisable(bgl.GL_DEPTH_TEST) bgl.glDisable(bgl.GL_CULL_FACE) bgl.glLineWidth(2) bgl.glPointSize(vertex_size) bgl.glDisable(bgl.GL_BLEND)