babylon.gradientMaterial.ts 20 KB

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