generate a extended prompt of this. create a full html file of a 3d marble race game with custom made tracks and you can upload a image to customize a marble. you can select new game to start a new game, load game to load a saved game, save a game to save the game, and you can select how many marbles you want on the track. you can also create your own track.
Assistant
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>3D Marble Race Game</title>
<style>
body, html {
margin: 0; padding: 0; height: 100%; overflow: hidden;
font-family: Arial, sans-serif;
background: #222;
color: white;
}
#ui {
position: absolute;
z-index: 10;
top: 0; left: 0; width: 300px;
background: rgba(0,0,0,0.8);
padding: 15px;
height: 100vh;
box-sizing: border-box;
overflow-y: auto;
}
#ui h2 {
margin-top: 0;
}
#ui label, #ui button, #ui select {
display: block;
margin: 8px 0;
width: 100%;
}
#game-container {
position: absolute;
left: 300px;
top: 0;
right: 0;
bottom: 0;
background: #000;
}
#canvas3d {
width: 100%; height: 100%;
display: block;
}
#track-editor {
margin-top: 15px;
background: #111;
padding: 10px;
border-radius: 5px;
}
#track-editor textarea {
width: 100%;
height: 100px;
background: #222;
color: #fff;
border: 1px solid #555;
font-family: monospace;
}
#marble-preview {
width: 100px;
height: 100px;
border: 1px solid #555;
margin-top: 10px;
background-size: contain;
background-repeat: no-repeat;
background-position: center;
border-radius: 50%;
}
</style>
</head>
<body>
<div id="ui">
<h2>3D Marble Race</h2>
<label for="marbleCount">Number of Marbles:</label>
<select id="marbleCount">
<option value="2">2</option>
<option value="3" selected>3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<label for="marbleImageUpload">Upload Marble Texture:</label>
<input type="file" id="marbleImageUpload" accept="image/*" />
<div id="marble-preview"></div>
<button id="btnNewGame">New Game</button>
<button id="btnSaveGame">Save Game</button>
<button id="btnLoadGame">Load Game</button>
<div id="track-editor">
<h3>Track Editor</h3>
<p>Define your track as a list of 3D points to create a custom path. JSON format:</p>
<textarea id="trackData">// Example: [{"x":0,"y":0,"z":0},{"x":10,"y":0,"z":-10},{"x":20,"y":0,"z":0}]</textarea>
<button id="btnCreateTrack">Create Track</button>
</div>
</div>
<div id="game-container">
<canvas id="canvas3d"></canvas>
</div>
<!-- Include Three.js -->
<script src="https://cdn.jsdelivr.net/npm/three@0.152.2/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.152.2/examples/js/controls/OrbitControls.js"></script>
<script>
(() => {
// Basic variables
let scene, camera, renderer, controls;
let marbles = [];
let marbleMeshes = [];
let marbleTextures = [];
let marbleMaterial;
let trackPoints = [];
let trackCurve;
let trackMesh;
let NUM_MARBLES = 3;
let animationId = null;
let raceProgress = []; // progress along track [0..1] for each marble
// For saving/loading game state
const GAME_STATE_KEY = "marble_race_save";
// References to UI elements
const marbleCountSelect = document.getElementById('marbleCount');
const marbleImageUpload = document.getElementById('marbleImageUpload');
const marblePreview = document.getElementById('marble-preview');
const btnNewGame = document.getElementById('btnNewGame');
const btnSaveGame = document.getElementById('btnSaveGame');
const btnLoadGame = document.getElementById('btnLoadGame');
const trackDataTextarea = document.getElementById('trackData');
const btnCreateTrack = document.getElementById('btnCreateTrack');
const canvas = document.getElementById('canvas3d');
// Initialize Three.js
function initThree() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0x202020);
camera = new THREE.PerspectiveCamera(60, canvas.clientWidth / canvas.clientHeight, 0.1, 1000);
camera.position.set(30, 25, 30);
renderer = new THREE.WebGLRenderer({canvas, antialias: true});
renderer.setSize(canvas.clientWidth, canvas.clientHeight);
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.target.set(0,0,0);
controls.update();
// Lights
const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
scene.add(ambientLight);
const dirLight = new THREE.DirectionalLight(0xffffff, 0.8);
dirLight.position.set(50, 50, 50);
scene.add(dirLight);
}
// Create default track
function createDefaultTrack() {
// Simple U shape track
trackPoints = [
new THREE.Vector3(0,0,0),
new THREE.Vector3(10,0,-10),
new THREE.Vector3(20,0,0),
new THREE.Vector3(10,0,10),
new THREE.Vector3(0,0,0)
];
createTrackMesh();
}
// Create track mesh from trackPoints using TubeGeometry
function createTrackMesh() {
if(trackMesh){
scene.remove(trackMesh);
trackMesh.geometry.dispose();
trackMesh.material.dispose();
trackMesh = null;
}
if(trackPoints.length < 2) return;
trackCurve = new THREE.CatmullRomCurve3(trackPoints, true, "catmullrom", 0.5);
const tubeGeometry = new THREE.TubeGeometry(trackCurve, 200, 1.5, 8, true);
const tubeMaterial = new THREE.MeshStandardMaterial({color: 0x555555, side: THREE.DoubleSide});
trackMesh = new THREE.Mesh(tubeGeometry, tubeMaterial);
trackMesh.receiveShadow = true;
scene.add(trackMesh);
}
// Initialize marbles
function initMarbles() {
// Clean old marbles
for(const m of marbleMeshes){
scene.remove(m);
if(m.geometry) m.geometry.dispose();
if(m.material) m.material.dispose();
}
marbleMeshes = [];
marbles = [];
raceProgress = [];
// Create marble material (using uploaded texture if available)
marbleMaterial = new THREE.MeshStandardMaterial({color: 0xffffff});
if(marbleTextures.length > 0){
marbleMaterial.map = marbleTextures[0]; // use first texture for all marbles for simplicity
marbleMaterial.needsUpdate = true;
}
for(let i=0; i < NUM_MARBLES; i++){
const geometry = new THREE.SphereGeometry(1, 32, 32);
const mat = marbleMaterial.clone();
// Give different marble colors if no textures
if(!marbleMaterial.map){
mat.color = new THREE.Color().setHSL(i/NUM_MARBLES, 0.7, 0.5);
}
const mesh = new THREE.Mesh(geometry, mat);
mesh.castShadow = true;
marbles.push({mesh, progress: 0});
marbleMeshes.push(mesh);
scene.add(mesh);
raceProgress.push(0);
}
resetMarblesPosition();
}
// Reset marbles position at start of track
function resetMarblesPosition(){
for(let i=0; i < raceProgress.length; i++){
raceProgress[i] = 0;
updateMarblePosition(i);
}
}
// Update marble position based on progress (0..1)
function updateMarblePosition(index){
if(!trackCurve) return;
const t = raceProgress[index];
const position = trackCurve.getPointAt(t);
const tangent = trackCurve.getTangentAt(t);
marbles[index].mesh.position.copy(position);
// Orient marble along tangent
const axis = new THREE.Vector3(0,1,0);
marbles[index].mesh.quaternion.setFromUnitVectors(axis, tangent.clone().normalize());
}
// Animate race
function animateRace() {
animationId = requestAnimationFrame(animateRace);
// Move each marble forward, random speed for simple simulation
for(let i=0; i < raceProgress.length; i++){
if(raceProgress[i] < 1){
raceProgress[i] += 0.001 + 0.001 * Math.random();
if(raceProgress[i] > 1) raceProgress[i] = 1;
updateMarblePosition(i);
}
}
controls.update();
renderer.render(scene, camera);
}
// Stop animation loop
function stopAnimation(){
if(animationId !== null){
cancelAnimationFrame(animationId);
animationId = null;
}
}
// Handle marble image upload
marbleImageUpload.addEventListener('change', (e) => {
const file = e.target.files[0];
if(!file) return;
const reader = new FileReader();
reader.onload = function(event){
const imgURL = event.target.result;
// Update preview div
marblePreview.style.backgroundImage = `url('${imgURL}')`;
// Load texture for marble
const loader = new THREE.TextureLoader();
loader.load(imgURL, texture => {
marbleTextures[0] = texture;
initMarbles();
});
}
reader.readAsDataURL(file);
});
// Handle New Game
btnNewGame.addEventListener('click', () => {
NUM_MARBLES = parseInt(marbleCountSelect.value);
if(trackPoints.length < 2) createDefaultTrack();
initMarbles();
resetMarblesPosition();
stopAnimation();
animateRace();
});
// Handle Save Game
btnSaveGame.addEventListener('click', () => {
const state = {
raceProgress,
trackPoints: trackPoints.map(p => ({x:p.x,y:p.y,z:p.z})),
numMarbles: NUM_MARBLES,
// Save marble texture image if present
marbleTextureData: marbleTextures[0]?.image?.src || null,
// you could save more data here...
};
localStorage.setItem(GAME_STATE_KEY, JSON.stringify(state));
alert("Game saved!");
});
// Handle Load Game
btnLoadGame.addEventListener('click', () => {
const stateString = localStorage.getItem(GAME_STATE_KEY);
if(!stateString){
alert("No saved game found.");
return;
}
const state = JSON.parse(stateString);
NUM_MARBLES = state.numMarbles || 3;
marbleCountSelect.value = NUM_MARBLES;
// Restore track points
if(state.trackPoints && state.trackPoints.length > 1){
trackPoints = state.trackPoints.map(p => new THREE.Vector3(p.x,p.y,p.z));
createTrackMesh();
trackDataTextarea.value = JSON.stringify(state.trackPoints, null, 2);
} else {
createDefaultTrack();
}
// Restore marble texture
if(state.marbleTextureData){
const loader = new THREE.TextureLoader();
loader.load(state.marbleTextureData, texture => {
marbleTextures[0] = texture;
initMarbles();
raceProgress = state.raceProgress;
resetMarblesPosition();
stopAnimation();
animateRace();
});
} else {
marbleTextures = [];
initMarbles();
raceProgress = state.raceProgress;
resetMarblesPosition();
stopAnimation();
animateRace();
}
alert("Game loaded!");
});
// Handle Marble Count Change
marbleCountSelect.addEventListener('change', () => {
NUM_MARBLES = parseInt(marbleCountSelect.value);
});
// Handle Track creation from text area
btnCreateTrack.addEventListener('click', () => {
try {
const pointsArr = JSON.parse(trackDataTextarea.value);
if(!Array.isArray(pointsArr) || pointsArr.length < 2){
alert('Please enter a valid array of at least 2 points.');
return;
}
// Validate points
trackPoints = pointsArr.map(p => new THREE.Vector3(p.x || 0, p.y || 0, p.z || 0));
createTrackMesh();
alert("Track created!");
} catch(ex){
alert("Invalid JSON format for track points.");
}
});
// Responsive resize
function onWindowResize() {
camera.aspect = canvas.clientWidth / canvas.clientHeight;
camera.updateProjectionMatrix();
renderer.setSize(canvas.clientWidth, canvas.clientHeight);
}
window.addEventListener('resize', onWindowResize);
// Initial setup
function init(){
initThree();
createDefaultTrack();
initMarbles();
animateRace();
}
init();
})();
</script>
</body>
</html>
Explanation
This is a fully self-contained HTML file implementing a simple 3D Marble Race Game with the requested features:
3D Rendering: Uses Three.js with OrbitControls for user camera control.
Custom Tracks: You can enter a custom track as JSON list of 3D points in the "Track Editor" section; pressing "Create Track" builds the track mesh (a tube).
Marble Customization: You can upload an image to texture marbles. The first uploaded texture is applied to all marbles for simplicity.
New Game: Starts a new race with the selected number of marbles.
Save Game / Load Game: Saves and loads game state (marble progress, number of marbles, track points, custom marble texture) to/from localStorage.
Marble Count Selection: You can select 2-5 marbles on track.
Race Animation: Marbles automatically "race" around the track with randomized speeds.
UI: Controls are presented on a sidebar with instructions, file upload, and buttons.
How to Use
Change marble count from dropdown.
Upload a texture image to customize marble appearance.
Modify/customize the track JSON in the textarea and hit "Create Track".
Click "New Game" to start the race.
Use "Save Game" to remember the game's current state.
Use "Load Game" to restore it.
Navigate the 3D scene using mouse drag/click (OrbitControls).
This starter implementation can be extended with physics, better race logic, multiple marble textures, GUI improvements, or persistent backend storage depending on your needs.