|
| 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 | + |
| 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 | +``` |
0 commit comments