index.html 13 KB

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