immersive-web / webxr-samples

Samples to demonstrate use of the WebXR Device API
https://immersive-web.github.io/webxr-samples/
MIT License
979 stars 474 forks source link

Send position,rotation,scale to a php server #184

Closed micfio closed 3 months ago

micfio commented 3 months ago

Hello, using the hit-test-with-anchors sample i send the psosition,rotation,scale of flowers to a php server: ` function addAnchoredObjectsToScene(anchor) { let flower = new Gltf2Node({url: 'media/gltf/sunflower/sunflower.gltf'}); scene.addNode(flower); anchoredObjects.push({ anchoredObject: flower, anchor: anchor });

// Ottieni la posizione, rotazione e scala del nodo flower

let posizione = flower.position;
let rotazione = flower.rotation;
let scala = flower.scale; 

    // Invia i dati al server PHP
    inviaDatiAlServer(posizione, rotazione, scala);

// Funzione per inviare i dati al server PHP function inviaDatiAlServer(posizione, rotazione, scala) { const dati = { posizione: posizione, rotazione: rotazione, scala: scala };

    // Invia i dati tramite richiesta AJAX
    const xhr = new XMLHttpRequest();
    xhr.open('POST', 'salva_dati.php', true);
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4 && xhr.status === 200) {
            console.log('Dati inviati al server PHP.');
        }
    };
    xhr.send(JSON.stringify(dati));
}

<?php // Percorso del file di testo in cui salvare i dati $file = 'dati_oggetti.txt';

// Ricevi i dati inviati dalla richiesta AJAX $data = json_decode(file_get_contents('php://input'), true);

// Costruisci la stringa dei dati da salvare $line = 'Posizione: ' . json_encode($data['posizione']) . ', Rotazione: ' . json_encode($data['rotazione']) . ', Scala: ' . json_encode($data['scala']) . PHP_EOL;

// Apri il file in modalità append $fp = fopen($file, 'a');

// Scrivi i dati nel file if ($fp) { fwrite($fp, $line); fclose($fp); echo 'Dati salvati con successo nel file.'; } else { echo 'Errore durante il salvataggio dei dati nel file.'; } ?>

Result file: Posizione: null, Rotazione: [0,0,0,1], Scala: [1,1,1] Posizione: null, Rotazione: [0,0,0,1], Scala: [1,1,1] Posizione: null, Rotazione: [0,0,0,1], Scala: [1,1,1] Posizione: null, Rotazione: [0,0,0,1], Scala: [1,1,1] Posizione: null, Rotazione: [0,0,0,1], Scala: [1,1,1] ` but the position is always null

How i can get the position ? Thanks in advances

himorin commented 3 months ago

closing. Ask PHP questions at a forum for PHP.