index.html 8.1 KB

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