123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Procedural textures Library</title>
- <script src="refs/dat.gui.min.js"></script>
- <script src="refs/babylon.max.js"></script>
- <script src="../dist/babylon.fireProceduralTexture.js"></script>
- <script src="../dist/babylon.woodProceduralTexture.js"></script>
- <style>
- html, body {
- width: 100%;
- height: 100%;
- padding: 0;
- margin: 0;
- overflow: hidden;
- }
- #renderCanvas {
- width: 100%;
- height: 100%;
- }
- #fps {
- position: absolute;
- background-color: black;
- border: 2px solid red;
- text-align: center;
- font-size: 16px;
- color: white;
- top: 15px;
- left: 10px;
- width: 60px;
- height: 20px;
- }
- </style>
- </head>
- <body>
- <div id="fps">0</div>
- <canvas id="renderCanvas"></canvas>
- <script src="index.js"></script>
- <script src="add/addFirePT.js"></script>
- <script src="add/addWoodPT.js"></script>
- <script>
- if (BABYLON.Engine.isSupported()) {
- var canvas = document.getElementById("renderCanvas");
- var engine = new BABYLON.Engine(canvas, true);
- var divFps = document.getElementById("fps");
- var scene = new BABYLON.Scene(engine);
- var camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 6, 50, BABYLON.Vector3.Zero(), scene);
- camera.attachControl(canvas, true);
- // Lights
- var hemisphericLight = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0), scene);
- var pointLight = new BABYLON.PointLight("point", new BABYLON.Vector3(20, 20, 10), scene);
- pointLight.setEnabled(false);
- var directionalLight = new BABYLON.DirectionalLight("directional", new BABYLON.Vector3(0,-1, 0), scene);
- directionalLight.setEnabled(false);
- var spotLight = new BABYLON.SpotLight("spot", new BABYLON.Vector3(0, -30, 0), new BABYLON.Vector3(0, 1, 0), 1.1, 1, scene);
- spotLight.setEnabled(false);
- // Create meshes
- var sphere = BABYLON.Mesh.CreateSphere("sphere", 32, 30.0, scene);
-
- var plane = BABYLON.MeshBuilder.CreateBox("plane", { width: 30, height: 1, depth:30 }, scene);
- plane.setEnabled(false);
-
- var ground = BABYLON.Mesh.CreateGround("ground", 512, 512, 32, scene, false);
- ground.scaling = new BABYLON.Vector3(0.1, 0.1, 0.1);
- ground.setEnabled(false);
-
- var knot = BABYLON.Mesh.CreateTorusKnot("knot", 10, 3, 128, 64, 2, 3, scene);
- knot.setEnabled(false);
-
- // Skybox
- var skybox = BABYLON.Mesh.CreateBox("skyBox", 1000.0, scene);
- var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
- skyboxMaterial.backFaceCulling = false;
- skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("textures/skybox/TropicalSunnyDay", scene);
- skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
- skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
- skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
- skyboxMaterial.disableLighting = true;
- skybox.material = skyboxMaterial;
- skybox.setEnabled(false);
- var currentMesh = sphere;
- // Rabbit
- var rabbit;
- BABYLON.SceneLoader.ImportMesh("Rabbit", "meshes/", "Rabbit.babylon", scene, function (newMeshes, particleSystems, skeletons) {
- rabbit = newMeshes[1];
- rabbit.setEnabled(false);
- rabbit.scaling = new BABYLON.Vector3(0.3, 0.3, 0.3);
- scene.beginAnimation(skeletons[0], 0, 100, true, 0.8);
- // Shadow caster
- var shadowCaster = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
- shadowCaster.setEnabled(false);
- shadowCaster.position = new BABYLON.Vector3(0, 30, 0);
-
- var shadowCaster2 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
- shadowCaster2.setEnabled(false);
- shadowCaster2.position = new BABYLON.Vector3(0, -30, 0);
-
- var shadowCaster3 = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
- shadowCaster3.setEnabled(false);
- shadowCaster3.position = new BABYLON.Vector3(20, 20, 10);
- var shadowGenerator = new BABYLON.ShadowGenerator(1024, directionalLight);
- shadowGenerator.getShadowMap().renderList.push(shadowCaster);
- shadowGenerator.usePoissonSampling = true;
-
- var shadowGenerator2 = new BABYLON.ShadowGenerator(1024, spotLight);
- shadowGenerator2.getShadowMap().renderList.push(shadowCaster2);
- shadowGenerator2.usePoissonSampling = true;
-
- var shadowGenerator3 = new BABYLON.ShadowGenerator(1024, pointLight);
- shadowGenerator3.getShadowMap().renderList.push(shadowCaster3);
- shadowGenerator3.usePoissonSampling = true;
- // Register a render loop to repeatedly render the scene
- engine.runRenderLoop(function () {
- scene.render();
- divFps.innerHTML = engine.getFps().toFixed() + " fps";
- shadowCaster.rotation.x += 0.01;
- shadowCaster.rotation.y += 0.01;
- shadowCaster2.rotation.x += 0.01;
- shadowCaster2.rotation.y += 0.01;
- shadowCaster3.rotation.x += 0.01;
- shadowCaster3.rotation.y += 0.01;
- });
- // Resize
- window.addEventListener("resize", function () {
- engine.resize();
- });
- // Fog
- scene.fogMode = BABYLON.Scene.FOGMODE_NONE;
- scene.fogDensity = 0.01;
-
- // Create shaders
- var std = new BABYLON.StandardMaterial("std", scene);
- std.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
- std.diffuseTexture.uScale = 5;
- std.diffuseTexture.vScale = 5;
- // Fire Procedural Texture
- var firePT = addFirePT();
- var fireMaterial = new BABYLON.StandardMaterial("fire", scene);
- fireMaterial.diffuseTexture = firePT;
-
- // Wood Procedural Texture
- var woodPT = addWoodPT();
- var woodMaterial = new BABYLON.StandardMaterial("wood", scene);
- woodMaterial.diffuseTexture = woodPT;
-
- // Default to std
- var currentMaterial = std;
- sphere.material = std;
- sphere.receiveShadows = true;
- gui.add(options, 'material', ['none','fire', 'wood']).onFinishChange(function () {
- switch (options.material) {
- case "fire":
- currentMaterial = fireMaterial;
- break;
- case "wood":
- currentMaterial = woodMaterial;
- break;
- case "none":
- default:
- currentMaterial = std;
- break;
- }
- currentMesh.material = currentMaterial;
- window.enableMaterial(options.material);
- });
- gui.add(options, 'mesh', ['sphere', 'knot', 'plane', 'ground', 'rabbit']).onFinishChange(function () {
- currentMesh.setEnabled(false);
- switch (options.mesh) {
- case "sphere":
- currentMesh = sphere;
- break;
- case "knot":
- currentMesh = knot;
- break;
- case "plane":
- currentMesh = plane;
- break;
- case "ground":
- currentMesh = ground;
- break;
- case "rabbit":
- currentMesh = rabbit;
- break;
- }
- currentMesh.setEnabled(true);
- currentMesh.receiveShadows = true;
- currentMesh.material = currentMaterial;
-
- water.mesh = currentMesh;
- });
- var f1 = gui.addFolder('lights');
- f1.add(options, 'hemisphericLight').onChange(function () {
- hemisphericLight.setEnabled(options.hemisphericLight);
- });
- f1.add(options, 'pointLight').onChange(function () {
- pointLight.setEnabled(options.pointLight);
- shadowCaster3.setEnabled(options.pointLight && options.castShadows);
- });
-
- f1.add(options, 'spotLight').onChange(function () {
- spotLight.setEnabled(options.spotLight);
- shadowCaster2.setEnabled(options.spotLight && options.castShadows);
- });
- f1.add(options, 'directionalLight').onChange(function () {
- directionalLight.setEnabled(options.directionalLight);
- shadowCaster.setEnabled(options.directionalLight && options.castShadows);
- });
- f1.add(options, 'castShadows').onChange(function () {
- shadowCaster.setEnabled(options.directionalLight && options.castShadows);
- shadowCaster2.setEnabled(options.spotLight && options.castShadows);
- shadowCaster3.setEnabled(options.pointLight && options.castShadows);
- });
- gui.add(options, 'fog').onChange(function () {
- scene.fogMode = options.fog ? BABYLON.Scene.FOGMODE_EXP : BABYLON.Scene.FOGMODE_NONE;
- });
-
- gui.add(options, 'skybox').onChange(function() {
- skybox.setEnabled(options.skybox);
- });
- });
- }
- </script>
- </body>
- </html>
|