index.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. gui.add(options, 'texture', ['default', 'fire', 'wood', 'cloud', 'grass', 'road', 'brick', 'marble', 'starfield']).onFinishChange(function () {
  162. switch (options.texture) {
  163. case "fire":
  164. currentTexture = firePT;
  165. break;
  166. case "wood":
  167. currentTexture = woodPT;
  168. break;
  169. case "cloud":
  170. currentTexture = cloudPT;
  171. break;
  172. case "grass":
  173. currentTexture = grassPT;
  174. break;
  175. case "road":
  176. currentTexture = roadPT;
  177. break;
  178. case "brick":
  179. currentTexture = brickPT;
  180. break;
  181. case "marble":
  182. currentTexture = marblePT;
  183. break;
  184. case "starfield":
  185. currentTexture = starfieldPT;
  186. break;
  187. case "none":
  188. default:
  189. currentTexture = diffuseTexture;
  190. break;
  191. }
  192. std.diffuseTexture = currentTexture;
  193. window.enableTexture(options.texture);
  194. });
  195. gui.add(options, 'mesh', ['sphere', 'knot', 'plane', 'ground', 'rabbit']).onFinishChange(function () {
  196. currentMesh.setEnabled(false);
  197. switch (options.mesh) {
  198. case "sphere":
  199. currentMesh = sphere;
  200. break;
  201. case "knot":
  202. currentMesh = knot;
  203. break;
  204. case "plane":
  205. currentMesh = plane;
  206. break;
  207. case "ground":
  208. currentMesh = ground;
  209. break;
  210. case "rabbit":
  211. currentMesh = rabbit;
  212. break;
  213. }
  214. currentMesh.setEnabled(true);
  215. currentMesh.receiveShadows = true;
  216. currentMesh.material = std;
  217. });
  218. var f1 = gui.addFolder('lights');
  219. f1.add(options, 'hemisphericLight').onChange(function () {
  220. hemisphericLight.setEnabled(options.hemisphericLight);
  221. });
  222. f1.add(options, 'pointLight').onChange(function () {
  223. pointLight.setEnabled(options.pointLight);
  224. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  225. });
  226. f1.add(options, 'spotLight').onChange(function () {
  227. spotLight.setEnabled(options.spotLight);
  228. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  229. });
  230. f1.add(options, 'directionalLight').onChange(function () {
  231. directionalLight.setEnabled(options.directionalLight);
  232. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  233. });
  234. f1.add(options, 'castShadows').onChange(function () {
  235. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  236. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  237. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  238. });
  239. gui.add(options, 'fog').onChange(function () {
  240. scene.fogMode = options.fog ? BABYLON.Scene.FOGMODE_EXP : BABYLON.Scene.FOGMODE_NONE;
  241. });
  242. gui.add(options, 'skybox').onChange(function() {
  243. skybox.setEnabled(options.skybox);
  244. });
  245. });
  246. }
  247. </script>
  248. </body>
  249. </html>