index.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>Procedural textures Library</title>
  5. <script src="refs/dat.gui.min.js"></script>
  6. <script src="refs/babylon.max.js"></script>
  7. <script src="../dist/babylon.fireProceduralTexture.js"></script>
  8. <script src="../dist/babylon.woodProceduralTexture.js"></script>
  9. <script src="../dist/babylon.cloudProceduralTexture.js"></script>
  10. <script src="../dist/babylon.grassProceduralTexture.js"></script>
  11. <script src="../dist/babylon.roadProceduralTexture.js"></script>
  12. <script src="../dist/babylon.brickProceduralTexture.js"></script>
  13. <script src="../dist/babylon.marbleProceduralTexture.js"></script>
  14. <script src="../dist/babylon.starfieldProceduralTexture.js"></script>
  15. <script src="../dist/babylon.asciiArtPostProcess.js"></script>
  16. <style>
  17. html, body {
  18. width: 100%;
  19. height: 100%;
  20. padding: 0;
  21. margin: 0;
  22. overflow: hidden;
  23. }
  24. #renderCanvas {
  25. width: 100%;
  26. height: 100%;
  27. }
  28. #fps {
  29. position: absolute;
  30. background-color: black;
  31. border: 2px solid red;
  32. text-align: center;
  33. font-size: 16px;
  34. color: white;
  35. top: 15px;
  36. left: 10px;
  37. width: 60px;
  38. height: 20px;
  39. }
  40. </style>
  41. </head>
  42. <body>
  43. <div id="fps">0</div>
  44. <canvas id="renderCanvas"></canvas>
  45. <script src="index.js"></script>
  46. <script src="add/addCloudPT.js"></script>
  47. <script src="add/addFirePT.js"></script>
  48. <script src="add/addWoodPT.js"></script>
  49. <script src="add/addGrassPT.js"></script>
  50. <script src="add/addRoadPT.js"></script>
  51. <script src="add/addBrickPT.js"></script>
  52. <script src="add/addMarblePT.js"></script>
  53. <script src="add/addStarfieldPT.js"></script>
  54. <script src="add/addAsciiArtPP.js"></script>
  55. <script>
  56. if (BABYLON.Engine.isSupported()) {
  57. var canvas = document.getElementById("renderCanvas");
  58. var engine = new BABYLON.Engine(canvas, true);
  59. var divFps = document.getElementById("fps");
  60. var scene = new BABYLON.Scene(engine);
  61. var camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 6, 50, BABYLON.Vector3.Zero(), scene);
  62. camera.attachControl(canvas, true);
  63. scene.postProcessesEnabled = false;
  64. // Lights
  65. var hemisphericLight = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0), scene);
  66. var pointLight = new BABYLON.PointLight("point", new BABYLON.Vector3(20, 20, 10), scene);
  67. pointLight.setEnabled(false);
  68. var directionalLight = new BABYLON.DirectionalLight("directional", new BABYLON.Vector3(0,-1, 0), scene);
  69. directionalLight.setEnabled(false);
  70. var spotLight = new BABYLON.SpotLight("spot", new BABYLON.Vector3(0, -30, 0), new BABYLON.Vector3(0, 1, 0), 1.1, 1, scene);
  71. spotLight.setEnabled(false);
  72. // Create meshes
  73. var sphere = BABYLON.Mesh.CreateSphere("sphere", 32, 30.0, scene);
  74. var plane = BABYLON.MeshBuilder.CreateBox("plane", { width: 30, height: 1, depth:30 }, scene);
  75. plane.setEnabled(false);
  76. var ground = BABYLON.Mesh.CreateGround("ground", 512, 512, 32, scene, false);
  77. ground.scaling = new BABYLON.Vector3(0.1, 0.1, 0.1);
  78. ground.setEnabled(false);
  79. var knot = BABYLON.Mesh.CreateTorusKnot("knot", 10, 3, 128, 64, 2, 3, scene);
  80. knot.setEnabled(false);
  81. // Skybox
  82. var skybox = BABYLON.Mesh.CreateBox("skyBox", 1000.0, scene);
  83. var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
  84. skyboxMaterial.backFaceCulling = false;
  85. skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("textures/skybox/TropicalSunnyDay", scene);
  86. skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
  87. skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
  88. skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
  89. skyboxMaterial.disableLighting = true;
  90. skybox.material = skyboxMaterial;
  91. skybox.setEnabled(false);
  92. var currentMesh = sphere;
  93. // Rabbit
  94. var rabbit;
  95. BABYLON.SceneLoader.ImportMesh("Rabbit", "meshes/", "Rabbit.babylon", scene, function (newMeshes, particleSystems, skeletons) {
  96. rabbit = newMeshes[1];
  97. rabbit.setEnabled(false);
  98. rabbit.scaling = new BABYLON.Vector3(0.3, 0.3, 0.3);
  99. scene.beginAnimation(skeletons[0], 0, 100, true, 0.8);
  100. // Shadow caster
  101. var shadowCaster = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  102. shadowCaster.setEnabled(false);
  103. shadowCaster.position = new BABYLON.Vector3(0, 30, 0);
  104. var shadowCaster2 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  105. shadowCaster2.setEnabled(false);
  106. shadowCaster2.position = new BABYLON.Vector3(0, -30, 0);
  107. var shadowCaster3 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  108. shadowCaster3.setEnabled(false);
  109. shadowCaster3.position = new BABYLON.Vector3(20, 20, 10);
  110. var shadowGenerator = new BABYLON.ShadowGenerator(1024, directionalLight);
  111. shadowGenerator.getShadowMap().renderList.push(shadowCaster);
  112. shadowGenerator.usePoissonSampling = true;
  113. var shadowGenerator2 = new BABYLON.ShadowGenerator(1024, spotLight);
  114. shadowGenerator2.getShadowMap().renderList.push(shadowCaster2);
  115. shadowGenerator2.usePoissonSampling = true;
  116. var shadowGenerator3 = new BABYLON.ShadowGenerator(1024, pointLight);
  117. shadowGenerator3.getShadowMap().renderList.push(shadowCaster3);
  118. shadowGenerator3.usePoissonSampling = true;
  119. // Register a render loop to repeatedly render the scene
  120. engine.runRenderLoop(function () {
  121. scene.render();
  122. divFps.innerHTML = engine.getFps().toFixed() + " fps";
  123. shadowCaster.rotation.x += 0.01;
  124. shadowCaster.rotation.y += 0.01;
  125. shadowCaster2.rotation.x += 0.01;
  126. shadowCaster2.rotation.y += 0.01;
  127. shadowCaster3.rotation.x += 0.01;
  128. shadowCaster3.rotation.y += 0.01;
  129. });
  130. // Resize
  131. window.addEventListener("resize", function () {
  132. engine.resize();
  133. });
  134. // Fog
  135. scene.fogMode = BABYLON.Scene.FOGMODE_NONE;
  136. scene.fogDensity = 0.01;
  137. // Create shaders
  138. var std = new BABYLON.StandardMaterial("std", scene);
  139. std.specularColor = new BABYLON.Color3(0, 0, 0);
  140. var diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  141. diffuseTexture.uScale = 5;
  142. diffuseTexture.vScale = 5;
  143. // Fire Procedural Texture
  144. var firePT = addFirePT();
  145. // Wood Procedural Texture
  146. var woodPT = addWoodPT();
  147. // Cloud Procedural Texture
  148. var cloudPT = addCloudPT();
  149. // Grass Procedural Texture
  150. var grassPT = addGrassPT();
  151. // Road Procedural Texture
  152. var roadPT = addRoadPT();
  153. // Brick Procedural Texture
  154. var brickPT = addBrickPT();
  155. // Marble Procedural Texture
  156. var marblePT = addMarblePT();
  157. // Starfield Procedural Texture
  158. var starfieldPT = addStarfieldPT();
  159. // Ascii Art
  160. var asciiPostProcess = addAsciiArtPP(camera);
  161. // Default to std
  162. var currentTexture = diffuseTexture;
  163. sphere.material = std;
  164. sphere.receiveShadows = true;
  165. std.diffuseTexture = currentTexture;
  166. //placeholder for current texture options
  167. var currentPTOptions = gui.addFolder('Texture Options');
  168. var PTOptions = [];
  169. dat.GUI.prototype.removeFolder = function(name) {
  170. var folder = this.__folders[name];
  171. if (!folder) {
  172. return;
  173. }
  174. folder.close();
  175. this.__ul.removeChild(folder.domElement.parentNode);
  176. delete this.__folders[name];
  177. this.onResize();
  178. }
  179. var resetPTOptions = function(){
  180. scene.postProcessesEnabled = false;
  181. //empty options
  182. while(PTOptions.length > 0){
  183. var option = PTOptions.pop()
  184. if(option.optionType === "Folder"){
  185. currentPTOptions.removeFolder(option.optionValue);
  186. }
  187. else {
  188. currentPTOptions.remove(option.optionValue);
  189. }
  190. }
  191. }
  192. var addFloatSubOption = function(guiElement, field, propertyName, proxy, initvalue){
  193. proxy[propertyName] = initvalue;
  194. guiElement.add(proxy, propertyName).onChange(function () {
  195. try {
  196. var xvalue = parseFloat(proxy[propertyName]);
  197. eval('field.' + propertyName + ' = xvalue');
  198. } catch (e) {
  199. }
  200. });
  201. }
  202. var addPToptions = function(pt, fieldNames){
  203. for(var fieldid = 0; fieldid < fieldNames.length; fieldid++){
  204. var field = fieldNames[fieldid]
  205. var added = {};
  206. if(typeof pt[field] != 'object' && pt[field] != 'undefined') {
  207. added.optionType = "Value";
  208. added.optionValue = currentPTOptions.add(pt, fieldNames[fieldid]);
  209. }
  210. else {
  211. var proxy = {};
  212. if(pt[field] instanceof BABYLON.Vector2){
  213. added.optionType = 'Folder';
  214. added.optionValue = field + ' ('+ pt[field].constructor.name +')';
  215. var folder = currentPTOptions.addFolder(added.optionValue);
  216. addFloatSubOption(folder, pt[field], 'x', proxy, pt[field].x);
  217. addFloatSubOption(folder, pt[field], 'y', proxy, pt[field].y);
  218. }
  219. else if(pt[field] instanceof BABYLON.Vector3){
  220. added.optionType = 'Folder';
  221. added.optionValue = field + ' ('+ pt[field].constructor.name +')';
  222. var folder = currentPTOptions.addFolder(added.optionValue);
  223. addFloatSubOption(folder, pt[field], 'x', proxy, pt[field].x);
  224. addFloatSubOption(folder, pt[field], 'y', proxy, pt[field].y);
  225. addFloatSubOption(folder, pt[field], 'z', proxy, pt[field].z);
  226. }
  227. else if(pt[field] instanceof BABYLON.Vector4){
  228. added.optionType = 'Folder';
  229. added.optionValue = field + ' ('+ pt[field].constructor.name +')';
  230. var folder = currentPTOptions.addFolder(added.optionValue);
  231. addFloatSubOption(folder, pt[field], 'x', proxy, pt[field].x);
  232. addFloatSubOption(folder, pt[field], 'y', proxy, pt[field].y);
  233. addFloatSubOption(folder, pt[field], 'z', proxy, pt[field].z);
  234. addFloatSubOption(folder, pt[field], 'w', proxy, pt[field].w);
  235. }
  236. else if(pt[field] instanceof BABYLON.Color3){
  237. added.optionType = 'Folder';
  238. added.optionValue = field + ' ('+ pt[field].constructor.name +')';
  239. var folder = currentPTOptions.addFolder(added.optionValue);
  240. addFloatSubOption(folder, pt[field], 'r', proxy, pt[field].r);
  241. addFloatSubOption(folder, pt[field], 'g', proxy, pt[field].g);
  242. addFloatSubOption(folder, pt[field], 'b', proxy, pt[field].b);
  243. }
  244. else if(pt[field] instanceof BABYLON.Color4){
  245. added.optionType = 'Folder';
  246. added.optionValue = field + ' ('+ pt[field].constructor.name +')';
  247. var folder = currentPTOptions.addFolder(added.optionValue);
  248. addFloatSubOption(folder, pt[field], 'r', proxy, pt[field].r);
  249. addFloatSubOption(folder, pt[field], 'g', proxy, pt[field].g);
  250. addFloatSubOption(folder, pt[field], 'b', proxy, pt[field].b);
  251. addFloatSubOption(folder, pt[field], 'a', proxy, pt[field].a);
  252. }
  253. else {
  254. proxy[field] = 'Object (' + pt[field].constructor.name +')';
  255. added.optionType = "Value";
  256. added.optionValue = currentPTOptions.add(proxy, field).onChange(function () {
  257. try {
  258. var res = eval(proxy[field]);
  259. pt[field] = res;
  260. } catch (e) { }
  261. });
  262. }
  263. }
  264. PTOptions.push(added);
  265. }
  266. }
  267. gui.add(options, 'texture', ['default', 'fire', 'wood', 'cloud', 'grass', 'road', 'brick', 'marble', 'starfield']).onFinishChange(function () {
  268. resetPTOptions();
  269. switch (options.texture) {
  270. case "fire":
  271. currentTexture = firePT;
  272. addPToptions(firePT, ['time', 'alphaThreshold', 'speed', ]);
  273. break;
  274. case "wood":
  275. currentTexture = woodPT;
  276. addPToptions(woodPT, ['ampScale', 'woodColor']);
  277. break;
  278. case "cloud":
  279. currentTexture = cloudPT;
  280. addPToptions(cloudPT, ['skyColor', 'cloudColor']);
  281. break;
  282. case "grass":
  283. currentTexture = grassPT;
  284. addPToptions(grassPT, ['groundColor']);
  285. break;
  286. case "road":
  287. currentTexture = roadPT;
  288. addPToptions(roadPT, ['roadColor']);
  289. break;
  290. case "brick":
  291. currentTexture = brickPT;
  292. addPToptions(brickPT, ['numberOfBricksHeight', 'numberOfBricksWidth', 'brickColor', 'jointColor']);
  293. break;
  294. case "marble":
  295. currentTexture = marblePT;
  296. addPToptions(marblePT, ['numberOfTilesHeight', 'numberOfTilesWidth', 'amplitude', 'jointColor']);
  297. break;
  298. case "starfield":
  299. currentTexture = starfieldPT;
  300. addPToptions(starfieldPT, ['saturation', 'distfading', 'darkmatter', 'alpha', 'time', 'beta', 'zoom', 'formuparam', 'stepsize', 'tile', 'brightness']);
  301. break;
  302. case "none":
  303. default:
  304. currentTexture = diffuseTexture;
  305. break;
  306. }
  307. std.diffuseTexture = currentTexture;
  308. window.enableTexture(options.texture);
  309. });
  310. gui.add(options, 'mesh', ['sphere', 'knot', 'plane', 'ground', 'rabbit']).onFinishChange(function () {
  311. currentMesh.setEnabled(false);
  312. switch (options.mesh) {
  313. case "sphere":
  314. currentMesh = sphere;
  315. break;
  316. case "knot":
  317. currentMesh = knot;
  318. break;
  319. case "plane":
  320. currentMesh = plane;
  321. break;
  322. case "ground":
  323. currentMesh = ground;
  324. break;
  325. case "rabbit":
  326. currentMesh = rabbit;
  327. break;
  328. }
  329. currentMesh.setEnabled(true);
  330. currentMesh.receiveShadows = true;
  331. currentMesh.material = std;
  332. });
  333. var f1 = gui.addFolder('lights');
  334. f1.add(options, 'hemisphericLight').onChange(function () {
  335. hemisphericLight.setEnabled(options.hemisphericLight);
  336. });
  337. f1.add(options, 'pointLight').onChange(function () {
  338. pointLight.setEnabled(options.pointLight);
  339. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  340. });
  341. f1.add(options, 'spotLight').onChange(function () {
  342. spotLight.setEnabled(options.spotLight);
  343. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  344. });
  345. f1.add(options, 'directionalLight').onChange(function () {
  346. directionalLight.setEnabled(options.directionalLight);
  347. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  348. });
  349. f1.add(options, 'castShadows').onChange(function () {
  350. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  351. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  352. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  353. });
  354. gui.add(options, 'fog').onChange(function () {
  355. scene.fogMode = options.fog ? BABYLON.Scene.FOGMODE_EXP : BABYLON.Scene.FOGMODE_NONE;
  356. });
  357. gui.add(options, 'skybox').onChange(function() {
  358. skybox.setEnabled(options.skybox);
  359. });
  360. gui.add(options, 'ascii').onChange(function() {
  361. scene.postProcessesEnabled = !scene.postProcessesEnabled;
  362. });
  363. });
  364. }
  365. </script>
  366. </body>
  367. </html>