babylon.furMaterial.ts 25 KB

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