Home Reference Source Repository

js/base/Material.js

  1. 'use strict'
  2.  
  3. import Vector4 from './Vector4'
  4.  
  5. /**
  6. * Material class
  7. * @access public
  8. */
  9. export default class Material {
  10. /**
  11. * constructor
  12. * @access public
  13. * @constructor
  14. * @param {Material} m -
  15. */
  16. constructor(m) {
  17. this.ambient = null
  18. this.diffuse = null
  19. this.specular = null
  20. this.shininess = 1.0
  21. this.emission = null
  22. this.alpha = 1.0
  23. this.toonIndex = 0
  24. this.edge = 0
  25. this.textureFileName = ''
  26. this.sphereFileName = ''
  27. this.toonFileName = ''
  28.  
  29. this.texture = null
  30. this.texture_repeat = true
  31. this.sphere = null
  32. this.toon = null
  33.  
  34. this._ambientCache = null
  35. this._diffuseCache = null
  36. this._specularCache = null
  37. this._emissionCache = null
  38.  
  39. if(m instanceof Material){
  40. this.ambient = new Vector4(m.ambient)
  41. this.diffuse = new Vector4(m.diffuse)
  42. this.specular = new Vector4(m.specular)
  43. this.shininess = m.shininess
  44. this.emission = new Vector4(m.emission)
  45. this.alpha = m.alpha
  46. this.toonIndex = m.toonIndex
  47. this.edge = m.edge
  48. this.textureFileName = m.textureFileName
  49. this.sphereFileName = m.sphereFileName
  50.  
  51. this.texture = m.texture
  52. this.texture_repeat = m.texture_repeat
  53. this.sphere = m.sphere
  54. }
  55. }
  56.  
  57. /**
  58. * get ambient value
  59. * @access public
  60. * @returns {Float32Array} - ambient value
  61. */
  62. getAmbient() {
  63. if(!this._ambientCache){
  64. this._ambientCache = new Float32Array([
  65. this.ambient.x,
  66. this.ambient.y,
  67. this.ambient.z,
  68. this.ambient.w
  69. ])
  70. }
  71. return this._ambientCache
  72. }
  73.  
  74. /**
  75. * get diffuse value
  76. * @access public
  77. * @returns {Float32Array} - diffuse value
  78. */
  79. getDiffuse() {
  80. if(!this._diffuseCache){
  81. this._diffuseCache = new Float32Array([
  82. this.diffuse.x,
  83. this.diffuse.y,
  84. this.diffuse.z,
  85. this.diffuse.w
  86. ])
  87. }
  88. return this._diffuseCache
  89. }
  90.  
  91. /**
  92. * get specular value
  93. * @access public
  94. * @returns {Float32Array} - specular value
  95. */
  96. getSpecular() {
  97. if(!this._specularCache){
  98. this._specularCache = new Float32Array([
  99. this.specular.x,
  100. this.specular.y,
  101. this.specular.z,
  102. this.specular.w
  103. ])
  104. }
  105. return this._specularCache
  106. }
  107.  
  108. /**
  109. * get shininess value
  110. * @access public
  111. * @returns {float} - shininess value
  112. */
  113. getShininess() {
  114. return this.shininess
  115. }
  116.  
  117. /**
  118. * get emission value
  119. * @access public
  120. * @returns {Float32Array} - emission value
  121. */
  122. getEmission() {
  123. if(!this._emissionCache){
  124. this._emissionCache = new Float32Array([
  125. this.emission.x,
  126. this.emission.y,
  127. this.emission.z,
  128. this.emission.w
  129. ])
  130. }
  131. return this._emissionCache
  132. }
  133.  
  134. /**
  135. * get alpha value
  136. * @access public
  137. * @returns {float} - alpha value
  138. */
  139. getAlpha() {
  140. return this.alpha
  141. }
  142.  
  143. /**
  144. * clear cache
  145. * @access public
  146. * @returns {void}
  147. */
  148. clearCache() {
  149. // FIXME
  150. this._ambientCache = null
  151. this._diffuseCache = null
  152. this._specularCache = null
  153. this._emissionCache = null
  154. }
  155.  
  156. }