Browse Source

Merge pull request #791 from julien-moreau/master

Materials library
David Catuhe 9 years ago
parent
commit
412425b38f

File diff suppressed because it is too large
+ 26 - 24
materialsLibrary/dist/babylon.waterMaterial.js


+ 29 - 26
materialsLibrary/materials/water/babylon.waterMaterial.ts

@@ -96,7 +96,7 @@ module BABYLON {
 		/*
 		* Private members
 		*/
-		private _mesh: Mesh;
+		private _mesh: AbstractMesh = null;
 		
 		private _refractionRTT: RenderTargetTexture;
 		private _reflectionRTT: RenderTargetTexture;
@@ -115,25 +115,14 @@ module BABYLON {
 		/**
 		* Constructor
 		*/
-		constructor(name: string, scene: Scene, sourceMesh: Mesh = null, renderTargetSize: Vector2 = new Vector2(512, 512)) {
+		constructor(name: string, scene: Scene, renderTargetSize: Vector2 = new Vector2(512, 512)) {
             super(name, scene);
 			
-			// Set this
-			this._mesh = sourceMesh;
-			
 			// Create render targets
 			this._createRenderTargets(scene, renderTargetSize);
         }
 		
         // Get / Set
-		public get mesh(): Mesh {
-			return this._mesh;
-		}
-		
-		public set mesh(mesh: Mesh) {
-			this._mesh = mesh;
-		}
-        
         public get refractionTexture(): RenderTargetTexture {
             return this._refractionRTT;
         }
@@ -166,7 +155,7 @@ module BABYLON {
         public getAlphaTestTexture(): BaseTexture {
             return null;
         }
-         
+        
         private _checkCache(scene: Scene, mesh?: AbstractMesh, useInstances?: boolean): boolean {
             if (!mesh) {
                 return true;
@@ -348,6 +337,8 @@ module BABYLON {
                     this._defines.INSTANCES = true;
                 }
             }
+            
+            this._mesh = mesh;
 
             // Get correct effect      
             if (!this._defines.isEqual(this._cachedDefines)) {
@@ -597,28 +588,38 @@ module BABYLON {
 			var mirrorMatrix = Matrix.Zero();
 			
 			this._refractionRTT.onBeforeRender = () => {
-				isVisible = this._mesh.isVisible;
-				this._mesh.isVisible = false;
-				
+                
+                if (this._mesh) {
+                    isVisible = this._mesh.isVisible;
+                    this._mesh.isVisible = false;
+                }
 				// Clip plane
 				clipPlane = scene.clipPlane;
-				//scene.clipPlane = Plane.FromPositionAndNormal(new Vector3(0, this._mesh.position.y, 0), new Vector3(0, 1, 0));
+                
+                var positiony = this._mesh ? this._mesh.position.y : 0.0;
+				scene.clipPlane = Plane.FromPositionAndNormal(new Vector3(0, positiony, 0), new Vector3(0, 1, 0));
 			};
 			
 			this._refractionRTT.onAfterRender = () => {
-				this._mesh.isVisible = isVisible;
-				
+                if (this._mesh) {
+				    this._mesh.isVisible = isVisible;
+                }
+                
 				// Clip plane
 				scene.clipPlane = clipPlane;
 			};
 			
 			this._reflectionRTT.onBeforeRender = () => {
-				isVisible = this._mesh.isVisible;
-				this._mesh.isVisible = false;
-				
+                if (this._mesh) {
+                    isVisible = this._mesh.isVisible;
+                    this._mesh.isVisible = false;
+                }
+                
 				// Clip plane
 				clipPlane = scene.clipPlane;
-				scene.clipPlane = Plane.FromPositionAndNormal(new Vector3(0, this._mesh.position.y, 0), new Vector3(0, -1, 0));
+                
+                var positiony = this._mesh ? this._mesh.position.y : 0.0;
+				scene.clipPlane = Plane.FromPositionAndNormal(new Vector3(0, positiony, 0), new Vector3(0, -1, 0));
 				
 				// Transform
 				Matrix.ReflectionToRef(scene.clipPlane, mirrorMatrix);
@@ -631,8 +632,10 @@ module BABYLON {
 			};
 			
 			this._reflectionRTT.onAfterRender = () => {
-				this._mesh.isVisible = isVisible;
-				
+                if (this._mesh) {
+				    this._mesh.isVisible = isVisible;
+                }
+                
 				// Clip plane
 				scene.clipPlane = clipPlane;
 				

+ 41 - 0
materialsLibrary/materials/water/readme.md

@@ -0,0 +1,41 @@
+# Water material
+
+## Using the water material
+
+The water material needs at least only a bump texture to render properly.
+Just create a new reference of the material and assign its bump texture:
+
+```
+var ground = BABYLON.Mesh.CreateGround("ground", 512, 512, 32, scene);
+
+var waterMaterial = new BABYLON.WaterMaterial("water_material", scene);
+waterMaterial.bumpTexture = new BABYLON.Texture("bump.png", scene); // Set the bump texture
+
+ground.material = waterMaterial;
+```
+
+To reflect and refract the world, you just have to add the wanted meshes to the render list:
+
+```
+waterMaterial.addToRenderList(skybox);
+waterMaterial.addToRenderList(mesh1);
+waterMaterial.addToRenderList(mesh2);
+// ... etc.
+```
+
+That's all.
+
+## Customize the water material
+
+You can customize special properties of the material:
+
+```
+waterMaterial.windForce = 45; // Represents the wind force applied on the water surface
+waterMaterial.waveHeight = 1.3; // Represents the height of the waves
+waterMaterial.bumpHeight = 0.3; // According to the bump map, represents the pertubation of reflection and refraction
+waterMaterial.windDirection = new BABYLON.Vector2(1.0, 1.0); // The wind direction on the water surface (on width and height)
+waterMaterial.waterColor = new BABYLON.Color3(0.1, 0.1, 0.6); // Represents the water color mixed with the reflected and refracted world
+waterMaterial.colorBlendFactor = 2.0; // Factor to determine how the water color is blended with the reflected and refracted world
+waterMaterial.waveLength = 0.1; // The lenght of waves. More the value small, more there are waves on the mesh
+```
+

+ 1 - 1
materialsLibrary/materials/water/water.vertex.fx

@@ -188,7 +188,7 @@ void main(void) {
 	vec3 p = position;
 	p.y += (sin(((p.x / 0.05) + time * 100.0)) * waveHeight * 5.0) + (cos(((p.z / 0.05) + time * 100.0)) * waveHeight * 5.0);
 	
-	gl_Position = viewProjection * finalWorld * vec4(p, 1.0);;
+	gl_Position = viewProjection * finalWorld * vec4(p, 1.0);
 
 	worldPos = viewProjection * finalWorld * vec4(position, 1.0);
 

+ 1 - 1
materialsLibrary/test/index.html

@@ -144,7 +144,7 @@
 				simple.diffuseTexture.uScale = 5;
 				simple.diffuseTexture.vScale = 5;
 				
-				var water = new BABYLON.WaterMaterial("water", scene, currentMesh);
+				var water = new BABYLON.WaterMaterial("water", scene);
 				water.backFaceCulling = false;
 				water.enableRenderTargets(false);
 				water.bumpTexture = new BABYLON.Texture("textures/waterbump.png", scene);