babylon.gradientMaterial.ts 20 KB

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