index.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>Shaders Library</title>
  5. <script src="dat.gui.min.js"></script>
  6. <script src="babylon.max.js"></script>
  7. <script src="../dist/babylon.simpleMaterial.js"></script>
  8. <script src="../dist/babylon.normalMaterial.js"></script>
  9. <script src="../dist/babylon.waterMaterial.js"></script>
  10. <script src="../dist/babylon.fireMaterial.js"></script>
  11. <script src="../dist/babylon.lavaMaterial.js"></script>
  12. <script src="../dist/babylon.terrainMaterial.js"></script>
  13. <style>
  14. html, body {
  15. width: 100%;
  16. height: 100%;
  17. padding: 0;
  18. margin: 0;
  19. overflow: hidden;
  20. }
  21. #renderCanvas {
  22. width: 100%;
  23. height: 100%;
  24. }
  25. #fps {
  26. position: absolute;
  27. background-color: black;
  28. border: 2px solid red;
  29. text-align: center;
  30. font-size: 16px;
  31. color: white;
  32. top: 15px;
  33. left: 10px;
  34. width: 60px;
  35. height: 20px;
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <div id="fps">0</div>
  41. <canvas id="renderCanvas"></canvas>
  42. <script>
  43. if (BABYLON.Engine.isSupported()) {
  44. var canvas = document.getElementById("renderCanvas");
  45. var engine = new BABYLON.Engine(canvas, true);
  46. var divFps = document.getElementById("fps");
  47. var scene = new BABYLON.Scene(engine);
  48. var camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 6, 50, BABYLON.Vector3.Zero(), scene);
  49. camera.attachControl(canvas, true);
  50. // Lights
  51. var hemisphericLight = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0), scene);
  52. var pointLight = new BABYLON.PointLight("point", new BABYLON.Vector3(20, 20, 10), scene);
  53. pointLight.setEnabled(false);
  54. var directionalLight = new BABYLON.DirectionalLight("directional", new BABYLON.Vector3(0,-1, 0), scene);
  55. directionalLight.setEnabled(false);
  56. var spotLight = new BABYLON.SpotLight("spot", new BABYLON.Vector3(0, -30, 0), new BABYLON.Vector3(0, 1, 0), 1.1, 1, scene);
  57. spotLight.setEnabled(false);
  58. // Create meshes
  59. var sphere = BABYLON.Mesh.CreateSphere("sphere", 32, 30.0, scene);
  60. var plane = BABYLON.MeshBuilder.CreateBox("plane", { width: 30, height: 1, depth:30 }, scene);
  61. plane.setEnabled(false);
  62. var ground = BABYLON.Mesh.CreateGround("ground", 512, 512, 32, scene, false);
  63. ground.scaling = new BABYLON.Vector3(0.1, 0.1, 0.1);
  64. ground.setEnabled(false);
  65. var knot = BABYLON.Mesh.CreateTorusKnot("knot", 10, 3, 128, 64, 2, 3, scene);
  66. knot.setEnabled(false);
  67. // Skybox
  68. var skybox = BABYLON.Mesh.CreateBox("skyBox", 1000.0, scene);
  69. var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
  70. skyboxMaterial.backFaceCulling = false;
  71. skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("textures/skybox/TropicalSunnyDay", scene);
  72. skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
  73. skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
  74. skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
  75. skyboxMaterial.disableLighting = true;
  76. skybox.material = skyboxMaterial;
  77. skybox.setEnabled(false);
  78. var currentMesh = sphere;
  79. // Rabbit
  80. var rabbit;
  81. BABYLON.SceneLoader.ImportMesh("Rabbit", "meshes/", "Rabbit.babylon", scene, function (newMeshes, particleSystems, skeletons) {
  82. rabbit = newMeshes[1];
  83. rabbit.setEnabled(false);
  84. rabbit.scaling = new BABYLON.Vector3(0.3, 0.3, 0.3);
  85. scene.beginAnimation(skeletons[0], 0, 100, true, 0.8);
  86. // Shadow caster
  87. var shadowCaster = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  88. shadowCaster.setEnabled(false);
  89. shadowCaster.position = new BABYLON.Vector3(0, 30, 0);
  90. var shadowCaster2 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  91. shadowCaster2.setEnabled(false);
  92. shadowCaster2.position = new BABYLON.Vector3(0, -30, 0);
  93. var shadowCaster3 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  94. shadowCaster3.setEnabled(false);
  95. shadowCaster3.position = new BABYLON.Vector3(20, 20, 10);
  96. var shadowGenerator = new BABYLON.ShadowGenerator(1024, directionalLight);
  97. shadowGenerator.getShadowMap().renderList.push(shadowCaster);
  98. shadowGenerator.usePoissonSampling = true;
  99. var shadowGenerator2 = new BABYLON.ShadowGenerator(1024, spotLight);
  100. shadowGenerator2.getShadowMap().renderList.push(shadowCaster2);
  101. shadowGenerator2.usePoissonSampling = true;
  102. var shadowGenerator3 = new BABYLON.ShadowGenerator(1024, pointLight);
  103. shadowGenerator3.getShadowMap().renderList.push(shadowCaster3);
  104. shadowGenerator3.usePoissonSampling = true;
  105. // Register a render loop to repeatedly render the scene
  106. engine.runRenderLoop(function () {
  107. scene.render();
  108. divFps.innerHTML = engine.getFps().toFixed() + " fps";
  109. shadowCaster.rotation.x += 0.01;
  110. shadowCaster.rotation.y += 0.01;
  111. shadowCaster2.rotation.x += 0.01;
  112. shadowCaster2.rotation.y += 0.01;
  113. shadowCaster3.rotation.x += 0.01;
  114. shadowCaster3.rotation.y += 0.01;
  115. });
  116. // Resize
  117. window.addEventListener("resize", function () {
  118. engine.resize();
  119. });
  120. // Fog
  121. scene.fogMode = BABYLON.Scene.FOGMODE_NONE;
  122. scene.fogDensity = 0.01;
  123. // Create shaders
  124. var std = new BABYLON.StandardMaterial("std", scene);
  125. std.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  126. std.diffuseTexture.uScale = 5;
  127. std.diffuseTexture.vScale = 5;
  128. // Lava
  129. var lava = new BABYLON.LavaMaterial("lava", scene);
  130. lava.diffuseTexture = new BABYLON.Texture("textures/lava/lavatile.jpg", scene);
  131. lava.diffuseTexture.uScale = 0.5;
  132. lava.diffuseTexture.vScale = 0.5;
  133. lava.noiseTexture = new BABYLON.Texture("textures/lava/cloud.png", scene);
  134. lava.fogColor = BABYLON.Color3.Black();
  135. lava.speed = 2.5;
  136. var simple = new BABYLON.SimpleMaterial("simple", scene);
  137. simple.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  138. simple.diffuseTexture.uScale = 5;
  139. simple.diffuseTexture.vScale = 5;
  140. var normal = new BABYLON.NormalMaterial("normal", scene);
  141. var water = new BABYLON.WaterMaterial("water", scene);
  142. water.backFaceCulling = false;
  143. water.enableRenderTargets(false);
  144. water.bumpTexture = new BABYLON.Texture("textures/waterbump.png", scene);
  145. water.windForce = -45;
  146. water.waveHeight = 1.3;
  147. water.windDirection = new BABYLON.Vector2(1, 1);
  148. water.addToRenderList(skybox);
  149. water.addToRenderList(shadowCaster);
  150. water.addToRenderList(shadowCaster2);
  151. water.addToRenderList(shadowCaster3);
  152. var fire = new BABYLON.FireMaterial("fire", scene);
  153. fire.diffuseTexture = new BABYLON.Texture("textures/fire/diffuse.png", scene);
  154. fire.distortionTexture = new BABYLON.Texture("textures/fire/distortion.png", scene);
  155. fire.opacityTexture = new BABYLON.Texture("textures/fire/opacity.png", scene);
  156. var terrain = new BABYLON.TerrainMaterial("terrain", scene);
  157. terrain.specularColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  158. terrain.specularPower = 64;
  159. terrain.mixTexture = new BABYLON.Texture("textures/mixMap.png", scene);
  160. terrain.diffuseTexture1 = new BABYLON.Texture("textures/grass.png", scene);
  161. terrain.diffuseTexture2 = new BABYLON.Texture("textures/rock.png", scene);
  162. terrain.diffuseTexture3 = new BABYLON.Texture("textures/floor.png", scene);
  163. terrain.bumpTexture1 = new BABYLON.Texture("textures/grassn.png", scene);
  164. terrain.bumpTexture2 = new BABYLON.Texture("textures/rockn.png", scene);
  165. terrain.bumpTexture3 = new BABYLON.Texture("textures/floor_bump.png", scene);
  166. terrain.diffuseTexture1.uScale = terrain.diffuseTexture1.vScale = 10;
  167. terrain.diffuseTexture2.uScale = terrain.diffuseTexture2.vScale = 10;
  168. terrain.diffuseTexture3.uScale = terrain.diffuseTexture3.vScale = 10;
  169. // Default to std
  170. var currentMaterial = std;
  171. sphere.material = std;
  172. sphere.receiveShadows = true;
  173. //UI
  174. var gui = new dat.GUI();
  175. var options = {
  176. material: "standard",
  177. mesh: "sphere",
  178. hemisphericLight: true,
  179. pointLight: false,
  180. directionalLight: false,
  181. castShadows: false,
  182. spotLight: false,
  183. fog: false,
  184. skybox: false
  185. }
  186. gui.add(options, 'material', ['standard', 'simple', 'water', 'fire', 'lava', 'normal', 'terrain']).onFinishChange(function () {
  187. water.enableRenderTargets(false);
  188. switch (options.material) {
  189. case "simple":
  190. currentMaterial = simple;
  191. break;
  192. case "water":
  193. currentMaterial = water;
  194. water.enableRenderTargets(true);
  195. skybox.setEnabled(true);
  196. break;
  197. case "fire":
  198. currentMaterial = fire;
  199. break;
  200. case "lava":
  201. currentMaterial = lava;
  202. break;
  203. case "normal":
  204. currentMaterial = normal;
  205. break;
  206. case "terrain":
  207. currentMaterial = terrain;
  208. break;
  209. default:
  210. currentMaterial = std;
  211. break;
  212. }
  213. currentMesh.material = currentMaterial;
  214. });
  215. gui.add(options, 'mesh', ['sphere', 'knot', 'plane', 'ground', 'rabbit']).onFinishChange(function () {
  216. currentMesh.setEnabled(false);
  217. switch (options.mesh) {
  218. case "sphere":
  219. currentMesh = sphere;
  220. break;
  221. case "knot":
  222. currentMesh = knot;
  223. break;
  224. case "plane":
  225. currentMesh = plane;
  226. break;
  227. case "ground":
  228. currentMesh = ground;
  229. break;
  230. case "rabbit":
  231. currentMesh = rabbit;
  232. break;
  233. }
  234. currentMesh.setEnabled(true);
  235. currentMesh.receiveShadows = true;
  236. currentMesh.material = currentMaterial;
  237. water.mesh = currentMesh;
  238. });
  239. var f1 = gui.addFolder('lights');
  240. f1.add(options, 'hemisphericLight').onChange(function () {
  241. hemisphericLight.setEnabled(options.hemisphericLight);
  242. });
  243. f1.add(options, 'pointLight').onChange(function () {
  244. pointLight.setEnabled(options.pointLight);
  245. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  246. });
  247. f1.add(options, 'spotLight').onChange(function () {
  248. spotLight.setEnabled(options.spotLight);
  249. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  250. });
  251. f1.add(options, 'directionalLight').onChange(function () {
  252. directionalLight.setEnabled(options.directionalLight);
  253. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  254. });
  255. f1.add(options, 'castShadows').onChange(function () {
  256. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  257. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  258. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  259. });
  260. gui.add(options, 'fog').onChange(function () {
  261. scene.fogMode = options.fog ? BABYLON.Scene.FOGMODE_EXP : BABYLON.Scene.FOGMODE_NONE;
  262. });
  263. gui.add(options, 'skybox').onChange(function() {
  264. skybox.setEnabled(options.skybox);
  265. });
  266. });
  267. }
  268. </script>
  269. </body>
  270. </html>