index.html 14 KB

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