monument-app.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. if (typeof AFRAME === 'undefined') {
  2. throw new Error('Component attempted to register before AFRAME was available.');
  3. }
  4. AFRAME.registerComponent('envmap', {
  5. /**
  6. * Creates a new THREE.ShaderMaterial using the two shaders defined
  7. * in vertex.glsl and fragment.glsl.
  8. */
  9. init: function () {
  10. const data = this.data;
  11. //this.applyToMesh();
  12. this.el.addEventListener('model-loaded', () => this.applyToMesh());
  13. },
  14. /**
  15. * Update the ShaderMaterial when component data changes.
  16. */
  17. update: function () {
  18. },
  19. getEnvMap: function () {
  20. var path = './assets/textures/skybox2/';
  21. var format = '.jpg';
  22. var urls = [
  23. path + 'px' + format, path + 'nx' + format,
  24. path + 'py' + format, path + 'ny' + format,
  25. path + 'pz' + format, path + 'nz' + format
  26. ];
  27. envMap = new THREE.CubeTextureLoader().load(urls);
  28. envMap.format = THREE.RGBFormat;
  29. return envMap;
  30. },
  31. /**
  32. * Apply the material to the current entity.
  33. */
  34. applyToMesh: function () {
  35. const mesh = this.el.getObject3D('mesh');
  36. //var scene = mesh;
  37. var envMap = this.getEnvMap();
  38. mesh.traverse(function (node) {
  39. if (node.material) {
  40. node.material.side = THREE.BackSide;
  41. node.material.needsUpdate = true;
  42. //side = THREE.DoubleSide; break;
  43. }
  44. });
  45. mesh.traverse(function (node) {
  46. if (node.material && (node.material.isMeshStandardMaterial ||
  47. (node.material.isShaderMaterial && node.material.envMap !== undefined))) {
  48. node.material.envMap = envMap;
  49. node.material.needsUpdate = true;
  50. }
  51. });
  52. // const mesh = this.el.getObject3D('mesh');
  53. // if (mesh) {
  54. // mesh.material = this.material;
  55. // }
  56. },
  57. /**
  58. * On each frame, update the 'time' uniform in the shaders.
  59. */
  60. tick: function (t) {
  61. }
  62. })
  63. //https://threejs.org/examples/webgl_shaders_sky.html
  64. AFRAME.registerComponent('skyshader', {
  65. init: function () {
  66. let sunSphereEl = document.querySelector('a-scene').querySelector('#sun');
  67. this.sunSphere = sunSphereEl.object3D;
  68. this.sky = new THREE.Sky();
  69. let scene = this.el.sceneEl;
  70. let effectController = {
  71. turbidity: 5,
  72. rayleigh: 2,
  73. mieCoefficient: 0.005,
  74. mieDirectionalG: 0.8,
  75. luminance: 1,
  76. inclination: 0, // elevation / inclination
  77. azimuth: 0.25, // Facing front,
  78. sun: ! true
  79. };
  80. let uniforms = this.sky.uniforms;
  81. uniforms.turbidity.value = effectController.turbidity;
  82. uniforms.rayleigh.value = effectController.rayleigh;
  83. uniforms.luminance.value = effectController.luminance;
  84. uniforms.mieCoefficient.value = effectController.mieCoefficient;
  85. uniforms.mieDirectionalG.value = effectController.mieDirectionalG;
  86. this.el.setObject3D('mesh', this.sky.mesh);
  87. let distance = 400000;
  88. var theta = Math.PI * (effectController.inclination - 0.5);
  89. var phi = 2 * Math.PI * (effectController.azimuth - 0.5);
  90. this.sunSphere.position.x = distance * Math.cos(phi);
  91. this.sunSphere.position.y = distance * Math.sin(phi) * Math.sin(theta);
  92. this.sunSphere.position.z = distance * Math.sin(phi) * Math.cos(theta);
  93. this.sunSphere.visible = effectController.sun;
  94. this.sky.uniforms.sunPosition.value.copy(this.sunSphere.position);
  95. },
  96. update: function () {
  97. },
  98. tick: function (t) {
  99. }
  100. })
  101. AFRAME.registerComponent('sun', {
  102. init: function () {
  103. this.sunSphere = new THREE.Mesh(
  104. new THREE.SphereBufferGeometry(20000, 16, 8),
  105. new THREE.MeshBasicMaterial({ color: 0xffffff })
  106. );
  107. this.sunSphere.position.y = - 700000;
  108. this.sunSphere.visible = true;
  109. this.el.setObject3D('mesh', this.sunSphere);
  110. },
  111. update: function () {
  112. },
  113. tick: function (t) {
  114. }
  115. })
  116. AFRAME.registerComponent('avatarbvh0', {
  117. /**
  118. * Creates a new THREE.ShaderMaterial using the two shaders defined
  119. * in vertex.glsl and fragment.glsl.
  120. */
  121. init: function () {
  122. let skeletonHelper;
  123. let self = this;
  124. this.clock = new THREE.Clock();
  125. let loader = new THREE.BVHLoader();
  126. loader.load("./assets/walk.bvh", function (result) {
  127. skeletonHelper = new THREE.SkeletonHelper(result.skeleton.bones[0]);
  128. skeletonHelper.skeleton = result.skeleton; // allow animation mixer to bind to SkeletonHelper directly
  129. //var material = new THREE.LineBasicMaterial( { vertexColors: THREE.VertexColors, depthTest: false, depthWrite: false, transparent: true } );
  130. skeletonHelper.material.depthTest = true;
  131. skeletonHelper.material.lineWidth = 10.0;
  132. skeletonHelper.material.depthWrite = true;
  133. skeletonHelper.material.transparent = false;
  134. skeletonHelper.material.flatShading = true;
  135. var boneContainer = new THREE.Group();
  136. boneContainer.add(result.skeleton.bones[0]);
  137. self.el.setObject3D('skeletonHelper', skeletonHelper);
  138. self.el.setObject3D('mesh', boneContainer);
  139. //scene.add( skeletonHelper );
  140. // scene.add( boneContainer );
  141. // play animation
  142. self.mixer = new THREE.AnimationMixer(skeletonHelper);
  143. self.mixer.clipAction(result.clip).setEffectiveWeight(1.0).play();
  144. });
  145. },
  146. /**
  147. * Update the ShaderMaterial when component data changes.
  148. */
  149. update: function () {
  150. },
  151. tick: function (t) {
  152. var delta = this.clock.getDelta();
  153. if (this.mixer) this.mixer.update(delta);
  154. }
  155. })
  156. AFRAME.registerComponent('avatarbvh', {
  157. init: function () {
  158. let helper;
  159. let self = this;
  160. this.clock = new THREE.Clock();
  161. console.log(this.el.object3D);
  162. this.el.addEventListener('model-loaded', function (evt) {
  163. const model = evt.detail.model
  164. console.log(model);
  165. // let loader = new THREE.BVHLoader();
  166. // loader.load("./assets/walk.bvh", function (bvh) {
  167. // console.log(model);
  168. // self.mixer = new THREE.AnimationMixer(model.children[0].children[0]);
  169. // self.mixer.clipAction(bvh.clip).setEffectiveWeight(1.0).play();
  170. // })
  171. //console.log(evt);
  172. self.mixer = new THREE.AnimationMixer(model);
  173. self.mixer.clipAction(model.animations[0]).setEffectiveWeight(1.0).play();
  174. })
  175. let loader = new THREE.BVHLoader();
  176. loader.load("./assets/walk.bvh", function (bvh) {
  177. const mesh = self.el.object3D;
  178. //mesh.material.skinning = true;
  179. // mesh.updateMatrixWorld();
  180. // mesh.skeleton.calculateInverses();
  181. // mesh.normalizeSkinWeights ()
  182. // See example from THREE.Skeleton for the armSkeleton
  183. // var rootBone = bvh.skeleton.bones[0]
  184. // mesh.add( rootBone );
  185. // mesh.bind( bvh.skeleton );
  186. // skeletonHelper = new THREE.SkeletonHelper(mesh);
  187. // self.mixer = new THREE.AnimationMixer(mesh );
  188. // self.mixer.clipAction(bvh.clip).setEffectiveWeight(1.0).play();
  189. })
  190. },
  191. update: function () {
  192. },
  193. tick: function (t) {
  194. var delta = this.clock.getDelta();
  195. // update skeletal animcation
  196. if (this.mixer){
  197. this.mixer.update(delta)
  198. }
  199. }
  200. })
  201. AFRAME.registerComponent('avatarbvh_best', {
  202. /**
  203. * Creates a new THREE.ShaderMaterial using the two shaders defined
  204. * in vertex.glsl and fragment.glsl.
  205. */
  206. createBones: function (object, jsonBones) {
  207. /* adapted from the THREE.SkinnedMesh constructor */
  208. // create bone instances from json bone data
  209. const bones = jsonBones.map(gbone => {
  210. bone = new THREE.Bone()
  211. bone.name = gbone.name
  212. bone.position.fromArray(gbone.pos)
  213. bone.quaternion.fromArray(gbone.rotq)
  214. if (gbone.scl !== undefined) bone.scale.fromArray(gbone.scl)
  215. return bone
  216. })
  217. // add bone instances to the root object
  218. jsonBones.forEach((gbone, index) => {
  219. if (gbone.parent !== -1 && gbone.parent !== null && bones[gbone.parent] !== undefined) {
  220. bones[gbone.parent].add(bones[index])
  221. } else {
  222. // object.add(bones[index])
  223. }
  224. })
  225. return bones
  226. },
  227. createSkinnedMesh: function (mesh, skeleton) {
  228. // create SkinnedMesh from static mesh geometry and swap it in the scene graph
  229. const skinnedMesh = new THREE.SkinnedMesh(mesh.geometry, mesh.material)
  230. skinnedMesh.castShadow = true
  231. skinnedMesh.receiveShadow = true
  232. // bind to skeleton
  233. skinnedMesh.bind(skeleton)
  234. // swap mesh for skinned mesh
  235. //mesh.parent.add(skinnedMesh)
  236. // mesh.parent.remove(mesh)
  237. return skinnedMesh
  238. },
  239. init: function () {
  240. let helper;
  241. let self = this;
  242. this.clock = new THREE.Clock();
  243. let loader = new THREE.BVHLoader();
  244. loader.load("./assets/walk.bvh", function (bvh) {
  245. // skeletonHelper = new THREE.SkeletonHelper(result.skeleton.bones[0]);
  246. // skeletonHelper.skeleton = result.skeleton; // allow animation mixer to bind to SkeletonHelper directly
  247. self.bvhData = bvh;
  248. new THREE.GLTFLoader().load("./assets/av2/avatar.gltf", result => {
  249. let scene = result
  250. const mesh = new THREE.SkinnedMesh(
  251. result,
  252. new THREE.MeshPhongMaterial()
  253. );
  254. mesh.material.skinning = true;
  255. //group.add( mesh );
  256. mesh.updateMatrixWorld();
  257. mesh.skeleton.calculateInverses();
  258. mesh.normalizeSkinWeights ()
  259. // See example from THREE.Skeleton for the armSkeleton
  260. var rootBone = self.bvhData.skeleton.bones[0]
  261. mesh.add( rootBone );
  262. // Bind the skeleton to the mesh
  263. mesh.bind( self.bvhData.skeleton );
  264. skeletonHelper = new THREE.SkeletonHelper(mesh);
  265. //skeletonHelper.skeleton = self.bvhData.skeleton;
  266. //var boneContainer = new THREE.Group();
  267. // boneContainer.add(mesh);
  268. // mesh.bind(self.bvhData.skeleton, mesh.matrixWorld)
  269. // const bones = self.createBones(result, result.bones)
  270. // mesh.updateMatrixWorld()
  271. // let skeleton = new THREE.Skeleton(bones, undefined, true)
  272. // mesh.bind(skeleton, mesh.matrixWorld)
  273. // let body = self.createSkinnedMesh(mesh, skeleton)
  274. // skeletonHelper = new THREE.SkeletonHelper(body);
  275. self.el.setObject3D('mesh', mesh)
  276. //self.el.setObject3D('mesh',boneContainer)
  277. self.mixer = new THREE.AnimationMixer(mesh );
  278. self.mixer.clipAction(self.bvhData.clip).setEffectiveWeight(1.0).play();
  279. //self.mixer.clipAction( mesh.geometry.animations[ 0 ] ).play();
  280. // const mesh = new THREE.SkinnedMesh(
  281. // result,
  282. // new THREE.MeshPhongMaterial()
  283. // );
  284. // mesh.material.skinning = true;
  285. // self.el.setObject3D('mesh', mesh)
  286. // self.mixer = new THREE.AnimationMixer( mesh );
  287. // self.mixer.clipAction( mesh.geometry.animations[ 0 ] ).play();
  288. // find armature root object.
  289. // This is a group instance with a userData.bones property
  290. // containing bones in the same format as would normally be
  291. // found on a SkinnedMesh Geometry instance
  292. //let root = scene.getObjectByName('Human')
  293. // manually create bones and parent them to the root object
  294. // NOTE: This is normally done in the SkinnedMesh constructor
  295. // const bones = self.createBones(root, root.userData.bones)
  296. // const bones = bvh.skeleton.bones[0]
  297. // Important! must update world matrices before creating skeleton instance
  298. //result.updateMatrixWorld()
  299. // create skeleton
  300. // let skeleton = new THREE.Skeleton(bones, undefined, true)
  301. // const skinnedMesh = new THREE.SkinnedMesh(result, null)
  302. // skinnedMesh.bind(skeleton, mesh.matrixWorld)
  303. // create SkinnedMesh from static mesh geometry
  304. // let body = self.createSkinnedMesh(root.getObjectByName('Body'), skeleton)
  305. //let body = self.createSkinnedMesh(result, skeleton)
  306. //let clothes = self.createSkinnedMesh(root.getObjectByName('Clothes'), skeleton)
  307. // create skeleton helper
  308. // self.helper = new THREE.SkeletonHelper(root)
  309. // scene.add(self.helper)
  310. // self.el.setObject3D('mesh', scene)
  311. // self.el.setObject3D('mesh', clothes)
  312. //self.el.setObject3D('skeletonHelper', self.helper)
  313. // scene.add( helper )
  314. // skeletal animation
  315. // self.mixer = new THREE.AnimationMixer(root);
  316. //self.mixer.clipAction(scene.animations[0]).play()
  317. // self.mixer.clipAction(bvh.clip).setEffectiveWeight(1.0).play();
  318. // let action = self.mixer.clipAction( scene.animations[ 0 ] ).play();
  319. //action.enabled = true;
  320. // self.mixer = new THREE.AnimationMixer(self.helper);
  321. // self.mixer.clipAction(scene.animations[0]).play();
  322. // start
  323. //animate()
  324. })
  325. })
  326. },
  327. /**
  328. * Update the ShaderMaterial when component data changes.
  329. */
  330. update: function () {
  331. },
  332. tick: function (t) {
  333. var delta = this.clock.getDelta();
  334. // update skeletal animcation
  335. if (this.mixer){
  336. this.mixer.update(delta)
  337. // update skeleton helper
  338. //this.helper.update()
  339. }
  340. }
  341. })