@firecms/neat 0.1.0

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 ADDED
@@ -0,0 +1,84 @@
1
+ # Neat gradients
2
+
3
+ Create awesome 3d gradients with this library based on three.js.
4
+
5
+ Check the demo and gradients editor to find your perfect config here:
6
+ [https://neat.firecms.co/](https://neat.firecms.co/)
7
+
8
+ Neat is released under the CC license, so you can use it for free in your projects,
9
+ commercial or not. You can also modify it and redistribute it, but you must keep
10
+ the license and the credits.
11
+
12
+ If you want to remove the Camberi link, you can reach us at hello@firecms.co
13
+
14
+ ### Installation:
15
+
16
+ ```
17
+ yarn install @firecms/neat three.js
18
+ ```
19
+
20
+ or
21
+
22
+ ```
23
+ npm install @firecms/neat three.js
24
+ ```
25
+
26
+ ### Usage:
27
+
28
+
29
+ ```typescript
30
+ import { NeatConfig, NeatGradient } from "@firecms/neat";
31
+
32
+ // Define your config
33
+ export const config: NeatConfig = {
34
+ colors: [
35
+ {
36
+ color: "#FF5373",
37
+ enabled: true
38
+ },
39
+ {
40
+ color: "#FFC858",
41
+ enabled: true
42
+ },
43
+ {
44
+ color: "#17E7FF",
45
+ enabled: true
46
+ },
47
+ {
48
+ color: "#6D3BFF",
49
+ enabled: true
50
+ },
51
+ {
52
+ color: "#f5e1e5",
53
+ enabled: false
54
+ }
55
+ ],
56
+ speed: 4,
57
+ horizontalPressure: 4,
58
+ verticalPressure: 5,
59
+ waveFrequencyX: 2,
60
+ waveFrequencyY: 3,
61
+ waveAmplitude: 5,
62
+ shadows: 0,
63
+ highlights: 2,
64
+ saturation: 7,
65
+ wireframe: false,
66
+ colorBlending: 6,
67
+ backgroundColor: "#003FFF",
68
+ backgroundAlpha: 1
69
+ };
70
+
71
+
72
+ // define an element with id="gradient" in your html
73
+ const neat = new NeatGradient({
74
+ ref: document.getElementById("gradient"),
75
+ ...config
76
+ });
77
+
78
+ // you can change the config at any time
79
+ neat.speed = 6;
80
+
81
+ // you can also destroy the gradient for cleanup
82
+ // e.g. returning from a useEffect hook in React
83
+ neat.destroy();
84
+ ```
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@firecms/neat",
3
+ "description": "Beautiful 3D gradients for your website",
4
+ "access": "public",
5
+ "version": "0.1.0",
6
+ "main": "./dist/index.umd.js",
7
+ "module": "./dist/index.es.js",
8
+ "types": "dist/index.d.ts",
9
+ "source": "src/index.ts",
10
+ "dependencies": {
11
+ "typescript": "^4.8.4"
12
+ },
13
+ "peerDependencies": {
14
+ "three": "^0.144.0"
15
+ },
16
+ "devDependencies": {
17
+ "vite": "^3.1.4",
18
+ "three": "^0.144.0"
19
+ },
20
+ "scripts": {
21
+ "start": "../node_modules/.bin/vite",
22
+ "build": "../node_modules/.bin/vite build"
23
+ },
24
+ "browserslist": {
25
+ "production": [
26
+ ">0.2%",
27
+ "not dead",
28
+ "not op_mini all"
29
+ ],
30
+ "development": [
31
+ "last 1 chrome version",
32
+ "last 1 firefox version",
33
+ "last 1 safari version"
34
+ ]
35
+ },
36
+ "repository": {
37
+ "type": "git",
38
+ "url": "git+https://github.com/Camberi/neat.git"
39
+ },
40
+ "keywords": [
41
+ "threejs",
42
+ "3d",
43
+ "gradient",
44
+ "background"
45
+ ],
46
+ "author": "Francesco Gatti",
47
+ "license": "ISC",
48
+ "bugs": {
49
+ "url": "https://github.com/Camberi/neat/issues"
50
+ },
51
+ "homepage": "https://github.com/Camberi/neat#readme",
52
+ "publishConfig": {
53
+ "access": "public"
54
+ }
55
+ }
@@ -0,0 +1,739 @@
1
+ import * as THREE from "three";
2
+
3
+ const PLANE_WIDTH = 50;
4
+ const PLANE_HEIGHT = 80;
5
+
6
+ const WIREFRAME = true;
7
+ const COLORS_COUNT = 5;
8
+
9
+ const clock = new THREE.Clock();
10
+
11
+ type SceneState = {
12
+ renderer: THREE.WebGLRenderer,
13
+ camera: THREE.OrthographicCamera,
14
+ scene: THREE.Scene,
15
+ meshes: THREE.Mesh[],
16
+ }
17
+
18
+ export type NeatConfig = {
19
+ speed?: number;
20
+ horizontalPressure?: number;
21
+ verticalPressure?: number;
22
+ waveFrequencyX?: number;
23
+ waveFrequencyY?: number;
24
+ waveAmplitude?: number;
25
+ highlights?: number;
26
+ shadows?: number;
27
+ saturation?: number;
28
+ colors: NeatColor[];
29
+ colorBlending?: number;
30
+ wireframe?: boolean;
31
+ backgroundColor?: string;
32
+ backgroundAlpha?: number;
33
+ };
34
+
35
+ export type NeatColor = {
36
+ color: string;
37
+ enabled: boolean;
38
+ /**
39
+ * Value from 0 to 1
40
+ */
41
+ influence?: number;
42
+ }
43
+
44
+ export type NeatController = {
45
+ destroy: () => void;
46
+ }
47
+
48
+ export class NeatGradient implements NeatController {
49
+
50
+ private _speed: number = -1;
51
+
52
+ private _horizontalPressure: number = -1;
53
+ private _verticalPressure: number = -1;
54
+
55
+ private _waveFrequencyX: number = -1;
56
+ private _waveFrequencyY: number = -1;
57
+ private _waveAmplitude: number = -1;
58
+
59
+ private _shadows: number = -1;
60
+ private _highlights: number = -1;
61
+ private _saturation: number = -1;
62
+
63
+ private _colorBlending: number = -1;
64
+
65
+ private _colors: NeatColor[] = [];
66
+ private _wireframe: boolean = false;
67
+
68
+ private _backgroundColor: string = "#FFFFFF";
69
+ private _backgroundAlpha: number = 1.0;
70
+
71
+ private requestRef: number = -1;
72
+ private sizeObserver: ResizeObserver;
73
+ private readonly sceneState: SceneState;
74
+
75
+ constructor(config: NeatConfig & { ref: HTMLCanvasElement }) {
76
+
77
+ const {
78
+ ref,
79
+ speed = 4,
80
+ horizontalPressure = 3,
81
+ verticalPressure = 3,
82
+ waveFrequencyX = 5,
83
+ waveFrequencyY = 5,
84
+ waveAmplitude = 3,
85
+ colors,
86
+ highlights = 4,
87
+ shadows = 4,
88
+ saturation = 0,
89
+ colorBlending = 5,
90
+ wireframe = false,
91
+ backgroundColor = "#FFFFFF",
92
+ backgroundAlpha = 1.0,
93
+ } = config;
94
+
95
+ const width = ref.width,
96
+ height = ref.height;
97
+
98
+ this.destroy = this.destroy.bind(this);
99
+ this._initScene = this._initScene.bind(this);
100
+ this._buildMaterial = this._buildMaterial.bind(this);
101
+
102
+ this.speed = speed;
103
+ this.horizontalPressure = horizontalPressure;
104
+ this.verticalPressure = verticalPressure;
105
+ this.waveFrequencyX = waveFrequencyX;
106
+ this.waveFrequencyY = waveFrequencyY;
107
+ this.waveAmplitude = waveAmplitude;
108
+ this.colorBlending = colorBlending;
109
+ this.colors = colors;
110
+ this.shadows = shadows;
111
+ this.highlights = highlights;
112
+ this.saturation = saturation;
113
+ this.wireframe = wireframe;
114
+ this.backgroundColor = backgroundColor;
115
+ this.backgroundAlpha = backgroundAlpha;
116
+
117
+ this.sceneState = this._initScene(ref, width, height);
118
+
119
+ const { renderer, camera, scene, meshes } = this.sceneState;
120
+
121
+ let tick = 0;
122
+ const render = () => {
123
+
124
+ if (Math.floor(tick * 10) % 5 === 0) {
125
+ addNeatLink(ref);
126
+ }
127
+
128
+ renderer.setClearColor(this._backgroundColor, this._backgroundAlpha);
129
+ meshes.forEach((mesh) => {
130
+
131
+ const colors = [
132
+ ...this._colors.map(color => ({
133
+ is_active: color.enabled,
134
+ color: new THREE.Color(color.color),
135
+ influence: color.influence
136
+ })),
137
+ ...Array.from({ length: COLORS_COUNT - this._colors.length }).map(() => ({
138
+ is_active: false,
139
+ color: new THREE.Color(0x000000)
140
+ }))
141
+ ];
142
+
143
+ tick += clock.getDelta() * this._speed;
144
+ // @ts-ignore
145
+ mesh.material.uniforms.u_time.value = tick;
146
+ // @ts-ignore
147
+ mesh.material.uniforms.u_resolution = { value: new THREE.Vector2(width, height) };
148
+ // @ts-ignore
149
+ mesh.material.uniforms.u_color_pressure = { value: new THREE.Vector2(this._horizontalPressure, this._verticalPressure) };
150
+ // @ts-ignore
151
+ mesh.material.uniforms.u_wave_frequency_x = { value: this._waveFrequencyX };
152
+ // @ts-ignore
153
+ mesh.material.uniforms.u_wave_frequency_y = { value: this._waveFrequencyY };
154
+ // @ts-ignore
155
+ mesh.material.uniforms.u_wave_amplitude = { value: this._waveAmplitude };
156
+ // @ts-ignore
157
+ mesh.material.uniforms.u_plane_width = { value: PLANE_WIDTH };
158
+ // @ts-ignore
159
+ mesh.material.uniforms.u_plane_height = { value: PLANE_HEIGHT };
160
+ // @ts-ignore
161
+ mesh.material.uniforms.u_color_blending = { value: this._colorBlending };
162
+ // @ts-ignore
163
+ mesh.material.uniforms.u_colors = { value: colors };
164
+ // @ts-ignore
165
+ mesh.material.uniforms.u_colors_count = { value: COLORS_COUNT };
166
+ // @ts-ignore
167
+ mesh.material.uniforms.u_shadows = { value: this._shadows };
168
+ // @ts-ignore
169
+ mesh.material.uniforms.u_highlights = { value: this._highlights };
170
+ // @ts-ignore
171
+ mesh.material.uniforms.u_saturation = { value: this._saturation };
172
+ // @ts-ignore
173
+ mesh.material.wireframe = this._wireframe;
174
+ });
175
+
176
+ renderer.render(scene, camera);
177
+ this.requestRef = requestAnimationFrame(render);
178
+ };
179
+
180
+ const setSize = () => {
181
+ const canvas = renderer.domElement;
182
+ const width = canvas.clientWidth;
183
+ const height = canvas.clientHeight;
184
+
185
+ this.sceneState.renderer.setSize(width, height, false);
186
+ updateCamera(this.sceneState.camera, width, height);
187
+ };
188
+
189
+ this.sizeObserver = new ResizeObserver(entries => {
190
+ setSize();
191
+ });
192
+
193
+ this.sizeObserver.observe(ref);
194
+
195
+
196
+ render();
197
+ }
198
+
199
+ destroy() {
200
+ if (this) {
201
+ cancelAnimationFrame(this.requestRef);
202
+ this.sizeObserver.disconnect();
203
+ }
204
+ }
205
+
206
+ set speed(speed: number) {
207
+ this._speed = speed / 20;
208
+ }
209
+
210
+ set horizontalPressure(horizontalPressure: number) {
211
+ this._horizontalPressure = horizontalPressure / 4;
212
+ }
213
+
214
+ set verticalPressure(verticalPressure: number) {
215
+ this._verticalPressure = verticalPressure / 4;
216
+ }
217
+
218
+ set waveFrequencyX(waveFrequencyX: number) {
219
+ this._waveFrequencyX = waveFrequencyX * 0.04;
220
+ }
221
+
222
+ set waveFrequencyY(waveFrequencyY: number) {
223
+ this._waveFrequencyY = waveFrequencyY * 0.04;
224
+ }
225
+
226
+ set waveAmplitude(waveAmplitude: number) {
227
+ this._waveAmplitude = waveAmplitude * .75;
228
+ }
229
+
230
+ set colors(colors: NeatColor[]) {
231
+ this._colors = colors;
232
+ }
233
+
234
+ set highlights(highlights: number) {
235
+ this._highlights = highlights / 100;
236
+ }
237
+
238
+ set shadows(shadows: number) {
239
+ this._shadows = shadows / 100;
240
+ }
241
+
242
+ set saturation(saturation: number) {
243
+ this._saturation = saturation / 10;
244
+ }
245
+
246
+ set colorBlending(colorBlending: number) {
247
+ this._colorBlending = colorBlending / 10;
248
+ }
249
+
250
+ set wireframe(wireframe: boolean) {
251
+ this._wireframe = wireframe;
252
+ }
253
+
254
+ set backgroundColor(backgroundColor: string) {
255
+ this._backgroundColor = backgroundColor;
256
+ }
257
+
258
+ set backgroundAlpha(backgroundAlpha: number) {
259
+ this._backgroundAlpha = backgroundAlpha;
260
+ }
261
+
262
+ _initScene(ref: HTMLCanvasElement, width: number, height: number): SceneState {
263
+
264
+ const renderer = new THREE.WebGLRenderer({
265
+ // antialias: true,
266
+ alpha: true,
267
+ canvas: ref
268
+ });
269
+
270
+ renderer.setClearColor(0xFF0000, .5);
271
+ renderer.setSize(width, height, false);
272
+
273
+ const meshes: THREE.Mesh[] = [];
274
+
275
+ const scene = new THREE.Scene();
276
+
277
+ const material = this._buildMaterial(width, height);
278
+
279
+ const geo = new THREE.PlaneGeometry(PLANE_WIDTH, PLANE_HEIGHT, 240, 240);
280
+ const plane = new THREE.Mesh(geo, material);
281
+ plane.rotation.x = -Math.PI / 3.5;
282
+ plane.position.z = -1;
283
+ meshes.push(plane);
284
+ scene.add(plane);
285
+
286
+ const camera = new THREE.OrthographicCamera(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
287
+ // camera.zoom = 1;
288
+ updateCamera(camera, width, height);
289
+
290
+ return {
291
+ renderer,
292
+ camera,
293
+ scene,
294
+ meshes
295
+ };
296
+ }
297
+
298
+ _buildMaterial(width: number, height: number) {
299
+
300
+ const colors = [
301
+ ...this._colors.map(color => ({
302
+ is_active: color.enabled,
303
+ color: new THREE.Color(color.color),
304
+ influence: color.influence
305
+ })),
306
+ ...Array.from({ length: COLORS_COUNT - this._colors.length }).map(() => ({
307
+ is_active: false,
308
+ color: new THREE.Color(0x000000)
309
+ }))
310
+ ];
311
+
312
+ const uniforms = {
313
+ u_time: { value: 0 },
314
+ u_color_pressure: { value: new THREE.Vector2(this._horizontalPressure, this._verticalPressure) },
315
+ u_wave_frequency_x: { value: this._waveFrequencyX },
316
+ u_wave_frequency_y: { value: this._waveFrequencyY },
317
+ u_wave_amplitude: { value: this._waveAmplitude },
318
+ u_resolution: { value: new THREE.Vector2(width, height) },
319
+ u_colors: { value: colors },
320
+ u_colors_count: { value: this._colors.length },
321
+ u_plane_width: { value: PLANE_WIDTH },
322
+ u_plane_height: { value: PLANE_HEIGHT },
323
+ u_shadows: { value: this._shadows },
324
+ u_highlights: { value: this._highlights },
325
+ };
326
+
327
+ const material = new THREE.ShaderMaterial({
328
+ uniforms: uniforms,
329
+ vertexShader: buildUniforms() + buildNoise() + buildColorFunctions() + buildVertexShader(),
330
+ fragmentShader: buildUniforms() + buildColorFunctions() + buildFragmentShader()
331
+ });
332
+
333
+ material.wireframe = WIREFRAME;
334
+ return material;
335
+ }
336
+
337
+
338
+ }
339
+
340
+ function updateCamera(camera: THREE.OrthographicCamera, width: number, height: number) {
341
+
342
+ const viewPortAreaRatio = 1000000;
343
+ const areaViewPort = width * height;
344
+ const targetPlaneArea =
345
+ areaViewPort / viewPortAreaRatio *
346
+ PLANE_WIDTH * PLANE_HEIGHT / 1.5;
347
+
348
+ const ratio = width / height;
349
+
350
+ const targetWidth = Math.sqrt(targetPlaneArea * ratio);
351
+ const targetHeight = targetPlaneArea / targetWidth;
352
+
353
+ const left = -PLANE_WIDTH / 2;
354
+ const right = Math.min((left + targetWidth) / 1.5, PLANE_WIDTH / 2);
355
+
356
+ const top = PLANE_HEIGHT / 4;
357
+ const bottom = Math.max((top - targetHeight) / 2, -PLANE_HEIGHT / 4);
358
+
359
+ const near = -100;
360
+ const far = 1000;
361
+ camera.left = left;
362
+ camera.right = right;
363
+ camera.top = top;
364
+ camera.bottom = bottom;
365
+ camera.near = near;
366
+ camera.far = far;
367
+ camera.updateProjectionMatrix();
368
+ }
369
+
370
+
371
+ function buildVertexShader() {
372
+ return `
373
+
374
+ void main() {
375
+
376
+ vUv = uv;
377
+
378
+ v_displacement_amount = cnoise( vec3(
379
+ u_wave_frequency_x * position.x + u_time,
380
+ u_wave_frequency_y * position.y + u_time,
381
+ u_time
382
+ ));
383
+
384
+ vec3 color;
385
+
386
+ // float t = mod(u_base_color, 100.0);
387
+ color = u_colors[0].color;
388
+
389
+ vec2 noise_cord = vUv * u_color_pressure;
390
+
391
+ const float minNoise = .0;
392
+ const float maxNoise = .9;
393
+
394
+ for (int i = 1; i < u_colors_count; i++) {
395
+
396
+ if(u_colors[i].is_active == 1.0){
397
+ float noiseFlow = (1. + float(i)) / 30.;
398
+ float noiseSpeed = (1. + float(i)) * 0.11;
399
+ float noiseSeed = 13. + float(i) * 7.;
400
+
401
+ float noise = snoise(
402
+ vec3(
403
+ noise_cord.x * u_color_pressure.x + u_time * noiseFlow * 2.,
404
+ noise_cord.y * u_color_pressure.y,
405
+ u_time * noiseSpeed
406
+ ) + noiseSeed
407
+ );
408
+
409
+ noise = clamp(minNoise, maxNoise + float(i) * 0.02, noise);
410
+ vec3 nextColor = u_colors[i].color;
411
+ color = mix(color, nextColor, smoothstep(0.0, u_color_blending, noise));
412
+ }
413
+
414
+ }
415
+
416
+ v_color = color;
417
+
418
+ vec3 newPosition = position + normal * v_displacement_amount * u_wave_amplitude;
419
+ gl_Position = projectionMatrix * modelViewMatrix * vec4( newPosition, 1.0 );
420
+
421
+ v_new_position = gl_Position;
422
+ }
423
+ `;
424
+ }
425
+
426
+ function buildFragmentShader() {
427
+ return `
428
+
429
+ void main(){
430
+ vec3 color = v_color;
431
+
432
+ color.rgb += pow(v_displacement_amount, 1.0) * u_highlights;
433
+ color.rgb -= pow(1.0 - v_displacement_amount, 2.0) * u_shadows;
434
+ color = saturation(color, 1.0 + u_saturation);
435
+
436
+ gl_FragColor = vec4(color,1.0);
437
+ }
438
+ `;
439
+ }
440
+
441
+ const buildUniforms = () => `
442
+ precision highp float;
443
+
444
+ struct Color {
445
+ float is_active;
446
+ vec3 color;
447
+ float value;
448
+ };
449
+
450
+ uniform float u_time;
451
+
452
+ uniform float u_wave_amplitude;
453
+ uniform float u_wave_frequency_x;
454
+ uniform float u_wave_frequency_y;
455
+
456
+ uniform vec2 u_color_pressure;
457
+
458
+ uniform float u_plane_width;
459
+ uniform float u_plane_height;
460
+
461
+ uniform float u_shadows;
462
+ uniform float u_highlights;
463
+ uniform float u_saturation;
464
+
465
+ uniform float u_color_blending;
466
+
467
+ uniform int u_colors_count;
468
+ uniform Color u_colors[5];
469
+ uniform vec2 u_resolution;
470
+
471
+ varying vec2 vUv;
472
+ varying vec4 v_new_position;
473
+ varying vec3 v_color;
474
+ varying float v_displacement_amount;
475
+
476
+ `;
477
+
478
+ const buildNoise = () => `
479
+
480
+ vec3 mod289(vec3 x)
481
+ {
482
+ return x - floor(x * (1.0 / 289.0)) * 289.0;
483
+ }
484
+
485
+ vec4 mod289(vec4 x)
486
+ {
487
+ return x - floor(x * (1.0 / 289.0)) * 289.0;
488
+ }
489
+
490
+ vec4 permute(vec4 x)
491
+ {
492
+ return mod289(((x*34.0)+1.0)*x);
493
+ }
494
+
495
+ vec4 taylorInvSqrt(vec4 r)
496
+ {
497
+ return 1.79284291400159 - 0.85373472095314 * r;
498
+ }
499
+
500
+ vec3 fade(vec3 t) {
501
+ return t*t*t*(t*(t*6.0-15.0)+10.0);
502
+ }
503
+
504
+ float snoise(vec3 v)
505
+ {
506
+ const vec2 C = vec2(1.0/6.0, 1.0/3.0) ;
507
+ const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);
508
+
509
+ // First corner
510
+ vec3 i = floor(v + dot(v, C.yyy) );
511
+ vec3 x0 = v - i + dot(i, C.xxx) ;
512
+
513
+ // Other corners
514
+ vec3 g = step(x0.yzx, x0.xyz);
515
+ vec3 l = 1.0 - g;
516
+ vec3 i1 = min( g.xyz, l.zxy );
517
+ vec3 i2 = max( g.xyz, l.zxy );
518
+
519
+ // x0 = x0 - 0.0 + 0.0 * C.xxx;
520
+ // x1 = x0 - i1 + 1.0 * C.xxx;
521
+ // x2 = x0 - i2 + 2.0 * C.xxx;
522
+ // x3 = x0 - 1.0 + 3.0 * C.xxx;
523
+ vec3 x1 = x0 - i1 + C.xxx;
524
+ vec3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
525
+ vec3 x3 = x0 - D.yyy; // -1.0+3.0*C.x = -0.5 = -D.y
526
+
527
+ // Permutations
528
+ i = mod289(i);
529
+ vec4 p = permute( permute( permute(
530
+ i.z + vec4(0.0, i1.z, i2.z, 1.0 ))
531
+ + i.y + vec4(0.0, i1.y, i2.y, 1.0 ))
532
+ + i.x + vec4(0.0, i1.x, i2.x, 1.0 ));
533
+
534
+ // Gradients: 7x7 points over a square, mapped onto an octahedron.
535
+ // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
536
+ float n_ = 0.142857142857; // 1.0/7.0
537
+ vec3 ns = n_ * D.wyz - D.xzx;
538
+
539
+ vec4 j = p - 49.0 * floor(p * ns.z * ns.z); // mod(p,7*7)
540
+
541
+ vec4 x_ = floor(j * ns.z);
542
+ vec4 y_ = floor(j - 7.0 * x_ ); // mod(j,N)
543
+
544
+ vec4 x = x_ *ns.x + ns.yyyy;
545
+ vec4 y = y_ *ns.x + ns.yyyy;
546
+ vec4 h = 1.0 - abs(x) - abs(y);
547
+
548
+ vec4 b0 = vec4( x.xy, y.xy );
549
+ vec4 b1 = vec4( x.zw, y.zw );
550
+
551
+ //vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0;
552
+ //vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0;
553
+ vec4 s0 = floor(b0)*2.0 + 1.0;
554
+ vec4 s1 = floor(b1)*2.0 + 1.0;
555
+ vec4 sh = -step(h, vec4(0.0));
556
+
557
+ vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
558
+ vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
559
+
560
+ vec3 p0 = vec3(a0.xy,h.x);
561
+ vec3 p1 = vec3(a0.zw,h.y);
562
+ vec3 p2 = vec3(a1.xy,h.z);
563
+ vec3 p3 = vec3(a1.zw,h.w);
564
+
565
+ //Normalise gradients
566
+ vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
567
+ p0 *= norm.x;
568
+ p1 *= norm.y;
569
+ p2 *= norm.z;
570
+ p3 *= norm.w;
571
+
572
+ // Mix final noise value
573
+ vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
574
+ m = m * m;
575
+ return 42.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1),
576
+ dot(p2,x2), dot(p3,x3) ) );
577
+ }
578
+
579
+ // Classic Perlin noise
580
+ float cnoise(vec3 P)
581
+ {
582
+ vec3 Pi0 = floor(P); // Integer part for indexing
583
+ vec3 Pi1 = Pi0 + vec3(1.0); // Integer part + 1
584
+ Pi0 = mod289(Pi0);
585
+ Pi1 = mod289(Pi1);
586
+ vec3 Pf0 = fract(P); // Fractional part for interpolation
587
+ vec3 Pf1 = Pf0 - vec3(1.0); // Fractional part - 1.0
588
+ vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
589
+ vec4 iy = vec4(Pi0.yy, Pi1.yy);
590
+ vec4 iz0 = Pi0.zzzz;
591
+ vec4 iz1 = Pi1.zzzz;
592
+
593
+ vec4 ixy = permute(permute(ix) + iy);
594
+ vec4 ixy0 = permute(ixy + iz0);
595
+ vec4 ixy1 = permute(ixy + iz1);
596
+
597
+ vec4 gx0 = ixy0 * (1.0 / 7.0);
598
+ vec4 gy0 = fract(floor(gx0) * (1.0 / 7.0)) - 0.5;
599
+ gx0 = fract(gx0);
600
+ vec4 gz0 = vec4(0.5) - abs(gx0) - abs(gy0);
601
+ vec4 sz0 = step(gz0, vec4(0.0));
602
+ gx0 -= sz0 * (step(0.0, gx0) - 0.5);
603
+ gy0 -= sz0 * (step(0.0, gy0) - 0.5);
604
+
605
+ vec4 gx1 = ixy1 * (1.0 / 7.0);
606
+ vec4 gy1 = fract(floor(gx1) * (1.0 / 7.0)) - 0.5;
607
+ gx1 = fract(gx1);
608
+ vec4 gz1 = vec4(0.5) - abs(gx1) - abs(gy1);
609
+ vec4 sz1 = step(gz1, vec4(0.0));
610
+ gx1 -= sz1 * (step(0.0, gx1) - 0.5);
611
+ gy1 -= sz1 * (step(0.0, gy1) - 0.5);
612
+
613
+ vec3 g000 = vec3(gx0.x,gy0.x,gz0.x);
614
+ vec3 g100 = vec3(gx0.y,gy0.y,gz0.y);
615
+ vec3 g010 = vec3(gx0.z,gy0.z,gz0.z);
616
+ vec3 g110 = vec3(gx0.w,gy0.w,gz0.w);
617
+ vec3 g001 = vec3(gx1.x,gy1.x,gz1.x);
618
+ vec3 g101 = vec3(gx1.y,gy1.y,gz1.y);
619
+ vec3 g011 = vec3(gx1.z,gy1.z,gz1.z);
620
+ vec3 g111 = vec3(gx1.w,gy1.w,gz1.w);
621
+
622
+ vec4 norm0 = taylorInvSqrt(vec4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110)));
623
+ g000 *= norm0.x;
624
+ g010 *= norm0.y;
625
+ g100 *= norm0.z;
626
+ g110 *= norm0.w;
627
+ vec4 norm1 = taylorInvSqrt(vec4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111)));
628
+ g001 *= norm1.x;
629
+ g011 *= norm1.y;
630
+ g101 *= norm1.z;
631
+ g111 *= norm1.w;
632
+
633
+ float n000 = dot(g000, Pf0);
634
+ float n100 = dot(g100, vec3(Pf1.x, Pf0.yz));
635
+ float n010 = dot(g010, vec3(Pf0.x, Pf1.y, Pf0.z));
636
+ float n110 = dot(g110, vec3(Pf1.xy, Pf0.z));
637
+ float n001 = dot(g001, vec3(Pf0.xy, Pf1.z));
638
+ float n101 = dot(g101, vec3(Pf1.x, Pf0.y, Pf1.z));
639
+ float n011 = dot(g011, vec3(Pf0.x, Pf1.yz));
640
+ float n111 = dot(g111, Pf1);
641
+
642
+ vec3 fade_xyz = fade(Pf0);
643
+ vec4 n_z = mix(vec4(n000, n100, n010, n110), vec4(n001, n101, n011, n111), fade_xyz.z);
644
+ vec2 n_yz = mix(n_z.xy, n_z.zw, fade_xyz.y);
645
+ float n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x);
646
+ return 2.2 * n_xyz;
647
+ }
648
+
649
+ // YUV to RGB matrix
650
+ mat3 yuv2rgb = mat3(1.0, 0.0, 1.13983,
651
+ 1.0, -0.39465, -0.58060,
652
+ 1.0, 2.03211, 0.0);
653
+
654
+ // RGB to YUV matrix
655
+ mat3 rgb2yuv = mat3(0.2126, 0.7152, 0.0722,
656
+ -0.09991, -0.33609, 0.43600,
657
+ 0.615, -0.5586, -0.05639);
658
+
659
+ `;
660
+
661
+ const buildColorFunctions = () => `
662
+
663
+ vec3 saturation(vec3 rgb, float adjustment) {
664
+ const vec3 W = vec3(0.2125, 0.7154, 0.0721);
665
+ vec3 intensity = vec3(dot(rgb, W));
666
+ return mix(intensity, rgb, adjustment);
667
+ }
668
+
669
+ float saturation(vec3 rgb)
670
+ {
671
+ vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
672
+ vec4 p = mix(vec4(rgb.bg, K.wz), vec4(rgb.gb, K.xy), step(rgb.b, rgb.g));
673
+ vec4 q = mix(vec4(p.xyw, rgb.r), vec4(rgb.r, p.yzx), step(p.x, rgb.r));
674
+
675
+ float d = q.x - min(q.w, q.y);
676
+ float e = 1.0e-10;
677
+ return abs(6.0 * d + e);
678
+ }
679
+
680
+ // get saturation of a color in values between 0 and 1
681
+ float getSaturation(vec3 color) {
682
+ float max = max(color.r, max(color.g, color.b));
683
+ float min = min(color.r, min(color.g, color.b));
684
+ return (max - min) / max;
685
+ }
686
+
687
+ vec3 rgb2hsv(vec3 c)
688
+ {
689
+ vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
690
+ vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
691
+ vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
692
+
693
+ float d = q.x - min(q.w, q.y);
694
+ float e = 1.0e-10;
695
+ return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
696
+ }
697
+
698
+ vec3 hsv2rgb(vec3 c)
699
+ {
700
+ vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
701
+ vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
702
+ return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
703
+ }
704
+ `;
705
+
706
+
707
+ const setLinkStyles = (link: HTMLAnchorElement) => {
708
+ link.id = "neat-link";
709
+ link.href = "https://neat.firecms.co";
710
+ link.target = "_blank";
711
+ link.style.position = "absolute";
712
+ link.style.display = "block";
713
+ link.style.opacity = "1";
714
+ link.style.bottom = "0";
715
+ link.style.right = "0";
716
+ link.style.padding = "10px";
717
+ link.style.color = "#dcdcdc";
718
+ link.style.fontFamily = "sans-serif";
719
+ link.style.fontSize = "16px";
720
+ link.style.fontWeight = "bold";
721
+ link.style.textDecoration = "none";
722
+ link.style.zIndex = "100";
723
+ link.innerHTML = "NEAT";
724
+ }
725
+
726
+ const addNeatLink = (ref: HTMLCanvasElement) => {
727
+ const existingLinks = ref.parentElement?.getElementsByTagName("a");
728
+ if (existingLinks) {
729
+ for (let i = 0; i < existingLinks.length; i++) {
730
+ if (existingLinks[i].id === "neat-link") {
731
+ setLinkStyles(existingLinks[i]);
732
+ return;
733
+ }
734
+ }
735
+ }
736
+ const link = document.createElement("a");
737
+ setLinkStyles(link);
738
+ ref.parentElement?.appendChild(link);
739
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./NeatGradient";
package/tsconfig.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "compilerOptions": {
3
+ "outDir": "dist",
4
+ "module": "esnext",
5
+ "target": "esnext",
6
+ "lib": [
7
+ "dom",
8
+ "dom.iterable",
9
+ "esnext"
10
+ ],
11
+ "moduleResolution": "node",
12
+ "noFallthroughCasesInSwitch": true,
13
+ "sourceMap": true,
14
+ "declaration": true,
15
+ "esModuleInterop": true,
16
+ "noImplicitReturns": true,
17
+ "noImplicitThis": true,
18
+ "noImplicitAny": true,
19
+ "strictNullChecks": true,
20
+ "suppressImplicitAnyIndexErrors": true,
21
+ "allowSyntheticDefaultImports": true,
22
+ "allowJs": true,
23
+ "skipLibCheck": true,
24
+ "strict": true,
25
+ "forceConsistentCasingInFileNames": true,
26
+ "resolveJsonModule": true,
27
+ "isolatedModules": true,
28
+ "types": ["vite/client", "node"]
29
+ },
30
+ "include": [
31
+ "src",
32
+ "./src/**/*.ts"
33
+ ],
34
+ "exclude": [
35
+ "node_modules",
36
+ "dist"
37
+ ]
38
+ }
package/vite.config.js ADDED
@@ -0,0 +1,23 @@
1
+ import path from "path";
2
+
3
+ import {defineConfig} from "vite";
4
+
5
+ const isExternal = (id) => !id.startsWith(".") && !path.isAbsolute(id);
6
+
7
+ export default defineConfig(() => ({
8
+ esbuild: {
9
+ logOverride: {"this-is-undefined-in-esm": "silent"}
10
+ },
11
+ build: {
12
+ lib: {
13
+ entry: path.resolve(__dirname, "src/index.ts"),
14
+ name: "FireCMS",
15
+ fileName: (format) => `index.${format}.js`
16
+ },
17
+ target: "esnext",
18
+ sourcemap: true,
19
+ rollupOptions: {
20
+ external: isExternal
21
+ }
22
+ }
23
+ }));