decorators cleanup

This commit is contained in:
QwiglyDee
2020-12-22 18:47:50 +07:00
parent 6f84bc6ce5
commit 8baf9b056d
@@ -21,13 +21,13 @@ class BaseDecorator():
VERT_GLSL = """ VERT_GLSL = """
uniform mat4 viewMatrix; uniform mat4 viewMatrix;
in vec3 pos; in vec3 pos;
in uint style; in uint topo;
out vec4 gl_Position; out vec4 gl_Position;
out uint vstyle; out uint type;
void main() { void main() {
gl_Position = viewMatrix * vec4(pos, 1.0); gl_Position = viewMatrix * vec4(pos, 1.0);
vstyle = style; type = topo;
} }
""" """
GEOM_GLSL = """ GEOM_GLSL = """
@@ -129,16 +129,22 @@ class BaseDecorator():
collection = bpy.data.collections.get("IfcGroup/" + drawing.name) collection = bpy.data.collections.get("IfcGroup/" + drawing.name)
return filter(lambda o: self.basename in o.name, collection.objects) return filter(lambda o: self.basename in o.name, collection.objects)
def get_path_geom(self, obj): def get_path_geom(self, obj, topo=True):
"""parse path geometry into line segments """Parses path geometry into line segments
returns: Args:
- vertices: 3-tuples of coords obj: Blender object with data of type Curve
- indices: 2-tuples of each segment verices' indices topo: bool; if types of vertices are needed
- styles: 0=internal, 1=beginning, 2=ending Returns:
vertices: 3-tuples of coords
indices: 2-tuples of each segment verices' indices
topology: types of vertices
0: internal
1: beginning
2: ending
""" """
vertices = [] vertices = []
indices = [] indices = []
styles = [] topology = []
idx = 0 idx = 0
for spline in obj.data.splines: for spline in obj.data.splines:
@@ -146,64 +152,46 @@ class BaseDecorator():
points = [obj.matrix_world @ p.co for p in spline_points] points = [obj.matrix_world @ p.co for p in spline_points]
cnt = len(points) cnt = len(points)
vertices.extend(p[:3] for p in points) vertices.extend(p[:3] for p in points)
styles.append(1) if topo:
styles.extend([0] * max(0, cnt - 2)) topology.append(1)
styles.append(2) topology.extend([0] * max(0, cnt - 2))
topology.append(2)
indices.extend((idx+i, idx+i+1) for i in range(cnt-1)) indices.extend((idx+i, idx+i+1) for i in range(cnt-1))
idx += cnt idx += cnt
return vertices, indices, styles return vertices, indices, topology
def get_mesh_geom(self, obj): def get_mesh_geom(self, obj):
"""parse mesh geometry into line segments """Parses mesh geometry into line segments
returns:
- vertices: 3-tuples of coords Args:
- indices: 2-tuples of each segment verices' indices obj: Blender object with data of type Mesh
- styles: 0=internal, 1=beginning, 2=ending
Returns:
vertices: 3-tuples of coords
indices: 2-tuples of each segment verices' indices
""" """
vertices = [obj.matrix_world @ v.co for v in obj.data.vertices] vertices = [obj.matrix_world @ v.co for v in obj.data.vertices]
indices = [e.vertices for e in obj.data.edges] indices = [e.vertices for e in obj.data.edges]
return vertices, indices, None return vertices, indices
def decorate(self, object): def decorate(self, object):
"""perform actuall drawing stuff""" """perform actuall drawing stuff"""
raise NotImplementedError() raise NotImplementedError()
def draw_lines(self, obj, geom): def draw_lines(self, obj, vertices, indices, topology=None):
region = self.context.region region = self.context.region
region3d = self.context.region_data region3d = self.context.region_data
vertices, indices, styles = geom
fmt = GPUVertFormat() fmt = GPUVertFormat()
fmt.attr_add(id="pos", comp_type='F32', len=3, fetch_mode='FLOAT') fmt.attr_add(id="pos", comp_type='F32', len=3, fetch_mode='FLOAT')
if topology:
fmt.attr_add(id="topo", comp_type='U8', len=1, fetch_mode='INT')
vbo = GPUVertBuf(len=len(vertices), format=fmt) vbo = GPUVertBuf(len=len(vertices), format=fmt)
vbo.attr_fill(id="pos", data=vertices) vbo.attr_fill(id="pos", data=vertices)
if topology:
ibo = GPUIndexBuf(type='LINES', seq=indices) vbo.attr_fill(id="topo", data=topology)
batch = GPUBatch(type='LINES', buf=vbo, elem=ibo)
self.shader.bind()
self.shader.uniform_float
self.shader.uniform_float("viewMatrix", region3d.perspective_matrix)
self.shader.uniform_float("winsize", (region.width, region.height))
batch.draw(self.shader)
def draw_lines_styled(self, obj, geom):
region = self.context.region
region3d = self.context.region_data
vertices, indices, styles = geom
fmt = GPUVertFormat()
fmt.attr_add(id="pos", comp_type='F32', len=3, fetch_mode='FLOAT')
fmt.attr_add(id="style", comp_type='U8', len=1, fetch_mode='INT')
vbo = GPUVertBuf(len=len(vertices), format=fmt)
vbo.attr_fill(id="pos", data=vertices)
vbo.attr_fill(id="style", data=styles)
ibo = GPUIndexBuf(type='LINES', seq=indices) ibo = GPUIndexBuf(type='LINES', seq=indices)
@@ -280,14 +268,13 @@ class DimensionDecorator(BaseDecorator):
self.dpi = context.preferences.system.dpi self.dpi = context.preferences.system.dpi
def decorate(self, obj): def decorate(self, obj):
geom = self.get_path_geom(obj) verts, idxs, _ = self.get_path_geom(obj, topo=False)
self.draw_lines(obj, geom) self.draw_lines(obj, verts, idxs)
self.draw_labels(obj, geom) self.draw_labels(obj, verts, idxs)
def draw_labels(self, obj, geom): def draw_labels(self, obj, vertices, indices):
region = self.context.region region = self.context.region
region3d = self.context.region_data region3d = self.context.region_data
vertices, indices, _ = geom
for i0, i1 in indices: for i0, i1 in indices:
v0 = Vector(vertices[i0]) v0 = Vector(vertices[i0])
v1 = Vector(vertices[i1]) v1 = Vector(vertices[i1])
@@ -341,10 +328,9 @@ class EqualityDecorator(DimensionDecorator):
basename = "IfcAnnotation/Equal" basename = "IfcAnnotation/Equal"
installed = None installed = None
def draw_labels(self, obj, geom): def draw_labels(self, obj, vertices, indices):
region = self.context.region region = self.context.region
region3d = self.context.region_data region3d = self.context.region_data
vertices, indices, _ = geom
for i0, i1 in indices: for i0, i1 in indices:
v0 = Vector(vertices[i0]) v0 = Vector(vertices[i0])
v1 = Vector(vertices[i1]) v1 = Vector(vertices[i1])
@@ -366,11 +352,11 @@ class LeaderDecorator(BaseDecorator):
layout(lines) in; layout(lines) in;
layout(line_strip, max_vertices=MAX_POINTS) out; layout(line_strip, max_vertices=MAX_POINTS) out;
in uint vstyle[]; in uint type[];
void main() { void main() {
vec4 p0 = gl_in[0].gl_Position, p1 = gl_in[1].gl_Position; vec4 p0 = gl_in[0].gl_Position, p1 = gl_in[1].gl_Position;
uint s0 = vstyle[0], s1 = vstyle[1]; uint t0 = type[0], t1 = type[1];
mat4 windowMatrix = mat4(winsize.x/2, 0, 0, 0, 0, winsize.y/2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); mat4 windowMatrix = mat4(winsize.x/2, 0, 0, 0, 0, winsize.y/2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
mat4 unwindowMatrix = inverse(windowMatrix); mat4 unwindowMatrix = inverse(windowMatrix);
@@ -385,7 +371,7 @@ class LeaderDecorator(BaseDecorator):
for(int i=0; i<3; i++) head_ndc[i] = unwindowMatrix * vec4(head[i], 0, 0) * p1.w; for(int i=0; i<3; i++) head_ndc[i] = unwindowMatrix * vec4(head[i], 0, 0) * p1.w;
// end edge arrow for last segment // end edge arrow for last segment
if (s1 == 2u) { if (t1 == 2u) {
gl_Position = p1; gl_Position = p1;
EmitVertex(); EmitVertex();
gl_Position = p1 - head_ndc[1]; gl_Position = p1 - head_ndc[1];
@@ -410,8 +396,8 @@ class LeaderDecorator(BaseDecorator):
""" """
def decorate(self, obj): def decorate(self, obj):
geom = self.get_path_geom(obj) verts, idxs, topo = self.get_path_geom(obj)
self.draw_lines_styled(obj, geom) self.draw_lines(obj, verts, idxs, topo)
class StairDecorator(BaseDecorator): class StairDecorator(BaseDecorator):
@@ -428,11 +414,11 @@ class StairDecorator(BaseDecorator):
layout(lines) in; layout(lines) in;
layout(line_strip, max_vertices=MAX_POINTS) out; layout(line_strip, max_vertices=MAX_POINTS) out;
in uint vstyle[]; in uint type[];
void main() { void main() {
vec4 p0 = gl_in[0].gl_Position, p1 = gl_in[1].gl_Position; vec4 p0 = gl_in[0].gl_Position, p1 = gl_in[1].gl_Position;
uint s0 = vstyle[0], s1 = vstyle[1]; uint t0 = type[0], t1 = type[1];
mat4 windowMatrix = mat4(winsize.x/2, 0, 0, 0, 0, winsize.y/2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); mat4 windowMatrix = mat4(winsize.x/2, 0, 0, 0, 0, winsize.y/2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
mat4 unwindowMatrix = inverse(windowMatrix); mat4 unwindowMatrix = inverse(windowMatrix);
@@ -442,7 +428,7 @@ class StairDecorator(BaseDecorator):
vec4 gap0 = vec4(0), gap1 = vec4(0); vec4 gap0 = vec4(0), gap1 = vec4(0);
// start edge circle for first segment // start edge circle for first segment
if (s0 == 1u) { if (t0 == 1u) {
vec2 head[CIRCLE_SEGS]; vec2 head[CIRCLE_SEGS];
circle_head(p0w, p1w, head); circle_head(p0w, p1w, head);
for(int i=0; i<CIRCLE_SEGS; i++) { for(int i=0; i<CIRCLE_SEGS; i++) {
@@ -458,7 +444,7 @@ class StairDecorator(BaseDecorator):
} }
// end edge arrow for last segment // end edge arrow for last segment
if (s1 == 2u) { if (t1 == 2u) {
vec2 head[3]; vec2 head[3];
vec4 head_ndc[3]; vec4 head_ndc[3];
arrow_head(p0w, p1w, head); arrow_head(p0w, p1w, head);
@@ -488,8 +474,8 @@ class StairDecorator(BaseDecorator):
""" """
def decorate(self, obj): def decorate(self, obj):
geom = self.get_path_geom(obj) verts, idxs, topo = self.get_path_geom(obj)
self.draw_lines_styled(obj, geom) self.draw_lines(obj, verts, idxs, topo)
class HiddenDecorator(BaseDecorator): class HiddenDecorator(BaseDecorator):
@@ -533,7 +519,6 @@ class HiddenDecorator(BaseDecorator):
void main() { void main() {
float t = fract(dist / DASH_SIZE); float t = fract(dist / DASH_SIZE);
// fragColor = vec4(1.0, 1.0, 1.0, 1.0) * t;
if (t > DASH_RATIO) { if (t > DASH_RATIO) {
discard; discard;
} else { } else {
@@ -543,5 +528,5 @@ class HiddenDecorator(BaseDecorator):
""" """
def decorate(self, obj): def decorate(self, obj):
geom = self.get_mesh_geom(obj) verts, idxs = self.get_mesh_geom(obj)
self.draw_lines(obj, geom) self.draw_lines(obj, verts, idxs)