material.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*jshint esversion: 6 */
  2. module.exports = (() => {
  3. const createTexture = require("./texture.js");
  4. var texture_mappings = {
  5. diff_color: 1,
  6. normal: 2,
  7. mirror: 8,
  8. diff_intensity: 16,
  9. spec_intensity: 32,
  10. emit: 32,
  11. alpha: 128,
  12. spec_hardness: 256,
  13. ray_mirror: 512,
  14. translucency: 1024,
  15. ambient: 2048,
  16. displacement: 4096,
  17. warp: 8192
  18. };
  19. let blender_specular_types = {
  20. cooktorr: 0,
  21. phong: 1,
  22. blinn: 2,
  23. toon: 3,
  24. wardiso: 4
  25. };
  26. function applyColorMapping(blender_texture, three_texture, material) {
  27. if (blender_texture.mapto & texture_mappings.diff_color) {
  28. material.map = three_texture;
  29. }
  30. }
  31. function applySpecMapping(blender_texture, three_texture, material) {
  32. if (blender_texture.mapto & texture_mappings.spec_color && material.type != "MeshStandardMaterial") {
  33. material.specularMap = three_texture;
  34. }
  35. if (blender_texture.mapto & texture_mappings.spec_intensity && material.type != "MeshStandardMaterial") {
  36. material.roughnessMap = three_texture;
  37. }
  38. }
  39. function applyAlphaMapping(blender_texture, three_texture, material) {
  40. if (blender_texture.mapto & texture_mappings.alpha) {
  41. material.alphaMap = three_texture;
  42. }
  43. }
  44. function applyNormalMapping(blender_texture, three_texture, material) {
  45. if (blender_texture.mapto & texture_mappings.normal) {
  46. material.normalMap = three_texture;
  47. material.normalScale = {
  48. x: blender_texture.norfac,
  49. y: blender_texture.norfac
  50. };
  51. }
  52. }
  53. function applyMirrorMapping(blender_texture, three_texture, material) {
  54. if (blender_texture.mapto & texture_mappings.mirror) {
  55. material.envMap = three_texture;
  56. material.envMapIntensity = blender_texture.mirrfac;
  57. }
  58. }
  59. var blender_texture_coordinates = {
  60. GENERATED : 1,
  61. REFLECTION : 2,
  62. NORMAL:4,
  63. GLOBAL : 8,
  64. UV : 16,
  65. OBJECT : 32,
  66. WINDOW: 1024,
  67. TANGENT:4096,
  68. PARTICLE: 8192,
  69. STRESS:16384
  70. }
  71. var blender_texture_mapping = {
  72. FLAT : 0,
  73. CUBE : 1,
  74. TUBE : 2,
  75. SPHERE : 3
  76. }
  77. function applyTexture(blender_texture, material) {
  78. //extract blender_texture data. Use Only if image has been supplied.
  79. if (blender_texture && blender_texture.tex && blender_texture.tex.ima) {
  80. let three_texture = createTexture(blender_texture.tex.ima);
  81. if(blender_texture.texco == blender_texture_coordinates.REFLECTION){
  82. switch(blender_texture.mapping){
  83. case blender_texture_mapping.FLAT:
  84. three_texture.mapping = THREE.EquirectangularReflectionMapping;
  85. break;
  86. case blender_texture_mapping.SPHERE:
  87. three_texture.mapping = THREE.SphericalReflectionMapping;
  88. break;
  89. }
  90. //three_texture.mapping = THREE.EquirectangularRefractionMapping;
  91. }
  92. applyColorMapping(blender_texture, three_texture, material);
  93. applySpecMapping(blender_texture, three_texture, material);
  94. applyAlphaMapping(blender_texture, three_texture, material);
  95. applyNormalMapping(blender_texture, three_texture, material);
  96. applyMirrorMapping(blender_texture, three_texture, material);
  97. }
  98. }
  99. return function createThreeJSMaterial(blend_mat) {
  100. var material = null;
  101. var textures = blend_mat.mtex;
  102. switch (blend_mat.spec_shader) {
  103. case blender_specular_types.lambert:
  104. material = new THREE.MeshLambertMaterial();
  105. material.color.setRGB(blend_mat.r, blend_mat.g, blend_mat.b);
  106. break;
  107. case blender_specular_types.blinn:
  108. case blender_specular_types.phong:
  109. material = new THREE.MeshStandardMaterial();
  110. material.color.setRGB(blend_mat.r, blend_mat.g, blend_mat.b);
  111. //material.specular.setRGB(blend_mat.specr, blend_mat.specg, blend_mat.specb);
  112. material.roughness = (1 - (blend_mat.har / 512));
  113. material.metalness = 1 - blend_mat.ref;
  114. if(blend_mat.alpha < 0.98){
  115. material.transparent = true;
  116. material.opacity = blend_mat.alpha;
  117. console.log(blend_mat, material)
  118. }
  119. break;
  120. case blender_specular_types.wardiso:
  121. case blender_specular_types.cooktorr:
  122. material = new THREE.MeshPhongMaterial();
  123. material.color.setRGB(blend_mat.r, blend_mat.g, blend_mat.b);
  124. material.specular.setRGB(blend_mat.specr, blend_mat.specg, blend_mat.specb);
  125. material.shininess = blend_mat.har / 512;
  126. material.reflectivity = blend_mat.ref * 100;
  127. break;
  128. default:
  129. material = new THREE.MeshLambertMaterial();
  130. material.color.setRGB(blend_mat.r, blend_mat.g, blend_mat.b);
  131. break;
  132. }
  133. var at = (texture) => applyTexture(texture, material);
  134. if (textures && textures.length) textures.map(at);
  135. return material;
  136. };
  137. })();