babylon.furMaterial.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON {
  3. class FurMaterialDefines extends MaterialDefines {
  4. public DIFFUSE = false;
  5. public HEIGHTMAP = false;
  6. public CLIPPLANE = false;
  7. public CLIPPLANE2 = false;
  8. public CLIPPLANE3 = false;
  9. public CLIPPLANE4 = false;
  10. public ALPHATEST = false;
  11. public DEPTHPREPASS = false;
  12. public POINTSIZE = false;
  13. public FOG = false;
  14. public NORMAL = false;
  15. public UV1 = false;
  16. public UV2 = false;
  17. public VERTEXCOLOR = false;
  18. public VERTEXALPHA = false;
  19. public NUM_BONE_INFLUENCERS = 0;
  20. public BonesPerMesh = 0;
  21. public INSTANCES = false;
  22. public HIGHLEVEL = false;
  23. constructor() {
  24. super();
  25. this.rebuild();
  26. }
  27. }
  28. export class FurMaterial extends PushMaterial {
  29. @serializeAsTexture("diffuseTexture")
  30. private _diffuseTexture: BaseTexture;
  31. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  32. public diffuseTexture: BaseTexture;
  33. @serializeAsTexture("heightTexture")
  34. private _heightTexture: BaseTexture;
  35. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  36. public heightTexture: BaseTexture;
  37. @serializeAsColor3()
  38. public diffuseColor = new Color3(1, 1, 1);
  39. @serialize()
  40. public furLength: number = 1;
  41. @serialize()
  42. public furAngle: number = 0;
  43. @serializeAsColor3()
  44. public furColor = new Color3(0.44, 0.21, 0.02);
  45. @serialize()
  46. public furOffset: number = 0.0;
  47. @serialize()
  48. public furSpacing: number = 12;
  49. @serializeAsVector3()
  50. public furGravity = new Vector3(0, 0, 0);
  51. @serialize()
  52. public furSpeed: number = 100;
  53. @serialize()
  54. public furDensity: number = 20;
  55. @serialize()
  56. public furOcclusion: number = 0.0;
  57. public furTexture: DynamicTexture;
  58. @serialize("disableLighting")
  59. private _disableLighting = false;
  60. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  61. public disableLighting: boolean;
  62. @serialize("maxSimultaneousLights")
  63. private _maxSimultaneousLights = 4;
  64. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  65. public maxSimultaneousLights: number;
  66. @serialize()
  67. public highLevelFur: boolean = true;
  68. public _meshes: AbstractMesh[];
  69. private _renderId: number;
  70. private _furTime: number = 0;
  71. constructor(name: string, scene: Scene) {
  72. super(name, scene);
  73. }
  74. @serialize()
  75. public get furTime() {
  76. return this._furTime;
  77. }
  78. public set furTime(furTime: number) {
  79. this._furTime = furTime;
  80. }
  81. public needAlphaBlending(): boolean {
  82. return (this.alpha < 1.0);
  83. }
  84. public needAlphaTesting(): boolean {
  85. return false;
  86. }
  87. public getAlphaTestTexture(): Nullable<BaseTexture> {
  88. return null;
  89. }
  90. public updateFur(): void {
  91. for (var i = 1; i < this._meshes.length; i++) {
  92. var offsetFur = <FurMaterial>this._meshes[i].material;
  93. offsetFur.furLength = this.furLength;
  94. offsetFur.furAngle = this.furAngle;
  95. offsetFur.furGravity = this.furGravity;
  96. offsetFur.furSpacing = this.furSpacing;
  97. offsetFur.furSpeed = this.furSpeed;
  98. offsetFur.furColor = this.furColor;
  99. offsetFur.diffuseTexture = this.diffuseTexture;
  100. offsetFur.furTexture = this.furTexture;
  101. offsetFur.highLevelFur = this.highLevelFur;
  102. offsetFur.furTime = this.furTime;
  103. offsetFur.furDensity = this.furDensity;
  104. }
  105. }
  106. // Methods
  107. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
  108. if (this.isFrozen) {
  109. if (this._wasPreviouslyReady && subMesh.effect) {
  110. return true;
  111. }
  112. }
  113. if (!subMesh._materialDefines) {
  114. subMesh._materialDefines = new FurMaterialDefines();
  115. }
  116. var defines = <FurMaterialDefines>subMesh._materialDefines;
  117. var scene = this.getScene();
  118. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  119. if (this._renderId === scene.getRenderId()) {
  120. return true;
  121. }
  122. }
  123. var engine = scene.getEngine();
  124. // Textures
  125. if (defines._areTexturesDirty) {
  126. if (scene.texturesEnabled) {
  127. if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  128. if (!this.diffuseTexture.isReady()) {
  129. return false;
  130. } else {
  131. defines._needUVs = true;
  132. defines.DIFFUSE = true;
  133. }
  134. }
  135. if (this.heightTexture && engine.getCaps().maxVertexTextureImageUnits) {
  136. if (!this.heightTexture.isReady()) {
  137. return false;
  138. } else {
  139. defines._needUVs = true;
  140. defines.HEIGHTMAP = true;
  141. }
  142. }
  143. }
  144. }
  145. // High level
  146. if (this.highLevelFur !== defines.HIGHLEVEL) {
  147. defines.HIGHLEVEL = true;
  148. defines.markAsUnprocessed();
  149. }
  150. // Misc.
  151. MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, this._shouldTurnAlphaTestOn(mesh), defines);
  152. // Lights
  153. defines._needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, false, this._maxSimultaneousLights, this._disableLighting);
  154. // Values that need to be evaluated on every frame
  155. MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances ? true : false);
  156. // Attribs
  157. MaterialHelper.PrepareDefinesForAttributes(mesh, defines, true, true);
  158. // Get correct effect
  159. if (defines.isDirty) {
  160. defines.markAsProcessed();
  161. scene.resetCachedMaterial();
  162. // Fallbacks
  163. var fallbacks = new EffectFallbacks();
  164. if (defines.FOG) {
  165. fallbacks.addFallback(1, "FOG");
  166. }
  167. MaterialHelper.HandleFallbacksForShadows(defines, fallbacks, this.maxSimultaneousLights);
  168. if (defines.NUM_BONE_INFLUENCERS > 0) {
  169. fallbacks.addCPUSkinningFallback(0, mesh);
  170. }
  171. //Attributes
  172. var attribs = [VertexBuffer.PositionKind];
  173. if (defines.NORMAL) {
  174. attribs.push(VertexBuffer.NormalKind);
  175. }
  176. if (defines.UV1) {
  177. attribs.push(VertexBuffer.UVKind);
  178. }
  179. if (defines.UV2) {
  180. attribs.push(VertexBuffer.UV2Kind);
  181. }
  182. if (defines.VERTEXCOLOR) {
  183. attribs.push(VertexBuffer.ColorKind);
  184. }
  185. MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  186. MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  187. // Legacy browser patch
  188. var shaderName = "fur";
  189. var join = defines.toString();
  190. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  191. "vFogInfos", "vFogColor", "pointSize",
  192. "vDiffuseInfos",
  193. "mBones",
  194. "vClipPlane", "vClipPlane2", "vClipPlane3", "vClipPlane4", "diffuseMatrix",
  195. "furLength", "furAngle", "furColor", "furOffset", "furGravity", "furTime", "furSpacing", "furDensity", "furOcclusion"
  196. ];
  197. var samplers = ["diffuseSampler",
  198. "heightTexture", "furTexture"
  199. ];
  200. var uniformBuffers = new Array<string>();
  201. MaterialHelper.PrepareUniformsAndSamplersList(<EffectCreationOptions>{
  202. uniformsNames: uniforms,
  203. uniformBuffersNames: uniformBuffers,
  204. samplers: samplers,
  205. defines: defines,
  206. maxSimultaneousLights: this.maxSimultaneousLights
  207. });
  208. subMesh.setEffect(scene.getEngine().createEffect(shaderName,
  209. <EffectCreationOptions>{
  210. attributes: attribs,
  211. uniformsNames: uniforms,
  212. uniformBuffersNames: uniformBuffers,
  213. samplers: samplers,
  214. defines: join,
  215. fallbacks: fallbacks,
  216. onCompiled: this.onCompiled,
  217. onError: this.onError,
  218. indexParameters: { maxSimultaneousLights: this.maxSimultaneousLights }
  219. }, engine), defines);
  220. }
  221. if (!subMesh.effect || !subMesh.effect.isReady()) {
  222. return false;
  223. }
  224. this._renderId = scene.getRenderId();
  225. this._wasPreviouslyReady = true;
  226. return true;
  227. }
  228. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  229. var scene = this.getScene();
  230. var defines = <FurMaterialDefines>subMesh._materialDefines;
  231. if (!defines) {
  232. return;
  233. }
  234. var effect = subMesh.effect;
  235. if (!effect) {
  236. return;
  237. }
  238. this._activeEffect = effect;
  239. // Matrices
  240. this.bindOnlyWorldMatrix(world);
  241. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  242. // Bones
  243. MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
  244. if (scene.getCachedMaterial() !== this) {
  245. // Textures
  246. if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  247. this._activeEffect.setTexture("diffuseSampler", this._diffuseTexture);
  248. this._activeEffect.setFloat2("vDiffuseInfos", this._diffuseTexture.coordinatesIndex, this._diffuseTexture.level);
  249. this._activeEffect.setMatrix("diffuseMatrix", this._diffuseTexture.getTextureMatrix());
  250. }
  251. if (this._heightTexture) {
  252. this._activeEffect.setTexture("heightTexture", this._heightTexture);
  253. }
  254. // Clip plane
  255. MaterialHelper.BindClipPlane(this._activeEffect, scene);
  256. // Point size
  257. if (this.pointsCloud) {
  258. this._activeEffect.setFloat("pointSize", this.pointSize);
  259. }
  260. MaterialHelper.BindEyePosition(effect, scene);
  261. }
  262. this._activeEffect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  263. if (scene.lightsEnabled && !this.disableLighting) {
  264. MaterialHelper.BindLights(scene, mesh, this._activeEffect, defines, this.maxSimultaneousLights);
  265. }
  266. // View
  267. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  268. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  269. }
  270. // Fog
  271. MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  272. this._activeEffect.setFloat("furLength", this.furLength);
  273. this._activeEffect.setFloat("furAngle", this.furAngle);
  274. this._activeEffect.setColor4("furColor", this.furColor, 1.0);
  275. if (this.highLevelFur) {
  276. this._activeEffect.setVector3("furGravity", this.furGravity);
  277. this._activeEffect.setFloat("furOffset", this.furOffset);
  278. this._activeEffect.setFloat("furSpacing", this.furSpacing);
  279. this._activeEffect.setFloat("furDensity", this.furDensity);
  280. this._activeEffect.setFloat("furOcclusion", this.furOcclusion);
  281. this._furTime += this.getScene().getEngine().getDeltaTime() / this.furSpeed;
  282. this._activeEffect.setFloat("furTime", this._furTime);
  283. this._activeEffect.setTexture("furTexture", this.furTexture);
  284. }
  285. this._afterBind(mesh, this._activeEffect);
  286. }
  287. public getAnimatables(): IAnimatable[] {
  288. var results = [];
  289. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  290. results.push(this.diffuseTexture);
  291. }
  292. if (this.heightTexture && this.heightTexture.animations && this.heightTexture.animations.length > 0) {
  293. results.push(this.heightTexture);
  294. }
  295. return results;
  296. }
  297. public getActiveTextures(): BaseTexture[] {
  298. var activeTextures = super.getActiveTextures();
  299. if (this._diffuseTexture) {
  300. activeTextures.push(this._diffuseTexture);
  301. }
  302. if (this._heightTexture) {
  303. activeTextures.push(this._heightTexture);
  304. }
  305. return activeTextures;
  306. }
  307. public hasTexture(texture: BaseTexture): boolean {
  308. if (super.hasTexture(texture)) {
  309. return true;
  310. }
  311. if (this.diffuseTexture === texture) {
  312. return true;
  313. }
  314. if (this._heightTexture === texture) {
  315. return true;
  316. }
  317. return false;
  318. }
  319. public dispose(forceDisposeEffect?: boolean): void {
  320. if (this.diffuseTexture) {
  321. this.diffuseTexture.dispose();
  322. }
  323. if (this._meshes) {
  324. for (var i = 1; i < this._meshes.length; i++) {
  325. let mat = this._meshes[i].material;
  326. if (mat) {
  327. mat.dispose(forceDisposeEffect);
  328. }
  329. this._meshes[i].dispose();
  330. }
  331. }
  332. super.dispose(forceDisposeEffect);
  333. }
  334. public clone(name: string): FurMaterial {
  335. return SerializationHelper.Clone(() => new FurMaterial(name, this.getScene()), this);
  336. }
  337. public serialize(): any {
  338. var serializationObject = SerializationHelper.Serialize(this);
  339. serializationObject.customType = "BABYLON.FurMaterial";
  340. if (this._meshes) {
  341. serializationObject.sourceMeshName = this._meshes[0].name;
  342. serializationObject.quality = this._meshes.length;
  343. }
  344. return serializationObject;
  345. }
  346. public getClassName(): string {
  347. return "FurMaterial";
  348. }
  349. // Statics
  350. public static Parse(source: any, scene: Scene, rootUrl: string): FurMaterial {
  351. var material = SerializationHelper.Parse(() => new FurMaterial(source.name, scene), source, scene, rootUrl);
  352. if (source.sourceMeshName && material.highLevelFur) {
  353. scene.executeWhenReady(() => {
  354. var sourceMesh = <Mesh>scene.getMeshByName(source.sourceMeshName);
  355. if (sourceMesh) {
  356. var furTexture = FurMaterial.GenerateTexture("Fur Texture", scene);
  357. material.furTexture = furTexture;
  358. FurMaterial.FurifyMesh(sourceMesh, source.quality);
  359. }
  360. });
  361. }
  362. return material;
  363. }
  364. public static GenerateTexture(name: string, scene: Scene): DynamicTexture {
  365. // Generate fur textures
  366. var texture = new DynamicTexture("FurTexture " + name, 256, scene, true);
  367. var context = texture.getContext();
  368. for (var i = 0; i < 20000; ++i) {
  369. context.fillStyle = "rgba(255, " + Math.floor(Math.random() * 255) + ", " + Math.floor(Math.random() * 255) + ", 1)";
  370. context.fillRect((Math.random() * texture.getSize().width), (Math.random() * texture.getSize().height), 2, 2);
  371. }
  372. texture.update(false);
  373. texture.wrapU = Texture.WRAP_ADDRESSMODE;
  374. texture.wrapV = Texture.WRAP_ADDRESSMODE;
  375. return texture;
  376. }
  377. // Creates and returns an array of meshes used as shells for the Fur Material
  378. // that can be disposed later in your code
  379. // The quality is in interval [0, 100]
  380. public static FurifyMesh(sourceMesh: Mesh, quality: number): Mesh[] {
  381. var meshes = [sourceMesh];
  382. var mat: FurMaterial = <FurMaterial>sourceMesh.material;
  383. var i;
  384. if (!(mat instanceof FurMaterial)) {
  385. throw "The material of the source mesh must be a Fur Material";
  386. }
  387. for (i = 1; i < quality; i++) {
  388. var offsetFur = new BABYLON.FurMaterial(mat.name + i, sourceMesh.getScene());
  389. sourceMesh.getScene().materials.pop();
  390. Tags.EnableFor(offsetFur);
  391. Tags.AddTagsTo(offsetFur, "furShellMaterial");
  392. offsetFur.furLength = mat.furLength;
  393. offsetFur.furAngle = mat.furAngle;
  394. offsetFur.furGravity = mat.furGravity;
  395. offsetFur.furSpacing = mat.furSpacing;
  396. offsetFur.furSpeed = mat.furSpeed;
  397. offsetFur.furColor = mat.furColor;
  398. offsetFur.diffuseTexture = mat.diffuseTexture;
  399. offsetFur.furOffset = i / quality;
  400. offsetFur.furTexture = mat.furTexture;
  401. offsetFur.highLevelFur = mat.highLevelFur;
  402. offsetFur.furTime = mat.furTime;
  403. offsetFur.furDensity = mat.furDensity;
  404. var offsetMesh = sourceMesh.clone(sourceMesh.name + i);
  405. offsetMesh.material = offsetFur;
  406. offsetMesh.skeleton = sourceMesh.skeleton;
  407. offsetMesh.position = Vector3.Zero();
  408. meshes.push(offsetMesh);
  409. }
  410. for (i = 1; i < meshes.length; i++) {
  411. meshes[i].parent = sourceMesh;
  412. }
  413. (<FurMaterial>sourceMesh.material)._meshes = meshes;
  414. return meshes;
  415. }
  416. }
  417. }