avatar.js 17 KB

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