react-three-nephilim
Version:
3D SPA engine based on React and Three.js
333 lines (332 loc) • 15.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Decoration = void 0;
var _lodash = _interopRequireDefault(require("lodash"));
var Three = _interopRequireWildcard(require("three"));
var Types = _interopRequireWildcard(require("../Types"));
var _react = require("react");
var _Motion = require("../Motion");
var _Helpers = require("../Helpers");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var Decoration = exports.Decoration = function (_Component) {
function Decoration() {
var _this;
_classCallCheck(this, Decoration);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _callSuper(this, Decoration, [].concat(args));
_defineProperty(_this, "setVisual", function () {
var _withInterface = (0, _Helpers.withInterface)(_this.props),
material = _withInterface.material,
geometry = _withInterface.geometry;
_this.setMaterial(material);
_this.setGeometry(geometry);
});
_defineProperty(_this, "setVisualState", function (state) {
if (!state) state = _this.props;
var vs = _this.calculateVisualState(state);
_this.visual.position.set(vs.position.x, vs.position.y, vs.position.z);
if (_this.type !== Types.Decoration.Emitter) {
_this.visual.scale.set(vs.scale.x, vs.scale.y, vs.scale.z);
}
if (vs.lookAt) {
_this.visual.lookAt(new Three.Vector3(vs.lookAt.x, vs.lookAt.y, vs.lookAt.z));
} else {
_this.visual.rotation.set(vs.rotation.x, vs.rotation.y, vs.rotation.z);
}
});
_defineProperty(_this, "setStateValue", function (sv, vv) {
return sv === undefined ? vv : Array.isArray(sv) ? (0, _Helpers.getDeviceOrientation)() === Types.Orientation.Portrait ? sv[0] : sv[1] : sv;
});
_defineProperty(_this, "setAudio", function (audio) {
var _this$manager = _this.manager,
audioListener = _this$manager.audioListener,
audioLoader = _this$manager.audioLoader;
_this.audio = {};
var _loop = function _loop(a) {
var _audio$a = audio[a],
src = _audio$a.src,
refDistance = _audio$a.refDistance,
maxDistance = _audio$a.maxDistance,
volume = _audio$a.volume,
loop = _audio$a.loop,
play = _audio$a.play;
_this.audio[a] = new Three.PositionalAudio(audioListener);
audioLoader.load(src, function (buffer) {
_this.audio[a].setBuffer(buffer);
if (refDistance !== undefined) {
_this.audio[a].setRefDistance(refDistance);
}
if (maxDistance !== undefined) {
_this.audio[a].setMaxDistance(maxDistance);
}
if (volume !== undefined) {
_this.audio[a].setVolume(volume);
}
if (loop !== undefined) {
_this.audio[a].setLoop(loop);
}
if (play) {
_this.audio[a].play();
}
_this.visual.add(_this.audio[a]);
});
};
for (var a in audio) {
_loop(a);
}
});
_defineProperty(_this, "setMotion", function (motion) {
_this.motion = new _Motion.Motion(_this.visual, motion);
});
_defineProperty(_this, "setActions", function (actions) {
_this.actions = actions;
_this.manager.connectActions(_this, actions);
});
_defineProperty(_this, "getActionState", function (a) {
return _this.actions[a];
});
_defineProperty(_this, "calculateVisualState", function (state) {
var _this$visual = _this.visual,
position = _this$visual.position,
rotation = _this$visual.rotation,
scale = _this$visual.scale;
var calculatedState = {
position: {
x: position.x,
y: position.y,
z: position.z
},
rotation: {
x: rotation.x,
y: rotation.y,
z: rotation.z
},
scale: {
x: scale.x,
y: scale.y,
z: scale.z
}
};
_this.metadata.state = {
position: position,
rotation: rotation,
scale: scale
};
if (state.position) {
calculatedState.position.x = _this.setStateValue(state.position.x, position.x);
calculatedState.position.y = _this.setStateValue(state.position.y, position.y);
calculatedState.position.z = _this.setStateValue(state.position.z, position.z);
_this.metadata.state.position = state.position;
}
if (state.scale) {
calculatedState.scale.x = _this.setStateValue(state.scale.x, scale.x);
calculatedState.scale.y = _this.setStateValue(state.scale.y, scale.y);
calculatedState.scale.z = _this.setStateValue(state.scale.z, scale.z);
_this.metadata.state.scale = state.scale;
}
if (state.rotation) {
calculatedState.rotation.x = _this.setStateValue(state.rotation.x, rotation.x);
calculatedState.rotation.y = _this.setStateValue(state.rotation.y, rotation.y);
calculatedState.rotation.z = _this.setStateValue(state.rotation.z, rotation.z);
_this.metadata.state.rotation = state.rotation;
} else if (state.lookAt) {
calculatedState.lookAt = {};
calculatedState.lookAt.x = _this.setStateValue(state.lookAt.x, 0);
calculatedState.lookAt.y = _this.setStateValue(state.lookAt.y, 0);
calculatedState.lookAt.z = _this.setStateValue(state.lookAt.z, 0);
_this.metadata.state.lookAt = state.lookAt;
}
return calculatedState;
});
_defineProperty(_this, "update", function (onlyGlobal) {
if (_this.type === Types.Decoration.Scene) {
onlyGlobal = _this.id !== _this.manager.activeScene.id;
}
if ((!onlyGlobal || onlyGlobal && _this.isGlobal) && _this.motion) {
_this.motion.update();
}
if (_this.type === Types.Decoration.Emitter) {
_this.nebula.update();
}
if (_this.children) {
for (var c in _this.children) {
_this.children[c].update(onlyGlobal);
}
}
});
_defineProperty(_this, "updateLayout", function () {
var state = _this.metadata.state;
if (_this.type === Types.Decoration.Container) {
_this.setCompositionState();
}
_this.setVisualState(state);
if (_this.children) {
for (var c in _this.children) {
_this.children[c].updateLayout();
}
}
});
_defineProperty(_this, "connect", function (fiberNode) {
var stateNode = fiberNode["return"].stateNode;
if (!stateNode) {
_this.connect(fiberNode["return"]);
} else {
if (!stateNode.visual) {
_this.connect(stateNode._reactInternals);
} else {
if (_this.type === Types.Decoration.Emitter) {
_this.connectEmitter(stateNode);
} else {
stateNode.visual.add(_this.visual);
}
stateNode.children[_this.id] = _this;
_this._nephilimParentNode = stateNode;
}
}
});
_defineProperty(_this, "find", function (id) {
if (_this.id === id) return _this;
if (!_this.children) return false;
for (var k in _this.children) {
var c = _this.children[k].find(id);
if (c) return c;
}
});
_defineProperty(_this, "findAll", function (id) {
var all = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
if (_this.id.includes(id)) all.push(_this);
if (!_this.children) return false;
for (var k in _this.children) {
_this.children[k].findAll(id, all);
}
return all;
});
return _this;
}
_inherits(Decoration, _Component);
return _createClass(Decoration, [{
key: "componentDidMount",
value: function componentDidMount() {
var _withInterface2 = (0, _Helpers.withInterface)(this.props),
id = _withInterface2.id,
audio = _withInterface2.audio,
motion = _withInterface2.motion,
actions = _withInterface2.actions,
layer = _withInterface2.layer,
isGlobal = _withInterface2.isGlobal,
isGLEvents = _withInterface2.isGLEvents,
onClick = _withInterface2.onClick,
onMouseOver = _withInterface2.onMouseOver;
this.id = id || this.visual.uuid || this.visual.id;
this.isGLEvents = this.isGLEvents || isGLEvents;
this.isGlobal = isGlobal || false;
this.metadata = {
state: {
position: {},
scale: {},
rotation: {},
lookAt: {}
}
};
this.setVisualState();
if (audio) this.setAudio(audio);
if (motion) this.setMotion(motion);
if (actions) this.setActions(actions);
if (onClick) this.visual.onClick = onClick;
if (onMouseOver) this.visual.onMouseOver = onMouseOver;
if (layer) {
this.visual.layers.enable(layer);
this.manager.layers[layer] = layer;
}
this.connect(this._reactInternals);
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(_ref) {
var prevPosition = _ref.position,
prevRotation = _ref.rotation,
prevScale = _ref.scale,
prevLookAt = _ref.lookAt,
prevMotion = _ref.motion,
prevActions = _ref.actions,
prevMaterial = _ref.material,
prevAudio = _ref.audio;
var _this$props = this.props,
position = _this$props.position,
rotation = _this$props.rotation,
scale = _this$props.scale,
lookAt = _this$props.lookAt,
motion = _this$props.motion,
actions = _this$props.actions,
material = _this$props.material,
audio = _this$props.audio;
if (!_lodash["default"].isEqual(position, prevPosition) || !_lodash["default"].isEqual(rotation, prevRotation) || !_lodash["default"].isEqual(scale, prevScale) || !_lodash["default"].isEqual(lookAt, prevLookAt)) {
this.setVisualState({
position: position,
rotation: rotation,
scale: scale,
lookAt: lookAt
});
}
if (!_lodash["default"].isEqual(motion, prevMotion)) {
this.setMotion(motion);
}
if (!_lodash["default"].isEqual(actions, prevActions)) {
this.setActions(actions);
}
if (!_lodash["default"].isEqual(material, prevMaterial)) {
this.updateMaterial(material);
}
if (!_lodash["default"].isEqual(audio, prevAudio)) {
for (var a in audio) {
if (audio[a].play) {
this.audio[a].play();
} else if (this.audio[a].isPlaying) {
this.audio[a].stop();
}
}
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
for (var a in this.audio) {
var _this$audio$a;
if ((_this$audio$a = this.audio[a]) !== null && _this$audio$a !== void 0 && _this$audio$a.isPlaying) {
this.audio[a].stop();
}
}
if (this.type === Types.Decoration.Emitter) {
this.visual.parent.destroy();
} else {
this.visual.parent.remove(this.visual);
}
delete this._nephilimParentNode.children[this.id];
}
}, {
key: "render",
value: function render() {
return null;
}
}]);
}(_react.Component);