avatar.js 16 KB

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