@fjandin/react-shader 0.0.21 → 0.0.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -213
- package/dist/example/frontend.d.ts.map +1 -1
- package/dist/index.cjs +0 -974
- package/dist/index.d.ts +1 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -974
- package/dist/types.d.ts +0 -28
- package/dist/types.d.ts.map +1 -1
- package/package.json +4 -4
- package/dist/ReactShader.d.ts +0 -3
- package/dist/ReactShader.d.ts.map +0 -1
- package/dist/example/examples/webgl.d.ts +0 -2
- package/dist/example/examples/webgl.d.ts.map +0 -1
- package/dist/hooks/useWebGL.d.ts +0 -20
- package/dist/hooks/useWebGL.d.ts.map +0 -1
- package/dist/shaders/color-palette.d.ts +0 -2
- package/dist/shaders/color-palette.d.ts.map +0 -1
- package/dist/shaders/distortion-ripple.d.ts +0 -16
- package/dist/shaders/distortion-ripple.d.ts.map +0 -1
- package/dist/shaders/scene-circles.d.ts +0 -21
- package/dist/shaders/scene-circles.d.ts.map +0 -1
- package/dist/shaders/simplex-noise.d.ts +0 -2
- package/dist/shaders/simplex-noise.d.ts.map +0 -1
- package/dist/shaders/utils.d.ts +0 -2
- package/dist/shaders/utils.d.ts.map +0 -1
- package/dist/utils/shader.d.ts +0 -6
- package/dist/utils/shader.d.ts.map +0 -1
- package/dist/utils/textures.d.ts +0 -23
- package/dist/utils/textures.d.ts.map +0 -1
- package/dist/utils/uniforms.d.ts +0 -12
- package/dist/utils/uniforms.d.ts.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -30,16 +30,10 @@ var __export = (target, all) => {
|
|
|
30
30
|
var exports_src = {};
|
|
31
31
|
__export(exports_src, {
|
|
32
32
|
useAudio: () => useAudio,
|
|
33
|
-
generateUtilsFunction: () => generateUtilsFunction,
|
|
34
33
|
generateSimplexNoiseFunctionGpu: () => generateSimplexNoiseFunctionGpu,
|
|
35
|
-
generateSimplexNoiseFunction: () => generateSimplexNoiseFunction,
|
|
36
34
|
generateSceneCirclesFunctionGpu: () => generateSceneCirclesFunctionGpu,
|
|
37
|
-
generateSceneCirclesFunction: () => generateSceneCirclesFunction,
|
|
38
35
|
generateDistortionRippleFunctionGpu: () => generateDistortionRippleFunctionGpu,
|
|
39
|
-
generateDistortionRippleFunction: () => generateDistortionRippleFunction,
|
|
40
36
|
generateColorPaletteFunctionGpu: () => generateColorPaletteFunctionGpu,
|
|
41
|
-
generateColorPaletteFunction: () => generateColorPaletteFunction,
|
|
42
|
-
ReactShader: () => ReactShader,
|
|
43
37
|
ReactGpuShader: () => ReactGpuShader
|
|
44
38
|
});
|
|
45
39
|
module.exports = __toCommonJS(exports_src);
|
|
@@ -865,721 +859,6 @@ function ReactGpuShader({
|
|
|
865
859
|
style: CANVAS_STYLE
|
|
866
860
|
}, undefined, false, undefined, this);
|
|
867
861
|
}
|
|
868
|
-
// src/ReactShader.tsx
|
|
869
|
-
var import_react5 = require("react");
|
|
870
|
-
|
|
871
|
-
// src/hooks/useWebGL.ts
|
|
872
|
-
var import_react4 = require("react");
|
|
873
|
-
|
|
874
|
-
// src/utils/shader.ts
|
|
875
|
-
function compileShader(gl, type, source) {
|
|
876
|
-
const shader = gl.createShader(type);
|
|
877
|
-
if (!shader) {
|
|
878
|
-
throw new Error(`Failed to create shader of type ${type === gl.VERTEX_SHADER ? "VERTEX" : "FRAGMENT"}`);
|
|
879
|
-
}
|
|
880
|
-
gl.shaderSource(shader, source);
|
|
881
|
-
gl.compileShader(shader);
|
|
882
|
-
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
883
|
-
const info = gl.getShaderInfoLog(shader);
|
|
884
|
-
gl.deleteShader(shader);
|
|
885
|
-
const shaderType = type === gl.VERTEX_SHADER ? "Vertex" : "Fragment";
|
|
886
|
-
throw new Error(`${shaderType} shader compilation failed:
|
|
887
|
-
${info}`);
|
|
888
|
-
}
|
|
889
|
-
return shader;
|
|
890
|
-
}
|
|
891
|
-
function createProgram(gl, vertexShader, fragmentShader) {
|
|
892
|
-
const program = gl.createProgram();
|
|
893
|
-
if (!program) {
|
|
894
|
-
throw new Error("Failed to create WebGL program");
|
|
895
|
-
}
|
|
896
|
-
gl.attachShader(program, vertexShader);
|
|
897
|
-
gl.attachShader(program, fragmentShader);
|
|
898
|
-
gl.linkProgram(program);
|
|
899
|
-
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
900
|
-
const info = gl.getProgramInfoLog(program);
|
|
901
|
-
gl.deleteProgram(program);
|
|
902
|
-
throw new Error(`Program linking failed:
|
|
903
|
-
${info}`);
|
|
904
|
-
}
|
|
905
|
-
return program;
|
|
906
|
-
}
|
|
907
|
-
function createShaderProgram(gl, vertexSource, fragmentSource) {
|
|
908
|
-
const vertexShader = compileShader(gl, gl.VERTEX_SHADER, vertexSource);
|
|
909
|
-
const fragmentShader = compileShader(gl, gl.FRAGMENT_SHADER, fragmentSource);
|
|
910
|
-
try {
|
|
911
|
-
return createProgram(gl, vertexShader, fragmentShader);
|
|
912
|
-
} finally {
|
|
913
|
-
gl.deleteShader(vertexShader);
|
|
914
|
-
gl.deleteShader(fragmentShader);
|
|
915
|
-
}
|
|
916
|
-
}
|
|
917
|
-
|
|
918
|
-
// src/utils/textures.ts
|
|
919
|
-
function isTextureSource(value) {
|
|
920
|
-
if (typeof window === "undefined")
|
|
921
|
-
return false;
|
|
922
|
-
return value instanceof HTMLImageElement || value instanceof HTMLCanvasElement || value instanceof HTMLVideoElement || typeof ImageBitmap !== "undefined" && value instanceof ImageBitmap || value instanceof ImageData || typeof OffscreenCanvas !== "undefined" && value instanceof OffscreenCanvas;
|
|
923
|
-
}
|
|
924
|
-
function isTextureOptions(value) {
|
|
925
|
-
return typeof value === "object" && value !== null && "source" in value && isTextureSource(value.source);
|
|
926
|
-
}
|
|
927
|
-
function isTexture(value) {
|
|
928
|
-
return isTextureSource(value) || isTextureOptions(value);
|
|
929
|
-
}
|
|
930
|
-
function getWrapMode(gl, wrap) {
|
|
931
|
-
switch (wrap) {
|
|
932
|
-
case "repeat":
|
|
933
|
-
return gl.REPEAT;
|
|
934
|
-
case "mirror":
|
|
935
|
-
return gl.MIRRORED_REPEAT;
|
|
936
|
-
default:
|
|
937
|
-
return gl.CLAMP_TO_EDGE;
|
|
938
|
-
}
|
|
939
|
-
}
|
|
940
|
-
function getMinFilter(gl, filter) {
|
|
941
|
-
switch (filter) {
|
|
942
|
-
case "nearest":
|
|
943
|
-
return gl.NEAREST;
|
|
944
|
-
case "mipmap":
|
|
945
|
-
return gl.LINEAR_MIPMAP_LINEAR;
|
|
946
|
-
default:
|
|
947
|
-
return gl.LINEAR;
|
|
948
|
-
}
|
|
949
|
-
}
|
|
950
|
-
function getMagFilter(gl, filter) {
|
|
951
|
-
switch (filter) {
|
|
952
|
-
case "nearest":
|
|
953
|
-
return gl.NEAREST;
|
|
954
|
-
default:
|
|
955
|
-
return gl.LINEAR;
|
|
956
|
-
}
|
|
957
|
-
}
|
|
958
|
-
function isPowerOfTwo(value) {
|
|
959
|
-
return (value & value - 1) === 0 && value !== 0;
|
|
960
|
-
}
|
|
961
|
-
function getSourceDimensions(source) {
|
|
962
|
-
if (source instanceof HTMLImageElement) {
|
|
963
|
-
return { width: source.naturalWidth, height: source.naturalHeight };
|
|
964
|
-
}
|
|
965
|
-
if (source instanceof HTMLVideoElement) {
|
|
966
|
-
return { width: source.videoWidth, height: source.videoHeight };
|
|
967
|
-
}
|
|
968
|
-
if (source instanceof ImageData) {
|
|
969
|
-
return { width: source.width, height: source.height };
|
|
970
|
-
}
|
|
971
|
-
return { width: source.width, height: source.height };
|
|
972
|
-
}
|
|
973
|
-
function createTextureManager(gl) {
|
|
974
|
-
const maxUnits = gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS);
|
|
975
|
-
return {
|
|
976
|
-
cache: new Map,
|
|
977
|
-
nextUnit: 0,
|
|
978
|
-
maxUnits
|
|
979
|
-
};
|
|
980
|
-
}
|
|
981
|
-
function createTexture(gl, source, options = {}) {
|
|
982
|
-
const texture = gl.createTexture();
|
|
983
|
-
if (!texture) {
|
|
984
|
-
throw new Error("Failed to create WebGL texture");
|
|
985
|
-
}
|
|
986
|
-
const { wrapS = "clamp", wrapT = "clamp", minFilter = "linear", magFilter = "linear", flipY = true } = options;
|
|
987
|
-
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
988
|
-
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY);
|
|
989
|
-
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, source);
|
|
990
|
-
const { width, height } = getSourceDimensions(source);
|
|
991
|
-
const pot = isPowerOfTwo(width) && isPowerOfTwo(height);
|
|
992
|
-
const isWebGL2 = "texStorage2D" in gl;
|
|
993
|
-
const actualMinFilter = minFilter === "mipmap" && (pot || isWebGL2) ? minFilter : minFilter === "mipmap" ? "linear" : minFilter;
|
|
994
|
-
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, getWrapMode(gl, wrapS));
|
|
995
|
-
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, getWrapMode(gl, wrapT));
|
|
996
|
-
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, getMinFilter(gl, actualMinFilter));
|
|
997
|
-
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, getMagFilter(gl, magFilter));
|
|
998
|
-
if (actualMinFilter === "mipmap") {
|
|
999
|
-
gl.generateMipmap(gl.TEXTURE_2D);
|
|
1000
|
-
}
|
|
1001
|
-
gl.bindTexture(gl.TEXTURE_2D, null);
|
|
1002
|
-
return texture;
|
|
1003
|
-
}
|
|
1004
|
-
function updateTexture(gl, texture, source, flipY = true) {
|
|
1005
|
-
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
1006
|
-
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY);
|
|
1007
|
-
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, source);
|
|
1008
|
-
gl.bindTexture(gl.TEXTURE_2D, null);
|
|
1009
|
-
}
|
|
1010
|
-
function needsVideoUpdate(source) {
|
|
1011
|
-
if (!(source instanceof HTMLVideoElement))
|
|
1012
|
-
return false;
|
|
1013
|
-
return !source.paused && !source.ended && source.readyState >= 2;
|
|
1014
|
-
}
|
|
1015
|
-
function bindTextureUniform(gl, program, name, value, manager, locationCache) {
|
|
1016
|
-
const source = isTextureOptions(value) ? value.source : value;
|
|
1017
|
-
const options = isTextureOptions(value) ? value : {};
|
|
1018
|
-
let entry = manager.cache.get(name);
|
|
1019
|
-
if (entry && entry.source !== source) {
|
|
1020
|
-
gl.deleteTexture(entry.texture);
|
|
1021
|
-
entry = undefined;
|
|
1022
|
-
}
|
|
1023
|
-
if (!entry) {
|
|
1024
|
-
if (manager.nextUnit >= manager.maxUnits) {
|
|
1025
|
-
console.warn(`Maximum texture units (${manager.maxUnits}) exceeded for uniform "${name}"`);
|
|
1026
|
-
return;
|
|
1027
|
-
}
|
|
1028
|
-
const texture = createTexture(gl, source, options);
|
|
1029
|
-
entry = {
|
|
1030
|
-
texture,
|
|
1031
|
-
unit: manager.nextUnit++,
|
|
1032
|
-
source
|
|
1033
|
-
};
|
|
1034
|
-
manager.cache.set(name, entry);
|
|
1035
|
-
}
|
|
1036
|
-
if (needsVideoUpdate(source)) {
|
|
1037
|
-
const flipY = isTextureOptions(value) ? value.flipY ?? true : true;
|
|
1038
|
-
updateTexture(gl, entry.texture, source, flipY);
|
|
1039
|
-
}
|
|
1040
|
-
gl.activeTexture(gl.TEXTURE0 + entry.unit);
|
|
1041
|
-
gl.bindTexture(gl.TEXTURE_2D, entry.texture);
|
|
1042
|
-
let location = locationCache.get(name);
|
|
1043
|
-
if (location === undefined) {
|
|
1044
|
-
location = gl.getUniformLocation(program, name);
|
|
1045
|
-
locationCache.set(name, location);
|
|
1046
|
-
}
|
|
1047
|
-
if (location !== null) {
|
|
1048
|
-
gl.uniform1i(location, entry.unit);
|
|
1049
|
-
}
|
|
1050
|
-
}
|
|
1051
|
-
function cleanupTextures(gl, manager) {
|
|
1052
|
-
for (const entry of manager.cache.values()) {
|
|
1053
|
-
gl.deleteTexture(entry.texture);
|
|
1054
|
-
}
|
|
1055
|
-
manager.cache.clear();
|
|
1056
|
-
manager.nextUnit = 0;
|
|
1057
|
-
}
|
|
1058
|
-
|
|
1059
|
-
// src/utils/uniforms.ts
|
|
1060
|
-
var MAX_ARRAY_LENGTH = 100;
|
|
1061
|
-
function isVec22(value) {
|
|
1062
|
-
return Array.isArray(value) && value.length === 2 && typeof value[0] === "number";
|
|
1063
|
-
}
|
|
1064
|
-
function isVec32(value) {
|
|
1065
|
-
return Array.isArray(value) && value.length === 3 && typeof value[0] === "number";
|
|
1066
|
-
}
|
|
1067
|
-
function isVec42(value) {
|
|
1068
|
-
return Array.isArray(value) && value.length === 4 && typeof value[0] === "number";
|
|
1069
|
-
}
|
|
1070
|
-
function isFloatArray(value) {
|
|
1071
|
-
return Array.isArray(value) && value.length > 4 && typeof value[0] === "number";
|
|
1072
|
-
}
|
|
1073
|
-
function isVec2Array(value) {
|
|
1074
|
-
return Array.isArray(value) && value.length > 0 && Array.isArray(value[0]) && value[0].length === 2;
|
|
1075
|
-
}
|
|
1076
|
-
function isVec3Array(value) {
|
|
1077
|
-
return Array.isArray(value) && value.length > 0 && Array.isArray(value[0]) && value[0].length === 3;
|
|
1078
|
-
}
|
|
1079
|
-
function isVec4Array(value) {
|
|
1080
|
-
return Array.isArray(value) && value.length > 0 && Array.isArray(value[0]) && value[0].length === 4;
|
|
1081
|
-
}
|
|
1082
|
-
function isArrayUniform(value) {
|
|
1083
|
-
return isFloatArray(value) || isVec2Array(value) || isVec3Array(value) || isVec4Array(value);
|
|
1084
|
-
}
|
|
1085
|
-
function setUniform(gl, location, value) {
|
|
1086
|
-
if (location === null) {
|
|
1087
|
-
return;
|
|
1088
|
-
}
|
|
1089
|
-
if (typeof value === "number") {
|
|
1090
|
-
gl.uniform1f(location, value);
|
|
1091
|
-
} else if (isVec4Array(value)) {
|
|
1092
|
-
gl.uniform4fv(location, value.flat());
|
|
1093
|
-
} else if (isVec3Array(value)) {
|
|
1094
|
-
gl.uniform3fv(location, value.flat());
|
|
1095
|
-
} else if (isVec2Array(value)) {
|
|
1096
|
-
gl.uniform2fv(location, value.flat());
|
|
1097
|
-
} else if (isFloatArray(value)) {
|
|
1098
|
-
gl.uniform1fv(location, value);
|
|
1099
|
-
} else if (isVec42(value)) {
|
|
1100
|
-
gl.uniform4f(location, value[0], value[1], value[2], value[3]);
|
|
1101
|
-
} else if (isVec32(value)) {
|
|
1102
|
-
gl.uniform3f(location, value[0], value[1], value[2]);
|
|
1103
|
-
} else if (isVec22(value)) {
|
|
1104
|
-
gl.uniform2f(location, value[0], value[1]);
|
|
1105
|
-
}
|
|
1106
|
-
}
|
|
1107
|
-
function getUniformLocation(gl, program, name) {
|
|
1108
|
-
return gl.getUniformLocation(program, name);
|
|
1109
|
-
}
|
|
1110
|
-
function setUniforms(gl, program, uniforms, locationCache, textureManager) {
|
|
1111
|
-
for (const [name, value] of Object.entries(uniforms)) {
|
|
1112
|
-
if (isTexture(value)) {
|
|
1113
|
-
if (textureManager) {
|
|
1114
|
-
bindTextureUniform(gl, program, name, value, textureManager, locationCache);
|
|
1115
|
-
}
|
|
1116
|
-
continue;
|
|
1117
|
-
}
|
|
1118
|
-
let location = locationCache.get(name);
|
|
1119
|
-
if (location === undefined) {
|
|
1120
|
-
location = getUniformLocation(gl, program, name);
|
|
1121
|
-
locationCache.set(name, location);
|
|
1122
|
-
}
|
|
1123
|
-
setUniform(gl, location, value);
|
|
1124
|
-
if (isArrayUniform(value)) {
|
|
1125
|
-
const countName = `${name}_count`;
|
|
1126
|
-
let countLocation = locationCache.get(countName);
|
|
1127
|
-
if (countLocation === undefined) {
|
|
1128
|
-
countLocation = getUniformLocation(gl, program, countName);
|
|
1129
|
-
locationCache.set(countName, countLocation);
|
|
1130
|
-
}
|
|
1131
|
-
if (countLocation !== null) {
|
|
1132
|
-
gl.uniform1i(countLocation, value.length);
|
|
1133
|
-
}
|
|
1134
|
-
}
|
|
1135
|
-
}
|
|
1136
|
-
}
|
|
1137
|
-
function createUniformLocationCache() {
|
|
1138
|
-
return new Map;
|
|
1139
|
-
}
|
|
1140
|
-
function getUniformType(value) {
|
|
1141
|
-
if (isTexture(value)) {
|
|
1142
|
-
return "sampler2D";
|
|
1143
|
-
}
|
|
1144
|
-
if (typeof value === "number") {
|
|
1145
|
-
return "float";
|
|
1146
|
-
}
|
|
1147
|
-
if (isVec4Array(value)) {
|
|
1148
|
-
return `vec4[${MAX_ARRAY_LENGTH}]`;
|
|
1149
|
-
}
|
|
1150
|
-
if (isVec3Array(value)) {
|
|
1151
|
-
return `vec3[${MAX_ARRAY_LENGTH}]`;
|
|
1152
|
-
}
|
|
1153
|
-
if (isVec2Array(value)) {
|
|
1154
|
-
return `vec2[${MAX_ARRAY_LENGTH}]`;
|
|
1155
|
-
}
|
|
1156
|
-
if (isFloatArray(value)) {
|
|
1157
|
-
return `float[${MAX_ARRAY_LENGTH}]`;
|
|
1158
|
-
}
|
|
1159
|
-
if (isVec42(value)) {
|
|
1160
|
-
return "vec4";
|
|
1161
|
-
}
|
|
1162
|
-
if (isVec32(value)) {
|
|
1163
|
-
return "vec3";
|
|
1164
|
-
}
|
|
1165
|
-
if (isVec22(value)) {
|
|
1166
|
-
return "vec2";
|
|
1167
|
-
}
|
|
1168
|
-
return "float";
|
|
1169
|
-
}
|
|
1170
|
-
function generateUniformDeclarations(uniforms) {
|
|
1171
|
-
const lines = [];
|
|
1172
|
-
for (const [name, value] of Object.entries(uniforms)) {
|
|
1173
|
-
const type = getUniformType(value);
|
|
1174
|
-
if (type.includes("[")) {
|
|
1175
|
-
const [baseType, arrayPart] = type.split("[");
|
|
1176
|
-
lines.push(`uniform ${baseType} ${name}[${arrayPart};`);
|
|
1177
|
-
lines.push(`uniform int ${name}_count;`);
|
|
1178
|
-
} else {
|
|
1179
|
-
lines.push(`uniform ${type} ${name};`);
|
|
1180
|
-
}
|
|
1181
|
-
}
|
|
1182
|
-
return lines.join(`
|
|
1183
|
-
`);
|
|
1184
|
-
}
|
|
1185
|
-
var UNIFORM_MARKER = "// @UNIFORM_VALUES";
|
|
1186
|
-
function injectUniformDeclarations(shaderSource, customUniforms, defaultUniforms) {
|
|
1187
|
-
if (!shaderSource.includes(UNIFORM_MARKER)) {
|
|
1188
|
-
return shaderSource;
|
|
1189
|
-
}
|
|
1190
|
-
const allUniforms = { ...defaultUniforms, ...customUniforms };
|
|
1191
|
-
const declarations = generateUniformDeclarations(allUniforms);
|
|
1192
|
-
return shaderSource.replace(UNIFORM_MARKER, declarations);
|
|
1193
|
-
}
|
|
1194
|
-
|
|
1195
|
-
// src/hooks/useWebGL.ts
|
|
1196
|
-
var DEFAULT_UNIFORM_TYPES = {
|
|
1197
|
-
iTime: 0,
|
|
1198
|
-
iMouse: [0, 0],
|
|
1199
|
-
iMouseNormalized: [0, 0],
|
|
1200
|
-
iMouseLeftDown: 0,
|
|
1201
|
-
iResolution: [0, 0]
|
|
1202
|
-
};
|
|
1203
|
-
function initializeWebGL(canvas, vertexSource, fragmentSource, customUniforms) {
|
|
1204
|
-
const gl = canvas.getContext("webgl2") || canvas.getContext("webgl");
|
|
1205
|
-
if (!gl) {
|
|
1206
|
-
throw new Error("WebGL not supported");
|
|
1207
|
-
}
|
|
1208
|
-
const processedVertex = injectUniformDeclarations(vertexSource, customUniforms, DEFAULT_UNIFORM_TYPES);
|
|
1209
|
-
const processedFragment = injectUniformDeclarations(fragmentSource, customUniforms, DEFAULT_UNIFORM_TYPES);
|
|
1210
|
-
const program = createShaderProgram(gl, processedVertex, processedFragment);
|
|
1211
|
-
const positionBuffer = gl.createBuffer();
|
|
1212
|
-
if (!positionBuffer) {
|
|
1213
|
-
throw new Error("Failed to create position buffer");
|
|
1214
|
-
}
|
|
1215
|
-
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
|
|
1216
|
-
const positions = new Float32Array([-1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1]);
|
|
1217
|
-
gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);
|
|
1218
|
-
const positionAttributeLocation = gl.getAttribLocation(program, "a_position");
|
|
1219
|
-
if (positionAttributeLocation === -1) {
|
|
1220
|
-
throw new Error('Vertex shader must have an "a_position" attribute');
|
|
1221
|
-
}
|
|
1222
|
-
return {
|
|
1223
|
-
gl,
|
|
1224
|
-
program,
|
|
1225
|
-
positionBuffer,
|
|
1226
|
-
positionAttributeLocation,
|
|
1227
|
-
uniformLocationCache: createUniformLocationCache(),
|
|
1228
|
-
textureManager: createTextureManager(gl)
|
|
1229
|
-
};
|
|
1230
|
-
}
|
|
1231
|
-
function cleanupWebGL(gl, state) {
|
|
1232
|
-
cleanupTextures(gl, state.textureManager);
|
|
1233
|
-
gl.deleteBuffer(state.positionBuffer);
|
|
1234
|
-
gl.deleteProgram(state.program);
|
|
1235
|
-
}
|
|
1236
|
-
function useWebGL(options) {
|
|
1237
|
-
const canvasRef = import_react4.useRef(null);
|
|
1238
|
-
const stateRef = import_react4.useRef(null);
|
|
1239
|
-
const animationFrameRef = import_react4.useRef(0);
|
|
1240
|
-
const elapsedTimeRef = import_react4.useRef(0);
|
|
1241
|
-
const lastFrameTimeRef = import_react4.useRef(0);
|
|
1242
|
-
const mouseRef = import_react4.useRef([0, 0]);
|
|
1243
|
-
const mouseNormalizedRef = import_react4.useRef([0, 0]);
|
|
1244
|
-
const mouseLeftDownRef = import_react4.useRef(false);
|
|
1245
|
-
const canvasRectRef = import_react4.useRef(null);
|
|
1246
|
-
const contextLostRef = import_react4.useRef(false);
|
|
1247
|
-
const uniformsRef = import_react4.useRef(options.uniforms);
|
|
1248
|
-
const onErrorRef = import_react4.useRef(options.onError);
|
|
1249
|
-
const onFrameRef = import_react4.useRef(options.onFrame);
|
|
1250
|
-
const onClickRef = import_react4.useRef(options.onClick);
|
|
1251
|
-
const onMouseDownRef = import_react4.useRef(options.onMouseDown);
|
|
1252
|
-
const onMouseUpRef = import_react4.useRef(options.onMouseUp);
|
|
1253
|
-
const onMouseMoveRef = import_react4.useRef(options.onMouseMove);
|
|
1254
|
-
const onMouseWheelRef = import_react4.useRef(options.onMouseWheel);
|
|
1255
|
-
const timeScaleRef = import_react4.useRef(options.timeScale ?? 1);
|
|
1256
|
-
const vertexRef = import_react4.useRef(options.vertex);
|
|
1257
|
-
const fragmentRef = import_react4.useRef(options.fragment);
|
|
1258
|
-
const dprRef = import_react4.useRef(window.devicePixelRatio || 1);
|
|
1259
|
-
const defaultUniformsRef = import_react4.useRef({
|
|
1260
|
-
iTime: 0,
|
|
1261
|
-
iMouse: [0, 0],
|
|
1262
|
-
iMouseNormalized: [0, 0],
|
|
1263
|
-
iMouseLeftDown: 0,
|
|
1264
|
-
iResolution: [0, 0]
|
|
1265
|
-
});
|
|
1266
|
-
uniformsRef.current = options.uniforms;
|
|
1267
|
-
onErrorRef.current = options.onError;
|
|
1268
|
-
onFrameRef.current = options.onFrame;
|
|
1269
|
-
onClickRef.current = options.onClick;
|
|
1270
|
-
onMouseDownRef.current = options.onMouseDown;
|
|
1271
|
-
onMouseUpRef.current = options.onMouseUp;
|
|
1272
|
-
onMouseMoveRef.current = options.onMouseMove;
|
|
1273
|
-
onMouseWheelRef.current = options.onMouseWheel;
|
|
1274
|
-
timeScaleRef.current = options.timeScale ?? 1;
|
|
1275
|
-
vertexRef.current = options.vertex;
|
|
1276
|
-
fragmentRef.current = options.fragment;
|
|
1277
|
-
const render = import_react4.useCallback((time) => {
|
|
1278
|
-
if (contextLostRef.current)
|
|
1279
|
-
return;
|
|
1280
|
-
const state = stateRef.current;
|
|
1281
|
-
const canvas = canvasRef.current;
|
|
1282
|
-
if (!state || !canvas)
|
|
1283
|
-
return;
|
|
1284
|
-
const deltaTime = lastFrameTimeRef.current === 0 ? 0 : (time - lastFrameTimeRef.current) / 1000;
|
|
1285
|
-
lastFrameTimeRef.current = time;
|
|
1286
|
-
elapsedTimeRef.current += deltaTime * timeScaleRef.current;
|
|
1287
|
-
const { gl, program, positionAttributeLocation, uniformLocationCache, textureManager } = state;
|
|
1288
|
-
const elapsedTime = elapsedTimeRef.current;
|
|
1289
|
-
const dpr = dprRef.current;
|
|
1290
|
-
const displayWidth = canvas.clientWidth;
|
|
1291
|
-
const displayHeight = canvas.clientHeight;
|
|
1292
|
-
if (displayWidth === 0 || displayHeight === 0) {
|
|
1293
|
-
animationFrameRef.current = requestAnimationFrame(render);
|
|
1294
|
-
return;
|
|
1295
|
-
}
|
|
1296
|
-
const bufferWidth = Math.round(displayWidth * dpr);
|
|
1297
|
-
const bufferHeight = Math.round(displayHeight * dpr);
|
|
1298
|
-
if (canvas.width !== bufferWidth || canvas.height !== bufferHeight) {
|
|
1299
|
-
canvas.width = bufferWidth;
|
|
1300
|
-
canvas.height = bufferHeight;
|
|
1301
|
-
gl.viewport(0, 0, bufferWidth, bufferHeight);
|
|
1302
|
-
}
|
|
1303
|
-
gl.clearColor(0, 0, 0, 1);
|
|
1304
|
-
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
1305
|
-
gl.useProgram(program);
|
|
1306
|
-
gl.enableVertexAttribArray(positionAttributeLocation);
|
|
1307
|
-
gl.bindBuffer(gl.ARRAY_BUFFER, state.positionBuffer);
|
|
1308
|
-
gl.vertexAttribPointer(positionAttributeLocation, 2, gl.FLOAT, false, 0, 0);
|
|
1309
|
-
const defaultUniforms = defaultUniformsRef.current;
|
|
1310
|
-
defaultUniforms.iTime = elapsedTime;
|
|
1311
|
-
defaultUniforms.iMouse = mouseRef.current;
|
|
1312
|
-
defaultUniforms.iMouseNormalized = mouseNormalizedRef.current;
|
|
1313
|
-
defaultUniforms.iMouseLeftDown = mouseLeftDownRef.current ? 1 : 0;
|
|
1314
|
-
defaultUniforms.iResolution = [canvas.width, canvas.height];
|
|
1315
|
-
setUniforms(gl, program, { ...defaultUniforms, ...uniformsRef.current }, uniformLocationCache, textureManager);
|
|
1316
|
-
gl.drawArrays(gl.TRIANGLES, 0, 6);
|
|
1317
|
-
if (onFrameRef.current) {
|
|
1318
|
-
onFrameRef.current({
|
|
1319
|
-
deltaTime,
|
|
1320
|
-
time: elapsedTime,
|
|
1321
|
-
resolution: [canvas.width, canvas.height],
|
|
1322
|
-
mouse: mouseRef.current,
|
|
1323
|
-
mouseNormalized: mouseNormalizedRef.current,
|
|
1324
|
-
mouseLeftDown: mouseLeftDownRef.current
|
|
1325
|
-
});
|
|
1326
|
-
}
|
|
1327
|
-
animationFrameRef.current = requestAnimationFrame(render);
|
|
1328
|
-
}, []);
|
|
1329
|
-
import_react4.useEffect(() => {
|
|
1330
|
-
const canvas = canvasRef.current;
|
|
1331
|
-
if (!canvas)
|
|
1332
|
-
return;
|
|
1333
|
-
const initialize = () => {
|
|
1334
|
-
try {
|
|
1335
|
-
stateRef.current = initializeWebGL(canvas, vertexRef.current, fragmentRef.current, uniformsRef.current);
|
|
1336
|
-
elapsedTimeRef.current = 0;
|
|
1337
|
-
lastFrameTimeRef.current = 0;
|
|
1338
|
-
contextLostRef.current = false;
|
|
1339
|
-
animationFrameRef.current = requestAnimationFrame(render);
|
|
1340
|
-
} catch (err) {
|
|
1341
|
-
const error = err instanceof Error ? err : new Error(String(err));
|
|
1342
|
-
if (onErrorRef.current) {
|
|
1343
|
-
onErrorRef.current(error);
|
|
1344
|
-
} else {
|
|
1345
|
-
console.error("WebGL initialization failed:", error);
|
|
1346
|
-
}
|
|
1347
|
-
}
|
|
1348
|
-
};
|
|
1349
|
-
const handleContextLost = (event) => {
|
|
1350
|
-
event.preventDefault();
|
|
1351
|
-
contextLostRef.current = true;
|
|
1352
|
-
cancelAnimationFrame(animationFrameRef.current);
|
|
1353
|
-
stateRef.current = null;
|
|
1354
|
-
};
|
|
1355
|
-
const handleContextRestored = () => {
|
|
1356
|
-
initialize();
|
|
1357
|
-
};
|
|
1358
|
-
const dprMediaQuery = window.matchMedia(`(resolution: ${dprRef.current}dppx)`);
|
|
1359
|
-
const handleDprChange = () => {
|
|
1360
|
-
dprRef.current = window.devicePixelRatio || 1;
|
|
1361
|
-
};
|
|
1362
|
-
dprMediaQuery.addEventListener("change", handleDprChange);
|
|
1363
|
-
canvas.addEventListener("webglcontextlost", handleContextLost);
|
|
1364
|
-
canvas.addEventListener("webglcontextrestored", handleContextRestored);
|
|
1365
|
-
initialize();
|
|
1366
|
-
return () => {
|
|
1367
|
-
dprMediaQuery.removeEventListener("change", handleDprChange);
|
|
1368
|
-
canvas.removeEventListener("webglcontextlost", handleContextLost);
|
|
1369
|
-
canvas.removeEventListener("webglcontextrestored", handleContextRestored);
|
|
1370
|
-
cancelAnimationFrame(animationFrameRef.current);
|
|
1371
|
-
if (stateRef.current) {
|
|
1372
|
-
cleanupWebGL(stateRef.current.gl, stateRef.current);
|
|
1373
|
-
stateRef.current = null;
|
|
1374
|
-
}
|
|
1375
|
-
};
|
|
1376
|
-
}, [render]);
|
|
1377
|
-
import_react4.useEffect(() => {
|
|
1378
|
-
const canvas = canvasRef.current;
|
|
1379
|
-
if (!canvas)
|
|
1380
|
-
return;
|
|
1381
|
-
const updateRect = () => {
|
|
1382
|
-
canvasRectRef.current = canvas.getBoundingClientRect();
|
|
1383
|
-
};
|
|
1384
|
-
updateRect();
|
|
1385
|
-
const resizeObserver = new ResizeObserver(updateRect);
|
|
1386
|
-
resizeObserver.observe(canvas);
|
|
1387
|
-
window.addEventListener("scroll", updateRect, { passive: true });
|
|
1388
|
-
const handleMouseMove = (event) => {
|
|
1389
|
-
const rect = canvasRectRef.current;
|
|
1390
|
-
if (!rect)
|
|
1391
|
-
return;
|
|
1392
|
-
const dpr = dprRef.current;
|
|
1393
|
-
const x = (event.clientX - rect.left) * dpr;
|
|
1394
|
-
const y = (rect.height - (event.clientY - rect.top)) * dpr;
|
|
1395
|
-
mouseRef.current = [x, y];
|
|
1396
|
-
const minDimension = Math.min(canvas.width, canvas.height) || 1;
|
|
1397
|
-
mouseNormalizedRef.current = [
|
|
1398
|
-
(mouseRef.current[0] - canvas.width / 2) / minDimension,
|
|
1399
|
-
(mouseRef.current[1] - canvas.height / 2) / minDimension
|
|
1400
|
-
];
|
|
1401
|
-
onMouseMoveRef.current?.({
|
|
1402
|
-
deltaTime: 0,
|
|
1403
|
-
time: elapsedTimeRef.current,
|
|
1404
|
-
resolution: [canvas.width, canvas.height],
|
|
1405
|
-
mouse: mouseRef.current,
|
|
1406
|
-
mouseNormalized: mouseNormalizedRef.current,
|
|
1407
|
-
mouseLeftDown: mouseLeftDownRef.current
|
|
1408
|
-
});
|
|
1409
|
-
};
|
|
1410
|
-
const handleMouseDown = (event) => {
|
|
1411
|
-
if (event.button === 0) {
|
|
1412
|
-
mouseLeftDownRef.current = true;
|
|
1413
|
-
}
|
|
1414
|
-
onMouseDownRef.current?.({
|
|
1415
|
-
deltaTime: 0,
|
|
1416
|
-
time: elapsedTimeRef.current,
|
|
1417
|
-
resolution: [canvas.width, canvas.height],
|
|
1418
|
-
mouse: mouseRef.current,
|
|
1419
|
-
mouseNormalized: mouseNormalizedRef.current,
|
|
1420
|
-
mouseLeftDown: mouseLeftDownRef.current
|
|
1421
|
-
});
|
|
1422
|
-
};
|
|
1423
|
-
const handleMouseUp = (event) => {
|
|
1424
|
-
if (event.button === 0) {
|
|
1425
|
-
mouseLeftDownRef.current = false;
|
|
1426
|
-
}
|
|
1427
|
-
onMouseUpRef.current?.({
|
|
1428
|
-
deltaTime: 0,
|
|
1429
|
-
time: elapsedTimeRef.current,
|
|
1430
|
-
resolution: [canvas.width, canvas.height],
|
|
1431
|
-
mouse: mouseRef.current,
|
|
1432
|
-
mouseNormalized: mouseNormalizedRef.current,
|
|
1433
|
-
mouseLeftDown: mouseLeftDownRef.current
|
|
1434
|
-
});
|
|
1435
|
-
};
|
|
1436
|
-
const handleClick = () => {
|
|
1437
|
-
if (!onClickRef.current)
|
|
1438
|
-
return;
|
|
1439
|
-
onClickRef.current({
|
|
1440
|
-
deltaTime: 0,
|
|
1441
|
-
time: elapsedTimeRef.current,
|
|
1442
|
-
resolution: [canvas.width, canvas.height],
|
|
1443
|
-
mouse: mouseRef.current,
|
|
1444
|
-
mouseNormalized: mouseNormalizedRef.current,
|
|
1445
|
-
mouseLeftDown: mouseLeftDownRef.current
|
|
1446
|
-
});
|
|
1447
|
-
};
|
|
1448
|
-
const handleMouseWheel = (event) => {
|
|
1449
|
-
onMouseWheelRef.current?.({
|
|
1450
|
-
deltaTime: 0,
|
|
1451
|
-
time: elapsedTimeRef.current,
|
|
1452
|
-
resolution: [canvas.width, canvas.height],
|
|
1453
|
-
mouse: mouseRef.current,
|
|
1454
|
-
mouseNormalized: mouseNormalizedRef.current,
|
|
1455
|
-
mouseLeftDown: mouseLeftDownRef.current
|
|
1456
|
-
}, event.deltaY);
|
|
1457
|
-
};
|
|
1458
|
-
window.addEventListener("mousemove", handleMouseMove);
|
|
1459
|
-
window.addEventListener("mousedown", handleMouseDown);
|
|
1460
|
-
window.addEventListener("mouseup", handleMouseUp);
|
|
1461
|
-
canvas.addEventListener("click", handleClick);
|
|
1462
|
-
window.addEventListener("wheel", handleMouseWheel);
|
|
1463
|
-
return () => {
|
|
1464
|
-
resizeObserver.disconnect();
|
|
1465
|
-
window.removeEventListener("scroll", updateRect);
|
|
1466
|
-
window.removeEventListener("mousemove", handleMouseMove);
|
|
1467
|
-
window.removeEventListener("mousedown", handleMouseDown);
|
|
1468
|
-
window.removeEventListener("mouseup", handleMouseUp);
|
|
1469
|
-
canvas.removeEventListener("click", handleClick);
|
|
1470
|
-
window.removeEventListener("wheel", handleMouseWheel);
|
|
1471
|
-
};
|
|
1472
|
-
}, []);
|
|
1473
|
-
return { canvasRef, mouseRef };
|
|
1474
|
-
}
|
|
1475
|
-
|
|
1476
|
-
// src/ReactShader.tsx
|
|
1477
|
-
var jsx_dev_runtime2 = require("react/jsx-dev-runtime");
|
|
1478
|
-
var DEFAULT_VERTEX = `#version 300 es
|
|
1479
|
-
precision highp float;
|
|
1480
|
-
in vec2 a_position;
|
|
1481
|
-
|
|
1482
|
-
void main() {
|
|
1483
|
-
gl_Position = vec4(a_position, 0.0, 1.0);
|
|
1484
|
-
}
|
|
1485
|
-
`;
|
|
1486
|
-
var FULLSCREEN_CONTAINER_STYLE2 = {
|
|
1487
|
-
position: "fixed",
|
|
1488
|
-
top: 0,
|
|
1489
|
-
left: 0,
|
|
1490
|
-
width: "100vw",
|
|
1491
|
-
height: "100vh",
|
|
1492
|
-
zIndex: 9000
|
|
1493
|
-
};
|
|
1494
|
-
var DEFAULT_CONTAINER_STYLE2 = {
|
|
1495
|
-
position: "relative",
|
|
1496
|
-
width: "100%",
|
|
1497
|
-
height: "100%"
|
|
1498
|
-
};
|
|
1499
|
-
var CANVAS_STYLE2 = {
|
|
1500
|
-
display: "block",
|
|
1501
|
-
width: "100%",
|
|
1502
|
-
height: "100%"
|
|
1503
|
-
};
|
|
1504
|
-
function ReactShader({
|
|
1505
|
-
className,
|
|
1506
|
-
fragment,
|
|
1507
|
-
vertex = DEFAULT_VERTEX,
|
|
1508
|
-
uniforms,
|
|
1509
|
-
fullscreen = false,
|
|
1510
|
-
timeScale = 1,
|
|
1511
|
-
onFrame,
|
|
1512
|
-
onClick,
|
|
1513
|
-
onMouseMove,
|
|
1514
|
-
onMouseDown,
|
|
1515
|
-
onMouseUp,
|
|
1516
|
-
onMouseWheel
|
|
1517
|
-
}) {
|
|
1518
|
-
const [error, setError] = import_react5.useState(null);
|
|
1519
|
-
const handleError = import_react5.useCallback((err) => {
|
|
1520
|
-
setError(err.message);
|
|
1521
|
-
console.error("ReactShader error:", err);
|
|
1522
|
-
}, []);
|
|
1523
|
-
import_react5.useEffect(() => {
|
|
1524
|
-
setError(null);
|
|
1525
|
-
}, [fragment, vertex]);
|
|
1526
|
-
const { canvasRef } = useWebGL({
|
|
1527
|
-
fragment,
|
|
1528
|
-
vertex,
|
|
1529
|
-
uniforms,
|
|
1530
|
-
onError: handleError,
|
|
1531
|
-
onFrame,
|
|
1532
|
-
onClick,
|
|
1533
|
-
onMouseMove,
|
|
1534
|
-
onMouseDown,
|
|
1535
|
-
onMouseUp,
|
|
1536
|
-
onMouseWheel,
|
|
1537
|
-
timeScale
|
|
1538
|
-
});
|
|
1539
|
-
const containerStyle = import_react5.useMemo(() => fullscreen ? FULLSCREEN_CONTAINER_STYLE2 : DEFAULT_CONTAINER_STYLE2, [fullscreen]);
|
|
1540
|
-
if (error) {
|
|
1541
|
-
return /* @__PURE__ */ jsx_dev_runtime2.jsxDEV("div", {
|
|
1542
|
-
className,
|
|
1543
|
-
style: {
|
|
1544
|
-
...containerStyle,
|
|
1545
|
-
display: "flex",
|
|
1546
|
-
alignItems: "center",
|
|
1547
|
-
justifyContent: "center",
|
|
1548
|
-
backgroundColor: "#1a1a1a",
|
|
1549
|
-
color: "#ff6b6b",
|
|
1550
|
-
fontFamily: "monospace",
|
|
1551
|
-
fontSize: "12px",
|
|
1552
|
-
padding: "16px",
|
|
1553
|
-
overflow: "auto",
|
|
1554
|
-
boxSizing: "border-box",
|
|
1555
|
-
width: "100%",
|
|
1556
|
-
height: "100%"
|
|
1557
|
-
},
|
|
1558
|
-
children: /* @__PURE__ */ jsx_dev_runtime2.jsxDEV("pre", {
|
|
1559
|
-
style: { margin: 0, whiteSpace: "pre-wrap" },
|
|
1560
|
-
children: error
|
|
1561
|
-
}, undefined, false, undefined, this)
|
|
1562
|
-
}, undefined, false, undefined, this);
|
|
1563
|
-
}
|
|
1564
|
-
return /* @__PURE__ */ jsx_dev_runtime2.jsxDEV("canvas", {
|
|
1565
|
-
ref: canvasRef,
|
|
1566
|
-
className,
|
|
1567
|
-
style: CANVAS_STYLE2
|
|
1568
|
-
}, undefined, false, undefined, this);
|
|
1569
|
-
}
|
|
1570
|
-
// src/shaders/color-palette.ts
|
|
1571
|
-
function generateColorPaletteFunction(name, paletteString) {
|
|
1572
|
-
const paletteArray = paletteString.replace("[[", "").replace("]]", "").split("] [").map((s) => s.split(" "));
|
|
1573
|
-
return `
|
|
1574
|
-
vec3 ${name}( float t ) {
|
|
1575
|
-
vec3 a = vec3(${paletteArray[0].join(",")});
|
|
1576
|
-
vec3 b = vec3(${paletteArray[1].join(",")});
|
|
1577
|
-
vec3 c = vec3(${paletteArray[2].join(",")});
|
|
1578
|
-
vec3 d = vec3(${paletteArray[3].join(",")});
|
|
1579
|
-
return a + b * cos(6.28318 * (c * t + d));
|
|
1580
|
-
}
|
|
1581
|
-
`;
|
|
1582
|
-
}
|
|
1583
862
|
// src/shaders/color-palette-gpu.ts
|
|
1584
863
|
function generateColorPaletteFunctionGpu(name, paletteString) {
|
|
1585
864
|
const paletteArray = paletteString.replace("[[", "").replace("]]", "").split("] [").map((s) => s.split(" "));
|
|
@@ -1593,29 +872,6 @@ fn ${name}(t: f32) -> vec3f {
|
|
|
1593
872
|
}
|
|
1594
873
|
`;
|
|
1595
874
|
}
|
|
1596
|
-
// src/shaders/distortion-ripple.ts
|
|
1597
|
-
function generateDistortionRippleFunction() {
|
|
1598
|
-
return `
|
|
1599
|
-
vec2 DistortionRipple(vec2 uv, vec2 center, float radius, float intensity, float thickness) {
|
|
1600
|
-
// 1. Calculate vector and distance from center
|
|
1601
|
-
vec2 dir = uv - center;
|
|
1602
|
-
float dist = length(dir);
|
|
1603
|
-
|
|
1604
|
-
// 2. Create a mask so the ripple only exists near the radius Z
|
|
1605
|
-
// Using smoothstep creates a soft edge for the ripple
|
|
1606
|
-
float mask = smoothstep(radius + thickness, radius, dist) * smoothstep(radius - thickness, radius, dist);
|
|
1607
|
-
|
|
1608
|
-
// 3. Calculate the displacement amount using a Sine wave
|
|
1609
|
-
// We subtract dist from radius to orient the wave correctly
|
|
1610
|
-
float wave = sin((dist - radius) * 20.0);
|
|
1611
|
-
|
|
1612
|
-
// 4. Apply intensity and mask, then offset the UV
|
|
1613
|
-
vec2 offset = normalize(dir) * wave * intensity * mask;
|
|
1614
|
-
|
|
1615
|
-
return offset;
|
|
1616
|
-
}
|
|
1617
|
-
`;
|
|
1618
|
-
}
|
|
1619
875
|
// src/shaders/distortion-ripple-gpu.ts
|
|
1620
876
|
function generateDistortionRippleFunctionGpu() {
|
|
1621
877
|
return `
|
|
@@ -1639,40 +895,6 @@ fn DistortionRipple(uv: vec2f, center: vec2f, radius: f32, intensity: f32, thick
|
|
|
1639
895
|
}
|
|
1640
896
|
`;
|
|
1641
897
|
}
|
|
1642
|
-
// src/shaders/scene-circles.ts
|
|
1643
|
-
function generateSceneCirclesFunction(paletteString = "[[0.5 0.5 0.5] [0.5 0.5 0.5] [1.0 1.0 1.0] [0.263 0.416 0.557]]") {
|
|
1644
|
-
return `
|
|
1645
|
-
${generateColorPaletteFunction("circlesPalette", paletteString)}
|
|
1646
|
-
|
|
1647
|
-
vec3 SceneCircles(
|
|
1648
|
-
vec2 uv0,
|
|
1649
|
-
float iterations,
|
|
1650
|
-
float fractMultiplier,
|
|
1651
|
-
float time,
|
|
1652
|
-
float waveLength,
|
|
1653
|
-
float edgeBlur,
|
|
1654
|
-
float contrast
|
|
1655
|
-
) {
|
|
1656
|
-
vec3 col = vec3(0.0);
|
|
1657
|
-
vec2 uv = uv0;
|
|
1658
|
-
|
|
1659
|
-
for (float i = 0.0; i < iterations; i++) {
|
|
1660
|
-
uv = fract(uv * fractMultiplier) - 0.5;
|
|
1661
|
-
|
|
1662
|
-
float d = length(uv) * exp(-length(uv0));
|
|
1663
|
-
|
|
1664
|
-
vec3 color = circlesPalette(length(uv0) + i * 0.4 + time * 0.4);
|
|
1665
|
-
|
|
1666
|
-
d = sin(d * waveLength + time) / waveLength;
|
|
1667
|
-
d = abs(d);
|
|
1668
|
-
d = pow(edgeBlur / d, contrast);
|
|
1669
|
-
|
|
1670
|
-
col += color * d;
|
|
1671
|
-
}
|
|
1672
|
-
return col;
|
|
1673
|
-
}
|
|
1674
|
-
`;
|
|
1675
|
-
}
|
|
1676
898
|
// src/shaders/scene-circles-gpu.ts
|
|
1677
899
|
function generateSceneCirclesFunctionGpu(paletteString = "[[0.5 0.5 0.5] [0.5 0.5 0.5] [1.0 1.0 1.0] [0.263 0.416 0.557]]") {
|
|
1678
900
|
return `
|
|
@@ -1711,190 +933,6 @@ fn SceneCircles(
|
|
|
1711
933
|
}
|
|
1712
934
|
`;
|
|
1713
935
|
}
|
|
1714
|
-
// src/shaders/simplex-noise.ts
|
|
1715
|
-
function generateSimplexNoiseFunction() {
|
|
1716
|
-
return `
|
|
1717
|
-
|
|
1718
|
-
vec4 mod289(vec4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
|
|
1719
|
-
|
|
1720
|
-
vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
|
|
1721
|
-
|
|
1722
|
-
float mod289(float x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
|
|
1723
|
-
|
|
1724
|
-
vec4 permute(vec4 x) { return mod289(((x*34.0)+10.0)*x); }
|
|
1725
|
-
|
|
1726
|
-
float permute(float x) { return mod289(((x*34.0)+10.0)*x); }
|
|
1727
|
-
|
|
1728
|
-
vec4 taylorInvSqrt(vec4 r) { return 1.79284291400159 - 0.85373472095314 * r; }
|
|
1729
|
-
|
|
1730
|
-
float taylorInvSqrt(float r) { return 1.79284291400159 - 0.85373472095314 * r; }
|
|
1731
|
-
|
|
1732
|
-
vec4 grad4(float j, vec4 ip) {
|
|
1733
|
-
const vec4 ones = vec4(1.0, 1.0, 1.0, -1.0);
|
|
1734
|
-
vec4 p,s;
|
|
1735
|
-
|
|
1736
|
-
p.xyz = floor( fract (vec3(j) * ip.xyz) * 7.0) * ip.z - 1.0;
|
|
1737
|
-
p.w = 1.5 - dot(abs(p.xyz), ones.xyz);
|
|
1738
|
-
s = vec4(lessThan(p, vec4(0.0)));
|
|
1739
|
-
p.xyz = p.xyz + (s.xyz*2.0 - 1.0) * s.www;
|
|
1740
|
-
|
|
1741
|
-
return p;
|
|
1742
|
-
}
|
|
1743
|
-
|
|
1744
|
-
// (sqrt(5) - 1)/4 = F4, used once below
|
|
1745
|
-
#define F4 0.309016994374947451
|
|
1746
|
-
|
|
1747
|
-
float SimplexNoise4D(vec4 v) {
|
|
1748
|
-
const vec4 C = vec4( 0.138196601125011, // (5 - sqrt(5))/20 G4
|
|
1749
|
-
0.276393202250021, // 2 * G4
|
|
1750
|
-
0.414589803375032, // 3 * G4
|
|
1751
|
-
-0.447213595499958); // -1 + 4 * G4
|
|
1752
|
-
|
|
1753
|
-
// First corner
|
|
1754
|
-
vec4 i = floor(v + dot(v, vec4(F4)) );
|
|
1755
|
-
vec4 x0 = v - i + dot(i, C.xxxx);
|
|
1756
|
-
|
|
1757
|
-
// Other corners
|
|
1758
|
-
|
|
1759
|
-
// Rank sorting originally contributed by Bill Licea-Kane, AMD (formerly ATI)
|
|
1760
|
-
vec4 i0;
|
|
1761
|
-
vec3 isX = step( x0.yzw, x0.xxx );
|
|
1762
|
-
vec3 isYZ = step( x0.zww, x0.yyz );
|
|
1763
|
-
// i0.x = dot( isX, vec3( 1.0 ) );
|
|
1764
|
-
i0.x = isX.x + isX.y + isX.z;
|
|
1765
|
-
i0.yzw = 1.0 - isX;
|
|
1766
|
-
// i0.y += dot( isYZ.xy, vec2( 1.0 ) );
|
|
1767
|
-
i0.y += isYZ.x + isYZ.y;
|
|
1768
|
-
i0.zw += 1.0 - isYZ.xy;
|
|
1769
|
-
i0.z += isYZ.z;
|
|
1770
|
-
i0.w += 1.0 - isYZ.z;
|
|
1771
|
-
|
|
1772
|
-
// i0 now contains the unique values 0,1,2,3 in each channel
|
|
1773
|
-
vec4 i3 = clamp( i0, 0.0, 1.0 );
|
|
1774
|
-
vec4 i2 = clamp( i0-1.0, 0.0, 1.0 );
|
|
1775
|
-
vec4 i1 = clamp( i0-2.0, 0.0, 1.0 );
|
|
1776
|
-
|
|
1777
|
-
// x0 = x0 - 0.0 + 0.0 * C.xxxx
|
|
1778
|
-
// x1 = x0 - i1 + 1.0 * C.xxxx
|
|
1779
|
-
// x2 = x0 - i2 + 2.0 * C.xxxx
|
|
1780
|
-
// x3 = x0 - i3 + 3.0 * C.xxxx
|
|
1781
|
-
// x4 = x0 - 1.0 + 4.0 * C.xxxx
|
|
1782
|
-
vec4 x1 = x0 - i1 + C.xxxx;
|
|
1783
|
-
vec4 x2 = x0 - i2 + C.yyyy;
|
|
1784
|
-
vec4 x3 = x0 - i3 + C.zzzz;
|
|
1785
|
-
vec4 x4 = x0 + C.wwww;
|
|
1786
|
-
|
|
1787
|
-
// Permutations
|
|
1788
|
-
i = mod289(i);
|
|
1789
|
-
float j0 = permute( permute( permute( permute(i.w) + i.z) + i.y) + i.x);
|
|
1790
|
-
vec4 j1 = permute( permute( permute( permute (
|
|
1791
|
-
i.w + vec4(i1.w, i2.w, i3.w, 1.0 ))
|
|
1792
|
-
+ i.z + vec4(i1.z, i2.z, i3.z, 1.0 ))
|
|
1793
|
-
+ i.y + vec4(i1.y, i2.y, i3.y, 1.0 ))
|
|
1794
|
-
+ i.x + vec4(i1.x, i2.x, i3.x, 1.0 ));
|
|
1795
|
-
|
|
1796
|
-
// Gradients: 7x7x6 points over a cube, mapped onto a 4-cross polytope
|
|
1797
|
-
// 7*7*6 = 294, which is close to the ring size 17*17 = 289.
|
|
1798
|
-
vec4 ip = vec4(1.0/294.0, 1.0/49.0, 1.0/7.0, 0.0) ;
|
|
1799
|
-
|
|
1800
|
-
vec4 p0 = grad4(j0, ip);
|
|
1801
|
-
vec4 p1 = grad4(j1.x, ip);
|
|
1802
|
-
vec4 p2 = grad4(j1.y, ip);
|
|
1803
|
-
vec4 p3 = grad4(j1.z, ip);
|
|
1804
|
-
vec4 p4 = grad4(j1.w, ip);
|
|
1805
|
-
|
|
1806
|
-
// Normalise gradients
|
|
1807
|
-
vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
|
|
1808
|
-
p0 *= norm.x;
|
|
1809
|
-
p1 *= norm.y;
|
|
1810
|
-
p2 *= norm.z;
|
|
1811
|
-
p3 *= norm.w;
|
|
1812
|
-
p4 *= taylorInvSqrt(dot(p4,p4));
|
|
1813
|
-
|
|
1814
|
-
// Mix contributions from the five corners
|
|
1815
|
-
vec3 m0 = max(0.6 - vec3(dot(x0,x0), dot(x1,x1), dot(x2,x2)), 0.0);
|
|
1816
|
-
vec2 m1 = max(0.6 - vec2(dot(x3,x3), dot(x4,x4) ), 0.0);
|
|
1817
|
-
m0 = m0 * m0;
|
|
1818
|
-
m1 = m1 * m1;
|
|
1819
|
-
return 49.0 * ( dot(m0*m0, vec3( dot( p0, x0 ), dot( p1, x1 ), dot( p2, x2 )))
|
|
1820
|
-
+ dot(m1*m1, vec2( dot( p3, x3 ), dot( p4, x4 ) ) ) ) ;
|
|
1821
|
-
}
|
|
1822
|
-
|
|
1823
|
-
float SimplexNoise3D(vec3 v) {
|
|
1824
|
-
const vec2 C = vec2(1.0/6.0, 1.0/3.0) ;
|
|
1825
|
-
const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);
|
|
1826
|
-
|
|
1827
|
-
// First corner
|
|
1828
|
-
vec3 i = floor(v + dot(v, C.yyy) );
|
|
1829
|
-
vec3 x0 = v - i + dot(i, C.xxx) ;
|
|
1830
|
-
|
|
1831
|
-
// Other corners
|
|
1832
|
-
vec3 g = step(x0.yzx, x0.xyz);
|
|
1833
|
-
vec3 l = 1.0 - g;
|
|
1834
|
-
vec3 i1 = min( g.xyz, l.zxy );
|
|
1835
|
-
vec3 i2 = max( g.xyz, l.zxy );
|
|
1836
|
-
|
|
1837
|
-
// x0 = x0 - 0.0 + 0.0 * C.xxx;
|
|
1838
|
-
// x1 = x0 - i1 + 1.0 * C.xxx;
|
|
1839
|
-
// x2 = x0 - i2 + 2.0 * C.xxx;
|
|
1840
|
-
// x3 = x0 - 1.0 + 3.0 * C.xxx;
|
|
1841
|
-
vec3 x1 = x0 - i1 + C.xxx;
|
|
1842
|
-
vec3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
|
|
1843
|
-
vec3 x3 = x0 - D.yyy; // -1.0+3.0*C.x = -0.5 = -D.y
|
|
1844
|
-
|
|
1845
|
-
// Permutations
|
|
1846
|
-
i = mod289(i);
|
|
1847
|
-
vec4 p = permute( permute( permute(
|
|
1848
|
-
i.z + vec4(0.0, i1.z, i2.z, 1.0 ))
|
|
1849
|
-
+ i.y + vec4(0.0, i1.y, i2.y, 1.0 ))
|
|
1850
|
-
+ i.x + vec4(0.0, i1.x, i2.x, 1.0 ));
|
|
1851
|
-
|
|
1852
|
-
// Gradients: 7x7 points over a square, mapped onto an octahedron.
|
|
1853
|
-
// The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
|
|
1854
|
-
float n_ = 0.142857142857; // 1.0/7.0
|
|
1855
|
-
vec3 ns = n_ * D.wyz - D.xzx;
|
|
1856
|
-
|
|
1857
|
-
vec4 j = p - 49.0 * floor(p * ns.z * ns.z); // mod(p,7*7)
|
|
1858
|
-
|
|
1859
|
-
vec4 x_ = floor(j * ns.z);
|
|
1860
|
-
vec4 y_ = floor(j - 7.0 * x_ ); // mod(j,N)
|
|
1861
|
-
|
|
1862
|
-
vec4 x = x_ *ns.x + ns.yyyy;
|
|
1863
|
-
vec4 y = y_ *ns.x + ns.yyyy;
|
|
1864
|
-
vec4 h = 1.0 - abs(x) - abs(y);
|
|
1865
|
-
|
|
1866
|
-
vec4 b0 = vec4( x.xy, y.xy );
|
|
1867
|
-
vec4 b1 = vec4( x.zw, y.zw );
|
|
1868
|
-
|
|
1869
|
-
//vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0;
|
|
1870
|
-
//vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0;
|
|
1871
|
-
vec4 s0 = floor(b0)*2.0 + 1.0;
|
|
1872
|
-
vec4 s1 = floor(b1)*2.0 + 1.0;
|
|
1873
|
-
vec4 sh = -step(h, vec4(0.0));
|
|
1874
|
-
|
|
1875
|
-
vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
|
|
1876
|
-
vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
|
|
1877
|
-
|
|
1878
|
-
vec3 p0 = vec3(a0.xy,h.x);
|
|
1879
|
-
vec3 p1 = vec3(a0.zw,h.y);
|
|
1880
|
-
vec3 p2 = vec3(a1.xy,h.z);
|
|
1881
|
-
vec3 p3 = vec3(a1.zw,h.w);
|
|
1882
|
-
|
|
1883
|
-
// Normalise gradients
|
|
1884
|
-
vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
|
|
1885
|
-
p0 *= norm.x;
|
|
1886
|
-
p1 *= norm.y;
|
|
1887
|
-
p2 *= norm.z;
|
|
1888
|
-
p3 *= norm.w;
|
|
1889
|
-
|
|
1890
|
-
// Mix final noise value
|
|
1891
|
-
vec4 m = max(0.5 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
|
|
1892
|
-
m = m * m;
|
|
1893
|
-
return 105.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1),
|
|
1894
|
-
dot(p2,x2), dot(p3,x3) ) );
|
|
1895
|
-
}
|
|
1896
|
-
`;
|
|
1897
|
-
}
|
|
1898
936
|
// src/shaders/simplex-noise-gpu.ts
|
|
1899
937
|
function generateSimplexNoiseFunctionGpu() {
|
|
1900
938
|
return `
|
|
@@ -2070,15 +1108,3 @@ fn SimplexNoise3D(v: vec3f) -> f32 {
|
|
|
2070
1108
|
}
|
|
2071
1109
|
`;
|
|
2072
1110
|
}
|
|
2073
|
-
// src/shaders/utils.ts
|
|
2074
|
-
function generateUtilsFunction() {
|
|
2075
|
-
return `
|
|
2076
|
-
vec2 GetUv(vec2 fragCoord, vec2 resolution) {
|
|
2077
|
-
return (fragCoord - 0.5 * resolution) / resolution.y;
|
|
2078
|
-
}
|
|
2079
|
-
|
|
2080
|
-
vec2 GetMouse(vec2 mouse, vec2 resolution) {
|
|
2081
|
-
return (mouse - 0.5 * resolution) / resolution.y;
|
|
2082
|
-
}
|
|
2083
|
-
`;
|
|
2084
|
-
}
|