Spaces:
Running
Running
File size: 2,561 Bytes
2bd5a64 23c6996 2bd5a64 695a588 2bd5a64 23c6996 2bd5a64 dd01638 2bd5a64 bb72c71 2bd5a64 bb72c71 2bd5a64 7f3416a 2bd5a64 aed8983 2bd5a64 aed8983 2bd5a64 aed8983 2bd5a64 f2e48ec 2bd5a64 85f79d1 2bd5a64 23c6996 2bd5a64 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
// Fix for cameraXPhone not working on mobile devices in PlayCanvas implementation
// The key issue is in the camera setup section of your PlayCanvas viewer
// Make sure these changes are applied to the camera creation code
// Look for the section where cameraEntity is created and positioned
// Create camera entity
cameraEntity = new pc.Entity('camera');
cameraEntity.addComponent('camera', {
clearColor: new pc.Color(
config.canvas_background ? parseInt(config.canvas_background.substr(1, 2), 16) / 255 : 0,
config.canvas_background ? parseInt(config.canvas_background.substr(3, 2), 16) / 255 : 0,
config.canvas_background ? parseInt(config.canvas_background.substr(5, 2), 16) / 255 : 0
),
toneMapping: pc.TONEMAP_ACES
});
// Log the chosen camera position for debugging
console.log(`Setting camera position for ${isMobile ? 'mobile' : 'desktop'}: (${chosenCameraX}, ${chosenCameraY}, ${chosenCameraZ})`);
// Set camera position directly using X, Y, Z coordinates from config
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
cameraEntity.lookAt(modelEntity.getPosition());
// The resetCamera function should also be updated to use the correct values:
function resetCamera() {
if (!cameraEntity || !modelEntity || !app) return;
try {
// Get the orbit camera script
const orbitCam = cameraEntity.script.orbitCamera;
if (!orbitCam) return;
// Store model position
const modelPos = modelEntity.getPosition();
console.log(`Reset camera position for ${isMobile ? 'mobile' : 'desktop'}: (${chosenCameraX}, ${chosenCameraY}, ${chosenCameraZ})`);
// 1. Create a temporary entity to help calculate new values
const tempEntity = new pc.Entity();
tempEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
tempEntity.lookAt(modelPos);
// 2. Calculate the distance between camera and model
const distance = new pc.Vec3().sub2(
new pc.Vec3(chosenCameraX, chosenCameraY, chosenCameraZ),
modelPos
).length();
// 3. Set camera position
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
cameraEntity.lookAt(modelPos);
// 4. Update the orbit camera's pivot point
orbitCam.pivotPoint = new pc.Vec3(modelPos.x, modelPos.y, modelPos.z);
// 5. Set the distance
orbitCam._targetDistance = distance;
orbitCam._distance = distance;
// Rest of your resetCamera function...
// ...
} catch (error) {
console.error("Error resetting camera:", error);
}
} |