avatar.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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.updateAvatarRotation = function (rotation) {
  278. let myRot = goog.vec.Vec3.clone(this.rotation);
  279. let myHeadRot = goog.vec.Vec3.clone(this.avatarNode.myHead.rotation);
  280. this.rotation = [myRot[0], rotation.y, myRot[2]];
  281. this.avatarNode.myHead.rotation = [rotation.x, myHeadRot[1], rotation.z];
  282. }
  283. this.followAvatarControl = function (position, rotation) {
  284. //this.position = goog.vec.Vec3.createFromValues(position.x, position.y, position.z);
  285. let myRot = goog.vec.Vec3.clone(this.rotation);
  286. let myHeadRot = goog.vec.Vec3.clone(this.avatarNode.myHead.rotation);
  287. //let myBodyRot = goog.vec.Vec3.clone(this.avatarNode.myBody.rotation);
  288. //let myRot = AFRAME.utils.coordinates.parse(this.rotation);
  289. //let myHeadRot = AFRAME.utils.coordinates.parse(this.avatarNode.myHead.rotation);
  290. //let myBodyRot = AFRAME.utils.coordinates.parse(this.avatarNode.myBody.rotation);
  291. // if(myRot[0] == null || myRot[2] == null) {
  292. // debugger
  293. // }
  294. this.rotation = [myRot[0], rotation.y, myRot[2]];
  295. //this.rotation = [myRot.x, rotation.y, myRot.z];
  296. // let myRot = this.avatarBodyModel.rotation;
  297. // this.avatarBodyModel.rotation = [myRot.x, -rotation.y, myRot.z];
  298. //this.avatarBody.rotation = [rotation.x, myRot.y, rotation.z];
  299. //this.avatarNameNode.rotation = [myRot.x, myRot.y, rotation.z];
  300. this.avatarNode.myHead.rotation = [rotation.x, myHeadRot[1], rotation.z];
  301. //this.avatarNode.myHead.rotation = [rotation.x, myHeadRot.y, rotation.z];
  302. // this.avatarNode.myCursor.rotation = [rotation.x, myHeadRot.y, rotation.z];
  303. // this.avatarCamera.rotation = [rotation.x, myHeadRot.y, rotation.z];
  304. }
  305. this.setUserAvatar = function(aName){
  306. this.displayName = aName;
  307. this.avatarNode.myName.value = aName;
  308. }
  309. this.changeCostume = function(val, restore){
  310. let userHeight = -1.6;
  311. var myNameValue = this.displayName;
  312. if (this.avatarNode) {
  313. myNameValue = this.avatarNode.children.myName.properties.value;
  314. this.children.delete(this.avatarNode);
  315. //this.children.delete(this.interpolation);
  316. }
  317. let newNode = Object.assign({}, val);
  318. newNode.properties.position = [0, userHeight, 0];
  319. newNode.properties.visible = true;
  320. newNode.properties.meta = "avatarCostume";
  321. if(!restore)
  322. newNode.children.myName.properties.value = myNameValue;
  323. this.children.create("avatarNode", newNode);
  324. }
  325. this.resetAvatar = function(){
  326. //TODO: add XR check
  327. if (this.avatarNode) {
  328. //myNameValue = this.avatarNode.children.myName.properties.value;
  329. this.children.delete(this.avatarNode);
  330. this.children.delete(this.interpolation);
  331. }
  332. this.createAvatarBody();
  333. }
  334. this.createSimpleAvatar = function(){
  335. if (this.avatarNode.myBody) {
  336. this.avatarNode.children.delete(this.avatarNode.myBody);
  337. }
  338. var myColor = this.getRandomColor();
  339. if (this.avatarNode.myHead){
  340. myColor = this.avatarNode.myHead.visual.material.color;
  341. }
  342. let myBodyDef = this.simpleBodyDef;
  343. myBodyDef.children.material.properties.color = myColor;
  344. this.avatarNode.children.create("myBody", myBodyDef);
  345. this.avatarNode.myHead.visual.properties.visible = true;
  346. }
  347. this.createAvatarFromGLTF = function(modelSrc){
  348. if (this.avatarNode.myBody) {
  349. this.avatarNode.children.delete(this.avatarNode.myBody);
  350. let myBodyDef = this.modelBodyDef;
  351. myBodyDef.properties.src = modelSrc;
  352. this.avatarNode.children.create("myBody", myBodyDef);
  353. this.avatarNode.myHead.visual.properties.visible = false;
  354. this.avatarNode.myHead.myCursor.properties.visible = true;
  355. }
  356. }
  357. this.showHideCursor = function(bool){
  358. this.avatarNode.myHead.myCursor.properties.visible = bool;
  359. }
  360. this.showHideAvatar = function(bool){
  361. this.properties.visible = bool;
  362. }
  363. this.setBigVideoHead = function(val){
  364. this.avatarNode.myHead.visual.height = 4;
  365. this.avatarNode.myHead.visual.width = 3;
  366. this.avatarNode.myBody.visible = false;
  367. }
  368. this.setSmallVideoHead = function(val){
  369. this.avatarNode.myHead.visual.height = 0.5;
  370. this.avatarNode.myHead.visual.width = 0.5;
  371. this.avatarNode.myBody.visible = true;
  372. }
  373. this.setVideoTexture = function(val){
  374. console.log(val);
  375. // this.setSmallVideoHead();
  376. this.avatarNode.myHead.visual.material.color = "white";
  377. this.avatarNode.myHead.visual.material.src = '#temp';
  378. this.avatarNode.myHead.visual.material.src = '#'+val;
  379. }
  380. this.removeVideoTexture = function(){
  381. // this.setSmallVideoHead();
  382. this.avatarNode.myHead.visual.material.color = this.avatarNode.myBody.material.color;
  383. this.avatarNode.myHead.visual.material.src = "";
  384. // this.avatarNode.myHead.visual.src = '#'+val;
  385. }
  386. this.removeSoundWebRTC = function(){
  387. if (this.avatarNode.audio)
  388. this.avatarNode.children.delete(this.avatarNode.audio);
  389. }
  390. this.setSoundWebRTC = function(val){
  391. console.log(val);
  392. if (this.avatarNode.audio) this.removeSoundWebRTC();
  393. var soundNode = {
  394. "extends": "http://vwf.example.com/aframe/aentity.vwf",
  395. "properties": {
  396. },
  397. "children":{
  398. "streamsound":{
  399. "extends": "http://vwf.example.com/aframe/streamSoundComponent.vwf",
  400. "type": "component",
  401. "properties": {
  402. }
  403. }
  404. }
  405. }
  406. this.avatarNode.children.create("audio", soundNode );
  407. // this.setSmallVideoHead();
  408. //this.avatarNode.audio.src = '#tempAudio';
  409. //this.avatarNode.audio.src = '#'+val;
  410. }
  411. this.webrtcTurnOnOff = function(val){
  412. console.log('WEBRTC is ', val);
  413. }
  414. this.webrtcMuteAudio = function(val){
  415. console.log('WEBRTC Audio is ', val);
  416. }
  417. this.webrtcMuteVideo = function(val){
  418. console.log('WEBRTC Video is ', val);
  419. }
  420. this.initialize = function() {
  421. // this.future(0).updateAvatar();
  422. };
  423. this.setMyName = function(val){
  424. this.avatarNode.myName.value = val
  425. }
  426. this.randomizeAvatar = function() {
  427. this.avatarNode.randomize();
  428. }