Skip to content

Commit b42d1b7

Browse files
Merge pull request #79 from Tresjs/feature-add-infinite-tube-demo
feat: add infinite demo to playground
2 parents 3193575 + e1fbb3f commit b42d1b7

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<script setup>
2+
import { Vector3, CatmullRomCurve3, BackSide, TubeGeometry, RepeatWrapping } from 'three'
3+
import { useWindowSize } from '@vueuse/core'
4+
5+
const { map, aoMap, normalMap } = await useTexture({
6+
map: 'https://raw.githubusercontent.com/Tresjs/assets/main/textures/alien_flesh/Alien_Flesh_001_color.jpg',
7+
normalMap: 'https://raw.githubusercontent.com/Tresjs/assets/main/textures/alien_flesh/Alien_Flesh_001_norm.jpg',
8+
aoMap: 'https://raw.githubusercontent.com/Tresjs/assets/main/textures/alien_flesh/Alien_Flesh_001_occ.jpg',
9+
})
10+
11+
const tubeMaterialRef = shallowRef(null)
12+
const tubeRef = ref(null)
13+
const progress = ref(0)
14+
const { width, height } = useWindowSize()
15+
16+
const points = []
17+
18+
for (let i = 0; i < 5; i += 1) {
19+
points.push(new Vector3(0, 0, 2.5 * (i / 4)))
20+
}
21+
22+
const curve = new CatmullRomCurve3(points)
23+
24+
watch(tubeMaterialRef, (value) => {
25+
value.map.wrapS = RepeatWrapping
26+
value.map.wrapT = RepeatWrapping
27+
value.normalMap.wrapS = RepeatWrapping
28+
value.normalMap.wrapT = RepeatWrapping
29+
value.aoMap.wrapS = RepeatWrapping
30+
value.aoMap.wrapT = RepeatWrapping
31+
})
32+
watch(progress, (value) => {
33+
tubeRef.value.geometry.dispose()
34+
curve.points[2].x = value * 0.05
35+
curve.points[2].y = - value * 0.05
36+
curve.points[4].x = value * 0.005
37+
tubeRef.value.geometry = new TubeGeometry(curve, 70, 0.02, 50, false)
38+
})
39+
40+
const { onLoop } = useRenderLoop()
41+
42+
onLoop(({ delta }) => {
43+
tubeMaterialRef.value.map.offset.x -= delta * 0.25
44+
tubeMaterialRef.value.normalMap.offset.x -= delta * 0.25
45+
tubeMaterialRef.value.aoMap.offset.x -= delta * 0.25
46+
})
47+
</script>
48+
49+
<template>
50+
<TresCanvas
51+
window-size
52+
clear-color="#000000"
53+
antialias
54+
>
55+
<TresFog :args="[0x222222, 0.6, 2.8]" />
56+
<TresPerspectiveCamera
57+
:args="[8, width / height, 0.1, 1000]"
58+
:position="[0, 0, 0.35]"
59+
/>
60+
<ScrollControls
61+
v-model="progress"
62+
:distance="0"
63+
/>
64+
<TresMesh
65+
ref="tubeRef"
66+
:position="[0, 0, -2]"
67+
>
68+
<TresTubeGeometry :args="[curve, 70, 0.02, 50, false]" />
69+
<TresMeshStandardMaterial
70+
ref="tubeMaterialRef"
71+
:side="BackSide"
72+
:map="map"
73+
:ao-map="aoMap"
74+
:normal-map="normalMap"
75+
/>
76+
</TresMesh>
77+
<TresDirectionalLight :args="[0xffffff, 0.8]" />
78+
<TresHemisphereLight :args="[0xffffbb, 0x887979, 0.9]" />
79+
</TresCanvas>
80+
</template>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
---
2+
thumbnail: /infinite-tube.png
3+
title: Infinite Tube
4+
slug: infinite-tube
5+
author: jaime-bboyjt
6+
status: published
7+
description: How to create an infinite tube easy with Tresjs
8+
tags: ['scroll-controls', 'tube-geometry', 'textures-offset']
9+
---
10+
11+
::infinite-tube
12+
::
13+
14+
::the-info
15+
16+
![infinite-tube](/infinite-tube.png)
17+
18+
# Infinite tube
19+
20+
Author: [@**jaimebboyjt**](https://twitter.com/jaimebboyjt).
21+
22+
> An easy way to create infinite tubes with TresJS.
23+
24+
```vue
25+
<script setup>
26+
import { Vector3, CatmullRomCurve3, BackSide, TubeGeometry, RepeatWrapping } from 'three'
27+
import { useWindowSize } from '@vueuse/core'
28+
29+
const { map, aoMap, normalMap } = await useTexture({
30+
map: 'https://raw.githubusercontent.com/Tresjs/assets/main/textures/alien_flesh/Alien_Flesh_001_color.jpg',
31+
normalMap: 'https://raw.githubusercontent.com/Tresjs/assets/main/textures/alien_flesh/Alien_Flesh_001_norm.jpg',
32+
aoMap: 'https://raw.githubusercontent.com/Tresjs/assets/main/textures/alien_flesh/Alien_Flesh_001_occ.jpg',
33+
})
34+
35+
const tubeMaterialRef = shallowRef(null)
36+
const tubeRef = ref(null)
37+
const progress = ref(0)
38+
const { width, height } = useWindowSize()
39+
40+
const points = []
41+
42+
for (let i = 0; i < 5; i += 1) {
43+
points.push(new Vector3(0, 0, 2.5 * (i / 4)))
44+
}
45+
46+
const curve = new CatmullRomCurve3(points)
47+
48+
watch(tubeMaterialRef, (value) => {
49+
value.map.wrapS = RepeatWrapping
50+
value.map.wrapT = RepeatWrapping
51+
value.normalMap.wrapS = RepeatWrapping
52+
value.normalMap.wrapT = RepeatWrapping
53+
value.aoMap.wrapS = RepeatWrapping
54+
value.aoMap.wrapT = RepeatWrapping
55+
})
56+
watch(progress, (value) => {
57+
tubeRef.value.geometry.dispose()
58+
curve.points[2].x = value * 0.05
59+
curve.points[2].y = - value * 0.05
60+
curve.points[4].x = value * 0.005
61+
tubeRef.value.geometry = new TubeGeometry(curve, 70, 0.02, 50, false)
62+
})
63+
64+
const { onLoop } = useRenderLoop()
65+
66+
onLoop(({ elapsed }) => {
67+
tubeMaterialRef.value.map.offset.x -= delta * 0.25
68+
tubeMaterialRef.value.normalMap.offset.x -= delta * 0.25
69+
tubeMaterialRef.value.aoMap.offset.x -= delta * 0.25
70+
})
71+
</script>
72+
73+
<template>
74+
<TresCanvas
75+
window-size
76+
clear-color="#ff4444"
77+
antialias
78+
>
79+
<TresFog :args="[0x222222, 0.6, 2.8]" />
80+
<TresPerspectiveCamera
81+
:args="[8, width / height, 0.1, 1000]"
82+
:position="[0, 0, 0.35]"
83+
/>
84+
<ScrollControls
85+
v-model="progress"
86+
:distance="0"
87+
/>
88+
<TresMesh
89+
ref="tubeRef"
90+
:position="[0, 0, -2]"
91+
>
92+
<TresTubeGeometry :args="[curve, 70, 0.02, 50, false]" />
93+
<TresMeshStandardMaterial
94+
ref="tubeMaterialRef"
95+
:side="BackSide"
96+
:map="map"
97+
:ao-map="aoMap"
98+
:normal-map="normalMap"
99+
/>
100+
</TresMesh>
101+
<TresDirectionalLight :args="[0xffffff, 0.8]" />
102+
<TresHemisphereLight :args="[0xffffbb, 0x887979, 0.9]" />
103+
</TresCanvas>
104+
</template>
105+
106+
```

public/infinite-tube.png

756 KB
Loading

0 commit comments

Comments
 (0)