Spaces:
Sleeping
Sleeping
Update index.html
Browse files- index.html +163 -112
index.html
CHANGED
|
@@ -5,7 +5,7 @@
|
|
| 5 |
<style>
|
| 6 |
body { margin: 0; overflow: hidden; }
|
| 7 |
canvas { display: block; }
|
| 8 |
-
/*
|
| 9 |
</style>
|
| 10 |
</head>
|
| 11 |
<body>
|
|
@@ -27,6 +27,9 @@
|
|
| 27 |
const playerSpeed = 0.15;
|
| 28 |
let newlyPlacedObjects = []; // Track objects added THIS session for saving
|
| 29 |
|
|
|
|
|
|
|
|
|
|
| 30 |
// --- Access State from Streamlit ---
|
| 31 |
const allInitialObjects = window.ALL_INITIAL_OBJECTS || [];
|
| 32 |
const selectedObjectType = window.SELECTED_OBJECT_TYPE || "None";
|
|
@@ -59,12 +62,16 @@
|
|
| 59 |
|
| 60 |
loadInitialObjects(); // Load ALL objects passed from Python
|
| 61 |
|
|
|
|
|
|
|
|
|
|
| 62 |
// Event Listeners
|
| 63 |
document.addEventListener('mousemove', onMouseMove, false);
|
| 64 |
document.addEventListener('click', onDocumentClick, false);
|
| 65 |
window.addEventListener('resize', onWindowResize, false);
|
| 66 |
document.addEventListener('keydown', onKeyDown);
|
| 67 |
document.addEventListener('keyup', onKeyUp);
|
|
|
|
| 68 |
|
| 69 |
// Define global functions needed by Python/streamlit-js-eval
|
| 70 |
window.teleportPlayer = teleportPlayer;
|
|
@@ -92,25 +99,17 @@
|
|
| 92 |
directionalLight.shadow.camera.bottom = -100;
|
| 93 |
directionalLight.shadow.bias = -0.001;
|
| 94 |
scene.add(directionalLight);
|
| 95 |
-
// Optional shadow helper
|
| 96 |
-
// const shadowHelper = new THREE.CameraHelper(directionalLight.shadow.camera);
|
| 97 |
-
// scene.add(shadowHelper);
|
| 98 |
}
|
| 99 |
|
| 100 |
-
function setupGround() {
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
const groundWidth = Math.max(plotWidth, nextPlotXOffset + plotWidth); // At least one plot wide, or cover all loaded + next slot
|
| 104 |
-
const groundDepth = 50; // Keep depth constant for now
|
| 105 |
-
|
| 106 |
const groundGeometry = new THREE.PlaneGeometry(groundWidth, groundDepth);
|
| 107 |
const groundMaterial = new THREE.MeshStandardMaterial({ color: 0x55aa55, roughness: 0.9, metalness: 0.1 });
|
| 108 |
groundMesh = new THREE.Mesh(groundGeometry, groundMaterial);
|
| 109 |
groundMesh.rotation.x = -Math.PI / 2;
|
| 110 |
groundMesh.position.y = -0.05;
|
| 111 |
-
|
| 112 |
-
groundMesh.position.x = (groundWidth / 2.0) - (plotWidth / 2.0) ; // Adjust so x=0 is near the start
|
| 113 |
-
|
| 114 |
groundMesh.receiveShadow = true;
|
| 115 |
scene.add(groundMesh);
|
| 116 |
console.log(`Ground setup with width: ${groundWidth} centered near x=0`);
|
|
@@ -129,44 +128,113 @@
|
|
| 129 |
function loadInitialObjects() {
|
| 130 |
console.log(`Loading ${allInitialObjects.length} initial objects from Python.`);
|
| 131 |
allInitialObjects.forEach(objData => {
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
case "Tree": loadedObject = createTree(); break;
|
| 137 |
-
case "Rock": loadedObject = createRock(); break;
|
| 138 |
-
case "Fence Post": loadedObject = createFencePost(); break;
|
| 139 |
-
// Add other types if needed
|
| 140 |
-
default: console.warn("Unknown object type in loaded data:", objData.type); break;
|
| 141 |
-
}
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
}
|
| 156 |
-
})
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
| 158 |
}
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
// --- Object Creation Functions (MUST add userData.type and obj_id) ---
|
| 161 |
function createObjectBase(type) { // Helper to assign common data
|
| 162 |
const obj = { userData: { type: type, obj_id: THREE.MathUtils.generateUUID() } };
|
| 163 |
return obj;
|
| 164 |
}
|
| 165 |
-
function createSimpleHouse() {
|
| 166 |
-
const base = createObjectBase("Simple House");
|
| 167 |
-
const group = new THREE.Group();
|
| 168 |
-
Object.assign(group, base); // Copy properties like userData
|
| 169 |
-
|
| 170 |
const mainMaterial = new THREE.MeshStandardMaterial({ color: 0xffccaa, roughness: 0.8 });
|
| 171 |
const roofMaterial = new THREE.MeshStandardMaterial({ color: 0xaa5533, roughness: 0.7 });
|
| 172 |
const baseMesh = new THREE.Mesh(new THREE.BoxGeometry(2, 1.5, 2.5), mainMaterial);
|
|
@@ -175,11 +243,8 @@
|
|
| 175 |
roof.position.y = 1.5 + 1 / 2; roof.rotation.y = Math.PI / 4; roof.castShadow = true; roof.receiveShadow = true; group.add(roof);
|
| 176 |
return group;
|
| 177 |
}
|
| 178 |
-
function createTree() {
|
| 179 |
-
const base = createObjectBase("Tree");
|
| 180 |
-
const group = new THREE.Group();
|
| 181 |
-
Object.assign(group, base);
|
| 182 |
-
|
| 183 |
const trunkMaterial = new THREE.MeshStandardMaterial({ color: 0x8B4513, roughness: 0.9 });
|
| 184 |
const leavesMaterial = new THREE.MeshStandardMaterial({ color: 0x228B22, roughness: 0.8 });
|
| 185 |
const trunk = new THREE.Mesh(new THREE.CylinderGeometry(0.3, 0.4, 2, 8), trunkMaterial);
|
|
@@ -188,22 +253,18 @@
|
|
| 188 |
leaves.position.y = 2 + 0.8; leaves.castShadow = true; leaves.receiveShadow = true; group.add(leaves);
|
| 189 |
return group;
|
| 190 |
}
|
| 191 |
-
function createRock() {
|
| 192 |
const base = createObjectBase("Rock");
|
| 193 |
const rockMaterial = new THREE.MeshStandardMaterial({ color: 0xaaaaaa, roughness: 0.8, metalness: 0.1 });
|
| 194 |
-
const rock = new THREE.Mesh(new THREE.IcosahedronGeometry(0.7, 0), rockMaterial);
|
| 195 |
-
Object.assign(rock, base); // Add userData
|
| 196 |
-
|
| 197 |
rock.position.y = 0.7 / 2; rock.rotation.x = Math.random() * Math.PI; rock.rotation.y = Math.random() * Math.PI;
|
| 198 |
rock.castShadow = true; rock.receiveShadow = true;
|
| 199 |
return rock;
|
| 200 |
}
|
| 201 |
-
function createFencePost() {
|
| 202 |
const base = createObjectBase("Fence Post");
|
| 203 |
const postMaterial = new THREE.MeshStandardMaterial({ color: 0xdeb887, roughness: 0.9 });
|
| 204 |
-
const post = new THREE.Mesh(new THREE.BoxGeometry(0.2, 1.5, 0.2), postMaterial);
|
| 205 |
-
Object.assign(post, base); // Add userData
|
| 206 |
-
|
| 207 |
post.position.y = 1.5 / 2; post.castShadow = true; post.receiveShadow = true;
|
| 208 |
return post;
|
| 209 |
}
|
|
@@ -223,26 +284,29 @@
|
|
| 223 |
|
| 224 |
if (intersects.length > 0) {
|
| 225 |
const intersectPoint = intersects[0].point;
|
| 226 |
-
let
|
| 227 |
-
|
| 228 |
-
switch (selectedObjectType) {
|
| 229 |
-
case "Simple House":
|
| 230 |
-
case "Tree":
|
| 231 |
-
case "Rock":
|
| 232 |
-
case "Fence Post":
|
| 233 |
-
default: return;
|
| 234 |
}
|
| 235 |
|
| 236 |
-
if (
|
| 237 |
// Position in WORLD coordinates where clicked
|
| 238 |
-
|
| 239 |
-
// Adjust Y
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
|
|
|
|
|
|
|
|
|
| 246 |
}
|
| 247 |
}
|
| 248 |
}
|
|
@@ -251,77 +315,64 @@
|
|
| 251 |
function onKeyUp(event) { keysPressed[event.code] = false; }
|
| 252 |
|
| 253 |
// --- Functions called by Python via streamlit-js-eval ---
|
| 254 |
-
function teleportPlayer(targetX) {
|
| 255 |
console.log("JS teleportPlayer called with targetX:", targetX);
|
| 256 |
if (playerMesh) {
|
| 257 |
-
|
| 258 |
-
playerMesh.position.
|
| 259 |
-
|
| 260 |
-
// Snap camera instantly - adjust Y height if needed
|
| 261 |
-
const offset = new THREE.Vector3(0, 15, 20); // Reset camera offset
|
| 262 |
const targetPosition = playerMesh.position.clone().add(offset);
|
| 263 |
camera.position.copy(targetPosition);
|
| 264 |
camera.lookAt(playerMesh.position);
|
| 265 |
console.log("Player teleported to:", playerMesh.position);
|
| 266 |
-
} else {
|
| 267 |
-
console.error("Player mesh not found for teleport.");
|
| 268 |
-
}
|
| 269 |
}
|
| 270 |
|
| 271 |
-
function getSaveData() {
|
| 272 |
-
console.log(`JS getSaveData called.
|
|
|
|
| 273 |
const objectsToSave = newlyPlacedObjects.map(obj => {
|
| 274 |
-
if (!obj.userData || !obj.userData.type) {
|
| 275 |
-
|
| 276 |
-
return null; // Skip objects without type/id
|
| 277 |
-
}
|
| 278 |
-
// Send WORLD positions to Python, it will make them relative
|
| 279 |
-
const rotation = {
|
| 280 |
-
_x: obj.rotation.x,
|
| 281 |
-
_y: obj.rotation.y,
|
| 282 |
-
_z: obj.rotation.z,
|
| 283 |
-
_order: obj.rotation.order
|
| 284 |
-
};
|
| 285 |
return {
|
| 286 |
-
obj_id: obj.userData.obj_id,
|
| 287 |
type: obj.userData.type,
|
| 288 |
position: { x: obj.position.x, y: obj.position.y, z: obj.position.z },
|
| 289 |
rotation: rotation
|
| 290 |
};
|
| 291 |
-
}).filter(obj => obj !== null);
|
| 292 |
|
| 293 |
console.log("Prepared data for saving:", objectsToSave);
|
| 294 |
return JSON.stringify(objectsToSave); // Return as JSON string
|
| 295 |
}
|
| 296 |
|
| 297 |
-
function resetNewlyPlacedObjects() {
|
| 298 |
-
console.log(`JS resetNewlyPlacedObjects called
|
| 299 |
-
|
| 300 |
}
|
| 301 |
|
| 302 |
|
| 303 |
// --- Animation Loop ---
|
| 304 |
function updatePlayerMovement() { /* ... unchanged ... */
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
}
|
| 318 |
}
|
| 319 |
|
| 320 |
function updateCamera() { /* ... unchanged ... */
|
| 321 |
if (!playerMesh) return;
|
| 322 |
const offset = new THREE.Vector3(0, 15, 20);
|
| 323 |
const targetPosition = playerMesh.position.clone().add(offset);
|
| 324 |
-
camera.position.lerp(targetPosition, 0.08);
|
| 325 |
camera.lookAt(playerMesh.position);
|
| 326 |
}
|
| 327 |
|
|
|
|
| 5 |
<style>
|
| 6 |
body { margin: 0; overflow: hidden; }
|
| 7 |
canvas { display: block; }
|
| 8 |
+
/* No Save button needed here anymore */
|
| 9 |
</style>
|
| 10 |
</head>
|
| 11 |
<body>
|
|
|
|
| 27 |
const playerSpeed = 0.15;
|
| 28 |
let newlyPlacedObjects = []; // Track objects added THIS session for saving
|
| 29 |
|
| 30 |
+
// --- Session Storage Key ---
|
| 31 |
+
const SESSION_STORAGE_KEY = 'unsavedWorldBuilderState';
|
| 32 |
+
|
| 33 |
// --- Access State from Streamlit ---
|
| 34 |
const allInitialObjects = window.ALL_INITIAL_OBJECTS || [];
|
| 35 |
const selectedObjectType = window.SELECTED_OBJECT_TYPE || "None";
|
|
|
|
| 62 |
|
| 63 |
loadInitialObjects(); // Load ALL objects passed from Python
|
| 64 |
|
| 65 |
+
// *** ADDED: Restore unsaved state from sessionStorage ***
|
| 66 |
+
restoreUnsavedState();
|
| 67 |
+
|
| 68 |
// Event Listeners
|
| 69 |
document.addEventListener('mousemove', onMouseMove, false);
|
| 70 |
document.addEventListener('click', onDocumentClick, false);
|
| 71 |
window.addEventListener('resize', onWindowResize, false);
|
| 72 |
document.addEventListener('keydown', onKeyDown);
|
| 73 |
document.addEventListener('keyup', onKeyUp);
|
| 74 |
+
// No save button listener needed here
|
| 75 |
|
| 76 |
// Define global functions needed by Python/streamlit-js-eval
|
| 77 |
window.teleportPlayer = teleportPlayer;
|
|
|
|
| 99 |
directionalLight.shadow.camera.bottom = -100;
|
| 100 |
directionalLight.shadow.bias = -0.001;
|
| 101 |
scene.add(directionalLight);
|
|
|
|
|
|
|
|
|
|
| 102 |
}
|
| 103 |
|
| 104 |
+
function setupGround() { /* ... unchanged ... */
|
| 105 |
+
const groundWidth = Math.max(plotWidth, nextPlotXOffset + plotWidth);
|
| 106 |
+
const groundDepth = 50;
|
|
|
|
|
|
|
|
|
|
| 107 |
const groundGeometry = new THREE.PlaneGeometry(groundWidth, groundDepth);
|
| 108 |
const groundMaterial = new THREE.MeshStandardMaterial({ color: 0x55aa55, roughness: 0.9, metalness: 0.1 });
|
| 109 |
groundMesh = new THREE.Mesh(groundGeometry, groundMaterial);
|
| 110 |
groundMesh.rotation.x = -Math.PI / 2;
|
| 111 |
groundMesh.position.y = -0.05;
|
| 112 |
+
groundMesh.position.x = (groundWidth / 2.0) - (plotWidth / 2.0) ;
|
|
|
|
|
|
|
| 113 |
groundMesh.receiveShadow = true;
|
| 114 |
scene.add(groundMesh);
|
| 115 |
console.log(`Ground setup with width: ${groundWidth} centered near x=0`);
|
|
|
|
| 128 |
function loadInitialObjects() {
|
| 129 |
console.log(`Loading ${allInitialObjects.length} initial objects from Python.`);
|
| 130 |
allInitialObjects.forEach(objData => {
|
| 131 |
+
createAndPlaceObject(objData, false); // Use helper, false=don't add to newlyPlaced
|
| 132 |
+
});
|
| 133 |
+
console.log("Finished loading initial objects.");
|
| 134 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
+
// --- NEW: Helper to create/place objects from data ---
|
| 137 |
+
function createAndPlaceObject(objData, isNewObject) {
|
| 138 |
+
let loadedObject = null;
|
| 139 |
+
// Need to deserialize object based on 'type' field from CSV/Storage
|
| 140 |
+
switch (objData.type) {
|
| 141 |
+
case "Simple House": loadedObject = createSimpleHouse(); break;
|
| 142 |
+
case "Tree": loadedObject = createTree(); break;
|
| 143 |
+
case "Rock": loadedObject = createRock(); break;
|
| 144 |
+
case "Fence Post": loadedObject = createFencePost(); break;
|
| 145 |
+
default: console.warn("Unknown object type in data:", objData.type); break;
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
if (loadedObject) {
|
| 149 |
+
// Position depends on whether it's from initial load (world) or storage (world)
|
| 150 |
+
// or potentially relative if storage format changes (keep world for now)
|
| 151 |
+
if (objData.position && objData.position.x !== undefined) {
|
| 152 |
+
loadedObject.position.set(objData.position.x, objData.position.y, objData.position.z);
|
| 153 |
+
} else if (objData.pos_x !== undefined) { // Handle CSV loaded format too
|
| 154 |
+
loadedObject.position.set(objData.pos_x, objData.pos_y, objData.pos_z);
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
// Apply rotation
|
| 158 |
+
if (objData.rotation) { // Format from storage/newlyPlaced
|
| 159 |
+
loadedObject.rotation.set(objData.rotation._x, objData.rotation._y, objData.rotation._z, objData.rotation._order || 'XYZ');
|
| 160 |
+
} else if (objData.rot_x !== undefined) { // Format from CSV
|
| 161 |
+
loadedObject.rotation.set(objData.rot_x, objData.rot_y, objData.rot_z, objData.rot_order || 'XYZ');
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
// Assign unique ID if it has one, else use the one generated by createObjectBase
|
| 165 |
+
loadedObject.userData.obj_id = objData.obj_id || loadedObject.userData.obj_id;
|
| 166 |
+
|
| 167 |
+
scene.add(loadedObject);
|
| 168 |
+
|
| 169 |
+
if (isNewObject) {
|
| 170 |
+
// Only add to this array if it's being placed now or restored from session
|
| 171 |
+
newlyPlacedObjects.push(loadedObject);
|
| 172 |
+
}
|
| 173 |
+
return loadedObject; // Return the mesh/group
|
| 174 |
+
}
|
| 175 |
+
return null;
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
// --- NEW: Save/Load/Clear Unsaved State using sessionStorage ---
|
| 180 |
+
function saveUnsavedState() {
|
| 181 |
+
try {
|
| 182 |
+
const stateToSave = newlyPlacedObjects.map(obj => ({
|
| 183 |
+
obj_id: obj.userData.obj_id,
|
| 184 |
+
type: obj.userData.type,
|
| 185 |
+
position: { x: obj.position.x, y: obj.position.y, z: obj.position.z },
|
| 186 |
+
rotation: { _x: obj.rotation.x, _y: obj.rotation.y, _z: obj.rotation.z, _order: obj.rotation.order }
|
| 187 |
+
}));
|
| 188 |
+
sessionStorage.setItem(SESSION_STORAGE_KEY, JSON.stringify(stateToSave));
|
| 189 |
+
console.log(`Saved ${stateToSave.length} unsaved objects to sessionStorage.`);
|
| 190 |
+
} catch (e) {
|
| 191 |
+
console.error("Error saving state to sessionStorage:", e);
|
| 192 |
+
}
|
| 193 |
+
}
|
| 194 |
|
| 195 |
+
function restoreUnsavedState() {
|
| 196 |
+
try {
|
| 197 |
+
const savedState = sessionStorage.getItem(SESSION_STORAGE_KEY);
|
| 198 |
+
if (savedState) {
|
| 199 |
+
console.log("Found unsaved state in sessionStorage. Restoring...");
|
| 200 |
+
const objectsToRestore = JSON.parse(savedState);
|
| 201 |
+
if (Array.isArray(objectsToRestore)) {
|
| 202 |
+
// Clear existing newlyPlaced array before restoring
|
| 203 |
+
newlyPlacedObjects = [];
|
| 204 |
+
let count = 0;
|
| 205 |
+
objectsToRestore.forEach(objData => {
|
| 206 |
+
// Create object, add to scene, AND add to newlyPlacedObjects array
|
| 207 |
+
if(createAndPlaceObject(objData, true)) {
|
| 208 |
+
count++;
|
| 209 |
+
}
|
| 210 |
+
});
|
| 211 |
+
console.log(`Restored ${count} objects.`);
|
| 212 |
+
}
|
| 213 |
+
} else {
|
| 214 |
+
console.log("No unsaved state found in sessionStorage.");
|
| 215 |
}
|
| 216 |
+
} catch (e) {
|
| 217 |
+
console.error("Error restoring state from sessionStorage:", e);
|
| 218 |
+
// Clear potentially corrupted storage
|
| 219 |
+
sessionStorage.removeItem(SESSION_STORAGE_KEY);
|
| 220 |
+
}
|
| 221 |
}
|
| 222 |
|
| 223 |
+
function clearUnsavedState() {
|
| 224 |
+
sessionStorage.removeItem(SESSION_STORAGE_KEY);
|
| 225 |
+
newlyPlacedObjects = []; // Clear the array in memory too
|
| 226 |
+
console.log("Cleared unsaved state from memory and sessionStorage.");
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
|
| 230 |
// --- Object Creation Functions (MUST add userData.type and obj_id) ---
|
| 231 |
function createObjectBase(type) { // Helper to assign common data
|
| 232 |
const obj = { userData: { type: type, obj_id: THREE.MathUtils.generateUUID() } };
|
| 233 |
return obj;
|
| 234 |
}
|
| 235 |
+
function createSimpleHouse() { /* ... unchanged, uses createObjectBase ... */
|
| 236 |
+
const base = createObjectBase("Simple House");
|
| 237 |
+
const group = new THREE.Group(); Object.assign(group, base);
|
|
|
|
|
|
|
| 238 |
const mainMaterial = new THREE.MeshStandardMaterial({ color: 0xffccaa, roughness: 0.8 });
|
| 239 |
const roofMaterial = new THREE.MeshStandardMaterial({ color: 0xaa5533, roughness: 0.7 });
|
| 240 |
const baseMesh = new THREE.Mesh(new THREE.BoxGeometry(2, 1.5, 2.5), mainMaterial);
|
|
|
|
| 243 |
roof.position.y = 1.5 + 1 / 2; roof.rotation.y = Math.PI / 4; roof.castShadow = true; roof.receiveShadow = true; group.add(roof);
|
| 244 |
return group;
|
| 245 |
}
|
| 246 |
+
function createTree() { /* ... unchanged, uses createObjectBase ... */
|
| 247 |
+
const base = createObjectBase("Tree"); const group = new THREE.Group(); Object.assign(group, base);
|
|
|
|
|
|
|
|
|
|
| 248 |
const trunkMaterial = new THREE.MeshStandardMaterial({ color: 0x8B4513, roughness: 0.9 });
|
| 249 |
const leavesMaterial = new THREE.MeshStandardMaterial({ color: 0x228B22, roughness: 0.8 });
|
| 250 |
const trunk = new THREE.Mesh(new THREE.CylinderGeometry(0.3, 0.4, 2, 8), trunkMaterial);
|
|
|
|
| 253 |
leaves.position.y = 2 + 0.8; leaves.castShadow = true; leaves.receiveShadow = true; group.add(leaves);
|
| 254 |
return group;
|
| 255 |
}
|
| 256 |
+
function createRock() { /* ... unchanged, uses createObjectBase ... */
|
| 257 |
const base = createObjectBase("Rock");
|
| 258 |
const rockMaterial = new THREE.MeshStandardMaterial({ color: 0xaaaaaa, roughness: 0.8, metalness: 0.1 });
|
| 259 |
+
const rock = new THREE.Mesh(new THREE.IcosahedronGeometry(0.7, 0), rockMaterial); Object.assign(rock, base);
|
|
|
|
|
|
|
| 260 |
rock.position.y = 0.7 / 2; rock.rotation.x = Math.random() * Math.PI; rock.rotation.y = Math.random() * Math.PI;
|
| 261 |
rock.castShadow = true; rock.receiveShadow = true;
|
| 262 |
return rock;
|
| 263 |
}
|
| 264 |
+
function createFencePost() { /* ... unchanged, uses createObjectBase ... */
|
| 265 |
const base = createObjectBase("Fence Post");
|
| 266 |
const postMaterial = new THREE.MeshStandardMaterial({ color: 0xdeb887, roughness: 0.9 });
|
| 267 |
+
const post = new THREE.Mesh(new THREE.BoxGeometry(0.2, 1.5, 0.2), postMaterial); Object.assign(post, base);
|
|
|
|
|
|
|
| 268 |
post.position.y = 1.5 / 2; post.castShadow = true; post.receiveShadow = true;
|
| 269 |
return post;
|
| 270 |
}
|
|
|
|
| 284 |
|
| 285 |
if (intersects.length > 0) {
|
| 286 |
const intersectPoint = intersects[0].point;
|
| 287 |
+
let newObjectToPlace = null; // Use a different name to avoid confusion
|
| 288 |
+
|
| 289 |
+
switch (selectedObjectType) {
|
| 290 |
+
case "Simple House": newObjectToPlace = createSimpleHouse(); break;
|
| 291 |
+
case "Tree": newObjectToPlace = createTree(); break;
|
| 292 |
+
case "Rock": newObjectToPlace = createRock(); break;
|
| 293 |
+
case "Fence Post": newObjectToPlace = createFencePost(); break;
|
| 294 |
+
default: return;
|
| 295 |
}
|
| 296 |
|
| 297 |
+
if (newObjectToPlace) {
|
| 298 |
// Position in WORLD coordinates where clicked
|
| 299 |
+
newObjectToPlace.position.copy(intersectPoint);
|
| 300 |
+
// Adjust Y based on object's geometry center if needed
|
| 301 |
+
// (Create functions mostly handle placing base at y=0)
|
| 302 |
+
|
| 303 |
+
scene.add(newObjectToPlace);
|
| 304 |
+
newlyPlacedObjects.push(newObjectToPlace); // Add to list for saving
|
| 305 |
+
|
| 306 |
+
// *** ADDED: Save state to sessionStorage immediately after placing ***
|
| 307 |
+
saveUnsavedState();
|
| 308 |
+
|
| 309 |
+
console.log(`Placed new ${selectedObjectType}. Total unsaved: ${newlyPlacedObjects.length}`);
|
| 310 |
}
|
| 311 |
}
|
| 312 |
}
|
|
|
|
| 315 |
function onKeyUp(event) { keysPressed[event.code] = false; }
|
| 316 |
|
| 317 |
// --- Functions called by Python via streamlit-js-eval ---
|
| 318 |
+
function teleportPlayer(targetX) { /* ... unchanged ... */
|
| 319 |
console.log("JS teleportPlayer called with targetX:", targetX);
|
| 320 |
if (playerMesh) {
|
| 321 |
+
playerMesh.position.x = targetX + 2.0;
|
| 322 |
+
playerMesh.position.z = 5.0;
|
| 323 |
+
const offset = new THREE.Vector3(0, 15, 20);
|
|
|
|
|
|
|
| 324 |
const targetPosition = playerMesh.position.clone().add(offset);
|
| 325 |
camera.position.copy(targetPosition);
|
| 326 |
camera.lookAt(playerMesh.position);
|
| 327 |
console.log("Player teleported to:", playerMesh.position);
|
| 328 |
+
} else { console.error("Player mesh not found for teleport."); }
|
|
|
|
|
|
|
| 329 |
}
|
| 330 |
|
| 331 |
+
function getSaveData() { // Called by Python when save button is clicked
|
| 332 |
+
console.log(`JS getSaveData called. Returning ${newlyPlacedObjects.length} new objects.`);
|
| 333 |
+
// Prepare data in the format Python expects (world positions)
|
| 334 |
const objectsToSave = newlyPlacedObjects.map(obj => {
|
| 335 |
+
if (!obj.userData || !obj.userData.type) { return null; }
|
| 336 |
+
const rotation = { _x: obj.rotation.x, _y: obj.rotation.y, _z: obj.rotation.z, _order: obj.rotation.order };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
return {
|
| 338 |
+
obj_id: obj.userData.obj_id,
|
| 339 |
type: obj.userData.type,
|
| 340 |
position: { x: obj.position.x, y: obj.position.y, z: obj.position.z },
|
| 341 |
rotation: rotation
|
| 342 |
};
|
| 343 |
+
}).filter(obj => obj !== null);
|
| 344 |
|
| 345 |
console.log("Prepared data for saving:", objectsToSave);
|
| 346 |
return JSON.stringify(objectsToSave); // Return as JSON string
|
| 347 |
}
|
| 348 |
|
| 349 |
+
function resetNewlyPlacedObjects() { // Called by Python AFTER successful save
|
| 350 |
+
console.log(`JS resetNewlyPlacedObjects called.`);
|
| 351 |
+
clearUnsavedState(); // Clear memory array AND sessionStorage
|
| 352 |
}
|
| 353 |
|
| 354 |
|
| 355 |
// --- Animation Loop ---
|
| 356 |
function updatePlayerMovement() { /* ... unchanged ... */
|
| 357 |
+
if (!playerMesh) return;
|
| 358 |
+
const moveDirection = new THREE.Vector3(0, 0, 0);
|
| 359 |
+
if (keysPressed['KeyW'] || keysPressed['ArrowUp']) moveDirection.z -= 1;
|
| 360 |
+
if (keysPressed['KeyS'] || keysPressed['ArrowDown']) moveDirection.z += 1;
|
| 361 |
+
if (keysPressed['KeyA'] || keysPressed['ArrowLeft']) moveDirection.x -= 1;
|
| 362 |
+
if (keysPressed['KeyD'] || keysPressed['ArrowRight']) moveDirection.x += 1;
|
| 363 |
+
|
| 364 |
+
if (moveDirection.lengthSq() > 0) {
|
| 365 |
+
moveDirection.normalize().multiplyScalar(playerSpeed);
|
| 366 |
+
playerMesh.position.add(moveDirection);
|
| 367 |
+
playerMesh.position.y = Math.max(playerMesh.position.y, 0.4 + 0.8/2);
|
| 368 |
+
}
|
|
|
|
| 369 |
}
|
| 370 |
|
| 371 |
function updateCamera() { /* ... unchanged ... */
|
| 372 |
if (!playerMesh) return;
|
| 373 |
const offset = new THREE.Vector3(0, 15, 20);
|
| 374 |
const targetPosition = playerMesh.position.clone().add(offset);
|
| 375 |
+
camera.position.lerp(targetPosition, 0.08);
|
| 376 |
camera.lookAt(playerMesh.position);
|
| 377 |
}
|
| 378 |
|