pts.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014-2020 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/livecodingspace/blob/master/LICENSE.md)
  4. */
  5. // VWF & A-Frame view driver
  6. import {Fabric} from '/core/vwf/fabric.js';
  7. class PTSView extends Fabric {
  8. constructor(module) {
  9. console.log("PTSView constructor");
  10. super(module, 'View');
  11. }
  12. factory() {
  13. let _self_ = this;
  14. return this.load(this.module,
  15. {
  16. // == Module Definition ====================================================================
  17. initialize: function (options) {
  18. let self = this;
  19. this.fabric = _self_;
  20. this.nodes = {};
  21. this.state.appInitialized = false;
  22. if (options === undefined) { options = {}; }
  23. if (typeof options == "object") {
  24. this.rootSelector = options["application-root"];
  25. }
  26. else {
  27. this.rootSelector = options;
  28. }
  29. },
  30. createdNode: function (nodeID, childID, childExtendsID, childImplementsIDs,
  31. childSource, childType, childIndex, childName, callback /* ( ready ) */) {
  32. let self = this;
  33. var node = this.state.nodes[childID];
  34. // If the "nodes" object does not have this object in it, it must not be one that
  35. // this driver cares about
  36. if (!node) {
  37. return;
  38. }
  39. if (this.state.scenes[childID]) {
  40. let scene = this.state.scenes[childID];
  41. let space = scene.obj;
  42. space.bindMouse().bindTouch().play();
  43. //TODO: FIX
  44. // let el = document.createElement("space");
  45. // el.setAttribute("id", "space");
  46. // document.querySelector("body").appendChild(el);
  47. // Pts.namespace( window );
  48. //var form = scene.obj.getForm();
  49. //_self_.createAvatar.call(self, childID);
  50. let avatarName = 'avatar-' + self.kernel.moniker();
  51. console.log("creating avatar...");
  52. // let avatarID = self.kernel.moniker();
  53. // var nodeName = 'avatar-' + avatarID;
  54. var newNode = {
  55. "id": avatarName,
  56. "uri": avatarName,
  57. "extends": "proxy/pts/pt.vwf",
  58. "properties": {}
  59. }
  60. if (!self.state.nodes[avatarName]) {
  61. vwf_view.kernel.createChild(childID, avatarName, newNode);
  62. // vwf_view.kernel.callMethod(avatarName, "createPlayer", []);
  63. }
  64. }
  65. if (this.state.nodes[childID] && this.state.nodes[childID].obj) {
  66. this.nodes[childID] = {
  67. id: childID,
  68. extends: childExtendsID,
  69. liveBindings: {}
  70. // lastTransformStep: 0,
  71. // lastAnimationStep: 0
  72. };
  73. if (this.nodes[childID].extends == "proxy/pts/pt.vwf") {
  74. console.log("CREATE PLAYER HERE!!");
  75. let scene = node.scene;
  76. let space = scene.obj;
  77. let form = scene.form;
  78. let color = "09f";
  79. let player = {
  80. myID: childID,
  81. start: (bound) => {},
  82. animate: (time, ftime) => {
  83. //let radius = Num.cycle( (time%1000)/1000 ) * 20;
  84. //form.fill("#09f").point( node.obj, radius, "circle" );
  85. form.fill(color).point( node.obj, 10 );
  86. },
  87. action: (type, x, y) => {
  88. if(type == 'click')
  89. console.log(x, ' - ', y);
  90. if(type == 'over')
  91. console.log(x, ' - ', y);
  92. }
  93. }
  94. space.add(player);
  95. }
  96. }
  97. // if(this.state.nodes[childID]) {
  98. // this.nodes[childID] = {id:childID,extends:childExtendsID};
  99. // }
  100. // else if (this.state.nodes[childID] && this.state.nodes[childID].aframeObj.object3D instanceof THREE.Object3D) {
  101. // this.nodes[childID] = {id:childID,extends:childExtendsID};
  102. // }
  103. },
  104. executed: function (nodeID, scriptText, scriptType) {
  105. let self = this;
  106. let node = this.state.nodes[nodeID];
  107. if (!(node)) {
  108. return;
  109. }
  110. },
  111. initializedNode: function (nodeID, childID) {
  112. let self = this;
  113. var node = this.state.nodes[childID];
  114. if (!node) {
  115. return;
  116. }
  117. },
  118. createdProperty: function (nodeId, propertyName, propertyValue) {
  119. return this.satProperty(nodeId, propertyName, propertyValue);
  120. },
  121. initializedProperty: function (nodeId, propertyName, propertyValue) {
  122. return this.satProperty(nodeId, propertyName, propertyValue);
  123. },
  124. gotProperty: function (nodeId, propertyName, propertyValue) {
  125. var node = this.state.nodes[nodeId];
  126. if (!(node && node.aframeObj)) {
  127. return;
  128. }
  129. },
  130. satProperty: function (nodeId, propertyName, propertyValue) {
  131. let self = this;
  132. var node = this.state.nodes[nodeId];
  133. if (!(node && node.ptsObj)) {
  134. return;
  135. }
  136. },
  137. deletedNode: function (childID) {
  138. delete this.nodes[childID];
  139. },
  140. firedEvent: function (nodeID, eventName, eventParameters) {
  141. let self = this;
  142. var node = this.state.nodes[nodeID];
  143. if (!(node)) {
  144. return;
  145. }
  146. var clientThatSatProperty = self.kernel.client();
  147. var me = self.kernel.moniker();
  148. if (eventName == "changingTransformFromView") {
  149. // If the transform property was initially updated by this view....
  150. if (clientThatSatProperty == me) {
  151. var node = this.state.nodes[nodeID];
  152. node.ignoreNextTransformUpdate = true;
  153. }
  154. }
  155. //var avatarID = vwf_view.kernel.find("", avatarName)
  156. // if (eventName == "postLoadAction") {
  157. // Object.entries(self.state.nodes).forEach(el => {
  158. // if (el[1].prototypes.includes("proxy/aframe/aentity.vwf")) {
  159. // vwf_view.kernel.callMethod(el[0], "setOwner", [self.kernel.moniker()]);
  160. // }
  161. // });
  162. // }
  163. var avatarName = 'avatar-' + self.kernel.moniker();
  164. if (eventName == "clickEvent" ||
  165. eventName == 'mousedownEvent' ||
  166. eventName == 'mouseupEvent'){
  167. if (clientThatSatProperty == me) {
  168. let methodName = eventName +'Method';
  169. self.kernel.callMethod(nodeID, methodName, eventParameters);
  170. if (eventName == "clickEvent"){
  171. let mode = vwf.getProperty(avatarName, 'selectMode');
  172. if (mode) {
  173. console.log("allow to click!!!")
  174. vwf_view.kernel.setProperty(avatarName, 'selectMode', false);
  175. let editorDriver = vwf.views["/drivers/view/editor"];
  176. if (editorDriver) {
  177. let selectSwitch = document.querySelector('#selectNodeSwitch');
  178. // const selectSwitchComp = new mdc.iconButton.MDCIconButtonToggle(selectSwitch); //new mdc.select.MDCIconToggle
  179. selectSwitch._comp.on = false;
  180. let currentNodeDIV = document.querySelector('#currentNode');
  181. if (currentNodeDIV) currentNodeDIV._setNode(nodeID);
  182. }
  183. }
  184. }
  185. }
  186. }
  187. let intersectEvents = ['fromhitstart', 'fromhitend', 'hitstart', 'hitend', 'intersect', 'clearIntersect']; //'intersect',
  188. let hitEvent = intersectEvents.filter(el=> el == eventName.slice(0,-5))[0]; //slice Event word
  189. if (hitEvent)
  190. {
  191. // If the transform property was initially updated by this view....
  192. if (clientThatSatProperty == me) {
  193. let methodName = eventName +'Method';
  194. self.kernel.callMethod(nodeID, methodName, eventParameters);
  195. }
  196. }
  197. // if (eventName == "clickEvent") {
  198. // if (self.kernel.moniker() == eventParameters[0]) {
  199. // let avatar = self.nodes[avatarName];
  200. // let mode = vwf.getProperty(avatarName, 'selectMode');
  201. // vwf_view.kernel.callMethod(nodeID, "clickEventMethod", [])
  202. // }
  203. // }
  204. },
  205. ticked: function (vwfTime) {
  206. let self = this;
  207. _self_.updateAvatarPosition();
  208. // _self_.updateFilters();
  209. //update vr controllers
  210. // if (this.isDesktop){
  211. // _self_.updateDesktopController('mouse-', '#mouse');
  212. // }
  213. //lerpTick ();
  214. },
  215. calledMethod: function (nodeID, methodName, methodParameters, methodValue) {
  216. let self = this;
  217. var node = this.state.nodes[nodeID];
  218. if (!(node && node.obj)) {
  219. return;
  220. }
  221. if (this.nodes[nodeID].extends == "proxy/pts/pt.vwf") {
  222. if(methodName == "createPlayer"){
  223. console.log("CREATE PLAYER HERE!!");
  224. let scene = node.scene;
  225. let space = scene.obj;
  226. let form = scene.form;
  227. let player = {
  228. myID: nodeID,
  229. start: (bound) => {},
  230. animate: (time, ftime) => {
  231. form.point( node.obj, 10 );
  232. },
  233. action: (type, x, y) => {
  234. if(type == 'click')
  235. console.log(x, ' - ', y);
  236. }
  237. }
  238. space.add(player);
  239. }
  240. }
  241. }
  242. });
  243. }
  244. updateAvatarPosition() {
  245. let self = this.instance;
  246. let avatarName = 'avatar-' + self.kernel.moniker();
  247. var node = self.state.nodes[avatarName];
  248. var nodeView = self.state.nodes[avatarName];
  249. if (!node) return;
  250. if (!node.obj) return;
  251. let space = node.scene.obj;
  252. let position = space.pointer;
  253. if(!nodeView.lastPosition){
  254. nodeView.lastPosition = new Pt([position.x, position.y])
  255. }
  256. let lastPosition = nodeView.lastPosition;
  257. if(position && !(position.equals(lastPosition))){
  258. self.kernel.setProperty(avatarName, "x", position.x);
  259. self.kernel.setProperty(avatarName, "y", position.y);
  260. }
  261. nodeView.lastPosition.to([position.x, position.y])
  262. }
  263. updateDesktopController(aName, aSelector) {
  264. let self = this.instance;
  265. //let avatarName = 'avatar-' + self.kernel.moniker();
  266. let delta = 0.001
  267. let avatarID = 'avatar-' + self.kernel.moniker();
  268. let avatarName = aName + self.kernel.moniker();
  269. var node = self.state.nodes[avatarName];
  270. if (!node) return;
  271. if (!node.aframeObj) return;
  272. //let elA = document.querySelector('#avatarControlParent');
  273. let elA = document.querySelector('#avatarControl');
  274. let el = document.querySelector(aSelector);
  275. let xrController = document.querySelector('#' + avatarName);
  276. if (el && elA) {
  277. // let positionC = el.object3D.position.clone();
  278. // let positionA = elA.object3D.position.clone();
  279. // let position = positionC.add(positionA);
  280. let position = elA.object3D.position;
  281. //new THREE.Vector3(elA.object3D.position.x, elA.object3D.position.y-0.05, elA.object3D.position.z);
  282. //let position = elA.object3D.position;
  283. // let mouse = el.components["desktop-controls"]._mouse;
  284. // self.kernel.callMethod(avatarName, "trackMouse",[mouse]);
  285. var rotation = el.getAttribute('rotation');
  286. var headRotation = el.object3D.quaternion;
  287. if (self.isMobile) {
  288. var headWorldQuat = new THREE.Quaternion();
  289. elA.object3D.getWorldQuaternion(headWorldQuat);
  290. rotation = this.getWorldRotation(elA, 'XYZ');
  291. headRotation = headWorldQuat;
  292. }
  293. //let rotation = el.getAttribute('rotation');
  294. //((AFRAME.utils.device.isMobile() && !AFRAME.utils.device.isMobileVR()) || self.isDesktop) ? elA.getAttribute('rotation') : el.getAttribute('rotation');
  295. //let rotation = el.getAttribute('rotation'); //this.getWorldRotation(el, 'YXZ');
  296. let lastRotation = self.nodes[avatarName].selfTickRotation;
  297. let lastPosition = self.nodes[avatarName].selfTickPosition ? self.nodes[avatarName].selfTickPosition: new THREE.Vector3(0, 0, 0);
  298. // let currentPosition = node.aframeObj.getAttribute('position');
  299. //let currentRotation = node.aframeObj.getAttribute('rotation');
  300. if (position && lastPosition) {
  301. let distance = lastPosition.distanceTo(position);
  302. if (distance > delta)
  303. {
  304. // console.log("position not equal");
  305. let idata = el.components["desktop-controls"].intersectionData;
  306. //if(idata) console.log('Point to: ', idata.point, ' intersect ', idata.elID);
  307. self.kernel.setProperty(avatarName, "position", position);
  308. self.kernel.callMethod(avatarName, "moveVRController",[idata]);
  309. }
  310. }
  311. if (rotation && lastRotation) {
  312. let distance = this.compareCoordinates(rotation, lastRotation, delta)
  313. if (distance)
  314. {
  315. //console.log("rotation not equal");
  316. let idata = el.components["desktop-controls"].intersectionData;
  317. //if(idata) console.log('Point to: ', idata.point, ' intersect ', idata.elID);
  318. self.kernel.setProperty(avatarName, "rotation", rotation);
  319. self.kernel.callMethod(avatarName, "moveVRController",[idata]);
  320. self.kernel.callMethod(avatarID, "moveHead", [headRotation]);
  321. }
  322. }
  323. self.nodes[avatarName].selfTickPosition = position.clone();
  324. self.nodes[avatarName].selfTickRotation = Object.assign({}, rotation);
  325. }
  326. }
  327. updateHandControllerVR(aName, aSelector) {
  328. let self = this.instance;
  329. //let avatarName = 'avatar-' + self.kernel.moniker();
  330. let delta = 0.001
  331. let avatarName = aName + self.kernel.moniker();
  332. var node = self.state.nodes[avatarName];
  333. if (!node) return;
  334. if (!node.aframeObj) return;
  335. let elA = document.querySelector('#avatarControlParent');
  336. let el = document.querySelector(aSelector);
  337. if (el && elA) {
  338. let positionC = el.object3D.position.clone();
  339. let positionA = elA.object3D.position.clone();
  340. let position = positionC.add(positionA);
  341. let rotation = el.getAttribute('rotation'); //getWorldRotation(el, 'YXZ');
  342. let lastRotation = self.nodes[avatarName].selfTickRotation;
  343. let lastPosition = self.nodes[avatarName].selfTickPosition ? self.nodes[avatarName].selfTickPosition: new THREE.Vector3(0, 0, 0);
  344. // let currentPosition = node.aframeObj.getAttribute('position');
  345. //let currentRotation = node.aframeObj.getAttribute('rotation');
  346. if (position && lastPosition) {
  347. let distance = lastPosition.distanceTo(position);
  348. if (distance > delta)
  349. {
  350. //let idata = el.components["xrcontroller"].intersectionData;
  351. let intersection = el.components.raycaster.intersections[0];
  352. let point = intersection ? intersection.point : null;
  353. let elID = intersection ? intersection.object.el.id : null;
  354. let idata = point ? {
  355. point: point,
  356. elID: elID
  357. } : null;
  358. // console.log("position not equal");
  359. self.kernel.setProperty(avatarName, "position", position);
  360. self.kernel.callMethod(avatarName, "moveVRController",[idata]);
  361. }
  362. }
  363. if (rotation && lastRotation) {
  364. let distance = this.compareCoordinates(rotation, lastRotation, delta)
  365. if (distance)
  366. {
  367. let intersection = el.components.raycaster.intersections[0];
  368. let point = intersection ? intersection.point : null;
  369. let elID = intersection ? intersection.object.el.id : null;
  370. let idata = point ? {
  371. point: point,
  372. elID: elID
  373. } : null;
  374. //console.log("rotation not equal");
  375. self.kernel.setProperty(avatarName, "rotation", rotation);
  376. self.kernel.callMethod(avatarName, "moveVRController",[idata]);
  377. }
  378. }
  379. // if (position && rotation && lastRotation && lastPosition) {
  380. // if (compareCoordinates(position, lastPosition, delta) || compareCoordinates(rotation, lastRotation, delta)) {
  381. // console.log("not equal!!");
  382. // vwf_view.kernel.callMethod(avatarName, "updateVRControl", [position, rotation]);
  383. // }
  384. // }
  385. //vwf_view.kernel.callMethod(avatarName, "updateVRControl", [position, rotation]);
  386. self.nodes[avatarName].selfTickPosition = position.clone();
  387. self.nodes[avatarName].selfTickRotation = Object.assign({}, rotation);
  388. }
  389. }
  390. getMovementVector(el, vel) {
  391. var directionVector = new THREE.Vector3(0, 0, 0);
  392. var rotationEuler = new THREE.Euler(0, 0, 0, 'YXZ');
  393. var rotation = el.getAttribute('rotation');
  394. var velocity = vel;
  395. directionVector.copy(velocity);
  396. directionVector.multiplyScalar(0.05);
  397. // Absolute.
  398. if (!rotation) { return directionVector; }
  399. //xRotation = this.data.fly ? rotation.x : 0;
  400. // Transform direction relative to heading.
  401. rotationEuler.set(THREE.Math.degToRad(0), THREE.Math.degToRad(rotation.y), 0);
  402. directionVector.applyEuler(rotationEuler);
  403. return directionVector;
  404. }
  405. setJoystickMoveInput (event) {
  406. const axes = event.detail.axis;
  407. //console.log(axes);
  408. let el = document.querySelector('#avatarControl');
  409. let position = new THREE.Vector3();
  410. el.object3D.localToWorld(position);//getWorldPosition(position);
  411. let vel = new THREE.Vector3(axes[0], 0, -axes[1]);
  412. el.object3D.position.add(this.getMovementVector(el,vel));
  413. }
  414. setJoystickRotateY (event) {
  415. const val = event.detail.value;
  416. //console.log(val);
  417. let el = document.querySelector('#avatarControl');
  418. let rotation = el.object3D.rotation;
  419. el.object3D.rotation.y += (-val)+rotation.y;
  420. //el.object3D.rotation.set(rotation.x,-val+rotation.y, rotation.z)
  421. }
  422. setJoystickRotateX (event) {
  423. const val = event.detail.value;
  424. //console.log(val);
  425. let el = document.querySelector('#avatarControl');
  426. let rotation = el.object3D.rotation;
  427. el.object3D.rotation.x += val+rotation.x;
  428. //el.object3D.rotation.set(val+rotation.x, rotation.y, rotation.z)
  429. }
  430. async createAvatarControl(aScene) {
  431. let self = this.instance;
  432. let avatarName = 'avatar-' + self.kernel.moniker();
  433. let avatarEl = document.createElement('a-entity');
  434. avatarEl.setAttribute('id', 'avatarControlParent');
  435. //avatarEl.setAttribute('position', '0 1.6 0');
  436. let controlEl = document.createElement('a-camera');
  437. controlEl.setAttribute('id', 'avatarControl');
  438. controlEl.setAttribute('wasd-controls', {acceleration:20});
  439. controlEl.setAttribute('look-controls', { pointerLockEnabled: false});
  440. controlEl.setAttribute('look-controls', 'enabled', true );
  441. controlEl.setAttribute('camera', 'near', 0.1 );
  442. //controlEl.setAttribute('gamepad-controls', {'controller': 0});
  443. let cursorEl = document.createElement('a-cursor');
  444. cursorEl.setAttribute('id', 'cursor-' + avatarName);
  445. cursorEl.setAttribute('raycaster', {});
  446. cursorEl.setAttribute('raycaster', 'objects', '.clickable');
  447. cursorEl.setAttribute('raycaster', 'showLine', false);
  448. if (self.d3DoF || _app.config.d3DoF) {
  449. //avatarEl.setAttribute('gearvr-controls', {});
  450. avatarEl.setAttribute('movement-controls', {});//{'controls': 'gamepad'});
  451. //avatarEl.setAttribute("gamepad-controls", {});
  452. //avatarEl.setAttribute('position', '0 0 0');
  453. }
  454. else if (AFRAME.utils.device.isMobile()) {
  455. //self.state.showMobileJoystick()
  456. //controlEl.setAttribute('look-controls', 'enabled', true );
  457. controlEl.setAttribute("virtual-gamepad-controls", {});
  458. controlEl.addEventListener("move", this.setJoystickMoveInput.bind(this));
  459. }
  460. //controlEl.addEventListener("rotateY", setJoystickRotateY);
  461. //controlEl.addEventListener("rotateX", setJoystickRotateX);
  462. //controlEl.setAttribute('gearvr-controls',{});
  463. else if (self.isDesktop){
  464. cursorEl.setAttribute('cursor',
  465. {
  466. rayOrigin: 'mouse'
  467. });
  468. cursorEl.setAttribute('visible', false);
  469. }
  470. // cursorEl.setAttribute('raycaster', {objects: '.intersectable', showLine: true, far: 100});
  471. // cursorEl.setAttribute('raycaster', 'showLine', true);
  472. controlEl.appendChild(cursorEl);
  473. avatarEl.appendChild(controlEl);
  474. aScene.appendChild(avatarEl);
  475. controlEl.setAttribute('camera', 'active', true);
  476. //avatarEl.setAttribute('avatar', {});
  477. // let arControl = document.createElement('a-entity');
  478. // arControl.setAttribute('id', 'arControlParent');
  479. // arControl.setAttribute('camera', {
  480. // active: true,
  481. // "look-controls-enabled": false,
  482. // "wasd-controls-enabled": false,
  483. // "user-height": 0
  484. // });
  485. // aScene.appendChild(arControl);
  486. return "OK!"
  487. // cb();
  488. }
  489. createXR(nodeID, nodeName, props) {
  490. let self = this;
  491. var newNode = {
  492. "id": nodeName,
  493. "uri": nodeName,
  494. "extends": "proxy/aframe/xrcontroller.vwf",
  495. "properties": {
  496. }
  497. }
  498. if (!self.state.nodes[nodeName]) {
  499. vwf_view.kernel.createChild(nodeID, nodeName, newNode);
  500. vwf_view.kernel.callMethod(nodeName, "createController", [props.position]);
  501. //"/../assets/controller/wmrvr.gltf"
  502. }
  503. }
  504. createGearVRController(nodeID, nodeName) {
  505. let self = this;
  506. var newNode = {
  507. "id": nodeName,
  508. "uri": nodeName,
  509. "extends": "proxy/aframe/gearvrcontroller.vwf",
  510. "properties": {
  511. }
  512. }
  513. if (!self.state.nodes[nodeName]) {
  514. vwf_view.kernel.createChild(nodeID, nodeName, newNode);
  515. vwf_view.kernel.callMethod(nodeName, "createController", []);
  516. //"/../assets/controller/gearvr.gltf"
  517. }
  518. }
  519. postLoadAction(nodeID) {
  520. //vwf_view.kernel.fireEvent(nodeID, "postLoadAction")
  521. }
  522. createAvatar(nodeID) {
  523. let self = this;
  524. // vwf_view.kernel.fireEvent(nodeID, "createAvatar");
  525. var avatarName = 'avatar-' + self.kernel.moniker();
  526. console.log("creating avatar...");
  527. // let avatarID = self.kernel.moniker();
  528. // var nodeName = 'avatar-' + avatarID;
  529. var newNode = {
  530. "id": avatarName,
  531. "uri": avatarName,
  532. "extends": "proxy/aframe/avatar.vwf",
  533. "properties": {
  534. "localUrl": '',
  535. "remoteUrl": '',
  536. // "displayName": 'Avatar ' + randId(),
  537. "sharing": { audio: true, video: true },
  538. "selectMode": false,
  539. "position": [0, 1.6, 0]
  540. }
  541. }
  542. if (!self.state.nodes[avatarName]) {
  543. if (_LCSDB.user().is) {
  544. _LCSDB.user().get('profile').get('alias').once(alias => {
  545. if (alias){
  546. newNode.properties.displayName = alias;
  547. //vwf_view.kernel.callMethod(avatarName, "setMyName", [alias]);
  548. }
  549. vwf_view.kernel.createChild(nodeID, avatarName, newNode);
  550. });
  551. _LCSDB.user().get('profile').get('avatarNode').not(res=>{
  552. //vwf_view.kernel.callMethod(avatarName, "createAvatarBody", []);
  553. })
  554. _LCSDB.user().get('profile').get('avatarNode').once(res => {
  555. var myNode = null;
  556. if (res) {
  557. //myNode = JSON.parse(res.avatarNode);
  558. var myNode = res;
  559. if (_app.helpers.testJSON(res)){
  560. myNode = JSON.parse(res);
  561. }
  562. vwf_view.kernel.callMethod(avatarName, "createAvatarBody", [myNode, null]);
  563. } else {
  564. vwf_view.kernel.callMethod(avatarName, "createAvatarBody", []);
  565. }
  566. // newNode.properties.displayName = res.alias;
  567. //"/../assets/avatars/male/avatar1.gltf"
  568. //vwf_view.kernel.callMethod(avatarName, 'setUserAvatar', [res] );
  569. });
  570. } else {
  571. vwf_view.kernel.createChild(nodeID, avatarName, newNode);
  572. vwf_view.kernel.callMethod(avatarName, "createAvatarBody", []);
  573. //"/../assets/avatars/male/avatar1.gltf"
  574. }
  575. //
  576. }
  577. }
  578. createGearVRControls() {
  579. let avatarControl = document.querySelector('#avatarControlParent');
  580. let el = document.createElement('a-entity');
  581. el.setAttribute('id', 'gearvrcontrol');
  582. el.setAttribute('gearvr-controls', {
  583. 'hand': 'right',
  584. 'model': true
  585. });
  586. //el.setAttribute('laser-controls', {hand: "right"});
  587. // gearvr.setAttribute('gearvr-controls', 'hand', 'right');
  588. el.setAttribute('teleport-controls', {
  589. cameraRig: '#avatarControlParent',
  590. teleportOrigin: '#avatarControl',
  591. startEvents: 'teleportstart',
  592. endEvents: 'teleportend'
  593. });
  594. el.setAttribute('gearvrcontrol', {});
  595. avatarControl.appendChild(el);
  596. }
  597. createDesktopControls() {
  598. let self = this.instance;
  599. let avatarControl = document.querySelector('#avatarControlParent');
  600. let el = document.createElement('a-entity');
  601. el.setAttribute('id', 'mouse');
  602. // el.setAttribute('geometry', {
  603. // primitive: 'box', width: 0.2, height: 0.2, depth: 1
  604. // });
  605. // el.setAttribute('position', {
  606. // x: 0, y: 0, z: -1
  607. // });
  608. el.setAttribute('desktop-controls', {});
  609. // el.setAttribute('raycaster', {objects: ".intersectable", far: 1000, showLine: true});
  610. avatarControl.appendChild(el);
  611. }
  612. createXRControls(hand) {
  613. let self = this.instance;
  614. let avatarControl = document.querySelector('#avatarControlParent');
  615. let el = document.createElement('a-entity');
  616. el.setAttribute('id', 'xrcontroller' + hand);
  617. el.setAttribute('hand-controls', {
  618. 'hand': hand,
  619. 'handModelStyle': 'lowPoly',
  620. 'color': '#ffcccc'
  621. });
  622. el.setAttribute('laser-controls', {hand: hand, model:false});
  623. //el.setAttribute('raycaster', {objects: ".collidable", far: 5, showLine: false});
  624. // wmrvr.setAttribute('windows-motion-controls', '');
  625. // wmrvr.setAttribute('windows-motion-controls', 'hand', hand);
  626. el.setAttribute('xrcontroller', { 'hand': hand });
  627. //add teleport controls
  628. el.setAttribute('teleport-controls', {
  629. cameraRig: '#avatarControlParent',
  630. teleportOrigin: '#avatarControl',
  631. startEvents: 'teleportstart',
  632. endEvents: 'teleportend'
  633. });
  634. avatarControl.appendChild(el);
  635. }
  636. updateMaterial(node) {
  637. let self = this.instance;
  638. let elID = '#' + node.aframeObj.getAttribute('id');
  639. Object.entries(self.state.nodes).forEach(el => {
  640. let material = el[1].aframeObj.getAttribute('material');
  641. if (material) {
  642. if (!material.src) {
  643. let materialID = vwf.find(el[0], 'material');
  644. self.kernel.callMethod(materialID, "refreshSrc", []);
  645. }
  646. else if (material.src) {
  647. if (material.src !== "") {
  648. let src = '#' + material.src.id;
  649. if (src == elID) {
  650. let materialID = vwf.find(el[0], 'material');
  651. self.kernel.callMethod(materialID, "updateSrc", [elID])
  652. }
  653. }
  654. }
  655. }
  656. })
  657. }
  658. }
  659. export { PTSView as default }