123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Procedural textures Library</title>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.6.2/dat.gui.min.js"></script>
- <script src="../tools/DevLoader/BabylonLoader.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="test/index.js"></script>
- <script src="test/addCloudPT.js"></script>
- <script src="test/addFirePT.js"></script>
- <script src="test/addWoodPT.js"></script>
- <script src="test/addGrassPT.js"></script>
- <script src="test/addRoadPT.js"></script>
- <script src="test/addBrickPT.js"></script>
- <script src="test/addMarblePT.js"></script>
- <script src="test/addStarfieldPT.js"></script>
- <script src="test/addNormalMapPT.js"></script>
- <script src="test/addPerlinNoisePT.js"></script>
- <script>
- BABYLONDEVTOOLS.Loader.load(function() {
- if (BABYLON.Engine.isSupported()) {
- var canvas = document.getElementById("renderCanvas");
- var engine = new BABYLON.Engine(canvas, true);
- BABYLONDEVTOOLS.Loader.debugShortcut(engine);
- var divFps = document.getElementById("fps");
- 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("/playground/textures/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", "/playground/scenes/", "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.specularColor = new BABYLON.Color3(0, 0, 0);
-
- var diffuseTexture = new BABYLON.Texture("/playground/textures/amiga.jpg", scene);
- diffuseTexture.uScale = 5;
- diffuseTexture.vScale = 5;
- // Fire Procedural Texture
- var firePT = addFirePT();
-
- // Wood Procedural Texture
- var woodPT = addWoodPT();
-
- // Cloud Procedural Texture
- var cloudPT = addCloudPT();
-
- // Grass Procedural Texture
- var grassPT = addGrassPT();
-
- // Road Procedural Texture
- var roadPT = addRoadPT();
-
- // Brick Procedural Texture
- var brickPT = addBrickPT();
-
- // Marble Procedural Texture
- var marblePT = addMarblePT();
-
- // Starfield Procedural Texture
- var starfieldPT = addStarfieldPT();
- // Normal Map Procedural Texture
- var normalMapPT = addNormalMapPT();
- normalMapPT.baseTexture = diffuseTexture;
- // Perlin Noise Procedural Texture
- var perlinNoisePT = addPerlinNoisePT();
-
- // Default to std
- var currentTexture = diffuseTexture;
- sphere.material = std;
- sphere.receiveShadows = true;
- std.diffuseTexture = currentTexture;
-
- //placeholder for current texture options
- var currentPTOptions = gui.addFolder('Texture Options');
- var PTOptions = [];
-
- dat.GUI.prototype.removeFolder = function(name) {
- var folder = this.__folders[name];
- if (!folder) {
- return;
- }
- folder.close();
- this.__ul.removeChild(folder.domElement.parentNode);
- delete this.__folders[name];
- this.onResize();
- }
-
- var resetPTOptions = function() {
-
- //empty options
- while(PTOptions.length > 0){
- var option = PTOptions.pop()
- if(option.optionType === "Folder"){
- currentPTOptions.removeFolder(option.optionValue);
- }
- else {
- currentPTOptions.remove(option.optionValue);
- }
- }
- }
-
- var addFloatSubOption = function(guiElement, field, propertyName, proxy, initvalue){
- proxy[propertyName] = initvalue;
- guiElement.add(proxy, propertyName).onChange(function () {
- try {
- var xvalue = parseFloat(proxy[propertyName]);
- eval('field.' + propertyName + ' = xvalue');
- } catch (e) {
-
- }
- });
- }
-
- var addPToptions = function(pt, fieldNames){
- for(var fieldid = 0; fieldid < fieldNames.length; fieldid++){
- var field = fieldNames[fieldid]
- var added = {};
- if(typeof pt[field] != 'object' && pt[field] != 'undefined') {
- added.optionType = "Value";
- added.optionValue = currentPTOptions.add(pt, fieldNames[fieldid]);
- }
- else {
- var proxy = {};
-
- if(pt[field] instanceof BABYLON.Vector2){
- added.optionType = 'Folder';
- added.optionValue = field + ' ('+ pt[field].constructor.name +')';
- var folder = currentPTOptions.addFolder(added.optionValue);
- addFloatSubOption(folder, pt[field], 'x', proxy, pt[field].x);
- addFloatSubOption(folder, pt[field], 'y', proxy, pt[field].y);
- }
-
- else if(pt[field] instanceof BABYLON.Vector3){
- added.optionType = 'Folder';
- added.optionValue = field + ' ('+ pt[field].constructor.name +')';
- var folder = currentPTOptions.addFolder(added.optionValue);
- addFloatSubOption(folder, pt[field], 'x', proxy, pt[field].x);
- addFloatSubOption(folder, pt[field], 'y', proxy, pt[field].y);
- addFloatSubOption(folder, pt[field], 'z', proxy, pt[field].z);
- }
- else if(pt[field] instanceof BABYLON.Vector4){
- added.optionType = 'Folder';
- added.optionValue = field + ' ('+ pt[field].constructor.name +')';
- var folder = currentPTOptions.addFolder(added.optionValue);
- addFloatSubOption(folder, pt[field], 'x', proxy, pt[field].x);
- addFloatSubOption(folder, pt[field], 'y', proxy, pt[field].y);
- addFloatSubOption(folder, pt[field], 'z', proxy, pt[field].z);
- addFloatSubOption(folder, pt[field], 'w', proxy, pt[field].w);
- }
- else if(pt[field] instanceof BABYLON.Color3){
- added.optionType = 'Folder';
- added.optionValue = field + ' ('+ pt[field].constructor.name +')';
- var folder = currentPTOptions.addFolder(added.optionValue);
- addFloatSubOption(folder, pt[field], 'r', proxy, pt[field].r);
- addFloatSubOption(folder, pt[field], 'g', proxy, pt[field].g);
- addFloatSubOption(folder, pt[field], 'b', proxy, pt[field].b);
- }
- else if(pt[field] instanceof BABYLON.Color4){
- added.optionType = 'Folder';
- added.optionValue = field + ' ('+ pt[field].constructor.name +')';
- var folder = currentPTOptions.addFolder(added.optionValue);
- addFloatSubOption(folder, pt[field], 'r', proxy, pt[field].r);
- addFloatSubOption(folder, pt[field], 'g', proxy, pt[field].g);
- addFloatSubOption(folder, pt[field], 'b', proxy, pt[field].b);
- addFloatSubOption(folder, pt[field], 'a', proxy, pt[field].a);
- }
- else {
- proxy[field] = 'Object (' + pt[field].constructor.name +')';
- added.optionType = "Value";
- added.optionValue = currentPTOptions.add(proxy, field).onChange(function () {
- try {
- var res = eval(proxy[field]);
- pt[field] = res;
- } catch (e) { }
- });
- }
- }
- PTOptions.push(added);
- }
- }
-
- gui.add(options, 'texture', ['default', 'fire', 'wood', 'cloud', 'grass', 'road', 'brick', 'marble', 'starfield', 'normalMap', 'perlinNoise']).onFinishChange(function () {
- resetPTOptions();
- switch (options.texture) {
- case "fire":
- currentTexture = firePT;
- addPToptions(firePT, ['time', 'alphaThreshold', 'speed', ]);
- break;
- case "wood":
- currentTexture = woodPT;
- addPToptions(woodPT, ['ampScale', 'woodColor']);
- break;
- case "cloud":
- currentTexture = cloudPT;
- addPToptions(cloudPT, ['skyColor', 'cloudColor']);
- break;
- case "grass":
- currentTexture = grassPT;
- addPToptions(grassPT, ['groundColor']);
- break;
- case "road":
- currentTexture = roadPT;
- addPToptions(roadPT, ['roadColor']);
- break;
- case "brick":
- currentTexture = brickPT;
- addPToptions(brickPT, ['numberOfBricksHeight', 'numberOfBricksWidth', 'brickColor', 'jointColor']);
- break;
- case "marble":
- currentTexture = marblePT;
- addPToptions(marblePT, ['numberOfTilesHeight', 'numberOfTilesWidth', 'amplitude', 'jointColor']);
- break;
- case "starfield":
- currentTexture = starfieldPT;
- addPToptions(starfieldPT, ['saturation', 'distfading', 'darkmatter', 'alpha', 'time', 'beta', 'zoom', 'formuparam', 'stepsize', 'tile', 'brightness']);
- break;
- case "normalMap":
- currentTexture = normalMapPT;
- break;
- case "perlinNoise":
- currentTexture = perlinNoisePT;
- break;
- case "none":
- default:
- currentTexture = diffuseTexture;
- break;
- }
- std.diffuseTexture = currentTexture;
- window.enableTexture(options.texture);
- });
- 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 = std;
- });
- 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>
|