@fjandin/react-shader 0.0.18 → 0.0.19
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/dist/hooks/useWebGPU.d.ts.map +1 -1
- package/dist/index.cjs +27 -19
- package/dist/index.js +27 -19
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useWebGPU.d.ts","sourceRoot":"","sources":["../../src/hooks/useWebGPU.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AAGtE,KAAK,eAAe,GAAG,MAAM,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,SAAS,CAAA;AAE9D,UAAU,gBAAgB;IACxB,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAA;IAC1C,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAA;IAChC,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,IAAI,CAAA;IACnC,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,IAAI,CAAA;IACnC,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,IAAI,CAAA;IACvC,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,IAAI,CAAA;IACvC,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,IAAI,CAAA;IACrC,YAAY,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,KAAK,IAAI,CAAA;CAC7D;
|
|
1
|
+
{"version":3,"file":"useWebGPU.d.ts","sourceRoot":"","sources":["../../src/hooks/useWebGPU.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AAGtE,KAAK,eAAe,GAAG,MAAM,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,SAAS,CAAA;AAE9D,UAAU,gBAAgB;IACxB,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAA;IAC1C,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAA;IAChC,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,IAAI,CAAA;IACnC,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,IAAI,CAAA;IACnC,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,IAAI,CAAA;IACvC,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,IAAI,CAAA;IACvC,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,IAAI,CAAA;IACrC,YAAY,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,KAAK,IAAI,CAAA;CAC7D;AAmXD,wBAAgB,SAAS,CAAC,OAAO,EAAE,gBAAgB;;;EAgRlD"}
|
package/dist/index.cjs
CHANGED
|
@@ -455,6 +455,9 @@ async function initializeWebGPU(canvas, fragmentSource, customUniforms) {
|
|
|
455
455
|
throw new Error("Failed to get WebGPU adapter");
|
|
456
456
|
}
|
|
457
457
|
const device = await adapter.requestDevice();
|
|
458
|
+
device.lost.then((info) => {
|
|
459
|
+
console.error(`WebGPU device lost: ${info.message}`);
|
|
460
|
+
});
|
|
458
461
|
const context = canvas.getContext("webgpu");
|
|
459
462
|
if (!context) {
|
|
460
463
|
throw new Error("Failed to get WebGPU context");
|
|
@@ -547,6 +550,14 @@ function useWebGPU(options) {
|
|
|
547
550
|
const fragmentRef = import_react2.useRef(options.fragment);
|
|
548
551
|
const uniformsRef = import_react2.useRef(options.uniforms);
|
|
549
552
|
const dprRef = import_react2.useRef(window.devicePixelRatio || 1);
|
|
553
|
+
const frameInfoRef = import_react2.useRef({
|
|
554
|
+
deltaTime: 0,
|
|
555
|
+
time: 0,
|
|
556
|
+
resolution: [0, 0],
|
|
557
|
+
mouse: [0, 0],
|
|
558
|
+
mouseNormalized: [0, 0],
|
|
559
|
+
mouseLeftDown: false
|
|
560
|
+
});
|
|
550
561
|
onErrorRef.current = options.onError;
|
|
551
562
|
onFrameRef.current = options.onFrame;
|
|
552
563
|
onClickRef.current = options.onClick;
|
|
@@ -557,17 +568,6 @@ function useWebGPU(options) {
|
|
|
557
568
|
timeScaleRef.current = options.timeScale ?? 1;
|
|
558
569
|
fragmentRef.current = options.fragment;
|
|
559
570
|
uniformsRef.current = options.uniforms;
|
|
560
|
-
const buildFrameInfo = import_react2.useCallback((deltaTime) => {
|
|
561
|
-
const canvas = canvasRef.current;
|
|
562
|
-
return {
|
|
563
|
-
deltaTime,
|
|
564
|
-
time: elapsedTimeRef.current,
|
|
565
|
-
resolution: [canvas?.width ?? 0, canvas?.height ?? 0],
|
|
566
|
-
mouse: mouseRef.current,
|
|
567
|
-
mouseNormalized: mouseNormalizedRef.current,
|
|
568
|
-
mouseLeftDown: mouseLeftDownRef.current
|
|
569
|
-
};
|
|
570
|
-
}, []);
|
|
571
571
|
const render = import_react2.useCallback((time) => {
|
|
572
572
|
const state = stateRef.current;
|
|
573
573
|
const canvas = canvasRef.current;
|
|
@@ -577,8 +577,16 @@ function useWebGPU(options) {
|
|
|
577
577
|
const deltaTime = lastFrameTimeRef.current === 0 ? 0 : (time - lastFrameTimeRef.current) / 1000;
|
|
578
578
|
lastFrameTimeRef.current = time;
|
|
579
579
|
elapsedTimeRef.current += deltaTime * timeScaleRef.current;
|
|
580
|
+
frameInfoRef.current = {
|
|
581
|
+
deltaTime,
|
|
582
|
+
time: elapsedTimeRef.current,
|
|
583
|
+
resolution: [canvas.width, canvas.height],
|
|
584
|
+
mouse: mouseRef.current,
|
|
585
|
+
mouseNormalized: mouseNormalizedRef.current,
|
|
586
|
+
mouseLeftDown: mouseLeftDownRef.current
|
|
587
|
+
};
|
|
580
588
|
if (onFrameRef.current) {
|
|
581
|
-
onFrameRef.current(
|
|
589
|
+
onFrameRef.current(frameInfoRef.current);
|
|
582
590
|
}
|
|
583
591
|
const { device, context, pipeline, uniformBuffer, uniformBindGroup, uniformLayout } = state;
|
|
584
592
|
const dpr = dprRef.current;
|
|
@@ -636,7 +644,7 @@ function useWebGPU(options) {
|
|
|
636
644
|
renderPass.end();
|
|
637
645
|
device.queue.submit([commandEncoder.finish()]);
|
|
638
646
|
animationFrameRef.current = requestAnimationFrame(render);
|
|
639
|
-
}, [
|
|
647
|
+
}, []);
|
|
640
648
|
import_react2.useEffect(() => {
|
|
641
649
|
const canvas = canvasRef.current;
|
|
642
650
|
if (!canvas)
|
|
@@ -704,25 +712,25 @@ function useWebGPU(options) {
|
|
|
704
712
|
(mouseRef.current[0] - canvas.width / 2) / minDimension,
|
|
705
713
|
(mouseRef.current[1] - canvas.height / 2) / minDimension
|
|
706
714
|
];
|
|
707
|
-
onMouseMoveRef.current?.(
|
|
715
|
+
onMouseMoveRef.current?.(frameInfoRef.current);
|
|
708
716
|
};
|
|
709
717
|
const handleMouseDown = (event) => {
|
|
710
718
|
if (event.button === 0) {
|
|
711
719
|
mouseLeftDownRef.current = true;
|
|
712
720
|
}
|
|
713
|
-
onMouseDownRef.current?.(
|
|
721
|
+
onMouseDownRef.current?.(frameInfoRef.current);
|
|
714
722
|
};
|
|
715
723
|
const handleMouseUp = (event) => {
|
|
716
724
|
if (event.button === 0) {
|
|
717
725
|
mouseLeftDownRef.current = false;
|
|
718
726
|
}
|
|
719
|
-
onMouseUpRef.current?.(
|
|
727
|
+
onMouseUpRef.current?.(frameInfoRef.current);
|
|
720
728
|
};
|
|
721
729
|
const handleClick = () => {
|
|
722
|
-
onClickRef.current?.(
|
|
730
|
+
onClickRef.current?.(frameInfoRef.current);
|
|
723
731
|
};
|
|
724
732
|
const handleMouseWheel = (event) => {
|
|
725
|
-
onMouseWheelRef.current?.(
|
|
733
|
+
onMouseWheelRef.current?.(frameInfoRef.current, event.deltaY);
|
|
726
734
|
};
|
|
727
735
|
window.addEventListener("mousemove", handleMouseMove);
|
|
728
736
|
window.addEventListener("mousedown", handleMouseDown);
|
|
@@ -738,7 +746,7 @@ function useWebGPU(options) {
|
|
|
738
746
|
canvas.removeEventListener("click", handleClick);
|
|
739
747
|
window.removeEventListener("wheel", handleMouseWheel);
|
|
740
748
|
};
|
|
741
|
-
}, [
|
|
749
|
+
}, []);
|
|
742
750
|
return { canvasRef, mouseRef };
|
|
743
751
|
}
|
|
744
752
|
|
package/dist/index.js
CHANGED
|
@@ -409,6 +409,9 @@ async function initializeWebGPU(canvas, fragmentSource, customUniforms) {
|
|
|
409
409
|
throw new Error("Failed to get WebGPU adapter");
|
|
410
410
|
}
|
|
411
411
|
const device = await adapter.requestDevice();
|
|
412
|
+
device.lost.then((info) => {
|
|
413
|
+
console.error(`WebGPU device lost: ${info.message}`);
|
|
414
|
+
});
|
|
412
415
|
const context = canvas.getContext("webgpu");
|
|
413
416
|
if (!context) {
|
|
414
417
|
throw new Error("Failed to get WebGPU context");
|
|
@@ -501,6 +504,14 @@ function useWebGPU(options) {
|
|
|
501
504
|
const fragmentRef = useRef2(options.fragment);
|
|
502
505
|
const uniformsRef = useRef2(options.uniforms);
|
|
503
506
|
const dprRef = useRef2(window.devicePixelRatio || 1);
|
|
507
|
+
const frameInfoRef = useRef2({
|
|
508
|
+
deltaTime: 0,
|
|
509
|
+
time: 0,
|
|
510
|
+
resolution: [0, 0],
|
|
511
|
+
mouse: [0, 0],
|
|
512
|
+
mouseNormalized: [0, 0],
|
|
513
|
+
mouseLeftDown: false
|
|
514
|
+
});
|
|
504
515
|
onErrorRef.current = options.onError;
|
|
505
516
|
onFrameRef.current = options.onFrame;
|
|
506
517
|
onClickRef.current = options.onClick;
|
|
@@ -511,17 +522,6 @@ function useWebGPU(options) {
|
|
|
511
522
|
timeScaleRef.current = options.timeScale ?? 1;
|
|
512
523
|
fragmentRef.current = options.fragment;
|
|
513
524
|
uniformsRef.current = options.uniforms;
|
|
514
|
-
const buildFrameInfo = useCallback2((deltaTime) => {
|
|
515
|
-
const canvas = canvasRef.current;
|
|
516
|
-
return {
|
|
517
|
-
deltaTime,
|
|
518
|
-
time: elapsedTimeRef.current,
|
|
519
|
-
resolution: [canvas?.width ?? 0, canvas?.height ?? 0],
|
|
520
|
-
mouse: mouseRef.current,
|
|
521
|
-
mouseNormalized: mouseNormalizedRef.current,
|
|
522
|
-
mouseLeftDown: mouseLeftDownRef.current
|
|
523
|
-
};
|
|
524
|
-
}, []);
|
|
525
525
|
const render = useCallback2((time) => {
|
|
526
526
|
const state = stateRef.current;
|
|
527
527
|
const canvas = canvasRef.current;
|
|
@@ -531,8 +531,16 @@ function useWebGPU(options) {
|
|
|
531
531
|
const deltaTime = lastFrameTimeRef.current === 0 ? 0 : (time - lastFrameTimeRef.current) / 1000;
|
|
532
532
|
lastFrameTimeRef.current = time;
|
|
533
533
|
elapsedTimeRef.current += deltaTime * timeScaleRef.current;
|
|
534
|
+
frameInfoRef.current = {
|
|
535
|
+
deltaTime,
|
|
536
|
+
time: elapsedTimeRef.current,
|
|
537
|
+
resolution: [canvas.width, canvas.height],
|
|
538
|
+
mouse: mouseRef.current,
|
|
539
|
+
mouseNormalized: mouseNormalizedRef.current,
|
|
540
|
+
mouseLeftDown: mouseLeftDownRef.current
|
|
541
|
+
};
|
|
534
542
|
if (onFrameRef.current) {
|
|
535
|
-
onFrameRef.current(
|
|
543
|
+
onFrameRef.current(frameInfoRef.current);
|
|
536
544
|
}
|
|
537
545
|
const { device, context, pipeline, uniformBuffer, uniformBindGroup, uniformLayout } = state;
|
|
538
546
|
const dpr = dprRef.current;
|
|
@@ -590,7 +598,7 @@ function useWebGPU(options) {
|
|
|
590
598
|
renderPass.end();
|
|
591
599
|
device.queue.submit([commandEncoder.finish()]);
|
|
592
600
|
animationFrameRef.current = requestAnimationFrame(render);
|
|
593
|
-
}, [
|
|
601
|
+
}, []);
|
|
594
602
|
useEffect(() => {
|
|
595
603
|
const canvas = canvasRef.current;
|
|
596
604
|
if (!canvas)
|
|
@@ -658,25 +666,25 @@ function useWebGPU(options) {
|
|
|
658
666
|
(mouseRef.current[0] - canvas.width / 2) / minDimension,
|
|
659
667
|
(mouseRef.current[1] - canvas.height / 2) / minDimension
|
|
660
668
|
];
|
|
661
|
-
onMouseMoveRef.current?.(
|
|
669
|
+
onMouseMoveRef.current?.(frameInfoRef.current);
|
|
662
670
|
};
|
|
663
671
|
const handleMouseDown = (event) => {
|
|
664
672
|
if (event.button === 0) {
|
|
665
673
|
mouseLeftDownRef.current = true;
|
|
666
674
|
}
|
|
667
|
-
onMouseDownRef.current?.(
|
|
675
|
+
onMouseDownRef.current?.(frameInfoRef.current);
|
|
668
676
|
};
|
|
669
677
|
const handleMouseUp = (event) => {
|
|
670
678
|
if (event.button === 0) {
|
|
671
679
|
mouseLeftDownRef.current = false;
|
|
672
680
|
}
|
|
673
|
-
onMouseUpRef.current?.(
|
|
681
|
+
onMouseUpRef.current?.(frameInfoRef.current);
|
|
674
682
|
};
|
|
675
683
|
const handleClick = () => {
|
|
676
|
-
onClickRef.current?.(
|
|
684
|
+
onClickRef.current?.(frameInfoRef.current);
|
|
677
685
|
};
|
|
678
686
|
const handleMouseWheel = (event) => {
|
|
679
|
-
onMouseWheelRef.current?.(
|
|
687
|
+
onMouseWheelRef.current?.(frameInfoRef.current, event.deltaY);
|
|
680
688
|
};
|
|
681
689
|
window.addEventListener("mousemove", handleMouseMove);
|
|
682
690
|
window.addEventListener("mousedown", handleMouseDown);
|
|
@@ -692,7 +700,7 @@ function useWebGPU(options) {
|
|
|
692
700
|
canvas.removeEventListener("click", handleClick);
|
|
693
701
|
window.removeEventListener("wheel", handleMouseWheel);
|
|
694
702
|
};
|
|
695
|
-
}, [
|
|
703
|
+
}, []);
|
|
696
704
|
return { canvasRef, mouseRef };
|
|
697
705
|
}
|
|
698
706
|
|