index.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  137. std.diffuseTexture.uScale = 5;
  138. std.diffuseTexture.vScale = 5;
  139. // Fire Procedural Texture
  140. var firePT = addFirePT();
  141. var fireMaterial = new BABYLON.StandardMaterial("fire", scene);
  142. fireMaterial.diffuseTexture = firePT;
  143. // Wood Procedural Texture
  144. var woodPT = addWoodPT();
  145. var woodMaterial = new BABYLON.StandardMaterial("wood", scene);
  146. woodMaterial.diffuseTexture = woodPT;
  147. // Cloud Procedural Texture
  148. var cloudPT = addCloudPT();
  149. var cloudMaterial = new BABYLON.StandardMaterial("cloud", scene);
  150. cloudMaterial.diffuseTexture = cloudPT;
  151. // Grass Procedural Texture
  152. var grassPT = addGrassPT();
  153. var grassMaterial = new BABYLON.StandardMaterial("grass", scene);
  154. grassMaterial.diffuseTexture = grassPT;
  155. // Road Procedural Texture
  156. var roadPT = addRoadPT();
  157. var roadMaterial = new BABYLON.StandardMaterial("road", scene);
  158. roadMaterial.diffuseTexture = roadPT;
  159. // Brick Procedural Texture
  160. var brickPT = addBrickPT();
  161. var brickMaterial = new BABYLON.StandardMaterial("brick", scene);
  162. brickMaterial.diffuseTexture = brickPT;
  163. // Marble Procedural Texture
  164. var marblePT = addMarblePT();
  165. var marbleMaterial = new BABYLON.StandardMaterial("marble", scene);
  166. marbleMaterial.diffuseTexture = marblePT;
  167. // Starfield Procedural Texture
  168. var starfieldPT = addStarfieldPT();
  169. var starfieldMaterial = new BABYLON.StandardMaterial("starfield", scene);
  170. starfieldMaterial.diffuseTexture = starfieldPT;
  171. // Default to std
  172. var currentMaterial = std;
  173. sphere.material = std;
  174. sphere.receiveShadows = true;
  175. gui.add(options, 'material', ['none', 'fire', 'wood', 'cloud', 'grass', 'road', 'brick', 'marble', 'starfield']).onFinishChange(function () {
  176. switch (options.material) {
  177. case "fire":
  178. currentMaterial = fireMaterial;
  179. break;
  180. case "wood":
  181. currentMaterial = woodMaterial;
  182. break;
  183. case "cloud":
  184. currentMaterial = cloudMaterial;
  185. break;
  186. case "grass":
  187. currentMaterial = grassMaterial;
  188. break;
  189. case "road":
  190. currentMaterial = roadMaterial;
  191. break;
  192. case "brick":
  193. currentMaterial = brickMaterial;
  194. break;
  195. case "marble":
  196. currentMaterial = marbleMaterial;
  197. break;
  198. case "starfield":
  199. currentMaterial = starfieldMaterial;
  200. break;
  201. case "none":
  202. default:
  203. currentMaterial = std;
  204. break;
  205. }
  206. currentMesh.material = currentMaterial;
  207. window.enableMaterial(options.material);
  208. });
  209. gui.add(options, 'mesh', ['sphere', 'knot', 'plane', 'ground', 'rabbit']).onFinishChange(function () {
  210. currentMesh.setEnabled(false);
  211. switch (options.mesh) {
  212. case "sphere":
  213. currentMesh = sphere;
  214. break;
  215. case "knot":
  216. currentMesh = knot;
  217. break;
  218. case "plane":
  219. currentMesh = plane;
  220. break;
  221. case "ground":
  222. currentMesh = ground;
  223. break;
  224. case "rabbit":
  225. currentMesh = rabbit;
  226. break;
  227. }
  228. currentMesh.setEnabled(true);
  229. currentMesh.receiveShadows = true;
  230. currentMesh.material = currentMaterial;
  231. });
  232. var f1 = gui.addFolder('lights');
  233. f1.add(options, 'hemisphericLight').onChange(function () {
  234. hemisphericLight.setEnabled(options.hemisphericLight);
  235. });
  236. f1.add(options, 'pointLight').onChange(function () {
  237. pointLight.setEnabled(options.pointLight);
  238. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  239. });
  240. f1.add(options, 'spotLight').onChange(function () {
  241. spotLight.setEnabled(options.spotLight);
  242. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  243. });
  244. f1.add(options, 'directionalLight').onChange(function () {
  245. directionalLight.setEnabled(options.directionalLight);
  246. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  247. });
  248. f1.add(options, 'castShadows').onChange(function () {
  249. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  250. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  251. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  252. });
  253. gui.add(options, 'fog').onChange(function () {
  254. scene.fogMode = options.fog ? BABYLON.Scene.FOGMODE_EXP : BABYLON.Scene.FOGMODE_NONE;
  255. });
  256. gui.add(options, 'skybox').onChange(function() {
  257. skybox.setEnabled(options.skybox);
  258. });
  259. });
  260. }
  261. </script>
  262. </body>
  263. </html>