Sure! This is only a partial code that updates the buffers if it is neccessary. The rest is similar to what is available at other sources regarding DirectContext3D.
def update_buffer(self):
if self.penetrations is None: return False
try:
lines = []
triangles = []
colors = []
for pen_item in self.penetrations:
if pen_item.proposed:
primitives = pen_item.proposed.get_wireframe()
else:
primitives = pen_item.current.get_wireframe()
lines.extend(primitives[0])
triangles.extend(primitives[1])
colors.append(pen_item.status_color)
tri_f_bits = dc.VertexFormatBits.PositionNormalColored
tri_vertex_format = dc.VertexFormat(tri_f_bits)
tri_effect_instance = dc.EffectInstance(tri_f_bits)
# If you try to handle transparency globally it will also
# overrides vertex colors, and overrides them to black
# tri_effect_instance.SetTransparency(0.8)
tri_vertex_buffer_size = \
dc.VertexPositionNormalColored.GetSizeInFloats() \
* len(triangles) * 3
tri_vertex_buffer = dc.VertexBuffer(tri_vertex_buffer_size)
tri_index_buffer_size = \
dc.IndexTriangle.GetSizeInShortInts() * len(triangles)
tri_index_buffer = dc.IndexBuffer(tri_index_buffer_size)
tri_vertex_buffer.Map(tri_vertex_buffer_size)
tri_index_buffer.Map(tri_index_buffer_size)
tri_vertex_stream_p = \
tri_vertex_buffer.GetVertexStreamPositionNormalColored()
tri_index_stream_p = tri_index_buffer.GetIndexStreamTriangle()
for triangle in triangles:
triangle_index = triangles.index(triangle)
first_idx = triangle_index * 3
tri_vertex_stream_p.AddVertex(dc.VertexPositionNormalColored(
triangle[1], triangle[0], colors[triangle_index / 12]
))
tri_vertex_stream_p.AddVertex(dc.VertexPositionNormalColored(
triangle[2], triangle[0], colors[triangle_index / 12]
))
tri_vertex_stream_p.AddVertex(dc.VertexPositionNormalColored(
triangle[3], triangle[0], colors[triangle_index / 12]
))
tri_index_stream_p.AddTriangle(dc.IndexTriangle(
first_idx,
first_idx + 1,
first_idx + 2
))
tri_vertex_buffer.Unmap()
tri_index_buffer.Unmap()
self.triangle_buffer = (
tri_vertex_buffer,
tri_vertex_buffer_size,
tri_index_buffer,
tri_index_buffer_size,
tri_vertex_format,
tri_effect_instance,
dc.PrimitiveType.TriangleList,
0,
len(triangles)
)
line_f_bits = dc.VertexFormatBits.PositionColored
line_vertex_format = dc.VertexFormat(line_f_bits)
line_effect_instance = dc.EffectInstance(line_f_bits)
line_vertex_buffer_size = \
dc.VertexPositionColored.GetSizeInFloats() * len(lines) * 2
line_vertex_buffer = dc.VertexBuffer(line_vertex_buffer_size)
line_index_buffer_size = \
dc.IndexLine.GetSizeInShortInts() * len(lines)
line_index_buffer = dc.IndexBuffer(line_index_buffer_size)
line_vertex_buffer.Map(line_vertex_buffer_size)
line_index_buffer.Map(line_index_buffer_size)
line_vertex_stream_p = \
line_vertex_buffer.GetVertexStreamPositionColored()
line_index_stream_p = line_index_buffer.GetIndexStreamLine()
for line in lines:
line_index = lines.index(line)
first_idx = line_index * 2
line_vertex_stream_p.AddVertex(dc.VertexPositionColored(
line.GetEndPoint(0), colors[line_index / 12]
))
line_vertex_stream_p.AddVertex(dc.VertexPositionColored(
line.GetEndPoint(1), colors[line_index / 12]
))
line_index_stream_p.AddLine(dc.IndexLine(
first_idx,
first_idx + 1
))
line_vertex_buffer.Unmap()
line_index_buffer.Unmap()
self.line_buffer = (
line_vertex_buffer,
line_vertex_buffer_size,
line_index_buffer,
line_index_buffer_size,
line_vertex_format,
line_effect_instance,
dc.PrimitiveType.LineList,
0,
len(lines)
)
self.update_flag = False
return True
except:
return False
EDIT: I've tried to reproduce the previous issue and provide screenshots, but I just couldn't. It is weird. I even reverted my whole branch to the state where it didn't work, but now it is working.
So... I have to withdraw everything I said, because I can't prove none of the above, and it remains and assumption.
At least there is some python example of DirectContext3D 😉