-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into terrysahaidak/upload-texture
- Loading branch information
Showing
25 changed files
with
509 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React from "react"; | ||
import { StyleSheet, View } from "react-native"; | ||
import { useFrameCallback } from "react-native-reanimated"; | ||
import { Canvas } from "react-native-wgpu"; | ||
|
||
import { useLoop } from "../../components/Animations"; | ||
|
||
import { drawBreatheDemo, useSkiaContext } from "./utils"; | ||
|
||
export function WebGPU() { | ||
const { ref, context } = useSkiaContext(); | ||
|
||
const progress = useLoop({ duration: 3000 }); | ||
|
||
useFrameCallback(() => { | ||
if (!context.value) { | ||
return; | ||
} | ||
|
||
const ctx = context.value; | ||
drawBreatheDemo(ctx, progress.value); | ||
ctx.present(); | ||
}); | ||
|
||
return ( | ||
<View style={style.container}> | ||
<Canvas ref={ref} style={style.webgpu} /> | ||
</View> | ||
); | ||
} | ||
|
||
const style = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
}, | ||
webgpu: { | ||
flex: 1, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./WebGPU"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import type { SkiaContext } from "@shopify/react-native-skia"; | ||
import { | ||
BlendMode, | ||
BlurStyle, | ||
mix, | ||
polar2Canvas, | ||
Skia, | ||
} from "@shopify/react-native-skia"; | ||
import { Dimensions, PixelRatio } from "react-native"; | ||
import { useSharedValue } from "react-native-reanimated"; | ||
import { useCanvasEffect } from "react-native-wgpu"; | ||
|
||
export const useSkiaContext = () => { | ||
const context = useSharedValue<SkiaContext | null>(null); | ||
const ref = useCanvasEffect(() => { | ||
const nativeSurface = ref.current!.getNativeSurface(); | ||
context.value = Skia.Context( | ||
nativeSurface.surface, | ||
nativeSurface.width * pd, | ||
nativeSurface.height * pd | ||
); | ||
}); | ||
return { | ||
context, | ||
ref, | ||
}; | ||
}; | ||
|
||
const pd = PixelRatio.get(); | ||
const { width, height } = Dimensions.get("window"); | ||
const center = { x: width / 2, y: height / 2 }; | ||
const R = width / 4; | ||
|
||
const c1 = "#61bea2"; | ||
const c2 = "#529ca0"; | ||
const root = Skia.Paint(); | ||
root.setBlendMode(BlendMode.Screen); | ||
root.setMaskFilter(Skia.MaskFilter.MakeBlur(BlurStyle.Solid, 10, true)); | ||
const p1 = root.copy(); | ||
p1.setColor(Skia.Color(c1)); | ||
const p2 = root.copy(); | ||
p2.setColor(Skia.Color(c2)); | ||
|
||
export const drawBreatheDemo = (ctx: SkiaContext, progress: number) => { | ||
"worklet"; | ||
const surface = ctx.getSurface(); | ||
const canvas = surface.getCanvas(); | ||
canvas.clear(Skia.Color("rgb(36, 43, 56)")); | ||
canvas.save(); | ||
canvas.scale(pd, pd); | ||
canvas.rotate(progress * -180, center.x, center.y); | ||
// const offscreen = Skia.Surface.MakeOffscreen(256, 256)!; | ||
// const offscreenCanvas = offscreen.getCanvas(); | ||
// offscreenCanvas.clear(Skia.Color("green")); | ||
// canvas.drawImage(offscreen.makeImageSnapshot(), 0, 0); | ||
new Array(6).fill(0).map((_, index) => { | ||
canvas.save(); | ||
const theta = (index * (2 * Math.PI)) / 6; | ||
const { x, y } = polar2Canvas( | ||
{ theta, radius: progress * R }, | ||
{ x: 0, y: 0 } | ||
); | ||
const scale = mix(progress, 0.3, 1); | ||
|
||
canvas.translate(center.x, center.y); | ||
canvas.translate(x, y); | ||
canvas.scale(scale, scale); | ||
canvas.translate(-center.x, -center.y); | ||
|
||
const paint = index % 2 ? p1 : p2; | ||
canvas.drawCircle(center.x, center.y, R, paint); | ||
canvas.restore(); | ||
}); | ||
|
||
canvas.restore(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export type StackParamList = { | ||
WebGPU: undefined; | ||
Tests: { | ||
title?: string; | ||
path?: string[]; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.