Selaa lähdekoodia

New parameters of lava material + readme

Temechon 9 vuotta sitten
vanhempi
commit
b0137a3ef7

+ 10 - 2
materialsLibrary/config.json

@@ -1,10 +1,18 @@
 {
   "materials": [
     {
+      "file": "materials/glass/babylon.glassMaterial.ts",
+      "shaderFiles": [
+        "materials/glass/glass.vertex.fx",
+        "materials/glass/glass.fragment.fx"
+      ],
+      "output": "babylon.glassMaterial.js"
+    },
+    {
       "file": "materials/lava/babylon.lavaMaterial.ts",
       "shaderFiles": [
-          "materials/lava/lava.vertex.fx",
-          "materials/lava/lava.fragment.fx"
+        "materials/lava/lava.vertex.fx",
+        "materials/lava/lava.fragment.fx"
       ],
       "output": "babylon.lavaMaterial.js"
     },

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 21 - 5
materialsLibrary/dist/babylon.lavaMaterial.js


+ 24 - 3
materialsLibrary/materials/lava/babylon.lavaMaterial.ts

@@ -61,6 +61,9 @@ module BABYLON {
     export class LavaMaterial extends Material {
         public diffuseTexture: BaseTexture;
         public noiseTexture: BaseTexture;
+        public fogColor: Color3;
+        public speed : number = 1;
+
         private _lastTime : number = 0;
 
         public diffuseColor = new Color3(1, 1, 1);
@@ -355,7 +358,7 @@ module BABYLON {
                         "vDiffuseInfos", 
                         "mBones",
                         "vClipPlane", "diffuseMatrix",
-                        "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3","time"
+                        "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3","time", "speed", "fogColor"
                     ],
                     ["diffuseSampler",
                         "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3", "noiseTexture"
@@ -485,7 +488,13 @@ module BABYLON {
 
 
             this._lastTime += scene.getEngine().getDeltaTime();
-            this._effect.setFloat("time", this._lastTime / 1000);
+            this._effect.setFloat("time", this._lastTime * this.speed / 1000);
+
+            if (! this.fogColor) {
+                this.fogColor = Color3.Black();
+            }
+            this._effect.setColor3("fogColor", this.fogColor);
+
 
             super.bind(world, mesh);
         }
@@ -497,6 +506,10 @@ module BABYLON {
                 results.push(this.diffuseTexture);
             }
 
+            if (this.noiseTexture && this.noiseTexture.animations && this.noiseTexture.animations.length > 0) {
+                results.push(this.noiseTexture);
+            }
+
             return results;
         }
 
@@ -504,6 +517,9 @@ module BABYLON {
             if (this.diffuseTexture) {
                 this.diffuseTexture.dispose();
             }
+            if (this.noiseTexture) {
+                this.noiseTexture.dispose();
+            }
 
             super.dispose(forceDisposeEffect);
         }
@@ -518,8 +534,13 @@ module BABYLON {
             if (this.diffuseTexture && this.diffuseTexture.clone) {
                 newMaterial.diffuseTexture = this.diffuseTexture.clone();
             }
+            if (this.noiseTexture && this.noiseTexture.clone) {
+                newMaterial.noiseTexture = this.noiseTexture.clone();
+            }
+            if (this.fogColor && this.fogColor.clone) {
+                newMaterial.fogColor = this.fogColor.clone();
+            }
 
-            newMaterial.diffuseColor = this.diffuseColor.clone();
             return newMaterial;
         }
     }

+ 4 - 10
materialsLibrary/materials/lava/lava.fragment.fx

@@ -9,8 +9,9 @@ varying vec3 vPositionW;
 
 // MAGMAAAA
 uniform float time;
+uniform float speed;
+uniform vec3 fogColor;
 uniform sampler2D noiseTexture;
-uniform float incandescent;
 
 // Varying
 varying float noise;
@@ -395,11 +396,9 @@ void main(void) {
 #ifdef DIFFUSE
     ////// MAGMA ///
 
-    vec3 fogColor = vec3(0.5,0.5,0.5);
-
 	vec4 noiseTex = texture2D( noiseTexture, vDiffuseUV );
 	vec2 T1 = vDiffuseUV + vec2( 1.5, -1.5 ) * time  * 0.02;
-	vec2 T2 = vDiffuseUV + vec2( -0.5, 2.0 ) * time * 0.01;
+	vec2 T2 = vDiffuseUV + vec2( -0.5, 2.0 ) * time * 0.01 * speed;
 
 	T1.x += noiseTex.x * 2.0;
 	T1.y += noiseTex.y * 2.0;
@@ -408,13 +407,9 @@ void main(void) {
 
 	float p = texture2D( noiseTexture, T1 * 3.0 ).a;
 
-	vec4 lavaColor = texture2D( diffuseSampler, T2 * 4.0 );
+	vec4 lavaColor = texture2D( diffuseSampler, T2 * 4.0);
 	vec4 temp = lavaColor * ( vec4( p, p, p, p ) * 2.0 ) + ( lavaColor * lavaColor - 0.1 );
 
-	if( temp.r > 1.0 ){ temp.bg += clamp( temp.r - 2.0, 0.0, 100.0 ); }
-	if( temp.g > 1.0 ){ temp.rb += temp.g - 1.0; }
-	if( temp.b > 1.0 ){ temp.rg += temp.b - 1.0; }
-
 	baseColor = temp;
 
 	float fogDensity = 0.15;
@@ -425,7 +420,6 @@ void main(void) {
 
     baseColor = mix( baseColor, vec4( fogColor, baseColor.w ), fogFactor );
 
-
     ///// END MAGMA ////
 
 

+ 2 - 1
materialsLibrary/materials/lava/lava.vertex.fx

@@ -1,6 +1,7 @@
 precision highp float;
 // Inputs
 uniform float time;
+uniform float speed;
 // Varying
 varying float noise;
 
@@ -222,7 +223,7 @@ void main(void) {
 
 
     // get a turbulent 3d noise using the normal, normal to high freq
-    noise = 10.0 *  -.10 * turbulence( .5 * normal + time*10.15 );
+    noise = 10.0 *  -.10 * turbulence( .5 * normal + time*1.15 );
     // get a 3d noise using the position, low frequency
     float b = 5.0 * pnoise( 0.05 * position +vec3(time*1.025), vec3( 100.0 ) );
     // compose both noises

+ 32 - 0
materialsLibrary/materials/lava/readme.md

@@ -0,0 +1,32 @@
+# Lava material
+
+## No playground example for now
+
+## Using the lava material
+
+The lava material needs at least a noise texture and a diffuse texture to render properly.
+Just create a new reference of the material and assign it two textures:
+
+```
+var ground = BABYLON.Mesh.CreateGround("ground", 512, 512, 32, scene);
+
+var lavaMaterial = new BABYLON.LavaMaterial("lava", scene);
+lavaMaterial.noiseTexture = new BABYLON.Texture("cloud.png", scene); // Set the bump texture
+lavaMaterial.diffuseTexture = new BABYLON.Texture("lavatile.jpg", scene); // Set the diffuse texture
+
+ground.material = lavaMaterial;
+```
+
+The diffuse texture will be the color of your lava, the noise texture will represent the lava deformation.
+Notice that this material will update each vertex position of your object. If there are not enough vertices, 
+some artefacts may appears.
+
+## Customize the water material
+
+You can customize two properties of the material:
+
+```
+lavaMaterial.speed = 2.0; // Default 1. Represents speed of perturbations of the lava
+lavaMaterial.fogColor = new BABYLON.Color3(1, 0, 0); // Default to (0,0,0) black. Represents the color of the fog displayed on the lava ground.
+```
+

+ 13 - 6
materialsLibrary/test/index.html

@@ -5,6 +5,7 @@
 	<script src="dat.gui.min.js"></script>
 	<script src="babylon.max.js"></script>
 	<script src="../dist/babylon.simpleMaterial.js"></script>
+	<script src="../dist/babylon.glassMaterial.js"></script>
 	<script src="../dist/babylon.waterMaterial.js"></script>
 	<script src="../dist/babylon.fireMaterial.js"></script>
     <script src="../dist/babylon.lavaMaterial.js"></script>
@@ -152,12 +153,15 @@
 				std.diffuseTexture.vScale = 5;
 
                 // Lava
-                var lava = new BABYLON.LavaMaterial("diffuseEmissive", scene);
+                var lava = new BABYLON.LavaMaterial("lava", scene);
                 lava.diffuseTexture = new BABYLON.Texture("textures/lava/lavatile.jpg", scene);
                 lava.diffuseTexture.uScale = 0.5;
                 lava.diffuseTexture.vScale = 0.5;
-                lava.noiseTexture = new BABYLON.Texture("textures/lava/cloud.png", scene);
+				lava.noiseTexture = new BABYLON.Texture("textures/lava/cloud.png", scene);
+				lava.fogColor = BABYLON.Color3.Black();
+				lava.speed = 2.5;
 
+				var glass = new BABYLON.GlassMaterial("glass", scene);
 
 				var simple = new BABYLON.SimpleMaterial("simple", scene);
 				simple.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
@@ -200,7 +204,7 @@
 					skybox: false
 				}
 
-				gui.add(options, 'material', ['standard', 'simple', 'water', 'fire', 'lava']).onFinishChange(function () {
+				gui.add(options, 'material', ['standard', 'simple', 'water', 'fire', 'lava', 'glass']).onFinishChange(function () {
 					water.enableRenderTargets(false);
 					
 					switch (options.material) {
@@ -215,9 +219,12 @@
                         case "fire":
                             currentMaterial = fire;
                             break;
-                        case "lava":
-                            currentMaterial = lava;
-                            break;
+						case "lava":
+							currentMaterial = lava;
+							break;
+						case "glass":
+							currentMaterial = glass;
+							break;
 						default:
 							currentMaterial = std;
 							break;