I am trying to save an image to the server that is being created with the following three.js script..
actualCode(THREE);
function actualCode(THREE) {
//Variables for rendering
const renderer = new THREE.WebGLRenderer({
antialias: true
});
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(30, 400.0 / 400, 1, 1000);
//Object variables
let texture;
let paintedMug;
//Preload image, then trigger rendering
const loader = new THREE.TextureLoader();
texture = loader.load("images/djmug2.jpg", function (_tex) {
// /*Debugging:*/ setTimeout(() => document.body.appendChild(texture.image), 100);
init();
//views 17.5=front | 355=side | 139.6=back
renderImageSolo(17.5);
});
function init() {
//Init scene and camera
camera.position.set(0, 1.3, 11);
camera.lookAt(scene.position);
renderer.setSize(400, 400);
//Set an ambient light
const light = new THREE.AmbientLight(0xffffff); // soft white light
scene.add(light);
//Draw white mug
const muggeom = new THREE.CylinderGeometry(1.5, 1.5, 3.5, 240, 1);
const mugmaterial = new THREE.MeshStandardMaterial({
color: "#fff",
});
const mug = new THREE.Mesh(muggeom, mugmaterial);
//Draw painting on mug with slightly larger radius
const paintgeom = new THREE.CylinderGeometry(1.5001, 1.5001, 3.3, 240, 1, true);
const paintmaterial = new THREE.MeshStandardMaterial({
map: texture,
});
const paint = new THREE.Mesh(paintgeom, paintmaterial);
//Define a group as mug + paint
paintedMug = new THREE.Group();
paintedMug.add(mug);
paintedMug.add(paint);
//Add group to scene
scene.add(paintedMug);
}
function renderImageSolo(angle) {
//Init just like main renderer / scene, will use same camera
const solo_renderer = new THREE.WebGLRenderer({
antialias: true
});
solo_renderer.setSize(renderer.domElement.width, renderer.domElement.height);
solo_renderer.domElement.style.marginTop = "0em"; //Space out canvas
solo_renderer.domElement.id = "canvas"; //give canvas id
document.body.appendChild(solo_renderer.domElement);
const solo_scene = new THREE.Scene();
//Set an ambient light
const light = new THREE.AmbientLight(0xffffff); // soft white light
solo_scene.add(light);
//Draw painting alone
const paintgeom = new THREE.CylinderGeometry(1.5, 1.5, 3.3, 240, 1, true);
const paintmaterial = new THREE.MeshStandardMaterial({
map: texture,
});
const paint = new THREE.Mesh(paintgeom, paintmaterial);
//Add paint to scene
solo_scene.add(paint);
//Rotate paint by angle
paint.rotation.y = angle
//Draw result with green screen bg
solo_scene.background = new THREE.Color(0x04F404);
//Draw result with trans bg (not working showing as black atm)
//solo_scene.background = new THREE.WebGLRenderer( { alpha: true } );
solo_renderer.render(solo_scene, camera);
saveit();
}
}
I then attempt to save the generated image with ajax as follows..
function saveit() {
const canvas = document.getElementById('canvas');
var photo = canvas.toDataURL('image/jpeg');
$.ajax({
method: 'POST',
url: 'photo_upload.php',
data: {
photo: photo
}
});
}
photo_upload.php contents..
$data = $_POST['photo'];
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
mkdir($_SERVER['DOCUMENT_ROOT'] . "/photos");
file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/photos/".time().'.png', $data);
die;
but nothing gets saved and /photos on the server remains empty, also, as a seperate issue if i right click and "save image" the saved image is just a black square and not what is shown on the screen.