html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>トゥーンレンダリングの3Dキャラクター</title>
<style>
body { margin: 0; }
canvas { display: block; }
</style>
</head>
<body>
<!-- three.jsライブラリをCDNから読み込む -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r148/three.min.js"></script>
<!-- OrbitControls(カメラ操作用) -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r148/examples/js/controls/OrbitControls.js"></script>
<script>
// シーンの作成
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xffffff); // 背景色を白に設定
// カメラの設定
const camera = new THREE.PerspectiveCamera(
75, // 視野角
window.innerWidth / window.innerHeight, // アスペクト比
0.1, // ニアクリップ
1000 // ファークリップ
);
camera.position.set(0, 0, 5); // カメラをz軸方向に少し離す
// レンダラーの設定
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// リサイズ対応
window.addEventListener('resize', () => {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height);
camera.aspect = width / height;
camera.updateProjectionMatrix();
});
// 照明の追加
// 環境光
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
// 平行光源(影響を与える)
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(5, 10, 7.5);
scene.add(directionalLight);
// トゥーンシェーダー用の色マップを作成
const gradientTexture = new THREE.TextureLoader().load('https://threejs.org/examples/textures/gradients/3.jpg');
gradientTexture.minFilter = THREE.LinearFilter;
// マテリアルの作成
const material = new THREE.MeshToonMaterial({
color: 0x156289,
gradientMap: gradientTexture
});
// ジオメトリの作成(ここではシンプルな立方体)
const geometry = new THREE.BoxGeometry(1, 1, 1);
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
// コントロールの追加(オプション)
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true; // 慣性を有効にする
controls.dampingFactor = 0.05;
// アニメーションループ
function animate() {
requestAnimationFrame(animate);
// キャラクターを回転させる
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
controls.update(); // コントロールを更新
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>