
AFRAME.registerComponent('heart-material', {
	init: function () {
	
		var baseColor = new THREE.TextureLoader().load( './images/Mat_Heart_baseColor.png' );
		baseColor.flipY = false;
		var normal = new THREE.TextureLoader().load('./images/Mat_Heart_normal.png');
		normal.flipY = false;
		var metalicRough = new THREE.TextureLoader().load('./images/Mat_Heart_occlusionRoughnessMetallic.png');
		metalicRough.flipY = false;
		var envMapRoot = "./images/EnvMap/";
		var envMapImages = [ envMapRoot + "posx.jpg", envMapRoot + "negx.jpg",
								envMapRoot + "posy.jpg", envMapRoot + "negy.jpg",
								envMapRoot + "posz.jpg", envMapRoot + "negz.jpg" ];
		var textureCube = new THREE.CubeTextureLoader().load( envMapImages );
		textureCube.encoding = THREE.sRGBEncoding;
	  
	  this.baseMaterial  = new THREE.MeshStandardMaterial({
		map : baseColor,
		metalness : 0,
		metalnessMap : metalicRough,
		normalMap : normal,
		roughnessMap : metalicRough,
		roughness : 1,
		skinning : true,
		morphTargets : true,
		// morphNormals : true,
		wireframe: false,
		envMap: textureCube,
		envMapIntensity: 1,
		transparent: true
	  });

	  this.crossMaterial  = new THREE.MeshStandardMaterial({
		map : baseColor,
		metalness : 0,
		metalnessMap : metalicRough,
		normalMap : normal,
		roughnessMap : metalicRough,
		roughness : 1,
		skinning : true,
		morphTargets : true,
		// morphNormals : true,
		wireframe: false,
		// envMap: textureCube,
		// envMapIntensity: 1,
		transparent: true
	  });

		// spEnvMap.mapping = THREE.SphericalReflectionMapping;
		// this.newMaterial.envMap = spEnvMap;
		// this.newMaterial.needsUpdate = true;

	  // Wait for model to load.
	  this.el.addEventListener('model-loaded', () => {
		// Grab the mesh / scene.
		const obj = this.el.getObject3D('mesh');
		// Go over the submeshes and modify materials we want.
		obj.traverse(node => {
		  if (node.isMesh && node.name.startsWith("main") || node.name.startsWith("valve")) {
			node.material = this.baseMaterial;
			node.castShadow = true;
		  }
		  else if(node.isMesh && (node.name.startsWith("front") || node.name.startsWith("veins"))) {
			node.material = this.crossMaterial;
			node.castShadow = false;
		  }
		});
	  });
	}
  });