babylon.furMaterial.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. (function webpackUniversalModuleDefinition(root, factory) {
  2. if(typeof exports === 'object' && typeof module === 'object')
  3. module.exports = factory(require("babylonjs"));
  4. else if(typeof define === 'function' && define.amd)
  5. define("babylonjs-materials", ["babylonjs"], factory);
  6. else if(typeof exports === 'object')
  7. exports["babylonjs-materials"] = factory(require("babylonjs"));
  8. else
  9. root["MATLIB"] = factory(root["BABYLON"]);
  10. })(window, function(__WEBPACK_EXTERNAL_MODULE_babylonjs__) {
  11. return /******/ (function(modules) { // webpackBootstrap
  12. /******/ // The module cache
  13. /******/ var installedModules = {};
  14. /******/
  15. /******/ // The require function
  16. /******/ function __webpack_require__(moduleId) {
  17. /******/
  18. /******/ // Check if module is in cache
  19. /******/ if(installedModules[moduleId]) {
  20. /******/ return installedModules[moduleId].exports;
  21. /******/ }
  22. /******/ // Create a new module (and put it into the cache)
  23. /******/ var module = installedModules[moduleId] = {
  24. /******/ i: moduleId,
  25. /******/ l: false,
  26. /******/ exports: {}
  27. /******/ };
  28. /******/
  29. /******/ // Execute the module function
  30. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  31. /******/
  32. /******/ // Flag the module as loaded
  33. /******/ module.l = true;
  34. /******/
  35. /******/ // Return the exports of the module
  36. /******/ return module.exports;
  37. /******/ }
  38. /******/
  39. /******/
  40. /******/ // expose the modules object (__webpack_modules__)
  41. /******/ __webpack_require__.m = modules;
  42. /******/
  43. /******/ // expose the module cache
  44. /******/ __webpack_require__.c = installedModules;
  45. /******/
  46. /******/ // define getter function for harmony exports
  47. /******/ __webpack_require__.d = function(exports, name, getter) {
  48. /******/ if(!__webpack_require__.o(exports, name)) {
  49. /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
  50. /******/ }
  51. /******/ };
  52. /******/
  53. /******/ // define __esModule on exports
  54. /******/ __webpack_require__.r = function(exports) {
  55. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  56. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  57. /******/ }
  58. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  59. /******/ };
  60. /******/
  61. /******/ // create a fake namespace object
  62. /******/ // mode & 1: value is a module id, require it
  63. /******/ // mode & 2: merge all properties of value into the ns
  64. /******/ // mode & 4: return value when already ns object
  65. /******/ // mode & 8|1: behave like require
  66. /******/ __webpack_require__.t = function(value, mode) {
  67. /******/ if(mode & 1) value = __webpack_require__(value);
  68. /******/ if(mode & 8) return value;
  69. /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
  70. /******/ var ns = Object.create(null);
  71. /******/ __webpack_require__.r(ns);
  72. /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
  73. /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
  74. /******/ return ns;
  75. /******/ };
  76. /******/
  77. /******/ // getDefaultExport function for compatibility with non-harmony modules
  78. /******/ __webpack_require__.n = function(module) {
  79. /******/ var getter = module && module.__esModule ?
  80. /******/ function getDefault() { return module['default']; } :
  81. /******/ function getModuleExports() { return module; };
  82. /******/ __webpack_require__.d(getter, 'a', getter);
  83. /******/ return getter;
  84. /******/ };
  85. /******/
  86. /******/ // Object.prototype.hasOwnProperty.call
  87. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  88. /******/
  89. /******/ // __webpack_public_path__
  90. /******/ __webpack_require__.p = "";
  91. /******/
  92. /******/
  93. /******/ // Load entry module and return exports
  94. /******/ return __webpack_require__(__webpack_require__.s = "./legacy/legacy-fur.ts");
  95. /******/ })
  96. /************************************************************************/
  97. /******/ ({
  98. /***/ "../Tools/Gulp/node_modules/webpack/buildin/global.js":
  99. /*!************************************************************!*\
  100. !*** ../Tools/Gulp/node_modules/webpack/buildin/global.js ***!
  101. \************************************************************/
  102. /*! no static exports found */
  103. /***/ (function(module, exports) {
  104. var g;
  105. // This works in non-strict mode
  106. g = (function() {
  107. return this;
  108. })();
  109. try {
  110. // This works if eval is allowed (see CSP)
  111. g = g || Function("return this")() || (1, eval)("this");
  112. } catch (e) {
  113. // This works if the window reference is available
  114. if (typeof window === "object") g = window;
  115. }
  116. // g can still be undefined, but nothing to do about it...
  117. // We return undefined, instead of nothing here, so it's
  118. // easier to handle this case. if(!global) { ...}
  119. module.exports = g;
  120. /***/ }),
  121. /***/ "./legacy/legacy-fur.ts":
  122. /*!******************************!*\
  123. !*** ./legacy/legacy-fur.ts ***!
  124. \******************************/
  125. /*! no static exports found */
  126. /***/ (function(module, exports, __webpack_require__) {
  127. "use strict";
  128. /* WEBPACK VAR INJECTION */(function(global) {
  129. function __export(m) {
  130. for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
  131. }
  132. Object.defineProperty(exports, "__esModule", { value: true });
  133. var MatLib = __webpack_require__(/*! ../src/fur/index */ "./src/fur/index.ts");
  134. /**
  135. * This is the entry point for the UMD module.
  136. * The entry point for a future ESM package should be index.ts
  137. */
  138. var globalObject = (typeof global !== 'undefined') ? global : ((typeof window !== 'undefined') ? window : undefined);
  139. if (typeof globalObject !== "undefined") {
  140. for (var key in MatLib) {
  141. globalObject.BABYLON[key] = MatLib[key];
  142. }
  143. }
  144. __export(__webpack_require__(/*! ../src/fur/index */ "./src/fur/index.ts"));
  145. /* WEBPACK VAR INJECTION */}.call(this, __webpack_require__(/*! ./../../Tools/Gulp/node_modules/webpack/buildin/global.js */ "../Tools/Gulp/node_modules/webpack/buildin/global.js")))
  146. /***/ }),
  147. /***/ "./src/fur/fur.fragment.fx":
  148. /*!*********************************!*\
  149. !*** ./src/fur/fur.fragment.fx ***!
  150. \*********************************/
  151. /*! no static exports found */
  152. /***/ (function(module, exports) {
  153. module.exports = "precision highp float;\n\nuniform vec3 vEyePosition;\nuniform vec4 vDiffuseColor;\n\nuniform vec4 furColor;\nuniform float furLength;\nvarying vec3 vPositionW;\nvarying float vfur_length;\n#ifdef NORMAL\nvarying vec3 vNormalW;\n#endif\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n\n#include<helperFunctions>\n\n#include<__decl__lightFragment>[0..maxSimultaneousLights]\n\n#ifdef DIFFUSE\nvarying vec2 vDiffuseUV;\nuniform sampler2D diffuseSampler;\nuniform vec2 vDiffuseInfos;\n#endif\n\n#ifdef HIGHLEVEL\nuniform float furOffset;\nuniform float furOcclusion;\nuniform sampler2D furTexture;\nvarying vec2 vFurUV;\n#endif\n#include<lightsFragmentFunctions>\n#include<shadowsFragmentFunctions>\n#include<fogFragmentDeclaration>\n#include<clipPlaneFragmentDeclaration>\nfloat Rand(vec3 rv) {\nfloat x=dot(rv,vec3(12.9898,78.233,24.65487));\nreturn fract(sin(x)*43758.5453);\n}\nvoid main(void) {\n\n#include<clipPlaneFragment>\nvec3 viewDirectionW=normalize(vEyePosition-vPositionW);\n\nvec4 baseColor=furColor;\nvec3 diffuseColor=vDiffuseColor.rgb;\n\nfloat alpha=vDiffuseColor.a;\n#ifdef DIFFUSE\nbaseColor*=texture2D(diffuseSampler,vDiffuseUV);\n#ifdef ALPHATEST\nif (baseColor.a<0.4)\ndiscard;\n#endif\n#include<depthPrePass>\nbaseColor.rgb*=vDiffuseInfos.y;\n#endif\n#ifdef VERTEXCOLOR\nbaseColor.rgb*=vColor.rgb;\n#endif\n\n#ifdef NORMAL\nvec3 normalW=normalize(vNormalW);\n#else\nvec3 normalW=vec3(1.0,1.0,1.0);\n#endif\n#ifdef HIGHLEVEL\n\nvec4 furTextureColor=texture2D(furTexture,vec2(vFurUV.x,vFurUV.y));\nif (furTextureColor.a<=0.0 || furTextureColor.g<furOffset) {\ndiscard;\n}\nfloat occlusion=mix(0.0,furTextureColor.b*1.2,furOffset);\nbaseColor=vec4(baseColor.xyz*max(occlusion,furOcclusion),1.1-furOffset);\n#endif\n\nvec3 diffuseBase=vec3(0.,0.,0.);\nlightingInfo info;\nfloat shadow=1.;\nfloat glossiness=0.;\n#ifdef SPECULARTERM\nvec3 specularBase=vec3(0.,0.,0.);\n#endif\n#include<lightFragment>[0..maxSimultaneousLights]\n#ifdef VERTEXALPHA\nalpha*=vColor.a;\n#endif\nvec3 finalDiffuse=clamp(diffuseBase.rgb*baseColor.rgb,0.0,1.0);\n\n#ifdef HIGHLEVEL\nvec4 color=vec4(finalDiffuse,alpha);\n#else\nfloat r=vfur_length/furLength*0.5;\nvec4 color=vec4(finalDiffuse*(0.5+r),alpha);\n#endif\n#include<fogFragment>\ngl_FragColor=color;\n}"
  154. /***/ }),
  155. /***/ "./src/fur/fur.vertex.fx":
  156. /*!*******************************!*\
  157. !*** ./src/fur/fur.vertex.fx ***!
  158. \*******************************/
  159. /*! no static exports found */
  160. /***/ (function(module, exports) {
  161. module.exports = "precision highp float;\n\nattribute vec3 position;\nattribute vec3 normal;\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#include<bonesDeclaration>\n\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#ifdef HIGHLEVEL\nvarying vec2 vFurUV;\n#endif\n#include<instancesDeclaration>\nuniform mat4 view;\nuniform mat4 viewProjection;\n#ifdef DIFFUSE\nvarying vec2 vDiffuseUV;\nuniform mat4 diffuseMatrix;\nuniform vec2 vDiffuseInfos;\n#endif\n#ifdef POINTSIZE\nuniform float pointSize;\n#endif\n\nvarying vec3 vPositionW;\n#ifdef NORMAL\nvarying vec3 vNormalW;\n#endif\nvarying float vfur_length;\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n#include<clipPlaneVertexDeclaration>\n#include<fogVertexDeclaration>\n#include<__decl__lightFragment>[0..maxSimultaneousLights]\nfloat Rand(vec3 rv) {\nfloat x=dot(rv,vec3(12.9898,78.233,24.65487));\nreturn fract(sin(x)*43758.5453);\n}\nvoid main(void) {\n#include<instancesVertex>\n#include<bonesVertex>\n\nfloat r=Rand(position);\n#ifdef HEIGHTMAP\n#if __VERSION__>100\nvfur_length=furLength*texture(heightTexture,uv).x;\n#else\nvfur_length=furLength*texture2D(heightTexture,uv).r;\n#endif\n#else \nvfur_length=(furLength*r);\n#endif\nvec3 tangent1=vec3(normal.y,-normal.x,0);\nvec3 tangent2=vec3(-normal.z,0,normal.x);\nr=Rand(tangent1*r);\nfloat J=(2.0+4.0*r);\nr=Rand(tangent2*r);\nfloat K=(2.0+2.0*r);\ntangent1=tangent1*J+tangent2*K;\ntangent1=normalize(tangent1);\nvec3 newPosition=position+normal*vfur_length*cos(furAngle)+tangent1*vfur_length*sin(furAngle);\n#ifdef HIGHLEVEL\n\nvec3 forceDirection=vec3(0.0,0.0,0.0);\nforceDirection.x=sin(furTime+position.x*0.05)*0.2;\nforceDirection.y=cos(furTime*0.7+position.y*0.04)*0.2;\nforceDirection.z=sin(furTime*0.7+position.z*0.04)*0.2;\nvec3 displacement=vec3(0.0,0.0,0.0);\ndisplacement=furGravity+forceDirection;\nfloat displacementFactor=pow(furOffset,3.0);\nvec3 aNormal=normal;\naNormal.xyz+=displacement*displacementFactor;\nnewPosition=vec3(newPosition.x,newPosition.y,newPosition.z)+(normalize(aNormal)*furOffset*furSpacing);\n#endif\n#ifdef NORMAL\nvNormalW=normalize(vec3(finalWorld*vec4(normal,0.0)));\n#endif\n\ngl_Position=viewProjection*finalWorld*vec4(newPosition,1.0);\nvec4 worldPos=finalWorld*vec4(newPosition,1.0);\nvPositionW=vec3(worldPos);\n\n#ifndef UV1\nvec2 uv=vec2(0.,0.);\n#endif\n#ifndef UV2\nvec2 uv2=vec2(0.,0.);\n#endif\n#ifdef DIFFUSE\nif (vDiffuseInfos.x == 0.)\n{\nvDiffuseUV=vec2(diffuseMatrix*vec4(uv,1.0,0.0));\n}\nelse\n{\nvDiffuseUV=vec2(diffuseMatrix*vec4(uv2,1.0,0.0));\n}\n#ifdef HIGHLEVEL\nvFurUV=vDiffuseUV*furDensity;\n#endif\n#else\n#ifdef HIGHLEVEL\nvFurUV=uv*furDensity;\n#endif\n#endif\n\n#include<clipPlaneVertex>\n\n#include<fogVertex>\n\n#include<shadowsVertex>[0..maxSimultaneousLights]\n\n#ifdef VERTEXCOLOR\nvColor=color;\n#endif\n\n#ifdef POINTSIZE\ngl_PointSize=pointSize;\n#endif\n}\n"
  162. /***/ }),
  163. /***/ "./src/fur/furMaterial.ts":
  164. /*!********************************!*\
  165. !*** ./src/fur/furMaterial.ts ***!
  166. \********************************/
  167. /*! no static exports found */
  168. /***/ (function(module, exports, __webpack_require__) {
  169. "use strict";
  170. var __extends = (this && this.__extends) || (function () {
  171. var extendStatics = function (d, b) {
  172. extendStatics = Object.setPrototypeOf ||
  173. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  174. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  175. return extendStatics(d, b);
  176. }
  177. return function (d, b) {
  178. extendStatics(d, b);
  179. function __() { this.constructor = d; }
  180. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  181. };
  182. })();
  183. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  184. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  185. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  186. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  187. return c > 3 && r && Object.defineProperty(target, key, r), r;
  188. };
  189. Object.defineProperty(exports, "__esModule", { value: true });
  190. var babylonjs_1 = __webpack_require__(/*! babylonjs */ "babylonjs");
  191. babylonjs_1.Effect.ShadersStore["furPixelShader"] = __webpack_require__(/*! ./fur.fragment.fx */ "./src/fur/fur.fragment.fx");
  192. babylonjs_1.Effect.ShadersStore["furVertexShader"] = __webpack_require__(/*! ./fur.vertex.fx */ "./src/fur/fur.vertex.fx");
  193. var FurMaterialDefines = /** @class */ (function (_super) {
  194. __extends(FurMaterialDefines, _super);
  195. function FurMaterialDefines() {
  196. var _this = _super.call(this) || this;
  197. _this.DIFFUSE = false;
  198. _this.HEIGHTMAP = false;
  199. _this.CLIPPLANE = false;
  200. _this.CLIPPLANE2 = false;
  201. _this.CLIPPLANE3 = false;
  202. _this.CLIPPLANE4 = false;
  203. _this.ALPHATEST = false;
  204. _this.DEPTHPREPASS = false;
  205. _this.POINTSIZE = false;
  206. _this.FOG = false;
  207. _this.NORMAL = false;
  208. _this.UV1 = false;
  209. _this.UV2 = false;
  210. _this.VERTEXCOLOR = false;
  211. _this.VERTEXALPHA = false;
  212. _this.NUM_BONE_INFLUENCERS = 0;
  213. _this.BonesPerMesh = 0;
  214. _this.INSTANCES = false;
  215. _this.HIGHLEVEL = false;
  216. _this.rebuild();
  217. return _this;
  218. }
  219. return FurMaterialDefines;
  220. }(babylonjs_1.MaterialDefines));
  221. var FurMaterial = /** @class */ (function (_super) {
  222. __extends(FurMaterial, _super);
  223. function FurMaterial(name, scene) {
  224. var _this = _super.call(this, name, scene) || this;
  225. _this.diffuseColor = new babylonjs_1.Color3(1, 1, 1);
  226. _this.furLength = 1;
  227. _this.furAngle = 0;
  228. _this.furColor = new babylonjs_1.Color3(0.44, 0.21, 0.02);
  229. _this.furOffset = 0.0;
  230. _this.furSpacing = 12;
  231. _this.furGravity = new babylonjs_1.Vector3(0, 0, 0);
  232. _this.furSpeed = 100;
  233. _this.furDensity = 20;
  234. _this.furOcclusion = 0.0;
  235. _this._disableLighting = false;
  236. _this._maxSimultaneousLights = 4;
  237. _this.highLevelFur = true;
  238. _this._furTime = 0;
  239. return _this;
  240. }
  241. Object.defineProperty(FurMaterial.prototype, "furTime", {
  242. get: function () {
  243. return this._furTime;
  244. },
  245. set: function (furTime) {
  246. this._furTime = furTime;
  247. },
  248. enumerable: true,
  249. configurable: true
  250. });
  251. FurMaterial.prototype.needAlphaBlending = function () {
  252. return (this.alpha < 1.0);
  253. };
  254. FurMaterial.prototype.needAlphaTesting = function () {
  255. return false;
  256. };
  257. FurMaterial.prototype.getAlphaTestTexture = function () {
  258. return null;
  259. };
  260. FurMaterial.prototype.updateFur = function () {
  261. for (var i = 1; i < this._meshes.length; i++) {
  262. var offsetFur = this._meshes[i].material;
  263. offsetFur.furLength = this.furLength;
  264. offsetFur.furAngle = this.furAngle;
  265. offsetFur.furGravity = this.furGravity;
  266. offsetFur.furSpacing = this.furSpacing;
  267. offsetFur.furSpeed = this.furSpeed;
  268. offsetFur.furColor = this.furColor;
  269. offsetFur.diffuseTexture = this.diffuseTexture;
  270. offsetFur.furTexture = this.furTexture;
  271. offsetFur.highLevelFur = this.highLevelFur;
  272. offsetFur.furTime = this.furTime;
  273. offsetFur.furDensity = this.furDensity;
  274. }
  275. };
  276. // Methods
  277. FurMaterial.prototype.isReadyForSubMesh = function (mesh, subMesh, useInstances) {
  278. if (this.isFrozen) {
  279. if (this._wasPreviouslyReady && subMesh.effect) {
  280. return true;
  281. }
  282. }
  283. if (!subMesh._materialDefines) {
  284. subMesh._materialDefines = new FurMaterialDefines();
  285. }
  286. var defines = subMesh._materialDefines;
  287. var scene = this.getScene();
  288. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  289. if (this._renderId === scene.getRenderId()) {
  290. return true;
  291. }
  292. }
  293. var engine = scene.getEngine();
  294. // Textures
  295. if (defines._areTexturesDirty) {
  296. if (scene.texturesEnabled) {
  297. if (this.diffuseTexture && babylonjs_1.StandardMaterial.DiffuseTextureEnabled) {
  298. if (!this.diffuseTexture.isReady()) {
  299. return false;
  300. }
  301. else {
  302. defines._needUVs = true;
  303. defines.DIFFUSE = true;
  304. }
  305. }
  306. if (this.heightTexture && engine.getCaps().maxVertexTextureImageUnits) {
  307. if (!this.heightTexture.isReady()) {
  308. return false;
  309. }
  310. else {
  311. defines._needUVs = true;
  312. defines.HEIGHTMAP = true;
  313. }
  314. }
  315. }
  316. }
  317. // High level
  318. if (this.highLevelFur !== defines.HIGHLEVEL) {
  319. defines.HIGHLEVEL = true;
  320. defines.markAsUnprocessed();
  321. }
  322. // Misc.
  323. babylonjs_1.MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, this._shouldTurnAlphaTestOn(mesh), defines);
  324. // Lights
  325. defines._needNormals = babylonjs_1.MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, false, this._maxSimultaneousLights, this._disableLighting);
  326. // Values that need to be evaluated on every frame
  327. babylonjs_1.MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances ? true : false);
  328. // Attribs
  329. babylonjs_1.MaterialHelper.PrepareDefinesForAttributes(mesh, defines, true, true);
  330. // Get correct effect
  331. if (defines.isDirty) {
  332. defines.markAsProcessed();
  333. scene.resetCachedMaterial();
  334. // Fallbacks
  335. var fallbacks = new babylonjs_1.EffectFallbacks();
  336. if (defines.FOG) {
  337. fallbacks.addFallback(1, "FOG");
  338. }
  339. babylonjs_1.MaterialHelper.HandleFallbacksForShadows(defines, fallbacks, this.maxSimultaneousLights);
  340. if (defines.NUM_BONE_INFLUENCERS > 0) {
  341. fallbacks.addCPUSkinningFallback(0, mesh);
  342. }
  343. //Attributes
  344. var attribs = [babylonjs_1.VertexBuffer.PositionKind];
  345. if (defines.NORMAL) {
  346. attribs.push(babylonjs_1.VertexBuffer.NormalKind);
  347. }
  348. if (defines.UV1) {
  349. attribs.push(babylonjs_1.VertexBuffer.UVKind);
  350. }
  351. if (defines.UV2) {
  352. attribs.push(babylonjs_1.VertexBuffer.UV2Kind);
  353. }
  354. if (defines.VERTEXCOLOR) {
  355. attribs.push(babylonjs_1.VertexBuffer.ColorKind);
  356. }
  357. babylonjs_1.MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  358. babylonjs_1.MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  359. // Legacy browser patch
  360. var shaderName = "fur";
  361. var join = defines.toString();
  362. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  363. "vFogInfos", "vFogColor", "pointSize",
  364. "vDiffuseInfos",
  365. "mBones",
  366. "vClipPlane", "vClipPlane2", "vClipPlane3", "vClipPlane4", "diffuseMatrix",
  367. "furLength", "furAngle", "furColor", "furOffset", "furGravity", "furTime", "furSpacing", "furDensity", "furOcclusion"
  368. ];
  369. var samplers = ["diffuseSampler",
  370. "heightTexture", "furTexture"
  371. ];
  372. var uniformBuffers = new Array();
  373. babylonjs_1.MaterialHelper.PrepareUniformsAndSamplersList({
  374. uniformsNames: uniforms,
  375. uniformBuffersNames: uniformBuffers,
  376. samplers: samplers,
  377. defines: defines,
  378. maxSimultaneousLights: this.maxSimultaneousLights
  379. });
  380. subMesh.setEffect(scene.getEngine().createEffect(shaderName, {
  381. attributes: attribs,
  382. uniformsNames: uniforms,
  383. uniformBuffersNames: uniformBuffers,
  384. samplers: samplers,
  385. defines: join,
  386. fallbacks: fallbacks,
  387. onCompiled: this.onCompiled,
  388. onError: this.onError,
  389. indexParameters: { maxSimultaneousLights: this.maxSimultaneousLights }
  390. }, engine), defines);
  391. }
  392. if (!subMesh.effect || !subMesh.effect.isReady()) {
  393. return false;
  394. }
  395. this._renderId = scene.getRenderId();
  396. this._wasPreviouslyReady = true;
  397. return true;
  398. };
  399. FurMaterial.prototype.bindForSubMesh = function (world, mesh, subMesh) {
  400. var scene = this.getScene();
  401. var defines = subMesh._materialDefines;
  402. if (!defines) {
  403. return;
  404. }
  405. var effect = subMesh.effect;
  406. if (!effect) {
  407. return;
  408. }
  409. this._activeEffect = effect;
  410. // Matrices
  411. this.bindOnlyWorldMatrix(world);
  412. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  413. // Bones
  414. babylonjs_1.MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
  415. if (scene.getCachedMaterial() !== this) {
  416. // Textures
  417. if (this._diffuseTexture && babylonjs_1.StandardMaterial.DiffuseTextureEnabled) {
  418. this._activeEffect.setTexture("diffuseSampler", this._diffuseTexture);
  419. this._activeEffect.setFloat2("vDiffuseInfos", this._diffuseTexture.coordinatesIndex, this._diffuseTexture.level);
  420. this._activeEffect.setMatrix("diffuseMatrix", this._diffuseTexture.getTextureMatrix());
  421. }
  422. if (this._heightTexture) {
  423. this._activeEffect.setTexture("heightTexture", this._heightTexture);
  424. }
  425. // Clip plane
  426. babylonjs_1.MaterialHelper.BindClipPlane(this._activeEffect, scene);
  427. // Point size
  428. if (this.pointsCloud) {
  429. this._activeEffect.setFloat("pointSize", this.pointSize);
  430. }
  431. babylonjs_1.MaterialHelper.BindEyePosition(effect, scene);
  432. }
  433. this._activeEffect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  434. if (scene.lightsEnabled && !this.disableLighting) {
  435. babylonjs_1.MaterialHelper.BindLights(scene, mesh, this._activeEffect, defines, this.maxSimultaneousLights);
  436. }
  437. // View
  438. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== babylonjs_1.Scene.FOGMODE_NONE) {
  439. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  440. }
  441. // Fog
  442. babylonjs_1.MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  443. this._activeEffect.setFloat("furLength", this.furLength);
  444. this._activeEffect.setFloat("furAngle", this.furAngle);
  445. this._activeEffect.setColor4("furColor", this.furColor, 1.0);
  446. if (this.highLevelFur) {
  447. this._activeEffect.setVector3("furGravity", this.furGravity);
  448. this._activeEffect.setFloat("furOffset", this.furOffset);
  449. this._activeEffect.setFloat("furSpacing", this.furSpacing);
  450. this._activeEffect.setFloat("furDensity", this.furDensity);
  451. this._activeEffect.setFloat("furOcclusion", this.furOcclusion);
  452. this._furTime += this.getScene().getEngine().getDeltaTime() / this.furSpeed;
  453. this._activeEffect.setFloat("furTime", this._furTime);
  454. this._activeEffect.setTexture("furTexture", this.furTexture);
  455. }
  456. this._afterBind(mesh, this._activeEffect);
  457. };
  458. FurMaterial.prototype.getAnimatables = function () {
  459. var results = [];
  460. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  461. results.push(this.diffuseTexture);
  462. }
  463. if (this.heightTexture && this.heightTexture.animations && this.heightTexture.animations.length > 0) {
  464. results.push(this.heightTexture);
  465. }
  466. return results;
  467. };
  468. FurMaterial.prototype.getActiveTextures = function () {
  469. var activeTextures = _super.prototype.getActiveTextures.call(this);
  470. if (this._diffuseTexture) {
  471. activeTextures.push(this._diffuseTexture);
  472. }
  473. if (this._heightTexture) {
  474. activeTextures.push(this._heightTexture);
  475. }
  476. return activeTextures;
  477. };
  478. FurMaterial.prototype.hasTexture = function (texture) {
  479. if (_super.prototype.hasTexture.call(this, texture)) {
  480. return true;
  481. }
  482. if (this.diffuseTexture === texture) {
  483. return true;
  484. }
  485. if (this._heightTexture === texture) {
  486. return true;
  487. }
  488. return false;
  489. };
  490. FurMaterial.prototype.dispose = function (forceDisposeEffect) {
  491. if (this.diffuseTexture) {
  492. this.diffuseTexture.dispose();
  493. }
  494. if (this._meshes) {
  495. for (var i = 1; i < this._meshes.length; i++) {
  496. var mat = this._meshes[i].material;
  497. if (mat) {
  498. mat.dispose(forceDisposeEffect);
  499. }
  500. this._meshes[i].dispose();
  501. }
  502. }
  503. _super.prototype.dispose.call(this, forceDisposeEffect);
  504. };
  505. FurMaterial.prototype.clone = function (name) {
  506. var _this = this;
  507. return babylonjs_1.SerializationHelper.Clone(function () { return new FurMaterial(name, _this.getScene()); }, this);
  508. };
  509. FurMaterial.prototype.serialize = function () {
  510. var serializationObject = babylonjs_1.SerializationHelper.Serialize(this);
  511. serializationObject.customType = "BABYLON.FurMaterial";
  512. if (this._meshes) {
  513. serializationObject.sourceMeshName = this._meshes[0].name;
  514. serializationObject.quality = this._meshes.length;
  515. }
  516. return serializationObject;
  517. };
  518. FurMaterial.prototype.getClassName = function () {
  519. return "FurMaterial";
  520. };
  521. // Statics
  522. FurMaterial.Parse = function (source, scene, rootUrl) {
  523. var material = babylonjs_1.SerializationHelper.Parse(function () { return new FurMaterial(source.name, scene); }, source, scene, rootUrl);
  524. if (source.sourceMeshName && material.highLevelFur) {
  525. scene.executeWhenReady(function () {
  526. var sourceMesh = scene.getMeshByName(source.sourceMeshName);
  527. if (sourceMesh) {
  528. var furTexture = FurMaterial.GenerateTexture("Fur Texture", scene);
  529. material.furTexture = furTexture;
  530. FurMaterial.FurifyMesh(sourceMesh, source.quality);
  531. }
  532. });
  533. }
  534. return material;
  535. };
  536. FurMaterial.GenerateTexture = function (name, scene) {
  537. // Generate fur textures
  538. var texture = new babylonjs_1.DynamicTexture("FurTexture " + name, 256, scene, true);
  539. var context = texture.getContext();
  540. for (var i = 0; i < 20000; ++i) {
  541. context.fillStyle = "rgba(255, " + Math.floor(Math.random() * 255) + ", " + Math.floor(Math.random() * 255) + ", 1)";
  542. context.fillRect((Math.random() * texture.getSize().width), (Math.random() * texture.getSize().height), 2, 2);
  543. }
  544. texture.update(false);
  545. texture.wrapU = babylonjs_1.Texture.WRAP_ADDRESSMODE;
  546. texture.wrapV = babylonjs_1.Texture.WRAP_ADDRESSMODE;
  547. return texture;
  548. };
  549. // Creates and returns an array of meshes used as shells for the Fur Material
  550. // that can be disposed later in your code
  551. // The quality is in interval [0, 100]
  552. FurMaterial.FurifyMesh = function (sourceMesh, quality) {
  553. var meshes = [sourceMesh];
  554. var mat = sourceMesh.material;
  555. var i;
  556. if (!(mat instanceof FurMaterial)) {
  557. throw "The material of the source mesh must be a Fur Material";
  558. }
  559. for (i = 1; i < quality; i++) {
  560. var offsetFur = new FurMaterial(mat.name + i, sourceMesh.getScene());
  561. sourceMesh.getScene().materials.pop();
  562. babylonjs_1.Tags.EnableFor(offsetFur);
  563. babylonjs_1.Tags.AddTagsTo(offsetFur, "furShellMaterial");
  564. offsetFur.furLength = mat.furLength;
  565. offsetFur.furAngle = mat.furAngle;
  566. offsetFur.furGravity = mat.furGravity;
  567. offsetFur.furSpacing = mat.furSpacing;
  568. offsetFur.furSpeed = mat.furSpeed;
  569. offsetFur.furColor = mat.furColor;
  570. offsetFur.diffuseTexture = mat.diffuseTexture;
  571. offsetFur.furOffset = i / quality;
  572. offsetFur.furTexture = mat.furTexture;
  573. offsetFur.highLevelFur = mat.highLevelFur;
  574. offsetFur.furTime = mat.furTime;
  575. offsetFur.furDensity = mat.furDensity;
  576. var offsetMesh = sourceMesh.clone(sourceMesh.name + i);
  577. offsetMesh.material = offsetFur;
  578. offsetMesh.skeleton = sourceMesh.skeleton;
  579. offsetMesh.position = babylonjs_1.Vector3.Zero();
  580. meshes.push(offsetMesh);
  581. }
  582. for (i = 1; i < meshes.length; i++) {
  583. meshes[i].parent = sourceMesh;
  584. }
  585. sourceMesh.material._meshes = meshes;
  586. return meshes;
  587. };
  588. __decorate([
  589. babylonjs_1.serializeAsTexture("diffuseTexture")
  590. ], FurMaterial.prototype, "_diffuseTexture", void 0);
  591. __decorate([
  592. babylonjs_1.expandToProperty("_markAllSubMeshesAsTexturesDirty")
  593. ], FurMaterial.prototype, "diffuseTexture", void 0);
  594. __decorate([
  595. babylonjs_1.serializeAsTexture("heightTexture")
  596. ], FurMaterial.prototype, "_heightTexture", void 0);
  597. __decorate([
  598. babylonjs_1.expandToProperty("_markAllSubMeshesAsTexturesDirty")
  599. ], FurMaterial.prototype, "heightTexture", void 0);
  600. __decorate([
  601. babylonjs_1.serializeAsColor3()
  602. ], FurMaterial.prototype, "diffuseColor", void 0);
  603. __decorate([
  604. babylonjs_1.serialize()
  605. ], FurMaterial.prototype, "furLength", void 0);
  606. __decorate([
  607. babylonjs_1.serialize()
  608. ], FurMaterial.prototype, "furAngle", void 0);
  609. __decorate([
  610. babylonjs_1.serializeAsColor3()
  611. ], FurMaterial.prototype, "furColor", void 0);
  612. __decorate([
  613. babylonjs_1.serialize()
  614. ], FurMaterial.prototype, "furOffset", void 0);
  615. __decorate([
  616. babylonjs_1.serialize()
  617. ], FurMaterial.prototype, "furSpacing", void 0);
  618. __decorate([
  619. babylonjs_1.serializeAsVector3()
  620. ], FurMaterial.prototype, "furGravity", void 0);
  621. __decorate([
  622. babylonjs_1.serialize()
  623. ], FurMaterial.prototype, "furSpeed", void 0);
  624. __decorate([
  625. babylonjs_1.serialize()
  626. ], FurMaterial.prototype, "furDensity", void 0);
  627. __decorate([
  628. babylonjs_1.serialize()
  629. ], FurMaterial.prototype, "furOcclusion", void 0);
  630. __decorate([
  631. babylonjs_1.serialize("disableLighting")
  632. ], FurMaterial.prototype, "_disableLighting", void 0);
  633. __decorate([
  634. babylonjs_1.expandToProperty("_markAllSubMeshesAsLightsDirty")
  635. ], FurMaterial.prototype, "disableLighting", void 0);
  636. __decorate([
  637. babylonjs_1.serialize("maxSimultaneousLights")
  638. ], FurMaterial.prototype, "_maxSimultaneousLights", void 0);
  639. __decorate([
  640. babylonjs_1.expandToProperty("_markAllSubMeshesAsLightsDirty")
  641. ], FurMaterial.prototype, "maxSimultaneousLights", void 0);
  642. __decorate([
  643. babylonjs_1.serialize()
  644. ], FurMaterial.prototype, "highLevelFur", void 0);
  645. __decorate([
  646. babylonjs_1.serialize()
  647. ], FurMaterial.prototype, "furTime", null);
  648. return FurMaterial;
  649. }(babylonjs_1.PushMaterial));
  650. exports.FurMaterial = FurMaterial;
  651. /***/ }),
  652. /***/ "./src/fur/index.ts":
  653. /*!**************************!*\
  654. !*** ./src/fur/index.ts ***!
  655. \**************************/
  656. /*! no static exports found */
  657. /***/ (function(module, exports, __webpack_require__) {
  658. "use strict";
  659. function __export(m) {
  660. for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
  661. }
  662. Object.defineProperty(exports, "__esModule", { value: true });
  663. __export(__webpack_require__(/*! ./furMaterial */ "./src/fur/furMaterial.ts"));
  664. /***/ }),
  665. /***/ "babylonjs":
  666. /*!****************************************************************************************************!*\
  667. !*** external {"root":"BABYLON","commonjs":"babylonjs","commonjs2":"babylonjs","amd":"babylonjs"} ***!
  668. \****************************************************************************************************/
  669. /*! no static exports found */
  670. /***/ (function(module, exports) {
  671. module.exports = __WEBPACK_EXTERNAL_MODULE_babylonjs__;
  672. /***/ })
  673. /******/ });
  674. });
  675. //# sourceMappingURL=babylon.furMaterial.js.map