Babylon.shaderBuilder.js 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var ShaderMaterialHelperStatics = (function () {
  4. function ShaderMaterialHelperStatics() {
  5. }
  6. ShaderMaterialHelperStatics.Dark = false;
  7. ShaderMaterialHelperStatics.Light = true;
  8. ShaderMaterialHelperStatics.PrecisionHighMode = 'highp';
  9. ShaderMaterialHelperStatics.PrecisionMediumMode = 'mediump';
  10. ShaderMaterialHelperStatics.face_back = "!gl_FrontFacing";
  11. ShaderMaterialHelperStatics.face_front = "gl_FrontFacing";
  12. ShaderMaterialHelperStatics.AttrPosition = 'position';
  13. ShaderMaterialHelperStatics.AttrNormal = 'normal';
  14. ShaderMaterialHelperStatics.AttrUv = 'uv';
  15. ShaderMaterialHelperStatics.AttrUv2 = 'uv2';
  16. ShaderMaterialHelperStatics.AttrTypeForPosition = 'vec3';
  17. ShaderMaterialHelperStatics.AttrTypeForNormal = 'vec3';
  18. ShaderMaterialHelperStatics.AttrTypeForUv = 'vec2';
  19. ShaderMaterialHelperStatics.AttrTypeForUv2 = 'vec2';
  20. ShaderMaterialHelperStatics.uniformView = "view";
  21. ShaderMaterialHelperStatics.uniformWorld = "world";
  22. ShaderMaterialHelperStatics.uniformWorldView = "worldView";
  23. ShaderMaterialHelperStatics.uniformViewProjection = "viewProjection";
  24. ShaderMaterialHelperStatics.uniformWorldViewProjection = "worldViewProjection";
  25. ShaderMaterialHelperStatics.uniformStandardType = "mat4";
  26. ShaderMaterialHelperStatics.uniformFlags = "flags";
  27. ShaderMaterialHelperStatics.Mouse = "mouse";
  28. ShaderMaterialHelperStatics.Screen = "screen";
  29. ShaderMaterialHelperStatics.Camera = "camera";
  30. ShaderMaterialHelperStatics.Look = "look";
  31. ShaderMaterialHelperStatics.Time = "time";
  32. ShaderMaterialHelperStatics.GlobalTime = "gtime";
  33. ShaderMaterialHelperStatics.Position = "pos";
  34. ShaderMaterialHelperStatics.WorldPosition = "wpos";
  35. ShaderMaterialHelperStatics.Normal = "nrm";
  36. ShaderMaterialHelperStatics.WorldNormal = "wnrm";
  37. ShaderMaterialHelperStatics.Uv = "vuv";
  38. ShaderMaterialHelperStatics.Uv2 = "vuv2";
  39. ShaderMaterialHelperStatics.Center = 'center';
  40. ShaderMaterialHelperStatics.ReflectMatrix = "refMat";
  41. ShaderMaterialHelperStatics.Texture2D = "txtRef_";
  42. ShaderMaterialHelperStatics.TextureCube = "cubeRef_";
  43. return ShaderMaterialHelperStatics;
  44. })();
  45. BABYLON.ShaderMaterialHelperStatics = ShaderMaterialHelperStatics;
  46. var Normals = (function () {
  47. function Normals() {
  48. }
  49. Normals.Default = ShaderMaterialHelperStatics.Normal;
  50. Normals.Inverse = '-1.*' + ShaderMaterialHelperStatics.Normal;
  51. Normals.Pointed = 'normalize(' + ShaderMaterialHelperStatics.Position + '-' + ShaderMaterialHelperStatics.Center + ')';
  52. Normals.Flat = 'normalize(cross(dFdx(' + ShaderMaterialHelperStatics.Position + ' * -1.), dFdy(' + ShaderMaterialHelperStatics.Position + ')))';
  53. Normals.Map = 'normalMap()';
  54. return Normals;
  55. })();
  56. BABYLON.Normals = Normals;
  57. var Speculars = (function () {
  58. function Speculars() {
  59. }
  60. Speculars.Map = 'specularMap()';
  61. return Speculars;
  62. })();
  63. var ShaderMaterialHelper = (function () {
  64. function ShaderMaterialHelper() {
  65. }
  66. ShaderMaterialHelper.prototype.ShaderMaterial = function (name, scene, shader, helpers) {
  67. return this.MakeShaderMaterialForEngine(name, scene, shader, helpers);
  68. };
  69. ShaderMaterialHelper.prototype.MakeShaderMaterialForEngine = function (name, scene, shader, helpers) { return {}; };
  70. ShaderMaterialHelper.prototype.DefineTexture = function (txt, scene) {
  71. return null;
  72. };
  73. ShaderMaterialHelper.prototype.DefineCubeTexture = function (txt, scene) {
  74. return null;
  75. };
  76. ShaderMaterialHelper.prototype.SetUniforms = function (meshes, cameraPos, cameraTarget, mouse, screen, time) {
  77. };
  78. return ShaderMaterialHelper;
  79. })();
  80. BABYLON.ShaderMaterialHelper = ShaderMaterialHelper;
  81. var Shader = (function () {
  82. function Shader() {
  83. }
  84. Shader.Replace = function (s, t, d) {
  85. var ignore = null;
  86. return s.replace(new RegExp(t.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g, "\\$&"), (ignore ? "gi" : "g")), (typeof (d) == "string") ? d.replace(/\$/g, "$$$$") : d);
  87. };
  88. Shader.Def = function (a, d) {
  89. if (a != undefined && a != null)
  90. return (d != undefined && d != null ? a : true);
  91. else if (d != Shader._null)
  92. return (d != undefined && d != null ? d : false);
  93. return null;
  94. };
  95. Shader.Join = function (s) {
  96. return s.join("\n\
  97. ");
  98. };
  99. Shader.Print = function (n) {
  100. if (n == undefined)
  101. return "0.";
  102. var sn = Shader.Replace(n.toString(), '-', '0');
  103. var reg = new RegExp('^\\d+$');
  104. if (reg.test(sn) && n.toString().indexOf('.') == -1)
  105. return n + ".";
  106. return n.toString();
  107. };
  108. Shader.Custom = function () {
  109. return "custom_" + this.Print(++this.Me.CustomIndexer) + "_";
  110. };
  111. Shader.Index = function () {
  112. return "_" + Shader.Indexer + "_";
  113. };
  114. Shader.DefCustom = function (t, c) {
  115. this.Me.Body += t + " custom_" + this.Print(++this.Me.CustomIndexer) + "_ = " + c + ";";
  116. };
  117. Shader._null = 'set null anyway';
  118. return Shader;
  119. })();
  120. BABYLON.Shader = Shader;
  121. var Helper = (function () {
  122. function Helper() {
  123. var setting = Shader.Me.Setting;
  124. var instance = new ShaderBuilder();
  125. instance.Parent = Shader.Me;
  126. instance.Setting = setting;
  127. return instance;
  128. }
  129. Helper.Red = 0;
  130. Helper.Yellow = 1;
  131. Helper.White = 2;
  132. Helper.Cyan = 4;
  133. Helper.Blue = 5;
  134. Helper.Pink = 6;
  135. Helper.Black = 7;
  136. Helper.Green = 8;
  137. return Helper;
  138. })();
  139. BABYLON.Helper = Helper;
  140. var ShaderSetting = (function () {
  141. function ShaderSetting() {
  142. this.PrecisionMode = ShaderMaterialHelperStatics.PrecisionHighMode;
  143. }
  144. return ShaderSetting;
  145. })();
  146. BABYLON.ShaderSetting = ShaderSetting;
  147. var ShaderBuilder = (function () {
  148. function ShaderBuilder() {
  149. this.Setting = new ShaderSetting();
  150. this.Extentions = [];
  151. this.Attributes = [];
  152. this.Fragment = [];
  153. this.Helpers = [];
  154. this.Uniforms = [];
  155. this.Varings = [];
  156. this.Vertex = [];
  157. this.Setting.Uv = true;
  158. this.Setting.Time = true;
  159. this.Setting.Camera = true;
  160. this.Setting.Helpers = true;
  161. this.Setting.NormalMap = "result = vec4(0.5);";
  162. this.Setting.SpecularMap = "float_result = 1.0;";
  163. this.Setting.NormalOpacity = "0.5";
  164. this.Setting.Normal = ShaderMaterialHelperStatics.Normal;
  165. if (Shader.Indexer == null)
  166. Shader.Indexer = 1;
  167. this.CustomIndexer = 1;
  168. Shader.Me = this;
  169. }
  170. ShaderBuilder.InitializeEngine = function () {
  171. eval(BABYLON.Shader.Replace(BABYLON.Shader.Replace("BABYLON.ShaderMaterialHelper.prototype.MakeShaderMaterialForEngine=function(name,scene,shader,helpers){BABYLON.Effect.ShadersStore[name+#[QT]VertexShader#[QT]]=shader.Vertex;BABYLON.Effect.ShadersStore[name+#[QT]PixelShader#[QT]]=shader.Pixel;return new BABYLON.ShaderMaterial(name,scene,{vertex:name,fragment:name},helpers);}", "#[QT]", '"'), '#[T]', "'"));
  172. eval(BABYLON.Shader.Replace(BABYLON.Shader.Replace("BABYLON.ShaderMaterialHelper.prototype.DefineTexture = function (option, sc) { var tx = new BABYLON.Texture(option, sc); return tx; } ", "#[QT]", '"'), '#[T]', "'"));
  173. eval(BABYLON.Shader.Replace(BABYLON.Shader.Replace("BABYLON.ShaderMaterialHelper.prototype.DefineCubeTexture = function (option, sc) { var tx = new BABYLON.CubeTexture(option, sc); tx.coordinatesMode = BABYLON.Texture.PLANAR_MODE; return tx; } ", "#[QT]", '"'), '#[T]', "'"));
  174. eval(BABYLON.Shader.Replace(BABYLON.Shader.Replace("BABYLON.ShaderMaterialHelper.prototype.SetUniforms = function (meshes, cameraPos, cameraTarget, mouse, screen, time) { for (var ms in meshes) { ms = meshes[ms]; if (ms.material && (ms.material.ShaderSetting != null || ms.material.ShaderSetting != undefined)) { if (ms.material.ShaderSetting.Camera) ms.material.setVector3(BABYLON.ShaderMaterialHelperStatics.Camera, cameraPos); if (ms.material.ShaderSetting.Center) ms.material.setVector3(BABYLON.ShaderMaterialHelperStatics.Center, { x: 0., y: 0., z: 0. }); if (ms.material.ShaderSetting.Mouse) ms.material.setVector2(BABYLON.ShaderMaterialHelperStatics.Mouse, mouse); if (ms.material.ShaderSetting.Screen) ms.material.setVector2(BABYLON.ShaderMaterialHelperStatics.Screen, screen); if (ms.material.ShaderSetting.GlobalTime) ms.material.setVector4(BABYLON.ShaderMaterialHelperStatics.GlobalTime, { x: 0., y: 0., z: 0., w: 0. }); if (ms.material.ShaderSetting.Look) ms.material.setVector3(BABYLON.ShaderMaterialHelperStatics.Look, cameraTarget); if (ms.material.ShaderSetting.Time) ms.material.setFloat(BABYLON.ShaderMaterialHelperStatics.Time, time); } } }", "#[QT]", '"'), '#[T]', "'"));
  175. };
  176. ShaderBuilder.prototype.PrepareBeforeMaterialBuild = function (scene) {
  177. this.Setting = Shader.Me.Setting;
  178. this.Attributes.push(ShaderMaterialHelperStatics.AttrPosition);
  179. this.Attributes.push(ShaderMaterialHelperStatics.AttrNormal);
  180. if (this.Setting.Uv) {
  181. this.Attributes.push(ShaderMaterialHelperStatics.AttrUv);
  182. }
  183. if (this.Setting.Uv2) {
  184. this.Attributes.push(ShaderMaterialHelperStatics.AttrUv2);
  185. }
  186. this.Uniforms.push(ShaderMaterialHelperStatics.uniformView, ShaderMaterialHelperStatics.uniformWorld, ShaderMaterialHelperStatics.uniformWorldView, ShaderMaterialHelperStatics.uniformViewProjection, ShaderMaterialHelperStatics.uniformWorldViewProjection);
  187. // start Build Vertex Frame
  188. this.Vertex.push("precision " + this.Setting.PrecisionMode + " float;");
  189. this.Vertex.push("attribute " + ShaderMaterialHelperStatics.AttrTypeForPosition + " " + ShaderMaterialHelperStatics.AttrPosition + ";");
  190. this.Vertex.push("attribute " + ShaderMaterialHelperStatics.AttrTypeForNormal + " " + ShaderMaterialHelperStatics.AttrNormal + ";");
  191. if (this.Setting.Uv) {
  192. this.Vertex.push("attribute " + ShaderMaterialHelperStatics.AttrTypeForUv + " " + ShaderMaterialHelperStatics.AttrUv + ";");
  193. this.Vertex.push("varying vec2 " + ShaderMaterialHelperStatics.Uv + ";");
  194. }
  195. if (this.Setting.Uv2) {
  196. this.Vertex.push("attribute " + ShaderMaterialHelperStatics.AttrTypeForUv2 + " " + ShaderMaterialHelperStatics.AttrUv2 + ";");
  197. this.Vertex.push("varying vec2 " + ShaderMaterialHelperStatics.Uv2 + ";");
  198. }
  199. this.Vertex.push("varying vec3 " + ShaderMaterialHelperStatics.Position + ";");
  200. this.Vertex.push("varying vec3 " + ShaderMaterialHelperStatics.Normal + ";");
  201. this.Vertex.push("uniform " + ShaderMaterialHelperStatics.uniformStandardType + ' ' + ShaderMaterialHelperStatics.uniformWorldViewProjection + ";");
  202. if (this.Setting.VertexView) {
  203. this.Vertex.push("uniform " + ShaderMaterialHelperStatics.uniformStandardType + ' ' + ShaderMaterialHelperStatics.uniformView + ";");
  204. }
  205. if (this.Setting.VertexWorld) {
  206. this.Vertex.push("uniform " + ShaderMaterialHelperStatics.uniformStandardType + ' ' + ShaderMaterialHelperStatics.uniformWorld + ";");
  207. }
  208. if (this.Setting.VertexViewProjection) {
  209. this.Vertex.push("uniform " + ShaderMaterialHelperStatics.uniformStandardType + ' ' + ShaderMaterialHelperStatics.uniformViewProjection + ";");
  210. }
  211. if (this.Setting.Flags) {
  212. this.Uniforms.push(ShaderMaterialHelperStatics.uniformFlags);
  213. this.Vertex.push("uniform float " + ShaderMaterialHelperStatics.uniformFlags + ";");
  214. }
  215. if (this.Setting.VertexWorldView) {
  216. this.Vertex.push("uniform " + ShaderMaterialHelperStatics.uniformStandardType + ' ' + ShaderMaterialHelperStatics.uniformWorldView + ";");
  217. }
  218. /*#extension GL_OES_standard_derivatives : enable*/
  219. this.Fragment.push("precision " + this.Setting.PrecisionMode + " float;\n\
  220. #extension GL_OES_standard_derivatives : enable\n\
  221. \n\
  222. \n\
  223. ");
  224. if (this.Setting.Uv) {
  225. this.Fragment.push("varying vec2 " + ShaderMaterialHelperStatics.Uv + ";");
  226. }
  227. if (this.Setting.Uv2) {
  228. this.Fragment.push("varying vec2 " + ShaderMaterialHelperStatics.Uv2 + ";");
  229. }
  230. if (this.Setting.FragmentView) {
  231. this.Fragment.push("uniform " + ShaderMaterialHelperStatics.uniformStandardType + ' ' + ShaderMaterialHelperStatics.uniformView + ";");
  232. }
  233. if (this.Setting.FragmentWorld) {
  234. this.Fragment.push("uniform " + ShaderMaterialHelperStatics.uniformStandardType + ' ' + ShaderMaterialHelperStatics.uniformWorld + ";");
  235. }
  236. if (this.Setting.FragmentViewProjection) {
  237. this.Fragment.push("uniform " + ShaderMaterialHelperStatics.uniformStandardType + ' ' + ShaderMaterialHelperStatics.uniformViewProjection + ";");
  238. }
  239. if (this.Setting.FragmentWorldView) {
  240. this.Fragment.push("uniform " + ShaderMaterialHelperStatics.uniformStandardType + ' ' + ShaderMaterialHelperStatics.uniformWorldView + ";");
  241. }
  242. if (this.Setting.Flags) {
  243. this.Fragment.push("uniform float " + ShaderMaterialHelperStatics.uniformFlags + ";");
  244. }
  245. this.Fragment.push("varying vec3 " + ShaderMaterialHelperStatics.Position + ";");
  246. this.Fragment.push("varying vec3 " + ShaderMaterialHelperStatics.Normal + ";");
  247. if (this.Setting.WorldPosition) {
  248. this.Vertex.push("varying vec3 " + ShaderMaterialHelperStatics.WorldPosition + ";");
  249. this.Vertex.push("varying vec3 " + ShaderMaterialHelperStatics.WorldNormal + ";");
  250. this.Fragment.push("varying vec3 " + ShaderMaterialHelperStatics.WorldPosition + ";");
  251. this.Fragment.push("varying vec3 " + ShaderMaterialHelperStatics.WorldNormal + ";");
  252. }
  253. if (this.Setting.Texture2Ds != null) {
  254. for (var s in this.Setting.Texture2Ds) {
  255. if (this.Setting.Texture2Ds[s].inVertex) {
  256. this.Vertex.push("uniform sampler2D " + ShaderMaterialHelperStatics.Texture2D + s + ";");
  257. }
  258. if (this.Setting.Texture2Ds[s].inFragment) {
  259. this.Fragment.push("uniform sampler2D " + ShaderMaterialHelperStatics.Texture2D + s + ";");
  260. }
  261. }
  262. }
  263. if (this.Setting.TextureCubes != null) {
  264. for (var s in this.Setting.TextureCubes) {
  265. if (this.Setting.TextureCubes[s].inVertex) {
  266. this.Vertex.push("uniform samplerCube " + ShaderMaterialHelperStatics.TextureCube + s + ";");
  267. }
  268. if (this.Setting.TextureCubes[s].inFragment) {
  269. this.Fragment.push("uniform samplerCube " + ShaderMaterialHelperStatics.TextureCube + s + ";");
  270. }
  271. }
  272. }
  273. if (this.Setting.Center) {
  274. this.Vertex.push("uniform vec3 " + ShaderMaterialHelperStatics.Center + ";");
  275. this.Fragment.push("uniform vec3 " + ShaderMaterialHelperStatics.Center + ";");
  276. }
  277. if (this.Setting.Mouse) {
  278. this.Vertex.push("uniform vec2 " + ShaderMaterialHelperStatics.Mouse + ";");
  279. this.Fragment.push("uniform vec2 " + ShaderMaterialHelperStatics.Mouse + ";");
  280. }
  281. if (this.Setting.Screen) {
  282. this.Vertex.push("uniform vec2 " + ShaderMaterialHelperStatics.Screen + ";");
  283. this.Fragment.push("uniform vec2 " + ShaderMaterialHelperStatics.Screen + ";");
  284. }
  285. if (this.Setting.Camera) {
  286. this.Vertex.push("uniform vec3 " + ShaderMaterialHelperStatics.Camera + ";");
  287. this.Fragment.push("uniform vec3 " + ShaderMaterialHelperStatics.Camera + ";");
  288. }
  289. if (this.Setting.Look) {
  290. this.Vertex.push("uniform vec3 " + ShaderMaterialHelperStatics.Look + ";");
  291. this.Fragment.push("uniform vec3 " + ShaderMaterialHelperStatics.Look + ";");
  292. }
  293. if (this.Setting.Time) {
  294. this.Vertex.push("uniform float " + ShaderMaterialHelperStatics.Time + ";");
  295. this.Fragment.push("uniform float " + ShaderMaterialHelperStatics.Time + ";");
  296. }
  297. if (this.Setting.GlobalTime) {
  298. this.Vertex.push("uniform vec4 " + ShaderMaterialHelperStatics.GlobalTime + ";");
  299. this.Fragment.push("uniform vec4 " + ShaderMaterialHelperStatics.GlobalTime + ";");
  300. }
  301. if (this.Setting.ReflectMatrix) {
  302. this.Vertex.push("uniform mat4 " + ShaderMaterialHelperStatics.ReflectMatrix + ";");
  303. this.Fragment.push("uniform mat4 " + ShaderMaterialHelperStatics.ReflectMatrix + ";");
  304. }
  305. if (this.Setting.Helpers) {
  306. var sresult = Shader.Join([
  307. "vec3 random3(vec3 c) { float j = 4096.0*sin(dot(c,vec3(17.0, 59.4, 15.0))); vec3 r; r.z = fract(512.0*j); j *= .125; r.x = fract(512.0*j); j *= .125; r.y = fract(512.0*j); return r-0.5; } ",
  308. "float rand(vec2 co){ return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); } ",
  309. "const float F3 = 0.3333333;const float G3 = 0.1666667;",
  310. "float simplex3d(vec3 p) { vec3 s = floor(p + dot(p, vec3(F3))); vec3 x = p - s + dot(s, vec3(G3)); vec3 e = step(vec3(0.0), x - x.yzx); vec3 i1 = e*(1.0 - e.zxy); vec3 i2 = 1.0 - e.zxy*(1.0 - e); vec3 x1 = x - i1 + G3; vec3 x2 = x - i2 + 2.0*G3; vec3 x3 = x - 1.0 + 3.0*G3; vec4 w, d; w.x = dot(x, x); w.y = dot(x1, x1); w.z = dot(x2, x2); w.w = dot(x3, x3); w = max(0.6 - w, 0.0); d.x = dot(random3(s), x); d.y = dot(random3(s + i1), x1); d.z = dot(random3(s + i2), x2); d.w = dot(random3(s + 1.0), x3); w *= w; w *= w; d *= w; return dot(d, vec4(52.0)); } ",
  311. "float noise(vec3 m) { return 0.5333333*simplex3d(m) +0.2666667*simplex3d(2.0*m) +0.1333333*simplex3d(4.0*m) +0.0666667*simplex3d(8.0*m); } ",
  312. "float dim(vec3 p1 , vec3 p2){ return sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y)+(p2.z-p1.z)*(p2.z-p1.z)); }",
  313. "vec2 rotate_xy(vec2 pr1,vec2 pr2,float alpha) {vec2 pp2 = vec2( pr2.x - pr1.x, pr2.y - pr1.y );return vec2( pr1.x + pp2.x * cos(alpha*3.14159265/180.) - pp2.y * sin(alpha*3.14159265/180.),pr1.y + pp2.x * sin(alpha*3.14159265/180.) + pp2.y * cos(alpha*3.14159265/180.));} \n vec3 r_y(vec3 n, float a,vec3 c) {vec3 c1 = vec3( c.x, c.y, c.z );c1.x = c1.x;c1.y = c1.z;vec2 p = rotate_xy(vec2(c1.x,c1.y), vec2( n.x, n.z ), a);n.x = p.x;n.z = p.y;return n; } \n vec3 r_x(vec3 n, float a,vec3 c) {vec3 c1 = vec3( c.x, c.y, c.z );c1.x = c1.y;c1.y = c1.z;vec2 p = rotate_xy(vec2(c1.x,c1.y), vec2( n.y, n.z ), a);n.y = p.x;n.z = p.y;return n; } \n vec3 r_z(vec3 n, float a,vec3 c) { vec3 c1 = vec3( c.x, c.y, c.z );vec2 p = rotate_xy(vec2(c1.x,c1.y), vec2( n.x, n.y ), a);n.x = p.x;n.y = p.y;return n; }",
  314. ]);
  315. this.Vertex.push(sresult);
  316. this.Fragment.push(sresult);
  317. }
  318. this.Vertex.push("void main(void) { \n\
  319. " + ShaderMaterialHelperStatics.Position + " = " + ShaderMaterialHelperStatics.AttrPosition + "; \n\
  320. " + ShaderMaterialHelperStatics.Normal + " = " + ShaderMaterialHelperStatics.AttrNormal + "; \n\
  321. vec4 result = vec4(" + ShaderMaterialHelperStatics.Position + ",1.); \n\
  322. #[Source] \n\
  323. gl_Position = worldViewProjection * result;\n\
  324. vuv = uv;\n\
  325. #[AfterFinishVertex] \n\
  326. }");
  327. // start Build Fragment Frame
  328. if (this.Setting.NormalMap != null) {
  329. this.Fragment.push("vec3 normalMap() { vec4 result = vec4(0.); " + this.Setting.NormalMap + "; \n\
  330. result = vec4( normalize( " + this.Setting.Normal + " -(normalize(result.xyz)*2.0-vec3(1.))*(max(-0.5,min(0.5," + Shader.Print(this.Setting.NormalOpacity) + ")) )),1.0); return result.xyz;}");
  331. }
  332. if (this.Setting.SpecularMap != null) {
  333. this.Fragment.push("float specularMap() { vec4 result = vec4(0.);float float_result = 0.; " + this.Setting.SpecularMap + "; return float_result ;}");
  334. }
  335. this.Fragment.push(this.FragmentBeforeMain);
  336. this.Fragment.push(" \n\
  337. void main(void) { \n\
  338. int discardState = 0;\n\
  339. vec4 result = vec4(0.);\n\
  340. #[Source] \n\
  341. if(discardState == 0)gl_FragColor = result; \n\
  342. }");
  343. };
  344. ShaderBuilder.prototype.PrepareMaterial = function (material, scene) {
  345. material.ShaderSetting =
  346. this.Setting;
  347. if (!this.Setting.Transparency) {
  348. material.needAlphaBlending = function () { return false; };
  349. }
  350. else {
  351. material.needAlphaBlending = function () { return true; };
  352. }
  353. ;
  354. if (!this.Setting.Back)
  355. this.Setting.Back = false;
  356. material.needAlphaTesting = function () { return true; };
  357. material.setVector3("camera", { x: 18., y: 18., z: 18. });
  358. material.backFaceCulling = !this.Setting.Back;
  359. material.wireframe = this.Setting.Wire;
  360. material.setFlags = function (flags) {
  361. if (this.ShaderSetting.Flags) {
  362. var s = 0.;
  363. for (var i = 0; i < 20; i++) {
  364. if (flags.length > i && flags[i] == '1')
  365. s += Math.pow(2., i);
  366. }
  367. this.flagNumber = s;
  368. this.setFloat(ShaderMaterialHelperStatics.uniformFlags, s);
  369. }
  370. };
  371. material.flagNumber = 0.;
  372. material.flagUp = function (flag) {
  373. if (this.ShaderSetting.Flags) {
  374. if (Math.floor((this.flagNumber / Math.pow(2., flag) % 2.)) != 1.)
  375. this.flagNumber += Math.pow(2., flag);
  376. this.setFloat(ShaderMaterialHelperStatics.uniformFlags, this.flagNumber);
  377. }
  378. };
  379. material.flagDown = function (flag) {
  380. if (this.ShaderSetting.Flags) {
  381. if (Math.floor((this.flagNumber / Math.pow(2., flag) % 2.)) == 1.)
  382. this.flagNumber -= Math.pow(2., flag);
  383. this.setFloat(ShaderMaterialHelperStatics.uniformFlags, this.flagNumber);
  384. }
  385. };
  386. material.onCompiled = function () {
  387. };
  388. if (this.Setting.Texture2Ds != null) {
  389. for (var s in this.Setting.Texture2Ds) {
  390. // setTexture2D
  391. var texture = new ShaderMaterialHelper().DefineTexture(this.Setting.Texture2Ds[s].key, scene);
  392. material.setTexture(ShaderMaterialHelperStatics.Texture2D + s, texture);
  393. }
  394. }
  395. if (this.Setting.TextureCubes != null) {
  396. for (var s in this.Setting.TextureCubes) {
  397. // setTexture2D
  398. var texture = new ShaderMaterialHelper().DefineCubeTexture(this.Setting.TextureCubes[s].key, scene);
  399. material.setTexture(ShaderMaterialHelperStatics.TextureCube + s, texture);
  400. material.setMatrix(ShaderMaterialHelperStatics.ReflectMatrix, texture.getReflectionTextureMatrix());
  401. }
  402. }
  403. Shader.Me = null;
  404. return material;
  405. };
  406. ShaderBuilder.prototype.Build = function () {
  407. Shader.Me.Parent.Setting = Shader.Me.Setting;
  408. Shader.Me = Shader.Me.Parent;
  409. return this.Body;
  410. };
  411. ShaderBuilder.prototype.BuildMaterial = function (scene) {
  412. this.PrepareBeforeMaterialBuild(scene);
  413. if (Shader.ShaderIdentity == null)
  414. Shader.ShaderIdentity = 0;
  415. Shader.ShaderIdentity++;
  416. var shaderMaterial = new ShaderMaterialHelper().ShaderMaterial("ShaderBuilder_" + Shader.ShaderIdentity, scene, {
  417. Pixel: Shader.Join(this.Fragment)
  418. .replace("#[Source]", this.Body),
  419. Vertex: Shader.Join(this.Vertex)
  420. .replace("#[Source]", Shader.Def(this.VertexBody, ""))
  421. .replace("#[AfterFinishVertex]", Shader.Def(this.AfterVertex, ""))
  422. }, {
  423. uniforms: this.Uniforms,
  424. attributes: this.Attributes
  425. });
  426. Shader.Indexer = 1;
  427. return this.PrepareMaterial(shaderMaterial, scene);
  428. };
  429. ShaderBuilder.prototype.Event = function (index, mat) {
  430. Shader.Me.Setting.Flags = true;
  431. Shader.Indexer++;
  432. this.Body = Shader.Def(this.Body, "");
  433. this.Body += " if ( floor(mod( " + ShaderMaterialHelperStatics.uniformFlags + "/pow(2.," + Shader.Print(index) + "),2.)) == 1.) { " + mat + " } ";
  434. return this;
  435. };
  436. ShaderBuilder.prototype.EventVertex = function (index, mat) {
  437. Shader.Me.Setting.Flags = true;
  438. Shader.Me.Setting.Vertex = true;
  439. Shader.Indexer++;
  440. this.VertexBody = Shader.Def(this.VertexBody, "");
  441. this.VertexBody += " if( floor(mod( " + ShaderMaterialHelperStatics.uniformFlags + "/pow(2.," + Shader.Print(index) + "),2.)) == 1. ){ " + mat + "}";
  442. return this;
  443. };
  444. ShaderBuilder.prototype.Transparency = function () {
  445. Shader.Me.Setting.Transparency = true;
  446. return this;
  447. };
  448. ShaderBuilder.prototype.Wired = function () {
  449. Shader.Me.Setting.Wire = true;
  450. return this;
  451. };
  452. ShaderBuilder.prototype.VertexShader = function (mat) {
  453. this.VertexBody = Shader.Def(this.VertexBody, "");
  454. this.VertexBody += mat;
  455. return this;
  456. };
  457. ShaderBuilder.prototype.Solid = function (color) {
  458. color = Shader.Def(color, { r: 0., g: 0., b: 0., a: 1. });
  459. color.a = Shader.Def(color.a, 1.);
  460. color.r = Shader.Def(color.r, 0.);
  461. color.g = Shader.Def(color.g, 0.);
  462. color.b = Shader.Def(color.b, 0.);
  463. this.Body = Shader.Def(this.Body, "");
  464. this.Body += " result = vec4(" + Shader.Print(color.r) + "," + Shader.Print(color.g) + "," + Shader.Print(color.b) + "," + Shader.Print(color.a) + ");";
  465. return this;
  466. };
  467. ShaderBuilder.prototype.GetMapIndex = function (key) {
  468. if (Shader.Me.Setting.Texture2Ds != null) {
  469. for (var it in Shader.Me.Setting.Texture2Ds) {
  470. if (this.Setting.Texture2Ds[it].key == key) {
  471. return it;
  472. }
  473. }
  474. }
  475. else
  476. Shader.Me.Setting.Texture2Ds = [];
  477. return -1;
  478. };
  479. ShaderBuilder.prototype.GetCubeMapIndex = function (key) {
  480. if (Shader.Me.Setting.TextureCubes != null) {
  481. for (var it in Shader.Me.Setting.TextureCubes) {
  482. if (this.Setting.TextureCubes[it].key == key) {
  483. return it;
  484. }
  485. }
  486. }
  487. else
  488. Shader.Me.Setting.TextureCubes = [];
  489. return -1;
  490. };
  491. ShaderBuilder.prototype.Map = function (option) {
  492. Shader.Indexer++;
  493. option = Shader.Def(option, { path: '/images/color.png' });
  494. var s = Shader.Me.GetMapIndex(option.path);
  495. if (s == -1) {
  496. Shader.Me.Setting.Texture2Ds.push({ key: option.path, inVertex: option.useInVertex, inFragment: true });
  497. }
  498. else {
  499. Shader.Me.Setting.Texture2Ds[s].inVertex = true;
  500. }
  501. s = Shader.Me.GetMapIndex(option.path);
  502. Shader.Me.Setting.Center = true;
  503. Shader.Me.Setting.Helpers = true;
  504. Shader.Me.Setting.Uv = true;
  505. option.normal = Shader.Def(option.normal, Normals.Map);
  506. option.alpha = Shader.Def(option.alpha, false);
  507. option.bias = Shader.Def(option.bias, "0.");
  508. option.normalLevel = Shader.Def(option.normalLevel, 1.0);
  509. option.path = Shader.Def(option.path, "qa.jpg");
  510. option.rotation = Shader.Def(option.rotation, { x: 0, y: 0, z: 0 });
  511. option.scaleX = Shader.Def(option.scaleX, 1.);
  512. option.scaleY = Shader.Def(option.scaleY, 1.);
  513. option.useInVertex = Shader.Def(option.useInVertex, false);
  514. option.x = Shader.Def(option.x, 0.0);
  515. option.y = Shader.Def(option.y, 0.0);
  516. option.uv = Shader.Def(option.uv, ShaderMaterialHelperStatics.Uv);
  517. option.animation = Shader.Def(option.animation, false);
  518. option.tiled = Shader.Def(option.tiled, false);
  519. option.columnIndex = Shader.Def(option.columnIndex, 1);
  520. option.rowIndex = Shader.Def(option.rowIndex, 1);
  521. option.animationSpeed = Shader.Def(option.animationSpeed, 2000);
  522. option.animationFrameEnd = Shader.Def(option.animationFrameEnd, 100) + option.indexCount;
  523. option.animationFrameStart = Shader.Def(option.animationFrameStart, 0) + option.indexCount;
  524. option.indexCount = Shader.Def(option.indexCount, 1);
  525. var frameLength = Math.min(option.animationFrameEnd - option.animationFrameStart, option.indexCount * option.indexCount);
  526. var uv = Shader.Def(option.uv, ShaderMaterialHelperStatics.Uv);
  527. if (option.uv == "planar") {
  528. uv = ShaderMaterialHelperStatics.Position;
  529. }
  530. else {
  531. uv = 'vec3(' + option.uv + '.x,' + option.uv + '.y,0.)';
  532. }
  533. option.scaleX /= option.indexCount;
  534. option.scaleY /= option.indexCount;
  535. var rotate = ["vec3 centeri#[Ind] = " + ShaderMaterialHelperStatics.Center + ";",
  536. "vec3 ppo#[Ind] = r_z( " + uv + "," + Shader.Print(option.rotation.x) + ",centeri#[Ind]); ",
  537. " ppo#[Ind] = r_y( ppo#[Ind]," + Shader.Print(option.rotation.y) + ",centeri#[Ind]); ",
  538. " ppo#[Ind] = r_x( ppo#[Ind]," + Shader.Print(option.rotation.x) + ",centeri#[Ind]); ",
  539. "vec3 nrm#[Ind] = r_z( " + option.normal + "," + Shader.Print(option.rotation.x) + ",centeri#[Ind]); ",
  540. " nrm#[Ind] = r_y( nrm#[Ind]," + Shader.Print(option.rotation.y) + ",centeri#[Ind]); ",
  541. " nrm#[Ind] = r_x( nrm#[Ind]," + Shader.Print(option.rotation.z) + ",centeri#[Ind]); "].join("\n\
  542. ");
  543. var sresult = Shader.Join([rotate,
  544. " vec4 color#[Ind] = texture2D(" +
  545. ShaderMaterialHelperStatics.Texture2D + s + " ,ppo#[Ind].xy*vec2(" +
  546. Shader.Print(option.scaleX) + "," + Shader.Print(option.scaleY) + ")+vec2(" +
  547. Shader.Print(option.x) + "," + Shader.Print(option.y) + ")" +
  548. (option.bias != null ? "," + Shader.Print(option.bias) : "") + ");",
  549. " if(nrm#[Ind].z < " + Shader.Print(option.normalLevel) + "){ ",
  550. (option.alpha ? " result = color#[Ind];" : "result = vec4(color#[Ind].rgb , 1.); "),
  551. "}"]);
  552. if (option.indexCount > 1 || option.tiled) {
  553. option.columnIndex = option.indexCount - option.columnIndex + 1.0;
  554. sresult = [
  555. " vec3 uvt#[Ind] = vec3(" + uv + ".x*" + Shader.Print(option.scaleX) + "+" + Shader.Print(option.x) + "," + uv + ".y*" + Shader.Print(option.scaleY) + "+" + Shader.Print(option.y) + ",0.0); ",
  556. " ",
  557. " float xst#[Ind] = 1./(" + Shader.Print(option.indexCount) + "*2.); ",
  558. " float yst#[Ind] =1./(" + Shader.Print(option.indexCount) + "*2.); ",
  559. " float xs#[Ind] = 1./" + Shader.Print(option.indexCount) + "; ",
  560. " float ys#[Ind] = 1./" + Shader.Print(option.indexCount) + "; ",
  561. " float yid#[Ind] = " + Shader.Print(option.columnIndex - 1.0) + " ; ",
  562. " float xid#[Ind] = " + Shader.Print(option.rowIndex - 1.0) + "; ",
  563. option.animation ? " float ind_a#[Ind] = floor(mod(time*0.001*" + Shader.Print(option.animationSpeed) + ", " + Shader.Print(frameLength) + " )+" + Shader.Print(option.animationFrameStart) + ");" +
  564. " yid#[Ind] = " + Shader.Print(option.indexCount) + "- floor(ind_a#[Ind] / " + Shader.Print(option.indexCount) + ");" +
  565. " xid#[Ind] = floor(mod(ind_a#[Ind] , " + Shader.Print(option.indexCount) + ")); "
  566. : "",
  567. " float xi#[Ind] = mod(uvt#[Ind].x ,xs#[Ind])+xs#[Ind]*xid#[Ind] ; ",
  568. " float yi#[Ind] = mod(uvt#[Ind].y ,ys#[Ind])+ys#[Ind]*yid#[Ind] ; ",
  569. " ",
  570. " float xi2#[Ind] = mod(uvt#[Ind].x -xs#[Ind]*0.5 ,xs#[Ind])+xs#[Ind]*xid#[Ind] ; ",
  571. " float yi2#[Ind] = mod(uvt#[Ind].y -ys#[Ind]*0.5,ys#[Ind])+ys#[Ind]*yid#[Ind] ; ",
  572. " ",
  573. " ",
  574. " vec4 f#[Ind] = texture2D(" + ShaderMaterialHelperStatics.Texture2D + s + ",vec2(xi#[Ind],yi#[Ind])) ; ",
  575. " result = f#[Ind] ; ",
  576. (option.tiled ? [" vec4 f2#[Ind] = texture2D(" + ShaderMaterialHelperStatics.Texture2D + s + ",vec2(xi2#[Ind]+xid#[Ind] ,yi#[Ind])) ; ",
  577. " vec4 f3#[Ind] = texture2D(" + ShaderMaterialHelperStatics.Texture2D + s + ",vec2(xi#[Ind],yi2#[Ind]+yid#[Ind])) ; ",
  578. " vec4 f4#[Ind] = texture2D(" + ShaderMaterialHelperStatics.Texture2D + s + ",vec2(xi2#[Ind]+xid#[Ind],yi2#[Ind]+yid#[Ind])) ; ",
  579. " ",
  580. " ",
  581. " float ir#[Ind] = 0.,ir2#[Ind] = 0.; ",
  582. " ",
  583. " if( yi2#[Ind] >= yid#[Ind] *ys#[Ind] ){ ",
  584. " ir2#[Ind] = min(2.,max(0.,( yi2#[Ind]-yid#[Ind] *ys#[Ind])*2.0/ys#[Ind] )) ; ",
  585. " if(ir2#[Ind] > 1.0) ir2#[Ind] =1.0-(ir2#[Ind]-1.0); ",
  586. " ir2#[Ind] = min(1.0,max(0.0,pow(ir2#[Ind]," + Shader.Print(15.) + " )*" + Shader.Print(3.) + ")); ",
  587. " result = result *(1.0-ir2#[Ind]) +f3#[Ind]*ir2#[Ind] ; ",
  588. " } ",
  589. " if( xi2#[Ind] >= xid#[Ind] *xs#[Ind] ){ ",
  590. " ir2#[Ind] = min(2.,max(0.,( xi2#[Ind]-xid#[Ind] *xs#[Ind])*2.0/xs#[Ind] )) ; ",
  591. " if(ir2#[Ind] > 1.0) ir2#[Ind] =1.0-(ir2#[Ind]-1.0); ",
  592. " ir2#[Ind] = min(1.0,max(0.0,pow(ir2#[Ind]," + Shader.Print(15.) + " )*" + Shader.Print(3.) + ")); ",
  593. " result = result *(1.0-ir2#[Ind]) +f2#[Ind]*ir2#[Ind] ; ",
  594. " } ",
  595. " if( xi2#[Ind] >= xid#[Ind] *xs#[Ind] && xi2#[Ind] >= xid#[Ind] *xs#[Ind] ){ ",
  596. " ir2#[Ind] = min(2.,max(0.,( xi2#[Ind]-xid#[Ind] *xs#[Ind])*2.0/xs#[Ind] )) ; ",
  597. " float ir3#[Ind] = min(2.,max(0.,( yi2#[Ind]-yid#[Ind] *ys#[Ind])*2.0/ys#[Ind] )) ; ",
  598. " if(ir2#[Ind] > 1.0) ir2#[Ind] =1.0-(ir2#[Ind]-1.0); ",
  599. " if(ir3#[Ind] > 1.0) ir3#[Ind] =1.0-(ir3#[Ind]-1.0); ",
  600. " ir2#[Ind] = min(1.0,max(0.0,pow(ir2#[Ind]," + Shader.Print(15.) + " )*" + Shader.Print(3.) + ")); ",
  601. " ir3#[Ind] = min(1.0,max(0.0,pow(ir3#[Ind]," + Shader.Print(15.) + " )*" + Shader.Print(3.) + ")); ",
  602. " ir2#[Ind] = min(1.0,max(0.0, ir2#[Ind]* ir3#[Ind] )); ",
  603. " if(nrm#[Ind].z < " + Shader.Print(option.normalLevel) + "){ ",
  604. (option.alpha ? " result = result *(1.0-ir2#[Ind]) +f4#[Ind]* ir2#[Ind] ;" : " result = vec4(result.xyz*(1.0-ir2#[Ind]) +f4#[Ind].xyz* ir2#[Ind] ,1.0); "),
  605. "}",
  606. " } "
  607. ].join("\n") : "")].join("\n");
  608. }
  609. sresult = Shader.Replace(sresult, '#[Ind]', "_" + Shader.Indexer + "_");
  610. this.Body = Shader.Def(this.Body, "");
  611. this.Body += sresult;
  612. return this;
  613. };
  614. ShaderBuilder.prototype.Multi = function (mats, combine) {
  615. combine = Shader.Def(combine, true);
  616. Shader.Indexer++;
  617. var pre = "", ps = ["", "", "", ""], psh = "0.0";
  618. for (var i = 0; i < mats.length; i++) {
  619. if (mats[i].result == undefined || mats[i].result == null)
  620. mats[i] = { result: mats[i], opacity: 1.0 };
  621. pre += " vec4 result#[Ind]" + i + ";result#[Ind]" + i + " = vec4(0.,0.,0.,0.); float rp#[Ind]" + i + " = " + Shader.Print(mats[i].opacity) + "; \n\
  622. ";
  623. pre += mats[i].result + "\n\
  624. ";
  625. pre += " result#[Ind]" + i + " = result; \n\
  626. ";
  627. ps[0] += (i == 0 ? "" : " + ") + "result#[Ind]" + i + ".x*rp#[Ind]" + i;
  628. ps[1] += (i == 0 ? "" : " + ") + "result#[Ind]" + i + ".y*rp#[Ind]" + i;
  629. ps[2] += (i == 0 ? "" : " + ") + "result#[Ind]" + i + ".z*rp#[Ind]" + i;
  630. ps[3] += (i == 0 ? "" : " + ") + "result#[Ind]" + i + ".w*rp#[Ind]" + i;
  631. psh += "+" + Shader.Print(mats[i].opacity);
  632. }
  633. if (combine) {
  634. ps[0] = "(" + ps[0] + ")/(" + Shader.Print(psh) + ")";
  635. ps[1] = "(" + ps[1] + ")/(" + Shader.Print(psh) + ")";
  636. ps[2] = "(" + ps[2] + ")/(" + Shader.Print(psh) + ")";
  637. ps[3] = "(" + ps[3] + ")/(" + Shader.Print(psh) + ")";
  638. }
  639. pre += "result = vec4(" + ps[0] + "," + ps[1] + "," + ps[2] + "," + ps[3] + ");";
  640. this.Body = Shader.Def(this.Body, "");
  641. this.Body += Shader.Replace(pre, "#[Ind]", "_" + Shader.Indexer + "_");
  642. return this;
  643. };
  644. ShaderBuilder.prototype.Back = function (mat) {
  645. Shader.Me.Setting.Back = true;
  646. mat = Shader.Def(mat, '');
  647. this.Body = Shader.Def(this.Body, "");
  648. this.Body += 'if(' + ShaderMaterialHelperStatics.face_back + '){' + mat + ';}';
  649. return this;
  650. };
  651. ShaderBuilder.prototype.InLine = function (mat) {
  652. mat = Shader.Def(mat, '');
  653. this.Body = Shader.Def(this.Body, "");
  654. this.Body += mat;
  655. return this;
  656. };
  657. ShaderBuilder.prototype.Front = function (mat) {
  658. mat = Shader.Def(mat, '');
  659. this.Body = Shader.Def(this.Body, "");
  660. this.Body += 'if(' + ShaderMaterialHelperStatics.face_front + '){' + mat + ';}';
  661. return this;
  662. };
  663. ShaderBuilder.prototype.Range = function (mat1, mat2, option) {
  664. Shader.Indexer++;
  665. var k = Shader.Indexer;
  666. option.start = Shader.Def(option.start, 0.);
  667. option.end = Shader.Def(option.end, 1.);
  668. option.direction = Shader.Def(option.direction, ShaderMaterialHelperStatics.Position + '.y');
  669. var sresult = [
  670. "float s_r_dim#[Ind] = " + option.direction + ";",
  671. "if(s_r_dim#[Ind] > " + Shader.Print(option.end) + "){",
  672. mat2,
  673. "}",
  674. "else { ",
  675. mat1,
  676. " vec4 mat1#[Ind]; mat1#[Ind] = result;",
  677. " if(s_r_dim#[Ind] > " + Shader.Print(option.start) + "){ ",
  678. mat2,
  679. " vec4 mati2#[Ind];mati2#[Ind] = result;",
  680. " float s_r_cp#[Ind] = (s_r_dim#[Ind] - (" + Shader.Print(option.start) + "))/(" + Shader.Print(option.end) + "-(" + Shader.Print(option.start) + "));",
  681. " float s_r_c#[Ind] = 1.0 - s_r_cp#[Ind];",
  682. " result = vec4(mat1#[Ind].x*s_r_c#[Ind]+mati2#[Ind].x*s_r_cp#[Ind],mat1#[Ind].y*s_r_c#[Ind]+mati2#[Ind].y*s_r_cp#[Ind],mat1#[Ind].z*s_r_c#[Ind]+mati2#[Ind].z*s_r_cp#[Ind],mat1#[Ind].w*s_r_c#[Ind]+mati2#[Ind].w*s_r_cp#[Ind]);",
  683. " }",
  684. " else { result = mat1#[Ind]; }",
  685. "}"
  686. ].join('\n\
  687. ');
  688. sresult = Shader.Replace(sresult, '#[Ind]', "_" + Shader.Indexer + "_");
  689. this.Body = Shader.Def(this.Body, "");
  690. this.Body += sresult;
  691. return this;
  692. };
  693. ShaderBuilder.prototype.Reference = function (index, mat) {
  694. if (Shader.Me.References == null)
  695. Shader.Me.References = "";
  696. var sresult = "vec4 resHelp#[Ind] = result;";
  697. if (Shader.Me.References.indexOf("," + index + ",") == -1) {
  698. Shader.Me.References += "," + index + ",";
  699. sresult += " vec4 result_" + index + " = vec4(0.);\n\
  700. ";
  701. }
  702. if (mat == null) {
  703. sresult += " result_" + index + " = result;";
  704. }
  705. else {
  706. sresult += mat + "\n\
  707. result_" + index + " = result;";
  708. }
  709. sresult += "result = resHelp#[Ind] ;";
  710. sresult = Shader.Replace(sresult, '#[Ind]', "_" + Shader.Indexer + "_");
  711. this.Body = Shader.Def(this.Body, "");
  712. this.Body += sresult;
  713. return this;
  714. };
  715. ShaderBuilder.prototype.ReplaceColor = function (index, color, mat, option) {
  716. Shader.Indexer++;
  717. option = Shader.Def(option, {});
  718. var d = Shader.Def(option.rangeStep, -0.280);
  719. var d2 = Shader.Def(option.rangePower, 0.0);
  720. var d3 = Shader.Def(option.colorIndex, 0.0);
  721. var d4 = Shader.Def(option.colorStep, 1.0);
  722. var ilg = Shader.Def(option.indexToEnd, false);
  723. var lg = " > 0.5 + " + Shader.Print(d) + " ";
  724. var lw = " < 0.5 - " + Shader.Print(d) + " ";
  725. var rr = "((result_" + index + ".x*" + Shader.Print(d4) + "-" + Shader.Print(d3) + ")>1.0 ? 0. : max(0.,(result_" + index + ".x*" + Shader.Print(d4) + "-" + Shader.Print(d3) + ")))";
  726. var rg = "((result_" + index + ".y*" + Shader.Print(d4) + "-" + Shader.Print(d3) + ")>1.0 ? 0. : max(0.,(result_" + index + ".y*" + Shader.Print(d4) + "-" + Shader.Print(d3) + ")))";
  727. var rb = "((result_" + index + ".z*" + Shader.Print(d4) + "-" + Shader.Print(d3) + ")>1.0 ? 0. : max(0.,(result_" + index + ".z*" + Shader.Print(d4) + "-" + Shader.Print(d3) + ")))";
  728. if (ilg) {
  729. rr = "min(1.0, max(0.,(result_" + index + ".x*" + Shader.Print(d4) + "-" + Shader.Print(d3) + ")))";
  730. rg = "min(1.0, max(0.,(result_" + index + ".y*" + Shader.Print(d4) + "-" + Shader.Print(d3) + ")))";
  731. rb = "min(1.0, max(0.,(result_" + index + ".z*" + Shader.Print(d4) + "-" + Shader.Print(d3) + ")))";
  732. }
  733. var a = " && ";
  734. var p = " + ";
  735. var r = "";
  736. var cond = "";
  737. switch (color) {
  738. case Helper.White:
  739. cond = rr + lg + a + rg + lg + a + rb + lg;
  740. r = "(" + rr + p + rg + p + rb + ")/3.0";
  741. break;
  742. case Helper.Cyan:
  743. cond = rr + lw + a + rg + lg + a + rb + lg;
  744. r = "(" + rg + p + rb + ")/2.0 - (" + rr + ")/1.0";
  745. break;
  746. case Helper.Pink:
  747. cond = rr + lg + a + rg + lw + a + rb + lg;
  748. r = "(" + rr + p + rb + ")/2.0 - (" + rg + ")/1.0";
  749. break;
  750. case Helper.Yellow:
  751. cond = rr + lg + a + rg + lg + a + rb + lw;
  752. r = "(" + rr + p + rg + ")/2.0 - (" + rb + ")/1.0";
  753. break;
  754. case Helper.Blue:
  755. cond = rr + lw + a + rg + lw + a + rb + lg;
  756. r = "(" + rb + ")/1.0 - (" + rr + p + rg + ")/2.0";
  757. break;
  758. case Helper.Red:
  759. cond = rr + lg + a + rg + lw + a + rb + lw;
  760. r = "(" + rr + ")/1.0 - (" + rg + p + rb + ")/2.0";
  761. break;
  762. case Helper.Green:
  763. cond = rr + lw + a + rg + lg + a + rb + lw;
  764. r = "(" + rg + ")/1.0 - (" + rr + p + rb + ")/2.0";
  765. break;
  766. case Helper.Black:
  767. cond = rr + lw + a + rg + lw + a + rb + lw;
  768. r = "1.0-(" + rr + p + rg + p + rb + ")/3.0";
  769. break;
  770. }
  771. var sresult = " if( " + cond + " ) { vec4 oldrs#[Ind] = vec4(result);float al#[Ind] = max(0.0,min(1.0," + r + "+(" + Shader.Print(d2) + "))); float l#[Ind] = 1.0-al#[Ind]; " + mat + " result = result*al#[Ind] + oldrs#[Ind] * l#[Ind]; }";
  772. sresult = Shader.Replace(sresult, '#[Ind]', "_" + Shader.Indexer + "_");
  773. this.Body = Shader.Def(this.Body, "");
  774. this.Body += sresult;
  775. return this;
  776. };
  777. ShaderBuilder.prototype.Blue = function (index, mat, option) {
  778. return this.ReplaceColor(index, Helper.Blue, mat, option);
  779. };
  780. ShaderBuilder.prototype.Cyan = function (index, mat, option) {
  781. return this.ReplaceColor(index, Helper.Cyan, mat, option);
  782. };
  783. ShaderBuilder.prototype.Red = function (index, mat, option) {
  784. return this.ReplaceColor(index, Helper.Red, mat, option);
  785. };
  786. ShaderBuilder.prototype.Yellow = function (index, mat, option) {
  787. return this.ReplaceColor(index, Helper.Yellow, mat, option);
  788. };
  789. ShaderBuilder.prototype.Green = function (index, mat, option) {
  790. return this.ReplaceColor(index, Helper.Green, mat, option);
  791. };
  792. ShaderBuilder.prototype.Pink = function (index, mat, option) {
  793. return this.ReplaceColor(index, Helper.Pink, mat, option);
  794. };
  795. ShaderBuilder.prototype.White = function (index, mat, option) {
  796. return this.ReplaceColor(index, Helper.White, mat, option);
  797. };
  798. ShaderBuilder.prototype.Black = function (index, mat, option) {
  799. return this.ReplaceColor(index, Helper.Black, mat, option);
  800. };
  801. ShaderBuilder.prototype.ReflectCube = function (option) {
  802. Shader.Indexer++;
  803. option = Shader.Def(option, { path: '/images/cube/a' });
  804. var s = Shader.Me.GetCubeMapIndex(option.path);
  805. if (s == -1) {
  806. Shader.Me.Setting.TextureCubes.push({ key: option.path, inVertex: option.useInVertex, inFragment: true });
  807. }
  808. else {
  809. Shader.Me.Setting.TextureCubes[s].inVertex = true;
  810. }
  811. s = Shader.Me.GetCubeMapIndex(option.path);
  812. option.normal = Shader.Def(option.normal, Normals.Map);
  813. option.alpha = Shader.Def(option.alpha, false);
  814. option.bias = Shader.Def(option.bias, "0.");
  815. option.normalLevel = Shader.Def(option.normalLevel, 1.0);
  816. option.rotation = Shader.Def(option.rotation, { x: 0, y: 0, z: 0 });
  817. option.scaleX = Shader.Def(option.scaleX, 1.);
  818. option.scaleY = Shader.Def(option.scaleY, 1.);
  819. option.useInVertex = Shader.Def(option.useInVertex, false);
  820. option.x = Shader.Def(option.x, 0.0);
  821. option.y = Shader.Def(option.y, 0.0);
  822. option.uv = Shader.Def(option.uv, ShaderMaterialHelperStatics.Uv);
  823. option.reflectMap = Shader.Def(option.reflectMap, "1.");
  824. Shader.Me.Setting.Center = true;
  825. Shader.Me.Setting.Camera = true;
  826. Shader.Me.Setting.ReflectMatrix = true;
  827. var sresult = "";
  828. if (option.equirectangular) {
  829. option.path = Shader.Def(option.path, '/images/cube/roofl1.jpg');
  830. var s = Shader.Me.GetMapIndex(option.path);
  831. if (s == -1) {
  832. Shader.Me.Setting.Texture2Ds.push({ key: option.path, inVertex: option.useInVertex, inFragment: true });
  833. }
  834. else {
  835. Shader.Me.Setting.Texture2Ds[s].inVertex = true;
  836. }
  837. s = Shader.Me.GetMapIndex(option.path);
  838. Shader.Me.Setting.VertexWorld = true;
  839. Shader.Me.Setting.FragmentWorld = true;
  840. sresult = ' vec3 nWorld#[Ind] = normalize( mat3( world[0].xyz, world[1].xyz, world[2].xyz ) * ' + option.normal + '); ' +
  841. ' vec3 vReflect#[Ind] = normalize( reflect( normalize( ' + ShaderMaterialHelperStatics.Camera + '- vec3(world * vec4(' + ShaderMaterialHelperStatics.Position + ', 1.0))), nWorld#[Ind] ) ); ' +
  842. 'float yaw#[Ind] = .5 - atan( vReflect#[Ind].z, -1.* vReflect#[Ind].x ) / ( 2.0 * 3.14159265358979323846264); ' +
  843. ' float pitch#[Ind] = .5 - atan( vReflect#[Ind].y, length( vReflect#[Ind].xz ) ) / ( 3.14159265358979323846264); ' +
  844. ' vec3 color#[Ind] = texture2D( ' + ShaderMaterialHelperStatics.Texture2D + s + ', vec2( yaw#[Ind], pitch#[Ind]),' + Shader.Print(option.bias) + ' ).rgb; result = vec4(color#[Ind] ,1.);';
  845. }
  846. else {
  847. option.path = Shader.Def(option.path, "/images/cube/a");
  848. sresult = [
  849. "vec3 viewDir#[Ind] = " + ShaderMaterialHelperStatics.Position + " - " + ShaderMaterialHelperStatics.Camera + " ;",
  850. " viewDir#[Ind] =r_x(viewDir#[Ind] ," + Shader.Print(option.rotation.x) + ", " + ShaderMaterialHelperStatics.Center + ");",
  851. " viewDir#[Ind] =r_y(viewDir#[Ind] ," + Shader.Print(option.rotation.y) + "," + ShaderMaterialHelperStatics.Center + ");",
  852. " viewDir#[Ind] =r_z(viewDir#[Ind] ," + Shader.Print(option.rotation.z) + "," + ShaderMaterialHelperStatics.Center + ");",
  853. "vec3 coords#[Ind] = " + (option.refract ? "refract" : "reflect") + "(viewDir#[Ind]" + (option.revers ? "*vec3(1.0)" : "*vec3(-1.0)") + ", " + option.normal + " " + (option.refract ? ",(" + Shader.Print(option.refractMap) + ")" : "") + " )+" + ShaderMaterialHelperStatics.Position + "; ",
  854. "vec3 vReflectionUVW#[Ind] = vec3( " + ShaderMaterialHelperStatics.ReflectMatrix + " * vec4(coords#[Ind], 0)); ",
  855. "vec3 rc#[Ind]= textureCube(" +
  856. ShaderMaterialHelperStatics.TextureCube + s + ", vReflectionUVW#[Ind]," + Shader.Print(option.bias) + ").rgb;",
  857. "result =result + vec4(rc#[Ind].x ,rc#[Ind].y,rc#[Ind].z, " + (!option.alpha ? "1." : "(rc#[Ind].x+rc#[Ind].y+rc#[Ind].z)/3.0 ") + ")*(min(1.,max(0.," + Shader.Print(option.reflectMap) + "))); "
  858. ].join('\n\
  859. ');
  860. }
  861. sresult = Shader.Replace(sresult, '#[Ind]', "_" + Shader.Indexer + "_");
  862. this.Body = Shader.Def(this.Body, "");
  863. this.Body += sresult;
  864. return this;
  865. };
  866. ShaderBuilder.prototype.NormalMap = function (val, mat) {
  867. Shader.Me.Setting.NormalOpacity = val;
  868. Shader.Me.Setting.NormalMap = mat;
  869. return this;
  870. };
  871. ShaderBuilder.prototype.SpecularMap = function (mat) {
  872. Shader.Me.Setting.SpecularMap = mat;
  873. return this;
  874. };
  875. ShaderBuilder.prototype.Instance = function () {
  876. var setting = Shader.Me.Setting;
  877. var instance = new ShaderBuilder();
  878. instance.Parent = Shader.Me;
  879. instance.Setting = setting;
  880. return instance;
  881. };
  882. ShaderBuilder.prototype.Reflect = function (option, opacity) {
  883. opacity = Shader.Def(opacity, 1.);
  884. return this.Multi(["result = result;", { result: this.Instance().ReflectCube(option).Build(), opacity: opacity }], true);
  885. };
  886. ShaderBuilder.prototype.Light = function (option) {
  887. option = Shader.Def(option, {});
  888. option.color = Shader.Def(option.color, { r: 1., g: 1., b: 1., a: 1. });
  889. option.darkColorMode = Shader.Def(option.darkColorMode, false);
  890. option.direction = Shader.Def(option.direction, "vec3(sin(time*0.02)*28.,sin(time*0.02)*8.+10.,cos(time*0.02)*28.)");
  891. option.normal = Shader.Def(option.normal, Normals.Map);
  892. option.rotation = Shader.Def(option.rotation, { x: 0., y: 0., z: 0. });
  893. option.specular = Shader.Def(option.specular, Speculars.Map);
  894. option.specularLevel = Shader.Def(option.specularLevel, 1.);
  895. option.specularPower = Shader.Def(option.specularPower, 1.);
  896. option.phonge = Shader.Def(option.phonge, 0.);
  897. option.phongePower = Shader.Def(option.phongePower, 1.);
  898. option.phongeLevel = Shader.Def(option.phongeLevel, 1.);
  899. option.supplement = Shader.Def(option.supplement, false);
  900. option.reducer = Shader.Def(option.reducer, '1.');
  901. var c_c = option.color;
  902. if (option.darkColorMode) {
  903. c_c.a = 1.0 - c_c.a;
  904. c_c.r = 1.0 - c_c.r;
  905. c_c.g = 1.0 - c_c.g;
  906. c_c.b = 1.0 - c_c.b;
  907. c_c.a = c_c.a - 1.0;
  908. }
  909. Shader.Indexer++;
  910. Shader.Me.Setting.Camera = true;
  911. Shader.Me.Setting.FragmentWorld = true;
  912. Shader.Me.Setting.VertexWorld = true;
  913. Shader.Me.Setting.Helpers = true;
  914. Shader.Me.Setting.Center = true;
  915. var sresult = Shader.Join([
  916. " vec3 dir#[Ind] = normalize( vec3(world * vec4(" + ShaderMaterialHelperStatics.Position + ",1.)) - " + ShaderMaterialHelperStatics.Camera + ");",
  917. " dir#[Ind] =r_x(dir#[Ind] ," + Shader.Print(option.rotation.x) + ",vec3(" + ShaderMaterialHelperStatics.Center + "));",
  918. " dir#[Ind] =r_y(dir#[Ind] ," + Shader.Print(option.rotation.y) + ",vec3(" + ShaderMaterialHelperStatics.Center + "));",
  919. " dir#[Ind] =r_z(dir#[Ind] ," + Shader.Print(option.rotation.z) + ",vec3(" + ShaderMaterialHelperStatics.Center + "));",
  920. " vec4 p1#[Ind] = vec4(" + option.direction + ",.0); ",
  921. " vec4 c1#[Ind] = vec4(" + Shader.Print(c_c.r) + "," + Shader.Print(c_c.g) + "," + Shader.Print(c_c.b) + ",0.0); ",
  922. " vec3 vnrm#[Ind] = normalize(vec3(world * vec4(" + option.normal + ", 0.0))); ",
  923. " vec3 l#[Ind]= normalize(p1#[Ind].xyz " +
  924. (!option.parallel ? "- vec3(world * vec4(" + ShaderMaterialHelperStatics.Position + ",1.)) " : "")
  925. + "); ",
  926. " vec3 vw#[Ind]= normalize(camera - vec3(world * vec4(" + ShaderMaterialHelperStatics.Position + ",1.))); ",
  927. " vec3 aw#[Ind]= normalize(vw#[Ind]+ l#[Ind]); ",
  928. " float sc#[Ind]= max(0.,min(1., dot(vnrm#[Ind], aw#[Ind]))); ",
  929. " sc#[Ind]= pow(sc#[Ind]*min(1.,max(0.," + Shader.Print(option.specular) + ")), (" + Shader.Print(option.specularPower * 1000.) + "))/" + Shader.Print(option.specularLevel) + " ; ",
  930. " float ph#[Ind]= pow(" + Shader.Print(option.phonge) + "*2., (" + Shader.Print(option.phongePower) + "*0.3333))/(" + Shader.Print(option.phongeLevel) + "*3.) ; ",
  931. " float ndl#[Ind] = max(0., dot(vnrm#[Ind], l#[Ind])); ",
  932. " float ls#[Ind] = " + (option.supplement ? "1.0 -" : "") + "max(0.,min(1.,ndl#[Ind]*ph#[Ind]*(" + Shader.Print(option.reducer) + "))) ; ",
  933. " result += vec4( c1#[Ind].xyz*( ls#[Ind])*" + Shader.Print(c_c.a) + " , ls#[Ind]); ",
  934. " float ls2#[Ind] = " + (option.supplement ? "1.0 -" : "") + "max(0.,min(1., sc#[Ind]*(" + Shader.Print(option.reducer) + "))) ; ",
  935. " result += vec4( c1#[Ind].xyz*( ls2#[Ind])*" + Shader.Print(c_c.a) + " , ls2#[Ind]); ",
  936. ]);
  937. sresult = Shader.Replace(sresult, '#[Ind]', "_" + Shader.Indexer + "_");
  938. this.Body = Shader.Def(this.Body, "");
  939. this.Body += sresult;
  940. return this;
  941. };
  942. ShaderBuilder.prototype.Effect = function (option) {
  943. var op = Shader.Def(option, {});
  944. Shader.Indexer++;
  945. var sresult = [
  946. 'vec4 res#[Ind] = vec4(0.);',
  947. 'res#[Ind].x = ' + (op.px ? Shader.Replace(Shader.Replace(Shader.Replace(Shader.Replace(op.px, 'px', 'result.x'), 'py', 'result.y'), 'pz', 'result.z'), 'pw', 'result.w') + ';' : ' result.x;'),
  948. 'res#[Ind].y = ' + (op.py ? Shader.Replace(Shader.Replace(Shader.Replace(Shader.Replace(op.py, 'px', 'result.x'), 'py', 'result.y'), 'pz', 'result.z'), 'pw', 'result.w') + ';' : ' result.y;'),
  949. 'res#[Ind].z = ' + (op.pz ? Shader.Replace(Shader.Replace(Shader.Replace(Shader.Replace(op.pz, 'px', 'result.x'), 'py', 'result.y'), 'pz', 'result.z'), 'pw', 'result.w') + ';' : ' result.z;'),
  950. 'res#[Ind].w = ' + (op.pw ? Shader.Replace(Shader.Replace(Shader.Replace(Shader.Replace(op.pw, 'px', 'result.x'), 'py', 'result.y'), 'pz', 'result.z'), 'pw', 'result.w') + ';' : ' result.w;'),
  951. 'res#[Ind] = ' + (op.pr ? ' vec4(' + Shader.Replace(Shader.Replace(Shader.Replace(Shader.Replace(Shader.Replace(op.pr, 'pr', 'res#[Ind].x'), 'px', 'result.x'), 'py', 'result.y'), 'pz', 'result.z'), 'pw', 'result.w') + ','
  952. + Shader.Replace(Shader.Replace(Shader.Replace(Shader.Replace(Shader.Replace(op.pr, 'pr', 'res#[Ind].y'), 'px', 'result.x'), 'py', 'result.y'), 'pz', 'result.z'), 'pw', 'result.w') + ',' +
  953. Shader.Replace(Shader.Replace(Shader.Replace(Shader.Replace(Shader.Replace(op.pr, 'pr', 'res#[Ind].z'), 'px', 'result.x'), 'py', 'result.y'), 'pz', 'result.z'), 'pw', 'result.w')
  954. + ',' +
  955. Shader.Replace(Shader.Replace(Shader.Replace(Shader.Replace(Shader.Replace(op.pr, 'pr', 'res#[Ind].w'), 'px', 'result.x'), 'py', 'result.y'), 'pz', 'result.z'), 'pw', 'result.w')
  956. + ');' : ' res#[Ind]*1.0;'),
  957. 'result = res#[Ind] ;'
  958. ].join('\n\
  959. ');
  960. sresult = Shader.Replace(sresult, '#[Ind]', "_" + Shader.Indexer + "_");
  961. this.Body = Shader.Def(this.Body, "");
  962. this.Body += sresult;
  963. return this;
  964. };
  965. return ShaderBuilder;
  966. })();
  967. BABYLON.ShaderBuilder = ShaderBuilder;
  968. })(BABYLON || (BABYLON = {}));
  969. //# sourceMappingURL=ShaderBuilder.js.map