index.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. <style>
  15. html, body {
  16. width: 100%;
  17. height: 100%;
  18. padding: 0;
  19. margin: 0;
  20. overflow: hidden;
  21. }
  22. #renderCanvas {
  23. width: 100%;
  24. height: 100%;
  25. }
  26. #fps {
  27. position: absolute;
  28. background-color: black;
  29. border: 2px solid red;
  30. text-align: center;
  31. font-size: 16px;
  32. color: white;
  33. top: 15px;
  34. left: 10px;
  35. width: 60px;
  36. height: 20px;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <div id="fps">0</div>
  42. <canvas id="renderCanvas"></canvas>
  43. <script src="index.js"></script>
  44. <script src="add/addCloudPT.js"></script>
  45. <script src="add/addFirePT.js"></script>
  46. <script src="add/addWoodPT.js"></script>
  47. <script src="add/addGrassPT.js"></script>
  48. <script src="add/addRoadPT.js"></script>
  49. <script src="add/addBrickPT.js"></script>
  50. <script src="add/addMarblePT.js"></script>
  51. <script>
  52. if (BABYLON.Engine.isSupported()) {
  53. var canvas = document.getElementById("renderCanvas");
  54. var engine = new BABYLON.Engine(canvas, true);
  55. var divFps = document.getElementById("fps");
  56. var scene = new BABYLON.Scene(engine);
  57. var camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 6, 50, BABYLON.Vector3.Zero(), scene);
  58. camera.attachControl(canvas, true);
  59. // Lights
  60. var hemisphericLight = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0), scene);
  61. var pointLight = new BABYLON.PointLight("point", new BABYLON.Vector3(20, 20, 10), scene);
  62. pointLight.setEnabled(false);
  63. var directionalLight = new BABYLON.DirectionalLight("directional", new BABYLON.Vector3(0,-1, 0), scene);
  64. directionalLight.setEnabled(false);
  65. var spotLight = new BABYLON.SpotLight("spot", new BABYLON.Vector3(0, -30, 0), new BABYLON.Vector3(0, 1, 0), 1.1, 1, scene);
  66. spotLight.setEnabled(false);
  67. // Create meshes
  68. var sphere = BABYLON.Mesh.CreateSphere("sphere", 32, 30.0, scene);
  69. var plane = BABYLON.MeshBuilder.CreateBox("plane", { width: 30, height: 1, depth:30 }, scene);
  70. plane.setEnabled(false);
  71. var ground = BABYLON.Mesh.CreateGround("ground", 512, 512, 32, scene, false);
  72. ground.scaling = new BABYLON.Vector3(0.1, 0.1, 0.1);
  73. ground.setEnabled(false);
  74. var knot = BABYLON.Mesh.CreateTorusKnot("knot", 10, 3, 128, 64, 2, 3, scene);
  75. knot.setEnabled(false);
  76. // Skybox
  77. var skybox = BABYLON.Mesh.CreateBox("skyBox", 1000.0, scene);
  78. var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
  79. skyboxMaterial.backFaceCulling = false;
  80. skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("textures/skybox/TropicalSunnyDay", scene);
  81. skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
  82. skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
  83. skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
  84. skyboxMaterial.disableLighting = true;
  85. skybox.material = skyboxMaterial;
  86. skybox.setEnabled(false);
  87. var currentMesh = sphere;
  88. // Rabbit
  89. var rabbit;
  90. BABYLON.SceneLoader.ImportMesh("Rabbit", "meshes/", "Rabbit.babylon", scene, function (newMeshes, particleSystems, skeletons) {
  91. rabbit = newMeshes[1];
  92. rabbit.setEnabled(false);
  93. rabbit.scaling = new BABYLON.Vector3(0.3, 0.3, 0.3);
  94. scene.beginAnimation(skeletons[0], 0, 100, true, 0.8);
  95. // Shadow caster
  96. var shadowCaster = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  97. shadowCaster.setEnabled(false);
  98. shadowCaster.position = new BABYLON.Vector3(0, 30, 0);
  99. var shadowCaster2 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  100. shadowCaster2.setEnabled(false);
  101. shadowCaster2.position = new BABYLON.Vector3(0, -30, 0);
  102. var shadowCaster3 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  103. shadowCaster3.setEnabled(false);
  104. shadowCaster3.position = new BABYLON.Vector3(20, 20, 10);
  105. var shadowGenerator = new BABYLON.ShadowGenerator(1024, directionalLight);
  106. shadowGenerator.getShadowMap().renderList.push(shadowCaster);
  107. shadowGenerator.usePoissonSampling = true;
  108. var shadowGenerator2 = new BABYLON.ShadowGenerator(1024, spotLight);
  109. shadowGenerator2.getShadowMap().renderList.push(shadowCaster2);
  110. shadowGenerator2.usePoissonSampling = true;
  111. var shadowGenerator3 = new BABYLON.ShadowGenerator(1024, pointLight);
  112. shadowGenerator3.getShadowMap().renderList.push(shadowCaster3);
  113. shadowGenerator3.usePoissonSampling = true;
  114. // Register a render loop to repeatedly render the scene
  115. engine.runRenderLoop(function () {
  116. scene.render();
  117. divFps.innerHTML = engine.getFps().toFixed() + " fps";
  118. shadowCaster.rotation.x += 0.01;
  119. shadowCaster.rotation.y += 0.01;
  120. shadowCaster2.rotation.x += 0.01;
  121. shadowCaster2.rotation.y += 0.01;
  122. shadowCaster3.rotation.x += 0.01;
  123. shadowCaster3.rotation.y += 0.01;
  124. });
  125. // Resize
  126. window.addEventListener("resize", function () {
  127. engine.resize();
  128. });
  129. // Fog
  130. scene.fogMode = BABYLON.Scene.FOGMODE_NONE;
  131. scene.fogDensity = 0.01;
  132. // Create shaders
  133. var std = new BABYLON.StandardMaterial("std", scene);
  134. std.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  135. std.diffuseTexture.uScale = 5;
  136. std.diffuseTexture.vScale = 5;
  137. // Fire Procedural Texture
  138. var firePT = addFirePT();
  139. var fireMaterial = new BABYLON.StandardMaterial("fire", scene);
  140. fireMaterial.diffuseTexture = firePT;
  141. // Wood Procedural Texture
  142. var woodPT = addWoodPT();
  143. var woodMaterial = new BABYLON.StandardMaterial("wood", scene);
  144. woodMaterial.diffuseTexture = woodPT;
  145. // Cloud Procedural Texture
  146. var cloudPT = addCloudPT();
  147. var cloudMaterial = new BABYLON.StandardMaterial("cloud", scene);
  148. cloudMaterial.diffuseTexture = cloudPT;
  149. // Grass Procedural Texture
  150. var grassPT = addGrassPT();
  151. var grassMaterial = new BABYLON.StandardMaterial("grass", scene);
  152. grassMaterial.diffuseTexture = grassPT;
  153. // Road Procedural Texture
  154. var roadPT = addRoadPT();
  155. var roadMaterial = new BABYLON.StandardMaterial("road", scene);
  156. roadMaterial.diffuseTexture = roadPT;
  157. // Brick Procedural Texture
  158. var brickPT = addBrickPT();
  159. var brickMaterial = new BABYLON.StandardMaterial("brick", scene);
  160. brickMaterial.diffuseTexture = brickPT;
  161. // Marble Procedural Texture
  162. var marblePT = addMarblePT();
  163. var marbleMaterial = new BABYLON.StandardMaterial("marble", scene);
  164. marbleMaterial.diffuseTexture = marblePT;
  165. // Default to std
  166. var currentMaterial = std;
  167. sphere.material = std;
  168. sphere.receiveShadows = true;
  169. gui.add(options, 'material', ['none', 'fire', 'wood', 'cloud', 'grass', 'road', 'brick', 'marble']).onFinishChange(function () {
  170. switch (options.material) {
  171. case "fire":
  172. currentMaterial = fireMaterial;
  173. break;
  174. case "wood":
  175. currentMaterial = woodMaterial;
  176. break;
  177. case "cloud":
  178. currentMaterial = cloudMaterial;
  179. break;
  180. case "grass":
  181. currentMaterial = grassMaterial;
  182. break;
  183. case "road":
  184. currentMaterial = roadMaterial;
  185. break;
  186. case "brick":
  187. currentMaterial = brickMaterial;
  188. break;
  189. case "marble":
  190. currentMaterial = marbleMaterial;
  191. break;
  192. case "none":
  193. default:
  194. currentMaterial = std;
  195. break;
  196. }
  197. currentMesh.material = currentMaterial;
  198. window.enableMaterial(options.material);
  199. });
  200. gui.add(options, 'mesh', ['sphere', 'knot', 'plane', 'ground', 'rabbit']).onFinishChange(function () {
  201. currentMesh.setEnabled(false);
  202. switch (options.mesh) {
  203. case "sphere":
  204. currentMesh = sphere;
  205. break;
  206. case "knot":
  207. currentMesh = knot;
  208. break;
  209. case "plane":
  210. currentMesh = plane;
  211. break;
  212. case "ground":
  213. currentMesh = ground;
  214. break;
  215. case "rabbit":
  216. currentMesh = rabbit;
  217. break;
  218. }
  219. currentMesh.setEnabled(true);
  220. currentMesh.receiveShadows = true;
  221. currentMesh.material = currentMaterial;
  222. water.mesh = currentMesh;
  223. });
  224. var f1 = gui.addFolder('lights');
  225. f1.add(options, 'hemisphericLight').onChange(function () {
  226. hemisphericLight.setEnabled(options.hemisphericLight);
  227. });
  228. f1.add(options, 'pointLight').onChange(function () {
  229. pointLight.setEnabled(options.pointLight);
  230. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  231. });
  232. f1.add(options, 'spotLight').onChange(function () {
  233. spotLight.setEnabled(options.spotLight);
  234. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  235. });
  236. f1.add(options, 'directionalLight').onChange(function () {
  237. directionalLight.setEnabled(options.directionalLight);
  238. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  239. });
  240. f1.add(options, 'castShadows').onChange(function () {
  241. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  242. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  243. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  244. });
  245. gui.add(options, 'fog').onChange(function () {
  246. scene.fogMode = options.fog ? BABYLON.Scene.FOGMODE_EXP : BABYLON.Scene.FOGMODE_NONE;
  247. });
  248. gui.add(options, 'skybox').onChange(function() {
  249. skybox.setEnabled(options.skybox);
  250. });
  251. });
  252. }
  253. </script>
  254. </body>
  255. </html>