123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- /*
- The MIT License (MIT)
- Copyright (c) 2014-2020 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/livecodingspace/blob/master/LICENSE.md)
- */
- // VWF & A-Frame components view driver
- import {Fabric} from '/core/vwf/fabric.js';
- class AFrameComponentView extends Fabric {
- constructor(module) {
- console.log("AFrameComponentView constructor");
- super(module, 'View');
- }
- factory() {
- let _self_ = this;
- return this.load(this.module,
- {
- // == Module Definition ====================================================================
-
- initialize: function (options) {
- let self = this;
- this.fabric = _self_;
- this.nodes = {};
-
- this.tickTime = 0;
- this.realTickDif = 50;
- this.lastrealTickDif = 50;
- this.lastRealTick = performance.now();
- this.interpolateView = true;
-
- this.state.appInitialized = false;
-
- if (typeof options == "object") {
-
- this.rootSelector = options["application-root"];
- }
- else {
- this.rootSelector = options;
- }
-
- this.getRotation = function(id) {
- let r = new THREE.Euler();
- let rot = r.copy(self.state.nodes[id].aframeObj.el.object3D.rotation);
- return rot;
- }
-
- this.getPosition = function (id) {
- // let p = (new THREE.Vector3()).copy(self.state.nodes[id].aframeObj.el.object3D.position);
- // let pos = goog.vec.Vec3.createFromValues(p.x, p.y, p.z)
- return self.state.nodes[id].aframeObj.el.object3D.position.clone();
- }
-
- this.getScale = function (id) {
- // let p = (new THREE.Vector3()).copy(self.state.nodes[id].aframeObj.el.object3D.scale);
- // let data = goog.vec.Vec3.createFromValues(p.x, p.y, p.z)
- return self.state.nodes[id].aframeObj.el.object3D.scale.clone();
- }
-
- // this.resetInterpolation = function (nodeID, propertyName) {
-
- // if (propertyName == 'position') {
- // self.nodes[nodeID].interpolate.position.lastTick = getPosition(nodeID);
- // self.nodes[nodeID].interpolate.position.selfTick = goog.vec.Vec3.clone(self.nodes[nodeID].interpolate.position.lastTick);
- // }
-
- // if (propertyName == 'rotation') {
- // self.nodes[nodeID].interpolate.rotation.lastTick = getRotation(nodeID);
- // self.nodes[nodeID].interpolate.rotation.selfTick = self.nodes[nodeID].interpolate.rotation.lastTick;
- // }
-
- // }
-
- },
-
- // initializedNode: function( nodeID, childID ) {
- // },
-
- createdNode: function (nodeID, childID, childExtendsID, childImplementsIDs,
- childSource, childType, childIndex, childName, callback /* ( ready ) */) {
-
- var node = this.state.nodes[childID];
-
- // If the "nodes" object does not have this object in it, it must not be one that
- // this driver cares about
- if (!node) {
- return;
- }
-
-
- if (this.state.nodes[childID]) {
- this.nodes[childID] = {
- id: childID,
- extends: childExtendsID,
- entityID: this.state.nodes[childID].parentID,
- liveBindings: {}
- };
-
- let entityID = this.state.nodes[childID].parentID;
- let viewDriver = vwf.views["/drivers/view/aframe"];
- let entityNode = Object.entries(viewDriver.nodes).find(el =>
- el[1].id == entityID
- )[1];
-
- if (!entityNode.components)
- entityNode.components = {}
-
-
- entityNode.components[this.state.nodes[childID].name] = childID;
- //console.log(entityNode);
-
-
- }
- // else if (this.state.nodes[childID] && this.state.nodes[childID].aframeObj) {
- // this.nodes[childID] = {
- // id:childID,
- // extends:childExtendsID
- // };
- // }
-
- },
-
- initializedNode: function (nodeID, childID) {
-
- let self = this;
- var node = this.state.nodes[childID];
-
- if (!node) {
- return;
- }
-
- if (this.nodes[childID].extends == "proxy/aframe/a-sound-component.vwf") {
- console.log(vwf.getProperty(childID, 'isPlaying'));
- self.kernel.callMethod(childID, "playSound");
- }
-
- },
-
- createdProperty: function (nodeId, propertyName, propertyValue) {
- return this.satProperty(nodeId, propertyName, propertyValue);
- },
-
- initializedProperty: function (nodeId, propertyName, propertyValue) {
- return this.satProperty(nodeId, propertyName, propertyValue);
- },
-
-
-
- satProperty: function (nodeId, propertyName, propertyValue) {
- let self = this;
- var node = this.state.nodes[nodeId];
-
- if (!(node && node.aframeObj)) {
- return;
- }
-
- if (node.name == "material") {
-
- if (propertyName == 'repeat') {
-
- self.kernel.callMethod(nodeId, "refreshSrc", []);
-
- // let src = node.aframeObj.el.getAttribute('material').src;
- // if (src) {
- // let srcID = src.id;
- // let elID = '#'+ srcID;
- // if(srcID) self.kernel.callMethod(nodeId, "updateSrc", [elID])
- // }
-
-
- }
-
-
- }
-
-
- },
-
- firedEvent: function (nodeID, eventName, eventParameters) {
- let self = this;
- var node = this.state.nodes[nodeID];
-
- if (!(node)) {
- return;
- }
-
- },
-
- executed: function (nodeID, scriptText, scriptType) {
- let self = this;
- var node = this.state.nodes[nodeID];
-
- if (!(node)) {
- return;
- }
-
- },
-
- ticked: function (vwfTime) {
-
- _self_.lerpTick();
-
- },
-
- tocked: function (vwfTime) {
-
- // lerpTock ();
-
- },
-
-
- gotProperty: function (nodeId, propertyName, propertyValue) {
-
- var node = this.state.nodes[nodeId];
-
- if (!(node && node.aframeObj)) {
- return;
- }
-
-
- // if (this.nodes[nodeId].extends == "proxy/aframe/a-sound-component.vwf"){
- // if (propertyName == "currentTime"){
- // console.log(node.aframeObj.el.components.sound.listener.context.currentTime);
- // }
- // }
-
- },
-
- calledMethod: function (nodeID, methodName, methodParameters, methodValue) {
- let self = this;
- var node = this.state.nodes[nodeID];
-
- if (!(node && node.aframeObj)) {
- return;
- }
-
-
- if (this.nodes[nodeID].extends == "proxy/aframe/a-sound-component.vwf") {
- if (methodName == "stopSound") {
-
- console.log("stop sound");
- node.aframeObj.el.components.sound.stopSound();
- self.kernel.setProperty(nodeID, "isPlaying", false);
- //node.aframeObj.stopSound();
- }
-
- if (methodName == "playSound") {
-
- console.log("play sound");
- node.aframeObj.el.components.sound.playSound();
- self.kernel.setProperty(nodeID, "isPlaying", true);
- //node.aframeObj.stopSound();
- }
-
- if (methodName == "pauseSound") {
-
- console.log("pause sound");
- node.aframeObj.el.components.sound.pauseSound();
- self.kernel.setProperty(nodeID, "isPlaying", false);
- //node.aframeObj.stopSound();
- }
- }
-
- }
- });
- }
- lerpTick() {
- // var now = performance.now();
- // self.realTickDif = now - self.lastRealTick;
- // self.lastRealTick = now;
- //reset - loading can cause us to get behind and always but up against the max prediction value
- // self.tickTime = 0;
- let self = this.instance;
- let interNodes = Object.entries(self.nodes).filter(node =>
- node[1].extends == 'proxy/aframe/interpolation-component.vwf');
- interNodes.forEach(node => {
- let nodeID = node[0];
- if (self.state.nodes[nodeID]) {
- if (!self.nodes[nodeID].lastRealTick)
- self.nodes[nodeID].lastRealTick = performance.now();
- var now = performance.now();
- self.nodes[nodeID].realTickDif = now - self.nodes[nodeID].lastRealTick;
- self.nodes[nodeID].lastRealTick = now;
- self.nodes[nodeID].tickTime = 0;
- //console.log(self.nodes[nodeID].realTickDif);
- if (!self.nodes[nodeID].interpolate) {
- self.nodes[nodeID].interpolate = {
- 'position': {},
- 'rotation': {},
- 'scale': {}
- }
- }
- self.nodes[nodeID].interpolate.position.lastTick = (self.nodes[nodeID].interpolate.position.selfTick);
- self.nodes[nodeID].interpolate.position.selfTick = self.getPosition(nodeID);
- self.nodes[nodeID].interpolate.rotation.lastTick = (self.nodes[nodeID].interpolate.rotation.selfTick);
- self.nodes[nodeID].interpolate.rotation.selfTick = self.getRotation(nodeID);
- self.nodes[nodeID].interpolate.scale.lastTick = (self.nodes[nodeID].interpolate.scale.selfTick);
- self.nodes[nodeID].interpolate.scale.selfTick = self.getScale(nodeID);
- //console.log(self.nodes[nodeID].interpolate.rotation.selfTick);
- //self.nodes[nodeID].lastTickTransform = self.nodes[nodeID].selfTickTransform;
- //self.nodes[nodeID].selfTickTransform = getTransform(nodeID);
- }
- })
- }
-
- }
- export { AFrameComponentView as default }
|