react-three-nephilim
Version:
3D SPA engine based on React and Three.js
157 lines (156 loc) • 6.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Motion = void 0;
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 _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 _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
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 Motion = exports.Motion = _createClass(function Motion(visual, params) {
var _this = this;
_classCallCheck(this, Motion);
_defineProperty(this, "update", function () {
var axis = null;
var _this$manager = _this.manager,
_this$manager$contain = _this$manager.container,
offsetWidth = _this$manager$contain.offsetWidth,
offsetHeight = _this$manager$contain.offsetHeight,
mouse = _this$manager.mouse,
touch = _this$manager.touch;
for (var m in _this.modes) {
switch (m) {
case 'trackMouse':
var trackMouseX = _this.modes[m].mouseX;
var trackMouseY = _this.modes[m].mouseY;
if (trackMouseX) {
_this.trackMouseByAxis({
track: trackMouseX,
axis: trackMouseX.symmetry ? mouse.x - offsetWidth / 2 : mouse.x
});
}
if (trackMouseY) {
_this.trackMouseByAxis({
track: trackMouseY,
axis: trackMouseY.symmetry ? mouse.y - offsetHeight / 2 : mouse.y
});
}
break;
case 'trackTouch':
var trackTouchX = _this.modes[m].touchX;
var trackTouchY = _this.modes[m].touchY;
if (trackTouchX) {
_this.trackMouseByAxis({
track: trackTouchX,
axis: trackTouchX.symmetry ? touch.x - offsetWidth / 2 : touch.x
});
}
if (trackTouchY) {
_this.trackMouseByAxis({
track: trackTouchY,
axis: trackTouchY.symmetry ? touch.y - offsetHeight / 2 : touch.y
});
}
break;
case 'uniforms':
var uniforms = _this.visual.material.uniforms;
if (uniforms) {
for (var u in _this.modes[m]) {
uniforms[u].value += _this.modes[m][u];
}
}
break;
case 'morph':
var mti = _this.visual.morphTargetInfluences;
for (var i = 0; i < mti.length; i++) {
mti[i] += _this.modes[m].step;
}
break;
case 'swarm':
var _this$visual$geometry = _this.visual.geometry.attributes.position,
array = _this$visual$geometry.array,
count = _this$visual$geometry.count;
axis = ['x', 'y', 'z'];
for (var c = 0; c < count; c++) {
var velocity = _this.swarmParams[c].velocity;
for (var a = 0; a < axis.length; a++) {
array[c * 3 + a] += velocity[axis[a]];
if (array[c * 3 + a] < -_this.modes[m].maxValue || array[c * 3 + a] > _this.modes[m].maxValue) {
velocity[axis[a]] = -velocity[axis[a]];
}
}
}
break;
default:
axis = _this.modes[m].axis;
for (var _a in axis) {
axis[_a].relativeValue += axis[_a].velocity;
_this.visual[m][_a] += axis[_a].velocity;
if (axis[_a].relativeValue < -axis[_a].maxValue || axis[_a].relativeValue > axis[_a].maxValue) {
axis[_a].velocity = -axis[_a].velocity;
}
}
break;
}
}
});
_defineProperty(this, "trackMouseByAxis", function (_ref) {
var track = _ref.track,
axis = _ref.axis;
['position', 'rotation', 'scale'].forEach(function (type) {
if (track[type]) {
if (track[type].x) {
_this.visual[type].x = axis * track[type].x + (track[type].modX || 0);
}
if (track[type].y) {
_this.visual[type].y = axis * track[type].y + (track[type].modY || 0);
}
if (track[type].z) {
_this.visual[type].z = axis * track[type].z + (track[type].modZ || 0);
}
}
});
});
this.visual = visual;
this.modes = {};
var _loop = function _loop(m) {
if (m === 'swarm') {
_this.swarmParams = [];
var count = _this.visual.geometry.attributes.position.count;
var _loop2 = function _loop2() {
var velocity = {};
['x', 'y', 'z'].forEach(function (a) {
var v = params[m].randVelocity;
velocity[a] = params[m].axis.includes(a) ? -v + Math.random() * v * 2 : 0;
});
_this.swarmParams.push({
velocity: velocity
});
};
for (var c = 0; c < count; c++) {
_loop2();
}
_this.modes[m] = params[m];
} else if (params[m].axis) {
var axis = params[m].axis.split('');
_this.modes[m] = {
axis: {}
};
axis.forEach(function (a) {
return _this.modes[m].axis[a] = {
relativeValue: 0,
maxValue: params[m].maxValue,
velocity: params[m].velocity ? params[m].reverse ? -params[m].velocity : params[m].velocity : -params[m].randVelocity + Math.random() * params[m].randVelocity * 2
};
});
} else {
_this.modes[m] = params[m];
}
};
for (var m in params) {
_loop(m);
}
});