o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
なんかめちゃ面白いゲームをhtml一つのファイルで動くもので作って(・ω・)3dの世界を一人称視点で移動できるものなら、何でも構いません(・ω・)
- User
なんかめちゃ面白いゲームをhtml一つのファイルで動くもので作って(・ω・)3dの世界を一人称視点で移動できるものなら、何でも構いません(・ω・)
- Assistant
なんかめちゃ面白いゲームをhtml一つのファイルで動くもので作って(・ω・)3dの世界を一人称視点で移動できるものなら、何でも構いません(・ω・)
わかりました!以下は、一人称視点で3Dの世界を移動できる簡単なゲームを、1つのHTMLファイルで実装したものです。このコードをコピーして、拡張子が「.html」のファイルに保存し、ブラウザで開けば動作します。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>シンプルな3D一人称視点ゲーム</title>
<style>
body, html { margin: 0; padding: 0; overflow: hidden; }
canvas { display: block; }
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
// キャンバスの初期設定
const canvas = document.getElementById('canvas');
const gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// シェーダーのソースコード
const vertexShaderSource = `
attribute vec3 position;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
void main(void){
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`;
const fragmentShaderSource = `
precision mediump float;
void main(void){
gl_FragColor = vec4(0.6, 0.8, 1.0, 1.0);
}
`;
// シェーダーのコンパイル
function createShader(gl, type, source) {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert('シェーダーのコンパイルに失敗しました: ' + gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
return null;
}
return shader;
}
// プログラムのリンク
function createProgram(gl, vs, fs) {
const program = gl.createProgram();
gl.attachShader(program, vs);
gl.attachShader(program, fs);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
alert('プログラムのリンクに失敗しました: ' + gl.getProgramInfoLog(program));
gl.deleteProgram(program);
return null;
}
return program;
}
// バッファの設定
function createCube(gl) {
const positions = new Float32Array([
// 前面
-1, -1, 1,
1, -1, 1,
1, 1, 1,
-1, 1, 1,
// 背面
-1, -1, -1,
-1, 1, -1,
1, 1, -1,
1, -1, -1,
// 上面
-1, 1, -1,
-1, 1, 1,
1, 1, 1,
1, 1, -1,
// 底面
-1, -1, -1,
1, -1, -1,
1, -1, 1,
-1, -1, 1,
// 右側面
1, -1, -1,
1, 1, -1,
1, 1, 1,
1, -1, 1,
// 左側面
-1, -1, -1,
-1, -1, 1,
-1, 1, 1,
-1, 1, -1,
]);
const indices = new Uint16Array([
0, 1, 2, 0, 2, 3, // 前面
4, 5, 6, 4, 6, 7, // 背面
8, 9, 10, 8, 10, 11, // 上面
12, 13, 14, 12, 14, 15, // 底面
16, 17, 18, 16, 18, 19, // 右側面
20, 21, 22, 20, 22, 23 // 左側面
]);
const positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);
const indexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
return {
position: positionBuffer,
indices: indexBuffer,
count: indices.length
};
}
// マトリックスのユーティリティ
function createMatrix() {
return new Float32Array(16);
}
function identity(out) {
for (let i = 0; i < 16; i++) out[i] = (i % 5 == 0) ? 1 : 0;
return out;
}
function perspective(out, fovy, aspect, near, far) {
const f = 1.0 / Math.tan(fovy / 2);
out[0] = f / aspect;
out[5] = f;
out[10] = (far + near) / (near - far);
out[11] = -1;
out[14] = (2 * far * near) / (near - far);
out[1] = out[2] = out[3] = out[4] = out[6] = out[7] = out[8] = out[9] = out[12] = out[13] = out[15] = 0;
return out;
}
function translate(out, a, v) {
out[12] = a[0] + v[0];
out[13] = a[1] + v[1];
out[14] = a[2] + v[2];
return out;
}
function rotateY(out, a, rad) {
const s = Math.sin(rad);
const c = Math.cos(rad);
out[0] = c;
out[2] = -s;
out[8] = s;
out[10] = c;
out[1] = out[3] = out[4] = out[5] = out[6] = out[7] = out[9] = out[11] = out[12] = out[13] = out[14] = out[15] = 0;
out[5] = out[15] = 1;
return out;
}
// キーボード入力の処理
const keys = {};
window.addEventListener('keydown', (e) => { keys[e.key] = true; });
window.addEventListener('keyup', (e) => { keys[e.key] = false; });
// メイン処理
function main() {
// シェーダーとプログラムの設定
const vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexShaderSource);
const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource);
const program = createProgram(gl, vertexShader, fragmentShader);
gl.useProgram(program);
const cube = createCube(gl);
const positionLocation = gl.getAttribLocation(program, 'position');
gl.enableVertexAttribArray(positionLocation);
gl.bindBuffer(gl.ARRAY_BUFFER, cube.position);
gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cube.indices);
const modelViewMatrixLocation = gl.getUniformLocation(program, 'modelViewMatrix');
const projectionMatrixLocation = gl.getUniformLocation(program, 'projectionMatrix');
const modelViewMatrix = createMatrix();
const projectionMatrix = createMatrix();
let cameraPosition = [0, 0, 5];
let cameraRotation = 0;
function render() {
// キー入力に応じてカメラを移動
const speed = 0.1;
if (keys['ArrowUp']) {
cameraPosition[2] -= speed * Math.cos(cameraRotation);
cameraPosition[0] -= speed * Math.sin(cameraRotation);
}
if (keys['ArrowDown']) {
cameraPosition[2] += speed * Math.cos(cameraRotation);
cameraPosition[0] += speed * Math.sin(cameraRotation);
}
if (keys['ArrowLeft']) {
cameraRotation += 0.05;
}
if (keys['ArrowRight']) {
cameraRotation -= 0.05;
}
// ビューポートとクリア
gl.viewport(0, 0, canvas.width, canvas.height);
gl.clearColor(0.3, 0.3, 0.3, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.enable(gl.DEPTH_TEST);
// マトリックスの設定
identity(modelViewMatrix);
rotateY(modelViewMatrix, modelViewMatrix, cameraRotation);
translate(modelViewMatrix, modelViewMatrix, [-cameraPosition[0], -cameraPosition[1], -cameraPosition[2]]);
perspective(projectionMatrix, Math.PI / 4, canvas.width / canvas.height, 0.1, 100);
gl.uniformMatrix4fv(modelViewMatrixLocation, false, modelViewMatrix);
gl.uniformMatrix4fv(projectionMatrixLocation, false, projectionMatrix);
// 描画
gl.drawElements(gl.TRIANGLES, cube.count, gl.UNSIGNED_SHORT, 0);
requestAnimationFrame(render);
}
render();
}
main();
// リサイズ対応
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
</script>
</body>
</html>
説明:
canvas要素を使用して描画し、シェーダーを直接JavaScript内に記述しています。使用方法:
注意点:
楽しんでください!(・ω・)
Not yet included in a published release.