How do I do carousel effect in react three fiber (r3f)
Answered
House Wren posted this in #help-forum
House WrenOP
something like this in the video I can't get the positions right
78 Replies
@House Wren something like this in the video I can't get the positions right
would you mind sharing the repo or a minimal repro repo? That would help a lot to test out specific settings
@B33fb0n3 would you mind sharing the repo or a minimal repro repo? That would help a lot to test out specific settings
House WrenOP
do you mean in the video ? I don't have it I have a scene with a just three cubes in it
House WrenOP
This is what I have currently, I'm testing three primitive models, where swipe right and left should shift them on position in the x. here's the code and I'll inculde a video of the scene
function PrimitiveModel({
primitiveType,
color,
index,
currentIndex,
setIndex,
models,
}) {
const mesh = useRef();
const [{ x, rotation }, set] = useSpring(() => ({
x: index - currentIndex,
rotation: 0,
config: { mass: 1, tension: 170, friction: 26 },
}));
useEffect(() => {
set({ x: index - currentIndex });
}, [currentIndex, index, set]);
useFrame(() => {
mesh.current.position.x = x.get();
mesh.current.rotation.y = rotation.get();
});
const bind = useDrag(
({ direction: [dx], distance, cancel }) => {
console.log(dx, distance);
if (distance > 50) cancel();
if (dx > 0) {
console.log("right swipe" + " current index: " + currentIndex);
setIndex((state) => state + 1);
}
if (dx < 0) {
console.log("left swipe" + " current index: " + currentIndex);
setIndex((state) => state - 1);
}
},
{ axis: "x" }
);
return (
<Float enabled={true}>
<a.mesh ref={mesh} {...bind()}>
{primitiveType === "box" && <boxGeometry args={[1, 1, 1]} />}
{primitiveType === "sphere" && <sphereGeometry args={[0.5, 32, 32]} />}
{primitiveType === "cone" && <coneGeometry args={[0.5, 1, 32]} />}
<meshStandardMaterial color={color} />
</a.mesh>
</Float>
);
}
export default function ThreeModelSwipe({ enableSwipe = true }) {
const [index, setIndex] = useState(0);
const models = [
{ primitiveType: "box", color: "orange" },
{ primitiveType: "sphere", color: "blue" },
{ primitiveType: "cone", color: "green" },
];
return (
<>
{models.map((model, i) => (
<PrimitiveModel
key={i}
primitiveType={model.primitiveType}
color={model.color}
index={i}
setIndex={setIndex}
currentIndex={index}
models={models}
/>
))}
</>
);
}their y position is there it should have been .5 to be above the ground but ignore that
@House Wren do you mean in the video ? I don't have it I have a scene with a just three cubes in it
Thanks for the code. Can’t you change just the camera position instead of changing the rotation? You save 3 positions of the camera (target & position) and then just set the position and target of the current camera ref to the next saved positions when moving the camera
What did you found about snap positions with threejs and react?
What did you found about snap positions with threejs and react?
@B33fb0n3 Thanks for the code. Can’t you change just the camera position instead of changing the rotation? You save 3 positions of the camera (target & position) and then just set the position and target of the current camera ref to the next saved positions when moving the camera
What did you found about snap positions with threejs and react?
House WrenOP
this will not create the effect you see in the first video
the error is in the index but i can't figure it out
@House Wren this will not create the effect you see in the first video
Why not? Instead of moving the whole scene, the camera will just move. I can show you can example (with camera move) in one scene if you want to see if it’s possible
@House Wren something like this in the video I can't get the positions right
House WrenOP
I said that because when you look this video you will notice the rotation of the shirts, and the stability of the three columns behind it
I'm trying to replicate this effect
I think in the video he didn't rotate the camera he rotated just the shirts
this seems like easier option also
@House Wren post it please
As you can see there are multiple heads (in your case multiple t-shirts) and just the camposition and cam target is changed. You can see a smooth "move" effect to preview the new head (in my case)
@B33fb0n3 As you can see there are multiple heads (in your case multiple t-shirts) and just the camposition and cam target is changed. You can see a smooth "move" effect to preview the new head (in my case)
House WrenOP
looks cool but I can't imagine how the 3 cloumns in the background will behave, if I rotated the camera to the right you be looking at the columns side
@House Wren looks cool but I can't imagine how the 3 cloumns in the background will behave, if I rotated the camera to the right you be looking at the columns side
thx. Send me a .glb with these objects inside it and I will show you how it would look like when doing it with them. It shouldn't be a difference
@House Wren can you share the code by the way
It's to complex to share the whole code to this. What I am doing is bascially just calling the setLookAt method from the CameraControls from react three drei to the new location. Inside my code it looks like this:
cameraControlRef.current.setLookAt(camPosition[0], camPosition[1], camPosition[2], camTarget[0], camTarget[1], camTarget[2], true);House WrenOP
cool
@B33fb0n3 It's to complex to share the whole code to this. What I am doing is bascially just calling the setLookAt method from the CameraControls from react three drei to the new location. Inside my code it looks like this:
tsx
cameraControlRef.current.setLookAt(camPosition[0], camPosition[1], camPosition[2], camTarget[0], camTarget[1], camTarget[2], true);
House WrenOP
my problem isn't with the camera movement and postion my problem is with these three columns behind the shirts
@House Wren cool
House WrenOP
this is the column
@House Wren my problem isn't with the camera movement and postion my problem is with these three columns behind the shirts
you mean that it will change his material & lighting?
btw while checking your thread you created a 2 hour headache for me, because my sql query wasnt correctly written. Now it is, but it was hell...
@B33fb0n3 btw while checking your thread you created a 2 hour headache for me, because my sql query wasnt correctly written. Now it is, but it was hell...
House WrenOP
I used to write queries, it's not productive
@B33fb0n3 you mean that it will change his material & lighting?
House WrenOP
no forget about the material and lighting thing in the column, I mean, when you rotate the camera you will be looking at the sides of the columns not the front
@House Wren just use prisma
that wouldnt solve this problem
@House Wren I used to write queries, it's not productive
I am using drizzle and it's also an ORM like prisma is
@B33fb0n3 I am using drizzle and it's also an ORM like prisma is
House WrenOP
yeah I know it
@B33fb0n3 I have no idea what u r talking about 💀 <:lolsob:753870958489632819>
House WrenOP
okay the camera at the begining of the scene is positioned at [0,0,0] and thus you see the font side only of these three columns in the back of the shirts when you rotate the camera (your idea) instead of rotating the shirts you will not be looking at the front side of the columns you get it ?
@House Wren Click to see attachment
Ahhhh now I understood! In my case the pillars wont move, because they are in a different block. So the camera would rotate to the next and the pillars stays the same in my example
@B33fb0n3 Ahhhh now I understood! In my case the pillars wont move, because they are in a different block. So the camera would rotate to the next and the pillars stays the same in my example
House WrenOP
yeah tha't will not work in my case
I was able to move the objects with dragging
but still no way near this effect
the drag function isn't on the models themselves it's on the component holder all the models
but is he making that effect that when you are swipping to right the models moves slowly with you and at some point it sets still to perfectly center at pos.x = 0
@House Wren Click to see attachment
for me it looks like a seperate file that checks the current drag and if there is a drag, then move it a bit to the drag and if there is no drag, move it to the selected element 🤔
Very interesting effect ✨
Very interesting effect ✨
so something like (while dragging) move component itself it the drag and on release set the closest shirt to x = 0 to x= 0 right?
how about the rotation
it's a simple move in x position effect how's he making the movement tho
the models are set like this from a top view
these rectangles are shirts haha
@House Wren yeah it's adidas
I just checked their page and it looks like they using portals for the background (ex: https://codesandbox.io/p/sandbox/threejs-portal-effect-rcsg0?file=%2Findex.html)
They also using the rig effect for the mouse (or any other effect that using
lerp)House WrenOP
what's a portal
House WrenOP
I found something
it's the same idea in this website but this website uses scroll instead of swipe
here's the repo
he's using threejs directly not r3f
@House Wren he's using threejs directly not r3f
Normally its easy to convert it 🤔
@B33fb0n3 this website is killing my browser because of the updating element on page 💀
House WrenOP
yeah so slow
but works 🥸
if it works it works 🥸
haha noooo please dont create it like that xD
I thought u want this cool effect
I thought u want this cool effect
@B33fb0n3 haha noooo please dont create it like that xD
I thought u want this cool effect
House WrenOP
I'm just trying to figure out the effect, the most reasonalbe solution is that when you swipe the models move with the swipe but when you release your finger the closest shirt gets centered in the middle now the question is how do you make not only move in x postion but also rotate
@House Wren I'm just trying to figure out the effect, the most reasonalbe solution is that when you swipe the models move with the swipe but when you release your finger the closest shirt gets centered in the middle now the question is how do you make not only move in x postion but also rotate
it sounds like there might be a way to use snap points. These snap points snap each t-shirt to the nearest t-shirt. I just checked the apple website on how they do their snap points and they have an objects for it:
{
"phi": 0,
"theta": 180,
"label": "front-top",
"threshold": 45
}House WrenOP
what are these field tho
good question. The label is pretty simple. The threshold sounds like a threshold when to snap to that point. phi and theta are rotation values. In this case it's from the iphone model, that you can customize. Front top is the top of the front page. So 180 is a degree for the rotation of the front and the phi is the rotation of the x axis I guess
House WrenOP
how do you make the transition from point to point with scroll for example
isn't there a lib for it?
I don't think people create the math each time doesn't make sense
@House Wren how do you make the transition from point to point with scroll for example
The CameraControl would do that for me. Like I would only call the
setLookAt function and the camera moves (and animates) automatically. And because I would change the camera, the camera would do the animation stuffHouse WrenOP
I think I figured it out
tell me if you want the code I will mark the thread as solved
@House Wren tell me if you want the code I will mark the thread as solved
Yea, let me see how you done it
House WrenOP
Answer
House WrenOP
and remember, if it works. it's correct
@House Wren Click to see attachment
wow! So much duplication, but as you said:
if it works. it's correct🤣
