three实现三维公园荡秋千场景动画代码

代码语言:html

所属分类:三维

代码描述:three实现三维公园荡秋千场景动画代码

代码标签: three 三维 公园 秋千 场景 动画 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>温馨公园秋千</title>
    <style>
        * { margin: 0; padding: 0; }
        body { overflow: hidden; background: #87CEEB; }
        canvas { display: block; }
        #info {
            position: absolute;
            top: 15px;
            width: 100%;
            text-align: center;
            color: #fff;
            font-family: 'Arial', sans-serif;
            font-size: 18px;
            text-shadow: 1px 1px 3px rgba(0,0,0,0.3);
            pointer-events: none;
        }
    </style>
</head>
<body>
    <div id="info">🌳 温馨公园秋千 🌳 鼠标拖拽旋转 | 滚轮缩放</div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
    <script>
        // 场景、相机、渲染器
        const scene = new THREE.Scene();
        
        // 渐变天空背景
        const canvas = document.createElement('canvas');
        canvas.width = 512;
        canvas.height = 512;
        const ctx = canvas.getContext('2d');
        const gradient = ctx.createLinearGradient(0, 0, 0, 512);
        gradient.addColorStop(0, '#FFB347');
        gradient.addColorStop(0.3, '#FFCC80');
        gradient.addColorStop(0.5, '#FFE0B2');
        gradient.addColorStop(0.7, '#B3E5FC');
        gradient.addColorStop(1, '#81D4FA');
        ctx.fillStyle = gradient;
        ctx.fillRect(0, 0, 512, 512);
        const skyTexture = new THREE.CanvasTexture(canvas);
        scene.background = skyTexture;

        // 雾效
        scene.fog = new THREE.FogExp2(0xFFE0B2, 0.008);

        const camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.set(8, 5, 12);
        camera.lookAt(0, 3, 0);

        const renderer = new THREE.WebGLRenderer({ antialias: true });
        renderer.setSize(window.innerWidth, window.innerHeight);
        renderer.shadowMap.enabled = true;
        renderer.shadowMap.type = THREE.PCFSoftShadowMap;
        renderer.toneMapping = THREE.ACESFilmicToneMapping;
        renderer.toneMappingExposure = 1.2;
        document.body.appendChild(renderer.domElement);

        // 灯光 - 温暖的夕阳氛围
        const ambientLight = new THREE.AmbientLight(0xFFE4C4, 0.6);
        scene.add(ambientLight);

        const sunLight = new THREE.DirectionalLight(0xFFD700, 1.0);
        sunLight.position.set(15, 20, 10);
        sunLight.castShadow = true;
        sunLight.shadow.mapSize.width = 2048;
        sunLight.shadow.mapSize.height = 2048;
        sunLight.shadow.camera.near = 0.5;
        sunLight.shadow.camera.far = 100;
        sunLight.shadow.camera.left = -20;
        sunLight.shadow.camera.right = 20;
        sunLight.shadow.camera.top = 20;
        sunLight.shadow.camera.bottom = -20;
        scene.add(sunLight);

        const warmLight = new THREE.PointLight(0xFFA500, 0.5, 30);
        warmLight.position.set(-5, 8, 5);
        scene.add(warmLight);

        const fillLight = new THREE.HemisphereLight(0xFFE4B5, 0x8B7355, 0.4);
        scene.add(fillLight);

        // ============ 地面 ============
        const groundGeo = new THREE.CircleGeometry(40, 64);
        const groundMat = new THREE.MeshLambertMaterial({ color: 0x4CAF50 });
  .........完整代码请登录后点击上方下载按钮下载查看

网友评论0