8
$\begingroup$

Is there a python code to find and delete back-faces? As a way to do "back-face culling".

$\endgroup$
0

3 Answers 3

6
$\begingroup$

If you know a little bit of python you can modify and expand upon this snippet:

from: http://freespace.virgin.net/hugo.elias/routines/r_dot.htm When deciding if a polygon is facing the camera, you need only calculate the dot product of the normal vector of that polygon, with a vector from the camera to one of the polygon's vertices.

  • If the dot product is less than zero, the polygon is facing the camera.
  • If the value is greater than zero, it is facing away from the camera.

this could be further optimized, but I think that obfuscates the concepts. Run this code from 3dview (a 3d view context) either through a camera or user/perspective view - with the object you want to work on selected and in Object mode.

from mathutils import Vector 

def get_locals(context):
    region = context.region  
    rv3d = context.space_data.region_3d  
    obj = context.active_object
    vertlist = obj.data.vertices
    return region, rv3d, obj, vertlist

and

def select_front_facing(context):

    region, rv3d, obj, vertlist = get_locals(context)

    # neat eye location code with the help of paleajed
    eye = Vector(rv3d.view_matrix[2][:3])
    eye.length = rv3d.view_distance
    eye_location = rv3d.view_location + eye  

    for idx, polygon in enumerate(obj.data.polygons):

        vert_index = polygon.vertices[0]
        pnormal = obj.matrix_world * polygon.normal
        world_coordinate = obj.matrix_world * vertlist[vert_index].co

        result_vector = eye_location - world_coordinate
        dot_value = pnormal.dot(result_vector.normalized())            

        if dot_value < 0.0:
            polygon.select = False
        else:
            polygon.select = True

Here is a short working demo, you can run this from text editor: https://gist.github.com/zeffii/5766177

I use it in my basic SVG renderer, to fake a wireframe material which occludes back-facing, it isn't perfect for all possible use-cases but it works to 'select back-facing'. You can run the SVG renderer directly from the Text Editor, and it will set every front facing Polygon to the select == True state.

$\endgroup$
4
  • $\begingroup$ you need a little bit of extra code like I have in the svg renderer, to give you view3d context. If you stick around i'll edit the original post with a small example $\endgroup$
    – zeffii
    Commented Jun 12, 2013 at 14:40
  • $\begingroup$ @oxtvy, i have updated the post with a github gist: gist.github.com/zeffii/5766177 $\endgroup$
    – zeffii
    Commented Jun 12, 2013 at 15:15
  • $\begingroup$ your github script is not adding any panel for me. though i found a way to try it and that eye math works. $\endgroup$
    – oxtvy
    Commented Jun 12, 2013 at 16:37
  • $\begingroup$ It should be visible in the N panel, though it was still called 'render svg' i've renamed it to 'select backfacing' if you ever re-download. $\endgroup$
    – zeffii
    Commented Jun 12, 2013 at 16:44
2
$\begingroup$

Have you looked at this Blender Artists thread? (Post #9) They changed the object's material to a node material. In the nodes, they mapped the back faces of the object to alpha zero using a geometry node.

Summary: you can make the back faces transparent to simulate back-face culling. I'm not sure how much faster it will be, but it will be nicer than deleting portions of your geometry.

enter image description here

$\endgroup$
4
  • $\begingroup$ this works only if i untick the "Traceable" in "Render Pipeline Options", but this stops the the material from casting shadows. also what if the material has it's own transparency? $\endgroup$
    – oxtvy
    Commented Jun 12, 2013 at 11:45
  • $\begingroup$ Not sure why it doesn't work; mine works with Traceable enabled. If the material has transparency, you'll need a Math:multiply node to combine the two alpha channels. $\endgroup$
    – Azmisov
    Commented Jun 12, 2013 at 16:24
  • $\begingroup$ i have two overlapping meshes facing in opposite directions(like two sides of the same mesh), because they're overlapping, they randomly drop shadows on each other. by stopping the faces, that are not facing toward the light, from casting shadows should fix this. is it possible to use this "transparency method" to make "back-face culling for the light source"??? the rays should pass trough the "back-faces from the lights perspective", without casting shadows. example blend file $\endgroup$
    – oxtvy
    Commented Jun 13, 2013 at 8:34
  • $\begingroup$ Both BI and cycles cull backfaces by default for me. $\endgroup$
    – gandalf3
    Commented Jun 11, 2014 at 6:42
1
$\begingroup$

And then, for the adventurous amongst you, I made a patch for Blender source code that allows getting occlusion information for BMesh elements.

Syntax:

state = bmelem.occlusion_get(rv3d)

rv3d being a RegionView3D

Get the patch here.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .