Skip to content

Commit

Permalink
(#60 shader) add: extrude cage vertices along normals
Browse files Browse the repository at this point in the history
The k multiplier was not calculated correctly (the vertex coordinates were just scaled but not moved outwards from the mesh)
  • Loading branch information
KirilStrezikozin committed Jan 21, 2024
1 parent 9abe635 commit c918be7
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions shader.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,28 @@
gpu_t = gpu.types


def mesh_data(mesh: bpy_t.Mesh) -> Tuple[np.ndarray, np.ndarray]:
def mesh_data(mesh: bpy_t.Mesh) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
mesh.calc_loop_triangles()

coords = np.empty(len(mesh.vertices) * 3, dtype=np.float32)
mesh.vertices.foreach_get('co', coords)
coords.shape = (-1, 3)

normals = np.empty(len(mesh.vertices) * 3, dtype=np.float32)
mesh.vertices.foreach_get('normal', normals)
normals.shape = (-1, 3)

indices = np.empty(len(mesh.loop_triangles) * 3, dtype=np.int32)
mesh.loop_triangles.foreach_get('vertices', indices)
indices.shape = (-1, 3)

return coords, indices
return coords, normals, indices


def cage() -> gpu_t.GPUShader:
vertex_source = """
in vec3 position;
in vec3 normal;
uniform mat4 viewProjectionMatrix;
uniform float extrusion;
Expand All @@ -69,12 +74,8 @@ def cage() -> gpu_t.GPUShader:
out vec4 fcolor;
void main() {
float x2 = position[0] * position[0];
float y2 = position[1] * position[1];
float z2 = position[2] * position[2];
float k = extrusion * pow((x2 + y2 + z2), -0.5f) + 1;
gl_Position = viewProjectionMatrix * vec4(position * k, 1.0f);
gl_Position = viewProjectionMatrix * vec4(
position + normal * extrusion, 1.0f);
fcolor = color;
}
Expand All @@ -94,11 +95,11 @@ def cage() -> gpu_t.GPUShader:


def cage_batch(shader: gpu_t.GPUShader, mesh: bpy_t.Mesh) -> gpu_t.GPUBatch:
coords, indices = mesh_data(mesh)
coords, normals, indices = mesh_data(mesh)

batch = batch_for_shader(
shader, 'TRIS',
{"position": coords},
{"position": coords, "normal": normals},
indices=indices)

return batch
Expand Down

0 comments on commit c918be7

Please sign in to comment.