Skip to content

Commit 5f35752

Browse files
add asciiTexture props
1 parent b9d3c70 commit 5f35752

File tree

2 files changed

+64
-32
lines changed

2 files changed

+64
-32
lines changed

playground/src/pages/postprocessing/ascii.vue

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,35 @@ import { ContactShadows, Environment, OrbitControls } from '@tresjs/cientos'
33
import { TresCanvas } from '@tresjs/core'
44
import { TresLeches, useControls } from '@tresjs/leches'
55
import { NoToneMapping } from 'three'
6-
import { BlendFunction } from 'postprocessing'
6+
import { ASCIITexture, BlendFunction } from 'postprocessing'
77
import { ASCIIPmndrs, EffectComposerPmndrs } from '@tresjs/post-processing'
88
99
import '@tresjs/leches/styles'
1010
1111
const gl = {
1212
clearColor: '#ffffff',
1313
toneMapping: NoToneMapping,
14-
multisampling: 8,
1514
}
1615
17-
const { blendFunction, opacity, cellSize, inverted, color, useSceneColor } = useControls({
16+
const { enabled, blendFunction, opacity, cellSize, inverted, color, useSceneColor, characters, font, fontSize, size, cellCount } = useControls({
17+
enabled: true,
1818
blendFunction: {
1919
options: Object.keys(BlendFunction).map(key => ({
2020
text: key,
2121
value: BlendFunction[key as keyof typeof BlendFunction],
2222
})),
23-
value: BlendFunction.OVERLAY,
23+
value: BlendFunction.NORMAL,
2424
},
2525
opacity: { value: 1, step: 0.1, min: 0.0, max: 1.0 },
2626
cellSize: { value: 12, step: 1, min: 2, max: 64 },
2727
useSceneColor: false,
28-
color: '#ff0000',
28+
color: '#ffffff',
2929
inverted: false,
30+
characters: { value: '@#8&$%*o!;.', label: 'Characters' },
31+
font: { value: 'Arial', label: 'Font' },
32+
fontSize: { value: 54, step: 1, min: 10, max: 100, label: 'Font Size' },
33+
size: { value: 1024, step: 128, min: 256, max: 2048, label: 'Texture Size' },
34+
cellCount: { value: 16, step: 1, min: 4, max: 64, label: 'Cell Count' },
3035
})
3136
</script>
3237

@@ -37,34 +42,36 @@ const { blendFunction, opacity, cellSize, inverted, color, useSceneColor } = use
3742
v-bind="gl"
3843
>
3944
<TresPerspectiveCamera
40-
:position="[5, 5, 5]"
45+
:position="[5, 6.5, 7.5]"
4146
:look-at="[0, 0, 0]"
4247
/>
4348
<OrbitControls auto-rotate />
4449

4550
<TresMesh :position="[0, .5, 0]">
4651
<TresBoxGeometry :args="[2, 2, 2]" />
47-
<TresMeshPhysicalMaterial color="black" :roughness=".25" />
52+
<TresMeshPhysicalMaterial color="white" />
4853
</TresMesh>
4954

50-
<ContactShadows
51-
:opacity="1"
52-
:position-y="-.5"
53-
/>
54-
5555
<Suspense>
56-
<Environment background :blur=".5" preset="night" />
56+
<Environment background preset="dawn" />
5757
</Suspense>
5858

5959
<Suspense>
6060
<EffectComposerPmndrs>
6161
<ASCIIPmndrs
62-
:blendFunction="Number(blendFunction)"
62+
:blendFunction="enabled ? Number(blendFunction) : Number(BlendFunction.SKIP)"
6363
:opacity="opacity"
6464
:cellSize="cellSize"
6565
:inverted="inverted"
6666
:color="color"
6767
:useSceneColor="useSceneColor"
68+
:asciiTexture="{
69+
characters,
70+
font,
71+
fontSize,
72+
size,
73+
cellCount,
74+
}"
6875
/>
6976
</EffectComposerPmndrs>
7077
</Suspense>

src/core/pmndrs/ASCIIPmndrs.vue

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
<script lang="ts" setup>
22
import type { BlendFunction } from 'postprocessing'
3-
import { ASCIIEffect } from 'postprocessing'
3+
import { ASCIIEffect, ASCIITexture } from 'postprocessing'
44
import { makePropWatchers } from '../../util/prop'
55
import { useEffectPmndrs } from './composables/useEffectPmndrs'
6-
import { watch } from 'vue'
6+
import { onUnmounted, watch } from 'vue'
77
import type { Color } from 'three'
88
9-
// TODO: ADD PROPS:
10-
// - asciiTexture: (ASCIITexture)
11-
129
export interface ASCIIPmndrsProps {
1310
/**
1411
* The blend function.
@@ -32,22 +29,33 @@ export interface ASCIIPmndrsProps {
3229
color?: Color | string | number | null
3330
/**
3431
* Controls whether the effect should use the scene color.
32+
* If `true`, it overrides the `color` props.
3533
*/
3634
useSceneColor?: boolean
35+
/**
36+
* The ASCII texture to use or a custom configuration for it.
37+
* https://pmndrs.github.io/postprocessing/public/docs/class/src/textures/ASCIITexture.js~ASCIITexture.html
38+
*/
39+
asciiTexture?: ASCIITexture
3740
}
3841
3942
const props = defineProps<ASCIIPmndrsProps>()
4043
41-
const { pass, effect } = useEffectPmndrs(() => new ASCIIEffect(props), props)
44+
const plainEffect = new ASCIITexture()
45+
46+
const { pass, effect } = useEffectPmndrs(() => new ASCIIEffect(), props)
4247
4348
defineExpose({ pass, effect })
4449
50+
onUnmounted(() => {
51+
plainEffect.dispose()
52+
})
53+
4554
makePropWatchers(
4655
[
4756
[() => props.blendFunction, 'blendMode.blendFunction'],
4857
[() => props.cellSize, 'cellSize'],
4958
[() => props.inverted, 'inverted'],
50-
[() => props.color, 'color'],
5159
],
5260
effect,
5361
() => new ASCIIEffect(),
@@ -61,10 +69,8 @@ watch(
6169
if (props.useSceneColor) {
6270
effect.value.color = null
6371
}
64-
else if (!props.useSceneColor) {
65-
const plainEffect = new ASCIIEffect()
66-
effect.value.color = props.color ? props.color : plainEffect.color
67-
plainEffect.dispose()
72+
else {
73+
effect.value.color = props.color ?? plainEffect.color
6874
}
6975
},
7076
{ immediate: true },
@@ -75,15 +81,34 @@ watch(
7581
() => {
7682
if (!effect.value) { return }
7783
78-
if (props.opacity !== undefined) {
79-
effect.value?.blendMode.setOpacity(props.opacity)
80-
}
81-
else {
82-
const plainEffect = new ASCIIEffect()
83-
effect.value?.blendMode.setOpacity(plainEffect.blendMode.getOpacity())
84-
plainEffect.dispose()
84+
effect.value.blendMode.setOpacity(props.opacity ?? plainEffect.blendMode.getOpacity())
85+
},
86+
{ immediate: true },
87+
)
88+
89+
watch(
90+
[effect, () => props.color],
91+
() => {
92+
if (!effect.value) { return }
93+
94+
if (!props.useSceneColor) {
95+
effect.value.color = props.color ?? null
8596
}
8697
},
8798
{ immediate: true },
8899
)
100+
101+
watch(
102+
[effect, () => props.asciiTexture],
103+
() => {
104+
if (!effect.value) { return }
105+
106+
const texture = props.asciiTexture
107+
? new ASCIITexture(props.asciiTexture)
108+
: plainEffect.asciiTexture
109+
110+
effect.value.asciiTexture = texture
111+
},
112+
{ immediate: true },
113+
)
89114
</script>

0 commit comments

Comments
 (0)