index.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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.furMaterial.js"></script>
  14. <script src="../dist/babylon.triPlanarMaterial.js"></script>
  15. <script src="../dist/babylon.gradientMaterial.js"></script>
  16. <script src="../dist/babylon.skyMaterial.js"></script>
  17. <script src="../dist/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="index.js"></script>
  48. <script src="add/addlava.js"></script>
  49. <script src="add/addnormal.js"></script>
  50. <script src="add/addwater.js"></script>
  51. <script src="add/addfur.js"></script>
  52. <script src="add/addterrain.js"></script>
  53. <script src="add/addfire.js"></script>
  54. <script src="add/addtriplanar.js"></script>
  55. <script src="add/addgradient.js"></script>
  56. <script src="add/addsky.js"></script>
  57. <script src="add/addgrid.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 triPlanar = prepareTriPlanar();
  165. var sky = prepareSky();
  166. var grid = prepareGrid();
  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', 'fur', 'triPlanar', 'gradient', 'sky', 'grid']).onFinishChange(function () {
  172. water.enableRenderTargets(false);
  173. skybox.material = skyboxMaterial;
  174. currentMesh.isVisible = true;
  175. fur.resetFur();
  176. switch (options.material) {
  177. case "simple":
  178. currentMaterial = simple;
  179. break;
  180. case "water":
  181. currentMaterial = water;
  182. water.enableRenderTargets(true);
  183. skybox.setEnabled(true);
  184. break;
  185. case "fire":
  186. currentMaterial = fire;
  187. break;
  188. case "lava":
  189. currentMaterial = lava;
  190. break;
  191. case "normal":
  192. currentMaterial = normal;
  193. break;
  194. case "terrain":
  195. currentMaterial = terrain;
  196. break;
  197. case "fur":
  198. currentMaterial = fur.material;
  199. fur.configureFur(currentMesh);
  200. break;
  201. case "triPlanar":
  202. currentMaterial = triPlanar;
  203. break;
  204. case "gradient":
  205. currentMaterial = gradient;
  206. break;
  207. case "sky":
  208. skybox.setEnabled(true);
  209. skybox.material = sky;
  210. break;
  211. case "grid":
  212. currentMaterial = grid;
  213. break;
  214. default:
  215. currentMaterial = std;
  216. break;
  217. }
  218. currentMesh.material = currentMaterial;
  219. window.enableMaterial(options.material);
  220. });
  221. gui.add(options, 'mesh', ['sphere', 'knot', 'plane', 'ground', 'heightMap', 'rabbit']).onFinishChange(function () {
  222. currentMesh.setEnabled(false);
  223. switch (options.mesh) {
  224. case "sphere":
  225. currentMesh = sphere;
  226. break;
  227. case "knot":
  228. currentMesh = knot;
  229. break;
  230. case "plane":
  231. currentMesh = plane;
  232. break;
  233. case "ground":
  234. currentMesh = ground;
  235. break;
  236. case "heightMap":
  237. currentMesh = heightMap;
  238. break;
  239. case "rabbit":
  240. currentMesh = rabbit;
  241. break;
  242. }
  243. currentMesh.setEnabled(true);
  244. currentMesh.receiveShadows = true;
  245. currentMesh.material = currentMaterial;
  246. water.mesh = currentMesh;
  247. if (currentMaterial === fur.material) {
  248. // Furify the mesh
  249. fur.resetFur();
  250. fur.configureFur(currentMesh);
  251. }
  252. });
  253. var f1 = gui.addFolder('lights');
  254. f1.add(options, 'lightIntensity').onChange(function() {
  255. hemisphericLight.intensity = options.lightIntensity;
  256. directionalLight.intensity = options.lightIntensity;
  257. pointLight.intensity = options.lightIntensity;
  258. spotLight.intensity = options.lightIntensity;
  259. });
  260. f1.add(options, 'lightRange').onChange(function() {
  261. hemisphericLight.range = options.lightRange;
  262. directionalLight.range = options.lightRange;
  263. pointLight.range = options.lightRange;
  264. spotLight.range = options.lightRange;
  265. });
  266. f1.add(options, 'lightRadius').onChange(function() {
  267. hemisphericLight.radius = options.lightRadius;
  268. directionalLight.radius = options.lightRadius;
  269. pointLight.radius = options.lightRadius;
  270. spotLight.radius = options.lightRadius;
  271. });
  272. f1.add(options, 'hemisphericLight').onChange(function () {
  273. hemisphericLight.setEnabled(options.hemisphericLight);
  274. });
  275. f1.add(options, 'pointLight').onChange(function () {
  276. pointLight.setEnabled(options.pointLight);
  277. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  278. });
  279. f1.add(options, 'spotLight').onChange(function () {
  280. spotLight.setEnabled(options.spotLight);
  281. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  282. });
  283. f1.add(options, 'directionalLight').onChange(function () {
  284. directionalLight.setEnabled(options.directionalLight);
  285. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  286. });
  287. f1.add(options, 'castShadows').onChange(function () {
  288. shadowCaster.setEnabled(options.directionalLight && options.castShadows);
  289. shadowCaster2.setEnabled(options.spotLight && options.castShadows);
  290. shadowCaster3.setEnabled(options.pointLight && options.castShadows);
  291. });
  292. gui.add(options, 'fog').onChange(function () {
  293. scene.fogMode = options.fog ? BABYLON.Scene.FOGMODE_EXP : BABYLON.Scene.FOGMODE_NONE;
  294. });
  295. gui.add(options, 'skybox').onChange(function() {
  296. skybox.setEnabled(options.skybox);
  297. });
  298. });
  299. }
  300. </script>
  301. </body>
  302. </html>