index.html 16 KB

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