I started with three js recently and I am trying to make a static website with a space theme. I did found a yt video to render bunch of stars and make them move towards the camera and once they go past the camera, they will simply re render. resulting in a cool effect. But I want to add some space shuttles and other 3D models to make it even better, I have loaded a space shuttle model that I want to use but I don’t know how to animate it so that it can create an effect of “floating” in the space.
This is the current state of my project, and I want the space shuttle model to also go past the camera just like stars and then re render. I am sorry if this sounds dumb, am just starting out. Do lemme know if someone want to have a look at the code.
What I would like to se: some suggestions/ study material on how to tackle this issue.
bunch of space shuttle will be rendered randomly and will just travel in a straight path and will re render once they pass the camera pov.
I tried following the same process like the stars. But it’s not really working. I did some searching and found out that I might need to do some basic animation. So I am figuring out the optimal solution for this.
If you want a simple movement of your space shuttle just like your stars, update your space shuttle’s position in the animate function like your are doing for stars, like so:
let scene, camera, renderer, starGeo, stars, root ; // make root global
function animate() {
starGeo.attributes.position.array.forEach((_, index) => {
const x = starGeo.attributes.position.array[index * 3];
const y = starGeo.attributes.position.array[index * 3 + 1];
const z = starGeo.attributes.position.array[index * 3 + 2];
starGeo.attributes.position.array[index * 3 + 2] += 0.5; // Move towards the camera
if (z > 300) {
starGeo.attributes.position.array[index * 3 + 2] = -300; // Reset position once it goes beyond the screen
}
});
if(root)
root.position.z += 0.1
starGeo.attributes.position.needsUpdate = true;
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
Also, instead of checking for root is undefined till it is loaded, better use a loading manager to wait for your space shuttle to load, then start animating.
I am trying to clone the 3D models to place them on to the scene and I have added this code, but I am running into some issue,
(1) this code have no effect
(2) but is slowing my program down and making it lag, which suggest it is working but not rendering any models?
That’s not the case because I have already tried doing that.
Update: once I reduced the amount of 3D models to be rendered to 10 from 3000 it stopped lagging. So, there has to be some process running in the background that is not letting the cloned models to render. Also it’s not showing any warnings in the console as well.