nanogl-gltf
Version:
61 lines (60 loc) • 2.18 kB
JavaScript
import Gltf2 from '../types/Gltf2';
import GltfTypes from '../types/GltfTypes';
import PerspectiveLens from 'nanogl-camera/perspective-lens';
import OrthographicLens from 'nanogl-camera/ortho-lens';
/**
* The Camera element only contains a perspective or orthographic lens object, used to render the scene.
*/
export default class Camera {
constructor() {
this.gltftype = GltfTypes.CAMERA;
}
/**
* Parse the Camera data, create the nanogl-camera lens object
* @param gltfLoader GLTFLoader to use, unused here
* @param data Data to parse
*/
parse(gltfLoader, data) {
this.type = data.type;
switch (this.type) {
case Gltf2.CameraType.PERSPECTIVE:
this.projectionData = data.perspective;
this.lens = this.createPerpective(this.projectionData);
break;
case Gltf2.CameraType.ORTHOGRAPHIC:
this.projectionData = data.orthographic;
this.lens = this.createOrtho(this.projectionData);
break;
default:
throw new Error('Camera - unsupported type ' + this.type);
}
return Promise.resolve();
}
/**
* Create a PerspectiveLens from the glTF data
* @param data Data coming from the glTF file, containing the perspective data
*/
createPerpective(data) {
var _a, _b;
const lens = new PerspectiveLens();
lens.near = data.znear;
lens.far = (_a = data.zfar) !== null && _a !== void 0 ? _a : 100; // todo: infinite projection
lens.setVerticalFov(data.yfov);
lens.aspect = (_b = data.aspectRatio) !== null && _b !== void 0 ? _b : 1;
return lens;
}
/**
* Create an OrthographicLens from the glTF data
* @param data Data coming from the glTF file, containing the orthographic data
*/
createOrtho(data) {
const lens = new OrthographicLens();
lens.near = data.znear;
lens.far = data.zfar;
lens._xMin = -data.xmag;
lens._xMax = data.xmag;
lens._yMin = -data.ymag;
lens._yMax = data.ymag;
return lens;
}
}