avatar.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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.updateAvatarBodyRotation = function (rotation) {
  313. let myRot = this.rotation.clone();
  314. this.rotation = [myRot.x, rotation.y, myRot.z];
  315. }
  316. this.updateAvatarRotation = function (rotation) {
  317. let myRot = this.rotation.clone();
  318. let myHeadRot = this.avatarNode.myHead.rotation.clone();
  319. this.rotation = [myRot.x, rotation.y, myRot.z];
  320. this.avatarNode.myHead.rotation = [rotation.x, myHeadRot.y, rotation.z];
  321. }
  322. this.setUserAvatar = function(aName){
  323. this.displayName = aName;
  324. this.avatarNode.myName.value = aName;
  325. }
  326. this.changeCostume = function(val, restore){
  327. let userHeight = -1.6;
  328. var myNameValue = this.displayName;
  329. if (this.avatarNode) {
  330. myNameValue = this.avatarNode.children.myName.properties.value;
  331. this.children.delete(this.avatarNode);
  332. //this.children.delete(this.interpolation);
  333. }
  334. let newNode = Object.assign({}, val);
  335. newNode.properties.position = [0, userHeight, 0];
  336. newNode.properties.visible = true;
  337. newNode.properties.meta = "avatarCostume";
  338. if(!restore)
  339. newNode.children.myName.properties.value = myNameValue;
  340. this.children.create("avatarNode", newNode);
  341. }
  342. this.resetAvatar = function(){
  343. //TODO: add XR check
  344. if (this.avatarNode) {
  345. //myNameValue = this.avatarNode.children.myName.properties.value;
  346. this.children.delete(this.avatarNode);
  347. this.children.delete(this.interpolation);
  348. }
  349. this.createAvatarBody();
  350. }
  351. this.createSimpleAvatar = function(){
  352. if (this.avatarNode.myBody) {
  353. this.avatarNode.children.delete(this.avatarNode.myBody);
  354. }
  355. var myColor = this.getRandomColor();
  356. if (this.avatarNode.myHead){
  357. myColor = this.avatarNode.myHead.visual.material.color;
  358. }
  359. let myBodyDef = this.simpleBodyDef;
  360. myBodyDef.children.material.properties.color = myColor;
  361. this.avatarNode.children.create("myBody", myBodyDef);
  362. this.avatarNode.myHead.visual.properties.visible = true;
  363. }
  364. this.createAvatarFromGLTF = function(modelSrc){
  365. if (this.avatarNode.myBody) {
  366. this.avatarNode.children.delete(this.avatarNode.myBody);
  367. let myBodyDef = this.modelBodyDef;
  368. myBodyDef.properties.src = modelSrc;
  369. this.avatarNode.children.create("myBody", myBodyDef);
  370. this.avatarNode.myHead.visual.properties.visible = false;
  371. //this.avatarNode.myHead.myCursor.properties.visible = true;
  372. }
  373. }
  374. this.showHideCursor = function(bool){
  375. //this.avatarNode.myHead.myCursor.properties.visible = bool;
  376. }
  377. this.showHideAvatar = function(bool){
  378. this.properties.visible = bool;
  379. }
  380. this.setBigVideoHead = function(val){
  381. this.avatarNode.myHead.visual.height = 4;
  382. this.avatarNode.myHead.visual.width = 3;
  383. this.avatarNode.myBody.visible = false;
  384. }
  385. this.setSmallVideoHead = function(val){
  386. this.avatarNode.myHead.visual.height = 0.5;
  387. this.avatarNode.myHead.visual.width = 0.5;
  388. this.avatarNode.myBody.visible = true;
  389. }
  390. this.setVideoTexture = function(val){
  391. console.log(val);
  392. // this.setSmallVideoHead();
  393. this.avatarNode.myHead.visual.material.color = "white";
  394. this.avatarNode.myHead.visual.material.src = '#temp';
  395. this.avatarNode.myHead.visual.material.src = '#'+val;
  396. }
  397. this.removeVideoTexture = function(){
  398. // this.setSmallVideoHead();
  399. this.avatarNode.myHead.visual.material.color = this.avatarNode.myBody.material.color;
  400. this.avatarNode.myHead.visual.material.src = "";
  401. // this.avatarNode.myHead.visual.src = '#'+val;
  402. }
  403. this.removeSoundWebRTC = function(){
  404. if (this.avatarNode.audio)
  405. this.avatarNode.children.delete(this.avatarNode.audio);
  406. }
  407. this.setSoundWebRTC = function(val){
  408. console.log(val);
  409. if (this.avatarNode.audio) this.removeSoundWebRTC();
  410. var soundNode = {
  411. "extends": "proxy/aframe/aentity.vwf",
  412. "properties": {
  413. },
  414. "children":{
  415. "streamsound":{
  416. "extends": "proxy/aframe/streamSoundComponent.vwf",
  417. "type": "component",
  418. "properties": {
  419. }
  420. }
  421. }
  422. }
  423. this.avatarNode.children.create("audio", soundNode );
  424. // this.setSmallVideoHead();
  425. //this.avatarNode.audio.src = '#tempAudio';
  426. //this.avatarNode.audio.src = '#'+val;
  427. }
  428. this.webrtcTurnOnOff = function(val){
  429. console.log('WEBRTC is ', val);
  430. }
  431. this.webrtcMuteAudio = function(val){
  432. console.log('WEBRTC Audio is ', val);
  433. }
  434. this.webrtcMuteVideo = function(val){
  435. console.log('WEBRTC Video is ', val);
  436. }
  437. this.initialize = function() {
  438. // this.future(0).updateAvatar();
  439. };
  440. this.setMyName = function(val){
  441. this.avatarNode.myName.value = val
  442. }
  443. this.randomizeAvatar = function() {
  444. this.avatarNode.randomize();
  445. }