index.html 15 KB

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