def draw_fn(self, shader):
indices = tf.placeholder(tf.int32, [None, 3], name="ph_indices")
verts = [None, None, None]
for i in range(3):
verts[i] = shader.vertex(indices[:, i], i)
verts[i] = tf.matmul(verts[i], self.viewport, transpose_b=True)
verts[i] = utils.affine_to_cartesian(verts[i])
bbmin, bbmax = bounds(verts, self.width, self.height)
def _fn(i):
bbmin_i = tf.gather(bbmin, i)
bbmax_i = tf.gather(bbmax, i)
verts_i = [tf.gather(verts[0], i),
tf.gather(verts[1], i),
tf.gather(verts[2], i)]
x, y = tf.meshgrid(tf.range(bbmin_i[0], bbmax_i[0]),
tf.range(bbmin_i[1], bbmax_i[1]))
num_frags = tf.reduce_prod(tf.shape(x))
p = tf.stack([tf.reshape(x, [-1]),
tf.reshape(y, [-1]),
tf.zeros([num_frags], dtype=tf.float32)], axis=1)
bc, valid = barycentric(verts_i, p)
p = tf.boolean_mask(p, valid)
bc = [tf.boolean_mask(bc[k], valid) for k in range(3)]
z = utils.tri_dot([verts_i[k][2] for k in range(3)], bc)
inds = tf.to_int32(tf.stack([p[:, 1], p[:, 0]], axis=1))
cur_z = tf.gather_nd(self.depth, inds)
visible = tf.less_equal(cur_z, z)
inds = tf.boolean_mask(inds, visible)
bc = [tf.boolean_mask(bc[k], visible) for k in range(3)]
z = tf.boolean_mask(z, visible)
c = utils.pack_colors(shader.fragment(bc, i), 1)
updates = [
tf.scatter_nd_update(self.color, inds, c, use_locking=False),
tf.scatter_nd_update(self.depth, inds, z, use_locking=False)]
return updates
num_faces = tf.shape(indices)[0]
updates = utils.sequential_for(_fn, 0, num_faces)
self.commands.append(updates)
def _draw(indices_val, **kwargs):
self.args[indices] = indices_val
for k, v in kwargs.items():
self.args[getattr(shader, k)] = v
return _draw
评论列表
文章目录