Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I’m sorry, I misunderstood your request. Here is the code for the webpage with what you described.
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Three.js scene with animated texture</title>
- <style>
- body {
- margin: 0;
- }
- canvas {
- width: 100%;
- height: 100%;
- }
- </style>
- </head>
- <body>
- <script src="https://threejs.org/build/three.js"></script>
- <script>
- // Create a scene
- var scene = new THREE.Scene();
- // Create a camera
- var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
- camera.position.z = 5;
- // Create a renderer
- var renderer = new THREE.WebGLRenderer();
- renderer.setSize(window.innerWidth, window.innerHeight);
- document.body.appendChild(renderer.domElement);
- // Create a cube geometry
- var geometry = new THREE.BoxGeometry(1, 1, 1);
- // Create a texture loader
- var loader = new THREE.TextureLoader();
- // Load the image with multiple frames and create a material
- var texture = loader.load("https://threejs.org/examples/textures/sprite.png");
- var material = new THREE.MeshBasicMaterial({map: texture});
- // Create a cube mesh with the material
- var cube = new THREE.Mesh(geometry, material);
- scene.add(cube);
- // Create a JSON object for the animation
- var animation = {"frametime": 0.5, "frames": [0, 1, 2, 3]};
- // Create a variable to store the current frame index
- var frameIndex = 0;
- // Create a function to update the texture according to the animation
- function updateTexture() {
- // Get the next frame index from the animation object
- frameIndex = (frameIndex + 1) % animation.frames.length;
- var nextFrame = animation.frames[frameIndex];
- // Calculate the offset of the texture map based on the frame index
- var offsetX = nextFrame % 4 * 0.25; // The image has 4 columns of frames
- var offsetY = Math.floor(nextFrame / 4) * 0.25; // The image has 2 rows of frames
- // Set the offset of the texture map
- texture.offset.set(offsetX, offsetY);
- // Schedule the next update after the frametime interval
- setTimeout(updateTexture, animation.frametime * 1000);
- }
- // Start the texture update loop
- updateTexture();
- // Create a function to animate the cube
- function animate() {
- requestAnimationFrame(animate);
- // Rotate the cube on its x and y axis
- cube.rotation.x += 0.01;
- cube.rotation.y += 0.01;
- // Render the scene with the camera
- renderer.render(scene, camera);
- }
- // Start the animation loop
- animate();
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement