2.2.0 • Published 4 years ago
fspy-camera-loader v2.2.0
FSpyCameraLoader
What is this?
Script for importing fSpy camera data into three.js.
You can also create a pseudo AR expression like this tweet.
It takes in the json format camera data output by fSpy and converts it into the PerspetiveCamera of three.js.
FSpyCameraLoader inherits the Loader object of three.js and can be used in the same way as other loaders (that is, there are load function and parse function etc.).
Installing from npm
$ npm install --save fspy-camera-loader
Getting Started
var loader = new FSpyCameraLoader();
var camera;
loader.load(
'path/to/fSpyJsonFile',
// onload
function ( result ) {
camera = result;
},
// onprogress
function ( xhr ) {
console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
},
// onerror
function ( error ) {
console.log( 'ERROR' );
}
);
If you want to include a background image, you can use the following example. Of course, other methods are also acceptable.
html,body {
margin: 0;
padding: 0;
height: 100%;
overflow: hidden;
}
#myCanvas {
width: 100%;
height: 100%;
background-image: url(path/to/image);
background-size: cover;
background-position: 50% 50%;
background-repeat: no-repeat;
}
var camera;
var renderer = new THREE.WebGLRenderer({
canvas: document.querySelector('#myCanvas'),
alpha: true,
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setClearColor(0x000000, 0);
renderer.setSize(window.innerWidth, window.innerHeight);
var scene = new THREE.Scene();
var geometry = new THREE.BoxGeometry(3, 3, 3);
var material = new THREE.MeshNormalMaterial();
var box = new THREE.Mesh(geometry, material);
box.position.set(0, 0, 0);
scene.add(box);
var fSpyCameraLoader = new FSpyCameraLoader();
fSpyCameraLoader.setCanvas(document.querySelector('#myCanvas'));
// If you want to make the behavior behave like CSS background-size: cover, use this function.
fSpyCameraLoader.setResizeUpdate();
fSpyCameraLoader.load('path/to/fSpyJson', function ( result ) {
camera = result;
renderLoop();
});
window.addEventListener('resize', function () {
renderer.setSize(window.innerWidth, window.innerHeight);
});
function renderLoop() {
requestAnimationFrame(renderLoop);
renderer.render(scene, camera);
box.rotation.y += 0.01;
}
LISENCE
MIT