123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- "use strict";
- /*
- The MIT License (MIT)
- Copyright (c) 2014-2018 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/livecodingspace/blob/master/LICENSE.md)
- Virtual World Framework Apache 2.0 license (https://github.com/NikolaySuslov/livecodingspace/blob/master/licenses/LICENSE_VWF.md)
- */
- // VWF & A-Frame components view driver
- define(["module", "vwf/view"], function (module, view) {
- var self;
- return view.load(module, {
- // == Module Definition ====================================================================
- initialize: function (options) {
- self = this;
- 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.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["vwf/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) {
- var node = this.state.nodes[childID];
- if (!node) {
- return;
- }
- if (this.nodes[childID].extends == "http://vwf.example.com/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) {
- 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) {
- var node = this.state.nodes[nodeID];
- if (!(node)) {
- return;
- }
- if (eventName == "printIt") {
- var clientThatSatProperty = self.kernel.client();
- var me = self.kernel.moniker();
- // If the transform property was initially updated by this view....
- if (clientThatSatProperty == me) {
- let selectedText = eventParameters[0];
- let printText = _app.helpers.replaceSubStringALL(selectedText, 'this', 'me');
- if (!self.nodes[nodeID].liveBindings) {
- self.nodes[nodeID].liveBindings = {}
- }
- //let bindObject = self.nodes[nodeID].liveBindings;
- let bindString = 'vwf.views["vwf/view/aframeComponent"].nodes["' + nodeID + '"].liveBindings';
- let sender = JSON.stringify(eventParameters[1]);
- let scriptText = 'let binding = ' + bindString + '; binding.me = this; binding.sender = ' + sender + '; lively.vm.syncEval("var print = ' + printText + '",{topLevelVarRecorder: binding});';
- vwf_view.kernel.execute(nodeID, scriptText);
- }
- }
- },
- executed: function (nodeID, scriptText, scriptType) {
- var node = this.state.nodes[nodeID];
- if (!(node)) {
- return;
- }
- if (scriptText.includes('var print')) {
- let print = self.nodes[nodeID].liveBindings.print;
- let me = self.kernel.moniker();
- let sender = self.nodes[nodeID].liveBindings.sender;
- if (me == sender) {
- let data = lively.lang.obj.inspect(print); //JSON.stringify(print);
- self.kernel.fireEvent(nodeID, "printEvent", [data]);
- }
- }
- },
- ticked: function (vwfTime) {
-
- lerpTick();
-
- },
- tocked: function (vwfTime) {
- // lerpTock ();
- },
- gotProperty: function (nodeId, propertyName, propertyValue) {
- var selfs = this;
- var node = this.state.nodes[nodeId];
- if (!(node && node.aframeObj)) {
- return;
- }
- // if (this.nodes[nodeId].extends == "http://vwf.example.com/aframe/a-sound-component.vwf"){
- // if (propertyName == "currentTime"){
- // console.log(node.aframeObj.el.components.sound.listener.context.currentTime);
- // }
- // }
- },
- calledMethod: function (nodeID, methodName, methodParameters, methodValue) {
- var node = this.state.nodes[nodeID];
- if (!(node && node.aframeObj)) {
- return;
- }
- if (this.nodes[nodeID].extends == "http://vwf.example.com/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();
- }
- }
- }
- });
- function 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 interNodes = Object.entries(self.nodes).filter(node =>
- node[1].extends == 'http://vwf.example.com/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 = getPosition(nodeID);
- self.nodes[nodeID].interpolate.rotation.lastTick = (self.nodes[nodeID].interpolate.rotation.selfTick);
- self.nodes[nodeID].interpolate.rotation.selfTick = getRotation(nodeID);
- self.nodes[nodeID].interpolate.scale.lastTick = (self.nodes[nodeID].interpolate.scale.selfTick);
- self.nodes[nodeID].interpolate.scale.selfTick = getScale(nodeID);
- //console.log(self.nodes[nodeID].interpolate.rotation.selfTick);
- //self.nodes[nodeID].lastTickTransform = self.nodes[nodeID].selfTickTransform;
- //self.nodes[nodeID].selfTickTransform = getTransform(nodeID);
- }
- })
- }
- function getRotation(id) {
- let r = new THREE.Euler();
- let rot = r.copy(self.state.nodes[id].aframeObj.el.object3D.rotation);
- //let rot = self.state.nodes[id].aframeObj.el.getAttribute('rotation');
- //let interp = (new THREE.Vector3()).fromArray(Object.values(rot))//goog.vec.Mat4.clone(self.state.nodes[id].threeObject.matrix.elements);
- return rot;
- }
- function getPosition(id) {
- // let p = new THREE.Vector3();
- // let pos = p.copy(self.state.nodes[id].aframeObj.el.object3D.position);
- 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)
- //let pos = self.state.nodes[id].aframeObj.el.getAttribute('position');
- //let interp = (new THREE.Vector3()).fromArray(Object.values(pos))//goog.vec.Mat4.clone(self.state.nodes[id].threeObject.matrix.elements);
- return pos;
- }
- function getScale(id) {
- // let p = new THREE.Vector3();
- // let pos = p.copy(self.state.nodes[id].aframeObj.el.object3D.position);
- 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)
- //let pos = self.state.nodes[id].aframeObj.el.getAttribute('position');
- //let interp = (new THREE.Vector3()).fromArray(Object.values(pos))//goog.vec.Mat4.clone(self.state.nodes[id].threeObject.matrix.elements);
- return data;
- }
- });
|