Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REQUEST: view cage in real time when changing extrusion #60

Closed
2 tasks done
KirilStrezikozin opened this issue Jan 18, 2024 · 6 comments
Closed
2 tasks done

REQUEST: view cage in real time when changing extrusion #60

KirilStrezikozin opened this issue Jan 18, 2024 · 6 comments
Assignees
Labels
feature upgrade for your old lawnmower solved How could you? GPT helped?

Comments

@KirilStrezikozin
Copy link
Owner

KirilStrezikozin commented Jan 18, 2024

This feature request is:

  • not a duplicate
  • implemented

Describe what you'd like to be implemented
@BusterCharlie wrote on Discord (rephrased):

Adding cage visualization would be a good feature.

Additional context
EZ Baker probably has that viz impl (update: EZ Baker's cage preview is dogwater).

@KirilStrezikozin KirilStrezikozin added the feature upgrade for your old lawnmower label Jan 18, 2024
@KirilStrezikozin KirilStrezikozin self-assigned this Jan 18, 2024
@KirilStrezikozin KirilStrezikozin added this to the BakeMaster 2.6.0 milestone Jan 18, 2024
@KirilStrezikozin
Copy link
Owner Author

KirilStrezikozin commented Jan 19, 2024

EZ Baker implements real-time cage preview by making a linked copy of a low-poly object.
The cage preview visualization has the following prerequisites:

  1. The solid shading mode properties include Color shading for Objects.
  2. The new cage object has solid shading, with wireframe shading, and is colored with transparency in viewport display properties.
    Creating a linked copy of a mesh shouldn't be a high cost, but it requires copying object modifiers and properties. Overhead is changing user-defined solid shading of objects in a scene.

Another method is to draw object outline (shadow-ghost shader) with blender's GPU and OpenGL graphics modules. Resource for drawing tips (but not a solution).

@KirilStrezikozin KirilStrezikozin added the in progress All night I stay not sleeping because I'm thinking about this label Jan 19, 2024
KirilStrezikozin added a commit that referenced this issue Jan 21, 2024
…me cage preview

explanation and notes are at the #60 issue thread
@KirilStrezikozin
Copy link
Owner Author

KirilStrezikozin commented Jan 21, 2024

Overview

Commit 9abe635 introduces a core cage shader already close to the initial idea. As pointed out in #60 (comment) (previous comment above), the cost of creating a low-poly copy (even linked, not considering modifiers) is higher than the cost of drawing a GLSL shader right into the 3D viewport, which is what 9abe635 is about.

Added shader.py script with essential functionality to make the cage shader preview the cage object in real-time. So far it includes functions derived from Blender's GPU module documentation resource (especially the Triangle with Custom Shader example), as well as How to draw an outline with bpy gpu module? on StackExchange.

The shader structure and working logic (apart from what could be read at this page) are the following:

  1. shader.mesh_data() accepts a mesh object and returns two arrays containing coordinates and indices.
  2. shader.cage is the shader base written in GLSL. The shader calculates the gl_Position and the fragment color of each supplied vertex position, considering the current viewport's viewProjectionMatrix and the cage extrusion value. Below is the vertex_source for the base cage shader with formulas and variables explained (don't get me wrong, I know I could use dot product):
in vec3 position; // input vertex position (x, y, z)

uniform mat4 viewProjectionMatrix;
uniform float extrusion;
uniform vec4 color;

out vec4 color;

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;

  // The gl_Position in the viewport is calculated using the mat4 viewProjectionMatrix
  // and the vertex position multiplied by k - scale factor determined using
  // vertex coordinates and extrusion value.
  gl_Position = viewProjectionMatrix * vec4(position * k, 1.0f);

  // fragment_source receives fcolor
  // (constant uniform, the whole cage shader uses one color)
  fcolor = color;
}

Examples:

  • A cage created for a sphere object with 0.1 extrusion (the torus in the middle is a different object used to view the cage overlaying other objects):
    image
  • The same setup, but with 0.5 extrusion (extrusion is the value in the default units e.g. meters. Internally, all length values in Blender are meters):
    image

The next steps:

  1. The k multiplier is not calculated correctly (the vertex coordinates are just scaled but not moved outwards from the mesh, e.g. alongside vertex normals?):
    image
  2. Take into account the object transform to calculate the shader. This fixes the following (the cage is still spherical although the object is transformed):
    image
  3. Display the wireframe overlay on top of the base cage shader (for beauty only).

@KirilStrezikozin
Copy link
Owner Author

KirilStrezikozin commented Jan 21, 2024

A halfway solution to solving 1st bullet point in the next steps above could be something like this (took it from this forum post, which is to move the vertex alongside its normal:

vec4 v = vec4(gl_Vertex.xyz + gl_Normal * moveamount, 1);
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * v;

The edit to suit our needs (vec3 normal is vertex normal taken from calculated loop triangle tessellation) would be:

gl_Position = viewProjectionMatrix * vec4(position + normal * extrusion, 1.0f);

KirilStrezikozin added a commit that referenced this issue Jan 21, 2024
The k multiplier was not calculated correctly (the vertex coordinates were just scaled but not moved outwards from the mesh)
KirilStrezikozin added a commit that referenced this issue Jan 21, 2024
fixing cage shader not considering object transforms (e.g. location, rotation, scale, parenting transforms)
KirilStrezikozin added a commit that referenced this issue Jan 21, 2024
@KirilStrezikozin
Copy link
Owner Author

I will reference this useful post on stackexchange about getting object data (like vertices) with modifiers and shape keys taken into account (evaluated object data).

@KirilStrezikozin
Copy link
Owner Author

KirilStrezikozin commented Jan 21, 2024

The latest cage with 0.1 extrusion (notice the cage following object transforms and deformations - modifiers):
image

@KirilStrezikozin
Copy link
Owner Author

KirilStrezikozin commented Jan 21, 2024

Integration todo:

  • Integrate ModalOperator with ui.

KirilStrezikozin added a commit that referenced this issue Jan 22, 2024
take max ray distance into account as well
KirilStrezikozin added a commit that referenced this issue Jan 22, 2024
added cancel() method to the Shader_Cage class to call draw_cancel() on OT cancel event
KirilStrezikozin added a commit that referenced this issue Jan 22, 2024
… value

That is probably it for the cage preview)
@KirilStrezikozin KirilStrezikozin added needs testing wow does it even work? solved How could you? GPT helped? close on release done here, but let's wait for the release and removed in progress All night I stay not sleeping because I'm thinking about this needs testing wow does it even work? labels Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature upgrade for your old lawnmower solved How could you? GPT helped?
Projects
None yet
Development

No branches or pull requests

1 participant