index.html 14 KB

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