Camera effects in CanvasLayer (+Cromatic Aberration)

This commit is contained in:
Mario Brandao
2023-12-13 22:48:27 +01:00
parent 0fc19ef815
commit 1668629916
16 changed files with 224 additions and 90 deletions

View File

@ -0,0 +1,59 @@
/**
* Draws a filled circle
*/
float drawFullCircle(vec2 center, float radius, vec2 uv)
{
return (distance(center, uv) <= radius) ? 1. : 0.;
}
/**
* Draws a line circle
*/
float drawLineCircle(vec2 center, float radius, float stroke, vec2 uv)
{
return drawFullCircle(center, radius, uv)
- drawFullCircle(center, radius - stroke, uv);
}
/**
* Draws a line from p1 to p2
*
* @param p1 vec2
* @param p2 vec2
* @param stroke float
* @param uv float to receive uv from fragment function
* @param one_px float This param should receive SCREEN_PIXEL_SIZE.x, as SCREEN_PIXEL_SIZE
* cannot be accessed from outside fragment() function
*/
float drawLine(vec2 p1, vec2 p2, float stroke, vec2 uv, float one_px)
{
float r = 0.;
// get dist between points
float d = distance(p1, p2);
// get dist between current pixel and p1
float duv = distance(p1, uv);
//if point is on line, according to dist, it should match current uv
r = 1.-floor(1.-(stroke * one_px)+distance (mix(p1, p2, clamp(duv/d, 0., 1.)), uv));
return r;
}
/**
* Draws a triangle
*/
float drawTriangle(vec2 p1, vec2 p2, vec2 p3, float stroke, vec2 uv, float one_px) {
return drawLine(p1, p2, stroke, uv, one_px)
+ drawLine(p2, p3, stroke, uv, one_px)
+ drawLine(p3, p1, stroke, uv, one_px);
}
/**
* Draws a triangle with no baseline
*/
float drawArrowTriangle(vec2 p1, vec2 p2, vec2 p3, float stroke, vec2 uv, float one_px) {
return drawLine(p1, p2, stroke, uv, one_px)
+ drawLine(p3, p1, stroke, uv, one_px);
}

View File

@ -0,0 +1,53 @@
const vec2 CENTER = vec2(.5, .5);
/**
* Rotate a shape
*/
vec2 rotate(vec2 origin, vec2 destination, float angleInDegrees) {
float angle_radians = radians(angleInDegrees);
float cosTheta = cos(angle_radians);
float sinTheta = sin(angle_radians);
// Translate the destination to be relative to the origin
vec2 translatedDestination = destination - origin;
// Apply the rotation transformation
vec2 rotatedDestination;
rotatedDestination.x = cosTheta * translatedDestination.x - sinTheta * translatedDestination.y;
rotatedDestination.y = sinTheta * translatedDestination.x + cosTheta * translatedDestination.y;
// Translate the rotated destination back to its original position
rotatedDestination += origin;
return rotatedDestination;
}
/**
* atan2 as defined in other languages
*/
float atan2(float y, float x) {
if (x > 0.0) {
return atan(y / x);
} else if (x < 0.0) {
if (y >= 0.0) {
return atan(y / x) + PI;
} else {
return atan(y / x) - PI;
}
} else {
if (y > 0.0) {
return PI / 2.;
} else if (y < 0.0) {
return -PI / 2.;
} else {
return 0.0;
}
}
}
/**
* Angle in degrees between 2 coords
*/
float getAngleInDegrees(vec2 v1, vec2 v2) {
return atan2(v1.y - v2.y, v1.x - v2.x) * (180. / PI);
}