12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166 |
- "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 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.state.appInitialized = false;
- this.state.showMobileJoystick = function(){
- let controlEl = document.querySelector('#avatarControl');
- if(controlEl){
- controlEl.setAttribute("virtual-gamepad-controls", {});
- controlEl.addEventListener("move", setJoystickMoveInput);
- }
- }
- this.state.hideMobileJoystick = function(){
- let controlEl = document.querySelector('#avatarControl');
- if(controlEl){
- controlEl.removeAttribute("virtual-gamepad-controls");
- controlEl.removeEventListener("move", setJoystickMoveInput);
- }
- }
- if (options === undefined) { options = {}; }
- if (typeof options == "object") {
- this.rootSelector = options["application-root"];
- }
- else {
- this.rootSelector = options;
- }
- // this.gearvr = options.gearvr !== undefined ? options.gearvr : false;
- // this.wmrright = options.wmrright !== undefined ? options.wmrright : false;
- // this.wmrleft = options.wmrleft !== undefined ? options.wmrleft : false;
- this.xrType = undefined;
- //TODO: FIX detection in better way! (now works for Oculus Browser only)
- this.hmd = false;
- this.threeDoF = false;
- this.sixDoF = false;
- this.threeDoFMobile = AFRAME.utils.device.isMobileVR() && AFRAME.utils.device.checkHeadsetConnected() && !navigator.userAgent.includes('Quest');
- this.sixDoFMobile = AFRAME.utils.device.checkHeadsetConnected() && navigator.userAgent.includes('Quest');
- this.sixDoFDesktop = !AFRAME.utils.device.isMobile() && !AFRAME.utils.device.isMobileVR() && AFRAME.utils.device.checkHeadsetConnected();
- },
- 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.scenes[childID]) {
- let scene = this.state.scenes[childID];
- //TODO: FIX
-
- let prepairAvatar = new Promise((resolve, reject) => {
- document.body.appendChild(scene);
- let createAvatarPromise = new Promise(r=> r(createAvatarControl(scene)));
- return resolve(createAvatarPromise)
- });
- prepairAvatar.then(res => {
- console.log("CREATE GEARVR HERE!!");
- if (self.threeDoFMobile || _app.config.d3DoF ) {
- this.xrType = 'mobileVR';
- let nodeName = 'gearvr-' + self.kernel.moniker();
- createGearVRControls();
- createGearVRController.call(self, childID, nodeName);
- }
-
- console.log("CREATE XRController RIGHT HERE!!");
- // if (!AFRAME.utils.device.isMobileVR()) {
- if (self.sixDoFMobile || self.sixDoFDesktop || _app.config.d6DoF ) {
- let nodeRight = 'xrcontroller-right-' + self.kernel.moniker();
- createXRControls('right');
- createXR.call(this, childID, nodeRight);
- console.log("CREATE XRController LEFT HERE!!");
- // if (!AFRAME.utils.device.isMobileVR()) {
-
- let nodeLeft = 'xrcontroller-left-' + self.kernel.moniker();
- createXRControls('left');
- createXR.call(this, childID, nodeLeft);
- }
- // }
-
- // console.log(res);
- createAvatar.call(self, childID);
- postLoadAction.call(self, childID);
- }).then(res=>{
- //document.body.appendChild(scene);
- })
- // this.state.appInitialized = true;
- //document.body.appendChild(scene); //append is not working in Edge browser
- }
- if (this.state.nodes[childID] && this.state.nodes[childID].aframeObj) {
- this.nodes[childID] = {
- id: childID,
- extends: childExtendsID,
- liveBindings: {}
- // lastTransformStep: 0,
- // lastAnimationStep: 0
- };
- }
- // if(this.state.nodes[childID]) {
- // this.nodes[childID] = {id:childID,extends:childExtendsID};
- // }
- // else if (this.state.nodes[childID] && this.state.nodes[childID].aframeObj.object3D instanceof THREE.Object3D) {
- // this.nodes[childID] = {id:childID,extends:childExtendsID};
- // }
- },
- 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 = JSON.stringify(print); //lively.lang.obj.inspect(print);
- self.kernel.fireEvent(nodeID, "printEvent", [data]);
- }
- }
- },
- initializedNode: function (nodeID, childID) {
- var node = this.state.nodes[childID];
- if (!node) {
- return;
- }
- if (node.extendsID == "http://vwf.example.com/aframe/gearvrcontroller.vwf") {
- console.log("gearVR controller initialized")
- }
- // if (node.extendsID == "http://vwf.example.com/aframe/atext.vwf") {
- // console.log("Text component initialized");
- // node.aframeObj.play();
- // }
- // if (node.prototypes.includes("http://vwf.example.com/aframe/aentity.vwf")) {
- // var clientThatSatProperty = self.kernel.client();
- // var me = self.kernel.moniker();
- // // If the transform property was initially updated by this view....
- // if (clientThatSatProperty == me) {
- // self.kernel.callMethod(childID, "setOwner", [me]);
- // }
- // }
- // if (propertyName == 'ownedBy')
- // {
- // // if (!node.aframeObj.el.getAttribute('ownedBy')){
- // // }
- // }
- },
- createdProperty: function (nodeId, propertyName, propertyValue) {
- return this.satProperty(nodeId, propertyName, propertyValue);
- },
- initializedProperty: function (nodeId, propertyName, propertyValue) {
- return this.satProperty(nodeId, propertyName, propertyValue);
- },
- gotProperty: function (nodeId, propertyName, propertyValue) {
- var selfs = this;
- var node = this.state.nodes[nodeId];
- if (!(node && node.aframeObj)) {
- return;
- }
- },
- satProperty: function (nodeId, propertyName, propertyValue) {
- //var selfs = this;
- var node = this.state.nodes[nodeId];
- if (!(node && node.aframeObj)) {
- return;
- }
- // if (propertyName == 'arjs') {
- // if(propertyValue) {
- // node.aframeObj.setAttribute('arjs', {trackingMethod: "best", sourceType: "webcam", debugUIEnabled: "true"});
- // // let sceneEl = document.querySelector('a-scene');
- // // let marker = document.createElement('a-marker-camera');
- // // marker.setAttribute('preset', 'hiro');
- // // sceneEl.appendChild(marker);
- // //<a-marker-camera preset='hiro'></a-marker-camera>
- // } else {
- // if(node.aframeObj.getAttribute('arjs')){
- // node.aframeObj.removeAttribute('arjs')
- // }
- // }
- // }
- if (propertyName == 'position') {
- receiveModelTransformChanges(nodeId, 'position', propertyValue);
- }
- if (propertyName == 'rotation') {
- receiveModelTransformChanges(nodeId, 'rotation', propertyValue);
- }
- if (propertyName == 'scale') {
- receiveModelTransformChanges(nodeId, 'scale', propertyValue);
- }
- if (node.aframeObj.nodeName == "AUDIO" && propertyName == 'itemSrc') {
- //console.log("sat new item");
- let elID = '#' + node.aframeObj.getAttribute('id');
- Object.entries(this.state.nodes).forEach(el => {
- let sound = el[1].aframeObj.getAttribute('sound');
- if (sound) {
- let soundID = vwf.find(el[0], 'sound');
- self.kernel.callMethod(soundID, "refreshSrc", [elID])
- // if (sound.src !== ""){
- // let src = '#' + sound.src.id;
- // if (src == elID){
- // let soundID = vwf.find(el[0], 'sound');
- // self.kernel.callMethod(soundID, "updateSrc", [elID])
- // }
- // }
- }
- })
- }
- if (node.aframeObj.nodeName == "IMG" && propertyName == 'itemSrc') {
- updateMaterial(node);
- }
- if (node.aframeObj.nodeName == "VIDEO" && propertyName == 'itemSrc') {
- updateMaterial(node);
- }
- if (node.aframeObj.nodeName == "A-ASSET-ITEM" && propertyName == 'itemSrc') {
- //console.log("sat new item");
- let elID = '#' + node.aframeObj.getAttribute('id');
- Object.entries(this.state.nodes).forEach(el => {
- let src = el[1].aframeObj.getAttribute('src');
- let mtl = el[1].aframeObj.getAttribute('mtl');
- if (src) {
- // console.log("my: " + src);
- if (src == elID)
- self.kernel.callMethod(el[0], "updateModel", [elID])
- }
- if (mtl) {
- // console.log("my: " + mtl);
- if (mtl == elID)
- self.kernel.callMethod(el[0], "updateModelMtl", [elID])
- }
- })
- }
- // var aframeObject = node.aframeObj;
- // switch (propertyName) {
- // case "clickable":
- // if (propertyValue) {
- // aframeObject.addEventListener('click', function (evt) {
- // let cursorID = 'cursor-avatar-'+selfs.kernel.moniker();
- // if (evt.detail.cursorEl.id == cursorID) {
- // vwf_view.kernel.fireEvent(nodeId, "clickEvent")
- // }
- // });
- // }
- // break;
- // }
- },
- deletedNode: function (childID) {
- delete this.nodes[childID];
- },
- 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/aframe"].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);
- }
- }
- if (eventName == "changingTransformFromView") {
- var clientThatSatProperty = self.kernel.client();
- var me = self.kernel.moniker();
- // If the transform property was initially updated by this view....
- if (clientThatSatProperty == me) {
- var node = this.state.nodes[nodeID];
- node.ignoreNextTransformUpdate = true;
- }
- }
- //var avatarID = vwf_view.kernel.find("", avatarName)
- // if (eventName == "postLoadAction") {
- // Object.entries(self.state.nodes).forEach(el => {
- // if (el[1].prototypes.includes("http://vwf.example.com/aframe/aentity.vwf")) {
- // vwf_view.kernel.callMethod(el[0], "setOwner", [self.kernel.moniker()]);
- // }
- // });
- // }
- var avatarName = 'avatar-' + self.kernel.moniker();
- let intersectEvents = ['hitstart', 'hitend', 'intersect', 'clearIntersect']; //'intersect',
- let hitEvent = intersectEvents.filter(el=> el == eventName.slice(0,-5))[0]; //slice Event word
- if (hitEvent)
- {
- var clientThatSatProperty = self.kernel.client();
- var me = self.kernel.moniker();
- // If the transform property was initially updated by this view....
- if (clientThatSatProperty == me) {
- let methodName = eventName +'Method';
- vwf.callMethod(nodeID, methodName, [])
- }
- }
-
- if (eventName == "clickEvent") {
- if (self.kernel.moniker() == eventParameters[0]) {
- let avatar = self.nodes[avatarName];
- let mode = vwf.getProperty(avatarName, 'selectMode');
- vwf_view.kernel.callMethod(nodeID, "clickEventMethod", [])
- if (mode) {
- console.log("allow to click!!!")
- vwf_view.kernel.setProperty(avatarName, 'selectMode', false);
- let editorDriver = vwf.views["vwf/view/editor-new"];
- if (editorDriver) {
- let selectSwitch = document.querySelector('#selectNodeSwitch');
- // const selectSwitchComp = new mdc.iconButton.MDCIconButtonToggle(selectSwitch); //new mdc.select.MDCIconToggle
- selectSwitch._comp.on = false;
- let currentNodeDIV = document.querySelector('#currentNode');
- if (currentNodeDIV) currentNodeDIV._setNode(nodeID);
- }
- }
- }
- }
- },
- ticked: function (vwfTime) {
- updateAvatarPosition();
- //update vr controllers
- if(self.hmd){
- if(self.threeDoF )
- updateHandControllerVR('gearvr-', '#gearvrcontrol');
- if(this.sixDoF ) {
- updateHandControllerVR('xrcontroller-right-', '#xrcontrollerright');
- updateHandControllerVR('xrcontroller-left-', '#xrcontrollerleft');
- }
- }
-
-
-
-
- //lerpTick ();
- },
- calledMethod: function (nodeID, methodName, methodParameters, methodValue) {
- var node = this.state.nodes[nodeID];
- if (!(node && node.aframeObj)) {
- return;
- }
- switch (methodName) {
- case "translateBy":
- case "translateTo":
- //resetInterpolationFromViewDriver(nodeID, methodParameters, 'position')
- break;
- // No need for rotateBy or rotateTo because they call the quaternion methods
- case "quaterniateBy":
- case "quaterniateTo":
- //resetInterpolationFromViewDriver(nodeID, methodParameters, 'rotation')
- break;
- case "scaleBy":
- case "scaleTo":
- // No need for transformBy or worldTransformBy because they call transformTo and worldTransformTo
- case "transformTo":
- case "worldTransformTo":
- //resetInterpolationFromViewDriver(nodeID, methodParameters, 'rotation')
- break;
- }
- if (this.nodes[nodeID].extends == "http://vwf.example.com/aframe/acamera.vwf") {
- if (methodName == "setCameraToActive") {
- if (methodParameters[0] == vwf.moniker_) {
- console.log("set active");
- let offsetComp = node.aframeObj.getAttribute('viewoffset');
- if (offsetComp) {
- let offsetCompID = vwf.find(nodeID, 'viewoffset');
- self.kernel.callMethod(offsetCompID, "setParams", []);
- }
- node.aframeObj.setAttribute('camera', 'active', true);
- document.querySelector('a-scene').emit('makeActiveCamera');
- }
- }
- }
- // if (methodName == "createGooglePoly") {
- // }
- }
- });
- function resetInterpolationFromViewDriver(nodeID, methodParameters, propertyName) {
- if (methodParameters.length < 2 || methodParameters[1] == 0) {
- let viewNode = self.nodes[nodeID];
- if (viewNode.components.interpolation) {
- let interID = viewNode.components.interpolation;
- let viewComponentDriver = vwf.views["vwf/view/aframeComponent"];
- viewComponentDriver.resetInterpolation(interID, propertyName);
- }
- }
- }
- // Receive Model Transform Changes algorithm
- // 1.0 If (own view changes) then IGNORE (only if no external changes have occurred since the user’s view
- // requested this change – otherwise, will need to treat like 1.1 or 1.2)
- // 1.1 Elseif (other external changes and no outstanding own view changes) then ADOPT
- // 1.2 Else Interpolate to the model’s transform (conflict b/w own view and external sourced model changes)
- function receiveModelTransformChanges(nodeID, propertyName, propertyValue) {
- var node = self.state.nodes[nodeID];
- // If the node does not exist in the state's list of nodes, then this update is from a prototype and we
- // should ignore it
- if (!node) {
- return;
- }
- // If the transform property was initially updated by this view....
- if (node.ignoreNextTransformUpdate) {
- node.outstandingTransformRequests.shift();
- node.ignoreNextTransformUpdate = false;
- } else {
- // adoptTransform( node, transformMatrix );
- let prop = self.state.setFromValue(propertyValue);
- if (propertyName == 'position') {
- let pos = goog.vec.Vec3.clone(prop);
- node.aframeObj.object3D.position.set(pos[0], pos[1], pos[2]);
- } else if (propertyName == 'rotation') {
- let rot = goog.vec.Vec3.clone(prop);
- node.aframeObj.object3D.rotation.set(
- THREE.Math.degToRad(rot[0]),
- THREE.Math.degToRad(rot[1]),
- THREE.Math.degToRad(rot[2])
- )
- //node.aframeObj.object3D.rotation.set(rot[0], rot[1], rot[2]);
- } else if (propertyName == 'scale') {
- let scale = goog.vec.Vec3.clone(prop);
- node.aframeObj.object3D.scale.set(scale[0], scale[1], scale[2]);
- }
- }
- }
- function compareCoordinates(a, b, delta) {
- return Math.abs(a.x - b.x) > delta || Math.abs(a.y - b.y) > delta || Math.abs(a.z - b.z) > delta
- }
- function getWorldRotation(el, order) {
- var worldQuat = new THREE.Quaternion();
- el.object3D.getWorldQuaternion(worldQuat);
- //console.log(worldQuat);
- let angle = (new THREE.Euler()).setFromQuaternion(worldQuat, order);
- let rotation = (new THREE.Vector3(THREE.Math.radToDeg(angle.x),
- THREE.Math.radToDeg(angle.y), THREE.Math.radToDeg(angle.z)));
- return rotation
- }
- // function updateAvatarPosition() {
- // let avatarName = 'avatar-' + self.kernel.moniker();
- // var node = self.state.nodes[avatarName];
- // if (!node) return;
- // if (!node.aframeObj) return;
- // let el = document.querySelector('#avatarControl');
- // if (el) {
- // //let position = el.object3D.getWorldPosition(); //el.getAttribute('position');
- // let position = new THREE.Vector3();
- // el.object3D.getWorldPosition(position);
- // let rotation = getWorldRotation(el);
- // vwf_view.kernel.callMethod(avatarName, "followAvatarControl", [position, rotation]);
- // }
- // }
- function updateAvatarPosition() {
- let delta = 0.001;
- let avatarName = 'avatar-' + self.kernel.moniker();
- var node = self.state.nodes[avatarName];
- if (!node) return;
- if (!node.aframeObj) return;
- let el = document.querySelector('#avatarControl');
- let elA = document.querySelector('#avatarControlParent');
- if (el && elA) {
- var position;
- if((self.hmd && self.sixDoF) || (self.hmd && self.threeDoF) || _app.config.d6DoF || _app.config.d3DoF){
- let positionC = el.object3D.position.clone();
- let positionA = elA.object3D.position.clone();
- position = positionC.add(positionA);
- } else {
- position = el.object3D.position;
- }
- //let position = el.object3D.position.add(elA.object3D.position);
- let rotation = el.getAttribute('rotation'); //getWorldRotation(el, 'YXZ');
- let lastRotation = self.nodes[avatarName].selfTickRotation;
- let lastPosition = self.nodes[avatarName].selfTickPosition ? self.nodes[avatarName].selfTickPosition: new THREE.Vector3(0, 0, 0);
- // let currentPosition = node.aframeObj.getAttribute('position');
- // let currentRotation = node.aframeObj.getAttribute('rotation');
-
- // compareCoordinates(position, lastPosition, delta)
- if(position && lastPosition ) {
- let distance = lastPosition.distanceTo(position);
- if (distance > delta)
- {
- // console.log("position not equal");
- self.kernel.setProperty(avatarName, "position", position);
- }
- }
- if (rotation && lastRotation) {
- let distance = Math.abs(rotation.y - lastRotation.y);
- if ( distance > delta) {
- //console.log("rotation not equal")
- self.kernel.callMethod(avatarName, "updateAvatarRotation", [rotation]);
- }
- }
- self.nodes[avatarName].selfTickRotation = Object.assign({}, rotation);
- //self.nodes[avatarName].selfTickPosition = Object.assign({}, position);
- self.nodes[avatarName].selfTickPosition = position.clone();
- }
- }
- function updateHandControllerVR(aName, aSelector) {
- //let avatarName = 'avatar-' + self.kernel.moniker();
- let delta = 0.001
- let avatarName = aName + self.kernel.moniker();
- var node = self.state.nodes[avatarName];
- if (!node) return;
- if (!node.aframeObj) return;
- let elA = document.querySelector('#avatarControlParent');
- let el = document.querySelector(aSelector);
- if (el && elA) {
- let positionC = el.object3D.position.clone();
- let positionA = elA.object3D.position.clone();
- let position = positionC.add(positionA);
- let rotation = el.getAttribute('rotation'); //getWorldRotation(el, 'YXZ');
- let lastRotation = self.nodes[avatarName].selfTickRotation;
- let lastPosition = self.nodes[avatarName].selfTickPosition ? self.nodes[avatarName].selfTickPosition: new THREE.Vector3(0, 0, 0);
- // let currentPosition = node.aframeObj.getAttribute('position');
- //let currentRotation = node.aframeObj.getAttribute('rotation');
- if (position && lastPosition) {
- let distance = lastPosition.distanceTo(position);
- if (distance > delta)
- {
- // console.log("position not equal");
- self.kernel.setProperty(avatarName, "position", position);
- self.kernel.callMethod(avatarName, "moveVRController",[]);
- }
- }
- if (rotation && lastRotation) {
- let distance = compareCoordinates(rotation, lastRotation, delta)
- if (distance)
- {
- //console.log("rotation not equal");
- self.kernel.setProperty(avatarName, "rotation", rotation);
- self.kernel.callMethod(avatarName, "moveVRController",[]);
- }
- }
- // if (position && rotation && lastRotation && lastPosition) {
- // if (compareCoordinates(position, lastPosition, delta) || compareCoordinates(rotation, lastRotation, delta)) {
- // console.log("not equal!!");
- // vwf_view.kernel.callMethod(avatarName, "updateVRControl", [position, rotation]);
- // }
- // }
- //vwf_view.kernel.callMethod(avatarName, "updateVRControl", [position, rotation]);
- self.nodes[avatarName].selfTickPosition = position.clone();
- self.nodes[avatarName].selfTickRotation = Object.assign({}, rotation);
- }
- }
- function getMovementVector(el, vel) {
- var directionVector = new THREE.Vector3(0, 0, 0);
- var rotationEuler = new THREE.Euler(0, 0, 0, 'YXZ');
-
-
- var rotation = el.getAttribute('rotation');
- var velocity = vel;
-
- directionVector.copy(velocity);
- directionVector.multiplyScalar(0.05);
-
- // Absolute.
- if (!rotation) { return directionVector; }
-
- //xRotation = this.data.fly ? rotation.x : 0;
-
- // Transform direction relative to heading.
- rotationEuler.set(THREE.Math.degToRad(0), THREE.Math.degToRad(rotation.y), 0);
- directionVector.applyEuler(rotationEuler);
- return directionVector;
- }
- function setJoystickMoveInput (event) {
- const axes = event.detail.axis;
- //console.log(axes);
- let el = document.querySelector('#avatarControl');
- let position = new THREE.Vector3();
- el.object3D.localToWorld(position);//getWorldPosition(position);
- let vel = new THREE.Vector3(axes[0], 0, -axes[1]);
- el.object3D.position.add(getMovementVector(el,vel));
- }
- function setJoystickRotateY (event) {
- const val = event.detail.value;
- //console.log(val);
- let el = document.querySelector('#avatarControl');
- let rotation = el.object3D.rotation;
- el.object3D.rotation.y += (-val)+rotation.y;
- //el.object3D.rotation.set(rotation.x,-val+rotation.y, rotation.z)
- }
- function setJoystickRotateX (event) {
- const val = event.detail.value;
- //console.log(val);
- let el = document.querySelector('#avatarControl');
- let rotation = el.object3D.rotation;
- el.object3D.rotation.x += val+rotation.x;
- //el.object3D.rotation.set(val+rotation.x, rotation.y, rotation.z)
- }
- async function createAvatarControl(aScene) {
- let avatarName = 'avatar-' + self.kernel.moniker();
- let avatarEl = document.createElement('a-entity');
- avatarEl.setAttribute('id', 'avatarControlParent');
- if (self.d3DoF || _app.config.d3DoF) {
- //avatarEl.setAttribute('gearvr-controls', {});
- avatarEl.setAttribute('movement-controls', {});//{'controls': 'gamepad'});
- //avatarEl.setAttribute("gamepad-controls", {});
- //avatarEl.setAttribute('position', '0 0 0');
- }
- //avatarEl.setAttribute('position', '0 1.6 0');
- let controlEl = document.createElement('a-camera');
- controlEl.setAttribute('id', 'avatarControl');
- controlEl.setAttribute('wasd-controls', {acceleration:20});
- controlEl.setAttribute('look-controls', { pointerLockEnabled: false});
- controlEl.setAttribute('look-controls', 'enabled', true );
- //controlEl.setAttribute('gamepad-controls', {'controller': 0});
- if (AFRAME.utils.device.isMobile()) {
- //self.state.showMobileJoystick()
- controlEl.setAttribute('look-controls', 'enabled', true );
- controlEl.setAttribute("virtual-gamepad-controls", {});
- controlEl.addEventListener("move", setJoystickMoveInput);
- }
- //controlEl.addEventListener("rotateY", setJoystickRotateY);
- //controlEl.addEventListener("rotateX", setJoystickRotateX);
-
- //controlEl.setAttribute('gearvr-controls',{});
- // controlEl.setAttribute('camera', 'near', 0.51);
- //controlEl.setAttribute('position', '0 0 0');
- let cursorEl = document.createElement('a-cursor');
- cursorEl.setAttribute('id', 'cursor-' + avatarName);
- cursorEl.setAttribute('raycaster', {});
- cursorEl.setAttribute('raycaster', 'objects', '.clickable');
- cursorEl.setAttribute('raycaster', 'showLine', false);
- // cursorEl.setAttribute('raycaster', {objects: '.intersectable', showLine: true, far: 100});
- // cursorEl.setAttribute('raycaster', 'showLine', true);
- controlEl.appendChild(cursorEl);
- avatarEl.appendChild(controlEl);
- aScene.appendChild(avatarEl);
- controlEl.setAttribute('camera', 'active', true);
- //avatarEl.setAttribute('avatar', {});
- // let arControl = document.createElement('a-entity');
- // arControl.setAttribute('id', 'arControlParent');
- // arControl.setAttribute('camera', {
- // active: true,
- // "look-controls-enabled": false,
- // "wasd-controls-enabled": false,
- // "user-height": 0
- // });
- // aScene.appendChild(arControl);
- return "OK!"
- // cb();
- // let gearVRControlsEl = document.createElement('a-entity');
- // gearVRControlsEl.setAttribute('id', 'gearvr-'+avatarName);
- // gearVRControlsEl.setAttribute('gearvr-controls', {});
- // aScene.appendChild(gearVRControlsEl);
- // controlEl.addEventListener('componentchanged', function (evt) {
- // if (evt.detail.name === 'position') {
- // var eventParameters = evt.detail.newData;
- // vwf_view.kernel.setProperty(avatarName, "position", [eventParameters.x, eventParameters.y, eventParameters.z]);
- // }
- // if (evt.detail.name === 'rotation') {
- // var eventParameters = evt.detail.newData;
- // vwf_view.kernel.setProperty(avatarName, "rotation", [eventParameters.x, eventParameters.y, eventParameters.z]);
- // }
- // });
- }
- function createXR(nodeID, nodeName) {
- var newNode = {
- "id": nodeName,
- "uri": nodeName,
- "extends": "http://vwf.example.com/aframe/xrcontroller.vwf",
- "properties": {
- }
- }
- if (!self.state.nodes[nodeName]) {
- vwf_view.kernel.createChild(nodeID, nodeName, newNode);
- vwf_view.kernel.callMethod(nodeName, "createController", []);
- //"/../assets/controller/wmrvr.gltf"
- }
- }
- function createGearVRController(nodeID, nodeName) {
- var newNode = {
- "id": nodeName,
- "uri": nodeName,
- "extends": "http://vwf.example.com/aframe/gearvrcontroller.vwf",
- "properties": {
- }
- }
- if (!self.state.nodes[nodeName]) {
- vwf_view.kernel.createChild(nodeID, nodeName, newNode);
- vwf_view.kernel.callMethod(nodeName, "createController", []);
- //"/../assets/controller/gearvr.gltf"
- }
- }
- function postLoadAction(nodeID) {
- //vwf_view.kernel.fireEvent(nodeID, "postLoadAction")
- }
- function createAvatar(nodeID) {
- // vwf_view.kernel.fireEvent(nodeID, "createAvatar");
- var avatarName = 'avatar-' + self.kernel.moniker();
- console.log("creating avatar...");
- // let avatarID = self.kernel.moniker();
- // var nodeName = 'avatar-' + avatarID;
- var newNode = {
- "id": avatarName,
- "uri": avatarName,
- "extends": "http://vwf.example.com/aframe/avatar.vwf",
- "properties": {
- "localUrl": '',
- "remoteUrl": '',
- // "displayName": 'Avatar ' + randId(),
- "sharing": { audio: true, video: true },
- "selectMode": false,
- "position": [0, 1.6, 0]
- }
- }
- if (!self.state.nodes[avatarName]) {
-
- if (_LCSDB.user().is) {
- _LCSDB.user().get('profile').get('alias').once(alias => {
- if (alias){
- newNode.properties.displayName = alias;
- //vwf_view.kernel.callMethod(avatarName, "setMyName", [alias]);
- }
- vwf_view.kernel.createChild(nodeID, avatarName, newNode);
- });
- _LCSDB.user().get('profile').get('avatarNode').not(res=>{
- //vwf_view.kernel.callMethod(avatarName, "createAvatarBody", []);
- })
- _LCSDB.user().get('profile').get('avatarNode').once(res => {
- var myNode = null;
- if (res) {
- //myNode = JSON.parse(res.avatarNode);
- var myNode = res;
- if (_app.helpers.testJSON(res)){
- myNode = JSON.parse(res);
- }
- vwf_view.kernel.callMethod(avatarName, "createAvatarBody", [myNode, null]);
- } else {
- vwf_view.kernel.callMethod(avatarName, "createAvatarBody", []);
- }
- // newNode.properties.displayName = res.alias;
-
-
- //"/../assets/avatars/male/avatar1.gltf"
-
-
- //vwf_view.kernel.callMethod(avatarName, 'setUserAvatar', [res] );
- });
-
-
- } else {
- vwf_view.kernel.createChild(nodeID, avatarName, newNode);
- vwf_view.kernel.callMethod(avatarName, "createAvatarBody", []);
- //"/../assets/avatars/male/avatar1.gltf"
- }
- //
-
- }
- // if(_LCSUSER.is){
- // _LCSUSER.get('profile').get('alias').once(res => {
- // vwf_view.kernel.callMethod(avatarName, 'setUserAvatar', [res] );
- // })
- // }
-
- // let avatarID = self.kernel.moniker();
- // var nodeName = 'avatar-' + avatarID;
- // var newNode = {
- // "id": nodeName,
- // "uri": nodeName,
- // "extends": "http://vwf.example.com/aframe/avatar.vwf"
- // }
- // vwf_view.kernel.createChild(nodeID, nodeName, newNode);
- // vwf_view.kernel.callMethod(nodeName, "createAvatarBody");
- }
- function randId() {
- return '_' + Math.random().toString(36).substr(2, 9);
- }
- function createGearVRControls() {
- let avatarControl = document.querySelector('#avatarControlParent');
- let el = document.createElement('a-entity');
- el.setAttribute('id', 'gearvrcontrol');
- el.setAttribute('gearvr-controls', {
- 'hand': 'right',
- 'model': true
- });
- //el.setAttribute('laser-controls', {hand: "right"});
-
- // gearvr.setAttribute('gearvr-controls', 'hand', 'right');
- el.setAttribute('teleport-controls', {
- cameraRig: '#avatarControlParent',
- teleportOrigin: '#avatarControl',
- startEvents: 'teleportstart',
- endEvents: 'teleportend'
- });
- el.setAttribute('gearvrcontrol', {});
- avatarControl.appendChild(el);
- }
- function createXRControls(hand) {
- let avatarControl = document.querySelector('#avatarControlParent');
- let el = document.createElement('a-entity');
- el.setAttribute('id', 'xrcontroller' + hand);
- el.setAttribute('hand-controls', {
- 'hand': hand,
- 'handModelStyle': 'lowPoly',
- 'color': '#ffcccc'
- });
- el.setAttribute('laser-controls', {hand: hand, model:false});
- //el.setAttribute('raycaster', {objects: ".collidable", far: 5, showLine: false});
- // wmrvr.setAttribute('windows-motion-controls', '');
- // wmrvr.setAttribute('windows-motion-controls', 'hand', hand);
- el.setAttribute('xrcontroller', { 'hand': hand });
- //add teleport controls
- el.setAttribute('teleport-controls', {
- cameraRig: '#avatarControlParent',
- teleportOrigin: '#avatarControl',
- startEvents: 'teleportstart',
- endEvents: 'teleportend'
- });
- avatarControl.appendChild(el);
- }
- function updateMaterial(node) {
- let elID = '#' + node.aframeObj.getAttribute('id');
- Object.entries(self.state.nodes).forEach(el => {
- let material = el[1].aframeObj.getAttribute('material');
- if (material) {
- if (!material.src) {
- let materialID = vwf.find(el[0], 'material');
- self.kernel.callMethod(materialID, "refreshSrc", []);
- }
- else if (material.src) {
- if (material.src !== "") {
- let src = '#' + material.src.id;
- if (src == elID) {
- let materialID = vwf.find(el[0], 'material');
- self.kernel.callMethod(materialID, "updateSrc", [elID])
- }
- }
- }
- }
- })
- }
- });
|