16、绕点旋转

创建日期:2024-07-08
更新日期:2025-01-12
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>16_绕点旋转</title>
    <link href="../css/index.css" rel="stylesheet" />
    <script src="../libs/three.js"></script>
    <script src="../libs/dat.gui.min.js"></script>
</head>
<body>
    <script>
        var scene = new THREE.Scene();

        var renderer = new THREE.WebGLRenderer();
        renderer.setClearColor(0x000000, 1.0);
        renderer.setSize(window.innerWidth, window.innerHeight);
        renderer.shadowMapEnabled = true;
        renderer.shadowMapType = THREE.PCFSoftShadowMap;
        document.body.appendChild(renderer.domElement);

        var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 10000);
        camera.position.set(30, 30, 30);
        camera.lookAt(scene.position);

        var sphereGeometry = new THREE.SphereGeometry(5);
        var sphereMaterial = new THREE.MeshLambertMaterial({ color: 0x0000ff });
        var sphereMesh = new THREE.Mesh(sphereGeometry, sphereMaterial);
        sphereMesh.receiveShadow = true;
        scene.add(sphereMesh);

        var pivotPoint = new THREE.Object3D();

        var boxGeometry = new THREE.BoxGeometry(5, 5, 5);
        var boxMaterial = new THREE.MeshLambertMaterial({ color: 0xff0000 });
        var boxMesh = new THREE.Mesh(boxGeometry, boxMaterial);
        boxMesh.castShadow = true;
        pivotPoint.add(boxMesh);
        boxMesh.position.set(8, 8, 8);
        scene.add(pivotPoint);

        var light = new THREE.DirectionalLight(0xffffff);
        light.castShadow = true;
        light.shadowCameraNear = 0.01;
        light.shadowCameraFar = 1000;
        scene.add(light);
        light.position.set(14, 14, 14);

        var control = new function () {
            this.rotationSpeedX = 0.001;
            this.rotationSpeedY = 0.001;
            this.rotationSpeedZ = 0.001;
        };
        var gui = new dat.GUI();
        gui.add(control, 'rotationSpeedX', -0.2, 0.2);
        gui.add(control, 'rotationSpeedY', -0.2, 0.2);
        gui.add(control, 'rotationSpeedZ', -0.2, 0.2);

        var render = function () {
            requestAnimationFrame(render);
            pivotPoint.rotation.x += control.rotationSpeedX;
            pivotPoint.rotation.y += control.rotationSpeedY;
            pivotPoint.rotation.z += control.rotationSpeedZ;
            renderer.render(scene, camera);
        }
        render();
    </script>
</body>
</html>