File size: 2,051 Bytes
c29f49d
 
09c7acd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9346d4c
 
 
 
 
 
09c7acd
9346d4c
 
 
 
 
 
 
 
 
 
 
 
09c7acd
 
 
 
 
9346d4c
 
 
 
 
 
 
 
 
 
 
 
 
09c7acd
 
 
 
 
 
 
 
9346d4c
09c7acd
 
c29f49d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<!DOCTYPE html>
<html>
<head>
  <title>Three.js Scene and Animation Example</title>
  <style>
    body {
      margin: 0;
      padding: 0;
    }
    canvas {
      display: block;
    }
  </style>
</head>
<body>
  <script src="https://threejs.org/build/three.min.js"></script>
  <script>
    // Set up the scene, camera, and renderer
    const scene = new THREE.Scene();
    const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
    const renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);

    // Add lighting to the scene
    const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
    scene.add(ambientLight);
    const pointLight = new THREE.PointLight(0xffffff, 1);
    pointLight.position.set(5, 5, 5);
    scene.add(pointLight);

    // Create a complex shaded shape geometry and material
    const geometry = new THREE.TorusKnotGeometry(1, 0.4, 256, 32, 2, 3, 1);
    const material = new THREE.MeshPhongMaterial({
      color: 0xffaaff,
      specular: 0x999999,
      shininess: 50,
      flatShading: false,
    });

    // Create a shape mesh and add it to the scene
    const shape = new THREE.Mesh(geometry, material);
    scene.add(shape);

    // Set up animation loop
    const animate = function () {
      requestAnimationFrame(animate);

      // Rotate the shape
      shape.rotation.x += 0.01;
      shape.rotation.y += 0.01;

      // Move the camera in a circle around the shape
      const time = Date.now() * 0.001;
      const radius = 10;
      camera.position.x = Math.sin(time) * radius;
      camera.position.z = Math.cos(time) * radius;
      camera.lookAt(new THREE.Vector3());

      // Update the lighting position to follow the camera
      pointLight.position.copy(camera.position);

      renderer.render(scene, camera);
    };

    // Start the animation loop
    animate();

    // Adjust camera position
    camera.position.z = 10;
  </script>
</body>
</html>