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