<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8" /> <meta name = "viewport" content = "width=device-width, initial-scale = 1.0"> <title>テスト</title> <style> #canvas { display: block; margin-bottom: 10px; } </style> </head> <body> <canvas id="canvas"></canvas> <script type="module"> import * as THREE from "https://cdn.jsdelivr.net/npm/three@0.167.0/build/three.module.js"; // サイズを指定 const width = 360; const height = 360; // レンダラー,シーン,カメラを作成 const renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('canvas'), }); const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera(45, width / height); // 箱 /** @type {THREE.Mesh} */ let box; window.onload = () => { renderer.setPixelRatio(window.devicePixelRatio); renderer.setSize(width, height); camera.position.set(0, 0, 150); addObjects(); const INTERVAL = 1000 / 60; let nextUpdateTime = new Date().getTime() + INTERVAL; frameProc(); function frameProc(){ const curTime = new Date().getTime(); if(nextUpdateTime < curTime){ nextUpdateTime += INTERVAL; update(); } requestAnimationFrame(() => frameProc()); } } function addObjects(){ const geometry = new THREE.BoxGeometry(32, 32, 32); const material = new THREE.MeshNormalMaterial(); box = new THREE.Mesh(geometry, material); scene.add(box); const lineX = createLine(1000, 0, 0, -1000, 0, 0, 0xff0000) scene.add(lineX); const lineY = createLine(0, 1000, 0, 0, -1000, 0, 0x00ff00) scene.add(lineY); } // 色がcolorの(x1, y1, z1)と(x2, y2, z2)を結ぶ直線を生成する function createLine(x1, y1, z1, x2, y2, z2, color){ const lineMaterial = new THREE.LineBasicMaterial({ color: color }); const lineGeometry = new THREE.BufferGeometry(); const vertices = new Float32Array( [ x1, y1, z1, x2, y2, z2, ] ); lineGeometry.setAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) ); return new THREE.Line(lineGeometry, lineMaterial); } // 更新処理 function update() { box.rotation.y += 0.02; renderer.render(scene, camera); // レンダリング } </script> </body> </html>