avatar.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. this.simpleBodyDef = {
  2. "extends": "proxy/aframe/abox.vwf",
  3. "properties": {
  4. "position": [0, 0.66, 0.7],
  5. "height": 1.3,
  6. "width": 0.65,
  7. "depth": 0.1,
  8. },
  9. "children": {
  10. "material": {
  11. "extends": "proxy/aframe/aMaterialComponent.vwf",
  12. "type": "component",
  13. "properties":{
  14. "color": "white"
  15. }
  16. }
  17. }
  18. }
  19. this.modelBodyDef = {
  20. "extends": "proxy/aframe/agltfmodel.vwf",
  21. "properties": {
  22. "src": "#avatar",
  23. "position": [0, 0, 0.8],
  24. "rotation": [0, 180, 0]
  25. },
  26. "children": {
  27. "animation-mixer": {
  28. "extends": "proxy/aframe/anim-mixer-component.vwf",
  29. "properties": {
  30. "clip": "*",
  31. "duration": 1
  32. }
  33. }
  34. }
  35. }
  36. this.findWorldAvatarCostume = function () {
  37. let scene = this.getScene();
  38. let def = scene.defaultAvatarCostume;
  39. let allChilds = this.find("//element(*,'proxy/aframe/aentity.vwf')"); //this.children
  40. let costumes = allChilds.filter(el => (el.meta == 'avatarCostume'));
  41. let avatarCostume = costumes ? costumes[0]: null;
  42. if (def || avatarCostume) {
  43. var defID;
  44. if (def) {
  45. defID = def.id;
  46. } else if (avatarCostume) {
  47. defID = avatarCostume.id;
  48. }
  49. let avatarNode = _app.helpers.getNodeDef(defID);
  50. return avatarNode
  51. }
  52. return null
  53. }
  54. this.updateYPositionForXR = function(height){
  55. if(this.avatarNode) {
  56. let position = this.avatarNode.position.clone();
  57. this.avatarNode.position = [position.x, height, position.z]
  58. }
  59. }
  60. this.createAvatarBody = function (nodeDef, modelSrc) {
  61. var userHeight = -1.6;
  62. let myColor = "white"; //this.getRandomColor();
  63. let myBodyDef = this.simpleBodyDef;
  64. //let myHandDef = this.simpleVrControllerDef;
  65. if(!this.displayName)
  66. this.displayName = 'Avatar ' + this.random().toString(36).substr(2, 9);
  67. myBodyDef.children.material.properties.color = myColor;
  68. var defaultNode = {
  69. "extends": "proxy/aframe/aentity.vwf",
  70. "properties": {
  71. "position": [0, userHeight, 0], //-userHeight
  72. "meta": "avatarCostume"
  73. },
  74. "methods": {
  75. "randomize":{
  76. "body":`
  77. let myColor = this.getRandomColor();
  78. this.myName.color = myColor;
  79. this.myBody.material.color = myColor;
  80. this.myHead.visual.material.color = myColor;
  81. //this.myHead.myCursor.vis.material.color = myColor;
  82. //this.myHead.myCursor.line.color = myColor;
  83. let cursorEl = this.getScene()['mouse-' + this.parent.id.slice(7)];
  84. cursorEl.xrnode.controller.cursorVisual.avatarColor = myColor;
  85. cursorEl.xrnode.controller.cursorVisual.color = myColor;
  86. `
  87. }
  88. },
  89. children: {
  90. "myBody": myBodyDef,
  91. //"myHand": myHandDef,
  92. "myHead": {
  93. "extends": "proxy/aframe/aentity.vwf",
  94. "properties": {
  95. "position": [0, 1.6, 0.7],
  96. "visible": true
  97. },
  98. children: {
  99. "interpolation":
  100. {
  101. "extends": "proxy/aframe/interpolation-component.vwf",
  102. "type": "component",
  103. "properties": {
  104. "enabled": true
  105. }
  106. },
  107. "visual": {
  108. "extends": "proxy/aframe/abox.vwf",
  109. "properties": {
  110. "height": 0.5,
  111. "width": 0.5,
  112. "depth": 0.1,
  113. "visible": true
  114. },
  115. "children": {
  116. "material": {
  117. "extends": "proxy/aframe/aMaterialComponent.vwf",
  118. "type": "component",
  119. "properties":{
  120. "color": myColor
  121. }
  122. }
  123. }
  124. },
  125. "myCamera":
  126. {
  127. //"id": 'camera-' + this.id,
  128. "extends": "proxy/aframe/acamera.vwf",
  129. "properties": {
  130. "position": [0, 0, -0.7], //userHeight
  131. "look-controls-enabled": false,
  132. "wasd-controls-enabled": false,
  133. "user-height": 0
  134. }
  135. },
  136. // "myCursor":
  137. // {
  138. // //"id": 'myCursor-' + this.id,
  139. // "extends": "proxy/aframe/aentity.vwf",
  140. // "properties": {},
  141. // "children": {
  142. // "vis": {
  143. // "extends": "proxy/aframe/abox.vwf",
  144. // "properties": {
  145. // "position": [0, 0, -3],
  146. // "height": 0.05,
  147. // "width": 0.05,
  148. // "depth": 0.01,
  149. // "visible": true
  150. // },
  151. // "children": {
  152. // "material": {
  153. // "extends": "proxy/aframe/aMaterialComponent.vwf",
  154. // "type": "component",
  155. // "properties":{
  156. // "color": myColor
  157. // }
  158. // },
  159. // "aabb-collider": {
  160. // "extends": "proxy/aframe/aabb-collider-component.vwf",
  161. // "type": "component",
  162. // "properties": {
  163. // debug: false,
  164. // interval: 10,
  165. // objects: ".hit"
  166. // }
  167. // }
  168. // }
  169. // },
  170. // "line": {
  171. // "extends": "proxy/aframe/lineComponent.vwf",
  172. // "type": "component",
  173. // "properties": {
  174. // "start": "0 0 -1",
  175. // "end": "0 0 -3",
  176. // "color": myColor
  177. // }
  178. // },
  179. // // "realCursor":{
  180. // // "extends": "proxy/aframe/acursor.vwf",
  181. // // "properties": {
  182. // // visible: false
  183. // // },
  184. // // "children": {
  185. // // "raycaster": {
  186. // // "extends": "proxy/aframe/raycasterComponent.vwf",
  187. // // "type": "component",
  188. // // "properties": {
  189. // // //recursive: false,
  190. // // //interval: 1000,
  191. // // far: 100,
  192. // // //objects: ".intersectable"
  193. // // }
  194. // // }
  195. // // }
  196. // // },
  197. // "myRayCaster": {
  198. // "extends": "proxy/aframe/aentity.vwf",
  199. // "properties": {},
  200. // "children": {
  201. // "raycaster": {
  202. // "extends": "proxy/aframe/raycasterComponent.vwf",
  203. // "type": "component",
  204. // "properties": {
  205. // recursive: false,
  206. // interval: 100,
  207. // far: 3,
  208. // objects: ".intersectable"
  209. // }
  210. // }
  211. // }
  212. // },
  213. // // "raycaster-listener": {
  214. // // "extends": "proxy/aframe/app-raycaster-listener-component.vwf",
  215. // // "type": "component"
  216. // // }
  217. // }
  218. // }
  219. }
  220. },
  221. // "myHand":{
  222. // "id": "hand-" + this.id,
  223. // "extends": "proxy/aframe/xrcontroller.vwf",
  224. // "properties": {
  225. // "position": [0, 1.2, -0.3]
  226. // }
  227. // },
  228. "myName": {
  229. "extends": "proxy/aframe/atext.vwf",
  230. "properties": {
  231. "color": myColor,
  232. "value": this.displayName,
  233. "side": "double",
  234. "rotation": [0, 180, 0],
  235. "position": [0.3, 2.05, 0.5]
  236. }
  237. }
  238. }
  239. }
  240. var newNode = Object.assign({}, defaultNode);
  241. //1. check for default user saved avatar...
  242. if (nodeDef){
  243. newNode = Object.assign({}, nodeDef);
  244. newNode.properties.position = [0, userHeight, 0];
  245. //newNode.children.myName.properties.value = this.displayName;
  246. //newNode = Object.assign(defaultNode, nodeDef);
  247. }
  248. //2. check for default avatar costume in world...
  249. // let defaultWorldCostume = this.findWorldAvatarCostume();
  250. // if(defaultWorldCostume) {
  251. // newNode = Object.assign({}, defaultWorldCostume);
  252. // newNode.properties.visible = true;
  253. // newNode.properties.position = [0, userHeight, 0];
  254. // newNode.children.myName.properties.value = this.displayName;
  255. // }
  256. //3. check for model...
  257. if (modelSrc) {
  258. let myBodyDef = this.modelBodyDef;
  259. myBodyDef.properties.src = modelSrc;
  260. newNode.children.myBody = myBodyDef;
  261. newNode.children.myHead.children.visual.properties.visible = false;
  262. //newNode.children.myHead.children.myCursor.properties.visible = true;
  263. }
  264. //let cursor =
  265. //{
  266. // "id": 'cursor-' + this.id,
  267. // "extends": "proxy/aframe/acursor.vwf",
  268. //}
  269. let interpolation = {
  270. "extends": "proxy/aframe/interpolation-component.vwf",
  271. "type": "component",
  272. "properties": {
  273. "enabled": true
  274. }
  275. }
  276. this.children.create( "interpolation", interpolation );
  277. //this.children.create( "myCursor", myCursor );
  278. //this.children.create( "avatarCamera", camera );
  279. // this.children.create( "avatarNameNode", avatarNameNode );
  280. this.children.create("avatarNode", newNode, function(child){
  281. if (!nodeDef) {
  282. child.randomize();
  283. }
  284. //child.myHand.createController();
  285. });
  286. // this.localUrl = '';
  287. // this.remoteUrl = '';
  288. // this.displayName = this.id;
  289. // this.sharing = { audio: true, video: true };
  290. // this.children.create("avatarNode", newNode);
  291. // this.children.create( "avatarBodyModel", newNodeModel );
  292. //this.interpolation = "50ms";
  293. //vwf_view.kernel.createChild(this.id, "avatarCursor", cursorVis);
  294. }
  295. this.moveHand = function (rotation) {
  296. this.avatarNode.myHand.rotation = rotation;
  297. this.moveHead(rotation);
  298. }
  299. this.moveHead = function (rotation) {
  300. let newRotation = this.globalToLocalRotation(rotation);
  301. this.avatarNode.myHead.rotation = newRotation;
  302. //let euler = new THREE.Euler().setFromVector3(rotation);
  303. //let euler = new THREE.Euler().setFromVector3(rotation);
  304. // let q = this.localQuaternion().inverse().multiply(rotation); //new THREE.Quaternion().setFromEuler(euler)
  305. // let localEuler = new THREE.Euler().setFromQuaternion(q, 'YXZ');
  306. // this.avatarNode.myHead.rotation = [
  307. // THREE.Math.radToDeg(localEuler.x),
  308. // THREE.Math.radToDeg(localEuler.y),
  309. // THREE.Math.radToDeg(localEuler.z)
  310. // ];
  311. }
  312. this.updateAvatarRotation = function (rotation) {
  313. let myRot = this.rotation.clone();
  314. let myHeadRot = this.avatarNode.myHead.rotation.clone();
  315. this.rotation = [myRot.x, rotation.y, myRot.z];
  316. this.avatarNode.myHead.rotation = [rotation.x, myHeadRot.y, rotation.z];
  317. }
  318. this.followAvatarControl = function (position, rotation) {
  319. let myRot = this.rotation.clone();
  320. let myHeadRot = this.avatarNode.myHead.rotation.clone();
  321. this.rotation = [myRot.x, rotation.y, myRot.z];
  322. this.avatarNode.myHead.rotation = [rotation.x, myHeadRot.y, rotation.z];
  323. }
  324. this.setUserAvatar = function(aName){
  325. this.displayName = aName;
  326. this.avatarNode.myName.value = aName;
  327. }
  328. this.changeCostume = function(val, restore){
  329. let userHeight = -1.6;
  330. var myNameValue = this.displayName;
  331. if (this.avatarNode) {
  332. myNameValue = this.avatarNode.children.myName.properties.value;
  333. this.children.delete(this.avatarNode);
  334. //this.children.delete(this.interpolation);
  335. }
  336. let newNode = Object.assign({}, val);
  337. newNode.properties.position = [0, userHeight, 0];
  338. newNode.properties.visible = true;
  339. newNode.properties.meta = "avatarCostume";
  340. if(!restore)
  341. newNode.children.myName.properties.value = myNameValue;
  342. this.children.create("avatarNode", newNode);
  343. }
  344. this.resetAvatar = function(){
  345. //TODO: add XR check
  346. if (this.avatarNode) {
  347. //myNameValue = this.avatarNode.children.myName.properties.value;
  348. this.children.delete(this.avatarNode);
  349. this.children.delete(this.interpolation);
  350. }
  351. this.createAvatarBody();
  352. }
  353. this.createSimpleAvatar = function(){
  354. if (this.avatarNode.myBody) {
  355. this.avatarNode.children.delete(this.avatarNode.myBody);
  356. }
  357. var myColor = this.getRandomColor();
  358. if (this.avatarNode.myHead){
  359. myColor = this.avatarNode.myHead.visual.material.color;
  360. }
  361. let myBodyDef = this.simpleBodyDef;
  362. myBodyDef.children.material.properties.color = myColor;
  363. this.avatarNode.children.create("myBody", myBodyDef);
  364. this.avatarNode.myHead.visual.properties.visible = true;
  365. }
  366. this.createAvatarFromGLTF = function(modelSrc){
  367. if (this.avatarNode.myBody) {
  368. this.avatarNode.children.delete(this.avatarNode.myBody);
  369. let myBodyDef = this.modelBodyDef;
  370. myBodyDef.properties.src = modelSrc;
  371. this.avatarNode.children.create("myBody", myBodyDef);
  372. this.avatarNode.myHead.visual.properties.visible = false;
  373. //this.avatarNode.myHead.myCursor.properties.visible = true;
  374. }
  375. }
  376. this.showHideCursor = function(bool){
  377. //this.avatarNode.myHead.myCursor.properties.visible = bool;
  378. }
  379. this.showHideAvatar = function(bool){
  380. this.properties.visible = bool;
  381. }
  382. this.setBigVideoHead = function(val){
  383. this.avatarNode.myHead.visual.height = 4;
  384. this.avatarNode.myHead.visual.width = 3;
  385. this.avatarNode.myBody.visible = false;
  386. }
  387. this.setSmallVideoHead = function(val){
  388. this.avatarNode.myHead.visual.height = 0.5;
  389. this.avatarNode.myHead.visual.width = 0.5;
  390. this.avatarNode.myBody.visible = true;
  391. }
  392. this.setVideoTexture = function(val){
  393. console.log(val);
  394. // this.setSmallVideoHead();
  395. this.avatarNode.myHead.visual.material.color = "white";
  396. this.avatarNode.myHead.visual.material.src = '#temp';
  397. this.avatarNode.myHead.visual.material.src = '#'+val;
  398. }
  399. this.removeVideoTexture = function(){
  400. // this.setSmallVideoHead();
  401. this.avatarNode.myHead.visual.material.color = this.avatarNode.myBody.material.color;
  402. this.avatarNode.myHead.visual.material.src = "";
  403. // this.avatarNode.myHead.visual.src = '#'+val;
  404. }
  405. this.removeSoundWebRTC = function(){
  406. if (this.avatarNode.audio)
  407. this.avatarNode.children.delete(this.avatarNode.audio);
  408. }
  409. this.setSoundWebRTC = function(val){
  410. console.log(val);
  411. if (this.avatarNode.audio) this.removeSoundWebRTC();
  412. var soundNode = {
  413. "extends": "proxy/aframe/aentity.vwf",
  414. "properties": {
  415. },
  416. "children":{
  417. "streamsound":{
  418. "extends": "proxy/aframe/streamSoundComponent.vwf",
  419. "type": "component",
  420. "properties": {
  421. }
  422. }
  423. }
  424. }
  425. this.avatarNode.children.create("audio", soundNode );
  426. // this.setSmallVideoHead();
  427. //this.avatarNode.audio.src = '#tempAudio';
  428. //this.avatarNode.audio.src = '#'+val;
  429. }
  430. this.webrtcTurnOnOff = function(val){
  431. console.log('WEBRTC is ', val);
  432. }
  433. this.webrtcMuteAudio = function(val){
  434. console.log('WEBRTC Audio is ', val);
  435. }
  436. this.webrtcMuteVideo = function(val){
  437. console.log('WEBRTC Video is ', val);
  438. }
  439. this.initialize = function() {
  440. // this.future(0).updateAvatar();
  441. };
  442. this.setMyName = function(val){
  443. this.avatarNode.myName.value = val
  444. }
  445. this.randomizeAvatar = function() {
  446. this.avatarNode.randomize();
  447. }