index.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 src="test/addlegacypbr.js"></script>
  49. <script src="test/addCell.js"></script>
  50. <script>
  51. BABYLONDEVTOOLS.Loader.load(function() {
  52. if (BABYLON.Engine.isSupported()) {
  53. var canvas = document.getElementById("renderCanvas");
  54. var engine = new BABYLON.Engine(canvas, true);
  55. BABYLONDEVTOOLS.Loader.debugShortcut(engine);
  56. var divFps = document.getElementById("fps");
  57. scene = new BABYLON.Scene(engine);
  58. var camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 6, 50, BABYLON.Vector3.Zero(), scene);
  59. camera.attachControl(canvas, true);
  60. camera.minZ = 0.1;
  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. directionalLight.position = new BABYLON.Vector3(0, 50, 0);
  68. var spotLight = new BABYLON.SpotLight("spot", new BABYLON.Vector3(0, -30, 0), new BABYLON.Vector3(0, 1, 0), 1.1, 1, scene);
  69. spotLight.setEnabled(false);
  70. // Create meshes
  71. var sphere = BABYLON.Mesh.CreateSphere("sphere", 48, 30.0, scene);
  72. var plane = BABYLON.MeshBuilder.CreateBox("plane", { width: 30, height: 1, depth:30 }, scene);
  73. plane.setEnabled(false);
  74. var ground = BABYLON.Mesh.CreateGround("ground", 512, 512, 32, scene, false);
  75. ground.scaling = new BABYLON.Vector3(0.1, 0.1, 0.1);
  76. ground.setEnabled(false);
  77. var knot = BABYLON.Mesh.CreateTorusKnot("knot", 10, 3, 128, 64, 2, 3, scene);
  78. knot.setEnabled(false);
  79. var heightMap = BABYLON.Mesh.CreateGroundFromHeightMap("heightMap", "../assets/textures/heightMap.png", 100, 100, 100, 0, 10, scene, false);
  80. heightMap.setEnabled(false);
  81. // Skybox
  82. var skybox = BABYLON.Mesh.CreateBox("skyBox", 1000.0, scene);
  83. var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
  84. skyboxMaterial.backFaceCulling = false;
  85. skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("../assets/textures/skybox/TropicalSunnyDay", scene);
  86. skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
  87. skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
  88. skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
  89. skyboxMaterial.disableLighting = true;
  90. skybox.material = skyboxMaterial;
  91. skybox.setEnabled(false);
  92. var currentMesh = sphere;
  93. // Rabbit
  94. var rabbit;
  95. BABYLON.SceneLoader.ImportMesh("Rabbit", "../assets/meshes/", "Rabbit.babylon", scene, function (newMeshes, particleSystems, skeletons) {
  96. rabbit = newMeshes[1];
  97. rabbit.setEnabled(false);
  98. rabbit.scaling = new BABYLON.Vector3(0.3, 0.3, 0.3);
  99. scene.beginAnimation(skeletons[0], 0, 100, true, 0.8);
  100. // Shadow caster
  101. var shadowCaster = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  102. shadowCaster.setEnabled(false);
  103. shadowCaster.position = new BABYLON.Vector3(0, 30, 0);
  104. var shadowCaster2 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  105. shadowCaster2.setEnabled(false);
  106. shadowCaster2.position = new BABYLON.Vector3(0, -30, 0);
  107. var shadowCaster3 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  108. shadowCaster3.setEnabled(false);
  109. shadowCaster3.position = new BABYLON.Vector3(20, 20, 10);
  110. var shadowGenerator = new BABYLON.ShadowGenerator(1024, directionalLight);
  111. shadowGenerator.getShadowMap().renderList.push(shadowCaster);
  112. shadowGenerator.usePoissonSampling = true;
  113. shadowGenerator.bias = 0;
  114. var shadowGenerator2 = new BABYLON.ShadowGenerator(1024, spotLight);
  115. shadowGenerator2.getShadowMap().renderList.push(shadowCaster2);
  116. shadowGenerator2.usePoissonSampling = true;
  117. var shadowGenerator3 = new BABYLON.ShadowGenerator(1024, pointLight);
  118. shadowGenerator3.getShadowMap().renderList.push(shadowCaster3);
  119. shadowGenerator3.usePoissonSampling = false;
  120. shadowGenerator3.bias = 0;
  121. // Register a render loop to repeatedly render the scene
  122. engine.runRenderLoop(function () {
  123. scene.render();
  124. divFps.innerHTML = engine.getFps().toFixed() + " fps";
  125. shadowCaster.rotation.x += 0.01;
  126. shadowCaster.rotation.y += 0.01;
  127. shadowCaster2.rotation.x += 0.01;
  128. shadowCaster2.rotation.y += 0.01;
  129. shadowCaster3.rotation.x += 0.01;
  130. shadowCaster3.rotation.y += 0.01;
  131. });
  132. // Resize
  133. window.addEventListener("resize", function () {
  134. engine.resize();
  135. });
  136. // Fog
  137. scene.fogMode = BABYLON.Scene.FOGMODE_NONE;
  138. scene.fogDensity = 0.01;
  139. // Create shaders
  140. var std = new BABYLON.StandardMaterial("std", scene);
  141. std.diffuseTexture = new BABYLON.Texture("../assets/textures/amiga.jpg", scene);
  142. std.diffuseTexture.uScale = 5;
  143. std.diffuseTexture.vScale = 5;
  144. var lava = prepareLava();
  145. var simple = new BABYLON.SimpleMaterial("simple", scene);
  146. simple.diffuseTexture = new BABYLON.Texture("../assets/textures/amiga.jpg", scene);
  147. simple.diffuseTexture.uScale = 5;
  148. simple.diffuseTexture.vScale = 5;
  149. var normal = prepareNormal();
  150. var gradient = prepareGradient();
  151. var fur = prepareFur();
  152. var water = prepareWater();
  153. water.addToRenderList(skybox);
  154. water.addToRenderList(shadowCaster);
  155. water.addToRenderList(shadowCaster2);
  156. water.addToRenderList(shadowCaster3);
  157. var fire = prepareFire();
  158. var terrain = prepareTerrain();
  159. var pbr = preparePBR();
  160. var legacypbr = prepareLegacyPBR();
  161. var triPlanar = prepareTriPlanar();
  162. var sky = prepareSky();
  163. var grid = prepareGrid();
  164. var shadowOnly = new BABYLON.ShadowOnlyMaterial();
  165. var cell = prepareCell();
  166. // Default to std
  167. var currentMaterial = std;
  168. sphere.material = std;
  169. sphere.receiveShadows = true;
  170. gui.add(options, 'material', ['standard', 'simple', 'water', 'fire', 'lava', 'normal', 'terrain', 'pbr', 'legacyPbr', 'fur', 'triPlanar', 'gradient', 'sky', 'grid', 'shadowOnly', 'cell']).onFinishChange(function () {
  171. water.enableRenderTargets(false);
  172. skybox.material = skyboxMaterial;
  173. currentMesh.isVisible = true;
  174. fur.resetFur();
  175. switch (options.material) {
  176. case "shadowOnly":
  177. currentMaterial = shadowOnly;
  178. break;
  179. case "simple":
  180. currentMaterial = simple;
  181. break;
  182. case "water":
  183. currentMaterial = water;
  184. water.enableRenderTargets(true);
  185. skybox.setEnabled(true);
  186. break;
  187. case "fire":
  188. currentMaterial = fire;
  189. break;
  190. case "lava":
  191. currentMaterial = lava;
  192. break;
  193. case "normal":
  194. currentMaterial = normal;
  195. break;
  196. case "terrain":
  197. currentMaterial = terrain;
  198. break;
  199. case "pbr":
  200. currentMaterial = pbr;
  201. break;
  202. case "legacyPbr":
  203. currentMaterial = legacypbr;
  204. break;
  205. case "fur":
  206. currentMaterial = fur.material;
  207. fur.configureFur(currentMesh);
  208. break;
  209. case "triPlanar":
  210. currentMaterial = triPlanar;
  211. break;
  212. case "gradient":
  213. currentMaterial = gradient;
  214. break;
  215. case "sky":
  216. skybox.setEnabled(true);
  217. skybox.material = sky;
  218. break;
  219. case "grid":
  220. currentMaterial = grid;
  221. break;
  222. case "cell":
  223. currentMaterial = cell;
  224. break;
  225. default:
  226. currentMaterial = std;
  227. break;
  228. }
  229. currentMesh.material = currentMaterial;
  230. window.enableMaterial(options.material);
  231. });
  232. gui.add(options, 'mesh', ['sphere', 'knot', 'plane', 'ground', 'heightMap', 'rabbit']).onFinishChange(function () {
  233. currentMesh.setEnabled(false);
  234. switch (options.mesh) {
  235. case "sphere":
  236. currentMesh = sphere;
  237. break;
  238. case "knot":
  239. currentMesh = knot;
  240. break;
  241. case "plane":
  242. currentMesh = plane;
  243. break;
  244. case "ground":
  245. currentMesh = ground;
  246. break;
  247. case "heightMap":
  248. currentMesh = heightMap;
  249. break;
  250. case "rabbit":
  251. currentMesh = rabbit;
  252. break;
  253. }
  254. currentMesh.setEnabled(true);
  255. currentMesh.receiveShadows = true;
  256. currentMesh.material = currentMaterial;
  257. water.mesh = currentMesh;
  258. if (currentMaterial === fur.material) {
  259. // Furify the mesh
  260. fur.resetFur();
  261. fur.configureFur(currentMesh);
  262. }
  263. });
  264. var f1 = gui.addFolder('lights');
  265. f1.add(options, 'lightIntensity').onChange(function() {
  266. hemisphericLight.intensity = options.lightIntensity;
  267. directionalLight.intensity = options.lightIntensity;
  268. pointLight.intensity = options.lightIntensity;
  269. spotLight.intensity = options.lightIntensity;
  270. });
  271. f1.add(options, 'lightRange').onChange(function() {
  272. hemisphericLight.range = options.lightRange;
  273. directionalLight.range = options.lightRange;
  274. pointLight.range = options.lightRange;
  275. spotLight.range = options.lightRange;
  276. });
  277. f1.add(options, 'lightRadius').onChange(function() {
  278. hemisphericLight.radius = options.lightRadius;
  279. directionalLight.radius = options.lightRadius;
  280. pointLight.radius = options.lightRadius;
  281. spotLight.radius = options.lightRadius;
  282. });
  283. f1.add(options, 'hemisphericLight').onChange(function () {
  284. hemisphericLight.setEnabled(options.hemisphericLight);
  285. });
  286. f1.add(options, 'pointLight').onChange(function () {
  287. pointLight.setEnabled(options.pointLight);
  288. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  289. });
  290. f1.add(options, 'spotLight').onChange(function () {
  291. spotLight.setEnabled(options.spotLight);
  292. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  293. });
  294. f1.add(options, 'directionalLight').onChange(function () {
  295. directionalLight.setEnabled(options.directionalLight);
  296. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  297. });
  298. f1.add(options, 'castShadows').onChange(function () {
  299. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  300. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  301. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  302. });
  303. gui.add(options, 'fog').onChange(function () {
  304. scene.fogMode = options.fog ? BABYLON.Scene.FOGMODE_EXP : BABYLON.Scene.FOGMODE_NONE;
  305. });
  306. gui.add(options, 'skybox').onChange(function() {
  307. skybox.setEnabled(options.skybox);
  308. });
  309. });
  310. }
  311. });
  312. </script>
  313. </body>
  314. </html>