UNPKG

reality-three

Version:

Simple light-weight game engine using three.js, three-mesh-ui and rapier

123 lines (122 loc) 5.79 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } import * as THREE from 'three'; import ThreeMeshUI from 'three-mesh-ui'; import VRMode from './VR/VRMode'; import Logger from './Logger'; var Renderer = /*#__PURE__*/function () { function Renderer(game, options) { _classCallCheck(this, Renderer); this.game = game; this.options = options; this.width = this.options.width; this.height = this.options.height; this.threeJSRenderer = new THREE.WebGL1Renderer({ antialias: true }); this.threeJSRenderer.gammaOutput = true; this.threeJSRenderer.outputEncoding = THREE.sRGBEncoding; this.threeJSRenderer.shadowMap.enabled = true; this.threeJSRenderer.shadowMap.type = 1; this.threeJSRenderer.toneMapping = 0; this.threeJSRenderer.toneMappingExposure = 1; this.threeJSRenderer.physicallyCorrectLights = false; this.threeJSRenderer.setPixelRatio(this.options.pixelRatio); this.threeJSRenderer.setSize(this.width, this.height); this.threeJSCamera = new THREE.PerspectiveCamera(50, this.width / this.height, 0.01, 1000); this.threeJSCamera.aspect = this.width / this.height; this.threeJSCamera.updateProjectionMatrix(); this.threeJSCameraAudioListener = new THREE.AudioListener(); this.threeJSCamera.add(this.threeJSCameraAudioListener); if (this.options.enableVR) { this._initVR(); } } _createClass(Renderer, [{ key: "_initVR", value: function _initVR() { var _this = this; this.vrMode = new VRMode(); this.vrMode.on('SESSION_STARTED', function (session) { var setSessionSuccess = function setSessionSuccess() { console.log('threeJSRenderer.xr.setSession succeeded'); }; var setSessionFailed = function setSessionFailed(error) { console.error('threeJSRenderer.xr.setSession failed!', error); throw error; }; _this.threeJSRenderer.xr.setSession(session).then(setSessionSuccess, setSessionFailed); }); this.vrMode.on('SESSION_ENDED', function () { console.log('VR Session Eneded'); }); this.threeJSRenderer.xr.enabled = true; } }, { key: "getCanvas", value: function getCanvas() { return this.threeJSRenderer.domElement; } }, { key: "setSize", value: function setSize(width, height) { this.width = width; this.height = height; this.threeJSRenderer.setSize(this.width, this.height); this.threeJSCamera.aspect = this.width / this.height; this.threeJSCamera.updateProjectionMatrix(); if (this.game.scene) { var threeJSScene = this.game.scene.threeJSScene; this.threeJSRenderer.render(threeJSScene, this.threeJSCamera); } } }, { key: "play", value: function play() { // Must use setAnimationLoop() (and not window.requestAnimationFrame()) for VR projects this.threeJSRenderer.setAnimationLoop(this._render.bind(this)); } }, { key: "pause", value: function pause() { this.threeJSRenderer.setAnimationLoop(null); } // time is a slowly increasing number of millisec since the beggining. }, { key: "_render", value: function _render(time) { var deltaTimeMS = this.previousRenderTime ? time - this.previousRenderTime : time; this.previousRenderTime = time; var deltaTimeInSec = deltaTimeMS / 1000; ThreeMeshUI.update(); if (this.game.scene) { var threeJSScene = this.game.scene.threeJSScene; threeJSScene.dispatchEvent({ type: 'update', deltaTimeInSec: deltaTimeInSec, camera: this.threeJSCamera }); this.threeJSRenderer.render(threeJSScene, this.threeJSCamera); } } }, { key: "printScene", value: function printScene() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { positions: true }; if (this.game.scene) { var threeJSScene = this.game.scene.threeJSScene; Logger.dumpObject(threeJSScene, options); } else { console.warn('printScene(): No scene currently loaded, nothing to print'); } } }]); return Renderer; }(); export default Renderer;