// Generated by tools/gen_shaders.py from p5.js src/webgl/shaders.
// DO NOT EDIT: re-run the script instead.
///|
/// basic.frag
let glsl_basic_frag : String =
#|IN vec4 vColor;
#|IN highp vec2 vVertTexCoord;
#|void main(void) {
#| HOOK_beforeFragment();
#| OUT_COLOR = HOOK_getFinalColor(vColor, vVertTexCoord);
#| OUT_COLOR.rgb *= OUT_COLOR.a; // Premultiply alpha before rendering
#| HOOK_afterFragment();
#|}
///|
/// filters/base.frag
let glsl_filters_base_frag : String =
#|precision highp float;
#|
#|uniform sampler2D tex0;
#|uniform vec2 canvasSize;
#|uniform vec2 texelSize;
#|
#|IN vec2 vTexCoord;
#|
#|struct FilterInputs {
#| vec2 texCoord;
#| vec2 canvasSize;
#| vec2 texelSize;
#|};
#|
#|void main(void) {
#| FilterInputs inputs;
#| inputs.texCoord = vTexCoord;
#| inputs.canvasSize = canvasSize;
#| inputs.texelSize = texelSize;
#| OUT_COLOR = HOOK_getColor(inputs, tex0);
#| OUT_COLOR.rgb *= outColor.a;
#|}
#|
///|
/// filters/base.vert
let glsl_filters_base_vert : String =
#|precision highp int;
#|
#|uniform mat4 uModelViewMatrix;
#|uniform mat4 uProjectionMatrix;
#|
#|IN vec3 aPosition;
#|IN vec2 aTexCoord;
#|OUT vec2 vTexCoord;
#|
#|void main() {
#| // transferring texcoords for the frag shader
#| vTexCoord = aTexCoord;
#|
#| // copy position with a fourth coordinate for projection (1.0 is normal)
#| vec4 positionVec4 = vec4(aPosition, 1.0);
#|
#| // project to 3D space
#| gl_Position = uProjectionMatrix * uModelViewMatrix * positionVec4;
#|}
#|
///|
/// font.frag
let glsl_font_frag : String =
#|#ifndef WEBGL2
#|#extension GL_OES_standard_derivatives : enable
#|#endif
#|
#|#if 0
#| // simulate integer math using floats
#| #define int float
#| #define ivec2 vec2
#| #define INT(x) float(x)
#|
#| int ifloor(float v) { return floor(v); }
#| ivec2 ifloor(vec2 v) { return floor(v); }
#|
#|#else
#| // use native integer math
#| precision highp int;
#| #define INT(x) x
#|
#| int ifloor(float v) { return int(v); }
#| int ifloor(int v) { return v; }
#| ivec2 ifloor(vec2 v) { return ivec2(v); }
#|
#|#endif
#|
#|uniform sampler2D uSamplerStrokes;
#|uniform sampler2D uSamplerRowStrokes;
#|uniform sampler2D uSamplerRows;
#|uniform sampler2D uSamplerColStrokes;
#|uniform sampler2D uSamplerCols;
#|
#|uniform ivec2 uStrokeImageSize;
#|uniform ivec2 uCellsImageSize;
#|uniform ivec2 uGridImageSize;
#|
#|uniform ivec2 uGridOffset;
#|uniform ivec2 uGridSize;
#|uniform vec4 uMaterialColor;
#|
#|IN vec2 vTexCoord;
#|
#|// some helper functions
#|int ROUND(float v) { return ifloor(v + 0.5); }
#|ivec2 ROUND(vec2 v) { return ifloor(v + 0.5); }
#|float saturate(float v) { return clamp(v, 0.0, 1.0); }
#|vec2 saturate(vec2 v) { return clamp(v, 0.0, 1.0); }
#|
#|int mul(float v1, int v2) {
#| return ifloor(v1 * float(v2));
#|}
#|
#|ivec2 mul(vec2 v1, ivec2 v2) {
#| return ifloor(v1 * vec2(v2) + 0.5);
#|}
#|
#|// unpack a 16-bit integer from a float vec2
#|int getInt16(vec2 v) {
#| ivec2 iv = ROUND(v * 255.0);
#| return iv.x * INT(128) + iv.y;
#|}
#|
#|vec2 pixelScale;
#|vec2 coverage = vec2(0.0);
#|vec2 weight = vec2(0.5);
#|const float minDistance = 1.0/8192.0;
#|const float hardness = 1.05; // amount of antialias
#|
#|// the maximum number of curves in a glyph
#|const int N = INT(250);
#|
#|// retrieves an indexed pixel from a sampler
#|vec4 getTexel(sampler2D sampler, int pos, ivec2 size) {
#| int width = size.x;
#| int y = ifloor(pos / width);
#| int x = pos - y * width; // pos % width
#|
#| return TEXTURE(sampler, (vec2(x, y) + 0.5) / vec2(size));
#|}
#|
#|void calulateCrossings(vec2 p0, vec2 p1, vec2 p2, out vec2 C1, out vec2 C2) {
#|
#| // get the coefficients of the quadratic in t
#| vec2 a = p0 - p1 * 2.0 + p2;
#| vec2 b = p0 - p1;
#| vec2 c = p0 - vTexCoord;
#|
#| // found out which values of 't' it crosses the axes
#| vec2 surd = sqrt(max(vec2(0.0), b * b - a * c));
#| vec2 t1 = ((b - surd) / a).yx;
#| vec2 t2 = ((b + surd) / a).yx;
#|
#| // approximate straight lines to avoid rounding errors
#| if (abs(a.y) < 0.001)
#| t1.x = t2.x = c.y / (2.0 * b.y);
#|
#| if (abs(a.x) < 0.001)
#| t1.y = t2.y = c.x / (2.0 * b.x);
#|
#| // plug into quadratic formula to find the corrdinates of the crossings
#| C1 = ((a * t1 - b * 2.0) * t1 + c) * pixelScale;
#| C2 = ((a * t2 - b * 2.0) * t2 + c) * pixelScale;
#|}
#|
#|void coverageX(vec2 p0, vec2 p1, vec2 p2) {
#|
#| vec2 C1, C2;
#| calulateCrossings(p0, p1, p2, C1, C2);
#|
#| // determine on which side of the x-axis the points lie
#| bool y0 = p0.y > vTexCoord.y;
#| bool y1 = p1.y > vTexCoord.y;
#| bool y2 = p2.y > vTexCoord.y;
#|
#| // could web be under the curve (after t1)?
#| if (y1 ? !y2 : y0) {
#| // add the coverage for t1
#| coverage.x += saturate(C1.x + 0.5);
#| // calculate the anti-aliasing for t1
#| weight.x = min(weight.x, abs(C1.x));
#| }
#|
#| // are we outside the curve (after t2)?
#| if (y1 ? !y0 : y2) {
#| // subtract the coverage for t2
#| coverage.x -= saturate(C2.x + 0.5);
#| // calculate the anti-aliasing for t2
#| weight.x = min(weight.x, abs(C2.x));
#| }
#|}
#|
#|// this is essentially the same as coverageX, but with the axes swapped
#|void coverageY(vec2 p0, vec2 p1, vec2 p2) {
#|
#| vec2 C1, C2;
#| calulateCrossings(p0, p1, p2, C1, C2);
#|
#| bool x0 = p0.x > vTexCoord.x;
#| bool x1 = p1.x > vTexCoord.x;
#| bool x2 = p2.x > vTexCoord.x;
#|
#| if (x1 ? !x2 : x0) {
#| coverage.y -= saturate(C1.y + 0.5);
#| weight.y = min(weight.y, abs(C1.y));
#| }
#|
#| if (x1 ? !x0 : x2) {
#| coverage.y += saturate(C2.y + 0.5);
#| weight.y = min(weight.y, abs(C2.y));
#| }
#|}
#|
#|void main() {
#|
#| // calculate the pixel scale based on screen-coordinates
#| pixelScale = hardness / fwidth(vTexCoord);
#|
#| // which grid cell is this pixel in?
#| ivec2 gridCoord = ifloor(vTexCoord * vec2(uGridSize));
#|
#| // intersect curves in this row
#| {
#| // the index into the row info bitmap
#| int rowIndex = gridCoord.y + uGridOffset.y;
#| // fetch the info texel
#| vec4 rowInfo = getTexel(uSamplerRows, rowIndex, uGridImageSize);
#| // unpack the rowInfo
#| int rowStrokeIndex = getInt16(rowInfo.xy);
#| int rowStrokeCount = getInt16(rowInfo.zw);
#|
#| for (int iRowStroke = INT(0); iRowStroke < N; iRowStroke++) {
#| if (iRowStroke >= rowStrokeCount)
#| break;
#|
#| // each stroke is made up of 3 points: the start and control point
#| // and the start of the next curve.
#| // fetch the indices of this pair of strokes:
#| vec4 strokeIndices = getTexel(uSamplerRowStrokes, rowStrokeIndex++, uCellsImageSize);
#|
#| // unpack the stroke index
#| int strokePos = getInt16(strokeIndices.xy);
#|
#| // fetch the two strokes
#| vec4 stroke0 = getTexel(uSamplerStrokes, strokePos + INT(0), uStrokeImageSize);
#| vec4 stroke1 = getTexel(uSamplerStrokes, strokePos + INT(1), uStrokeImageSize);
#|
#| // calculate the coverage
#| coverageX(stroke0.xy, stroke0.zw, stroke1.xy);
#| }
#| }
#|
#| // intersect curves in this column
#| {
#| int colIndex = gridCoord.x + uGridOffset.x;
#| vec4 colInfo = getTexel(uSamplerCols, colIndex, uGridImageSize);
#| int colStrokeIndex = getInt16(colInfo.xy);
#| int colStrokeCount = getInt16(colInfo.zw);
#|
#| for (int iColStroke = INT(0); iColStroke < N; iColStroke++) {
#| if (iColStroke >= colStrokeCount)
#| break;
#|
#| vec4 strokeIndices = getTexel(uSamplerColStrokes, colStrokeIndex++, uCellsImageSize);
#|
#| int strokePos = getInt16(strokeIndices.xy);
#| vec4 stroke0 = getTexel(uSamplerStrokes, strokePos + INT(0), uStrokeImageSize);
#| vec4 stroke1 = getTexel(uSamplerStrokes, strokePos + INT(1), uStrokeImageSize);
#| coverageY(stroke0.xy, stroke0.zw, stroke1.xy);
#| }
#| }
#|
#| weight = saturate(1.0 - weight * 2.0);
#| float distance = max(weight.x + weight.y, minDistance); // manhattan approx.
#| float antialias = abs(dot(coverage, weight) / distance);
#| float cover = min(abs(coverage.x), abs(coverage.y));
#| OUT_COLOR = vec4(uMaterialColor.rgb, 1.) * uMaterialColor.a;
#| OUT_COLOR *= saturate(max(antialias, cover));
#|}
#|
///|
/// font.vert
let glsl_font_vert : String =
#|IN vec3 aPosition;
#|IN vec2 aTexCoord;
#|uniform mat4 uModelViewMatrix;
#|uniform mat4 uProjectionMatrix;
#|
#|uniform vec4 uGlyphRect;
#|uniform float uGlyphOffset;
#|
#|OUT vec2 vTexCoord;
#|
#|void main() {
#| vec4 positionVec4 = vec4(aPosition, 1.0);
#|
#| // scale by the size of the glyph's rectangle
#| positionVec4.xy *= uGlyphRect.zw - uGlyphRect.xy;
#|
#| // Expand glyph bounding boxes by 1px on each side to give a bit of room
#| // for antialiasing
#| vec3 newOrigin = (uModelViewMatrix * vec4(0., 0., 0., 1.)).xyz;
#| vec3 newDX = (uModelViewMatrix * vec4(1., 0., 0., 1.)).xyz;
#| vec3 newDY = (uModelViewMatrix * vec4(0., 1., 0., 1.)).xyz;
#| vec2 pixelScale = vec2(
#| 1. / length(newOrigin - newDX),
#| 1. / length(newOrigin - newDY)
#| );
#| vec2 offset = pixelScale * normalize(aTexCoord - vec2(0.5, 0.5));
#| vec2 textureOffset = offset * (1. / vec2(
#| uGlyphRect.z - uGlyphRect.x,
#| uGlyphRect.w - uGlyphRect.y
#| ));
#|
#| // move to the corner of the glyph
#| positionVec4.xy += uGlyphRect.xy;
#|
#| // move to the letter's line offset
#| positionVec4.x += uGlyphOffset;
#|
#| positionVec4.xy += offset;
#|
#| gl_Position = uProjectionMatrix * uModelViewMatrix * positionVec4;
#| vTexCoord = aTexCoord + textureOffset;
#|}
#|
///|
/// functions/noise3DGLSL.glsl
let glsl_functions_noise3dglsl_glsl : String =
#|// Based on https://github.com/stegu/webgl-noise/blob/22434e04d7753f7e949e8d724ab3da2864c17a0f/src/noise3D.glsl
#|// MIT licensed, adapted for p5.strands
#|
#|vec3 mod289(vec3 x) {
#| return x - floor(x * (1.0 / 289.0)) * 289.0;
#|}
#|
#|vec4 mod289(vec4 x) {
#| return x - floor(x * (1.0 / 289.0)) * 289.0;
#|}
#|
#|vec4 permute(vec4 x) {
#| return mod289(((x*34.0)+10.0)*x);
#|}
#|
#|vec4 taylorInvSqrt(vec4 r)
#|{
#| return 1.79284291400159 - 0.85373472095314 * r;
#|}
#|
#|float baseNoise(vec3 v)
#|{
#| const vec2 C = vec2(1.0/6.0, 1.0/3.0) ;
#| const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);
#|
#| // First corner
#| vec3 i = floor(v + dot(v, C.yyy) );
#| vec3 x0 = v - i + dot(i, C.xxx) ;
#|
#| // Other corners
#| vec3 g = step(x0.yzx, x0.xyz);
#| vec3 l = 1.0 - g;
#| vec3 i1 = min( g.xyz, l.zxy );
#| vec3 i2 = max( g.xyz, l.zxy );
#|
#| // x0 = x0 - 0.0 + 0.0 * C.xxx;
#| // x1 = x0 - i1 + 1.0 * C.xxx;
#| // x2 = x0 - i2 + 2.0 * C.xxx;
#| // x3 = x0 - 1.0 + 3.0 * C.xxx;
#| vec3 x1 = x0 - i1 + C.xxx;
#| vec3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
#| vec3 x3 = x0 - D.yyy; // -1.0+3.0*C.x = -0.5 = -D.y
#|
#| // Permutations
#| i = mod289(i);
#| vec4 p = permute( permute( permute(
#| i.z + vec4(0.0, i1.z, i2.z, 1.0 ))
#| + i.y + vec4(0.0, i1.y, i2.y, 1.0 ))
#| + i.x + vec4(0.0, i1.x, i2.x, 1.0 ));
#|
#| // Gradients: 7x7 points over a square, mapped onto an octahedron.
#| // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
#| float n_ = 0.142857142857; // 1.0/7.0
#| vec3 ns = n_ * D.wyz - D.xzx;
#|
#| vec4 j = p - 49.0 * floor(p * ns.z * ns.z); // mod(p,7*7)
#|
#| vec4 x_ = floor(j * ns.z);
#| vec4 y_ = floor(j - 7.0 * x_ ); // mod(j,N)
#|
#| vec4 x = x_ *ns.x + ns.yyyy;
#| vec4 y = y_ *ns.x + ns.yyyy;
#| vec4 h = 1.0 - abs(x) - abs(y);
#|
#| vec4 b0 = vec4( x.xy, y.xy );
#| vec4 b1 = vec4( x.zw, y.zw );
#|
#| //vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0;
#| //vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0;
#| vec4 s0 = floor(b0)*2.0 + 1.0;
#| vec4 s1 = floor(b1)*2.0 + 1.0;
#| vec4 sh = -step(h, vec4(0.0));
#|
#| vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
#| vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
#|
#| vec3 p0 = vec3(a0.xy,h.x);
#| vec3 p1 = vec3(a0.zw,h.y);
#| vec3 p2 = vec3(a1.xy,h.z);
#| vec3 p3 = vec3(a1.zw,h.w);
#|
#| //Normalise gradients
#| vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
#| p0 *= norm.x;
#| p1 *= norm.y;
#| p2 *= norm.z;
#| p3 *= norm.w;
#|
#| // Mix final noise value
#| vec4 m = max(0.5 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
#| m = m * m;
#| return 105.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1),
#| dot(p2,x2), dot(p3,x3) ) );
#|}
#|
#|float noise(vec3 st, int octaves, float ampFalloff) {
#| float result = 0.0;
#| float amplitude = 1.0;
#| float frequency = 1.0;
#|
#| for (int i = 0; i < 8; i++) {
#| if (i >= octaves) break;
#| result += amplitude * baseNoise(st * frequency);
#| frequency *= 2.0;
#| amplitude *= ampFalloff;
#| }
#| return (result + 1.0) * 0.5;
#|}
#|
///|
/// functions/randomGLSL.glsl
let glsl_functions_randomglsl_glsl : String =
#|// _p5_hash: "Hash without Sine" by Dave Hoskins (https://www.shadertoy.com/view/4djSRW)
#|// Mixing constants: R₂ sequence by Martin Roberts (https://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/)
#|// α₁ = 1/φ₂ = 0.7548776662 (plastic constant reciprocal)
#|// α₂ = 1/φ₂² = 0.5698402910
#|// 1/φ = 0.6180339887 (golden ratio conjugate)
#|
#|int _p5_randomCallIndex = 0;
#|
#|float _p5_hash(vec3 p) {
#| p = fract(p * vec3(0.1031, 0.1030, 0.0973));
#| p += dot(p, p.yxz + 33.33);
#| return fract((p.x + p.y) * p.z);
#|}
#|
#|float random(float seed) {
#| vec2 pixelCoord = gl_FragCoord.xy;
#| float callIndex = float(_p5_randomCallIndex);
#| _p5_randomCallIndex += 1;
#| // fract(seed * α₁) normalizes large seeds (e.g. performance.now()) into [0,1)
#| // and spreads them optimally via the R₂ sequence's plastic constant
#| float s = fract(seed * 0.7548776662);
#| return _p5_hash(vec3(
#| pixelCoord.x + s,
#| pixelCoord.y + callIndex * 0.5698402910,
#| s + callIndex * 0.6180339887
#| ));
#|}
#|
///|
/// functions/randomVertGLSL.glsl
let glsl_functions_randomvertglsl_glsl : String =
#|// _p5_hash: "Hash without Sine" by Dave Hoskins (https://www.shadertoy.com/view/4djSRW)
#|// Mixing constants: R₂ sequence by Martin Roberts (https://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/)
#|// α₁ = 1/φ₂ = 0.7548776662 (plastic constant reciprocal)
#|// α₂ = 1/φ₂² = 0.5698402910
#|// 1/φ = 0.6180339887 (golden ratio conjugate)
#|
#|int _p5_randomCallIndex = 0;
#|
#|float _p5_hash(vec3 p) {
#| p = fract(p * vec3(0.1031, 0.1030, 0.0973));
#| p += dot(p, p.yxz + 33.33);
#| return fract((p.x + p.y) * p.z);
#|}
#|
#|float random(float seed) {
#| float vid = float(gl_VertexID);
#| float callIndex = float(_p5_randomCallIndex);
#| _p5_randomCallIndex += 1;
#| float s = fract(seed * 0.7548776662);
#| return _p5_hash(vec3(
#| vid + s,
#| vid * 0.5698402910 + callIndex * 0.6180339887,
#| s + callIndex * 0.7548776662
#| ));
#|}
#|
///|
/// imageLight.vert
let glsl_imagelight_vert : String =
#|precision highp float;
#|IN vec3 aPosition;
#|IN vec3 aNormal;
#|IN vec2 aTexCoord;
#|
#|OUT vec3 localPos;
#|OUT vec3 vWorldNormal;
#|OUT vec3 vWorldPosition;
#|OUT vec2 vTexCoord;
#|
#|uniform mat4 uModelViewMatrix;
#|uniform mat4 uProjectionMatrix;
#|uniform mat3 uNormalMatrix;
#|
#|void main() {
#| // Multiply the position by the matrix.
#| vec4 viewModelPosition = uModelViewMatrix * vec4(aPosition, 1.0);
#| gl_Position = uProjectionMatrix * viewModelPosition;
#|
#| // orient the normals and pass to the fragment shader
#| vWorldNormal = uNormalMatrix * aNormal;
#|
#| // send the view position to the fragment shader
#| vWorldPosition = (uModelViewMatrix * vec4(aPosition, 1.0)).xyz;
#|
#| localPos = vWorldPosition;
#| vTexCoord = aTexCoord;
#|}
#|
#|
#|/*
#|in the vertex shader we'll compute the world position and world oriented normal of the vertices and pass those to the fragment shader as varyings.
#|*/
#|
///|
/// imageLightDiffused.frag
let glsl_imagelightdiffused_frag : String =
#|precision highp float;
#|IN vec3 localPos;
#|
#|// the HDR cubemap converted (can be from an equirectangular environment map.)
#|uniform sampler2D environmentMap;
#|IN vec2 vTexCoord;
#|
#|const float PI = 3.14159265359;
#|
#|vec2 nTOE( vec3 v ){
#| // x = r sin(phi) cos(theta)
#| // y = r cos(phi)
#| // z = r sin(phi) sin(theta)
#| float phi = acos( v.y );
#| // if phi is 0, then there are no x, z components
#| float theta = 0.0;
#| // else
#| theta = acos(v.x / sin(phi));
#| float sinTheta = v.z / sin(phi);
#| if (sinTheta < 0.0) {
#| // Turn it into -theta, but in the 0-2PI range
#| theta = 2.0 * PI - theta;
#| }
#| theta = theta / (2.0 * 3.14159);
#| phi = phi / 3.14159 ;
#|
#| vec2 angles = vec2( phi, theta );
#| return angles;
#|}
#|
#|float random(vec2 p) {
#| vec3 p3 = fract(vec3(p.xyx) * .1031);
#| p3 += dot(p3, p3.yzx + 33.33);
#| return fract((p3.x + p3.y) * p3.z);
#|}
#|
#|void main()
#|{
#| // the sample direction equals the hemisphere's orientation
#| float phi = vTexCoord.x * 2.0 * PI;
#| float theta = vTexCoord.y * PI;
#| float x = sin(theta) * cos(phi);
#| float y = sin(theta) * sin(phi);
#| float z = cos(theta);
#| vec3 normal = vec3( x, y, z);
#|
#| // Discretely sampling the hemisphere given the integral's
#| // spherical coordinates translates to the following fragment code:
#| vec3 irradiance = vec3(0.0);
#| vec3 up = vec3(0.0, 1.0, 0.0);
#| vec3 right = normalize(cross(up, normal));
#| up = normalize(cross(normal, right));
#|
#| // We specify a fixed sampleDelta delta value to traverse
#| // the hemisphere; decreasing or increasing the sample delta
#| // will increase or decrease the accuracy respectively.
#| const float sampleDelta = 0.100;
#| float nrSamples = 0.0;
#| float randomOffset = random(gl_FragCoord.xy) * sampleDelta;
#| for(float rawPhi = 0.0; rawPhi < 2.0 * PI; rawPhi += sampleDelta)
#| {
#| float phi = rawPhi + randomOffset;
#| for(float rawTheta = 0.0; rawTheta < ( 0.5 ) * PI; rawTheta += sampleDelta)
#| {
#| float theta = rawTheta + randomOffset;
#| // spherical to cartesian (in tangent space) // tangent space to world // add each sample result to irradiance
#| float x = sin(theta) * cos(phi);
#| float y = sin(theta) * sin(phi);
#| float z = cos(theta);
#| vec3 tangentSample = vec3( x, y, z);
#|
#| vec3 sampleVec = tangentSample.x * right + tangentSample.y * up + tangentSample.z * normal;
#| irradiance += (TEXTURE(environmentMap, nTOE(sampleVec)).xyz) * cos(theta) * sin(theta);
#| nrSamples++;
#| }
#| }
#| // divide by the total number of samples taken, giving us the average sampled irradiance.
#| irradiance = PI * irradiance * (1.0 / float(nrSamples )) ;
#|
#|
#| OUT_COLOR = vec4(irradiance, 1.0);
#|}
///|
/// imageLightSpecular.frag
let glsl_imagelightspecular_frag : String =
#|precision highp float;
#|IN vec3 localPos;
#|IN vec2 vTexCoord;
#|
#|// our texture
#|uniform sampler2D environmentMap;
#|uniform float roughness;
#|
#|const float PI = 3.14159265359;
#|
#|float VanDerCorput(int bits);
#|vec2 HammersleyNoBitOps(int i, int N);
#|vec3 ImportanceSampleGGX(vec2 Xi, vec3 N, float roughness);
#|
#|
#|vec2 nTOE( vec3 v ){
#| // x = r sin(phi) cos(theta)
#| // y = r cos(phi)
#| // z = r sin(phi) sin(theta)
#| float phi = acos( v.y );
#| // if phi is 0, then there are no x, z components
#| float theta = 0.0;
#| // else
#| theta = acos(v.x / sin(phi));
#| float sinTheta = v.z / sin(phi);
#| if (sinTheta < 0.0) {
#| // Turn it into -theta, but in the 0-2PI range
#| theta = 2.0 * PI - theta;
#| }
#| theta = theta / (2.0 * 3.14159);
#| phi = phi / 3.14159 ;
#|
#| vec2 angles = vec2( phi, theta );
#| return angles;
#|}
#|
#|
#|void main(){
#| const int SAMPLE_COUNT = 400; // 4096
#| int lowRoughnessLimit = int(pow(2.0,(roughness+0.1)*20.0));
#| float totalWeight = 0.0;
#| vec3 prefilteredColor = vec3(0.0);
#| float phi = vTexCoord.x * 2.0 * PI;
#| float theta = vTexCoord.y * PI;
#| float x = sin(theta) * cos(phi);
#| float y = sin(theta) * sin(phi);
#| float z = cos(theta);
#| vec3 N = vec3(x,y,z);
#| vec3 V = N;
#| for (int i = 0; i < SAMPLE_COUNT; ++i)
#| {
#| // break at smaller sample numbers for low roughness levels
#| if(i == lowRoughnessLimit)
#| {
#| break;
#| }
#| vec2 Xi = HammersleyNoBitOps(i, SAMPLE_COUNT);
#| vec3 H = ImportanceSampleGGX(Xi, N, roughness);
#| vec3 L = normalize(2.0 * dot(V, H) * H - V);
#|
#| float NdotL = max(dot(N, L), 0.0);
#| if (NdotL > 0.0)
#| {
#| prefilteredColor += TEXTURE(environmentMap, nTOE(L)).xyz * NdotL;
#| totalWeight += NdotL;
#| }
#| }
#| prefilteredColor = prefilteredColor / totalWeight;
#|
#| OUT_COLOR = vec4(prefilteredColor, 1.0);
#|}
#|
#|vec3 ImportanceSampleGGX(vec2 Xi, vec3 N, float roughness){
#| float a = roughness * roughness;
#|
#| float phi = 2.0 * PI * Xi.x;
#| float cosTheta = sqrt((1.0 - Xi.y) / (1.0 + (a * a - 1.0) * Xi.y));
#| float sinTheta = sqrt(1.0 - cosTheta * cosTheta);
#| // from spherical coordinates to cartesian coordinates
#| vec3 H;
#| H.x = cos(phi) * sinTheta;
#| H.y = sin(phi) * sinTheta;
#| H.z = cosTheta;
#|
#| // from tangent-space vector to world-space sample vector
#| vec3 up = abs(N.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);
#| vec3 tangent = normalize(cross(up, N));
#| vec3 bitangent = cross(N, tangent);
#|
#| vec3 sampleVec = tangent * H.x + bitangent * H.y + N * H.z;
#| return normalize(sampleVec);
#|}
#|
#|
#|float VanDerCorput(int n, int base)
#|{
#|#ifdef WEBGL2
#|
#| uint bits = uint(n);
#| bits = (bits << 16u) | (bits >> 16u);
#| bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
#| bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
#| bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
#| bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
#| return float(bits) * 2.3283064365386963e-10; // / 0x100000000
#|
#|#else
#|
#| float invBase = 1.0 / float(base);
#| float denom = 1.0;
#| float result = 0.0;
#|
#|
#| for (int i = 0; i < 32; ++i)
#| {
#| if (n > 0)
#| {
#| denom = mod(float(n), 2.0);
#| result += denom * invBase;
#| invBase = invBase / 2.0;
#| n = int(float(n) / 2.0);
#| }
#| }
#|
#|
#| return result;
#|
#|#endif
#|}
#|
#|vec2 HammersleyNoBitOps(int i, int N)
#|{
#| return vec2(float(i) / float(N), VanDerCorput(i, 2));
#|}
#|
///|
/// light.vert
let glsl_light_vert : String =
#|// include lighting.glgl
#|
#|IN vec3 aPosition;
#|IN vec3 aNormal;
#|IN vec2 aTexCoord;
#|IN vec4 aVertexColor;
#|
#|uniform mat4 uModelViewMatrix;
#|uniform mat4 uProjectionMatrix;
#|uniform mat3 uNormalMatrix;
#|
#|uniform bool uUseVertexColor;
#|uniform vec4 uMaterialColor;
#|
#|OUT highp vec2 vVertTexCoord;
#|OUT vec3 vDiffuseColor;
#|OUT vec3 vSpecularColor;
#|OUT vec4 vColor;
#|
#|void main(void) {
#|
#| vec4 viewModelPosition = uModelViewMatrix * vec4(aPosition, 1.0);
#| gl_Position = uProjectionMatrix * viewModelPosition;
#|
#| vec3 vertexNormal = normalize(uNormalMatrix * aNormal);
#| vVertTexCoord = aTexCoord;
#|
#| totalLight(viewModelPosition.xyz, vertexNormal, vDiffuseColor, vSpecularColor);
#|
#| for (int i = 0; i < 8; i++) {
#| if (i < uAmbientLightCount) {
#| vDiffuseColor += uAmbientColor[i];
#| }
#| }
#|
#| vColor = ((uUseVertexColor && aVertexColor.x >= 0.0) ? aVertexColor : uMaterialColor);
#|}
#|
///|
/// light_texture.frag
let glsl_light_texture_frag : String =
#|uniform vec4 uTint;
#|uniform sampler2D uSampler;
#|uniform bool isTexture;
#|uniform bool uEmissive;
#|
#|IN highp vec2 vVertTexCoord;
#|IN vec3 vDiffuseColor;
#|IN vec3 vSpecularColor;
#|IN vec4 vColor;
#|
#|void main(void) {
#| if(uEmissive && !isTexture) {
#| OUT_COLOR = vColor;
#| }
#| else {
#| vec4 baseColor = isTexture
#| // Textures come in with premultiplied alpha. To apply tint and still have
#| // premultiplied alpha output, we need to multiply the RGB channels by the
#| // tint RGB, and all channels by the tint alpha.
#| ? TEXTURE(uSampler, vVertTexCoord) * vec4(uTint.rgb/255., 1.) * (uTint.a/255.)
#| // Colors come in with unmultiplied alpha, so we need to multiply the RGB
#| // channels by alpha to convert it to premultiplied alpha.
#| : vec4(vColor.rgb * vColor.a, vColor.a);
#| OUT_COLOR = vec4(baseColor.rgb * vDiffuseColor + vSpecularColor, baseColor.a);
#| }
#|}
#|
///|
/// lighting.glsl
let glsl_lighting_glsl : String =
#|#define PI 3.141592
#|
#|precision highp float;
#|precision highp int;
#|
#|uniform mat4 uViewMatrix;
#|uniform mat3 uCameraNormalMatrix;
#|
#|uniform bool uUseLighting;
#|
#|uniform int uDirectionalLightCount;
#|uniform vec3 uLightingDirection[5];
#|uniform vec3 uDirectionalDiffuseColors[5];
#|uniform vec3 uDirectionalSpecularColors[5];
#|
#|uniform int uPointLightCount;
#|uniform vec3 uPointLightLocation[5];
#|uniform vec3 uPointLightDiffuseColors[5];
#|uniform vec3 uPointLightSpecularColors[5];
#|
#|uniform int uSpotLightCount;
#|uniform float uSpotLightAngle[5];
#|uniform float uSpotLightConc[5];
#|uniform vec3 uSpotLightDiffuseColors[5];
#|uniform vec3 uSpotLightSpecularColors[5];
#|uniform vec3 uSpotLightLocation[5];
#|uniform vec3 uSpotLightDirection[5];
#|
#|uniform bool uSpecular;
#|uniform float uShininess;
#|uniform float uMetallic;
#|
#|uniform float uConstantAttenuation;
#|uniform float uLinearAttenuation;
#|uniform float uQuadraticAttenuation;
#|
#|// setting from _setImageLightUniforms()
#|// boolean to initiate the calculateImageDiffuse and calculateImageSpecular
#|uniform bool uUseImageLight;
#|// texture for use in calculateImageDiffuse
#|uniform sampler2D environmentMapDiffused;
#|// texture for use in calculateImageSpecular
#|uniform sampler2D environmentMapSpecular;
#|
#|const float specularFactor = 2.0;
#|const float diffuseFactor = 0.73;
#|
#|struct LightResult {
#| float specular;
#| float diffuse;
#|};
#|
#|float _phongSpecular(
#| vec3 lightDirection,
#| vec3 viewDirection,
#| vec3 surfaceNormal,
#| float shininess) {
#|
#| vec3 R = reflect(lightDirection, surfaceNormal);
#| return pow(max(0.0, dot(R, viewDirection)), shininess);
#|}
#|
#|float _lambertDiffuse(vec3 lightDirection, vec3 surfaceNormal) {
#| return max(0.0, dot(-lightDirection, surfaceNormal));
#|}
#|
#|LightResult _light(vec3 viewDirection, vec3 normal, vec3 lightVector, float shininess, float metallic) {
#|
#| vec3 lightDir = normalize(lightVector);
#|
#| //compute our diffuse & specular terms
#| LightResult lr;
#| float specularIntensity = mix(1.0, 0.4, metallic);
#| float diffuseIntensity = mix(1.0, 0.1, metallic);
#| if (uSpecular)
#| lr.specular = (_phongSpecular(lightDir, viewDirection, normal, shininess)) * specularIntensity;
#| lr.diffuse = _lambertDiffuse(lightDir, normal) * diffuseIntensity;
#| return lr;
#|}
#|
#|// converts the range of "value" from [min1 to max1] to [min2 to max2]
#|float map(float value, float min1, float max1, float min2, float max2) {
#| return min2 + (value - min1) * (max2 - min2) / (max1 - min1);
#|}
#|
#|vec2 mapTextureToNormal( vec3 v ){
#| // x = r sin(phi) cos(theta)
#| // y = r cos(phi)
#| // z = r sin(phi) sin(theta)
#| float phi = acos( v.y );
#| // if phi is 0, then there are no x, z components
#| float theta = 0.0;
#| // else
#| theta = acos(v.x / sin(phi));
#| float sinTheta = v.z / sin(phi);
#| if (sinTheta < 0.0) {
#| // Turn it into -theta, but in the 0-2PI range
#| theta = 2.0 * PI - theta;
#| }
#| theta = theta / (2.0 * 3.14159);
#| phi = phi / 3.14159 ;
#|
#| vec2 angles = vec2( fract(theta + 0.25), 1.0 - phi );
#| return angles;
#|}
#|
#|
#|vec3 calculateImageDiffuse(vec3 vNormal, vec3 vViewPosition, float metallic){
#| // make 2 seperate builds
#| vec3 worldCameraPosition = vec3(0.0, 0.0, 0.0); // hardcoded world camera position
#| vec3 worldNormal = normalize(vNormal * uCameraNormalMatrix);
#| vec2 newTexCoor = mapTextureToNormal( worldNormal );
#| vec4 texture = TEXTURE( environmentMapDiffused, newTexCoor );
#| // this is to make the darker sections more dark
#| // png and jpg usually flatten the brightness so it is to reverse that
#| return mix(smoothstep(vec3(0.0), vec3(1.0), texture.xyz), vec3(0.0), metallic);
#|}
#|
#|vec3 calculateImageSpecular(vec3 vNormal, vec3 vViewPosition, float shininess, float metallic){
#| vec3 worldCameraPosition = vec3(0.0, 0.0, 0.0);
#| vec3 worldNormal = normalize(vNormal);
#| vec3 lightDirection = normalize( vViewPosition - worldCameraPosition );
#| vec3 R = reflect(lightDirection, worldNormal) * uCameraNormalMatrix;
#| vec2 newTexCoor = mapTextureToNormal( R );
#|#ifdef WEBGL2
#| // In p5js the range of shininess is >= 1,
#| // Therefore roughness range will be ([0,1]*8)*20 or [0, 160]
#| // The factor of 8 is because currently the getSpecularTexture
#| // only calculated 8 different levels of roughness
#| // The factor of 20 is just to spread up this range so that,
#| // [1, max] of shininess is converted to [0,160] of roughness
#| float roughness = 20. / shininess;
#| vec4 outColor = textureLod(environmentMapSpecular, newTexCoor, roughness * 8.);
#|#else
#| vec4 outColor = TEXTURE(environmentMapSpecular, newTexCoor);
#|#endif
#| // this is to make the darker sections more dark
#| // png and jpg usually flatten the brightness so it is to reverse that
#| return mix(
#| pow(outColor.xyz, vec3(10)),
#| pow(outColor.xyz, vec3(1.2)),
#| metallic
#| );
#|}
#|
#|void totalLight(
#| vec3 modelPosition,
#| vec3 normal,
#| float shininess,
#| float metallic,
#| out vec3 totalDiffuse,
#| out vec3 totalSpecular
#|) {
#|
#| totalSpecular = vec3(0.0);
#|
#| if (!uUseLighting) {
#| totalDiffuse = vec3(1.0);
#| return;
#| }
#|
#| totalDiffuse = vec3(0.0);
#|
#| vec3 viewDirection = normalize(-modelPosition);
#|
#| for (int j = 0; j < 5; j++) {
#| if (j < uDirectionalLightCount) {
#| vec3 lightVector = (uViewMatrix * vec4(uLightingDirection[j], 0.0)).xyz;
#| vec3 lightColor = uDirectionalDiffuseColors[j];
#| vec3 specularColor = uDirectionalSpecularColors[j];
#| LightResult result = _light(viewDirection, normal, lightVector, shininess, metallic);
#| totalDiffuse += result.diffuse * lightColor;
#| totalSpecular += result.specular * lightColor * specularColor;
#| }
#|
#| if (j < uPointLightCount) {
#| vec3 lightPosition = (uViewMatrix * vec4(uPointLightLocation[j], 1.0)).xyz;
#| vec3 lightVector = modelPosition - lightPosition;
#| //calculate attenuation
#| float lightDistance = length(lightVector);
#| float lightFalloff = 1.0 / (uConstantAttenuation + lightDistance * uLinearAttenuation + (lightDistance * lightDistance) * uQuadraticAttenuation);
#| vec3 lightColor = lightFalloff * uPointLightDiffuseColors[j];
#| vec3 specularColor = lightFalloff * uPointLightSpecularColors[j];
#|
#| LightResult result = _light(viewDirection, normal, lightVector, shininess, metallic);
#| totalDiffuse += result.diffuse * lightColor;
#| totalSpecular += result.specular * lightColor * specularColor;
#| }
#|
#| if(j < uSpotLightCount) {
#| vec3 lightPosition = (uViewMatrix * vec4(uSpotLightLocation[j], 1.0)).xyz;
#| vec3 lightVector = modelPosition - lightPosition;
#|
#| float lightDistance = length(lightVector);
#| float lightFalloff = 1.0 / (uConstantAttenuation + lightDistance * uLinearAttenuation + (lightDistance * lightDistance) * uQuadraticAttenuation);
#|
#| vec3 lightDirection = (uViewMatrix * vec4(uSpotLightDirection[j], 0.0)).xyz;
#| float spotDot = dot(normalize(lightVector), normalize(lightDirection));
#| float spotFalloff;
#| if(spotDot < uSpotLightAngle[j]) {
#| spotFalloff = 0.0;
#| }
#| else {
#| spotFalloff = pow(spotDot, uSpotLightConc[j]);
#| }
#| lightFalloff *= spotFalloff;
#|
#| vec3 lightColor = uSpotLightDiffuseColors[j];
#| vec3 specularColor = uSpotLightSpecularColors[j];
#|
#| LightResult result = _light(viewDirection, normal, lightVector, shininess, metallic);
#|
#| totalDiffuse += result.diffuse * lightColor * lightFalloff;
#| totalSpecular += result.specular * lightColor * specularColor * lightFalloff;
#| }
#| }
#|
#| if( uUseImageLight ){
#| totalDiffuse += calculateImageDiffuse(normal, modelPosition, metallic);
#| totalSpecular += calculateImageSpecular(normal, modelPosition, shininess, metallic);
#| }
#|
#| totalDiffuse *= diffuseFactor;
#| totalSpecular *= specularFactor;
#|}
#|
///|
/// line.frag
let glsl_line_frag : String =
#|precision highp int;
#|precision highp float;
#|
#|uniform vec4 uMaterialColor;
#|uniform int uStrokeCap;
#|uniform int uStrokeJoin;
#|
#|IN vec4 vColor;
#|IN vec2 vTangent;
#|IN vec2 vCenter;
#|IN vec2 vPosition;
#|IN float vStrokeWeight;
#|IN float vMaxDist;
#|IN float vCap;
#|IN float vJoin;
#|
#|float distSquared(vec2 a, vec2 b) {
#| vec2 aToB = b - a;
#| return dot(aToB, aToB);
#|}
#|
#|struct Inputs {
#| vec4 color;
#| vec2 tangent;
#| vec2 center;
#| vec2 position;
#| float strokeWeight;
#|};
#|
#|void main() {
#| HOOK_beforeFragment();
#|
#| Inputs inputs;
#| inputs.color = vColor;
#| inputs.tangent = vTangent;
#| inputs.center = vCenter;
#| inputs.position = vPosition;
#| inputs.strokeWeight = vStrokeWeight;
#| inputs = HOOK_getPixelInputs(inputs);
#|
#| if (vCap > 0.) {
#| if (
#| uStrokeCap == STROKE_CAP_ROUND &&
#| HOOK_shouldDiscard(distSquared(inputs.position, inputs.center) > inputs.strokeWeight * inputs.strokeWeight * 0.25)
#| ) {
#| discard;
#| } else if (
#| uStrokeCap == STROKE_CAP_SQUARE &&
#| HOOK_shouldDiscard(dot(inputs.position - inputs.center, inputs.tangent) > 0.)
#| ) {
#| discard;
#| // Use full area for PROJECT
#| } else if (HOOK_shouldDiscard(false)) {
#| discard;
#| }
#| } else if (vJoin > 0.) {
#| if (
#| uStrokeJoin == STROKE_JOIN_ROUND &&
#| HOOK_shouldDiscard(distSquared(inputs.position, inputs.center) > inputs.strokeWeight * inputs.strokeWeight * 0.25)
#| ) {
#| discard;
#| } else if (uStrokeJoin == STROKE_JOIN_BEVEL) {
#| vec2 normal = vec2(-inputs.tangent.y, inputs.tangent.x);
#| if (HOOK_shouldDiscard(abs(dot(inputs.position - inputs.center, normal)) > vMaxDist)) {
#| discard;
#| }
#| // Use full area for MITER
#| } else if (HOOK_shouldDiscard(false)) {
#| discard;
#| }
#| }
#| OUT_COLOR = HOOK_getFinalColor(inputs.color, vec2(0.0, 0.0));
#| OUT_COLOR.rgb *= OUT_COLOR.a;
#| HOOK_afterFragment();
#|}
#|
///|
/// line.vert
let glsl_line_vert : String =
#|/*
#| Part of the Processing project - http://processing.org
#| Copyright (c) 2012-15 The Processing Foundation
#| Copyright (c) 2004-12 Ben Fry and Casey Reas
#| Copyright (c) 2001-04 Massachusetts Institute of Technology
#| This library is free software; you can redistribute it and/or
#| modify it under the terms of the GNU Lesser General Public
#| License as published by the Free Software Foundation, version 2.1.
#| This library is distributed in the hope that it will be useful,
#| but WITHOUT ANY WARRANTY; without even the implied warranty of
#| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#| Lesser General Public License for more details.
#| You should have received a copy of the GNU Lesser General
#| Public License along with this library; if not, write to the
#| Free Software Foundation, Inc., 59 Temple Place, Suite 330,
#| Boston, MA 02111-1307 USA
#|*/
#|
#|#define PROCESSING_LINE_SHADER
#|
#|#define HOOK_DEFINES
#|
#|precision highp int;
#|precision highp float;
#|
#|#ifdef AUGMENTED_HOOK_getWorldInputs
#|uniform mat4 uModelMatrix;
#|uniform mat4 uViewMatrix;
#|#else
#|uniform mat4 uModelViewMatrix;
#|#endif
#|
#|uniform mat4 uProjectionMatrix;
#|uniform float uStrokeWeight;
#|
#|uniform bool uUseLineColor;
#|uniform bool uSimpleLines;
#|uniform vec4 uMaterialColor;
#|
#|uniform vec4 uViewport;
#|uniform int uPerspective;
#|uniform int uStrokeJoin;
#|
#|IN vec3 aPosition;
#|IN vec3 aTangentIn;
#|IN vec3 aTangentOut;
#|IN float aSide;
#|IN vec4 aVertexColor;
#|
#|OUT vec4 vColor;
#|OUT vec2 vTangent;
#|OUT vec2 vCenter;
#|OUT vec2 vPosition;
#|OUT float vMaxDist;
#|OUT float vCap;
#|OUT float vJoin;
#|OUT float vStrokeWeight;
#|
#|vec2 lineIntersection(vec2 aPoint, vec2 aDir, vec2 bPoint, vec2 bDir) {
#| // Rotate and translate so a starts at the origin and goes out to the right
#| bPoint -= aPoint;
#| vec2 rotatedBFrom = vec2(
#| bPoint.x*aDir.x + bPoint.y*aDir.y,
#| bPoint.y*aDir.x - bPoint.x*aDir.y
#| );
#| vec2 bTo = bPoint + bDir;
#| vec2 rotatedBTo = vec2(
#| bTo.x*aDir.x + bTo.y*aDir.y,
#| bTo.y*aDir.x - bTo.x*aDir.y
#| );
#| float intersectionDistance =
#| rotatedBTo.x + (rotatedBFrom.x - rotatedBTo.x) * rotatedBTo.y /
#| (rotatedBTo.y - rotatedBFrom.y);
#| return aPoint + aDir * intersectionDistance;
#|}
#|
#|struct StrokeVertex {
#| vec3 position;
#| vec3 tangentIn;
#| vec3 tangentOut;
#| vec4 color;
#| float weight;
#|};
#|
#|void main() {
#| HOOK_beforeVertex();
#|
#| if (!uSimpleLines) {
#| // Caps have one of either the in or out tangent set to 0
#| vCap = (aTangentIn == vec3(0.)) != (aTangentOut == vec3(0.)) ? 1. : 0.;
#|
#| // Joins have two unique, defined tangents
#| vJoin = (
#| aTangentIn != vec3(0.) &&
#| aTangentOut != vec3(0.) &&
#| aTangentIn != aTangentOut
#| ) ? 1. : 0.;
#| }
#|
#| StrokeVertex inputs;
#| inputs.position = aPosition.xyz;
#| inputs.color = uUseLineColor ? aVertexColor : uMaterialColor;
#| inputs.weight = uStrokeWeight;
#| inputs.tangentIn = aTangentIn;
#| inputs.tangentOut = aTangentOut;
#|
#|#ifdef AUGMENTED_HOOK_getObjectInputs
#| inputs = HOOK_getObjectInputs(inputs);
#|#endif
#|
#|#ifdef AUGMENTED_HOOK_getWorldInputs
#| inputs.position = (uModelMatrix * vec4(inputs.position, 1.)).xyz;
#| inputs.tangentIn = (uModelMatrix * vec4(aTangentIn, 0.)).xyz;
#| inputs.tangentOut = (uModelMatrix * vec4(aTangentOut, 0.)).xyz;
#| inputs = HOOK_getWorldInputs(inputs);
#|#endif
#|
#|#ifdef AUGMENTED_HOOK_getWorldInputs
#| // Already multiplied by the model matrix, just apply view
#| inputs.position = (uViewMatrix * vec4(inputs.position, 1.)).xyz;
#| inputs.tangentIn = (uViewMatrix * vec4(aTangentIn, 0.)).xyz;
#| inputs.tangentOut = (uViewMatrix * vec4(aTangentOut, 0.)).xyz;
#|#else
#| // Apply both at once
#| inputs.position = (uModelViewMatrix * vec4(inputs.position, 1.)).xyz;
#| inputs.tangentIn = (uModelViewMatrix * vec4(aTangentIn, 0.)).xyz;
#| inputs.tangentOut = (uModelViewMatrix * vec4(aTangentOut, 0.)).xyz;
#|#endif
#|#ifdef AUGMENTED_HOOK_getCameraInputs
#| inputs = HOOK_getCameraInputs(inputs);
#|#endif
#|
#| vec4 posp = vec4(inputs.position, 1.);
#| vec4 posqIn = vec4(inputs.position + inputs.tangentIn, 1.);
#| vec4 posqOut = vec4(inputs.position + inputs.tangentOut, 1.);
#| vStrokeWeight = inputs.weight;
#|
#| float facingCamera = pow(
#| // The word space tangent's z value is 0 if it's facing the camera
#| abs(normalize(posqIn-posp).z),
#|
#| // Using pow() here to ramp `facingCamera` up from 0 to 1 really quickly
#| // so most lines get scaled and don't get clipped
#| 0.25
#| );
#|
#| // Moving vertices slightly toward the camera
#| // to avoid depth-fighting with the fill triangles.
#| // A mix of scaling and offsetting is used based on distance
#| // Discussion here:
#| // https://github.com/processing/p5.js/issues/7200
#|
#| // using a scale <1 moves the lines towards nearby camera
#| // in order to prevent popping effects due to half of
#| // the line disappearing behind the geometry faces.
#| float zDistance = -posp.z;
#| float distanceFactor = smoothstep(0.0, 800.0, zDistance);
#|
#| // Discussed here:
#| // http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=252848
#| float scale = mix(1., 0.995, facingCamera);
#| float dynamicScale = mix(scale, 1.0, distanceFactor); // Closer = more scale, farther = less
#|
#| posp.xyz = posp.xyz * dynamicScale;
#| posqIn.xyz = posqIn.xyz * dynamicScale;
#| posqOut.xyz = posqOut.xyz * dynamicScale;
#|
#| // Moving vertices slightly toward camera when far away
#| // https://github.com/processing/p5.js/issues/6956
#| float zOffset = mix(0., -1., facingCamera);
#| float dynamicZAdjustment = mix(0.0, zOffset, distanceFactor); // Closer = less zAdjustment, farther = more
#|
#| posp.z -= dynamicZAdjustment;
#| posqIn.z -= dynamicZAdjustment;
#| posqOut.z -= dynamicZAdjustment;
#|
#| vec4 p = uProjectionMatrix * posp;
#| vec4 qIn = uProjectionMatrix * posqIn;
#| vec4 qOut = uProjectionMatrix * posqOut;
#|
#| // formula to convert from clip space (range -1..1) to screen space (range 0..[width or height])
#| // screen_p = (p.xy/p.w + <1,1>) * 0.5 * uViewport.zw
#|
#| // prevent division by W by transforming the tangent formula (div by 0 causes
#| // the line to disappear, see https://github.com/processing/processing/issues/5183)
#| // t = screen_q - screen_p
#| //
#| // tangent is normalized and we don't care which aDirection it points to (+-)
#| // t = +- normalize( screen_q - screen_p )
#| // t = +- normalize( (q.xy/q.w+<1,1>)*0.5*uViewport.zw - (p.xy/p.w+<1,1>)*0.5*uViewport.zw )
#| //
#| // extract common factor, <1,1> - <1,1> cancels out
#| // t = +- normalize( (q.xy/q.w - p.xy/p.w) * 0.5 * uViewport.zw )
#| //
#| // convert to common divisor
#| // t = +- normalize( ((q.xy*p.w - p.xy*q.w) / (p.w*q.w)) * 0.5 * uViewport.zw )
#| //
#| // remove the common scalar divisor/factor, not needed due to normalize and +-
#| // (keep uViewport - can't remove because it has different components for x and y
#| // and corrects for aspect ratio, see https://github.com/processing/processing/issues/5181)
#| // t = +- normalize( (q.xy*p.w - p.xy*q.w) * uViewport.zw )
#|
#| vec2 tangentIn = normalize((qIn.xy*p.w - p.xy*qIn.w) * uViewport.zw);
#| vec2 tangentOut = normalize((qOut.xy*p.w - p.xy*qOut.w) * uViewport.zw);
#|
#| vec2 curPerspScale;
#| if(uPerspective == 1) {
#| // Perspective ---
#| // convert from world to clip by multiplying with projection scaling factor
#| // to get the right thickness (see https://github.com/processing/processing/issues/5182)
#|
#| // The y value of the projection matrix may be flipped if rendering to a Framebuffer.
#| // Multiplying again by its sign here negates the flip to get just the scale.
#| curPerspScale = (uProjectionMatrix * vec4(1, sign(uProjectionMatrix[1][1]), 0, 0)).xy;
#| } else {
#| // No Perspective ---
#| // multiply by W (to cancel out division by W later in the pipeline) and
#| // convert from screen to clip (derived from clip to screen above)
#| curPerspScale = p.w / (0.5 * uViewport.zw);
#| }
#|
#| vec2 offset;
#| if (vJoin == 1. && !uSimpleLines) {
#| vTangent = normalize(tangentIn + tangentOut);
#| vec2 normalIn = vec2(-tangentIn.y, tangentIn.x);
#| vec2 normalOut = vec2(-tangentOut.y, tangentOut.x);
#| float side = sign(aSide);
#| float sideEnum = abs(aSide);
#|
#| // We generate vertices for joins on either side of the centerline, but
#| // the "elbow" side is the only one needing a join. By not setting the
#| // offset for the other side, all its vertices will end up in the same
#| // spot and not render, effectively discarding it.
#| if (sign(dot(tangentOut, vec2(-tangentIn.y, tangentIn.x))) != side) {
#| // Side enums:
#| // 1: the side going into the join
#| // 2: the middle of the join
#| // 3: the side going out of the join
#| if (sideEnum == 2.) {
#| // Calculate the position + tangent on either side of the join, and
#| // find where the lines intersect to find the elbow of the join
#| vec2 c = (posp.xy/posp.w + vec2(1.,1.)) * 0.5 * uViewport.zw;
#| vec2 intersection = lineIntersection(
#| c + (side * normalIn * inputs.weight / 2.),
#| tangentIn,
#| c + (side * normalOut * inputs.weight / 2.),
#| tangentOut
#| );
#| offset = (intersection - c);
#|
#| // When lines are thick and the angle of the join approaches 180, the
#| // elbow might be really far from the center. We'll apply a limit to
#| // the magnitude to avoid lines going across the whole screen when this
#| // happens.
#| float mag = length(offset);
#| float maxMag = 3. * inputs.weight;
#| if (mag > maxMag) {
#| offset *= maxMag / mag;
#| }
#| } else if (sideEnum == 1.) {
#| offset = side * normalIn * inputs.weight / 2.;
#| } else if (sideEnum == 3.) {
#| offset = side * normalOut * inputs.weight / 2.;
#| }
#| }
#| if (uStrokeJoin == STROKE_JOIN_BEVEL) {
#| vec2 avgNormal = vec2(-vTangent.y, vTangent.x);
#| vMaxDist = abs(dot(avgNormal, normalIn * inputs.weight / 2.));
#| } else {
#| vMaxDist = inputs.weight / 2.;
#| }
#| } else {
#| vec2 tangent = aTangentIn == vec3(0.) ? tangentOut : tangentIn;
#|
#| vTangent = tangent;
#| vec2 normal = vec2(-tangent.y, tangent.x);
#|
#| float normalOffset = sign(aSide);
#| // Caps will have side values of -2 or 2 on the edge of the cap that
#| // extends out from the line
#| float tangentOffset = abs(aSide) - 1.;
#| offset = (normal * normalOffset + tangent * tangentOffset) *
#| inputs.weight * 0.5;
#| vMaxDist = inputs.weight / 2.;
#| }
#|
#| vCenter = p.xy;
#| vPosition = vCenter + offset;
#| vColor = inputs.color;
#|
#| gl_Position.xy = p.xy + offset.xy * curPerspScale;
#| gl_Position.zw = p.zw;
#|
#| HOOK_afterVertex();
#|}
#|
///|
/// normal.frag
let glsl_normal_frag : String =
#|IN vec3 vVertexNormal;
#|IN highp vec2 vVertTexCoord;
#|void main(void) {
#| HOOK_beforeFragment();
#| OUT_COLOR = HOOK_getFinalColor(vec4(vVertexNormal, 1.0), vVertTexCoord);
#| HOOK_afterFragment();
#|}
///|
/// normal.vert
let glsl_normal_vert : String =
#|IN vec3 aPosition;
#|IN vec3 aNormal;
#|IN vec2 aTexCoord;
#|IN vec4 aVertexColor;
#|
#|#define HOOK_DEFINES
#|
#|#ifdef AUGMENTED_HOOK_getWorldInputs
#|uniform mat4 uModelMatrix;
#|uniform mat4 uViewMatrix;
#|uniform mat3 uModelNormalMatrix;
#|uniform mat3 uCameraNormalMatrix;
#|#else
#|uniform mat4 uModelViewMatrix;
#|uniform mat3 uNormalMatrix;
#|#endif
#|uniform mat4 uProjectionMatrix;
#|
#|uniform vec4 uMaterialColor;
#|uniform bool uUseVertexColor;
#|
#|OUT vec3 vVertexNormal;
#|OUT highp vec2 vVertTexCoord;
#|OUT vec4 vColor;
#|
#|struct Vertex {
#| vec3 position;
#| vec3 normal;
#| vec2 texCoord;
#| vec4 color;
#|};
#|
#|void main(void) {
#| HOOK_beforeVertex();
#|
#| Vertex inputs;
#| inputs.position = aPosition;
#| inputs.normal = aNormal;
#| inputs.texCoord = aTexCoord;
#| inputs.color = (uUseVertexColor && aVertexColor.x >= 0.0) ? aVertexColor : uMaterialColor;
#|#ifdef AUGMENTED_HOOK_getObjectInputs
#| inputs = HOOK_getObjectInputs(inputs);
#|#endif
#|
#|#ifdef AUGMENTED_HOOK_getWorldInputs
#| inputs.position = (uModelMatrix * vec4(inputs.position, 1.)).xyz;
#| inputs.normal = uModelNormalMatrix * inputs.normal;
#| inputs = HOOK_getWorldInputs(inputs);
#|#endif
#|
#|#ifdef AUGMENTED_HOOK_getWorldInputs
#| // Already multiplied by the model matrix, just apply view
#| inputs.position = (uViewMatrix * vec4(inputs.position, 1.)).xyz;
#| inputs.normal = uCameraNormalMatrix * inputs.normal;
#|#else
#| // Apply both at once
#| inputs.position = (uModelViewMatrix * vec4(inputs.position, 1.)).xyz;
#| inputs.normal = uNormalMatrix * inputs.normal;
#|#endif
#|#ifdef AUGMENTED_HOOK_getCameraInputs
#| inputs = HOOK_getCameraInputs(inputs);
#|#endif
#|
#| // Pass varyings to fragment shader
#| vVertTexCoord = inputs.texCoord;
#| vVertexNormal = normalize(inputs.normal);
#| vColor = inputs.color;
#|
#| gl_Position = uProjectionMatrix * vec4(inputs.position, 1.);
#|
#| HOOK_afterVertex();
#|}
#|
///|
/// phong.frag
let glsl_phong_frag : String =
#|// include lighting.glsl
#|precision highp int;
#|
#|uniform bool uHasSetAmbient;
#|uniform vec3 uAmbientColor;
#|uniform vec4 uSpecularMatColor;
#|uniform vec4 uAmbientMatColor;
#|uniform vec4 uEmissiveMatColor;
#|
#|uniform vec4 uTint;
#|uniform sampler2D uSampler;
#|uniform bool isTexture;
#|
#|// the mtl texture maps only exist in the USE_TEXTURE_MAPS variant of this
#|// shader. models that don't use any map get the plain phong shader instead,
#|// so none of these samplers/branches are even compiled in for them. dave's
#|// call: separate variant rather than branching in one shader that everyone pays for.
#|#ifdef USE_TEXTURE_MAPS
#|uniform sampler2D uSpecularSampler;
#|uniform bool uHasSpecularTex;
#|uniform sampler2D uAmbientSampler;
#|uniform bool uHasAmbientTex;
#|uniform sampler2D uShininessSampler;
#|uniform bool uHasShininessTex;
#|uniform sampler2D uNormalSampler;
#|uniform bool uHasNormalMap;
#|uniform float uNormalScale;
#|uniform int uNormalMapMode;
#|uniform vec2 uNormalTexelSize;
#|#endif
#|
#|IN vec3 vNormal;
#|IN vec2 vTexCoord;
#|IN vec3 vViewPosition;
#|IN vec4 vColor;
#|#ifdef USE_TEXTURE_MAPS
#|IN vec4 vTangent;
#|#endif
#|
#|struct ColorComponents {
#| vec3 baseColor;
#| float opacity;
#| vec3 ambientColor;
#| vec3 specularColor;
#| vec3 diffuse;
#| vec3 ambient;
#| vec3 specular;
#| vec3 emissive;
#|};
#|
#|struct Inputs {
#| vec3 normal;
#| vec2 texCoord;
#| vec3 ambientLight;
#| vec3 ambientMaterial;
#| vec3 specularMaterial;
#| vec3 emissiveMaterial;
#| vec4 color;
#| float shininess;
#| float metalness;
#|};
#|
#|void main(void) {
#| HOOK_beforeFragment();
#|
#| Inputs inputs;
#| vec3 N = normalize(vNormal);
#|#ifdef USE_TEXTURE_MAPS
#| if (uHasNormalMap) {
#| // rebuild the tangent basis (TBN) from the smooth per-vertex tangent and
#| // perturb the normal by the map. gram-schmidt keeps T perpendicular to the
#| // interpolated normal so the frame stays smooth across triangles (no facets).
#| vec3 T = normalize(vTangent.xyz);
#| T = normalize(T - N * dot(N, T));
#| vec3 B = cross(N, T) * vTangent.w;
#| vec3 mapN;
#| if (uNormalMapMode == 1) {
#| // bump map: brightness is height, so the tangent-space normal comes from
#| // how fast that height changes between neighbouring texels. sampling both
#| // sides keeps the slope right at the edges of the map, where reaching past
#| // one side would otherwise clamp and read back the same texel.
#| vec2 du = vec2(uNormalTexelSize.x, 0.0);
#| vec2 dv = vec2(0.0, uNormalTexelSize.y);
#| float hl = TEXTURE(uNormalSampler, vTexCoord - du).r;
#| float hr = TEXTURE(uNormalSampler, vTexCoord + du).r;
#| float hd = TEXTURE(uNormalSampler, vTexCoord - dv).r;
#| float hu = TEXTURE(uNormalSampler, vTexCoord + dv).r;
#| // the surface leans away from the direction height increases in
#| mapN = normalize(vec3((hl - hr) * 0.5, (hd - hu) * 0.5, 1.0));
#| } else {
#| // normal map: rgb already holds the tangent-space normal
#| mapN = TEXTURE(uNormalSampler, vTexCoord).rgb * 2.0 - 1.0;
#| }
#| // scale the tangent-space slope so the strength can be tuned (-bm)
#| mapN.xy *= uNormalScale;
#| N = normalize(mat3(T, B, N) * mapN);
#| }
#|#endif
#| inputs.normal = N;
#| inputs.texCoord = vTexCoord;
#| inputs.ambientLight = uAmbientColor;
#| inputs.color = isTexture
#| ? TEXTURE(uSampler, vTexCoord) * (vec4(uTint.rgb/255., 1.) * uTint.a/255.)
#| : vColor;
#| if (isTexture && inputs.color.a > 0.0) {
#| // Textures come in with premultiplied alpha. Temporarily unpremultiply it
#| // so hooks users don't have to think about premultiplied alpha.
#| inputs.color.rgb /= inputs.color.a;
#| }
#| inputs.metalness = uMetallic;
#|#ifdef USE_TEXTURE_MAPS
#| // map variant: shininess/ambient/specular can be modulated by their maps
#| inputs.shininess = uHasShininessTex
#| ? uShininess * TEXTURE(uShininessSampler, vTexCoord).r
#| : uShininess;
#| inputs.ambientMaterial = uHasAmbientTex
#| ? TEXTURE(uAmbientSampler, vTexCoord).rgb * uAmbientMatColor.rgb
#| : (uHasSetAmbient ? uAmbientMatColor.rgb : inputs.color.rgb);
#| inputs.specularMaterial = uHasSpecularTex
#| ? TEXTURE(uSpecularSampler, vTexCoord).rgb * uSpecularMatColor.rgb
#| : uSpecularMatColor.rgb;
#|#else
#| // default variant: plain phong, exactly as before this feature existed
#| inputs.shininess = uShininess;
#| inputs.ambientMaterial = uHasSetAmbient ? uAmbientMatColor.rgb : inputs.color.rgb;
#| inputs.specularMaterial = uSpecularMatColor.rgb;
#|#endif
#| inputs.emissiveMaterial = uEmissiveMatColor.rgb;
#| inputs = HOOK_getPixelInputs(inputs);
#|
#| vec3 diffuse;
#| vec3 specular;
#| totalLight(vViewPosition, inputs.normal, inputs.shininess, inputs.metalness, diffuse, specular);
#|
#| // Calculating final color as result of all lights (plus emissive term).
#|
#| vec4 baseColor = inputs.color;
#| ColorComponents c;
#| c.opacity = baseColor.a;
#| c.baseColor = baseColor.rgb;
#| c.ambientColor = inputs.ambientMaterial;
#| c.specularColor = inputs.specularMaterial;
#| c.diffuse = diffuse;
#| c.ambient = inputs.ambientLight;
#| c.specular = specular;
#| c.emissive = inputs.emissiveMaterial;
#| OUT_COLOR = HOOK_getFinalColor(HOOK_combineColors(c), vTexCoord);
#| OUT_COLOR.rgb *= OUT_COLOR.a; // Premultiply alpha before rendering
#| HOOK_afterFragment();
#|}
#|
///|
/// phong.vert
let glsl_phong_vert : String =
#|precision highp int;
#|
#|#define HOOK_DEFINES
#|
#|IN vec3 aPosition;
#|IN vec3 aNormal;
#|IN vec2 aTexCoord;
#|IN vec4 aVertexColor;
#|#ifdef USE_TEXTURE_MAPS
#|IN vec4 aTangent;
#|#endif
#|
#|#ifdef AUGMENTED_HOOK_getWorldInputs
#|uniform mat4 uModelMatrix;
#|uniform mat4 uViewMatrix;
#|uniform mat3 uModelNormalMatrix;
#|uniform mat3 uCameraNormalMatrix;
#|#else
#|uniform mat4 uModelViewMatrix;
#|uniform mat3 uNormalMatrix;
#|#endif
#|uniform mat4 uProjectionMatrix;
#|
#|uniform bool uUseVertexColor;
#|uniform vec4 uMaterialColor;
#|
#|OUT vec3 vNormal;
#|OUT vec2 vTexCoord;
#|OUT vec3 vViewPosition;
#|OUT vec3 vAmbientColor;
#|OUT vec4 vColor;
#|#ifdef USE_TEXTURE_MAPS
#|OUT vec4 vTangent;
#|#endif
#|
#|struct Vertex {
#| vec3 position;
#| vec3 normal;
#| vec2 texCoord;
#| vec4 color;
#|};
#|
#|void main(void) {
#| HOOK_beforeVertex();
#|
#| Vertex inputs;
#| inputs.position = aPosition;
#| inputs.normal = aNormal;
#| inputs.texCoord = aTexCoord;
#| inputs.color = (uUseVertexColor && aVertexColor.x >= 0.0) ? aVertexColor : uMaterialColor;
#|#ifdef USE_TEXTURE_MAPS
#| // transform the surface tangent alongside the normal so it ends up in the
#| // same space; handedness (w) is passed through for rebuilding the bitangent.
#| vec3 tangent = aTangent.xyz;
#|#endif
#|#ifdef AUGMENTED_HOOK_getObjectInputs
#| inputs = HOOK_getObjectInputs(inputs);
#|#endif
#|
#|#ifdef AUGMENTED_HOOK_getWorldInputs
#| inputs.position = (uModelMatrix * vec4(inputs.position, 1.)).xyz;
#| inputs.normal = uModelNormalMatrix * inputs.normal;
#|#ifdef USE_TEXTURE_MAPS
#| tangent = uModelNormalMatrix * tangent;
#|#endif
#| inputs = HOOK_getWorldInputs(inputs);
#|#endif
#|
#|#ifdef AUGMENTED_HOOK_getWorldInputs
#| // Already multiplied by the model matrix, just apply view
#| inputs.position = (uViewMatrix * vec4(inputs.position, 1.)).xyz;
#| inputs.normal = uCameraNormalMatrix * inputs.normal;
#|#ifdef USE_TEXTURE_MAPS
#| tangent = uCameraNormalMatrix * tangent;
#|#endif
#|#else
#| // Apply both at once
#| inputs.position = (uModelViewMatrix * vec4(inputs.position, 1.)).xyz;
#| inputs.normal = uNormalMatrix * inputs.normal;
#|#ifdef USE_TEXTURE_MAPS
#| tangent = uNormalMatrix * tangent;
#|#endif
#|#endif
#|#ifdef AUGMENTED_HOOK_getCameraInputs
#| inputs = HOOK_getCameraInputs(inputs);
#|#endif
#|
#| // Pass varyings to fragment shader
#| vViewPosition = inputs.position;
#| vTexCoord = inputs.texCoord;
#| vNormal = inputs.normal;
#| vColor = inputs.color;
#|#ifdef USE_TEXTURE_MAPS
#| vTangent = vec4(tangent, aTangent.w);
#|#endif
#|
#| gl_Position = uProjectionMatrix * vec4(inputs.position, 1.);
#| HOOK_afterVertex();
#|}
#|
///|
/// webgl2Compatibility.glsl
let glsl_webgl2compatibility_glsl : String =
#|#ifdef WEBGL2
#|
#|#define IN in
#|#define OUT out
#|
#|#ifdef FRAGMENT_SHADER
#|out vec4 outColor;
#|#define OUT_COLOR outColor
#|#endif
#|#define TEXTURE texture
#|
#|#else
#|
#|#ifdef FRAGMENT_SHADER
#|#define IN varying
#|#else
#|#define IN attribute
#|#endif
#|#define OUT varying
#|#define TEXTURE texture2D
#|
#|#ifdef FRAGMENT_SHADER
#|#define OUT_COLOR gl_FragColor
#|#endif
#|
#|#endif
#|
#|vec4 getTexture(in sampler2D content, vec2 coord) {
#| vec4 color = TEXTURE(content, coord);
#| if (color.a > 0.) color.rgb /= color.a;
#| return color;
#|}
#|