babylon.customMaterial.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON {
  3. export class CustomShaderHelper{
  4. }
  5. export interface ICustomMaterialBuilder {
  6. (builder:CustomShaderHelper , name: string ,mainPart: string , diffusePart:string ,vertexPositionPart:string ): string;
  7. }
  8. export class CustomMaterial extends StandardMaterial {
  9. public builder: ICustomMaterialBuilder;
  10. private _mainPart = 'void main(void) {';
  11. private _diffusePart = 'vec3 diffuseColor=vDiffuseColor.rgb;';
  12. private _vertexPositionPart = 'gl_Position=viewProjection*finalWorld*vec4(position,1.0);';
  13. constructor (name: string, builder:ICustomMaterialBuilder, scene: Scene) {
  14. super(name, scene);
  15. this.builder = builder;
  16. }
  17. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  18. if (this.isFrozen) {
  19. if (this._wasPreviouslyReady) {
  20. return true;
  21. }
  22. }
  23. var scene = this.getScene();
  24. var engine = scene.getEngine();
  25. var needUVs = false;
  26. var needNormals = false;
  27. this._defines.reset();
  28. // Lights
  29. if (scene.lightsEnabled && !this.disableLighting) {
  30. needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines, this.maxSimultaneousLights);
  31. }
  32. if (!this.checkReadyOnEveryCall) {
  33. if (this._renderId === scene.getRenderId()) {
  34. if (this._checkCache(scene, mesh, useInstances)) {
  35. return true;
  36. }
  37. }
  38. }
  39. // Textures
  40. if (scene.texturesEnabled) {
  41. if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  42. if (!this.diffuseTexture.isReady()) {
  43. return false;
  44. } else {
  45. needUVs = true;
  46. this._defines.DIFFUSE = true;
  47. }
  48. }
  49. if (this.ambientTexture && StandardMaterial.AmbientTextureEnabled) {
  50. if (!this.ambientTexture.isReady()) {
  51. return false;
  52. } else {
  53. needUVs = true;
  54. this._defines.AMBIENT = true;
  55. }
  56. }
  57. if (this.opacityTexture && StandardMaterial.OpacityTextureEnabled) {
  58. if (!this.opacityTexture.isReady()) {
  59. return false;
  60. } else {
  61. needUVs = true;
  62. this._defines.OPACITY = true;
  63. if (this.opacityTexture.getAlphaFromRGB) {
  64. this._defines.OPACITYRGB = true;
  65. }
  66. }
  67. }
  68. if (this.reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  69. if (!this.reflectionTexture.isReady()) {
  70. return false;
  71. } else {
  72. needNormals = true;
  73. this._defines.REFLECTION = true;
  74. if (this.roughness > 0) {
  75. this._defines.ROUGHNESS = true;
  76. }
  77. if (this.useReflectionOverAlpha) {
  78. this._defines.REFLECTIONOVERALPHA = true;
  79. }
  80. if (this.reflectionTexture.coordinatesMode === Texture.INVCUBIC_MODE) {
  81. this._defines.INVERTCUBICMAP = true;
  82. }
  83. this._defines.REFLECTIONMAP_3D = this.reflectionTexture.isCube;
  84. switch (this.reflectionTexture.coordinatesMode) {
  85. case Texture.CUBIC_MODE:
  86. case Texture.INVCUBIC_MODE:
  87. this._defines.REFLECTIONMAP_CUBIC = true;
  88. break;
  89. case Texture.EXPLICIT_MODE:
  90. this._defines.REFLECTIONMAP_EXPLICIT = true;
  91. break;
  92. case Texture.PLANAR_MODE:
  93. this._defines.REFLECTIONMAP_PLANAR = true;
  94. break;
  95. case Texture.PROJECTION_MODE:
  96. this._defines.REFLECTIONMAP_PROJECTION = true;
  97. break;
  98. case Texture.SKYBOX_MODE:
  99. this._defines.REFLECTIONMAP_SKYBOX = true;
  100. break;
  101. case Texture.SPHERICAL_MODE:
  102. this._defines.REFLECTIONMAP_SPHERICAL = true;
  103. break;
  104. case Texture.EQUIRECTANGULAR_MODE:
  105. this._defines.REFLECTIONMAP_EQUIRECTANGULAR = true;
  106. break;
  107. case Texture.FIXED_EQUIRECTANGULAR_MODE:
  108. this._defines.REFLECTIONMAP_EQUIRECTANGULAR_FIXED = true;
  109. break;
  110. }
  111. }
  112. }
  113. if (this.emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
  114. if (!this.emissiveTexture.isReady()) {
  115. return false;
  116. } else {
  117. needUVs = true;
  118. this._defines.EMISSIVE = true;
  119. }
  120. }
  121. if (this.lightmapTexture && StandardMaterial.LightmapTextureEnabled) {
  122. if (!this.lightmapTexture.isReady()) {
  123. return false;
  124. } else {
  125. needUVs = true;
  126. this._defines.LIGHTMAP = true;
  127. this._defines.USELIGHTMAPASSHADOWMAP = this.useLightmapAsShadowmap;
  128. }
  129. }
  130. if (this.specularTexture && StandardMaterial.SpecularTextureEnabled) {
  131. if (!this.specularTexture.isReady()) {
  132. return false;
  133. } else {
  134. needUVs = true;
  135. this._defines.SPECULAR = true;
  136. this._defines.GLOSSINESS = this.useGlossinessFromSpecularMapAlpha;
  137. }
  138. }
  139. if (scene.getEngine().getCaps().standardDerivatives && this.bumpTexture && StandardMaterial.BumpTextureEnabled) {
  140. if (!this.bumpTexture.isReady()) {
  141. return false;
  142. } else {
  143. needUVs = true;
  144. this._defines.BUMP = true;
  145. if (this.useParallax) {
  146. this._defines.PARALLAX = true;
  147. if (this.useParallaxOcclusion) {
  148. this._defines.PARALLAXOCCLUSION = true;
  149. }
  150. }
  151. if (this.invertNormalMapX) {
  152. this._defines.INVERTNORMALMAPX = true;
  153. }
  154. if (this.invertNormalMapY) {
  155. this._defines.INVERTNORMALMAPY = true;
  156. }
  157. if (scene._mirroredCameraPosition) {
  158. this._defines.INVERTNORMALMAPX = !this._defines.INVERTNORMALMAPX;
  159. this._defines.INVERTNORMALMAPY = !this._defines.INVERTNORMALMAPY;
  160. }
  161. }
  162. }
  163. if (this.refractionTexture && StandardMaterial.RefractionTextureEnabled) {
  164. if (!this.refractionTexture.isReady()) {
  165. return false;
  166. } else {
  167. needUVs = true;
  168. this._defines.REFRACTION = true;
  169. this._defines.REFRACTIONMAP_3D = this.refractionTexture.isCube;
  170. }
  171. }
  172. if (this.cameraColorGradingTexture && StandardMaterial.ColorGradingTextureEnabled) {
  173. if (!this.cameraColorGradingTexture.isReady()) {
  174. return false;
  175. } else {
  176. this._defines.CAMERACOLORGRADING = true;
  177. }
  178. }
  179. }
  180. // Effect
  181. if (scene.clipPlane) {
  182. this._defines.CLIPPLANE = true;
  183. }
  184. if (engine.getAlphaTesting()) {
  185. this._defines.ALPHATEST = true;
  186. }
  187. if (this._shouldUseAlphaFromDiffuseTexture()) {
  188. this._defines.ALPHAFROMDIFFUSE = true;
  189. }
  190. if (this.useEmissiveAsIllumination) {
  191. this._defines.EMISSIVEASILLUMINATION = true;
  192. }
  193. if (this.linkEmissiveWithDiffuse) {
  194. this._defines.LINKEMISSIVEWITHDIFFUSE = true;
  195. }
  196. if (this.useLogarithmicDepth) {
  197. this._defines.LOGARITHMICDEPTH = true;
  198. }
  199. if (this.cameraColorCurves) {
  200. this._defines.CAMERACOLORCURVES = true;
  201. }
  202. // Point size
  203. if (this.pointsCloud || scene.forcePointsCloud) {
  204. this._defines.POINTSIZE = true;
  205. }
  206. // Fog
  207. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) {
  208. this._defines.FOG = true;
  209. }
  210. if (StandardMaterial.FresnelEnabled) {
  211. // Fresnel
  212. if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled ||
  213. this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled ||
  214. this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled ||
  215. this.refractionFresnelParameters && this.refractionFresnelParameters.isEnabled ||
  216. this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
  217. if (this.diffuseFresnelParameters && this.diffuseFresnelParameters.isEnabled) {
  218. this._defines.DIFFUSEFRESNEL = true;
  219. }
  220. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  221. this._defines.OPACITYFRESNEL = true;
  222. }
  223. if (this.reflectionFresnelParameters && this.reflectionFresnelParameters.isEnabled) {
  224. this._defines.REFLECTIONFRESNEL = true;
  225. if (this.useReflectionFresnelFromSpecular) {
  226. this._defines.REFLECTIONFRESNELFROMSPECULAR = true;
  227. }
  228. }
  229. if (this.refractionFresnelParameters && this.refractionFresnelParameters.isEnabled) {
  230. this._defines.REFRACTIONFRESNEL = true;
  231. }
  232. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  233. this._defines.EMISSIVEFRESNEL = true;
  234. }
  235. needNormals = true;
  236. this._defines.FRESNEL = true;
  237. }
  238. }
  239. if (this._defines.SPECULARTERM && this.useSpecularOverAlpha) {
  240. this._defines.SPECULAROVERALPHA = true;
  241. }
  242. // Attribs
  243. if (mesh) {
  244. if (needNormals && mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  245. this._defines.NORMAL = true;
  246. }
  247. if (needUVs) {
  248. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  249. this._defines.UV1 = true;
  250. }
  251. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  252. this._defines.UV2 = true;
  253. }
  254. }
  255. if (mesh.useVertexColors && mesh.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  256. this._defines.VERTEXCOLOR = true;
  257. if (mesh.hasVertexAlpha) {
  258. this._defines.VERTEXALPHA = true;
  259. }
  260. }
  261. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  262. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  263. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  264. }
  265. // Instances
  266. if (useInstances) {
  267. this._defines.INSTANCES = true;
  268. }
  269. }
  270. // Get correct effect
  271. if (!this._defines.isEqual(this._cachedDefines)) {
  272. this._defines.cloneTo(this._cachedDefines);
  273. scene.resetCachedMaterial();
  274. // Fallbacks
  275. var fallbacks = new EffectFallbacks();
  276. if (this._defines.REFLECTION) {
  277. fallbacks.addFallback(0, "REFLECTION");
  278. }
  279. if (this._defines.SPECULAR) {
  280. fallbacks.addFallback(0, "SPECULAR");
  281. }
  282. if (this._defines.BUMP) {
  283. fallbacks.addFallback(0, "BUMP");
  284. }
  285. if (this._defines.PARALLAX) {
  286. fallbacks.addFallback(1, "PARALLAX");
  287. }
  288. if (this._defines.PARALLAXOCCLUSION) {
  289. fallbacks.addFallback(0, "PARALLAXOCCLUSION");
  290. }
  291. if (this._defines.SPECULAROVERALPHA) {
  292. fallbacks.addFallback(0, "SPECULAROVERALPHA");
  293. }
  294. if (this._defines.FOG) {
  295. fallbacks.addFallback(1, "FOG");
  296. }
  297. if (this._defines.POINTSIZE) {
  298. fallbacks.addFallback(0, "POINTSIZE");
  299. }
  300. if (this._defines.LOGARITHMICDEPTH) {
  301. fallbacks.addFallback(0, "LOGARITHMICDEPTH");
  302. }
  303. MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks, this.maxSimultaneousLights);
  304. if (this._defines.SPECULARTERM) {
  305. fallbacks.addFallback(0, "SPECULARTERM");
  306. }
  307. if (this._defines.DIFFUSEFRESNEL) {
  308. fallbacks.addFallback(1, "DIFFUSEFRESNEL");
  309. }
  310. if (this._defines.OPACITYFRESNEL) {
  311. fallbacks.addFallback(2, "OPACITYFRESNEL");
  312. }
  313. if (this._defines.REFLECTIONFRESNEL) {
  314. fallbacks.addFallback(3, "REFLECTIONFRESNEL");
  315. }
  316. if (this._defines.EMISSIVEFRESNEL) {
  317. fallbacks.addFallback(4, "EMISSIVEFRESNEL");
  318. }
  319. if (this._defines.FRESNEL) {
  320. fallbacks.addFallback(4, "FRESNEL");
  321. }
  322. //Attributes
  323. var attribs = [VertexBuffer.PositionKind];
  324. if (this._defines.NORMAL) {
  325. attribs.push(VertexBuffer.NormalKind);
  326. }
  327. if (this._defines.UV1) {
  328. attribs.push(VertexBuffer.UVKind);
  329. }
  330. if (this._defines.UV2) {
  331. attribs.push(VertexBuffer.UV2Kind);
  332. }
  333. if (this._defines.VERTEXCOLOR) {
  334. attribs.push(VertexBuffer.ColorKind);
  335. }
  336. MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  337. MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  338. var shaderName = "default";
  339. if (this.builder) {
  340. shaderName = this.builder(
  341. new CustomShaderHelper(),
  342. shaderName,
  343. this._mainPart,
  344. this._diffusePart,
  345. this._vertexPositionPart );
  346. }
  347. var join = this._defines.toString();
  348. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vAmbientColor", "vDiffuseColor", "vSpecularColor", "vEmissiveColor",
  349. "vFogInfos", "vFogColor", "pointSize",
  350. "vDiffuseInfos", "vAmbientInfos", "vOpacityInfos", "vReflectionInfos", "vEmissiveInfos", "vSpecularInfos", "vBumpInfos", "vLightmapInfos", "vRefractionInfos",
  351. "mBones",
  352. "vClipPlane", "diffuseMatrix", "ambientMatrix", "opacityMatrix", "reflectionMatrix", "emissiveMatrix", "specularMatrix", "bumpMatrix", "lightmapMatrix", "refractionMatrix",
  353. "depthValues",
  354. "diffuseLeftColor", "diffuseRightColor", "opacityParts", "reflectionLeftColor", "reflectionRightColor", "emissiveLeftColor", "emissiveRightColor", "refractionLeftColor", "refractionRightColor",
  355. "logarithmicDepthConstant"
  356. ];
  357. var samplers = ["diffuseSampler", "ambientSampler", "opacitySampler", "reflectionCubeSampler", "reflection2DSampler", "emissiveSampler", "specularSampler", "bumpSampler", "lightmapSampler", "refractionCubeSampler", "refraction2DSampler"]
  358. if (this._defines.CAMERACOLORCURVES) {
  359. ColorCurves.PrepareUniforms(uniforms);
  360. }
  361. if (this._defines.CAMERACOLORGRADING) {
  362. ColorGradingTexture.PrepareUniformsAndSamplers(uniforms, samplers);
  363. }
  364. MaterialHelper.PrepareUniformsAndSamplersList(uniforms, samplers, this._defines, this.maxSimultaneousLights);
  365. this._effect = scene.getEngine().createEffect(shaderName,
  366. attribs, uniforms, samplers,
  367. join, fallbacks, this.onCompiled, this.onError, { maxSimultaneousLights: this.maxSimultaneousLights - 1 });
  368. }
  369. if (!this._effect.isReady()) {
  370. return false;
  371. }
  372. this._renderId = scene.getRenderId();
  373. this._wasPreviouslyReady = true;
  374. if (mesh) {
  375. if (!mesh._materialDefines) {
  376. mesh._materialDefines = new StandardMaterialDefines();
  377. }
  378. this._defines.cloneTo(mesh._materialDefines);
  379. }
  380. return true;
  381. }
  382. }
  383. }