You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Translates a point in 2D space by a given offset// p: the original point// offset: the translation vectorvec2 translate(vec2 p, vec2 offset) {
return p - offset; // Shift the point by the offset
}
Rotate
// Rotates a point in 2D space around the origin// p: the original point// angle: the rotation angle in radiansvec2 rotate(vec2 p, float angle) {
float c =cos(angle); // Precompute cosinefloat s =sin(angle); // Precompute sinemat2 rotationMatrix =mat2(c, -s, s, c); // Create rotation matrixreturn rotationMatrix * p; // Apply rotation to the point
}
Scale
// Scales a point in 2D space// p: the original point// scale: the scale factorvec2 scale(vec2 p, vec2 scale) {
return p * scale; // Multiply the point by the scale factor
}
5. Shapes and Patterns
Stripes
// Creates a horizontal stripe pattern// uv: the input UV coordinates// width: the width of the stripesfloat stripes(vec2 uv, float width) {
returnstep(0.5, fract(uv.y / width)); // Creates alternating bands
}
Grid
// Creates a grid pattern// uv: the input UV coordinates// spacing: the spacing between grid linesfloat grid(vec2 uv, float spacing) {
vec2 gridLines =step(0.5, fract(uv / spacing)); // Creates lines in x and y directionsreturnmax(gridLines.x, gridLines.y); // Combine the lines into a grid
}
Checkerboard
// Creates a checkerboard pattern// uv: the input UV coordinatesfloat checkerboard(vec2 uv) {
vec2 check =floor(uv); // Get integer coordinatesreturnmod(check.x + check.y, 2.0); // Alternate between 0 and 1
}
Radial Patterns
// Creates a radial pattern (concentric circles)// uv: the input UV coordinates// frequency: the number of circlesfloat radialPattern(vec2 uv, float frequency) {
float r =length(uv); // Compute distance from the centerreturnfract(r * frequency); // Create repeating rings
}
Hexagonal or Triangular Tiling
// Creates a hexagonal tiling pattern// uv: the input UV coordinatesfloat hexTiling(vec2 uv) {
constfloat sqrt3 =1.73205080757; // Square root of 3 for hex grid spacing
uv.x += uv.y *0.5; // Offset x by half of y
uv.y *= sqrt3 *0.5; // Scale y by sqrt(3)/2vec2 hex =mod(uv, 1.0) -0.5; // Get local coordinates within a hex cellreturnlength(hex) -0.5; // Return distance to the cell edge
}