babylon.furMaterial.js 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var BABYLON;
  3. (function (BABYLON) {
  4. var maxSimultaneousLights = 4;
  5. var FurMaterialDefines = (function (_super) {
  6. __extends(FurMaterialDefines, _super);
  7. function FurMaterialDefines() {
  8. _super.call(this);
  9. this.DIFFUSE = false;
  10. this.HEIGHTMAP = false;
  11. this.CLIPPLANE = false;
  12. this.ALPHATEST = false;
  13. this.POINTSIZE = false;
  14. this.FOG = false;
  15. this.LIGHT0 = false;
  16. this.LIGHT1 = false;
  17. this.LIGHT2 = false;
  18. this.LIGHT3 = false;
  19. this.SPOTLIGHT0 = false;
  20. this.SPOTLIGHT1 = false;
  21. this.SPOTLIGHT2 = false;
  22. this.SPOTLIGHT3 = false;
  23. this.HEMILIGHT0 = false;
  24. this.HEMILIGHT1 = false;
  25. this.HEMILIGHT2 = false;
  26. this.HEMILIGHT3 = false;
  27. this.DIRLIGHT0 = false;
  28. this.DIRLIGHT1 = false;
  29. this.DIRLIGHT2 = false;
  30. this.DIRLIGHT3 = false;
  31. this.POINTLIGHT0 = false;
  32. this.POINTLIGHT1 = false;
  33. this.POINTLIGHT2 = false;
  34. this.POINTLIGHT3 = false;
  35. this.SHADOW0 = false;
  36. this.SHADOW1 = false;
  37. this.SHADOW2 = false;
  38. this.SHADOW3 = false;
  39. this.SHADOWS = false;
  40. this.SHADOWVSM0 = false;
  41. this.SHADOWVSM1 = false;
  42. this.SHADOWVSM2 = false;
  43. this.SHADOWVSM3 = false;
  44. this.SHADOWPCF0 = false;
  45. this.SHADOWPCF1 = false;
  46. this.SHADOWPCF2 = false;
  47. this.SHADOWPCF3 = false;
  48. this.NORMAL = false;
  49. this.UV1 = false;
  50. this.UV2 = false;
  51. this.VERTEXCOLOR = false;
  52. this.VERTEXALPHA = false;
  53. this.BONES = false;
  54. this.BONES4 = false;
  55. this.BonesPerMesh = 0;
  56. this.INSTANCES = false;
  57. this.HIGHLEVEL = false;
  58. this._keys = Object.keys(this);
  59. }
  60. return FurMaterialDefines;
  61. })(BABYLON.MaterialDefines);
  62. var FurMaterial = (function (_super) {
  63. __extends(FurMaterial, _super);
  64. function FurMaterial(name, scene) {
  65. _super.call(this, name, scene);
  66. this.diffuseColor = new BABYLON.Color3(1, 1, 1);
  67. this.furLength = 1;
  68. this.furAngle = 0;
  69. this.furColor = new BABYLON.Color3(0.44, 0.21, 0.02);
  70. this.furOffset = 0.0;
  71. this.furSpacing = 12;
  72. this.furGravity = new BABYLON.Vector3(0, 0, 0);
  73. this.furSpeed = 100;
  74. this.furDensity = 20;
  75. this.disableLighting = false;
  76. this.highLevelFur = true;
  77. this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  78. this._scaledDiffuse = new BABYLON.Color3(1., 1., 1.);
  79. this._furTime = 0;
  80. this._defines = new FurMaterialDefines();
  81. this._cachedDefines = new FurMaterialDefines();
  82. this._cachedDefines.BonesPerMesh = -1;
  83. }
  84. Object.defineProperty(FurMaterial.prototype, "furTime", {
  85. get: function () {
  86. return this._furTime;
  87. },
  88. set: function (furTime) {
  89. this._furTime = furTime;
  90. },
  91. enumerable: true,
  92. configurable: true
  93. });
  94. FurMaterial.prototype.needAlphaBlending = function () {
  95. return (this.alpha < 1.0);
  96. };
  97. FurMaterial.prototype.needAlphaTesting = function () {
  98. return false;
  99. };
  100. FurMaterial.prototype.getAlphaTestTexture = function () {
  101. return null;
  102. };
  103. // Methods
  104. FurMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  105. if (!mesh) {
  106. return true;
  107. }
  108. if (this._defines.INSTANCES !== useInstances) {
  109. return false;
  110. }
  111. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  112. return true;
  113. }
  114. return false;
  115. };
  116. FurMaterial.prototype.isReady = function (mesh, useInstances) {
  117. if (this.checkReadyOnlyOnce) {
  118. if (this._wasPreviouslyReady) {
  119. return true;
  120. }
  121. }
  122. var scene = this.getScene();
  123. if (!this.checkReadyOnEveryCall) {
  124. if (this._renderId === scene.getRenderId()) {
  125. if (this._checkCache(scene, mesh, useInstances)) {
  126. return true;
  127. }
  128. }
  129. }
  130. var engine = scene.getEngine();
  131. var needNormals = false;
  132. var needUVs = false;
  133. this._defines.reset();
  134. // Textures
  135. if (scene.texturesEnabled) {
  136. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  137. if (!this.diffuseTexture.isReady()) {
  138. return false;
  139. }
  140. else {
  141. needUVs = true;
  142. this._defines.DIFFUSE = true;
  143. }
  144. }
  145. if (this.heightTexture) {
  146. if (!this.heightTexture.isReady()) {
  147. return false;
  148. }
  149. else {
  150. needUVs = true;
  151. this._defines.HEIGHTMAP = true;
  152. }
  153. }
  154. }
  155. // Effect
  156. if (scene.clipPlane) {
  157. this._defines.CLIPPLANE = true;
  158. }
  159. if (engine.getAlphaTesting()) {
  160. this._defines.ALPHATEST = true;
  161. }
  162. // Point size
  163. if (this.pointsCloud || scene.forcePointsCloud) {
  164. this._defines.POINTSIZE = true;
  165. }
  166. // Fog
  167. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  168. this._defines.FOG = true;
  169. }
  170. // High level
  171. if (this.highLevelFur) {
  172. this._defines.HIGHLEVEL = true;
  173. }
  174. var lightIndex = 0;
  175. if (scene.lightsEnabled && !this.disableLighting) {
  176. for (var index = 0; index < scene.lights.length; index++) {
  177. var light = scene.lights[index];
  178. if (!light.isEnabled()) {
  179. continue;
  180. }
  181. // Excluded check
  182. if (light._excludedMeshesIds.length > 0) {
  183. for (var excludedIndex = 0; excludedIndex < light._excludedMeshesIds.length; excludedIndex++) {
  184. var excludedMesh = scene.getMeshByID(light._excludedMeshesIds[excludedIndex]);
  185. if (excludedMesh) {
  186. light.excludedMeshes.push(excludedMesh);
  187. }
  188. }
  189. light._excludedMeshesIds = [];
  190. }
  191. // Included check
  192. if (light._includedOnlyMeshesIds.length > 0) {
  193. for (var includedOnlyIndex = 0; includedOnlyIndex < light._includedOnlyMeshesIds.length; includedOnlyIndex++) {
  194. var includedOnlyMesh = scene.getMeshByID(light._includedOnlyMeshesIds[includedOnlyIndex]);
  195. if (includedOnlyMesh) {
  196. light.includedOnlyMeshes.push(includedOnlyMesh);
  197. }
  198. }
  199. light._includedOnlyMeshesIds = [];
  200. }
  201. if (!light.canAffectMesh(mesh)) {
  202. continue;
  203. }
  204. needNormals = true;
  205. this._defines["LIGHT" + lightIndex] = true;
  206. var type;
  207. if (light instanceof BABYLON.SpotLight) {
  208. type = "SPOTLIGHT" + lightIndex;
  209. }
  210. else if (light instanceof BABYLON.HemisphericLight) {
  211. type = "HEMILIGHT" + lightIndex;
  212. }
  213. else if (light instanceof BABYLON.PointLight) {
  214. type = "POINTLIGHT" + lightIndex;
  215. }
  216. else {
  217. type = "DIRLIGHT" + lightIndex;
  218. }
  219. this._defines[type] = true;
  220. // Shadows
  221. if (scene.shadowsEnabled) {
  222. var shadowGenerator = light.getShadowGenerator();
  223. if (mesh && mesh.receiveShadows && shadowGenerator) {
  224. this._defines["SHADOW" + lightIndex] = true;
  225. this._defines.SHADOWS = true;
  226. if (shadowGenerator.useVarianceShadowMap || shadowGenerator.useBlurVarianceShadowMap) {
  227. this._defines["SHADOWVSM" + lightIndex] = true;
  228. }
  229. if (shadowGenerator.usePoissonSampling) {
  230. this._defines["SHADOWPCF" + lightIndex] = true;
  231. }
  232. }
  233. }
  234. lightIndex++;
  235. if (lightIndex === maxSimultaneousLights)
  236. break;
  237. }
  238. }
  239. // Attribs
  240. if (mesh) {
  241. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  242. this._defines.NORMAL = true;
  243. }
  244. if (needUVs) {
  245. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  246. this._defines.UV1 = true;
  247. }
  248. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  249. this._defines.UV2 = true;
  250. }
  251. }
  252. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  253. this._defines.VERTEXCOLOR = true;
  254. if (mesh.hasVertexAlpha) {
  255. this._defines.VERTEXALPHA = true;
  256. }
  257. }
  258. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  259. this._defines.BONES = true;
  260. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  261. this._defines.BONES4 = true;
  262. }
  263. // Instances
  264. if (useInstances) {
  265. this._defines.INSTANCES = true;
  266. }
  267. }
  268. // Get correct effect
  269. if (!this._defines.isEqual(this._cachedDefines)) {
  270. this._defines.cloneTo(this._cachedDefines);
  271. scene.resetCachedMaterial();
  272. // Fallbacks
  273. var fallbacks = new BABYLON.EffectFallbacks();
  274. if (this._defines.FOG) {
  275. fallbacks.addFallback(1, "FOG");
  276. }
  277. for (lightIndex = 0; lightIndex < maxSimultaneousLights; lightIndex++) {
  278. if (!this._defines["LIGHT" + lightIndex]) {
  279. continue;
  280. }
  281. if (lightIndex > 0) {
  282. fallbacks.addFallback(lightIndex, "LIGHT" + lightIndex);
  283. }
  284. if (this._defines["SHADOW" + lightIndex]) {
  285. fallbacks.addFallback(0, "SHADOW" + lightIndex);
  286. }
  287. if (this._defines["SHADOWPCF" + lightIndex]) {
  288. fallbacks.addFallback(0, "SHADOWPCF" + lightIndex);
  289. }
  290. if (this._defines["SHADOWVSM" + lightIndex]) {
  291. fallbacks.addFallback(0, "SHADOWVSM" + lightIndex);
  292. }
  293. }
  294. if (this._defines.BONES4) {
  295. fallbacks.addFallback(0, "BONES4");
  296. }
  297. //Attributes
  298. var attribs = [BABYLON.VertexBuffer.PositionKind];
  299. if (this._defines.NORMAL) {
  300. attribs.push(BABYLON.VertexBuffer.NormalKind);
  301. }
  302. if (this._defines.UV1) {
  303. attribs.push(BABYLON.VertexBuffer.UVKind);
  304. }
  305. if (this._defines.UV2) {
  306. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  307. }
  308. if (this._defines.VERTEXCOLOR) {
  309. attribs.push(BABYLON.VertexBuffer.ColorKind);
  310. }
  311. if (this._defines.BONES) {
  312. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  313. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  314. }
  315. if (this._defines.INSTANCES) {
  316. attribs.push("world0");
  317. attribs.push("world1");
  318. attribs.push("world2");
  319. attribs.push("world3");
  320. }
  321. // Legacy browser patch
  322. var shaderName = "fur";
  323. var join = this._defines.toString();
  324. this._effect = scene.getEngine().createEffect(shaderName, attribs, ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  325. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  326. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  327. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  328. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  329. "vFogInfos", "vFogColor", "pointSize",
  330. "vDiffuseInfos",
  331. "mBones",
  332. "vClipPlane", "diffuseMatrix",
  333. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3",
  334. "furLength", "furAngle", "furColor", "furOffset", "furGravity", "furTime", "furSpacing", "furDensity"
  335. ], ["diffuseSampler",
  336. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3",
  337. "heightTexture", "furTexture"
  338. ], join, fallbacks, this.onCompiled, this.onError);
  339. }
  340. if (!this._effect.isReady()) {
  341. return false;
  342. }
  343. this._renderId = scene.getRenderId();
  344. this._wasPreviouslyReady = true;
  345. if (mesh) {
  346. if (!mesh._materialDefines) {
  347. mesh._materialDefines = new FurMaterialDefines();
  348. }
  349. this._defines.cloneTo(mesh._materialDefines);
  350. }
  351. return true;
  352. };
  353. FurMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  354. this._effect.setMatrix("world", world);
  355. };
  356. FurMaterial.prototype.bind = function (world, mesh) {
  357. var scene = this.getScene();
  358. // Matrices
  359. this.bindOnlyWorldMatrix(world);
  360. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  361. // Bones
  362. if (mesh && mesh.useBones && mesh.computeBonesUsingShaders) {
  363. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  364. }
  365. if (scene.getCachedMaterial() !== this) {
  366. // Textures
  367. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  368. this._effect.setTexture("diffuseSampler", this.diffuseTexture);
  369. this._effect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  370. this._effect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  371. }
  372. if (this.heightTexture) {
  373. this._effect.setTexture("heightTexture", this.heightTexture);
  374. }
  375. // Clip plane
  376. if (scene.clipPlane) {
  377. var clipPlane = scene.clipPlane;
  378. this._effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  379. }
  380. // Point size
  381. if (this.pointsCloud) {
  382. this._effect.setFloat("pointSize", this.pointSize);
  383. }
  384. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  385. }
  386. this._effect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  387. if (scene.lightsEnabled && !this.disableLighting) {
  388. var lightIndex = 0;
  389. for (var index = 0; index < scene.lights.length; index++) {
  390. var light = scene.lights[index];
  391. if (!light.isEnabled()) {
  392. continue;
  393. }
  394. if (!light.canAffectMesh(mesh)) {
  395. continue;
  396. }
  397. if (light instanceof BABYLON.PointLight) {
  398. // Point Light
  399. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  400. }
  401. else if (light instanceof BABYLON.DirectionalLight) {
  402. // Directional Light
  403. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  404. }
  405. else if (light instanceof BABYLON.SpotLight) {
  406. // Spot Light
  407. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightDirection" + lightIndex);
  408. }
  409. else if (light instanceof BABYLON.HemisphericLight) {
  410. // Hemispheric Light
  411. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightGround" + lightIndex);
  412. }
  413. light.diffuse.scaleToRef(light.intensity, this._scaledDiffuse);
  414. this._effect.setColor4("vLightDiffuse" + lightIndex, this._scaledDiffuse, light.range);
  415. // Shadows
  416. if (scene.shadowsEnabled) {
  417. var shadowGenerator = light.getShadowGenerator();
  418. if (mesh.receiveShadows && shadowGenerator) {
  419. this._effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
  420. this._effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
  421. this._effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
  422. }
  423. }
  424. lightIndex++;
  425. if (lightIndex === maxSimultaneousLights)
  426. break;
  427. }
  428. }
  429. // View
  430. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  431. this._effect.setMatrix("view", scene.getViewMatrix());
  432. }
  433. // Fog
  434. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  435. this._effect.setFloat4("vFogInfos", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
  436. this._effect.setColor3("vFogColor", scene.fogColor);
  437. }
  438. this._effect.setFloat("furLength", this.furLength);
  439. this._effect.setFloat("furAngle", this.furAngle);
  440. this._effect.setColor4("furColor", this.furColor, 1.0);
  441. if (this.highLevelFur) {
  442. this._effect.setVector3("furGravity", this.furGravity);
  443. this._effect.setFloat("furOffset", this.furOffset);
  444. this._effect.setFloat("furSpacing", this.furSpacing);
  445. this._effect.setFloat("furDensity", this.furDensity);
  446. this._furTime += this.getScene().getEngine().getDeltaTime() / this.furSpeed;
  447. this._effect.setFloat("furTime", this._furTime);
  448. this._effect.setTexture("furTexture", this.furTexture);
  449. }
  450. _super.prototype.bind.call(this, world, mesh);
  451. };
  452. FurMaterial.prototype.getAnimatables = function () {
  453. var results = [];
  454. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  455. results.push(this.diffuseTexture);
  456. }
  457. if (this.heightTexture && this.heightTexture.animations && this.heightTexture.animations.length > 0) {
  458. results.push(this.heightTexture);
  459. }
  460. return results;
  461. };
  462. FurMaterial.prototype.dispose = function (forceDisposeEffect) {
  463. if (this.diffuseTexture) {
  464. this.diffuseTexture.dispose();
  465. }
  466. _super.prototype.dispose.call(this, forceDisposeEffect);
  467. };
  468. FurMaterial.prototype.clone = function (name) {
  469. var newMaterial = new FurMaterial(name, this.getScene());
  470. // Base material
  471. this.copyTo(newMaterial);
  472. // Fur material
  473. if (this.diffuseTexture && this.diffuseTexture.clone) {
  474. newMaterial.diffuseTexture = this.diffuseTexture.clone();
  475. }
  476. if (this.heightTexture && this.heightTexture.clone) {
  477. newMaterial.heightTexture = this.heightTexture.clone();
  478. }
  479. if (this.diffuseColor && this.diffuseColor.clone) {
  480. newMaterial.diffuseColor = this.diffuseColor.clone();
  481. }
  482. return newMaterial;
  483. };
  484. FurMaterial.prototype.serialize = function () {
  485. var serializationObject = _super.prototype.serialize.call(this);
  486. serializationObject.customType = "BABYLON.FurMaterial";
  487. serializationObject.diffuseColor = this.diffuseColor.asArray();
  488. serializationObject.disableLighting = this.disableLighting;
  489. serializationObject.furLength = this.furLength;
  490. serializationObject.furAngle = this.furAngle;
  491. serializationObject.furColor = this.furColor.asArray();
  492. serializationObject.furGravity = this.furGravity.asArray();
  493. serializationObject.furSpacing = this.furSpacing;
  494. serializationObject.furSpeed = this.furSpeed;
  495. serializationObject.furDensity = this.furDensity;
  496. if (this.diffuseTexture) {
  497. serializationObject.diffuseTexture = this.diffuseTexture.serialize();
  498. }
  499. if (this.heightTexture) {
  500. serializationObject.heightTexture = this.heightTexture.serialize();
  501. }
  502. return serializationObject;
  503. };
  504. FurMaterial.Parse = function (source, scene, rootUrl) {
  505. var material = new FurMaterial(source.name, scene);
  506. material.diffuseColor = BABYLON.Color3.FromArray(source.diffuseColor);
  507. material.furLength = source.furLength;
  508. material.furAngle = source.furAngle;
  509. material.furColor = BABYLON.Color3.FromArray(source.furColor);
  510. material.furGravity = BABYLON.Vector3.FromArray(source.furGravity);
  511. material.furSpacing = source.furSpacing;
  512. material.furSpeed = source.furSpeed;
  513. material.furDensity = source.furDensity;
  514. material.disableLighting = source.disableLighting;
  515. material.alpha = source.alpha;
  516. material.id = source.id;
  517. BABYLON.Tags.AddTagsTo(material, source.tags);
  518. material.backFaceCulling = source.backFaceCulling;
  519. material.wireframe = source.wireframe;
  520. if (source.diffuseTexture) {
  521. material.diffuseTexture = BABYLON.Texture.Parse(source.diffuseTexture, scene, rootUrl);
  522. }
  523. if (source.heightTexture) {
  524. material.heightTexture = BABYLON.Texture.Parse(source.heightTexture, scene, rootUrl);
  525. }
  526. if (source.checkReadyOnlyOnce) {
  527. material.checkReadyOnlyOnce = source.checkReadyOnlyOnce;
  528. }
  529. return material;
  530. };
  531. FurMaterial.GenerateTexture = function (name, scene) {
  532. // Generate fur textures
  533. var texture = new BABYLON.DynamicTexture("FurTexture " + name, 256, scene, true);
  534. var context = texture.getContext();
  535. for (var i = 0; i < 20000; ++i) {
  536. context.fillStyle = "rgba(255, " + Math.floor(Math.random() * 255) + ", " + Math.floor(Math.random() * 255) + ", 1)";
  537. context.fillRect((Math.random() * texture.getSize().width), (Math.random() * texture.getSize().height), 2, 2);
  538. }
  539. texture.update(false);
  540. texture.wrapU = BABYLON.Texture.WRAP_ADDRESSMODE;
  541. texture.wrapV = BABYLON.Texture.WRAP_ADDRESSMODE;
  542. return texture;
  543. };
  544. // Creates and returns an array of meshes used as shells for the Fur Material
  545. // that can be disposed later in your code
  546. // The quality is in interval [0, 100]
  547. FurMaterial.FurifyMesh = function (sourceMesh, quality) {
  548. var meshes = [sourceMesh];
  549. var mat = sourceMesh.material;
  550. if (!(mat instanceof FurMaterial)) {
  551. throw "The material of the source mesh must be a Fur Material";
  552. }
  553. for (var i = 1; i < quality; i++) {
  554. var offsetFur = new BABYLON.FurMaterial(mat.name + i, sourceMesh.getScene());
  555. offsetFur.furLength = mat.furLength;
  556. offsetFur.furAngle = mat.furAngle;
  557. offsetFur.furGravity = mat.furGravity;
  558. offsetFur.furSpacing = mat.furSpacing;
  559. offsetFur.furSpeed = mat.furSpeed;
  560. offsetFur.furColor = mat.furColor;
  561. offsetFur.diffuseTexture = mat.diffuseTexture;
  562. offsetFur.furOffset = i / quality;
  563. offsetFur.furTexture = mat.furTexture;
  564. offsetFur.highLevelFur = mat.highLevelFur;
  565. offsetFur.furTime = mat.furTime;
  566. offsetFur.furDensity = mat.furDensity;
  567. var offsetMesh = sourceMesh.clone(sourceMesh.name + i);
  568. offsetMesh.material = offsetFur;
  569. offsetMesh.skeleton = sourceMesh.skeleton;
  570. meshes.push(offsetMesh);
  571. }
  572. return meshes;
  573. };
  574. return FurMaterial;
  575. })(BABYLON.Material);
  576. BABYLON.FurMaterial = FurMaterial;
  577. })(BABYLON || (BABYLON = {}));
  578. BABYLON.Effect.ShadersStore['furVertexShader'] = "precision highp float;\n\n// Attributes\nattribute vec3 position;\nattribute vec3 normal;\n\n#ifdef UV1\nattribute vec2 uv;\n#endif\n#ifdef UV2\nattribute vec2 uv2;\n#endif\n#ifdef VERTEXCOLOR\nattribute vec4 color;\n#endif\n#ifdef BONES\nattribute vec4 matricesIndices;\nattribute vec4 matricesWeights;\n#endif\n\n\n// Uniforms\nuniform float furLength;\nuniform float furAngle;\n#ifdef HIGHLEVEL\nuniform float furOffset;\nuniform vec3 furGravity;\nuniform float furTime;\nuniform float furSpacing;\nuniform float furDensity;\n#endif\n#ifdef HEIGHTMAP\nuniform sampler2D heightTexture;\n#endif\n\n#ifdef HIGHLEVEL\nvarying vec2 vFurUV;\n#endif\n\n#ifdef INSTANCES\nattribute vec4 world0;\nattribute vec4 world1;\nattribute vec4 world2;\nattribute vec4 world3;\n#else\nuniform mat4 world;\n#endif\n\nuniform mat4 view;\nuniform mat4 viewProjection;\n\n#ifdef DIFFUSE\nvarying vec2 vDiffuseUV;\nuniform mat4 diffuseMatrix;\nuniform vec2 vDiffuseInfos;\n#endif\n\n#ifdef BONES\nuniform mat4 mBones[BonesPerMesh];\n#endif\n\n#ifdef POINTSIZE\nuniform float pointSize;\n#endif\n\n// Output\nvarying vec3 vPositionW;\n#ifdef NORMAL\nvarying vec3 vNormalW;\n#endif\nvarying float vfur_length;\n\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n\n#ifdef CLIPPLANE\nuniform vec4 vClipPlane;\nvarying float fClipDistance;\n#endif\n\n#ifdef FOG\nvarying float fFogDistance;\n#endif\n\n#ifdef SHADOWS\n#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)\nuniform mat4 lightMatrix0;\nvarying vec4 vPositionFromLight0;\n#endif\n#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)\nuniform mat4 lightMatrix1;\nvarying vec4 vPositionFromLight1;\n#endif\n#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)\nuniform mat4 lightMatrix2;\nvarying vec4 vPositionFromLight2;\n#endif\n#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)\nuniform mat4 lightMatrix3;\nvarying vec4 vPositionFromLight3;\n#endif\n#endif\n\nfloat Rand(vec3 rv) {\n\tfloat x = dot(rv, vec3(12.9898,78.233, 24.65487));\n\treturn fract(sin(x) * 43758.5453);\n}\n\nvoid main(void) {\n\tmat4 finalWorld;\n\n#ifdef INSTANCES\n\tfinalWorld = mat4(world0, world1, world2, world3);\n#else\n\tfinalWorld = world;\n#endif\n\n#ifdef BONES\n\tmat4 m0 = mBones[int(matricesIndices.x)] * matricesWeights.x;\n\tmat4 m1 = mBones[int(matricesIndices.y)] * matricesWeights.y;\n\tmat4 m2 = mBones[int(matricesIndices.z)] * matricesWeights.z;\n\n#ifdef BONES4\n\tmat4 m3 = mBones[int(matricesIndices.w)] * matricesWeights.w;\n\tfinalWorld = finalWorld * (m0 + m1 + m2 + m3);\n#else\n\tfinalWorld = finalWorld * (m0 + m1 + m2);\n#endif \n\n#endif\n\n//FUR\nfloat r = Rand(position);\n#ifdef HEIGHTMAP\t\n\tvfur_length = furLength * texture2D(heightTexture, uv).rgb.x;\n#else\t\n\tvfur_length = (furLength * r);\n#endif\n\tvec3 tangent1 = vec3(normal.y, -normal.x, 0);\n\tvec3 tangent2 = vec3(-normal.z, 0, normal.x);\n\tr = Rand(tangent1 * r);\n\tfloat J = (2.0 + 4.0 * r);\n\tr = Rand(tangent2*r);\n\tfloat K = (2.0 + 2.0 * r);\n\ttangent1 = tangent1*J + tangent2 * K;\n\ttangent1 = normalize(tangent1);\n\t\n vec3 newPosition = position + normal * vfur_length*cos(furAngle) + tangent1 * vfur_length * sin(furAngle);\n \n\t#ifdef HIGHLEVEL\n\t// Compute fur data passed to the pixel shader\n\tvec3 forceDirection = vec3(0.0, 0.0, 0.0);\n\tforceDirection.x = sin(furTime + position.x * 0.05) * 0.2;\n\tforceDirection.y = cos(furTime * 0.7 + position.y * 0.04) * 0.2;\n\tforceDirection.z = sin(furTime * 0.7 + position.z * 0.04) * 0.2;\n\t\n\tvec3 displacement = vec3(0.0, 0.0, 0.0);\n\tdisplacement = furGravity + forceDirection;\n\t\n\tfloat displacementFactor = pow(furOffset, 3.0);\n\t\n\tvec3 aNormal = normal;\n\taNormal.xyz += displacement * displacementFactor;\n\t\n\tnewPosition = vec3(newPosition.x, newPosition.y, newPosition.z) + (normalize(aNormal) * furOffset * furSpacing);\n\t#endif\n\t\n\t#ifdef NORMAL\n\t#ifdef HIGHLEVEL\n\tvNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)) * aNormal);\n\t#else\n\tvNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)));\n\t#endif\n\t#endif\n\t\n//END FUR\n\tgl_Position = viewProjection * finalWorld * vec4(newPosition, 1.0);\n\n\tvec4 worldPos = finalWorld * vec4(newPosition, 1.0);\n\tvPositionW = vec3(worldPos);\n\n\t// Texture coordinates\n#ifndef UV1\n\tvec2 uv = vec2(0., 0.);\n#endif\n#ifndef UV2\n\tvec2 uv2 = vec2(0., 0.);\n#endif\n\n#ifdef DIFFUSE\n\tif (vDiffuseInfos.x == 0.)\n\t{\n\t\tvDiffuseUV = vec2(diffuseMatrix * vec4(uv, 1.0, 0.0));\n\t}\n\telse\n\t{\n\t\tvDiffuseUV = vec2(diffuseMatrix * vec4(uv2, 1.0, 0.0));\n\t}\n \n #ifdef HIGHLEVEL\n\tvFurUV = vDiffuseUV * furDensity;\n\t#endif\n#else\n #ifdef HIGHLEVEL\n\tvFurUV = uv * furDensity;\n\t#endif\n#endif\n\n\t// Clip plane\n#ifdef CLIPPLANE\n\tfClipDistance = dot(worldPos, vClipPlane);\n#endif\n\n\t// Fog\n#ifdef FOG\n\tfFogDistance = (view * worldPos).z;\n#endif\n\n\t// Shadows\n#ifdef SHADOWS\n#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)\n\tvPositionFromLight0 = lightMatrix0 * worldPos;\n#endif\n#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)\n\tvPositionFromLight1 = lightMatrix1 * worldPos;\n#endif\n#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)\n\tvPositionFromLight2 = lightMatrix2 * worldPos;\n#endif\n#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)\n\tvPositionFromLight3 = lightMatrix3 * worldPos;\n#endif\n#endif\n\n\t// Vertex color\n#ifdef VERTEXCOLOR\n\tvColor = color;\n#endif\n\n\t// Point size\n#ifdef POINTSIZE\n\tgl_PointSize = pointSize;\n#endif\n}\n";
  579. BABYLON.Effect.ShadersStore['furPixelShader'] = "precision highp float;\n\n// Constants\nuniform vec3 vEyePosition;\nuniform vec4 vDiffuseColor;\n\n// Input\nuniform vec4 furColor;\nvarying vec3 vPositionW;\nvarying float vfur_length;\n\n#ifdef NORMAL\nvarying vec3 vNormalW;\n#endif\n\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n\n// Lights\n#ifdef LIGHT0\nuniform vec4 vLightData0;\nuniform vec4 vLightDiffuse0;\n#ifdef SHADOW0\n#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)\nvarying vec4 vPositionFromLight0;\nuniform sampler2D shadowSampler0;\n#else\nuniform samplerCube shadowSampler0;\n#endif\nuniform vec3 shadowsInfo0;\n#endif\n#ifdef SPOTLIGHT0\nuniform vec4 vLightDirection0;\n#endif\n#ifdef HEMILIGHT0\nuniform vec3 vLightGround0;\n#endif\n#endif\n\n#ifdef LIGHT1\nuniform vec4 vLightData1;\nuniform vec4 vLightDiffuse1;\n#ifdef SHADOW1\n#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)\nvarying vec4 vPositionFromLight1;\nuniform sampler2D shadowSampler1;\n#else\nuniform samplerCube shadowSampler1;\n#endif\nuniform vec3 shadowsInfo1;\n#endif\n#ifdef SPOTLIGHT1\nuniform vec4 vLightDirection1;\n#endif\n#ifdef HEMILIGHT1\nuniform vec3 vLightGround1;\n#endif\n#endif\n\n#ifdef LIGHT2\nuniform vec4 vLightData2;\nuniform vec4 vLightDiffuse2;\n#ifdef SHADOW2\n#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)\nvarying vec4 vPositionFromLight2;\nuniform sampler2D shadowSampler2;\n#else\nuniform samplerCube shadowSampler2;\n#endif\nuniform vec3 shadowsInfo2;\n#endif\n#ifdef SPOTLIGHT2\nuniform vec4 vLightDirection2;\n#endif\n#ifdef HEMILIGHT2\nuniform vec3 vLightGround2;\n#endif\n#endif\n\n#ifdef LIGHT3\nuniform vec4 vLightData3;\nuniform vec4 vLightDiffuse3;\n#ifdef SHADOW3\n#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)\nvarying vec4 vPositionFromLight3;\nuniform sampler2D shadowSampler3;\n#else\nuniform samplerCube shadowSampler3;\n#endif\nuniform vec3 shadowsInfo3;\n#endif\n#ifdef SPOTLIGHT3\nuniform vec4 vLightDirection3;\n#endif\n#ifdef HEMILIGHT3\nuniform vec3 vLightGround3;\n#endif\n#endif\n\n// Samplers\n#ifdef DIFFUSE\nvarying vec2 vDiffuseUV;\nuniform sampler2D diffuseSampler;\nuniform vec2 vDiffuseInfos;\n#endif\n\n// Fur uniforms\n#ifdef HIGHLEVEL\nuniform float furOffset;\nuniform sampler2D furTexture;\n\nvarying vec2 vFurUV;\n#endif\n\n// Shadows\n#ifdef SHADOWS\n\nfloat unpack(vec4 color)\n{\n\tconst vec4 bit_shift = vec4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0);\n\treturn dot(color, bit_shift);\n}\n\n#if defined(POINTLIGHT0) || defined(POINTLIGHT1) || defined(POINTLIGHT2) || defined(POINTLIGHT3)\nfloat computeShadowCube(vec3 lightPosition, samplerCube shadowSampler, float darkness, float bias)\n{\n\tvec3 directionToLight = vPositionW - lightPosition;\n\tfloat depth = length(directionToLight);\n\n\tdepth = clamp(depth, 0., 1.);\n\n\tdirectionToLight.y = 1.0 - directionToLight.y;\n\n\tfloat shadow = unpack(textureCube(shadowSampler, directionToLight)) + bias;\n\n\tif (depth > shadow)\n\t{\n\t\treturn darkness;\n\t}\n\treturn 1.0;\n}\n\nfloat computeShadowWithPCFCube(vec3 lightPosition, samplerCube shadowSampler, float bias, float darkness)\n{\n\tvec3 directionToLight = vPositionW - lightPosition;\n\tfloat depth = length(directionToLight);\n\n\tdepth = clamp(depth, 0., 1.);\n\n\tdirectionToLight.y = 1.0 - directionToLight.y;\n\n\tfloat visibility = 1.;\n\n\tvec3 poissonDisk[4];\n\tpoissonDisk[0] = vec3(-0.094201624, 0.04, -0.039906216);\n\tpoissonDisk[1] = vec3(0.094558609, -0.04, -0.076890725);\n\tpoissonDisk[2] = vec3(-0.094184101, 0.01, -0.092938870);\n\tpoissonDisk[3] = vec3(0.034495938, -0.01, 0.029387760);\n\n\t// Poisson Sampling\n\tfloat biasedDepth = depth - bias;\n\n\tif (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[0])) < biasedDepth) visibility -= 0.25;\n\tif (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[1])) < biasedDepth) visibility -= 0.25;\n\tif (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[2])) < biasedDepth) visibility -= 0.25;\n\tif (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[3])) < biasedDepth) visibility -= 0.25;\n\n\treturn min(1.0, visibility + darkness);\n}\n#endif\n\n#if defined(SPOTLIGHT0) || defined(SPOTLIGHT1) || defined(SPOTLIGHT2) || defined(SPOTLIGHT3) || defined(DIRLIGHT0) || defined(DIRLIGHT1) || defined(DIRLIGHT2) || defined(DIRLIGHT3)\nfloat computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness, float bias)\n{\n\tvec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;\n\tdepth = 0.5 * depth + vec3(0.5);\n\tvec2 uv = depth.xy;\n\n\tif (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)\n\t{\n\t\treturn 1.0;\n\t}\n\n\tfloat shadow = unpack(texture2D(shadowSampler, uv)) + bias;\n\n\tif (depth.z > shadow)\n\t{\n\t\treturn darkness;\n\t}\n\treturn 1.;\n}\n\nfloat computeShadowWithPCF(vec4 vPositionFromLight, sampler2D shadowSampler, float mapSize, float bias, float darkness)\n{\n\tvec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;\n\tdepth = 0.5 * depth + vec3(0.5);\n\tvec2 uv = depth.xy;\n\n\tif (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)\n\t{\n\t\treturn 1.0;\n\t}\n\n\tfloat visibility = 1.;\n\n\tvec2 poissonDisk[4];\n\tpoissonDisk[0] = vec2(-0.94201624, -0.39906216);\n\tpoissonDisk[1] = vec2(0.94558609, -0.76890725);\n\tpoissonDisk[2] = vec2(-0.094184101, -0.92938870);\n\tpoissonDisk[3] = vec2(0.34495938, 0.29387760);\n\n\t// Poisson Sampling\n\tfloat biasedDepth = depth.z - bias;\n\n\tif (unpack(texture2D(shadowSampler, uv + poissonDisk[0] / mapSize)) < biasedDepth) visibility -= 0.25;\n\tif (unpack(texture2D(shadowSampler, uv + poissonDisk[1] / mapSize)) < biasedDepth) visibility -= 0.25;\n\tif (unpack(texture2D(shadowSampler, uv + poissonDisk[2] / mapSize)) < biasedDepth) visibility -= 0.25;\n\tif (unpack(texture2D(shadowSampler, uv + poissonDisk[3] / mapSize)) < biasedDepth) visibility -= 0.25;\n\n\treturn min(1.0, visibility + darkness);\n}\n\n// Thanks to http://devmaster.net/\nfloat unpackHalf(vec2 color)\n{\n\treturn color.x + (color.y / 255.0);\n}\n\nfloat linstep(float low, float high, float v) {\n\treturn clamp((v - low) / (high - low), 0.0, 1.0);\n}\n\nfloat ChebychevInequality(vec2 moments, float compare, float bias)\n{\n\tfloat p = smoothstep(compare - bias, compare, moments.x);\n\tfloat variance = max(moments.y - moments.x * moments.x, 0.02);\n\tfloat d = compare - moments.x;\n\tfloat p_max = linstep(0.2, 1.0, variance / (variance + d * d));\n\n\treturn clamp(max(p, p_max), 0.0, 1.0);\n}\n\nfloat computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler, float bias, float darkness)\n{\n\tvec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;\n\tdepth = 0.5 * depth + vec3(0.5);\n\tvec2 uv = depth.xy;\n\n\tif (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0 || depth.z >= 1.0)\n\t{\n\t\treturn 1.0;\n\t}\n\n\tvec4 texel = texture2D(shadowSampler, uv);\n\n\tvec2 moments = vec2(unpackHalf(texel.xy), unpackHalf(texel.zw));\n\treturn min(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness);\n}\n#endif\n#endif\n\n\n#ifdef CLIPPLANE\nvarying float fClipDistance;\n#endif\n\n// Fog\n#ifdef FOG\n\n#define FOGMODE_NONE 0.\n#define FOGMODE_EXP 1.\n#define FOGMODE_EXP2 2.\n#define FOGMODE_LINEAR 3.\n#define E 2.71828\n\nuniform vec4 vFogInfos;\nuniform vec3 vFogColor;\nvarying float fFogDistance;\n\nfloat CalcFogFactor()\n{\n\tfloat fogCoeff = 1.0;\n\tfloat fogStart = vFogInfos.y;\n\tfloat fogEnd = vFogInfos.z;\n\tfloat fogDensity = vFogInfos.w;\n\n\tif (FOGMODE_LINEAR == vFogInfos.x)\n\t{\n\t\tfogCoeff = (fogEnd - fFogDistance) / (fogEnd - fogStart);\n\t}\n\telse if (FOGMODE_EXP == vFogInfos.x)\n\t{\n\t\tfogCoeff = 1.0 / pow(E, fFogDistance * fogDensity);\n\t}\n\telse if (FOGMODE_EXP2 == vFogInfos.x)\n\t{\n\t\tfogCoeff = 1.0 / pow(E, fFogDistance * fFogDistance * fogDensity * fogDensity);\n\t}\n\n\treturn clamp(fogCoeff, 0.0, 1.0);\n}\n#endif\n\n// Light Computing\nstruct lightingInfo\n{\n\tvec3 diffuse;\n};\n\nlightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, float range) {\n\tlightingInfo result;\n\n\tvec3 lightVectorW;\n\tfloat attenuation = 1.0;\n\tif (lightData.w == 0.)\n\t{\n\t\tvec3 direction = lightData.xyz - vPositionW;\n\n\t\tattenuation = max(0., 1.0 - length(direction) / range);\n\t\tlightVectorW = normalize(direction);\n\t}\n\telse\n\t{\n\t\tlightVectorW = normalize(-lightData.xyz);\n\t}\n\n\t// diffuse\n\tfloat ndl = max(0., dot(vNormal, lightVectorW));\n\tresult.diffuse = ndl * diffuseColor * attenuation;\n\n\treturn result;\n}\n\nlightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 diffuseColor, float range) {\n\tlightingInfo result;\n\n\tvec3 direction = lightData.xyz - vPositionW;\n\tvec3 lightVectorW = normalize(direction);\n\tfloat attenuation = max(0., 1.0 - length(direction) / range);\n\n\t// diffuse\n\tfloat cosAngle = max(0., dot(-lightDirection.xyz, lightVectorW));\n\tfloat spotAtten = 0.0;\n\n\tif (cosAngle >= lightDirection.w)\n\t{\n\t\tcosAngle = max(0., pow(cosAngle, lightData.w));\n\t\tspotAtten = clamp((cosAngle - lightDirection.w) / (1. - cosAngle), 0.0, 1.0);\n\n\t\t// Diffuse\n\t\tfloat ndl = max(0., dot(vNormal, -lightDirection.xyz));\n\t\tresult.diffuse = ndl * spotAtten * diffuseColor * attenuation;\n\n\t\treturn result;\n\t}\n\n\tresult.diffuse = vec3(0.);\n\n\treturn result;\n}\n\nlightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 groundColor) {\n\tlightingInfo result;\n\n\t// Diffuse\n\tfloat ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;\n\tresult.diffuse = mix(groundColor, diffuseColor, ndl);\n\n\treturn result;\n}\n\nfloat Rand(vec3 rv) {\n\tfloat x = dot(rv, vec3(12.9898,78.233, 24.65487));\n\treturn fract(sin(x) * 43758.5453);\n}\n\nvoid main(void) {\n\t// Clip plane\n#ifdef CLIPPLANE\n\tif (fClipDistance > 0.0)\n\t\tdiscard;\n#endif\n\n\tvec3 viewDirectionW = normalize(vEyePosition - vPositionW);\n\n\t// Base color\n\tvec4 baseColor = furColor;\n\tvec3 diffuseColor = vDiffuseColor.rgb;\n\n\t// Alpha\n\tfloat alpha = vDiffuseColor.a;\n\n#ifdef DIFFUSE\n\tbaseColor *= texture2D(diffuseSampler, vDiffuseUV);\n \n#ifdef ALPHATEST\n\tif (baseColor.a < 0.4)\n\t\tdiscard;\n#endif\n\n\tbaseColor.rgb *= vDiffuseInfos.y;\n#endif\n\n#ifdef VERTEXCOLOR\n\tbaseColor.rgb *= vColor.rgb;\n#endif\n\n\t// Bump\n#ifdef NORMAL\n\tvec3 normalW = normalize(vNormalW);\n#else\n\tvec3 normalW = vec3(1.0, 1.0, 1.0);\n#endif\n\n\t#ifdef HIGHLEVEL\n\t// Fur\n\tvec4 furTextureColor = texture2D(furTexture, vec2(vFurUV.x, vFurUV.y));\n\t\n\tif (furTextureColor.a <= 0.0 || furTextureColor.g < furOffset) {\n\t\tdiscard;\n\t}\n\t\n\tbaseColor = vec4(baseColor.xyz, 1.0 - furOffset);\n\t#endif\n\n\t// Lighting\n\tvec3 diffuseBase = vec3(0., 0., 0.);\n\tfloat shadow = 1.;\n\n#ifdef LIGHT0\n#ifdef SPOTLIGHT0\n\tlightingInfo info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0.rgb, vLightDiffuse0.a);\n#endif\n#ifdef HEMILIGHT0\n\tlightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightGround0);\n#endif\n#if defined(POINTLIGHT0) || defined(DIRLIGHT0)\n\tlightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightDiffuse0.a);\n#endif\n#ifdef SHADOW0\n#ifdef SHADOWVSM0\n\tshadow = computeShadowWithVSM(vPositionFromLight0, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x);\n#else\n#ifdef SHADOWPCF0\n\t#if defined(POINTLIGHT0)\n\tshadow = computeShadowWithPCFCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x);\n\t#else\n\tshadow = computeShadowWithPCF(vPositionFromLight0, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);\n\t#endif\n#else\n\t#if defined(POINTLIGHT0)\n\tshadow = computeShadowCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);\n\t#else\n\tshadow = computeShadow(vPositionFromLight0, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);\n\t#endif\n#endif\n#endif\n#else\n\tshadow = 1.;\n#endif\n\tdiffuseBase += info.diffuse * shadow;\n#endif\n\n#ifdef LIGHT1\n#ifdef SPOTLIGHT1\n\tinfo = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1.rgb, vLightDiffuse1.a);\n#endif\n#ifdef HEMILIGHT1\n\tinfo = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightGround1.a);\n#endif\n#if defined(POINTLIGHT1) || defined(DIRLIGHT1)\n\tinfo = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightDiffuse1.a);\n#endif\n#ifdef SHADOW1\n#ifdef SHADOWVSM1\n\tshadow = computeShadowWithVSM(vPositionFromLight1, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x);\n#else\n#ifdef SHADOWPCF1\n#if defined(POINTLIGHT1)\n\tshadow = computeShadowWithPCFCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x);\n#else\n\tshadow = computeShadowWithPCF(vPositionFromLight1, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);\n#endif\n#else\n\t#if defined(POINTLIGHT1)\n\tshadow = computeShadowCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);\n\t#else\n\tshadow = computeShadow(vPositionFromLight1, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);\n\t#endif\n#endif\n#endif\n#else\n\tshadow = 1.;\n#endif\n\tdiffuseBase += info.diffuse * shadow;\n#endif\n\n#ifdef LIGHT2\n#ifdef SPOTLIGHT2\n\tinfo = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2.rgb, vLightDiffuse2.a);\n#endif\n#ifdef HEMILIGHT2\n\tinfo = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightGround2);\n#endif\n#if defined(POINTLIGHT2) || defined(DIRLIGHT2)\n\tinfo = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightDiffuse2.a);\n#endif\n#ifdef SHADOW2\n#ifdef SHADOWVSM2\n\tshadow = computeShadowWithVSM(vPositionFromLight2, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x);\n#else\n#ifdef SHADOWPCF2\n#if defined(POINTLIGHT2)\n\tshadow = computeShadowWithPCFCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x);\n#else\n\tshadow = computeShadowWithPCF(vPositionFromLight2, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);\n#endif\n#else\n\t#if defined(POINTLIGHT2)\n\tshadow = computeShadowCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);\n\t#else\n\tshadow = computeShadow(vPositionFromLight2, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);\n\t#endif\n#endif\t\n#endif\t\n#else\n\tshadow = 1.;\n#endif\n\tdiffuseBase += info.diffuse * shadow;\n#endif\n\n#ifdef LIGHT3\n#ifdef SPOTLIGHT3\n\tinfo = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3.rgb, vLightDiffuse3.a);\n#endif\n#ifdef HEMILIGHT3\n\tinfo = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightGround3);\n#endif\n#if defined(POINTLIGHT3) || defined(DIRLIGHT3)\n\tinfo = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightDiffuse3.a);\n#endif\n#ifdef SHADOW3\n#ifdef SHADOWVSM3\n\t\tshadow = computeShadowWithVSM(vPositionFromLight3, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x);\n#else\n#ifdef SHADOWPCF3\n#if defined(POINTLIGHT3)\n\tshadow = computeShadowWithPCFCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x);\n#else\n\tshadow = computeShadowWithPCF(vPositionFromLight3, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);\n#endif\n#else\n\t#if defined(POINTLIGHT3)\n\tshadow = computeShadowCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);\n\t#else\n\tshadow = computeShadow(vPositionFromLight3, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);\n\t#endif\n#endif\t\n#endif\t\n#else\n\tshadow = 1.;\n#endif\n\tdiffuseBase += info.diffuse * shadow;\n#endif\n\n#ifdef VERTEXALPHA\n\talpha *= vColor.a;\n#endif\n\n\tvec3 finalDiffuse = clamp(diffuseBase * diffuseColor, 0.0, 1.0) * baseColor.rgb;\n\n\t// Composition\n\t#ifdef HIGHLEVEL\n\tvec4 color = vec4(finalDiffuse, alpha);\n\t#else\n\tfloat r = vfur_length * 0.5;\n\tvec4 color = vec4(finalDiffuse * (0.5 + r), alpha);\n\t#endif\n\t\n#ifdef FOG\n\tfloat fog = CalcFogFactor();\n\tcolor.rgb = fog * color.rgb + (1.0 - fog) * vFogColor;\n#endif\n\n\tgl_FragColor = color;\n}";