Advertisement
EugeneFitz

Untitled

Mar 27th, 2023
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.69 KB | None | 0 0
  1. I’m sorry, I misunderstood your request. Here is the code for the webpage with what you described.
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5.   <meta charset="utf-8">
  6.   <title>Three.js scene with animated texture</title>
  7.   <style>
  8.     body {
  9.       margin: 0;
  10.     }
  11.     canvas {
  12.       width: 100%;
  13.       height: 100%;
  14.     }
  15.   </style>
  16. </head>
  17. <body>
  18.   <script src="https://threejs.org/build/three.js"></script>
  19.   <script>
  20.     // Create a scene
  21.     var scene = new THREE.Scene();
  22.  
  23.     // Create a camera
  24.     var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
  25.     camera.position.z = 5;
  26.  
  27.     // Create a renderer
  28.     var renderer = new THREE.WebGLRenderer();
  29.     renderer.setSize(window.innerWidth, window.innerHeight);
  30.     document.body.appendChild(renderer.domElement);
  31.  
  32.     // Create a cube geometry
  33.     var geometry = new THREE.BoxGeometry(1, 1, 1);
  34.  
  35.     // Create a texture loader
  36.     var loader = new THREE.TextureLoader();
  37.  
  38.     // Load the image with multiple frames and create a material
  39.     var texture = loader.load("https://threejs.org/examples/textures/sprite.png");
  40.     var material = new THREE.MeshBasicMaterial({map: texture});
  41.  
  42.     // Create a cube mesh with the material
  43.     var cube = new THREE.Mesh(geometry, material);
  44.     scene.add(cube);
  45.  
  46.     // Create a JSON object for the animation
  47.     var animation = {"frametime": 0.5, "frames": [0, 1, 2, 3]};
  48.  
  49.     // Create a variable to store the current frame index
  50.     var frameIndex = 0;
  51.  
  52.     // Create a function to update the texture according to the animation
  53.     function updateTexture() {
  54.       // Get the next frame index from the animation object
  55.       frameIndex = (frameIndex + 1) % animation.frames.length;
  56.       var nextFrame = animation.frames[frameIndex];
  57.  
  58.       // Calculate the offset of the texture map based on the frame index
  59.       var offsetX = nextFrame % 4 * 0.25; // The image has 4 columns of frames
  60.       var offsetY = Math.floor(nextFrame / 4) * 0.25; // The image has 2 rows of frames
  61.  
  62.       // Set the offset of the texture map
  63.       texture.offset.set(offsetX, offsetY);
  64.  
  65.       // Schedule the next update after the frametime interval
  66.       setTimeout(updateTexture, animation.frametime * 1000);
  67.     }
  68.  
  69.     // Start the texture update loop
  70.     updateTexture();
  71.  
  72.     // Create a function to animate the cube
  73.     function animate() {
  74.       requestAnimationFrame(animate);
  75.  
  76.       // Rotate the cube on its x and y axis
  77.       cube.rotation.x += 0.01;
  78.       cube.rotation.y += 0.01;
  79.  
  80.       // Render the scene with the camera
  81.       renderer.render(scene, camera);
  82.     }
  83.  
  84.     // Start the animation loop
  85.     animate();
  86.   </script>
  87. </body>
  88. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement