index.html 12 KB

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