index.html 14 KB

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