Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

246 lignes
9.2 KiB

  1. import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
  2. import { ModalController } from '@ionic/angular';
  3. import { IJeelizFaceFilter } from 'vendors/jeelizFaceFilter/typescript/JeelizFaceFilterInterfaces';
  4. import { SocialSharing } from '@ionic-native/social-sharing/ngx';
  5. declare var JEELIZFACEFILTER: IJeelizFaceFilter;
  6. declare var JeelizThreeHelper: any;
  7. declare var JeelizResizer: any;
  8. declare var THREE: any;
  9. @Component({
  10. selector: 'app-ar-fan-cam',
  11. templateUrl: './ar-fan-cam.page.html',
  12. styleUrls: ['./ar-fan-cam.page.scss'],
  13. })
  14. export class ArFanCamPage implements OnInit {
  15. @ViewChild('sourceCanvasElement') sourceCanvasElement: ElementRef<HTMLCanvasElement>;
  16. THREECAMERA = null;
  17. width: number;
  18. height: number;
  19. tempImg = '';
  20. glassesFramesMeshLoaded: boolean = false;
  21. glassesLensMeshLoaded: boolean = false;
  22. constructor(
  23. private modalController: ModalController,
  24. private socialSharing: SocialSharing
  25. ) {
  26. }
  27. back() {
  28. JEELIZFACEFILTER.destroy();
  29. this.modalController.dismiss();
  30. }
  31. // AR Face detection and drawing
  32. detect_callback(faceIndex, isDetected) {
  33. if (isDetected) {
  34. console.log('INFO in detect_callback(): DETECTED');
  35. } else {
  36. console.log('INFO in detect_callback(): LOST');
  37. }
  38. }
  39. // build the 3D. called once when Jeeliz Face Filter is OK
  40. init_threeScene(spec) {
  41. const threeStuffs = JeelizThreeHelper.init(spec, this.detect_callback);
  42. // improve WebGLRenderer settings:
  43. threeStuffs.renderer.toneMapping = THREE.ACESFilmicToneMapping;
  44. threeStuffs.renderer.outputEncoding = THREE.sRGBEncoding;
  45. // CREATE THE GLASSES AND ADD THEM
  46. const r = this.JeelizThreeGlassesCreator({
  47. envMapURL: "assets/ar-accessories/envMap.jpg",
  48. frameMeshURL: "assets/ar-accessories/models3D/glassesFramesBranchesBent.json",
  49. lensesMeshURL: "assets/ar-accessories/models3D/glassesLenses.json",
  50. occluderURL: "assets/ar-accessories/models3D/face.json"
  51. });
  52. // vertical offset:
  53. const dy = 0.07;
  54. // create and add the occluder:
  55. r.occluder.rotation.set(0.3, 0, 0);
  56. r.occluder.position.set(0, 0.03 + dy,-0.04);
  57. r.occluder.scale.multiplyScalar(0.0084);
  58. threeStuffs.faceObject.add(r.occluder);
  59. // create and add the glasses mesh:
  60. const threeGlasses = r.glasses;
  61. //threeGlasses.rotation.set(-0.15,0,0); / /X neg -> rotate branches down
  62. threeGlasses.position.set(0, dy, 0.4);
  63. threeGlasses.scale.multiplyScalar(0.006);
  64. threeStuffs.faceObject.add(threeGlasses);
  65. // add a debug cube:
  66. /* const sc = 0.1;
  67. const debugCube = new THREE.Mesh(new THREE.BoxGeometry(sc,sc,sc), new THREE.MeshNormalMaterial());
  68. threeStuffs.faceObject.add(debugCube); //*/
  69. // CREATE THE CAMERA:
  70. this.THREECAMERA = JeelizThreeHelper.create_camera();
  71. }
  72. JeelizThreeGlassesCreator(spec) {
  73. const threeGlasses = new THREE.Object3D();
  74. // envMap texture:
  75. const textureEquirec = new THREE.TextureLoader().load( spec.envMapURL );
  76. textureEquirec.mapping = THREE.EquirectangularReflectionMapping;
  77. textureEquirec.magFilter = THREE.LinearFilter;
  78. textureEquirec.minFilter = THREE.LinearMipMapLinearFilter;
  79. // glasses frames:
  80. new THREE.BufferGeometryLoader().load(spec.frameMeshURL, function(glassesFramesGeometry){
  81. glassesFramesGeometry.computeVertexNormals();
  82. // custom material with fading at the end of the branches:
  83. const us = THREE.ShaderLib.standard.uniforms;
  84. const uniforms = {
  85. roughness: {value: 0},
  86. metalness: {value: 0.05},
  87. reflectivity: {value: 1},
  88. envMap: {value: textureEquirec},
  89. envMapIntensity: {value: 1},
  90. diffuse: {value: new THREE.Color().setHex(0xffffff)},
  91. uBranchFading: {value: new THREE.Vector2(-90, 60)} // first value: position (lower -> to the back), second: transition brutality
  92. };
  93. // tweak vertex shader to give the Z of the current point:
  94. let vertexShaderSource = "varying float vPosZ;\n" + THREE.ShaderLib.standard.vertexShader;
  95. vertexShaderSource = vertexShaderSource.replace('#include <fog_vertex>', 'vPosZ = position.z;');
  96. // tweak fragment shader to apply transparency at the end of the branches:
  97. let fragmentShaderSource = "uniform vec2 uBranchFading;\n varying float vPosZ;\n" + THREE.ShaderLib.standard.fragmentShader;
  98. const GLSLcomputeAlpha = 'gl_FragColor.a = smoothstep(uBranchFading.x - uBranchFading.y*0.5, uBranchFading.x + uBranchFading.y*0.5, vPosZ);'
  99. fragmentShaderSource = fragmentShaderSource.replace('#include <fog_fragment>', GLSLcomputeAlpha);
  100. const mat = new THREE.ShaderMaterial({
  101. vertexShader: vertexShaderSource,
  102. fragmentShader: fragmentShaderSource,
  103. uniforms: uniforms,
  104. flatShading: false,
  105. transparent: true,
  106. extensions: { // fix for https://github.com/jeeliz/jeelizFaceFilter/issues/154
  107. //derivatives: true,
  108. //shaderTextureLOD: true
  109. }
  110. });
  111. mat.envMap = textureEquirec;
  112. const glassesFramesMesh = new THREE.Mesh(glassesFramesGeometry, mat);
  113. threeGlasses.add(glassesFramesMesh);
  114. this.glassesFramesMeshLoaded = true;
  115. // window.debugMatFrames = mat; // to debug the material il the JS console
  116. });
  117. // glasses lenses:
  118. new THREE.BufferGeometryLoader().load(spec.lensesMeshURL, function(glassesLensesGeometry){
  119. glassesLensesGeometry.computeVertexNormals();
  120. const mat = new THREE.MeshBasicMaterial({
  121. envMap: textureEquirec,
  122. opacity: 0.7,
  123. color: new THREE.Color().setHex(0x2233aa),
  124. transparent: true,
  125. fog: false
  126. });
  127. const glassesLensesMesh = new THREE.Mesh(glassesLensesGeometry, mat);
  128. threeGlasses.add(glassesLensesMesh);
  129. this.glassesLensMeshLoaded = true;
  130. // window.debugMatLens = mat; // to debug the material il the JS console
  131. });
  132. const occluderMesh = JeelizThreeHelper.create_threejsOccluder(spec.occluderURL);
  133. return {
  134. glasses: threeGlasses,
  135. occluder: occluderMesh
  136. };
  137. }
  138. main(){
  139. JeelizResizer.size_canvas({
  140. canvasId: 'source-canvas',
  141. isApplyCSS: true,
  142. callback: (isError, bestVideoSettings) => {
  143. this.init_faceFilter(bestVideoSettings);
  144. }
  145. })
  146. }
  147. init_faceFilter(videoSettings){
  148. JEELIZFACEFILTER.init({
  149. followZRot: true,
  150. canvasId: 'source-canvas',
  151. NNCPath: 'assets/ar-accessories/neuralNets/', // root of NN_DEFAULT.json file
  152. maxFacesDetected: 1,
  153. videoSettings: {
  154. ...videoSettings,
  155. idealWidth: window.innerWidth,
  156. idealHeight: window.innerHeight,
  157. maxWidth: window.innerWidth,
  158. maxHeight: window.innerHeight,
  159. },
  160. callbackReady: (errCode, spec) => {
  161. if (errCode){
  162. console.log('AN ERROR HAPPENS. ERR =', errCode);
  163. return;
  164. }
  165. console.log('INFO: JEELIZFACEFILTER IS READY');
  166. this.init_threeScene(spec);
  167. },
  168. // called at each render iteration (drawing loop):
  169. callbackTrack: (detectState) => {
  170. JeelizThreeHelper.render(detectState, this.THREECAMERA);
  171. }
  172. }); //end JEELIZFACEFILTER.init call
  173. }
  174. // End AR Face detection and drawing
  175. async capture() {
  176. const dataURI = this.sourceCanvasElement.nativeElement.toDataURL('image/jpeg');
  177. this.tempImg = dataURI;
  178. }
  179. ngOnInit() {
  180. this.width = window.innerWidth;
  181. this.height = window.innerHeight;
  182. this.main();
  183. }
  184. approveImage() {
  185. this.modalController.dismiss({
  186. imageData: this.tempImg
  187. });
  188. }
  189. shareVia(media: 'WhastApp' | 'Instagram' | 'Facebook' | 'others') {
  190. switch(media) {
  191. case 'WhastApp': this.socialSharing.shareViaWhatsApp('#SaddaPunjab, Download Punjab kings to checkout new filters', this.tempImg, ''); break;
  192. case 'Instagram': this.socialSharing.shareViaInstagram('#SaddaPunjab, Download Punjab kings to checkout new filters', this.tempImg); break;
  193. case 'Facebook': this.socialSharing.shareViaFacebook('#SaddaPunjab, Download Punjab kings to checkout new filters', this.tempImg, ''); break;
  194. case 'others': this.socialSharing.share('#SaddaPunjab, Download Punjab kings to checkout new filters', '', this.tempImg, ''); break;
  195. default: break;
  196. }
  197. }
  198. }