Spaces:
Running
Running
<html> | |
<head> | |
<title>Three.js Infinite World</title> | |
<style> | |
body { margin: 0; overflow: hidden; } | |
canvas { display: block; } | |
.info-panel { | |
position: absolute; bottom: 10px; left: 10px; | |
background: rgba(0,0,0,0.6); color: white; | |
padding: 10px; border-radius: 5px; | |
font-family: Arial, sans-serif; font-size: 14px; | |
pointer-events: none; | |
} | |
</style> | |
<script> | |
function pollGameState() { | |
console.log("Polling updated game state:", window.GAME_STATE); | |
} | |
setInterval(pollGameState, 5000); | |
</script> | |
</head> | |
<body> | |
<div class="info-panel">WASD or Arrow Keys to move | Click to place objects</div> | |
<script type="importmap"> | |
{ | |
"imports": { | |
"three": "https://unpkg.com/[email protected]/build/three.module.js", | |
"three/addons/": "https://unpkg.com/[email protected]/examples/jsm/" | |
} | |
} | |
</script> | |
<script type="module"> | |
import * as THREE from 'three'; | |
let scene, camera, renderer, playerMesh; | |
let raycaster, mouse; | |
const keysPressed = {}; | |
const playerSpeed = 0.15; | |
let newlyPlacedObjects = []; | |
const placeholderPlots = new Set(); | |
const groundMeshes = {}; | |
const SESSION_STORAGE_KEY = 'unsavedInfiniteWorldState'; | |
const allInitialObjects = window.ALL_INITIAL_OBJECTS || []; | |
const plotsMetadata = window.PLOTS_METADATA || []; | |
const selectedObjectType = window.SELECTED_OBJECT_TYPE || "None"; | |
const customScale = window.CUSTOM_SCALE || 1.0; | |
const customRotationY = window.CUSTOM_ROTATION_Y || 0; | |
const plotWidth = window.PLOT_WIDTH || 50.0; | |
const plotDepth = window.PLOT_DEPTH || 50.0; | |
const materials = { | |
ground: new THREE.MeshStandardMaterial({color: 0x55aa55, roughness: 0.9, metalness: 0.1, side: THREE.DoubleSide}), | |
placeholderGround: new THREE.MeshStandardMaterial({color: 0x448844, roughness: 0.95, metalness: 0.1, side: THREE.DoubleSide}), | |
wood: new THREE.MeshStandardMaterial({color: 0x8B4513, roughness: 0.9, metalness: 0.1}), | |
darkWood: new THREE.MeshStandardMaterial({color: 0x5D2906, roughness: 0.85, metalness: 0.15}), | |
redBrick: new THREE.MeshStandardMaterial({color: 0xA03C28, roughness: 0.9, metalness: 0.05}), | |
metal: new THREE.MeshStandardMaterial({color: 0x888888, roughness: 0.4, metalness: 0.8}), | |
rustedMetal: new THREE.MeshStandardMaterial({color: 0x964B00, roughness: 0.7, metalness: 0.6}), | |
glass: new THREE.MeshStandardMaterial({color: 0x88CCFF, roughness: 0.1, metalness: 0.9, transparent: true, opacity: 0.4}), | |
concrete: new THREE.MeshStandardMaterial({color: 0x888888, roughness: 0.9, metalness: 0.1}), | |
damagedConcrete: new THREE.MeshStandardMaterial({color: 0x777777, roughness: 0.95, metalness: 0.05}), | |
darkGreen: new THREE.MeshStandardMaterial({color: 0x225522, roughness: 0.8, metalness: 0.1}), | |
skin: new THREE.MeshStandardMaterial({color: 0xE0AC69, roughness: 0.7, metalness: 0.1}), | |
cloth: new THREE.MeshStandardMaterial({color: 0xCCAA88, roughness: 0.9, metalness: 0.0}), | |
neonGlow: new THREE.MeshStandardMaterial({color: 0x00FFFF, roughness: 0.2, metalness: 0.8, emissive: 0x00FFFF, emissiveIntensity: 1.0}), | |
goldMetal: new THREE.MeshStandardMaterial({color: 0xFFD700, roughness: 0.3, metalness: 1.0}), | |
leather: new THREE.MeshStandardMaterial({color: 0x8B4513, roughness: 0.9, metalness: 0.1}), | |
stone: new THREE.MeshStandardMaterial({color: 0x777777, roughness: 0.9, metalness: 0.1}), | |
bone: new THREE.MeshStandardMaterial({color: 0xE3DAC9, roughness: 0.7, metalness: 0.1}), | |
zombie: new THREE.MeshStandardMaterial({color: 0x7D9F85, roughness: 0.8, metalness: 0.1}), | |
lava: new THREE.MeshStandardMaterial({color: 0xFF4500, roughness: 0.7, metalness: 0.3, emissive: 0xFF4500, emissiveIntensity: 0.8}), | |
crystal: new THREE.MeshStandardMaterial({color: 0x88CCFF, roughness: 0.1, metalness: 0.9, transparent: true, opacity: 0.7}), | |
energyBeam: new THREE.MeshStandardMaterial({color: 0x88FFFF, roughness: 0.1, metalness: 0.5, emissive: 0x88FFFF, emissiveIntensity: 1.0, transparent: true, opacity: 0.7}) | |
}; | |
function init() { | |
scene = new THREE.Scene(); | |
scene.background = new THREE.Color(0xabcdef); | |
const aspect = window.innerWidth / window.innerHeight; | |
camera = new THREE.PerspectiveCamera(60, aspect, 0.1, 4000); | |
camera.position.set(0, 15, 20); | |
camera.lookAt(0, 0, 0); | |
scene.add(camera); | |
setupLighting(); | |
setupInitialGround(); | |
setupPlayer(); | |
raycaster = new THREE.Raycaster(); | |
mouse = new THREE.Vector2(); | |
renderer = new THREE.WebGLRenderer({ antialias: true }); | |
renderer.setSize(window.innerWidth, window.innerHeight); | |
renderer.shadowMap.enabled = true; | |
renderer.shadowMap.type = THREE.PCFSoftShadowMap; | |
document.body.appendChild(renderer.domElement); | |
loadInitialObjects(); | |
restoreUnsavedState(); | |
document.addEventListener('mousemove', onMouseMove, false); | |
document.addEventListener('click', onDocumentClick, false); | |
window.addEventListener('resize', onWindowResize, false); | |
document.addEventListener('keydown', onKeyDown); | |
document.addEventListener('keyup', onKeyUp); | |
window.teleportPlayer = teleportPlayer; | |
window.getSaveDataAndPosition = getSaveDataAndPosition; | |
window.getSaveDataForNamedPlot = getSaveDataForNamedPlot; | |
window.resetNewlyPlacedObjects = resetNewlyPlacedObjects; | |
console.log("Three.js Initialized. World ready."); | |
animate(); | |
} | |
function setupLighting() { | |
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5); | |
scene.add(ambientLight); | |
const directionalLight = new THREE.DirectionalLight(0xffffff, 1.0); | |
directionalLight.position.set(50, 150, 100); | |
directionalLight.castShadow = true; | |
directionalLight.shadow.mapSize.width = 4096; | |
directionalLight.shadow.mapSize.height = 4096; | |
directionalLight.shadow.camera.near = 0.5; | |
directionalLight.shadow.camera.far = 500; | |
directionalLight.shadow.camera.left = -150; | |
directionalLight.shadow.camera.right = 150; | |
directionalLight.shadow.camera.top = 150; | |
directionalLight.shadow.camera.bottom = -150; | |
directionalLight.shadow.bias = -0.001; | |
scene.add(directionalLight); | |
} | |
function setupInitialGround() { | |
console.log(`Setting up initial ground for ${plotsMetadata.length} plots.`); | |
plotsMetadata.forEach(plot => { | |
createGroundPlane(plot.grid_x, plot.grid_z, false); | |
}); | |
if (plotsMetadata.length === 0) { | |
createGroundPlane(0, 0, false); | |
} | |
} | |
function createGroundPlane(gridX, gridZ, isPlaceholder) { | |
const gridKey = `${gridX}_${gridZ}`; | |
if (groundMeshes[gridKey]) return; | |
console.log(`Creating ${isPlaceholder ? 'placeholder' : 'initial'} ground at ${gridX}, ${gridZ}`); | |
const groundGeometry = new THREE.PlaneGeometry(plotWidth, plotDepth); | |
const material = isPlaceholder ? materials.placeholderGround : materials.ground; | |
const groundMesh = new THREE.Mesh(groundGeometry, material); | |
groundMesh.rotation.x = -Math.PI / 2; | |
groundMesh.position.y = -0.05; | |
groundMesh.position.x = gridX * plotWidth + plotWidth / 2.0; | |
groundMesh.position.z = gridZ * plotDepth + plotDepth / 2.0; | |
groundMesh.receiveShadow = true; | |
groundMesh.userData.gridKey = gridKey; | |
scene.add(groundMesh); | |
groundMeshes[gridKey] = groundMesh; | |
if (isPlaceholder) placeholderPlots.add(gridKey); | |
} | |
function setupPlayer() { | |
const playerGeo = new THREE.CapsuleGeometry(0.4, 0.8, 4, 8); | |
const playerMat = new THREE.MeshStandardMaterial({ color: 0x0000ff, roughness: 0.6 }); | |
playerMesh = new THREE.Mesh(playerGeo, playerMat); | |
playerMesh.position.set(plotWidth / 2, 0.4 + 0.8/2, plotDepth/2); | |
playerMesh.castShadow = true; | |
playerMesh.receiveShadow = true; | |
scene.add(playerMesh); | |
} | |
function loadInitialObjects() { | |
console.log(`Loading ${allInitialObjects.length} initial objects from Python.`); | |
allInitialObjects.forEach(objData => { createAndPlaceObject(objData, false); }); | |
console.log("Finished loading initial objects."); | |
} | |
function saveUnsavedState() { | |
try { | |
const stateToSave = newlyPlacedObjects.map(obj => ({ | |
obj_id: obj.userData.obj_id, | |
type: obj.userData.type, | |
position: { x: obj.position.x, y: obj.position.y, z: obj.position.z }, | |
rotation: { _x: obj.rotation.x, _y: obj.rotation.y, _z: obj.rotation.z, _order: obj.rotation.order } | |
})); | |
sessionStorage.setItem(SESSION_STORAGE_KEY, JSON.stringify(stateToSave)); | |
console.log(`Saved ${stateToSave.length} unsaved objects to sessionStorage.`); | |
} catch (e) { | |
console.error("Error saving state to sessionStorage:", e); | |
} | |
} | |
function restoreUnsavedState() { | |
try { | |
const savedState = sessionStorage.getItem(SESSION_STORAGE_KEY); | |
if (savedState) { | |
console.log("Found unsaved state in sessionStorage. Restoring..."); | |
const objectsToRestore = JSON.parse(savedState); | |
if (Array.isArray(objectsToRestore)) { | |
newlyPlacedObjects = []; | |
let count = 0; | |
objectsToRestore.forEach(objData => { | |
if(createAndPlaceObject(objData, true)) { count++; } | |
}); | |
console.log(`Restored ${count} objects.`); | |
} | |
} else { | |
console.log("No unsaved state found in sessionStorage."); | |
} | |
} catch (e) { | |
console.error("Error restoring state from sessionStorage:", e); | |
sessionStorage.removeItem(SESSION_STORAGE_KEY); | |
} | |
} | |
function clearUnsavedState() { | |
sessionStorage.removeItem(SESSION_STORAGE_KEY); | |
newlyPlacedObjects = []; | |
console.log("Cleared unsaved state from memory and sessionStorage."); | |
} | |
function createObjectBase(type) { | |
return { userData: { type: type, obj_id: THREE.MathUtils.generateUUID() } }; | |
} | |
function createAndPlaceObject(objData, isNewObject) { | |
let loadedObject = null; | |
switch (objData.type) { | |
case "Simple House": loadedObject = createSimpleHouse(); break; | |
case "Tree": loadedObject = createTree(); break; | |
case "Rock": loadedObject = createRock(); break; | |
case "Fence Post": loadedObject = createFence động viên Post(); break; | |
case "Cyberpunk Wall Panel": loadedObject = createCyberpunkWallPanel(); break; | |
case "Modular Hab Block": loadedObject = createModularHabBlock(); break; | |
case "MegaCorp Skyscraper": loadedObject = createMegaCorpSkyscraper(); break; | |
case "Castle Wall Section": loadedObject = createCastleWallSection(); break; | |
case "Wooden Door": loadedObject = createWoodenDoor(); break; | |
case "House Roof Section": loadedObject = createHouseRoofSection(); break; | |
case "Concrete Bunker Wall": loadedObject = createConcreteBunkerWall(); break; | |
case "Damaged House Facade": loadedObject = createDamagedHouseFacade(); break; | |
case "Pine Tree": loadedObject = createPineTree(); break; | |
case "Boulder": loadedObject = createBoulder(); break; | |
case "Alien Plant": loadedObject = createAlienPlant(); break; | |
case "Floating Rock Platform": loadedObject = createFloatingRockPlatform(); break; | |
case "Rubble Pile": loadedObject = createRubblePile(); break; | |
case "Rooftop AC Unit": loadedObject = createRooftopACUnit(); break; | |
case "Holographic Window Display": loadedObject = createHolographicWindowDisplay(); break; | |
case "Jersey Barrier": loadedObject = createJerseyBarrier(); break; | |
case "Oil Drum": loadedObject = createOilDrum(); break; | |
case "Canned Food": loadedObject = createCannedFood(); break; | |
case "Treasure Chest": loadedObject = createTreasureChest(); break; | |
case "Wall Torch": loadedObject = createWallTorch(); break; | |
case "Bone Pile": loadedObject = createBonePile(); break; | |
case "King Figure": loadedObject = createKingFigure(); break; | |
case "Soldier Figure": loadedObject = createSoldierFigure(); break; | |
case "Mage Figure": loadedObject = createMageFigure(); break; | |
case "Zombie Figure": loadedObject = createZombieFigure(); break; | |
case "Survivor Figure": loadedObject = createSurvivorFigure(); break; | |
case "Dwarf Miner Figure": loadedObject = createDwarfMinerFigure(); break; | |
case "Undead Knight Figure": loadedObject = createUndeadKnightFigure(); break; | |
case "Hero Figure": loadedObject = createHeroFigure(); break; | |
case "Wooden Cart": loadedObject = createWoodenCart(); break; | |
case "Ballista": loadedObject = createBallista(); break; | |
case "Siege Tower": loadedObject = createSiegeTower(); break; | |
case "Buggy Frame": loadedObject = createBuggyFrame(); break; | |
case "Motorbike": loadedObject = createMotorbike(); break; | |
case "Hover Bike": loadedObject = createHoverBike(); break; | |
case "APC": loadedObject = createAPC(); break; | |
case "Sand Boat": loadedObject = createSandBoat(); break; | |
case "Makeshift Machete": loadedObject = createMakeshiftMachete(); break; | |
case "Pistol Body": loadedObject = createPistolBody(); break; | |
case "Scope Attachment": loadedObject = createScopeAttachment(); break; | |
case "Laser Pistol": loadedObject = createLaserPistol(); break; | |
case "Energy Sword": loadedObject = createEnergySword(); break; | |
case "Dwarven Axe": loadedObject = createDwarvenAxe(); break; | |
case "Magic Staff": loadedObject = createMagicStaff(); break; | |
case "Candle Flame": loadedObject = createCandleFlame(); break; | |
case "Dust Cloud": loadedObject = createDustCloud(); break; | |
case "Blood Splat Decal": loadedObject = createBloodSplatDecal(); break; | |
case "Burning Barrel Fire": loadedObject = createBurningBarrelFire(); break; | |
case "Warp Tunnel Effect": loadedObject = createWarpTunnelEffect(); break; | |
case "Laser Beam": loadedObject = createLaserBeam(); break; | |
case "Gold Sparkle": loadedObject = createGoldSparkle(); break; | |
case "Steam Vent": loadedObject = createSteamVent(); break; | |
default: console.warn("Unknown object type in data:", objData.type); break; | |
} | |
if (loadedObject) { | |
if (objData.position && objData.position.x !== undefined) { | |
loadedObject.position.set(objData.position.x, objData.position.y, objData.position.z); | |
} else if (objData.pos_x !== undefined) { | |
loadedObject.position.set(objData.pos_x, objData.pos_y, objData.pos_z); | |
} | |
if (objData.rotation) { | |
loadedObject.rotation.set(objData.rotation._x, objData.rotation._y, objData.rotation._z, objData.rotation._order || 'XYZ'); | |
} else if (objData.rot_x !== undefined) { | |
loadedObject.rotation.set(objData.rot_x, objData.rot_y, objData.rot_z, objData.rot_order || 'XYZ'); | |
} | |
if (isNewObject && window.CUSTOM_SCALE && window.CUSTOM_SCALE !== 1.0) { | |
loadedObject.scale.set(window.CUSTOM_SCALE, window.CUSTOM_SCALE, window.CUSTOM_SCALE); | |
} | |
if (isNewObject && window.CUSTOM_ROTATION_Y) { | |
loadedObject.rotation.y = window.CUSTOM_ROTATION_Y; | |
} | |
loadedObject.userData.obj_id = objData.obj_id || loadedObject.userData.obj_id; | |
scene.add(loadedObject); | |
if (isNewObject) newlyPlacedObjects.push(loadedObject); | |
return loadedObject; | |
} | |
return null; | |
} | |
function createSimpleHouse() { | |
const base = createObjectBase("Simple House"); | |
const group = new THREE.Group(); | |
Object.assign(group, base); | |
const mat1 = new THREE.MeshStandardMaterial({color:0xffccaa, roughness:0.8}); | |
const mat2 = new THREE.MeshStandardMaterial({color:0xaa5533, roughness:0.7}); | |
const m1 = new THREE.Mesh(new THREE.BoxGeometry(2,1.5,2.5), mat1); | |
m1.position.y = 1.5/2; | |
m1.castShadow = true; | |
m1.receiveShadow = true; | |
group.add(m1); | |
const m2 = new THREE.Mesh(new THREE.ConeGeometry(1.8,1,4), mat2); | |
m2.position.y = 1.5+1/2; | |
m2.rotation.y = Math.PI/4; | |
m2.castShadow = true; | |
m2.receiveShadow = true; | |
group.add(m2); | |
return group; | |
} | |
function createTree() { | |
const base = createObjectBase("Tree"); | |
const group = new THREE.Group(); | |
Object.assign(group, base); | |
const mat1 = new THREE.MeshStandardMaterial({color:0x8B4513, roughness:0.9}); | |
const mat2 = new THREE.MeshStandardMaterial({color:0x228B22, roughness:0.8}); | |
const m1 = new THREE.Mesh(new THREE.CylinderGeometry(0.3,0.4,2,8), mat1); | |
m1.position.y = 1; | |
m1.castShadow = true; | |
m1.receiveShadow = true; | |
group.add(m1); | |
const m2 = new THREE.Mesh(new THREE.IcosahedronGeometry(1.2,0), mat2); | |
m2.position.y = 2.8; | |
m2.castShadow = true; | |
m2.receiveShadow = true; | |
group.add(m2); | |
return group; | |
} | |
function createRock() { | |
const base = createObjectBase("Rock"); | |
const mat = new THREE.MeshStandardMaterial({color:0xaaaaaa, roughness:0.8, metalness:0.1}); | |
const rock = new THREE.Mesh(new THREE.IcosahedronGeometry(0.7,0), mat); | |
Object.assign(rock, base); | |
rock.position.y = 0.35; | |
rock.rotation.set(Math.random()*Math.PI, Math.random()*Math.PI, 0); | |
rock.castShadow = true; | |
rock.receiveShadow = true; | |
return rock; | |
} | |
function createFencePost() { | |
const base = createObjectBase("Fence Post"); | |
const mat = new THREE.MeshStandardMaterial({color:0xdeb887, roughness:0.9}); | |
const post = new THREE.Mesh(new THREE.BoxGeometry(0.2,1.5,0.2), mat); | |
Object.assign(post, base); | |
post.position.y = 0.75; | |
post.castShadow = true; | |
post.receiveShadow = true; | |
return post; | |
} | |
function createCyberpunkWallPanel() { | |
const base = createObjectBase("Cyberpunk Wall Panel"); | |
const group = new THREE.Group(); | |
Object.assign(group, base); | |
const panelGeo = new THREE.BoxGeometry(3, 4, 0.3); | |
const panel = new THREE.Mesh(panelGeo, materials.metal); | |
panel.castShadow = true; | |
panel.receiveShadow = true; | |
group.add(panel); | |
const trimGeo = new THREE.BoxGeometry(3.2, 0.1, 0.4); | |
const topTrim = new THREE.Mesh(trimGeo, materials.neonGlow); | |
topTrim.position.y = 2; | |
group.add(topTrim); | |
const bottomTrim = new THREE.Mesh(trimGeo, materials.neonGlow); | |
bottomTrim.position.y = -2; | |
group.add(bottomTrim); | |
const detailGeo = new THREE.BoxGeometry(2.5, 0.1, 0.1); | |
for (let i = 0; i < 4; i++) { | |
const detail = new THREE.Mesh(detailGeo, materials.metal); | |
detail.position.y = -1.5 + i; | |
detail.position.z = 0.2; | |
group.add(detail); | |
} | |
group.position.y = 2; | |
return group; | |
} | |
function createModularHabBlock() { | |
const base = createObjectBase("Modular Hab Block"); | |
const group = new THREE.Group(); | |
Object.assign(group, base); | |
const buildingGeo = new THREE.BoxGeometry(5, 5, 5); | |
const building = new THREE.Mesh(buildingGeo, materials.concrete); | |
building.castShadow = true; | |
building.receiveShadow = true; | |
group.add(building); | |
const windowGeo = new THREE.BoxGeometry(1, 1, 0.1); | |
const windowPositions = [ | |
[-1.5, 1.5, 2.51], [0, 1.5, 2.51], [1.5, 1.5, 2.51], | |
[-1.5, 0, 2.51], [0, 0, 2.51], [1.5, 0, 2.51], | |
[-1.5, -1.5, 2.51], [0, -1.5, 2.51], [1.5, -1.5, 2.51] | |
]; | |
windowPositions.forEach(pos => { | |
const window = new THREE.Mesh(windowGeo, materials.glass); | |
window.position.set(...pos); | |
group.add(window); | |
}); | |
const acGeo = new THREE.BoxGeometry(1.5, 0.8, 1.2); | |
const ac = new THREE.Mesh(acGeo, materials.metal); | |
ac.position.set(1.5, 2.9, 1.5); | |
group.add(ac); | |
const roofAccessGeo = new THREE.BoxGeometry(1.2, 1.5, 1.2); | |
const roofAccess = new THREE.Mesh(roofAccessGeo, materials.concrete); | |
roofAccess.position.set(-1.5, 3.25, 1.5); | |
group.add(roofAccess); | |
group.position.y = 2.5; | |
return group; | |
} | |
function createMagicStaff() { | |
const base = createObjectBase("Magic Staff"); | |
const group = new THREE.Group(); | |
Object.assign(group, base); | |
const poleGeo = new THREE.CylinderGeometry(0.05, 0.06, 1.8, 8); | |
const pole = new THREE.Mesh(poleGeo, materials.wood); | |
pole.castShadow = true; | |
pole.receiveShadow = true; | |
group.add(pole); | |
const orbGeo = new THREE.IcosahedronGeometry(0.15, 1); | |
const orbMat = new THREE.MeshStandardMaterial({ | |
color: 0x9900FF, | |
roughness: 0.2, | |
metalness: 0.5, | |
emissive: 0x6600CC, | |
emissiveIntensity: 0.5, | |
transparent: true, | |
opacity: 0.7 | |
}); | |
const orb = new THREE.Mesh(orbGeo, orbMat); | |
orb.position.y = 0.9; | |
orb.castShadow = true; | |
orb.receiveShadow = true; | |
group.add(orb); | |
const numClaws = 4; | |
for (let i = 0; i < numClaws; i++) { | |
const angle = (i / numClaws) * Math.PI * 2; | |
const clawGeo = new THREE.CylinderGeometry(0.02, 0.01, 0.2, 4); | |
const claw = new THREE.Mesh(clawGeo, materials.metal); | |
claw.position.set( | |
Math.cos(angle) * 0.1, | |
0.82, | |
Math.sin(angle) * 0.1 | |
); | |
claw.rotation.x = Math.PI/6; | |
claw.rotation.y = -angle; | |
claw.castShadow = true; | |
claw.receiveShadow = true; | |
group.add(claw); | |
} | |
group.position.y = 0.5; | |
return group; | |
} | |
function createCandleFlame() { | |
const base = createObjectBase("Candle Flame"); | |
const group = new THREE.Group(); | |
Object.assign(group, base); | |
const candleGeo = new THREE.CylinderGeometry(0.1, 0.1, 0.3, 8); | |
const candle = new THREE.Mesh(candleGeo, new THREE.MeshStandardMaterial({ | |
color: 0xF8F8F0, | |
roughness: 0.7 | |
})); | |
candle.castShadow = true; | |
candle.receiveShadow = true; | |
group.add(candle); | |
const flameGeo = new THREE.ConeGeometry(0.07, 0.2, 8); | |
const flameMat = new THREE.MeshStandardMaterial({ | |
color: 0xFF9900, | |
emissive: 0xFF6600, | |
emissiveIntensity: 1.0, | |
transparent: true, | |
opacity: 0.9 | |
}); | |
const flame = new THREE.Mesh(flameGeo, flameMat); | |
flame.position.y = 0.25; | |
group.add(flame); | |
const innerFlameGeo = new THREE.ConeGeometry(0.03, 0.1, 8); | |
const innerFlameMat = new THREE.MeshStandardMaterial({ | |
color: 0xFFFF66, | |
emissive: 0xFFFF00, | |
emissiveIntensity: 1.0, | |
transparent: true, | |
opacity: 0.9 | |
}); | |
const innerFlame = new THREE.Mesh(innerFlameGeo, innerFlameMat); | |
innerFlame.position.y = 0.05; | |
flame.add(innerFlame); | |
const light = new THREE.PointLight(0xFF9933, 0.7, 1.5); | |
light.position.y = 0.3; | |
group.add(light); | |
group.position.y = 0.15; | |
return group; | |
} | |
function createPlaceholderObject(type) { | |
console.log(`Placeholder for ${type}: Implementation pending`); | |
const base = createObjectBase(type); | |
const geometry = new THREE.BoxGeometry(1, 1, 1); | |
const material = new THREE.MeshStandardMaterial({ color: 0xff0000, roughness: 0.8 }); | |
const mesh = new THREE.Mesh(geometry, material); | |
Object.assign(mesh, base); | |
mesh.position.y = 0.5; | |
mesh.castShadow = true; | |
mesh.receiveShadow = true; | |
return mesh; | |
} | |
function createMegaCorpSkyscraper() { return createPlaceholderObject("MegaCorp Skyscraper"); } | |
function createCastleWallSection() { return createPlaceholderObject("Castle Wall Section"); } | |
function createWoodenDoor() { return createPlaceholderObject("Wooden Door"); } | |
function createHouseRoofSection() { return createPlaceholderObject("House Roof Section"); } | |
function createConcreteBunkerWall() { return createPlaceholderObject("Concrete Bunker Wall"); } | |
function createDamagedHouseFacade() { return createPlaceholderObject("Damaged House Facade"); } | |
function createPineTree() { return createPlaceholderObject("Pine Tree"); } | |
function createBoulder() { return createPlaceholderObject("Boulder"); } | |
function createAlienPlant() { return createPlaceholderObject("Alien Plant"); } | |
function createFloatingRockPlatform() { return createPlaceholderObject("Floating Rock Platform"); } | |
function createRubblePile() { return createPlaceholderObject("Rubble Pile"); } | |
function createRooftopACUnit() { return createPlaceholderObject("Rooftop AC Unit"); } | |
function createHolographicWindowDisplay() { return createPlaceholderObject("Holographic Window Display"); } | |
function createJerseyBarrier() { return createPlaceholderObject("Jersey Barrier"); } | |
function createOilDrum() { return createPlaceholderObject("Oil Drum"); } | |
function createCannedFood() { return createPlaceholderObject("Canned Food"); } | |
function createTreasureChest() { return createPlaceholderObject("Treasure Chest"); } | |
function createWallTorch() { return createPlaceholderObject("Wall Torch"); } | |
function createBonePile() { return createPlaceholderObject("Bone Pile"); } | |
function createKingFigure() { return createPlaceholderObject("King Figure"); } | |
function createSoldierFigure() { return createPlaceholderObject("Soldier Figure"); } | |
function createMageFigure() { return createPlaceholderObject("Mage Figure"); } | |
function createZombieFigure() { return createPlaceholderObject("Zombie Figure"); } | |
function createSurvivorFigure() { return createPlaceholderObject("Survivor Figure"); } | |
function createDwarfMinerFigure() { return createPlaceholderObject("Dwarf Miner Figure"); } | |
function createUndeadKnightFigure() { return createPlaceholderObject("Undead Knight Figure"); } | |
function createHeroFigure() { return createPlaceholderObject("Hero Figure"); } | |
function createWoodenCart() { return createPlaceholderObject("Wooden Cart"); } | |
function createBallista() { return createPlaceholderObject("Ballista"); } | |
function createSiegeTower() { return createPlaceholderObject("Siege Tower"); } | |
function createBuggyFrame() { return createPlaceholderObject("Buggy Frame"); } | |
function createMotorbike() { return createPlaceholderObject("Motorbike"); } | |
function createHoverBike() { return createPlaceholderObject("Hover Bike"); } | |
function createAPC() { return createPlaceholderObject("APC"); } | |
function createSandBoat() { return createPlaceholderObject("Sand Boat"); } | |
function createMakeshiftMachete() { return createPlaceholderObject("Makeshift Machete"); } | |
function createPistolBody() { return createPlaceholderObject("Pistol Body"); } | |
function createScopeAttachment() { return createPlaceholderObject("Scope Attachment"); } | |
function createLaserPistol() { return createPlaceholderObject("Laser Pistol"); } | |
function createEnergySword() { return createPlaceholderObject("Energy Sword"); } | |
function createDwarvenAxe() { return createPlaceholderObject("Dwarven Axe"); } | |
function createDustCloud() { return createPlaceholderObject("Dust Cloud"); } | |
function createBloodSplatDecal() { return createPlaceholderObject("Blood Splat Decal"); } | |
function createBurningBarrelFire() { return createPlaceholderObject("Burning Barrel Fire"); } | |
function createWarpTunnelEffect() { return createPlaceholderObject("Warp Tunnel Effect"); } | |
function createLaserBeam() { return createPlaceholderObject("Laser Beam"); } | |
function createGoldSparkle() { return createPlaceholderObject("Gold Sparkle"); } | |
function createSteamVent() { return createPlaceholderObject("Steam Vent"); } | |
function onMouseMove(event) { | |
mouse.x = (event.clientX / window.innerWidth) * 2 - 1; | |
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1; | |
} | |
function onDocumentClick(event) { | |
if (selectedObjectType === "None") return; | |
const groundCandidates = Object.values(groundMeshes); | |
if (groundCandidates.length === 0) return; | |
raycaster.setFromCamera(mouse, camera); | |
const intersects = raycaster.intersectObjects(groundCandidates); | |
if (intersects.length > 0) { | |
const intersectPoint = intersects[0].point; | |
let newObjectToPlace = null; | |
switch (selectedObjectType) { | |
case "Simple House": newObjectToPlace = createSimpleHouse(); break; | |
case "Tree": newObjectToPlace = createTree(); break; | |
case "Rock": newObjectToPlace = createRock(); break; | |
case "Fence Post": newObjectToPlace = createFencePost(); break; | |
case "Cyberpunk Wall Panel": newObjectToPlace = createCyberpunkWallPanel(); break; | |
case "Modular Hab Block": newObjectToPlace = createModularHabBlock(); break; | |
case "MegaCorp Skyscraper": newObjectToPlace = createMegaCorpSkyscraper(); break; | |
case "Castle Wall Section": newObjectToPlace = createCastleWallSection(); break; | |
case "Wooden Door": newObjectToPlace = createWoodenDoor(); break; | |
case "House Roof Section": newObjectToPlace = createHouseRoofSection(); break; | |
case "Concrete Bunker Wall": newObjectToPlace = createConcreteBunkerWall(); break; | |
case "Damaged House Facade": newObjectToPlace = createDamagedHouseFacade(); break; | |
case "Pine Tree": newObjectToPlace = createPineTree(); break; | |
case "Boulder": newObjectToPlace = createBoulder(); break; | |
case "Alien Plant": newObjectToPlace = createAlienPlant(); break; | |
case "Floating Rock Platform": newObjectToPlace = createFloatingRockPlatform(); break; | |
case "Rubble Pile": newObjectToPlace = createRubblePile(); break; | |
case "Rooftop AC Unit": newObjectToPlace = createRooftopACUnit(); break; | |
case "Holographic Window Display": newObjectხ4>0.5, 0.5)).toFixed(2); | |
const newObjectToPlace = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1), new THREE.MeshStandardMaterial({color: 0xff0000})); | |
newObjectToPlace.position.copy(intersectPoint); | |
newObjectToPlace.position.y += 0.01; | |
if (window.CUSTOM_SCALE && window.CUSTOM_SCALE !== 1.0) { | |
newObjectToPlace.scale.set(window.CUSTOM_SCALE, window.CUSTOM_SCALE, window.CUSTOM_SCALE); | |
} | |
if (window.CUSTOM_ROTATION_Y) { | |
newObjectToPlace.rotation.y = window.CUSTOM_ROTATION_Y; | |
} | |
scene.add(newObjectToPlace); | |
newlyPlacedObjects.push(newObjectToPlace); | |
saveUnsavedState(); | |
console.log(`Placed new ${selectedObjectType}. Total unsaved: ${newlyPlacedObjects.length}`); | |
} | |
} | |
} | |
function onKeyDown(event) { | |
keysPressed[event.code] = true; | |
} | |
function onKeyUp(event) { | |
keysPressed[event.code] = false; | |
} | |
function teleportPlayer(targetX, targetZ) { | |
console.log(`JS teleportPlayer called with targetX: ${targetX}, targetZ: ${targetZ}`); | |
if (playerMesh) { | |
playerMesh.position.x = targetX; | |
playerMesh.position.z = targetZ; | |
const offset = new THREE.Vector3(0, 15, 20); | |
const targetPosition = playerMesh.position.clone().add(offset); | |
camera.position.copy(targetPosition); | |
camera.lookAt(playerMesh.position); | |
console.log("Player teleported to:", playerMesh.position); | |
} else { | |
console.error("Player mesh not found for teleport."); | |
} | |
} | |
function getSaveDataAndPosition() { | |
console.log(`JS getSaveDataAndPosition called. Found ${newlyPlacedObjects.length} new objects.`); | |
const objectsToSave = newlyPlacedObjects.map(obj => { | |
if (!obj.userData || !obj.userData.type) { return null; } | |
const rotation = { _x: obj.rotation.x, _y: obj.rotation.y, _z: obj.rotation.z, _order: obj.rotation.order }; | |
return { | |
obj_id: obj.userData.obj_id, type: obj.userData.type, | |
position: { x: obj.position.x, y: obj.position.y, z: obj.position.z }, | |
rotation: rotation | |
}; | |
}).filter(obj => obj !== null); | |
const playerPos = playerMesh ? { x: playerMesh.position.x, y: playerMesh.position.y, z: playerMesh.position.z } : {x:0, y:0, z:0}; | |
const payload = { | |
playerPosition: playerPos, | |
objectsToSave: objectsToSave | |
}; | |
console.log("Prepared payload for saving:", payload); | |
return JSON.stringify(payload); | |
} | |
function getSaveDataForNamedPlot() { | |
return getSaveDataAndPosition(); | |
} | |
function resetNewlyPlacedObjects() { | |
console.log(`JS resetNewlyPlacedObjects called.`); | |
clearUnsavedState(); | |
} | |
function updatePlayerMovement() { | |
if (!playerMesh) return; | |
const moveDirection = new THREE.Vector3(0, 0, 0); | |
if (keysPressed['KeyW'] || keysPressed['ArrowUp']) moveDirection.z -= 1; | |
if (keysPressed['KeyS'] || keysPressed['ArrowDown']) moveDirection.z += 1; | |
if (keysPressed['KeyA'] || keysPressed['ArrowLeft']) moveDirection.x -= 1; | |
if (keysPressed['KeyD'] || keysPressed['ArrowRight']) moveDirection.x += 1; | |
if (moveDirection.lengthSq() > 0) { | |
moveDirection.normalize().multiplyScalar(playerSpeed); | |
const forward = new THREE.Vector3(); | |
camera.getWorldDirection(forward); | |
forward.y = 0; | |
forward.normalize(); | |
const right = new THREE.Vector3().crossVectors(camera.up, forward).normalize(); | |
const worldMove = new THREE.Vector3(); | |
worldMove.add(forward.multiplyScalar(-moveDirection.z)); | |
worldMove.add(right.multiplyScalar(-moveDirection.x)); | |
worldMove.normalize().multiplyScalar(playerSpeed); | |
playerMesh.position.add(worldMove); | |
playerMesh.position.y = Math.max(playerMesh.position.y, 0.4 + 0.8/2); | |
checkAndExpandGround(); | |
} | |
} | |
function checkAndExpandGround() { | |
if (!playerMesh) return; | |
const currentGridX = Math.floor(playerMesh.position.x / plotWidth); | |
const currentGridZ = Math.floor(playerMesh.position.z / plotDepth); | |
for (let dx = -1; dx <= 1; dx++) { | |
for (let dz = -1; dz <= 1; dz++) { | |
if (dx === 0 && dz === 0) continue; | |
const checkX = currentGridX + dx; | |
const checkZ = currentGridZ + dz; | |
const gridKey = `${checkX}_${checkZ}`; | |
if (!groundMeshes[gridKey]) { | |
const isSavedPlot = plotsMetadata.some(plot => plot.grid_x === checkX && plot.grid_z === checkZ); | |
if (!isSavedPlot) { | |
createGroundPlane(checkX, checkZ, true); | |
} | |
} | |
} | |
} | |
} | |
function updateCamera() { | |
if (!playerMesh) return; | |
const offset = new THREE.Vector3(0, 15, 20); | |
const targetPosition = playerMesh.position.clone().add(offset); | |
camera.position.lerp(targetPosition, 0.08); | |
camera.lookAt(playerMesh.position); | |
} | |
function onWindowResize() { | |
camera.aspect = window.innerWidth / window.innerHeight; | |
camera.updateProjectionMatrix(); | |
renderer.setSize(window.innerWidth, window.innerHeight); | |
} | |
function animate() { | |
requestAnimationFrame(animate); | |
updatePlayerMovement(); | |
updateCamera(); | |
renderer.render(scene, camera); | |
} | |
init(); | |
</script> | |
</body> | |
</html> |