babylon.grassProceduralTexture.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var BABYLON;
  3. (function (BABYLON) {
  4. var GrassProceduralTexture = (function (_super) {
  5. __extends(GrassProceduralTexture, _super);
  6. function GrassProceduralTexture(name, size, scene, fallbackTexture, generateMipMaps) {
  7. _super.call(this, name, size, "grassProceduralTexture", scene, fallbackTexture, generateMipMaps);
  8. this._herb1 = new BABYLON.Color3(0.29, 0.38, 0.02);
  9. this._herb2 = new BABYLON.Color3(0.36, 0.49, 0.09);
  10. this._herb3 = new BABYLON.Color3(0.51, 0.6, 0.28);
  11. this._groundColor = new BABYLON.Color3(1, 1, 1);
  12. this._grassColors = [
  13. new BABYLON.Color3(0.29, 0.38, 0.02),
  14. new BABYLON.Color3(0.36, 0.49, 0.09),
  15. new BABYLON.Color3(0.51, 0.6, 0.28)
  16. ];
  17. this.updateShaderUniforms();
  18. this.refreshRate = 0;
  19. }
  20. GrassProceduralTexture.prototype.updateShaderUniforms = function () {
  21. this.setColor3("herb1Color", this._grassColors[0]);
  22. this.setColor3("herb2Color", this._grassColors[1]);
  23. this.setColor3("herb3Color", this._grassColors[2]);
  24. this.setColor3("groundColor", this._groundColor);
  25. };
  26. Object.defineProperty(GrassProceduralTexture.prototype, "grassColors", {
  27. get: function () {
  28. return this._grassColors;
  29. },
  30. set: function (value) {
  31. this._grassColors = value;
  32. this.updateShaderUniforms();
  33. },
  34. enumerable: true,
  35. configurable: true
  36. });
  37. Object.defineProperty(GrassProceduralTexture.prototype, "groundColor", {
  38. get: function () {
  39. return this._groundColor;
  40. },
  41. set: function (value) {
  42. this.groundColor = value;
  43. this.updateShaderUniforms();
  44. },
  45. enumerable: true,
  46. configurable: true
  47. });
  48. return GrassProceduralTexture;
  49. })(BABYLON.ProceduralTexture);
  50. BABYLON.GrassProceduralTexture = GrassProceduralTexture;
  51. })(BABYLON || (BABYLON = {}));
  52. BABYLON.Effect.ShadersStore['grassProceduralTexturePixelShader'] = "precision highp float;\r\n\r\nvarying vec2 vPosition;\r\nvarying vec2 vUV;\r\n\r\nuniform vec3 herb1Color;\r\nuniform vec3 herb2Color;\r\nuniform vec3 herb3Color;\r\nuniform vec3 groundColor;\r\n\r\nfloat rand(vec2 n) {\r\n\treturn fract(cos(dot(n, vec2(12.9898, 4.1414))) * 43758.5453);\r\n}\r\n\r\nfloat noise(vec2 n) {\r\n\tconst vec2 d = vec2(0.0, 1.0);\r\n\tvec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n));\r\n\treturn mix(mix(rand(b), rand(b + d.yx), f.x), mix(rand(b + d.xy), rand(b + d.yy), f.x), f.y);\r\n}\r\n\r\nfloat fbm(vec2 n) {\r\n\tfloat total = 0.0, amplitude = 1.0;\r\n\tfor (int i = 0; i < 4; i++) {\r\n\t\ttotal += noise(n) * amplitude;\r\n\t\tn += n;\r\n\t\tamplitude *= 0.5;\r\n\t}\r\n\treturn total;\r\n}\r\n\r\nvoid main(void) {\r\n\tvec3 color = mix(groundColor, herb1Color, rand(gl_FragCoord.xy * 4.0));\r\n\tcolor = mix(color, herb2Color, rand(gl_FragCoord.xy * 8.0));\r\n\tcolor = mix(color, herb3Color, rand(gl_FragCoord.xy));\r\n\tcolor = mix(color, herb1Color, fbm(gl_FragCoord.xy * 16.0));\r\n\tgl_FragColor = vec4(color, 1.0);\r\n}";