avatar.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. this.simpleBodyDef = {
  2. "extends": "http://vwf.example.com/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": "http://vwf.example.com/aframe/aMaterialComponent.vwf",
  12. "type": "component",
  13. "properties":{
  14. "color": "white"
  15. }
  16. }
  17. }
  18. }
  19. this.modelBodyDef = {
  20. "extends": "http://vwf.example.com/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": "http://vwf.example.com/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(*,'http://vwf.example.com/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 = goog.vec.Vec3.clone(this.avatarNode.position);
  57. this.avatarNode.position = [position[0], height, position[2]]
  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": "http://vwf.example.com/aframe/aentity.vwf",
  70. "properties": {
  71. "position": [0, userHeight, 0], //-userHeight
  72. "meta": "avatarCostume"
  73. },
  74. "methods": {
  75. "randomize":{
  76. "body":"let myColor = this.getRandomColor(); \n this.myName.color = myColor; \n this.myBody.material.color = myColor; \n this.myHead.visual.material.color = myColor; \n this.myHead.myCursor.vis.material.color = myColor; \n this.myHead.myCursor.line.color = myColor;"
  77. }
  78. },
  79. children: {
  80. "myBody": myBodyDef,
  81. //"myHand": myHandDef,
  82. "myHead": {
  83. "extends": "http://vwf.example.com/aframe/aentity.vwf",
  84. "properties": {
  85. "position": [0, 1.6, 0.7],
  86. "visible": true
  87. },
  88. children: {
  89. "interpolation":
  90. {
  91. "extends": "http://vwf.example.com/aframe/interpolation-component.vwf",
  92. "type": "component",
  93. "properties": {
  94. "enabled": true
  95. }
  96. },
  97. "visual": {
  98. "extends": "http://vwf.example.com/aframe/abox.vwf",
  99. "properties": {
  100. "height": 0.5,
  101. "width": 0.5,
  102. "depth": 0.1,
  103. "visible": true
  104. },
  105. "children": {
  106. "material": {
  107. "extends": "http://vwf.example.com/aframe/aMaterialComponent.vwf",
  108. "type": "component",
  109. "properties":{
  110. "color": myColor
  111. }
  112. }
  113. }
  114. },
  115. "myCamera":
  116. {
  117. //"id": 'camera-' + this.id,
  118. "extends": "http://vwf.example.com/aframe/acamera.vwf",
  119. "properties": {
  120. "position": [0, 0, -0.7],
  121. "look-controls-enabled": false,
  122. "wasd-controls-enabled": false,
  123. "user-height": 0
  124. }
  125. },
  126. "myCursor":
  127. {
  128. //"id": 'myCursor-' + this.id,
  129. "extends": "http://vwf.example.com/aframe/aentity.vwf",
  130. "properties": {},
  131. "children": {
  132. "vis": {
  133. "extends": "http://vwf.example.com/aframe/abox.vwf",
  134. "properties": {
  135. "position": [0, 0, -3],
  136. "height": 0.05,
  137. "width": 0.05,
  138. "depth": 0.01,
  139. "visible": true
  140. },
  141. "children": {
  142. "material": {
  143. "extends": "http://vwf.example.com/aframe/aMaterialComponent.vwf",
  144. "type": "component",
  145. "properties":{
  146. "color": myColor
  147. }
  148. },
  149. "aabb-collider": {
  150. "extends": "http://vwf.example.com/aframe/aabb-collider-component.vwf",
  151. "type": "component",
  152. "properties": {
  153. debug: false,
  154. interval: 10,
  155. objects: ".hit"
  156. }
  157. }
  158. }
  159. },
  160. "line": {
  161. "extends": "http://vwf.example.com/aframe/lineComponent.vwf",
  162. "type": "component",
  163. "properties": {
  164. "start": "0 0 -1",
  165. "end": "0 0 -3",
  166. "color": myColor
  167. }
  168. },
  169. // "realCursor":{
  170. // "extends": "http://vwf.example.com/aframe/acursor.vwf",
  171. // "properties": {
  172. // visible: false
  173. // },
  174. // "children": {
  175. // "raycaster": {
  176. // "extends": "http://vwf.example.com/aframe/raycasterComponent.vwf",
  177. // "type": "component",
  178. // "properties": {
  179. // //recursive: false,
  180. // //interval: 1000,
  181. // far: 100,
  182. // //objects: ".intersectable"
  183. // }
  184. // }
  185. // }
  186. // },
  187. "myRayCaster": {
  188. "extends": "http://vwf.example.com/aframe/aentity.vwf",
  189. "properties": {},
  190. "children": {
  191. "raycaster": {
  192. "extends": "http://vwf.example.com/aframe/raycasterComponent.vwf",
  193. "type": "component",
  194. "properties": {
  195. recursive: false,
  196. interval: 100,
  197. far: 3,
  198. objects: ".intersectable"
  199. }
  200. }
  201. }
  202. },
  203. // "raycaster-listener": {
  204. // "extends": "http://vwf.example.com/aframe/app-raycaster-listener-component.vwf",
  205. // "type": "component"
  206. // }
  207. }
  208. }
  209. }
  210. },
  211. "myName": {
  212. "extends": "http://vwf.example.com/aframe/atext.vwf",
  213. "properties": {
  214. "color": myColor,
  215. "value": this.displayName,
  216. "side": "double",
  217. "rotation": [0, 180, 0],
  218. "position": [0.3, 2.05, 0.5]
  219. }
  220. }
  221. }
  222. }
  223. var newNode = Object.assign({}, defaultNode);
  224. //1. check for default user saved avatar...
  225. if (nodeDef){
  226. newNode = Object.assign({}, nodeDef);
  227. newNode.properties.position = [0, userHeight, 0];
  228. //newNode.children.myName.properties.value = this.displayName;
  229. //newNode = Object.assign(defaultNode, nodeDef);
  230. }
  231. //2. check for default avatar costume in world...
  232. // let defaultWorldCostume = this.findWorldAvatarCostume();
  233. // if(defaultWorldCostume) {
  234. // newNode = Object.assign({}, defaultWorldCostume);
  235. // newNode.properties.visible = true;
  236. // newNode.properties.position = [0, userHeight, 0];
  237. // newNode.children.myName.properties.value = this.displayName;
  238. // }
  239. //3. check for model...
  240. if (modelSrc) {
  241. let myBodyDef = this.modelBodyDef;
  242. myBodyDef.properties.src = modelSrc;
  243. newNode.children.myBody = myBodyDef;
  244. newNode.children.myHead.children.visual.properties.visible = false;
  245. newNode.children.myHead.children.myCursor.properties.visible = true;
  246. }
  247. //let cursor =
  248. //{
  249. // "id": 'cursor-' + this.id,
  250. // "extends": "http://vwf.example.com/aframe/acursor.vwf",
  251. //}
  252. let interpolation = {
  253. "extends": "http://vwf.example.com/aframe/interpolation-component.vwf",
  254. "type": "component",
  255. "properties": {
  256. "enabled": true
  257. }
  258. }
  259. this.children.create( "interpolation", interpolation );
  260. //this.children.create( "myCursor", myCursor );
  261. //this.children.create( "avatarCamera", camera );
  262. // this.children.create( "avatarNameNode", avatarNameNode );
  263. this.children.create("avatarNode", newNode, function(child){
  264. if (!nodeDef) {
  265. child.randomize();
  266. }
  267. });
  268. // this.localUrl = '';
  269. // this.remoteUrl = '';
  270. // this.displayName = this.id;
  271. // this.sharing = { audio: true, video: true };
  272. // this.children.create("avatarNode", newNode);
  273. // this.children.create( "avatarBodyModel", newNodeModel );
  274. //this.interpolation = "50ms";
  275. //vwf_view.kernel.createChild(this.id, "avatarCursor", cursorVis);
  276. }
  277. this.followAvatarControl = function (position, rotation) {
  278. // this.position = AFRAME.utils.coordinates.stringify(position);
  279. // this.rotation = AFRAME.utils.coordinates.stringify(rotation);
  280. //debugger;
  281. // this.position = AFRAME.utils.coordinates.stringify(position);
  282. this.position = goog.vec.Vec3.createFromValues(position.x, position.y, position.z);
  283. let myRot = goog.vec.Vec3.clone(this.rotation);
  284. let myHeadRot = goog.vec.Vec3.clone(this.avatarNode.myHead.rotation);
  285. let myBodyRot = goog.vec.Vec3.clone(this.avatarNode.myBody.rotation);
  286. //let myRot = AFRAME.utils.coordinates.parse(this.rotation);
  287. //let myHeadRot = AFRAME.utils.coordinates.parse(this.avatarNode.myHead.rotation);
  288. //let myBodyRot = AFRAME.utils.coordinates.parse(this.avatarNode.myBody.rotation);
  289. // if(myRot[0] == null || myRot[2] == null) {
  290. // debugger
  291. // }
  292. this.rotation = [myRot[0], rotation.y, myRot[2]];
  293. //this.rotation = [myRot.x, rotation.y, myRot.z];
  294. // let myRot = this.avatarBodyModel.rotation;
  295. // this.avatarBodyModel.rotation = [myRot.x, -rotation.y, myRot.z];
  296. //this.avatarBody.rotation = [rotation.x, myRot.y, rotation.z];
  297. //this.avatarNameNode.rotation = [myRot.x, myRot.y, rotation.z];
  298. this.avatarNode.myHead.rotation = [rotation.x, myHeadRot[1], rotation.z];
  299. //this.avatarNode.myHead.rotation = [rotation.x, myHeadRot.y, rotation.z];
  300. // this.avatarNode.myCursor.rotation = [rotation.x, myHeadRot.y, rotation.z];
  301. // this.avatarCamera.rotation = [rotation.x, myHeadRot.y, rotation.z];
  302. }
  303. this.setUserAvatar = function(aName){
  304. this.displayName = aName;
  305. this.avatarNode.myName.value = aName;
  306. }
  307. this.changeCostume = function(val, restore){
  308. let userHeight = -1.6;
  309. var myNameValue = this.displayName;
  310. if (this.avatarNode) {
  311. myNameValue = this.avatarNode.children.myName.properties.value;
  312. this.children.delete(this.avatarNode);
  313. //this.children.delete(this.interpolation);
  314. }
  315. let newNode = Object.assign({}, val);
  316. newNode.properties.position = [0, userHeight, 0];
  317. newNode.properties.visible = true;
  318. newNode.properties.meta = "avatarCostume";
  319. if(!restore)
  320. newNode.children.myName.properties.value = myNameValue;
  321. this.children.create("avatarNode", newNode);
  322. }
  323. this.resetAvatar = function(){
  324. //TODO: add XR check
  325. if (this.avatarNode) {
  326. //myNameValue = this.avatarNode.children.myName.properties.value;
  327. this.children.delete(this.avatarNode);
  328. this.children.delete(this.interpolation);
  329. }
  330. this.createAvatarBody();
  331. }
  332. this.createSimpleAvatar = function(){
  333. if (this.avatarNode.myBody) {
  334. this.avatarNode.children.delete(this.avatarNode.myBody);
  335. }
  336. var myColor = this.getRandomColor();
  337. if (this.avatarNode.myHead){
  338. myColor = this.avatarNode.myHead.visual.material.color;
  339. }
  340. let myBodyDef = this.simpleBodyDef;
  341. myBodyDef.children.material.properties.color = myColor;
  342. this.avatarNode.children.create("myBody", myBodyDef);
  343. this.avatarNode.myHead.visual.properties.visible = true;
  344. }
  345. this.createAvatarFromGLTF = function(modelSrc){
  346. if (this.avatarNode.myBody) {
  347. this.avatarNode.children.delete(this.avatarNode.myBody);
  348. let myBodyDef = this.modelBodyDef;
  349. myBodyDef.properties.src = modelSrc;
  350. this.avatarNode.children.create("myBody", myBodyDef);
  351. this.avatarNode.myHead.visual.properties.visible = false;
  352. this.avatarNode.myHead.myCursor.properties.visible = true;
  353. }
  354. }
  355. this.showHideCursor = function(bool){
  356. this.avatarNode.myHead.myCursor.properties.visible = bool;
  357. }
  358. this.showHideAvatar = function(bool){
  359. this.properties.visible = bool;
  360. }
  361. this.setBigVideoHead = function(val){
  362. this.avatarNode.myHead.visual.height = 4;
  363. this.avatarNode.myHead.visual.width = 3;
  364. this.avatarNode.myBody.visible = false;
  365. }
  366. this.setSmallVideoHead = function(val){
  367. this.avatarNode.myHead.visual.height = 0.5;
  368. this.avatarNode.myHead.visual.width = 0.5;
  369. this.avatarNode.myBody.visible = true;
  370. }
  371. this.setVideoTexture = function(val){
  372. console.log(val);
  373. // this.setSmallVideoHead();
  374. this.avatarNode.myHead.visual.material.color = "white";
  375. this.avatarNode.myHead.visual.material.src = '#temp';
  376. this.avatarNode.myHead.visual.material.src = '#'+val;
  377. }
  378. this.removeVideoTexture = function(){
  379. // this.setSmallVideoHead();
  380. this.avatarNode.myHead.visual.material.color = this.avatarNode.myBody.material.color;
  381. this.avatarNode.myHead.visual.material.src = "";
  382. // this.avatarNode.myHead.visual.src = '#'+val;
  383. }
  384. this.removeSoundWebRTC = function(){
  385. if (this.avatarNode.audio)
  386. this.avatarNode.children.delete(this.avatarNode.audio);
  387. }
  388. this.setSoundWebRTC = function(val){
  389. console.log(val);
  390. if (this.avatarNode.audio) this.removeSoundWebRTC();
  391. var soundNode = {
  392. "extends": "http://vwf.example.com/aframe/aentity.vwf",
  393. "properties": {
  394. },
  395. "children":{
  396. "streamsound":{
  397. "extends": "http://vwf.example.com/aframe/streamSoundComponent.vwf",
  398. "type": "component",
  399. "properties": {
  400. }
  401. }
  402. }
  403. }
  404. this.avatarNode.children.create("audio", soundNode );
  405. // this.setSmallVideoHead();
  406. //this.avatarNode.audio.src = '#tempAudio';
  407. //this.avatarNode.audio.src = '#'+val;
  408. }
  409. this.webrtcTurnOnOff = function(val){
  410. console.log('WEBRTC is ', val);
  411. }
  412. this.webrtcMuteAudio = function(val){
  413. console.log('WEBRTC Audio is ', val);
  414. }
  415. this.webrtcMuteVideo = function(val){
  416. console.log('WEBRTC Video is ', val);
  417. }
  418. this.initialize = function() {
  419. // this.future(0).updateAvatar();
  420. };
  421. this.setMyName = function(val){
  422. this.avatarNode.myName.value = val
  423. }
  424. this.randomizeAvatar = function() {
  425. this.avatarNode.randomize();
  426. }