babylon.gradientMaterial.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var BABYLON;
  3. (function (BABYLON) {
  4. var maxSimultaneousLights = 4;
  5. var GradientMaterialDefines = (function (_super) {
  6. __extends(GradientMaterialDefines, _super);
  7. function GradientMaterialDefines() {
  8. _super.call(this);
  9. this.DIFFUSE = false;
  10. this.CLIPPLANE = false;
  11. this.ALPHATEST = false;
  12. this.POINTSIZE = false;
  13. this.FOG = false;
  14. this.LIGHT0 = false;
  15. this.LIGHT1 = false;
  16. this.LIGHT2 = false;
  17. this.LIGHT3 = false;
  18. this.SPOTLIGHT0 = false;
  19. this.SPOTLIGHT1 = false;
  20. this.SPOTLIGHT2 = false;
  21. this.SPOTLIGHT3 = false;
  22. this.HEMILIGHT0 = false;
  23. this.HEMILIGHT1 = false;
  24. this.HEMILIGHT2 = false;
  25. this.HEMILIGHT3 = false;
  26. this.DIRLIGHT0 = false;
  27. this.DIRLIGHT1 = false;
  28. this.DIRLIGHT2 = false;
  29. this.DIRLIGHT3 = false;
  30. this.POINTLIGHT0 = false;
  31. this.POINTLIGHT1 = false;
  32. this.POINTLIGHT2 = false;
  33. this.POINTLIGHT3 = false;
  34. this.SHADOW0 = false;
  35. this.SHADOW1 = false;
  36. this.SHADOW2 = false;
  37. this.SHADOW3 = false;
  38. this.SHADOWS = false;
  39. this.SHADOWVSM0 = false;
  40. this.SHADOWVSM1 = false;
  41. this.SHADOWVSM2 = false;
  42. this.SHADOWVSM3 = false;
  43. this.SHADOWPCF0 = false;
  44. this.SHADOWPCF1 = false;
  45. this.SHADOWPCF2 = false;
  46. this.SHADOWPCF3 = false;
  47. this.NORMAL = false;
  48. this.UV1 = false;
  49. this.UV2 = false;
  50. this.VERTEXCOLOR = false;
  51. this.VERTEXALPHA = false;
  52. this.NUM_BONE_INFLUENCERS = 0;
  53. this.BonesPerMesh = 0;
  54. this.INSTANCES = false;
  55. this._keys = Object.keys(this);
  56. }
  57. return GradientMaterialDefines;
  58. })(BABYLON.MaterialDefines);
  59. var GradientMaterial = (function (_super) {
  60. __extends(GradientMaterial, _super);
  61. function GradientMaterial(name, scene) {
  62. _super.call(this, name, scene);
  63. // The gradient top color, red by default
  64. this.topColor = new BABYLON.Color3(1, 0, 0);
  65. this.topColorAlpha = 1.0;
  66. // The gradient top color, blue by default
  67. this.bottomColor = new BABYLON.Color3(0, 0, 1);
  68. this.bottomColorAlpha = 1.0;
  69. // Gradient offset
  70. this.offset = 0;
  71. this.smoothness = 1.0;
  72. this.disableLighting = false;
  73. this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  74. this._scaledDiffuse = new BABYLON.Color3();
  75. this._defines = new GradientMaterialDefines();
  76. this._cachedDefines = new GradientMaterialDefines();
  77. this._cachedDefines.BonesPerMesh = -1;
  78. }
  79. GradientMaterial.prototype.needAlphaBlending = function () {
  80. return (this.alpha < 1.0 || this.topColorAlpha < 1.0 || this.bottomColorAlpha < 1.0);
  81. };
  82. GradientMaterial.prototype.needAlphaTesting = function () {
  83. return true;
  84. };
  85. GradientMaterial.prototype.getAlphaTestTexture = function () {
  86. return null;
  87. };
  88. // Methods
  89. GradientMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  90. if (!mesh) {
  91. return true;
  92. }
  93. if (this._defines.INSTANCES !== useInstances) {
  94. return false;
  95. }
  96. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  97. return true;
  98. }
  99. return false;
  100. };
  101. GradientMaterial.prototype.isReady = function (mesh, useInstances) {
  102. if (this.checkReadyOnlyOnce) {
  103. if (this._wasPreviouslyReady) {
  104. return true;
  105. }
  106. }
  107. var scene = this.getScene();
  108. if (!this.checkReadyOnEveryCall) {
  109. if (this._renderId === scene.getRenderId()) {
  110. if (this._checkCache(scene, mesh, useInstances)) {
  111. return true;
  112. }
  113. }
  114. }
  115. var engine = scene.getEngine();
  116. var needNormals = false;
  117. var needUVs = false;
  118. this._defines.reset();
  119. // No textures
  120. // Effect
  121. if (scene.clipPlane) {
  122. this._defines.CLIPPLANE = true;
  123. }
  124. if (engine.getAlphaTesting()) {
  125. this._defines.ALPHATEST = true;
  126. }
  127. // Point size
  128. if (this.pointsCloud || scene.forcePointsCloud) {
  129. this._defines.POINTSIZE = true;
  130. }
  131. // Fog
  132. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  133. this._defines.FOG = true;
  134. }
  135. var lightIndex = 0;
  136. if (scene.lightsEnabled && !this.disableLighting) {
  137. needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines);
  138. }
  139. // Attribs
  140. if (mesh) {
  141. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  142. this._defines.NORMAL = true;
  143. }
  144. if (needUVs) {
  145. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  146. this._defines.UV1 = true;
  147. }
  148. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  149. this._defines.UV2 = true;
  150. }
  151. }
  152. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  153. this._defines.VERTEXCOLOR = true;
  154. if (mesh.hasVertexAlpha) {
  155. this._defines.VERTEXALPHA = true;
  156. }
  157. }
  158. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  159. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  160. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  161. }
  162. // Instances
  163. if (useInstances) {
  164. this._defines.INSTANCES = true;
  165. }
  166. }
  167. // Get correct effect
  168. if (!this._defines.isEqual(this._cachedDefines)) {
  169. this._defines.cloneTo(this._cachedDefines);
  170. scene.resetCachedMaterial();
  171. // Fallbacks
  172. var fallbacks = new BABYLON.EffectFallbacks();
  173. if (this._defines.FOG) {
  174. fallbacks.addFallback(1, "FOG");
  175. }
  176. BABYLON.MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks);
  177. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  178. fallbacks.addCPUSkinningFallback(0, mesh);
  179. }
  180. //Attributes
  181. var attribs = [BABYLON.VertexBuffer.PositionKind];
  182. if (this._defines.NORMAL) {
  183. attribs.push(BABYLON.VertexBuffer.NormalKind);
  184. }
  185. if (this._defines.UV1) {
  186. attribs.push(BABYLON.VertexBuffer.UVKind);
  187. }
  188. if (this._defines.UV2) {
  189. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  190. }
  191. if (this._defines.VERTEXCOLOR) {
  192. attribs.push(BABYLON.VertexBuffer.ColorKind);
  193. }
  194. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  195. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  196. // Legacy browser patch
  197. var shaderName = "gradient";
  198. var join = this._defines.toString();
  199. this._effect = scene.getEngine().createEffect(shaderName, attribs, ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  200. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  201. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  202. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  203. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  204. "vFogInfos", "vFogColor", "pointSize",
  205. "vDiffuseInfos",
  206. "mBones",
  207. "vClipPlane", "diffuseMatrix",
  208. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3", "depthValues", "topColor", "bottomColor", "offset", "smoothness"
  209. ], ["diffuseSampler",
  210. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  211. ], join, fallbacks, this.onCompiled, this.onError);
  212. }
  213. if (!this._effect.isReady()) {
  214. return false;
  215. }
  216. this._renderId = scene.getRenderId();
  217. this._wasPreviouslyReady = true;
  218. if (mesh) {
  219. if (!mesh._materialDefines) {
  220. mesh._materialDefines = new GradientMaterialDefines();
  221. }
  222. this._defines.cloneTo(mesh._materialDefines);
  223. }
  224. return true;
  225. };
  226. GradientMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  227. this._effect.setMatrix("world", world);
  228. };
  229. GradientMaterial.prototype.bind = function (world, mesh) {
  230. var scene = this.getScene();
  231. // Matrices
  232. this.bindOnlyWorldMatrix(world);
  233. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  234. // Bones
  235. BABYLON.MaterialHelper.BindBonesParameters(mesh, this._effect);
  236. if (scene.getCachedMaterial() !== this) {
  237. // Clip plane
  238. BABYLON.MaterialHelper.BindClipPlane(this._effect, scene);
  239. // Point size
  240. if (this.pointsCloud) {
  241. this._effect.setFloat("pointSize", this.pointSize);
  242. }
  243. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  244. }
  245. this._effect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  246. if (scene.lightsEnabled && !this.disableLighting) {
  247. BABYLON.MaterialHelper.BindLights(scene, mesh, this._effect, this._defines);
  248. }
  249. // View
  250. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  251. this._effect.setMatrix("view", scene.getViewMatrix());
  252. }
  253. // Fog
  254. BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  255. this._effect.setColor4("topColor", this.topColor, this.topColorAlpha);
  256. this._effect.setColor4("bottomColor", this.bottomColor, this.bottomColorAlpha);
  257. this._effect.setFloat("offset", this.offset);
  258. this._effect.setFloat("smoothness", this.smoothness);
  259. _super.prototype.bind.call(this, world, mesh);
  260. };
  261. GradientMaterial.prototype.getAnimatables = function () {
  262. return [];
  263. };
  264. GradientMaterial.prototype.dispose = function (forceDisposeEffect) {
  265. _super.prototype.dispose.call(this, forceDisposeEffect);
  266. };
  267. GradientMaterial.prototype.clone = function (name) {
  268. var newMaterial = new GradientMaterial(name, this.getScene());
  269. // Base material
  270. this.copyTo(newMaterial);
  271. // Gradient material
  272. newMaterial.topColor = this.topColor.clone();
  273. newMaterial.bottomColor = this.bottomColor.clone();
  274. newMaterial.offset = this.offset;
  275. return newMaterial;
  276. };
  277. GradientMaterial.prototype.serialize = function () {
  278. var serializationObject = _super.prototype.serialize.call(this);
  279. serializationObject.customType = "BABYLON.GradientMaterial";
  280. serializationObject.topColor = this.topColor.asArray();
  281. serializationObject.bottomColor = this.bottomColor.asArray();
  282. serializationObject.offset = this.offset;
  283. serializationObject.disableLighting = this.disableLighting;
  284. return serializationObject;
  285. };
  286. GradientMaterial.Parse = function (source, scene, rootUrl) {
  287. var material = new GradientMaterial(source.name, scene);
  288. material.topColor = BABYLON.Color3.FromArray(source.topColor);
  289. material.bottomColor = BABYLON.Color3.FromArray(source.bottomColor);
  290. material.offset = source.offset;
  291. material.disableLighting = source.disableLighting;
  292. material.alpha = source.alpha;
  293. material.id = source.id;
  294. BABYLON.Tags.AddTagsTo(material, source.tags);
  295. material.backFaceCulling = source.backFaceCulling;
  296. material.wireframe = source.wireframe;
  297. if (source.checkReadyOnlyOnce) {
  298. material.checkReadyOnlyOnce = source.checkReadyOnlyOnce;
  299. }
  300. return material;
  301. };
  302. return GradientMaterial;
  303. })(BABYLON.Material);
  304. BABYLON.GradientMaterial = GradientMaterial;
  305. })(BABYLON || (BABYLON = {}));
  306. BABYLON.Effect.ShadersStore['gradientVertexShader'] = "precision highp float;\r\n\r\n\r\n// Attributes\r\nattribute vec3 position;\r\n#ifdef NORMAL\r\nattribute vec3 normal;\r\n#endif\r\n#ifdef UV1\r\nattribute vec2 uv;\r\n#endif\r\n#ifdef UV2\r\nattribute vec2 uv2;\r\n#endif\r\n#ifdef VERTEXCOLOR\r\nattribute vec4 color;\r\n#endif\r\n#include<bonesDeclaration>\r\n\r\n// Uniforms\r\n#include<instancesDeclaration>\r\n\r\nuniform mat4 view;\r\nuniform mat4 viewProjection;\r\n\r\n#ifdef DIFFUSE\r\nvarying vec2 vDiffuseUV;\r\nuniform mat4 diffuseMatrix;\r\nuniform vec2 vDiffuseInfos;\r\n#endif\r\n\r\n#ifdef POINTSIZE\r\nuniform float pointSize;\r\n#endif\r\n\r\n// Output\r\nvarying vec3 vPositionW;\r\n#ifdef NORMAL\r\nvarying vec3 vNormalW;\r\n#endif\r\n\r\n#ifdef VERTEXCOLOR\r\nvarying vec4 vColor;\r\n#endif\r\n\r\n#include<clipPlaneVertexDeclaration>\r\n\r\n#include<fogVertexDeclaration>\r\n#include<shadowsVertexDeclaration>\r\n\r\nvoid main(void) {\r\n#include<instancesVertex>\r\n#include<bonesVertex> \r\n\r\n\tgl_Position = viewProjection * finalWorld * vec4(position, 1.0);\r\n\r\n\tvec4 worldPos = finalWorld * vec4(position, 1.0);\r\n\tvPositionW = vec3(worldPos);\r\n\r\n#ifdef NORMAL\r\n\tvNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)));\r\n#endif\r\n\r\n\t// Texture coordinates\r\n#ifndef UV1\r\n\tvec2 uv = vec2(0., 0.);\r\n#endif\r\n#ifndef UV2\r\n\tvec2 uv2 = vec2(0., 0.);\r\n#endif\r\n\r\n#ifdef DIFFUSE\r\n\tif (vDiffuseInfos.x == 0.)\r\n\t{\r\n\t\tvDiffuseUV = vec2(diffuseMatrix * vec4(uv, 1.0, 0.0));\r\n\t}\r\n\telse\r\n\t{\r\n\t\tvDiffuseUV = vec2(diffuseMatrix * vec4(uv2, 1.0, 0.0));\r\n\t}\r\n#endif\r\n\r\n\t// Clip plane\r\n#include<clipPlaneVertex>\r\n\r\n // Fog\r\n#include<fogVertex>\r\n#include<shadowsVertex>\r\n\r\n\t// Vertex color\r\n#ifdef VERTEXCOLOR\r\n\tvColor = color;\r\n#endif\r\n\r\n\t// Point size\r\n#ifdef POINTSIZE\r\n\tgl_PointSize = pointSize;\r\n#endif\r\n}\r\n";
  307. BABYLON.Effect.ShadersStore['gradientPixelShader'] = "precision highp float;\r\n\r\n// Constants\r\nuniform vec3 vEyePosition;\r\nuniform vec4 vDiffuseColor;\r\n\r\n// Gradient variables\r\nuniform vec4 topColor;\r\nuniform vec4 bottomColor;\r\nuniform float offset;\r\nuniform float smoothness;\r\n\r\n// Input\r\nvarying vec3 vPositionW;\r\n\r\n#ifdef NORMAL\r\nvarying vec3 vNormalW;\r\n#endif\r\n\r\n#ifdef VERTEXCOLOR\r\nvarying vec4 vColor;\r\n#endif\r\n\r\n// Lights\r\n\r\n#include<light0FragmentDeclaration>\r\n#include<light1FragmentDeclaration>\r\n#include<light2FragmentDeclaration>\r\n#include<light3FragmentDeclaration>\r\n\r\n\r\n#include<lightsFragmentFunctions>\r\n#include<shadowsFragmentFunctions>\r\n\r\n// Samplers\r\n#ifdef DIFFUSE\r\nvarying vec2 vDiffuseUV;\r\nuniform sampler2D diffuseSampler;\r\nuniform vec2 vDiffuseInfos;\r\n#endif\r\n\r\n#include<clipPlaneFragmentDeclaration>\r\n\r\n// Fog\r\n#include<fogFragmentDeclaration>\r\n\r\nvoid main(void) {\r\n#include<clipPlaneFragment>\r\n\r\n\tvec3 viewDirectionW = normalize(vEyePosition - vPositionW);\r\n\r\n float h = normalize(vPositionW).y + offset;\r\n float mysmoothness = clamp(smoothness, 0.01, max(smoothness, 10.));\r\n\r\n vec4 baseColor = mix(bottomColor, topColor, max(pow(max(h, 0.0), mysmoothness), 0.0));\r\n\r\n\t// Base color\r\n\tvec3 diffuseColor = baseColor.rgb;\r\n\r\n\t// Alpha\r\n\tfloat alpha = baseColor.a;\r\n\r\n\r\n#ifdef ALPHATEST\r\n\tif (baseColor.a < 0.4)\r\n\t\tdiscard;\r\n#endif\r\n\r\n#ifdef VERTEXCOLOR\r\n\tbaseColor.rgb *= vColor.rgb;\r\n#endif\r\n\r\n\t// Bump\r\n#ifdef NORMAL\r\n\tvec3 normalW = normalize(vNormalW);\r\n#else\r\n\tvec3 normalW = vec3(1.0, 1.0, 1.0);\r\n#endif\r\n\r\n\t// Lighting\r\n\tvec3 diffuseBase = vec3(0., 0., 0.);\r\n\tfloat shadow = 1.;\r\n float glossiness = 0.;\r\n \r\n#include<light0Fragment>\r\n#include<light1Fragment>\r\n#include<light2Fragment>\r\n#include<light3Fragment>\r\n\r\n#ifdef VERTEXALPHA\r\n\talpha *= vColor.a;\r\n#endif\r\n\r\n\tvec3 finalDiffuse = clamp(diffuseBase * diffuseColor, 0.0, 1.0) * baseColor.rgb;\r\n\r\n\t// Composition\r\n\tvec4 color = vec4(finalDiffuse, alpha);\r\n\r\n#include<fogFragment>\r\n\r\n\tgl_FragColor = color;\r\n}";