index.html 14 KB

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