Skip to content

Commit

Permalink
Start to update vertex hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
davepagurek committed Dec 18, 2024
1 parent ddb3a33 commit 3ebde70
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 54 deletions.
51 changes: 15 additions & 36 deletions src/webgl/material.js
Original file line number Diff line number Diff line change
Expand Up @@ -1195,56 +1195,34 @@ function material(p5, fn){
* </td></tr>
* <tr><td>
*
* `vec3 getLocalPosition`
*
* </td><td>
*
* Update the position of vertices before transforms are applied. It takes in `vec3 position` and must return a modified version.
*
* </td></tr>
* <tr><td>
*
* `vec3 getWorldPosition`
* `Vertex getObjectInputs`
*
* </td><td>
*
* Update the position of vertices after transforms are applied. It takes in `vec3 position` and pust return a modified version.
*
* </td></tr>
* <tr><td>
*
* `vec3 getLocalNormal`
*
* </td><td>
*
* Update the normal before transforms are applied. It takes in `vec3 normal` and must return a modified version.
*
* </td></tr>
* <tr><td>
*
* `vec3 getWorldNormal`
*
* </td><td>
*
* Update the normal after transforms are applied. It takes in `vec3 normal` and must return a modified version.
* Update the vertex data of the model being drawn before any positioning has been applied. It takes in a `Vertex` struct, which includes:
* - `vec3 position`, the position of the vertex
* - `vec3 normal`, the direction facing out of the surface
* - `vec2 uv`, the texture coordinates associeted with the vertex
* - `vec4 color`, the per-vertex color
* The struct can be modified and returned.
*
* </td></tr>
* <tr><td>
*
* `vec2 getUV`
* `Vertex getWorldInputs`
*
* </td><td>
*
* Update the texture coordinates. It takes in `vec2 uv` and must return a modified version.
* Update the vertex data of the model being drawn after transformations such as `translate()` and `scale()` have been applied, but before the camera has been applied. It takes in a `Vertex` struct like, in the `getObjectInputs` hook above, that can be modified and returned.
*
* </td></tr>
* <tr><td>
*
* `vec4 getVertexColor`
* `Vertex getCameraInputs`
*
* </td><td>
*
* Update the color of each vertex. It takes in a `vec4 color` and must return a modified version.
* Update the vertex data of the model being drawn as they appear relative to the camera. It takes in a `Vertex` struct like, in the `getObjectInputs` hook above, that can be modified and returned.
*
* </td></tr>
* <tr><td>
Expand Down Expand Up @@ -1342,9 +1320,10 @@ function material(p5, fn){
* uniforms: {
* 'float time': () => millis()
* },
* 'vec3 getWorldPosition': `(vec3 pos) {
* pos.y += 20.0 * sin(time * 0.001 + pos.x * 0.05);
* return pos;
* 'Vertex getWorldInputs': `(Vertex inputs) {
* inputs.position.y +=
* 20.0 * sin(time * 0.001 + inputs.position.x * 0.05);
* return inputs;
* }`
* });
* }
Expand Down
9 changes: 3 additions & 6 deletions src/webgl/p5.RendererGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -1809,12 +1809,9 @@ class RendererGL extends Renderer {
{
vertex: {
"void beforeVertex": "() {}",
"vec3 getLocalPosition": "(vec3 position) { return position; }",
"vec3 getWorldPosition": "(vec3 position) { return position; }",
"vec3 getLocalNormal": "(vec3 normal) { return normal; }",
"vec3 getWorldNormal": "(vec3 normal) { return normal; }",
"vec2 getUV": "(vec2 uv) { return uv; }",
"vec4 getVertexColor": "(vec4 color) { return color; }",
"Vertex getObjectInputs": "(Vertex inputs) { return inputs; }",
"Vertex getWorldInputs": "(Vertex inputs) { return inputs; }",
"Vertex getCameraInputs": "(Vertex inputs) { return inputs; }",
"void afterVertex": "() {}",
},
fragment: {
Expand Down
5 changes: 3 additions & 2 deletions src/webgl/p5.Shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ class Shader {
if (typeof IS_MINIFIED !== 'undefined') {
console.error(glError);
} else {
throw(glError);
p5._friendlyError(
`Yikes! An error occurred compiling the vertex shader:${glError}`
);
Expand Down Expand Up @@ -629,8 +630,8 @@ class Shader {
this.init();
}
}


/**
* Queries the active attributes for this shader and loads
* their names and locations into the attributes array.
Expand Down
52 changes: 42 additions & 10 deletions src/webgl/shaders/phong.vert
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ IN vec4 aVertexColor;
uniform vec3 uAmbientColor[5];

uniform mat4 uModelViewMatrix;
uniform mat4 uModelMatrix;
uniform mat4 uViewMatrix;
uniform mat4 uProjectionMatrix;
uniform mat3 uNormalMatrix;
uniform int uAmbientLightCount;
Expand All @@ -21,18 +23,48 @@ OUT vec3 vViewPosition;
OUT vec3 vAmbientColor;
OUT vec4 vColor;

struct Vertex {
vec3 position;
vec3 normal;
vec2 uv;
vec4 color;
};

void main(void) {
HOOK_beforeVertex();
vec4 viewModelPosition = vec4(HOOK_getWorldPosition(
(uModelViewMatrix * vec4(HOOK_getLocalPosition(aPosition), 1.0)).xyz
), 1.);

// Pass varyings to fragment shader
vViewPosition = viewModelPosition.xyz;
gl_Position = uProjectionMatrix * viewModelPosition;
Vertex inputs;
inputs.position = aPosition;
inputs.normal = aNormal;
inputs.uv = aTexCoord;
inputs.color = (uUseVertexColor && aVertexColor.x >= 0.0) ? aVertexColor : uMaterialColor;
#ifdef AUGMENTED_HOOK_getLocalInputs
inputs = HOOK_getObjectInputs(inputs);
#endif

#ifdef AUGMENTED_HOOK_getWorldInputs
inputs.position = (uModelMatrix * vec4(inputs.position, 1.)).xyz;
inputs = HOOK_getWorldInputs(inputs);
#endif

vNormal = HOOK_getWorldNormal(uNormalMatrix * HOOK_getLocalNormal(aNormal));
vTexCoord = HOOK_getUV(aTexCoord);
#ifdef AUGMENTED_HOOK_getObjectInputs
// Already multiplied by the object matrix, just apply view
inputs.position = (uViewMatrix * vec4(inputs.position, 1.)).xyz;
#else
// Apply both at once
inputs.position = (uModelViewMatrix * vec4(inputs.position, 1.)).xyz;
#endif
// TODO: do the same as above here?
inputs.normal = uNormalMatrix * inputs.normal;
#ifdef AUGMENTED_HOOK_getCameraInputs
inputs = HOOK_getWorldInputs(inputs);
#endif

// Pass varyings to fragment shader
vViewPosition = inputs.position;
vTexCoord = inputs.uv;
vNormal = inputs.normal;
vColor = inputs.color;

// TODO: this should be a uniform
vAmbientColor = vec3(0.0);
Expand All @@ -41,7 +73,7 @@ void main(void) {
vAmbientColor += uAmbientColor[i];
}
}
vColor = HOOK_getVertexColor(((uUseVertexColor && aVertexColor.x >= 0.0) ? aVertexColor : uMaterialColor));

gl_Position = uProjectionMatrix * vec4(inputs.position, 1.);
HOOK_afterVertex();
}

0 comments on commit 3ebde70

Please sign in to comment.