babylon.gradientMaterial.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON {
  3. var maxSimultaneousLights = 4;
  4. class GradientMaterialDefines extends MaterialDefines {
  5. public DIFFUSE = false;
  6. public CLIPPLANE = false;
  7. public ALPHATEST = false;
  8. public POINTSIZE = false;
  9. public FOG = false;
  10. public LIGHT0 = false;
  11. public LIGHT1 = false;
  12. public LIGHT2 = false;
  13. public LIGHT3 = false;
  14. public SPOTLIGHT0 = false;
  15. public SPOTLIGHT1 = false;
  16. public SPOTLIGHT2 = false;
  17. public SPOTLIGHT3 = false;
  18. public HEMILIGHT0 = false;
  19. public HEMILIGHT1 = false;
  20. public HEMILIGHT2 = false;
  21. public HEMILIGHT3 = false;
  22. public DIRLIGHT0 = false;
  23. public DIRLIGHT1 = false;
  24. public DIRLIGHT2 = false;
  25. public DIRLIGHT3 = false;
  26. public POINTLIGHT0 = false;
  27. public POINTLIGHT1 = false;
  28. public POINTLIGHT2 = false;
  29. public POINTLIGHT3 = false;
  30. public SHADOW0 = false;
  31. public SHADOW1 = false;
  32. public SHADOW2 = false;
  33. public SHADOW3 = false;
  34. public SHADOWS = false;
  35. public SHADOWVSM0 = false;
  36. public SHADOWVSM1 = false;
  37. public SHADOWVSM2 = false;
  38. public SHADOWVSM3 = false;
  39. public SHADOWPCF0 = false;
  40. public SHADOWPCF1 = false;
  41. public SHADOWPCF2 = false;
  42. public SHADOWPCF3 = false;
  43. public NORMAL = false;
  44. public UV1 = false;
  45. public UV2 = false;
  46. public VERTEXCOLOR = false;
  47. public VERTEXALPHA = false;
  48. public BONES = false;
  49. public BONES4 = false;
  50. public BonesPerMesh = 0;
  51. public INSTANCES = false;
  52. constructor() {
  53. super();
  54. this._keys = Object.keys(this);
  55. }
  56. }
  57. export class GradientMaterial extends Material {
  58. // The gradient top color, red by default
  59. public topColor = new Color3(1, 0, 0);
  60. public topColorAlpha = 1.0;
  61. // The gradient top color, blue by default
  62. public bottomColor = new Color3(0, 0, 1);
  63. public bottomColorAlpha = 1.0;
  64. // Gradient offset
  65. public offset = 0;
  66. public smoothness = 1.0;
  67. public disableLighting = false;
  68. private _worldViewProjectionMatrix = Matrix.Zero();
  69. private _scaledDiffuse = new Color3();
  70. private _renderId: number;
  71. private _defines = new GradientMaterialDefines();
  72. private _cachedDefines = new GradientMaterialDefines();
  73. constructor(name: string, scene: Scene) {
  74. super(name, scene);
  75. this._cachedDefines.BonesPerMesh = -1;
  76. }
  77. public needAlphaBlending(): boolean {
  78. return (this.alpha < 1.0 || this.topColorAlpha < 1.0 || this.bottomColorAlpha < 1.0);
  79. }
  80. public needAlphaTesting(): boolean {
  81. return true;
  82. }
  83. public getAlphaTestTexture(): BaseTexture {
  84. return null;
  85. }
  86. // Methods
  87. private _checkCache(scene: Scene, mesh?: AbstractMesh, useInstances?: boolean): boolean {
  88. if (!mesh) {
  89. return true;
  90. }
  91. if (this._defines.INSTANCES !== useInstances) {
  92. return false;
  93. }
  94. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  95. return true;
  96. }
  97. return false;
  98. }
  99. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  100. if (this.checkReadyOnlyOnce) {
  101. if (this._wasPreviouslyReady) {
  102. return true;
  103. }
  104. }
  105. var scene = this.getScene();
  106. if (!this.checkReadyOnEveryCall) {
  107. if (this._renderId === scene.getRenderId()) {
  108. if (this._checkCache(scene, mesh, useInstances)) {
  109. return true;
  110. }
  111. }
  112. }
  113. var engine = scene.getEngine();
  114. var needNormals = false;
  115. var needUVs = false;
  116. this._defines.reset();
  117. // No textures
  118. // Effect
  119. if (scene.clipPlane) {
  120. this._defines.CLIPPLANE = true;
  121. }
  122. if (engine.getAlphaTesting()) {
  123. this._defines.ALPHATEST = true;
  124. }
  125. // Point size
  126. if (this.pointsCloud || scene.forcePointsCloud) {
  127. this._defines.POINTSIZE = true;
  128. }
  129. // Fog
  130. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) {
  131. this._defines.FOG = true;
  132. }
  133. var lightIndex = 0;
  134. if (scene.lightsEnabled && !this.disableLighting) {
  135. for (var index = 0; index < scene.lights.length; index++) {
  136. var light = scene.lights[index];
  137. if (!light.isEnabled()) {
  138. continue;
  139. }
  140. // Excluded check
  141. if (light._excludedMeshesIds.length > 0) {
  142. for (var excludedIndex = 0; excludedIndex < light._excludedMeshesIds.length; excludedIndex++) {
  143. var excludedMesh = scene.getMeshByID(light._excludedMeshesIds[excludedIndex]);
  144. if (excludedMesh) {
  145. light.excludedMeshes.push(excludedMesh);
  146. }
  147. }
  148. light._excludedMeshesIds = [];
  149. }
  150. // Included check
  151. if (light._includedOnlyMeshesIds.length > 0) {
  152. for (var includedOnlyIndex = 0; includedOnlyIndex < light._includedOnlyMeshesIds.length; includedOnlyIndex++) {
  153. var includedOnlyMesh = scene.getMeshByID(light._includedOnlyMeshesIds[includedOnlyIndex]);
  154. if (includedOnlyMesh) {
  155. light.includedOnlyMeshes.push(includedOnlyMesh);
  156. }
  157. }
  158. light._includedOnlyMeshesIds = [];
  159. }
  160. if (!light.canAffectMesh(mesh)) {
  161. continue;
  162. }
  163. needNormals = true;
  164. this._defines["LIGHT" + lightIndex] = true;
  165. var type;
  166. if (light instanceof SpotLight) {
  167. type = "SPOTLIGHT" + lightIndex;
  168. } else if (light instanceof HemisphericLight) {
  169. type = "HEMILIGHT" + lightIndex;
  170. } else if (light instanceof PointLight) {
  171. type = "POINTLIGHT" + lightIndex;
  172. } else {
  173. type = "DIRLIGHT" + lightIndex;
  174. }
  175. this._defines[type] = true;
  176. // Shadows
  177. if (scene.shadowsEnabled) {
  178. var shadowGenerator = light.getShadowGenerator();
  179. if (mesh && mesh.receiveShadows && shadowGenerator) {
  180. this._defines["SHADOW" + lightIndex] = true;
  181. this._defines.SHADOWS = true;
  182. if (shadowGenerator.useVarianceShadowMap || shadowGenerator.useBlurVarianceShadowMap) {
  183. this._defines["SHADOWVSM" + lightIndex] = true;
  184. }
  185. if (shadowGenerator.usePoissonSampling) {
  186. this._defines["SHADOWPCF" + lightIndex] = true;
  187. }
  188. }
  189. }
  190. lightIndex++;
  191. if (lightIndex === maxSimultaneousLights)
  192. break;
  193. }
  194. }
  195. // Attribs
  196. if (mesh) {
  197. if (needNormals && mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  198. this._defines.NORMAL = true;
  199. }
  200. if (needUVs) {
  201. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  202. this._defines.UV1 = true;
  203. }
  204. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  205. this._defines.UV2 = true;
  206. }
  207. }
  208. if (mesh.useVertexColors && mesh.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  209. this._defines.VERTEXCOLOR = true;
  210. if (mesh.hasVertexAlpha) {
  211. this._defines.VERTEXALPHA = true;
  212. }
  213. }
  214. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  215. this._defines.BONES = true;
  216. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  217. this._defines.BONES4 = true;
  218. }
  219. // Instances
  220. if (useInstances) {
  221. this._defines.INSTANCES = true;
  222. }
  223. }
  224. // Get correct effect
  225. if (!this._defines.isEqual(this._cachedDefines)) {
  226. this._defines.cloneTo(this._cachedDefines);
  227. scene.resetCachedMaterial();
  228. // Fallbacks
  229. var fallbacks = new EffectFallbacks();
  230. if (this._defines.FOG) {
  231. fallbacks.addFallback(1, "FOG");
  232. }
  233. for (lightIndex = 0; lightIndex < maxSimultaneousLights; lightIndex++) {
  234. if (!this._defines["LIGHT" + lightIndex]) {
  235. continue;
  236. }
  237. if (lightIndex > 0) {
  238. fallbacks.addFallback(lightIndex, "LIGHT" + lightIndex);
  239. }
  240. if (this._defines["SHADOW" + lightIndex]) {
  241. fallbacks.addFallback(0, "SHADOW" + lightIndex);
  242. }
  243. if (this._defines["SHADOWPCF" + lightIndex]) {
  244. fallbacks.addFallback(0, "SHADOWPCF" + lightIndex);
  245. }
  246. if (this._defines["SHADOWVSM" + lightIndex]) {
  247. fallbacks.addFallback(0, "SHADOWVSM" + lightIndex);
  248. }
  249. }
  250. if (this._defines.BONES4) {
  251. fallbacks.addFallback(0, "BONES4");
  252. }
  253. //Attributes
  254. var attribs = [VertexBuffer.PositionKind];
  255. if (this._defines.NORMAL) {
  256. attribs.push(VertexBuffer.NormalKind);
  257. }
  258. if (this._defines.UV1) {
  259. attribs.push(VertexBuffer.UVKind);
  260. }
  261. if (this._defines.UV2) {
  262. attribs.push(VertexBuffer.UV2Kind);
  263. }
  264. if (this._defines.VERTEXCOLOR) {
  265. attribs.push(VertexBuffer.ColorKind);
  266. }
  267. if (this._defines.BONES) {
  268. attribs.push(VertexBuffer.MatricesIndicesKind);
  269. attribs.push(VertexBuffer.MatricesWeightsKind);
  270. }
  271. if (this._defines.INSTANCES) {
  272. attribs.push("world0");
  273. attribs.push("world1");
  274. attribs.push("world2");
  275. attribs.push("world3");
  276. }
  277. // Legacy browser patch
  278. var shaderName = "gradient";
  279. var join = this._defines.toString();
  280. this._effect = scene.getEngine().createEffect(shaderName,
  281. attribs,
  282. ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  283. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  284. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  285. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  286. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  287. "vFogInfos", "vFogColor", "pointSize",
  288. "vDiffuseInfos",
  289. "mBones",
  290. "vClipPlane", "diffuseMatrix",
  291. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3", "depthValues", "topColor", "bottomColor", "offset", "smoothness"
  292. ],
  293. ["diffuseSampler",
  294. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  295. ],
  296. join, fallbacks, this.onCompiled, this.onError);
  297. }
  298. if (!this._effect.isReady()) {
  299. return false;
  300. }
  301. this._renderId = scene.getRenderId();
  302. this._wasPreviouslyReady = true;
  303. if (mesh) {
  304. if (!mesh._materialDefines) {
  305. mesh._materialDefines = new GradientMaterialDefines();
  306. }
  307. this._defines.cloneTo(mesh._materialDefines);
  308. }
  309. return true;
  310. }
  311. public bindOnlyWorldMatrix(world: Matrix): void {
  312. this._effect.setMatrix("world", world);
  313. }
  314. public bind(world: Matrix, mesh?: Mesh): void {
  315. var scene = this.getScene();
  316. // Matrices
  317. this.bindOnlyWorldMatrix(world);
  318. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  319. // Bones
  320. if (mesh && mesh.useBones && mesh.computeBonesUsingShaders) {
  321. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices(mesh));
  322. }
  323. if (scene.getCachedMaterial() !== this) {
  324. // Clip plane
  325. if (scene.clipPlane) {
  326. var clipPlane = scene.clipPlane;
  327. this._effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  328. }
  329. // Point size
  330. if (this.pointsCloud) {
  331. this._effect.setFloat("pointSize", this.pointSize);
  332. }
  333. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  334. }
  335. this._effect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  336. if (scene.lightsEnabled && !this.disableLighting) {
  337. var lightIndex = 0;
  338. var depthValuesAlreadySet = false;
  339. for (var index = 0; index < scene.lights.length; index++) {
  340. var light = scene.lights[index];
  341. if (!light.isEnabled()) {
  342. continue;
  343. }
  344. if (!light.canAffectMesh(mesh)) {
  345. continue;
  346. }
  347. if (light instanceof PointLight) {
  348. // Point Light
  349. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  350. } else if (light instanceof DirectionalLight) {
  351. // Directional Light
  352. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  353. } else if (light instanceof SpotLight) {
  354. // Spot Light
  355. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightDirection" + lightIndex);
  356. } else if (light instanceof HemisphericLight) {
  357. // Hemispheric Light
  358. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightGround" + lightIndex);
  359. }
  360. light.diffuse.scaleToRef(light.intensity, this._scaledDiffuse);
  361. this._effect.setColor4("vLightDiffuse" + lightIndex, this._scaledDiffuse, light.range);
  362. // Shadows
  363. if (scene.shadowsEnabled) {
  364. var shadowGenerator = light.getShadowGenerator();
  365. if (mesh.receiveShadows && shadowGenerator) {
  366. if (!(<any>light).needCube()) {
  367. this._effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
  368. } else {
  369. if (!depthValuesAlreadySet) {
  370. depthValuesAlreadySet = true;
  371. this._effect.setFloat2("depthValues", scene.activeCamera.minZ, scene.activeCamera.maxZ);
  372. }
  373. }
  374. this._effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
  375. this._effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
  376. }
  377. }
  378. lightIndex++;
  379. if (lightIndex === maxSimultaneousLights)
  380. break;
  381. }
  382. }
  383. // View
  384. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  385. this._effect.setMatrix("view", scene.getViewMatrix());
  386. }
  387. // Fog
  388. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  389. this._effect.setFloat4("vFogInfos", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
  390. this._effect.setColor3("vFogColor", scene.fogColor);
  391. }
  392. this._effect.setColor4("topColor", this.topColor, this.topColorAlpha);
  393. this._effect.setColor4("bottomColor", this.bottomColor, this.bottomColorAlpha);
  394. this._effect.setFloat("offset", this.offset);
  395. this._effect.setFloat("smoothness", this.smoothness);
  396. super.bind(world, mesh);
  397. }
  398. public getAnimatables(): IAnimatable[] {
  399. return [];
  400. }
  401. public dispose(forceDisposeEffect?: boolean): void {
  402. super.dispose(forceDisposeEffect);
  403. }
  404. public clone(name: string): GradientMaterial {
  405. var newMaterial = new GradientMaterial(name, this.getScene());
  406. // Base material
  407. this.copyTo(newMaterial);
  408. // Gradient material
  409. newMaterial.topColor = this.topColor.clone();
  410. newMaterial.bottomColor = this.bottomColor.clone();
  411. newMaterial.offset = this.offset;
  412. return newMaterial;
  413. }
  414. public serialize(): any {
  415. var serializationObject = super.serialize();
  416. serializationObject.customType = "BABYLON.GradientMaterial";
  417. serializationObject.topColor = this.topColor.asArray();
  418. serializationObject.bottomColor = this.bottomColor.asArray();
  419. serializationObject.offset = this.offset;
  420. serializationObject.disableLighting = this.disableLighting;
  421. return serializationObject;
  422. }
  423. public static Parse(source: any, scene: Scene, rootUrl: string): GradientMaterial {
  424. var material = new GradientMaterial(source.name, scene);
  425. material.topColor = Color3.FromArray(source.topColor);
  426. material.bottomColor = Color3.FromArray(source.bottomColor);
  427. material.offset = source.offset;
  428. material.disableLighting = source.disableLighting;
  429. material.alpha = source.alpha;
  430. material.id = source.id;
  431. Tags.AddTagsTo(material, source.tags);
  432. material.backFaceCulling = source.backFaceCulling;
  433. material.wireframe = source.wireframe;
  434. if (source.checkReadyOnlyOnce) {
  435. material.checkReadyOnlyOnce = source.checkReadyOnlyOnce;
  436. }
  437. return material;
  438. }
  439. }
  440. }