-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
547 additions
and
647 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,82 @@ | ||
<script lang="ts"> | ||
import _ from 'lodash' | ||
import Regl from 'regl' | ||
<script lang="ts" setup> | ||
import Regl, {DrawConfig} from 'regl' | ||
import REGL from 'regl' | ||
import { | ||
computed, | ||
defineComponent, | ||
onMounted, | ||
onUnmounted, | ||
PropType, | ||
ref, | ||
watch, | ||
} from 'vue' | ||
import {computed, onMounted, onUnmounted, ref, watch} from 'vue' | ||
import {REGL_QUAD_DEFAULT} from './util' | ||
const REGL_QUAD_DEFAULT: DrawConfig = { | ||
vert: ` | ||
precision mediump float; | ||
attribute vec2 position; | ||
varying vec2 uv; | ||
void main() { | ||
uv = position / 2.0 + 0.5; | ||
gl_Position = vec4(position, 0, 1); | ||
}`, | ||
attributes: { | ||
position: [-1, -1, 1, -1, -1, 1, 1, 1], | ||
}, | ||
depth: { | ||
enable: false, | ||
}, | ||
count: 4, | ||
primitive: 'triangle strip', | ||
} | ||
interface UniformsProp { | ||
[name: string]: number[] | string | ||
interface Props { | ||
fragmentString: string | ||
uniforms: Record<string, number | number[] | string> | ||
} | ||
export default defineComponent({ | ||
name: 'GlslCnavas', | ||
props: { | ||
fragmentString: { | ||
type: String, | ||
default: ` | ||
precision mediump float; | ||
varying vec2 uv; | ||
void main() { gl_FragColor = vec4(uv, 0, 1); }`, | ||
}, | ||
uniforms: { | ||
type: Object as PropType<UniformsProp>, | ||
default: () => ({}), | ||
}, | ||
}, | ||
setup(props) { | ||
const canvas = ref<null | HTMLCanvasElement>(null) | ||
const props = withDefaults(defineProps<Props>(), { | ||
fragmentString: ` | ||
precision mediump float; | ||
varying vec2 uv; | ||
void main() { gl_FragColor = vec4(uv, 0, 1); }`, | ||
uniforms: () => ({}), | ||
}) | ||
const regl = ref<null | REGL.Regl>(null) | ||
const $canvas = ref<null | HTMLCanvasElement>(null) | ||
onMounted(() => { | ||
if (!canvas.value) { | ||
return | ||
} | ||
const regl = ref<null | REGL.Regl>(null) | ||
regl.value = Regl({ | ||
attributes: { | ||
depth: false, | ||
premultipliedAlpha: false, | ||
}, | ||
canvas: canvas.value, | ||
}) | ||
}) | ||
onMounted(() => { | ||
if (!$canvas.value) return | ||
onUnmounted(() => regl.value?.destroy()) | ||
const uniformKeys = ref(_.keys(props.uniforms)) | ||
regl.value = Regl({ | ||
attributes: { | ||
depth: false, | ||
premultipliedAlpha: false, | ||
}, | ||
canvas: $canvas.value, | ||
}) | ||
}) | ||
const drawCommand = computed(() => { | ||
if (!regl.value) return null | ||
onUnmounted(() => regl.value?.destroy()) | ||
const prop = regl.value.prop as any | ||
const drawCommand = computed(() => { | ||
if (!regl.value) return null | ||
const uniforms = _.fromPairs(uniformKeys.value.map(k => [k, prop(k)])) | ||
return regl.value({ | ||
...REGL_QUAD_DEFAULT, | ||
frag: props.fragmentString, | ||
}) | ||
}) | ||
return regl.value({ | ||
...REGL_QUAD_DEFAULT, | ||
frag: props.fragmentString, | ||
uniforms, | ||
watch( | ||
() => [drawCommand.value, props.uniforms] as const, | ||
([draw, uniforms]) => { | ||
console.log(uniforms) | ||
draw && | ||
draw({ | ||
hsv: [0, 1, 1], | ||
modeX: 0, | ||
modeY: 1, | ||
}) | ||
}) | ||
watch( | ||
() => [regl.value, drawCommand.value, props.uniforms], | ||
() => { | ||
drawCommand.value && drawCommand.value(props.uniforms) | ||
} | ||
) | ||
return {canvas} | ||
}, | ||
}) | ||
{immediate: true} | ||
) | ||
</script> | ||
|
||
<template> | ||
<canvas ref="canvas" /> | ||
<canvas ref="$canvas" /> | ||
</template> |
Oops, something went wrong.