phaser
Version:
A fast, free and fun HTML5 Game Framework for Desktop and Mobile web browsers from the team at Phaser Studio Inc.
165 lines (164 loc) • 6.95 kB
JavaScript
module.exports = [
'#version 100',
'#pragma phaserTemplate(shaderName)',
'#pragma phaserTemplate(fragmentIterations)',
'#pragma phaserTemplate(fragmentNormalMap)',
'#ifndef ITERATION_COUNT',
'#define ITERATION_COUNT 1.0',
'#endif',
'#ifndef WARP_ITERATION_COUNT',
'#define WARP_ITERATION_COUNT 1.0',
'#endif',
'#ifdef GL_FRAGMENT_PRECISION_HIGH',
'precision highp float;',
'#else',
'precision mediump float;',
'#endif',
'uniform vec3 uCells;',
'uniform vec3 uPeriod;',
'uniform vec3 uOffset;',
'uniform float uFlow;',
'uniform float uDetailPower;',
'uniform float uFlowPower;',
'uniform float uContributionPower;',
'uniform float uWarpDetailPower;',
'uniform float uWarpFlowPower;',
'uniform float uWarpContributionPower;',
'uniform float uWarpAmount;',
'uniform vec4 uColorStart;',
'uniform vec4 uColorEnd;',
'uniform float uNormalScale;',
'uniform float uValueFactor;',
'uniform float uValueAdd;',
'uniform float uValuePower;',
'uniform vec3 uSeed;',
'varying vec2 outTexCoord;',
'vec4 permute(vec4 i) {',
' vec4 im = mod(i, 289.0);',
' return mod(((im * 34.0) + 10.0) * im, 289.0);',
'}',
'float psrdnoise(vec3 x, vec3 period, float alpha) {',
' const mat3 M = mat3(0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0);',
' const mat3 Mi = mat3(-0.5, 0.5, 0.5, 0.5, -0.5, 0.5, 0.5, 0.5, -0.5);',
' vec3 uvw = M * x;',
' vec3 i0 = floor(uvw);',
' vec3 f0 = fract(uvw);',
' vec3 g_ = step(f0.xyx, f0.yzz);',
' vec3 l_ = 1.0 - g_;',
' vec3 g = vec3(l_.z, g_.xy);',
' vec3 l = vec3(l_.xy, g_.z);',
' vec3 o1 = min(g, l);',
' vec3 o2 = max(g, l);',
' vec3 i1 = i0 + o1, i2 = i0 + o2, i3 = i0 + 1.0;',
' vec3 v0 = Mi * i0, v1 = Mi * i1, v2 = Mi * i2, v3 = Mi * i3;',
' vec3 x0 = x - v0, x1 = x - v1, x2 = x - v2, x3 = x - v3;',
' if(any(greaterThan(period, vec3(0.0)))) {',
' vec4 vx = vec4(v0.x, v1.x, v2.x, v3.x);',
' vec4 vy = vec4(v0.y, v1.y, v2.y, v3.y);',
' vec4 vz = vec4(v0.z, v1.z, v2.z, v3.z);',
' if(period.x > 0.0)',
' vx = mod(vx, period.x);',
' if(period.y > 0.0)',
' vy = mod(vy, period.y);',
' if(period.z > 0.0)',
' vz = mod(vz, period.z);',
' i0 = floor(M * vec3(vx.x, vy.x, vz.x) + 0.5);',
' i1 = floor(M * vec3(vx.y, vy.y, vz.y) + 0.5);',
' i2 = floor(M * vec3(vx.z, vy.z, vz.z) + 0.5);',
' i3 = floor(M * vec3(vx.w, vy.w, vz.w) + 0.5);',
' }',
' i0 += uSeed; i1 += uSeed; i2 += uSeed; i3 += uSeed;',
' vec4 hash = permute(permute(permute(vec4(i0.z, i1.z, i2.z, i3.z)) + vec4(i0.y, i1.y, i2.y, i3.y)) + vec4(i0.x, i1.x, i2.x, i3.x));',
' vec4 theta = hash * 3.883222077;',
' vec4 sz = 0.996539792 - 0.006920415 * hash;',
' vec4 psi = hash * 0.108705628;',
' vec4 Ct = cos(theta);',
' vec4 St = sin(theta);',
' vec4 sz_prime = sqrt(1.0 - sz * sz);',
' vec4 gx, gy, gz;',
' if(alpha != 0.0) {',
' vec4 px = Ct * sz_prime;',
' vec4 py = St * sz_prime;',
' vec4 pz = sz;',
' vec4 Sp = sin(psi);',
' vec4 Cp = cos(psi);',
' vec4 Ctp = St * Sp - Ct * Cp;',
' vec4 qx = mix(Ctp * St, Sp, sz);',
' vec4 qy = mix(-Ctp * Ct, Cp, sz);',
' vec4 qz = -(py * Cp + px * Sp);',
' vec4 Sa = vec4(sin(alpha));',
' vec4 Ca = vec4(cos(alpha));',
' gx = Ca * px + Sa * qx;',
' gy = Ca * py + Sa * qy;',
' gz = Ca * pz + Sa * qz;',
' } else {',
' gx = Ct * sz_prime;',
' gy = St * sz_prime;',
' gz = sz;',
' }',
' vec3 g0 = vec3(gx.x, gy.x, gz.x), g1 = vec3(gx.y, gy.y, gz.y);',
' vec3 g2 = vec3(gx.z, gy.z, gz.z), g3 = vec3(gx.w, gy.w, gz.w);',
' vec4 w = 0.5 - vec4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3));',
' w = max(w, 0.0);',
' vec4 w2 = w * w;',
' vec4 w3 = w2 * w;',
' vec4 gdotx = vec4(dot(g0, x0), dot(g1, x1), dot(g2, x2), dot(g3, x3));',
' float n = dot(w3, gdotx);',
' return 39.5 * n;',
'}',
'float iterate (vec3 coord, float detailPower, float flowPower, float contributionPower)',
'{',
' float value = 0.;',
' float itValue = 0.;',
' for (float iteration = 0.; iteration < ITERATION_COUNT; iteration++)',
' {',
' float detailScale = pow(detailPower, iteration);',
' float flowScale = pow(flowPower, iteration);',
' itValue = psrdnoise((coord + iteration) * detailScale, uPeriod * detailScale, uFlow * flowScale + iteration);',
' value += itValue / pow(contributionPower, iteration);',
' }',
' return value;',
'}',
'float iterateWarp (vec3 coord, float detailPower, float flowPower, float contributionPower)',
'{',
' float value = 0.;',
' float itValue = 0.;',
' for (float iteration = 0.; iteration < WARP_ITERATION_COUNT; iteration++)',
' {',
' float detailScale = pow(detailPower, iteration);',
' float flowScale = pow(flowPower, iteration);',
' itValue = psrdnoise((coord + iteration) * detailScale, uPeriod * detailScale, uFlow * flowScale + iteration);',
' value += itValue / pow(contributionPower, iteration);',
' }',
' return value;',
'}',
'vec3 warp (vec3 coord)',
'{',
' vec3 o2 = vec3(11.3, 23.7, 13.1);',
' vec3 o3 = vec3(29.9, 2.3, 31.7);',
' float coord1 = iterateWarp(coord, uWarpDetailPower, uWarpFlowPower, uWarpContributionPower);',
' float coord2 = iterateWarp(coord + o2, uWarpDetailPower, uWarpFlowPower, uWarpContributionPower);',
' float coord3 = iterateWarp(coord + o3, uWarpDetailPower, uWarpFlowPower, uWarpContributionPower);',
' return vec3(coord1, coord2, coord3);',
'}',
'void main ()',
'{',
' vec3 coord = (vec3(outTexCoord, 0.0) * uCells) + uOffset;',
' if (uWarpAmount > 0.)',
' {',
' coord += warp(coord) * uWarpAmount;',
' }',
' float value = iterate(coord, uDetailPower, uFlowPower, uContributionPower) * uValueFactor + uValueAdd;',
' value = pow(clamp(value, 0., 1.), uValuePower);',
' #ifndef NORMAL_MAP',
' vec4 color = mix(uColorStart, uColorEnd, value);',
' color.rgb *= color.a;',
' gl_FragColor = color;',
' #else',
' float dx = dFdx(value) * uNormalScale;',
' float dy = dFdy(value) * uNormalScale;',
' vec3 normal = vec3(dx, dy, 1.0 - sqrt(dx * dx + dy * dy)) * 0.5 + 0.5;',
' gl_FragColor = vec4(normal, 1.0);',
' #endif',
'}',
].join('\n');