index.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>Shaders Library</title>
  5. <script src="refs/dat.gui.min.js"></script>
  6. <script src="refs/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. <script src="../dist/babylon.pbrMaterial.js"></script>
  14. <script src="../dist/babylon.furMaterial.js"></script>
  15. <script src="../dist/babylon.triPlanarMaterial.js"></script>
  16. <script src="../dist/babylon.gradientMaterial.js"></script>
  17. <script src="../dist/babylon.skyMaterial.js"></script>
  18. <script src="../dist/babylon.gridMaterial.js"></script>
  19. <style>
  20. html, body {
  21. width: 100%;
  22. height: 100%;
  23. padding: 0;
  24. margin: 0;
  25. overflow: hidden;
  26. }
  27. #renderCanvas {
  28. width: 100%;
  29. height: 100%;
  30. }
  31. #fps {
  32. position: absolute;
  33. background-color: black;
  34. border: 2px solid red;
  35. text-align: center;
  36. font-size: 16px;
  37. color: white;
  38. top: 15px;
  39. left: 10px;
  40. width: 60px;
  41. height: 20px;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <div id="fps">0</div>
  47. <canvas id="renderCanvas"></canvas>
  48. <script src="index.js"></script>
  49. <script src="add/addpbr.js"></script>
  50. <script src="add/addlava.js"></script>
  51. <script src="add/addnormal.js"></script>
  52. <script src="add/addwater.js"></script>
  53. <script src="add/addfur.js"></script>
  54. <script src="add/addterrain.js"></script>
  55. <script src="add/addfire.js"></script>
  56. <script src="add/addtriplanar.js"></script>
  57. <script src="add/addgradient.js"></script>
  58. <script src="add/addsky.js"></script>
  59. <script src="add/addgrid.js"></script>
  60. <script>
  61. if (BABYLON.Engine.isSupported()) {
  62. var canvas = document.getElementById("renderCanvas");
  63. var engine = new BABYLON.Engine(canvas, true);
  64. var divFps = document.getElementById("fps");
  65. var scene = new BABYLON.Scene(engine);
  66. var camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 6, 50, BABYLON.Vector3.Zero(), scene);
  67. camera.attachControl(canvas, true);
  68. camera.minZ = 0.1;
  69. // Lights
  70. var hemisphericLight = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0), scene);
  71. var pointLight = new BABYLON.PointLight("point", new BABYLON.Vector3(20, 20, 10), scene);
  72. pointLight.setEnabled(false);
  73. var directionalLight = new BABYLON.DirectionalLight("directional", new BABYLON.Vector3(0,-1, 0), scene);
  74. directionalLight.setEnabled(false);
  75. var spotLight = new BABYLON.SpotLight("spot", new BABYLON.Vector3(0, -30, 0), new BABYLON.Vector3(0, 1, 0), 1.1, 1, scene);
  76. spotLight.setEnabled(false);
  77. // Create meshes
  78. var sphere = BABYLON.Mesh.CreateSphere("sphere", 48, 30.0, scene);
  79. var plane = BABYLON.MeshBuilder.CreateBox("plane", { width: 30, height: 1, depth:30 }, scene);
  80. plane.setEnabled(false);
  81. var ground = BABYLON.Mesh.CreateGround("ground", 512, 512, 32, scene, false);
  82. ground.scaling = new BABYLON.Vector3(0.1, 0.1, 0.1);
  83. ground.setEnabled(false);
  84. var knot = BABYLON.Mesh.CreateTorusKnot("knot", 10, 3, 128, 64, 2, 3, scene);
  85. knot.setEnabled(false);
  86. var heightMap = BABYLON.Mesh.CreateGroundFromHeightMap("heightMap", "textures/heightMap.png", 100, 100, 100, 0, 10, scene, false);
  87. heightMap.setEnabled(false);
  88. // Skybox
  89. var skybox = BABYLON.Mesh.CreateBox("skyBox", 1000.0, scene);
  90. var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
  91. skyboxMaterial.backFaceCulling = false;
  92. skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("textures/skybox/TropicalSunnyDay", scene);
  93. skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
  94. skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
  95. skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
  96. skyboxMaterial.disableLighting = true;
  97. skybox.material = skyboxMaterial;
  98. skybox.setEnabled(false);
  99. var currentMesh = sphere;
  100. // Rabbit
  101. var rabbit;
  102. BABYLON.SceneLoader.ImportMesh("Rabbit", "meshes/", "Rabbit.babylon", scene, function (newMeshes, particleSystems, skeletons) {
  103. rabbit = newMeshes[1];
  104. rabbit.setEnabled(false);
  105. rabbit.scaling = new BABYLON.Vector3(0.3, 0.3, 0.3);
  106. scene.beginAnimation(skeletons[0], 0, 100, true, 0.8);
  107. // Shadow caster
  108. var shadowCaster = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  109. shadowCaster.setEnabled(false);
  110. shadowCaster.position = new BABYLON.Vector3(0, 30, 0);
  111. var shadowCaster2 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  112. shadowCaster2.setEnabled(false);
  113. shadowCaster2.position = new BABYLON.Vector3(0, -30, 0);
  114. var shadowCaster3 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  115. shadowCaster3.setEnabled(false);
  116. shadowCaster3.position = new BABYLON.Vector3(20, 20, 10);
  117. var shadowGenerator = new BABYLON.ShadowGenerator(1024, directionalLight);
  118. shadowGenerator.getShadowMap().renderList.push(shadowCaster);
  119. shadowGenerator.usePoissonSampling = true;
  120. var shadowGenerator2 = new BABYLON.ShadowGenerator(1024, spotLight);
  121. shadowGenerator2.getShadowMap().renderList.push(shadowCaster2);
  122. shadowGenerator2.usePoissonSampling = true;
  123. var shadowGenerator3 = new BABYLON.ShadowGenerator(1024, pointLight);
  124. shadowGenerator3.getShadowMap().renderList.push(shadowCaster3);
  125. shadowGenerator3.usePoissonSampling = false;
  126. shadowGenerator3.bias = 0;
  127. // Register a render loop to repeatedly render the scene
  128. engine.runRenderLoop(function () {
  129. scene.render();
  130. divFps.innerHTML = engine.getFps().toFixed() + " fps";
  131. shadowCaster.rotation.x += 0.01;
  132. shadowCaster.rotation.y += 0.01;
  133. shadowCaster2.rotation.x += 0.01;
  134. shadowCaster2.rotation.y += 0.01;
  135. shadowCaster3.rotation.x += 0.01;
  136. shadowCaster3.rotation.y += 0.01;
  137. });
  138. // Resize
  139. window.addEventListener("resize", function () {
  140. engine.resize();
  141. });
  142. // Fog
  143. scene.fogMode = BABYLON.Scene.FOGMODE_NONE;
  144. scene.fogDensity = 0.01;
  145. // Create shaders
  146. var std = new BABYLON.StandardMaterial("std", scene);
  147. std.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  148. std.diffuseTexture.uScale = 5;
  149. std.diffuseTexture.vScale = 5;
  150. // Lava
  151. var lava = prepareLava();
  152. var simple = new BABYLON.SimpleMaterial("simple", scene);
  153. simple.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  154. simple.diffuseTexture.uScale = 5;
  155. simple.diffuseTexture.vScale = 5;
  156. var normal = prepareNormal();
  157. var gradient = prepareGradient();
  158. var fur = prepareFur();
  159. var water = prepareWater();
  160. water.addToRenderList(skybox);
  161. water.addToRenderList(shadowCaster);
  162. water.addToRenderList(shadowCaster2);
  163. water.addToRenderList(shadowCaster3);
  164. var fire = prepareFire();
  165. var terrain = prepareTerrain();
  166. var pbr = preparePBR();
  167. var triPlanar = prepareTriPlanar();
  168. var sky = prepareSky();
  169. var grid = prepareGrid();
  170. // Default to std
  171. var currentMaterial = std;
  172. sphere.material = std;
  173. sphere.receiveShadows = true;
  174. gui.add(options, 'material', ['standard', 'simple', 'water', 'fire', 'lava', 'normal', 'terrain', 'pbr', 'fur', 'triPlanar', 'gradient', 'sky', 'grid']).onFinishChange(function () {
  175. water.enableRenderTargets(false);
  176. skybox.material = skyboxMaterial;
  177. currentMesh.isVisible = true;
  178. fur.resetFur();
  179. switch (options.material) {
  180. case "simple":
  181. currentMaterial = simple;
  182. break;
  183. case "water":
  184. currentMaterial = water;
  185. water.enableRenderTargets(true);
  186. skybox.setEnabled(true);
  187. break;
  188. case "fire":
  189. currentMaterial = fire;
  190. break;
  191. case "lava":
  192. currentMaterial = lava;
  193. break;
  194. case "normal":
  195. currentMaterial = normal;
  196. break;
  197. case "terrain":
  198. currentMaterial = terrain;
  199. break;
  200. case "pbr":
  201. currentMaterial = pbr;
  202. break;
  203. case "fur":
  204. currentMaterial = fur.material;
  205. fur.configureFur(currentMesh);
  206. break;
  207. case "triPlanar":
  208. currentMaterial = triPlanar;
  209. break;
  210. case "gradient":
  211. currentMaterial = gradient;
  212. break;
  213. case "sky":
  214. skybox.setEnabled(true);
  215. skybox.material = sky;
  216. break;
  217. case "grid":
  218. currentMaterial = grid;
  219. break;
  220. default:
  221. currentMaterial = std;
  222. break;
  223. }
  224. currentMesh.material = currentMaterial;
  225. window.enableMaterial(options.material);
  226. });
  227. gui.add(options, 'mesh', ['sphere', 'knot', 'plane', 'ground', 'heightMap', 'rabbit']).onFinishChange(function () {
  228. currentMesh.setEnabled(false);
  229. switch (options.mesh) {
  230. case "sphere":
  231. currentMesh = sphere;
  232. break;
  233. case "knot":
  234. currentMesh = knot;
  235. break;
  236. case "plane":
  237. currentMesh = plane;
  238. break;
  239. case "ground":
  240. currentMesh = ground;
  241. break;
  242. case "heightMap":
  243. currentMesh = heightMap;
  244. break;
  245. case "rabbit":
  246. currentMesh = rabbit;
  247. break;
  248. }
  249. currentMesh.setEnabled(true);
  250. currentMesh.receiveShadows = true;
  251. currentMesh.material = currentMaterial;
  252. water.mesh = currentMesh;
  253. if (currentMaterial === fur.material) {
  254. // Furify the mesh
  255. fur.resetFur();
  256. fur.configureFur(currentMesh);
  257. }
  258. });
  259. var f1 = gui.addFolder('lights');
  260. f1.add(options, 'lightIntensity').onChange(function() {
  261. hemisphericLight.intensity = options.lightIntensity;
  262. directionalLight.intensity = options.lightIntensity;
  263. pointLight.intensity = options.lightIntensity;
  264. spotLight.intensity = options.lightIntensity;
  265. });
  266. f1.add(options, 'lightRange').onChange(function() {
  267. hemisphericLight.range = options.lightRange;
  268. directionalLight.range = options.lightRange;
  269. pointLight.range = options.lightRange;
  270. spotLight.range = options.lightRange;
  271. });
  272. f1.add(options, 'lightRadius').onChange(function() {
  273. hemisphericLight.radius = options.lightRadius;
  274. directionalLight.radius = options.lightRadius;
  275. pointLight.radius = options.lightRadius;
  276. spotLight.radius = options.lightRadius;
  277. });
  278. f1.add(options, 'hemisphericLight').onChange(function () {
  279. hemisphericLight.setEnabled(options.hemisphericLight);
  280. });
  281. f1.add(options, 'pointLight').onChange(function () {
  282. pointLight.setEnabled(options.pointLight);
  283. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  284. });
  285. f1.add(options, 'spotLight').onChange(function () {
  286. spotLight.setEnabled(options.spotLight);
  287. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  288. });
  289. f1.add(options, 'directionalLight').onChange(function () {
  290. directionalLight.setEnabled(options.directionalLight);
  291. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  292. });
  293. f1.add(options, 'castShadows').onChange(function () {
  294. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  295. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  296. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  297. });
  298. gui.add(options, 'fog').onChange(function () {
  299. scene.fogMode = options.fog ? BABYLON.Scene.FOGMODE_EXP : BABYLON.Scene.FOGMODE_NONE;
  300. });
  301. gui.add(options, 'skybox').onChange(function() {
  302. skybox.setEnabled(options.skybox);
  303. });
  304. });
  305. }
  306. </script>
  307. </body>
  308. </html>