Skip to content

Commit 0e823fb

Browse files
authored
feat: add port of R3F space game (#199)
* feat: add port of R3F space game * author of R3F version: https://twitter.com/0xca0 * chore: remove tmp file * feat(space-game): make explosions bigger * feat(space-game): tweak lighting * fix(space-game): fix rocks rotation inconsistency after rock destroyed * chore(space-game): remove console.log * chore(space-game): remove unused variable * chore: remove unused files * refactor(space-game): destructure three imports
1 parent b1fb022 commit 0e823fb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+3475
-551
lines changed

components/content/array-cameras/index.vue

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ cameraOptions.forEach((data) => {
5050
const { hasFinishLoading, progress } = await useProgress()
5151
5252
useControls('fpsgraph')
53-
5453
</script>
5554

5655
<template>

components/content/brickelangelo-david/David.vue

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const lego = legoNodes.LegoPiece
2424
const legoMaterial = new MeshPhongMaterial({ color: 'lightgray' })
2525
const legoInstancedMesh = new InstancedMesh(lego.geometry, legoMaterial, instanceCount)
2626
27-
2827
useControls('fpsgraph')
2928
3029
const sampler = new MeshSurfaceSampler(david)

components/content/dancing-blob/TheDancingBlob.vue

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<!-- Github Luckystriike: https://github.com/luckystriike22/TresJsPlayground/ -->
22
<script lang="ts" setup>
3+
import { Vector2 } from 'three'
34
import vertexShader from './shaders/vertex.glsl'
45
import fragmentShader from './shaders/fragment.glsl'
56

6-
import { Vector2 } from 'three'
7-
87
const props = defineProps<{
98
analyser: any
109
dataArray: any
@@ -16,43 +15,43 @@ const { onBeforeRender } = useLoop()
1615
// refs
1716
const blobRef = shallowRef<any>(null)
1817

19-
2018
onBeforeRender(({ elapsed }) => {
2119
if (blobRef.value && props.analyser && props.dataArray) {
22-
props.analyser?.getByteFrequencyData(props.dataArray);
20+
props.analyser?.getByteFrequencyData(props.dataArray)
2321

2422
// calc average frequency
25-
let sum = 0;
23+
let sum = 0
2624
for (let i = 0; i < props.dataArray?.length; i++) {
27-
sum += props.dataArray[i];
25+
sum += props.dataArray[i]
2826
}
2927

30-
uniforms.value.u_frequency.value = sum > 0 ? sum / props.dataArray?.length : 0;
28+
uniforms.value.u_frequency.value = sum > 0 ? sum / props.dataArray?.length : 0
3129
uniforms.value.u_time.value = elapsed
3230
blobRef.value.rotation.x += 0.01
3331
}
3432
})
3533

36-
3734
// shader
3835
// set props to pass into the shader
3936
const uniforms = ref({
4037
u_resolution: { type: 'V2', value: new Vector2(window.innerWidth, window.innerHeight) },
4138
u_time: { type: 'f', value: 0.0 },
4239
u_frequency: { type: 'f', value: 0.0 },
43-
u_amplitude: { type: 'f', value: 1.5 }
44-
});
45-
46-
40+
u_amplitude: { type: 'f', value: 1.5 },
41+
})
4742
</script>
4843

4944
<template>
5045
<TresPerspectiveCamera :position="[13, 0, 0]" />
5146
<OrbitControls />
5247
<TresMesh ref="blobRef">
53-
<TresIcosahedronGeometry :args="[4, 80]"></TresIcosahedronGeometry>
54-
<TresShaderMaterial wireframe :uniforms="uniforms" :fragment-shader="fragmentShader"
55-
:vertex-shader="vertexShader" />
48+
<TresIcosahedronGeometry :args="[4, 80]" />
49+
<TresShaderMaterial
50+
wireframe
51+
:uniforms="uniforms"
52+
:fragment-shader="fragmentShader"
53+
:vertex-shader="vertexShader"
54+
/>
5655
</TresMesh>
5756
<TresDirectionalLight :position="[1, 1, 1]" />
5857
<TresAmbientLight :intensity="1" />

components/content/dancing-blob/index.vue

+39-26
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,73 @@
11
<!-- Github Luckystriike: https://github.com/luckystriike22/TresJsPlayground/ -->
22
<script lang="ts" setup>
3-
const analyser = shallowRef();
4-
const audioStream = shallowRef();
5-
const dataArray = shallowRef();
6-
const showInfoDialog = shallowRef(false);
3+
const analyser = shallowRef()
4+
const audioStream = shallowRef()
5+
const dataArray = shallowRef()
6+
const showInfoDialog = shallowRef(false)
77

88
// lifecycle
99
onMounted(async () => {
10-
await nextTick();
10+
await nextTick()
1111

1212
try {
1313
const access = await navigator.permissions.query({ name: 'microphone' })
14-
showInfoDialog.value = access.state != "granted";
14+
showInfoDialog.value = access.state != 'granted'
1515

16-
audioStream.value = await navigator.mediaDevices.getUserMedia({ audio: true });
17-
showInfoDialog.value = false;
16+
audioStream.value = await navigator.mediaDevices.getUserMedia({ audio: true })
17+
showInfoDialog.value = false
1818
handleMicrophoneAccess()
1919

20-
} catch (error) {
21-
showInfoDialog.value = true;
22-
alert('Not able to accessing microphone');
20+
}
21+
catch (error) {
22+
showInfoDialog.value = true
23+
alert('Not able to accessing microphone')
2324
}
2425
})
2526

2627
// handle the microphone connection
2728
const handleMicrophoneAccess = () => {
28-
const audioContext = new (window.AudioContext)();
29-
const source = audioContext.createMediaStreamSource(audioStream.value);
29+
const audioContext = new (window.AudioContext)()
30+
const source = audioContext.createMediaStreamSource(audioStream.value)
3031

31-
analyser.value = audioContext.createAnalyser();
32-
analyser.value.fftSize = 2048;
33-
source.connect(analyser.value);
32+
analyser.value = audioContext.createAnalyser()
33+
analyser.value.fftSize = 2048
34+
source.connect(analyser.value)
3435

35-
const bufferLength = analyser.value.frequencyBinCount;
36-
dataArray.value = new Uint8Array(bufferLength);
37-
};
36+
const bufferLength = analyser.value.frequencyBinCount
37+
dataArray.value = new Uint8Array(bufferLength)
38+
}
3839
</script>
3940

4041
<template>
4142
<button
42-
class="gitBtn bg-gray-600 hover:bg-gray-700 opacity-40 transition-color shadow-lg hover:shadow-xl infline-flex w-12 h-12 justify-center items-center rounded-full absolute bottom-2 right-2">
43-
<a href="https://github.com/Tresjs/lab/tree/main/components/content/dancing-blob" target="_blank">Code</a>
43+
class="gitBtn bg-gray-600 hover:bg-gray-700 opacity-40 transition-color shadow-lg hover:shadow-xl infline-flex w-12 h-12 justify-center items-center rounded-full absolute bottom-2 right-2"
44+
>
45+
<a
46+
href="https://github.com/Tresjs/lab/tree/main/components/content/dancing-blob"
47+
target="_blank"
48+
>Code</a>
4449
</button>
4550

46-
<TresCanvas :clear-color="'#0c1a30'" v-show="!showInfoDialog">
47-
<TheDancingBlob :analyser="analyser" :data-array="dataArray" />
51+
<TresCanvas
52+
v-show="!showInfoDialog"
53+
clear-color="#0c1a30"
54+
>
55+
<TheDancingBlob
56+
:analyser="analyser"
57+
:data-array="dataArray"
58+
/>
4859
</TresCanvas>
49-
<span class="blobPermissionDialog justify-center items-center infline-flex absolute" v-if="showInfoDialog">
60+
<span
61+
v-if="showInfoDialog"
62+
class="blobPermissionDialog justify-center items-center infline-flex absolute"
63+
>
5064
<p>
51-
Hey! <br />
65+
Hey! <br>
5266
This site requires microphone permissions. The
5367
microphone is only used to calculate the frequency necessary for the blob to dance. A browser pop-up will ask you
5468
for permission.
5569
</p>
5670
</span>
57-
5871
</template>
5972

6073
<style scoped>

components/content/galaxy-generator/index.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ onLoop(({ elapsed }) => {
134134
}
135135
})
136136
useControls('fpsgraph')
137-
const {count, size, radius, branches, spin, randomness, randomnessPower, insideColor, outsideColor} = useControls({
137+
const { count, size, radius, branches, spin, randomness, randomnessPower, insideColor, outsideColor } = useControls({
138138
139139
count: {
140140
value: 30000,

components/content/glass-material/index.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const options = reactive({
3131
envMapIntensity: 1.5,
3232
})
3333
34-
const {transmission, thickness, roughness, envMapIntensity, useHDR} = useControls({
34+
const { transmission, thickness, roughness, envMapIntensity, useHDR } = useControls({
3535
transmission: {
3636
value: 1,
3737
min: 0,
@@ -71,7 +71,7 @@ watch(useHDR.value, (value) => {
7171
</script>
7272

7373
<template>
74-
<TresLeches/>
74+
<TresLeches />
7575
<TresCanvas
7676
window-size
7777
v-bind="gl"

components/content/html-phone/iPhone.vue

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<script setup lang="ts">
22
import { Html, useGLTF } from '@tresjs/cientos'
33
import { Color, MeshStandardMaterial } from 'three'
4+
5+
const emit = defineEmits(['view-clicked'])
46
const { nodes } = await useGLTF('/models/iphone/iphonex.glb', { draco: true })
57
const model = nodes['iphonex']
68
const back = nodes['iphonexback']
@@ -12,13 +14,11 @@ screen?.position.set(0, 0, 0.01)
1214
screen.material.transparent = true
1315
screen.material.opacity = 0.5
1416
15-
1617
bottomTab.material = new MeshStandardMaterial({
1718
color: new Color('#000000'),
1819
emissive: new Color('#000000'),
1920
})
2021
21-
const emit = defineEmits(['view-clicked'])
2222
const closeUp = ref(false)
2323
function onViewClose() {
2424
closeUp.value = true
@@ -27,11 +27,23 @@ function onViewClose() {
2727
</script>
2828

2929
<template>
30-
<Levioso :speed="closeUp ? 0: 1" :rotationFactor="closeUp ? 0: 1">
31-
<TresGroup :position="[0, 1, 0]" ref="">
30+
<Levioso
31+
:speed="closeUp ? 0 : 1"
32+
:rotation-factor="closeUp ? 0 : 1"
33+
>
34+
<TresGroup
35+
ref=""
36+
:position="[0, 1, 0]"
37+
>
3238
<primitive :object="model">
33-
<Html :distance-factor="1.39" transform :position="[1, 0, 0.2]" :occlude="[back]" v-if="!closeUp">
34-
<button @click="onViewClose"
39+
<Html
40+
v-if="!closeUp"
41+
:distance-factor="1.39"
42+
transform
43+
:position="[1, 0, 0.2]"
44+
:occlude="[back]"
45+
>
46+
<button
3547
class="
3648
p-6
3749
flex
@@ -43,8 +55,9 @@ function onViewClose() {
4355
transition-colors
4456
duration-200
4557
ease-in-out
46-
"
47-
>
58+
"
59+
@click="onViewClose"
60+
>
4861
<i class="i-carbon-view" />
4962
</button>
5063
</Html>
@@ -62,7 +75,6 @@ function onViewClose() {
6275
frameborder="0"
6376
/>
6477
</Html>
65-
6678
</primitive>
6779
<primitive :object="back" />
6880
</TresGroup>

components/content/html-phone/index.vue

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script setup lang="ts">
22
import { TresCanvas } from '@tresjs/core'
3-
import { BasicShadowMap, SRGBColorSpace, NoToneMapping, PerspectiveCamera } from 'three'
3+
import type { PerspectiveCamera } from 'three'
4+
import { BasicShadowMap, SRGBColorSpace, NoToneMapping } from 'three'
45
import gsap from 'gsap'
56
67
const gl = {
@@ -14,16 +15,16 @@ const gl = {
1415
const cameraRef = ref<PerspectiveCamera | null>(null)
1516
1617
const onViewClicked = () => {
17-
if(!cameraRef.value) return
18+
if (!cameraRef.value) return
1819
gsap.to(cameraRef.value.position, {
1920
duration: 1,
2021
x: 0,
2122
y: 3,
2223
z: 3,
2324
ease: 'power2.inOut',
2425
onUpdate: () => {
25-
if(cameraRef.value)
26-
cameraRef.value.lookAt(0, 3, 0)
26+
if (cameraRef.value)
27+
cameraRef.value.lookAt(0, 3, 0)
2728
},
2829
})
2930
}
@@ -33,9 +34,15 @@ const { hasFinishLoading, progress } = await useProgress()
3334

3435
<template>
3536
<div class="hero absolute z-30 prose p-24">
36-
<h2 class="text-6xl opacity-0 animate-fade-in animate-delay-1s animate-forwards transition-all ease-in-out">iTres</h2>
37-
<p class="text-2xl opacity-0 animate-fade-in animate-delay-2s animate-forwards transition-all ease-in-out">New fancy phone, mind-blowing. Head turning. </p>
38-
<p class="opacity-0 animate-fade-in animate-delay-4s animate-forwards transition-all ease-in-out">Only $2999.99</p>
37+
<h2 class="text-6xl opacity-0 animate-fade-in animate-delay-1s animate-forwards transition-all ease-in-out">
38+
iTres
39+
</h2>
40+
<p class="text-2xl opacity-0 animate-fade-in animate-delay-2s animate-forwards transition-all ease-in-out">
41+
New fancy phone, mind-blowing. Head turning.
42+
</p>
43+
<p class="opacity-0 animate-fade-in animate-delay-4s animate-forwards transition-all ease-in-out">
44+
Only $2999.99
45+
</p>
3946
</div>
4047
<Transition
4148
name="fade-overlay"
@@ -48,7 +55,7 @@ const { hasFinishLoading, progress } = await useProgress()
4855
>
4956
<div class="w-200px text-black text-center">
5057
<p class="animate-tada">
51-
🤳
58+
🤳
5259
</p>
5360
Loading... {{ progress }} %
5461
</div>

0 commit comments

Comments
 (0)