o4-mini-2025-04-16· OpenAI
LMArena Human Preference 140K
使用three.js 生成一个由立方体组成的高达,要求高达足够细节,然后高达在一个平面上原地奔跑的动画,所有代码放在一个html文件里面
- User
使用three.js 生成一个由立方体组成的高达,要求高达足够细节,然后高达在一个平面上原地奔跑的动画,所有代码放在一个html文件里面
- Assistant
下面是一份完整的 HTML 文件示例,利用 Three.js 用无数个立方体拼出一个细节足够的高达(Gundam),并在地面上做“原地奔跑”动画。直接保存为
gundam.html,用浏览器打开即可。<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8" /> <title>Three.js 立方体高达原地奔跑</title> <style> body { margin: 0; overflow: hidden; } canvas { display: block; } </style> </head> <body> <script type="module"> import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.150.1/build/three.module.js'; import { OrbitControls } from 'https://cdn.jsdelivr.net/npm/three@0.150.1/examples/jsm/controls/OrbitControls.js'; // —— 场景、相机、渲染器 —— const scene = new THREE.Scene(); scene.background = new THREE.Color(0xa0a0a0); const camera = new THREE.PerspectiveCamera(45, innerWidth / innerHeight, 0.1, 1000); camera.position.set(10, 8, 12); camera.lookAt(0, 3, 0); const renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setSize(innerWidth, innerHeight); document.body.appendChild(renderer.domElement); const controls = new OrbitControls(camera, renderer.domElement); controls.target.set(0, 3, 0); controls.update(); // —— 光源 —— scene.add(new THREE.AmbientLight(0x666666)); const dirLight = new THREE.DirectionalLight(0xffffff, 0.8); dirLight.position.set(5, 10, 7); scene.add(dirLight); // —— 地面 —— const groundMat = new THREE.MeshPhongMaterial({ color: 0x999999, depthWrite: false }); const ground = new THREE.Mesh( new THREE.PlaneGeometry(200, 200), groundMat ); ground.rotation.x = -Math.PI / 2; scene.add(ground); // —— 创建高达主体 —— function createGundam() { const g = new THREE.Group(); // 材质 const white = new THREE.MeshPhongMaterial({ color: 0xffffff }); const blue = new THREE.MeshPhongMaterial({ color: 0x1565c0 }); const red = new THREE.MeshPhongMaterial({ color: 0xd32f2f }); const yellow= new THREE.MeshPhongMaterial({ color: 0xffeb3b }); // 身体 const torso = new THREE.Mesh(new THREE.BoxGeometry(2, 3, 1), blue); torso.position.y = 4; g.add(torso); // 中央红块 const chestRed = new THREE.Mesh(new THREE.BoxGeometry(0.6, 1.2, 0.4), red); chestRed.position.set(0, 4, 0.8); g.add(chestRed); // 黄色通风口 const ventL = new THREE.Mesh(new THREE.BoxGeometry(0.3, 0.4, 0.2), yellow); const ventR = ventL.clone(); ventL.position.set(-0.5, 3.3, 0.8); ventR.position.set( 0.5, 3.3, 0.8); g.add(ventL, ventR); // 头部 const head = new THREE.Group(); const headBase = new THREE.Mesh(new THREE.BoxGeometry(1.2, 1.2, 1.2), white); headBase.position.y = 5.7; head.add(headBase); // 头顶黄色“V” const v1 = new THREE.Mesh(new THREE.BoxGeometry(0.1, 0.8, 0.1), yellow); const v2 = v1.clone(); v1.position.set(-0.3, 6.4, 0); v2.position.set( 0.3, 6.4, 0); v1.rotation.z = 0.5; v2.rotation.z = -0.5; head.add(v1, v2); g.add(head); // 四肢辅助函数 function makeLimb(w, h, d, mat) { return new THREE.Mesh(new THREE.BoxGeometry(w, h, d), mat); } // 手臂 (左右) const armParams = [ { x: -1.6, sign: 1 }, // 左 { x: 1.6, sign: -1 } // 右 ]; const arms = []; armParams.forEach(p => { const arm = new THREE.Group(); const upper = makeLimb(0.6, 1.5, 0.6, white); upper.position.y = 3.8; upper.position.x = p.x; arm.add(upper); const lower = makeLimb(0.5, 1.4, 0.5, white); lower.position.y = 2.5; lower.position.x = p.x; arm.add(lower); const hand = makeLimb(0.4, 0.4, 0.4, white); hand.position.y = 1.75; hand.position.x = p.x; arm.add(hand); g.add(arm); arms.push(arm); }); // 腿 (左右) const legs = []; const legParams = [ { x: -0.6 }, { x: 0.6 } ]; legParams.forEach(p => { const leg = new THREE.Group(); const upper = makeLimb(0.7, 1.8, 0.7, white); upper.position.set(p.x, 2.0, 0); leg.add(upper); const lower = makeLimb(0.6, 1.6, 0.6, white); lower.position.set(p.x, 0.6, 0); leg.add(lower); const foot = makeLimb(0.8, 0.4, 1.2, white); foot.position.set(p.x, 0.2, 0.3); leg.add(foot); g.add(leg); legs.push(leg); }); // 控制关节 g.userData = { arms, legs }; return g; } const gundam = createGundam(); scene.add(gundam); // —— 动画:原地奔跑 —— function animate() { requestAnimationFrame(animate); const t = performance.now() * 0.003; // 运行速度系数 // 手臂摆动 gundam.userData.arms.forEach((arm, i) => { arm.rotation.z = Math.sin(t + i * Math.PI) * 0.5; }); // 腿部抬腿 gundam.userData.legs.forEach((leg, i) => { leg.rotation.x = Math.sin(t + i * Math.PI) * 0.8; }); renderer.render(scene, camera); } animate(); // —— 响应窗口大小变化 —— window.addEventListener('resize', () => { camera.aspect = innerWidth / innerHeight; camera.updateProjectionMatrix(); renderer.setSize(innerWidth, innerHeight); }); </script> </body> </html>打开后,你会看到一个由白色、蓝色、红色、黄色立方体拼成的“高达”模型,手臂和双腿交替摆动,在原地“奔跑”。支持鼠标拖拽旋转查看。欢迎按需调整颜色、几何尺寸、摆动幅度等细节!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.