You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

325 lines
11 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015-2020 Mario Luzeiro <mrluzeiro@ua.pt>
  5. * Copyright (C) 2015-2021 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include "material.h"
  25. #include <3d_math.h>
  26. #include <wx/debug.h>
  27. int MATERIAL::m_defaultRefractionRayCount = 4;
  28. int MATERIAL::m_defaultReflectionRayCount = 3;
  29. int MATERIAL::m_defaultRefractionRecursionCount = 2;
  30. int MATERIAL::m_defaultFeflectionRecursionCount = 3;
  31. // This may be a good value if based on nr of lights
  32. // that contribute to the illumination of that point
  33. #define AMBIENT_FACTOR (1.0f / 6.0f)
  34. #define SPECULAR_FACTOR 1.0f
  35. MATERIAL::MATERIAL()
  36. {
  37. m_ambientColor = SFVEC3F( 0.2f, 0.2f, 0.2f );
  38. m_emissiveColor = SFVEC3F( 0.0f, 0.0f, 0.0f );
  39. m_specularColor = SFVEC3F( 1.0f, 1.0f, 1.0f );
  40. m_reflectivity = 50.2f;
  41. m_transparency = 0.0f; // completely opaque
  42. m_castShadows = true;
  43. m_reflection = 0.0f;
  44. m_absorbance = 1.0f;
  45. m_refractionRayCount = m_defaultRefractionRayCount;
  46. m_reflectionRayCount = m_defaultReflectionRayCount;
  47. m_refractionRecursionCount = m_defaultRefractionRecursionCount;
  48. m_reflectionRecursionCount = m_defaultFeflectionRecursionCount;
  49. m_generator = nullptr;
  50. }
  51. MATERIAL::MATERIAL( const SFVEC3F& aAmbient, const SFVEC3F& aEmissive, const SFVEC3F& aSpecular,
  52. float aShinness, float aTransparency, float aReflection )
  53. {
  54. wxASSERT( aReflection >= 0.0f );
  55. wxASSERT( aReflection <= 1.0f );
  56. wxASSERT( aTransparency >= 0.0f );
  57. wxASSERT( aTransparency <= 1.0f );
  58. wxASSERT( aShinness >= 0.0f );
  59. wxASSERT( aShinness <= 180.0f );
  60. m_ambientColor = aAmbient * SFVEC3F(AMBIENT_FACTOR);
  61. m_emissiveColor = aEmissive;
  62. m_specularColor = aSpecular;
  63. m_reflectivity = aShinness;
  64. m_transparency = glm::clamp( aTransparency, 0.0f, 1.0f );
  65. m_absorbance = 1.0f;
  66. m_reflection = aReflection;
  67. m_castShadows = true;
  68. m_refractionRayCount = m_defaultRefractionRayCount;
  69. m_reflectionRayCount = m_defaultReflectionRayCount;
  70. m_refractionRecursionCount = m_defaultRefractionRecursionCount;
  71. m_reflectionRecursionCount = m_defaultFeflectionRecursionCount;
  72. m_generator = nullptr;
  73. }
  74. void MATERIAL::Generate( SFVEC3F& aNormal, const RAY& aRay, const HITINFO& aHitInfo ) const
  75. {
  76. if( m_generator )
  77. {
  78. aNormal = aNormal + m_generator->Generate( aRay, aHitInfo );
  79. aNormal = glm::normalize( aNormal );
  80. }
  81. }
  82. // https://en.wikipedia.org/wiki/Blinn%E2%80%93Phong_shading_model
  83. SFVEC3F BLINN_PHONG_MATERIAL::Shade( const RAY& aRay, const HITINFO& aHitInfo, float NdotL,
  84. const SFVEC3F& aDiffuseObjColor, const SFVEC3F& aDirToLight,
  85. const SFVEC3F& aLightColor,
  86. float aShadowAttenuationFactor ) const
  87. {
  88. wxASSERT( NdotL >= FLT_EPSILON );
  89. // This is a hack to get some kind of fake ambient illumination
  90. // There is no logic behind this, just pure artistic experimentation
  91. if( aShadowAttenuationFactor > FLT_EPSILON )
  92. {
  93. // Calculate the diffuse light factoring in light color,
  94. // power and the attenuation
  95. const SFVEC3F diffuse = NdotL * aLightColor;
  96. // Calculate the half vector between the light vector and the view vector.
  97. const SFVEC3F H = glm::normalize( aDirToLight - aRay.m_Dir );
  98. //Intensity of the specular light
  99. const float NdotH = glm::dot( H, aHitInfo.m_HitNormal );
  100. const float intensitySpecular = glm::pow( glm::max( NdotH, 0.0f ), m_reflectivity );
  101. return m_ambientColor +
  102. aShadowAttenuationFactor * ( diffuse * aDiffuseObjColor + SPECULAR_FACTOR *
  103. aLightColor * intensitySpecular * m_specularColor );
  104. }
  105. return m_ambientColor;
  106. }
  107. MATERIAL_GENERATOR::MATERIAL_GENERATOR()
  108. {
  109. }
  110. static PerlinNoise s_perlinNoise = PerlinNoise( 0 );
  111. BOARD_NORMAL::BOARD_NORMAL( float aScale ) : MATERIAL_GENERATOR()
  112. {
  113. m_scale = ( 2.0f * glm::pi<float>() ) / aScale;
  114. }
  115. SFVEC3F BOARD_NORMAL::Generate( const RAY& aRay, const HITINFO& aHitInfo ) const
  116. {
  117. const SFVEC3F hitPos = aHitInfo.m_HitPoint * m_scale;
  118. // http://www.fooplot.com/#W3sidHlwZSI6MCwiZXEiOiJzaW4oc2luKHNpbih4KSoxLjkpKjEuNSkiLCJjb2xvciI6IiMwMDAwMDAifSx7InR5cGUiOjEwMDAsIndpbmRvdyI6WyItMC45NjIxMDU3MDgwNzg1MjYyIiwiNy45NzE0MjYyNjc2MDE0MyIsIi0yLjUxNzYyMDM1MTQ4MjQ0OSIsIjIuOTc5OTM3Nzg3Mzk3NTMwMyJdLCJzaXplIjpbNjQ2LDM5Nl19XQ--
  119. // Implement a texture as the "measling crazing blistering" method of FR4
  120. const float x = glm::sin( glm::sin( hitPos.x ) * 1.5f ) * 0.06f;
  121. const float y = glm::sin( glm::sin( hitPos.y ) * 1.5f ) * 0.03f;
  122. const float z = -(x + y) + glm::sin( hitPos.z ) * 0.06f;
  123. const float noise1 = s_perlinNoise.noise( hitPos.x * 1.0f, hitPos.y * 0.7f ) - 0.5f;
  124. const float noise2 = s_perlinNoise.noise( hitPos.x * 0.7f, hitPos.y * 1.0f ) - 0.5f;
  125. const float noise3 = s_perlinNoise.noise( hitPos.x * 0.3f, hitPos.z * 1.0f ) - 0.5f;
  126. return ( SFVEC3F( noise1, noise2, -( noise3 ) ) * 0.3f + SFVEC3F( x, y, z ) );
  127. }
  128. COPPER_NORMAL::COPPER_NORMAL( float aScale, const MATERIAL_GENERATOR* aBoardNormalGenerator )
  129. {
  130. m_board_normal_generator = aBoardNormalGenerator;
  131. m_scale = 1.0f / aScale;
  132. }
  133. SFVEC3F COPPER_NORMAL::Generate( const RAY& aRay, const HITINFO& aHitInfo ) const
  134. {
  135. if( m_board_normal_generator )
  136. {
  137. const SFVEC3F boardNormal = m_board_normal_generator->Generate( aRay, aHitInfo );
  138. SFVEC3F hitPos = aHitInfo.m_HitPoint * m_scale;
  139. const float noise =
  140. ( s_perlinNoise.noise( hitPos.x + boardNormal.y + aRay.m_Origin.x * 0.2f,
  141. hitPos.y + boardNormal.x ) - 0.5f ) * 2.0f;
  142. float scratchPattern =
  143. ( s_perlinNoise.noise( noise + hitPos.x / 100.0f, hitPos.y * 100.0f ) - 0.5f );
  144. const float x = scratchPattern * 0.14f;
  145. const float y = (noise + noise * scratchPattern) * 0.14f;
  146. return SFVEC3F( x, y, - ( x + y ) ) + boardNormal * 0.25f;
  147. }
  148. else
  149. {
  150. return SFVEC3F( 0.0f );
  151. }
  152. }
  153. SOLDER_MASK_NORMAL::SOLDER_MASK_NORMAL( const MATERIAL_GENERATOR* aCopperNormalGenerator )
  154. {
  155. m_copper_normal_generator = aCopperNormalGenerator;
  156. }
  157. SFVEC3F SOLDER_MASK_NORMAL::Generate( const RAY& aRay, const HITINFO& aHitInfo ) const
  158. {
  159. if( m_copper_normal_generator )
  160. {
  161. const SFVEC3F copperNormal = m_copper_normal_generator->Generate( aRay, aHitInfo );
  162. return copperNormal * 0.05f;
  163. }
  164. else
  165. {
  166. return SFVEC3F( 0.0f );
  167. }
  168. }
  169. SFVEC3F PLATED_COPPER_NORMAL::Generate( const RAY& aRay, const HITINFO& aHitInfo ) const
  170. {
  171. SFVEC3F hitPos = aHitInfo.m_HitPoint * m_scale;
  172. const float noise1 = ( s_perlinNoise.noise( hitPos.x, hitPos.y ) - 0.5f );
  173. const float noise2 = ( s_perlinNoise.noise( hitPos.y, hitPos.x ) - 0.5f );
  174. return SFVEC3F( noise1, noise2, -( noise1 + noise2 ) ) * 0.02f;
  175. }
  176. PLASTIC_NORMAL::PLASTIC_NORMAL( float aScale )
  177. {
  178. m_scale = 1.0f / aScale;
  179. }
  180. SFVEC3F PLASTIC_NORMAL::Generate( const RAY& aRay, const HITINFO& aHitInfo ) const
  181. {
  182. const SFVEC3F hitPos = aHitInfo.m_HitPoint * m_scale;
  183. const float noise1 = s_perlinNoise.noise( hitPos.x * 1.0f, hitPos.y * 1.1f,
  184. hitPos.z * 1.2f ) - 0.5f;
  185. const float noise2 = s_perlinNoise.noise( hitPos.x * 1.3f, hitPos.y * 1.0f,
  186. hitPos.z * 1.5f ) - 0.5f;
  187. const float noise3 = s_perlinNoise.noise( hitPos.x * 1.0f, hitPos.y * 1.0f,
  188. hitPos.z * 1.8f ) - 0.5f;
  189. const float distanceReduction = 1.0f / ( aHitInfo.m_tHit + 0.5f );
  190. return SFVEC3F( noise1, noise2, noise3 ) * SFVEC3F( distanceReduction );
  191. }
  192. PLASTIC_SHINE_NORMAL::PLASTIC_SHINE_NORMAL( float aScale )
  193. {
  194. m_scale = 1.0f / aScale;
  195. }
  196. SFVEC3F PLASTIC_SHINE_NORMAL::Generate( const RAY& aRay, const HITINFO& aHitInfo ) const
  197. {
  198. const SFVEC3F hitPos = aHitInfo.m_HitPoint * m_scale;
  199. const float noise1 = s_perlinNoise.noise( hitPos.x * 0.01f, hitPos.y * 0.01f,
  200. hitPos.z * 0.01f ) - 0.5f;
  201. const float noise2 = s_perlinNoise.noise( hitPos.x * 1.0f, hitPos.y * 1.0f,
  202. hitPos.z * 1.6f ) - 0.5f;
  203. float noise3 = s_perlinNoise.noise( hitPos.x * 1.5f, hitPos.y * 1.5f,
  204. hitPos.z * 1.0f ) - 0.5f;
  205. noise3 = noise3 * noise3 * noise3;
  206. return SFVEC3F( noise1, noise2, noise3 ) * SFVEC3F( 0.1f, 0.2f, 1.0f );
  207. }
  208. BRUSHED_METAL_NORMAL::BRUSHED_METAL_NORMAL( float aScale )
  209. {
  210. m_scale = 1.0f / aScale;
  211. }
  212. SFVEC3F BRUSHED_METAL_NORMAL::Generate( const RAY& aRay, const HITINFO& aHitInfo ) const
  213. {
  214. const SFVEC3F hitPos = aHitInfo.m_HitPoint * m_scale;
  215. const float noise1 = s_perlinNoise.noise( hitPos.x * 1.0f, hitPos.y * 1.1f,
  216. hitPos.z * 1.2f ) - 0.5f;
  217. const float noise2 = s_perlinNoise.noise( hitPos.x * 1.3f, hitPos.y * 1.4f,
  218. hitPos.z * 1.5f ) - 0.5f;
  219. const float noise3 = s_perlinNoise.noise( hitPos.x * 0.1f, hitPos.y * 0.1f,
  220. hitPos.z * 1.0f ) - 0.5f;
  221. return SFVEC3F( noise1 * 0.15f + noise3 * 0.35f, noise2 * 0.25f, noise1 * noise2 * noise3 );
  222. }
  223. SILK_SCREEN_NORMAL::SILK_SCREEN_NORMAL( float aScale )
  224. {
  225. m_scale = 1.0f / aScale;
  226. }
  227. SFVEC3F SILK_SCREEN_NORMAL::Generate( const RAY& aRay, const HITINFO& aHitInfo ) const
  228. {
  229. const SFVEC3F hitPos = aHitInfo.m_HitPoint * m_scale;
  230. const float noise1 = s_perlinNoise.noise( hitPos.x * 2.0f, hitPos.y * 2.0f, hitPos.z );
  231. const float noise2 = s_perlinNoise.noise( hitPos.x * 0.6f, hitPos.y * 0.6f, hitPos.z );
  232. SFVEC3F t = SFVEC3F( noise1, noise2, 0.0f ) - 0.5f;
  233. SFVEC3F tt = t * t;
  234. t = t * tt * tt * 100.0f; // this factor controls the intensity of the effect
  235. t.z = 0.0f; // this will keep untouch the original z component of the normal
  236. return t;
  237. }