avatar.js 17 KB

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