index.html 12 KB

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