瀏覽代碼

New procedural textures
Fixing CSG bug with transformations

David Catuhe 10 年之前
父節點
當前提交
9bed4edf65

+ 50 - 0
Babylon/Materials/textures/Procedurals/babylon.standardProceduralTexture.js

@@ -300,5 +300,55 @@ var BABYLON;
         return RoadProceduralTexture;
     })(BABYLON.ProceduralTexture);
     BABYLON.RoadProceduralTexture = RoadProceduralTexture;
+
+    var BrickProceduralTexture = (function (_super) {
+        __extends(BrickProceduralTexture, _super);
+        function BrickProceduralTexture(name, size, scene, fallbackTexture, generateMipMaps) {
+            _super.call(this, name, size, "brick", scene, fallbackTexture, generateMipMaps);
+            this._numberOfBricksHeight = 15;
+            this._numberOfBricksWidth = 5;
+
+            this.updateShaderUniforms();
+
+            // Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
+            this.refreshRate = 0;
+        }
+        BrickProceduralTexture.prototype.updateShaderUniforms = function () {
+            this.setFloat("numberOfBricksHeight", this._numberOfBricksHeight);
+            this.setFloat("numberOfBricksWidth", this._numberOfBricksWidth);
+        };
+
+        Object.defineProperty(BrickProceduralTexture.prototype, "numberOfBricksHeight", {
+            get: function () {
+                return this._numberOfBricksHeight;
+            },
+            enumerable: true,
+            configurable: true
+        });
+
+        Object.defineProperty(BrickProceduralTexture.prototype, "cloudColor", {
+            set: function (value) {
+                this._numberOfBricksHeight = value;
+                this.updateShaderUniforms();
+            },
+            enumerable: true,
+            configurable: true
+        });
+
+        Object.defineProperty(BrickProceduralTexture.prototype, "numberOfBricksWidth", {
+            get: function () {
+                return this._numberOfBricksWidth;
+            },
+            set: function (value) {
+                this._numberOfBricksHeight = value;
+                this.updateShaderUniforms();
+            },
+            enumerable: true,
+            configurable: true
+        });
+
+        return BrickProceduralTexture;
+    })(BABYLON.ProceduralTexture);
+    BABYLON.BrickProceduralTexture = BrickProceduralTexture;
 })(BABYLON || (BABYLON = {}));
 //# sourceMappingURL=babylon.standardProceduralTexture.js.map

+ 42 - 0
Babylon/Materials/textures/Procedurals/babylon.standardProceduralTexture.ts

@@ -252,4 +252,46 @@
         }
     }
 
+
+    export class BrickProceduralTexture extends ProceduralTexture {
+
+        private _numberOfBricksHeight: number = 15;
+        private _numberOfBricksWidth: number = 5;
+
+        constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
+            super(name, size, "brick", scene, fallbackTexture, generateMipMaps);
+
+            this.updateShaderUniforms();
+
+            // Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
+            this.refreshRate = 0;
+
+        }
+
+        public updateShaderUniforms() {
+
+            this.setFloat("numberOfBricksHeight", this._numberOfBricksHeight);
+            this.setFloat("numberOfBricksWidth", this._numberOfBricksWidth);
+        }
+
+
+        public get numberOfBricksHeight(): number {
+            return this._numberOfBricksHeight;
+        }
+
+        public set cloudColor(value: number) {
+            this._numberOfBricksHeight = value;
+            this.updateShaderUniforms();
+        }
+
+        public get numberOfBricksWidth(): number {
+            return this._numberOfBricksWidth;
+        }
+
+        public set numberOfBricksWidth(value: number) {
+            this._numberOfBricksHeight = value;
+            this.updateShaderUniforms();
+        }
+    }
+
 }

文件差異過大導致無法顯示
+ 2325 - 2325
Babylon/Math/babylon.math.ts


+ 10 - 10
Babylon/Mesh/babylon.csg.js

@@ -303,11 +303,11 @@
                 for (var i = subMeshes[sm].indexStart, il = subMeshes[sm].indexCount + subMeshes[sm].indexStart; i < il; i += 3) {
                     vertices = [];
                     for (var j = 0; j < 3; j++) {
-                        normal = new BABYLON.Vector3(normals[indices[i + j] * 3], normals[indices[i + j] * 3 + 1], normals[indices[i + j] * 3 + 2]);
+                        var sourceNormal = new BABYLON.Vector3(normals[indices[i + j] * 3], normals[indices[i + j] * 3 + 1], normals[indices[i + j] * 3 + 2]);
                         uv = new BABYLON.Vector2(uvs[indices[i + j] * 2], uvs[indices[i + j] * 2 + 1]);
-                        position = new BABYLON.Vector3(positions[indices[i + j] * 3], positions[indices[i + j] * 3 + 1], positions[indices[i + j] * 3 + 2]);
-                        BABYLON.Vector3.TransformCoordinatesToRef(position, matrix, position);
-                        BABYLON.Vector3.TransformNormalToRef(normal, matrix, normal);
+                        var sourcePosition = new BABYLON.Vector3(positions[indices[i + j] * 3], positions[indices[i + j] * 3 + 1], positions[indices[i + j] * 3 + 2]);
+                        position = BABYLON.Vector3.TransformCoordinates(sourcePosition, matrix);
+                        normal = BABYLON.Vector3.TransformNormal(sourceNormal, matrix);
 
                         vertex = new Vertex(position, normal, uv);
                         vertices.push(vertex);
@@ -506,17 +506,17 @@
                         vertex.copyFrom(polygon.vertices[polygonIndices[k]].pos);
                         normal.copyFrom(polygon.vertices[polygonIndices[k]].normal);
                         uv.copyFrom(polygon.vertices[polygonIndices[k]].uv);
-                        BABYLON.Vector3.TransformCoordinatesToRef(vertex, matrix, vertex);
-                        BABYLON.Vector3.TransformNormalToRef(normal, matrix, normal);
+                        var localVertex = BABYLON.Vector3.TransformCoordinates(vertex, matrix);
+                        var localNormal = BABYLON.Vector3.TransformNormal(normal, matrix);
 
-                        vertex_idx = vertice_dict[vertex.x + ',' + vertex.y + ',' + vertex.z];
+                        vertex_idx = vertice_dict[localVertex.x + ',' + localVertex.y + ',' + localVertex.z];
 
                         // Check if 2 points can be merged
-                        if (!(typeof vertex_idx !== 'undefined' && normals[vertex_idx * 3] === normal.x && normals[vertex_idx * 3 + 1] === normal.y && normals[vertex_idx * 3 + 2] === normal.z && uvs[vertex_idx * 2] === uv.x && uvs[vertex_idx * 2 + 1] === uv.y)) {
-                            vertices.push(vertex.x, vertex.y, vertex.z);
+                        if (!(typeof vertex_idx !== 'undefined' && normals[vertex_idx * 3] === localNormal.x && normals[vertex_idx * 3 + 1] === localNormal.y && normals[vertex_idx * 3 + 2] === localNormal.z && uvs[vertex_idx * 2] === uv.x && uvs[vertex_idx * 2 + 1] === uv.y)) {
+                            vertices.push(localVertex.x, localVertex.y, localVertex.z);
                             uvs.push(uv.x, uv.y);
                             normals.push(normal.x, normal.y, normal.z);
-                            vertex_idx = vertice_dict[vertex.x + ',' + vertex.y + ',' + vertex.z] = (vertices.length / 3) - 1;
+                            vertex_idx = vertice_dict[localVertex.x + ',' + localVertex.y + ',' + localVertex.z] = (vertices.length / 3) - 1;
                         }
 
                         indices.push(vertex_idx);

+ 12 - 14
Babylon/Mesh/babylon.csg.ts

@@ -306,11 +306,11 @@
                 for (var i = subMeshes[sm].indexStart, il = subMeshes[sm].indexCount + subMeshes[sm].indexStart; i < il; i += 3) {
                     vertices = [];
                     for (var j = 0; j < 3; j++) {
-                        normal = new BABYLON.Vector3(normals[indices[i + j] * 3], normals[indices[i + j] * 3 + 1], normals[indices[i + j] * 3 + 2]);
+                        var sourceNormal = new BABYLON.Vector3(normals[indices[i + j] * 3], normals[indices[i + j] * 3 + 1], normals[indices[i + j] * 3 + 2]);
                         uv = new BABYLON.Vector2(uvs[indices[i + j] * 2], uvs[indices[i + j] * 2 + 1]);
-                        position = new BABYLON.Vector3(positions[indices[i + j] * 3], positions[indices[i + j] * 3 + 1], positions[indices[i + j] * 3 + 2]);
-                        BABYLON.Vector3.TransformCoordinatesToRef(position, matrix, position);
-                        BABYLON.Vector3.TransformNormalToRef(normal, matrix, normal);
+                        var sourcePosition = new BABYLON.Vector3(positions[indices[i + j] * 3], positions[indices[i + j] * 3 + 1], positions[indices[i + j] * 3 + 2]);
+                        position = BABYLON.Vector3.TransformCoordinates(sourcePosition, matrix);
+                        normal = BABYLON.Vector3.TransformNormal(sourceNormal, matrix);
 
                         vertex = new Vertex(position, normal, uv);
                         vertices.push(vertex);
@@ -522,24 +522,22 @@
                         vertex.copyFrom(polygon.vertices[polygonIndices[k]].pos);
                         normal.copyFrom(polygon.vertices[polygonIndices[k]].normal);
                         uv.copyFrom(polygon.vertices[polygonIndices[k]].uv);
-                        BABYLON.Vector3.TransformCoordinatesToRef(vertex, matrix, vertex);
-                        BABYLON.Vector3.TransformNormalToRef(normal, matrix, normal);
+                        var localVertex = BABYLON.Vector3.TransformCoordinates(vertex, matrix);
+                        var localNormal = BABYLON.Vector3.TransformNormal(normal, matrix);
 
-
-
-                        vertex_idx = vertice_dict[vertex.x + ',' + vertex.y + ',' + vertex.z];
+                        vertex_idx = vertice_dict[localVertex.x + ',' + localVertex.y + ',' + localVertex.z];
 
                         // Check if 2 points can be merged
                         if (!(typeof vertex_idx !== 'undefined' &&
-                            normals[vertex_idx * 3] === normal.x &&
-                            normals[vertex_idx * 3 + 1] === normal.y &&
-                            normals[vertex_idx * 3 + 2] === normal.z &&
+                            normals[vertex_idx * 3] === localNormal.x &&
+                            normals[vertex_idx * 3 + 1] === localNormal.y &&
+                            normals[vertex_idx * 3 + 2] === localNormal.z &&
                             uvs[vertex_idx * 2] === uv.x &&
                             uvs[vertex_idx * 2 + 1] === uv.y)) {
-                            vertices.push(vertex.x, vertex.y, vertex.z);
+                            vertices.push(localVertex.x, localVertex.y, localVertex.z);
                             uvs.push(uv.x, uv.y);
                             normals.push(normal.x, normal.y, normal.z);
-                            vertex_idx = vertice_dict[vertex.x + ',' + vertex.y + ',' + vertex.z] = (vertices.length / 3) - 1;
+                            vertex_idx = vertice_dict[localVertex.x + ',' + localVertex.y + ',' + localVertex.z] = (vertices.length / 3) - 1;
                         }
 
                         indices.push(vertex_idx);

+ 81 - 0
Babylon/Shaders/brick.fragment.fx

@@ -0,0 +1,81 @@
+#ifdef GL_ES
+precision highp float;
+#endif
+
+varying vec2 vPosition;
+varying vec2 vUV;
+
+uniform float numberOfBricksHeight;
+uniform float numberOfBricksWidth;
+
+float rand(vec2 n) {
+	return fract(cos(dot(n, vec2(12.9898, 4.1414))) * 43758.5453);
+}
+
+float noise(vec2 n) {
+	const vec2 d = vec2(0.0, 1.0);
+	vec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n));
+	return mix(mix(rand(b), rand(b + d.yx), f.x), mix(rand(b + d.xy), rand(b + d.yy), f.x), f.y);
+}
+
+float fbm(vec2 n) {
+	float total = 0.0, amplitude = 1.0;
+	for (int i = 0; i < 4; i++) {
+		total += noise(n) * amplitude;
+		n += n;
+		amplitude *= 0.5;
+	}
+	return total;
+}
+
+float round(float number){
+	return sign(number)*floor(abs(number) + 0.5);
+}
+
+void main(void)
+{
+	vec3 brick = vec3(0.77, 0.47, 0.40);
+	vec3 joint = vec3(0.72, 0.72, 0.72);
+
+	float brickW = 1.0 / numberOfBricksWidth;
+	float brickH = 1.0 / numberOfBricksHeight;
+	float jointWPercentage = 0.01;
+	float jointHPercentage = 0.05;
+
+	vec3 color = brick;
+
+
+	float yi = vUV.y / brickH;
+	float nyi = round(yi);
+
+	float xi = vUV.x / brickW;
+
+	if (mod(floor(yi), 2.0) == 0.0){
+		xi = xi - 0.5;
+	}
+
+	float nxi = round(xi);
+
+
+
+	if (yi < nyi + jointHPercentage && yi > nyi - jointHPercentage){
+		color = mix(joint, vec3(0.37, 0.25, 0.25), (yi - nyi) / jointHPercentage + 0.2);
+	}
+	else if (xi < nxi + jointWPercentage && xi > nxi - jointWPercentage){
+		color = mix(joint, vec3(0.44, 0.44, 0.44), (xi - nxi) / jointWPercentage + 0.2);
+	}
+	else {
+		float momo = mod(floor(yi) + floor(xi), 3.0);
+
+		if (momo == 0.0)
+			color = mix(color, vec3(0.33, 0.33, 0.33), 0.3);
+		else if (momo == 2.0)
+			color = mix(color, vec3(0.11, 0.11, 0.11), 0.3);
+
+
+		//color = mix(color, vec3(0.1, 0.1, 0.1), fbm(vUV * 64.0));
+	}
+
+
+	gl_FragColor = vec4(color, 1.0);
+}

+ 98 - 92
Babylon/Shaders/rock.fragment.fx

@@ -5,110 +5,116 @@ precision highp float;
 varying vec2 vPosition;
 varying vec2 vUV;
 
-uniform float ampScale;
-uniform float ringScale;
-uniform vec3 woodColor1;
-uniform vec3 woodColor2;
+vec3 hash(vec3 x)
+{
+	x = vec3(dot(x, vec3(127.1, 311.7, 74.7)),
+		dot(x, vec3(269.5, 183.3, 246.1)),
+		dot(x, vec3(113.5, 271.9, 124.6)));
 
-
-float rand(vec2 n) {
-	return fract(cos(dot(n, vec2(12.9898, 4.1414))) * 43758.5453);
+	return fract(sin(x)*43758.5453123);
 }
 
-float noise(vec2 n) {
-	const vec2 d = vec2(0.0, 1.0);
-	vec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n));
-	return mix(mix(rand(b), rand(b + d.yx), f.x), mix(rand(b + d.xy), rand(b + d.yy), f.x), f.y);
-}
+// returns closest, second closest, and cell id
+vec3 voronoi(in vec3 x)
+{
+	vec3 p = floor(x);
+	vec3 f = fract(x);
+
+	float id = 0.0;
+	vec2 res = vec2(100.0);
+	for (int k = -1; k <= 1; k++)
+		for (int j = -1; j <= 1; j++)
+			for (int i = -1; i <= 1; i++)
+			{
+		vec3 b = vec3(float(i), float(j), float(k));
+		vec3 r = vec3(b) - f + hash(p + b);
+		float d = dot(r, r);
+
+		if (d < res.x)
+		{
+			id = dot(p + b, vec3(1.0, 57.0, 113.0));
+			res = vec2(d, res.x);
+		}
+		else if (d < res.y)
+		{
+			res.y = d;
+		}
+			}
 
-float fbm(vec2 n) {
-	float total = 0.0, amplitude = 1.0;
-	for (int i = 0; i < 4; i++) {
-		total += noise(n) * amplitude;
-		n += n;
-		amplitude *= 0.5;
-	}
-	return total;
+	return vec3(sqrt(res), abs(id));
 }
 
+const mat3 m = mat3(0.00, 0.80, 0.60,
+	-0.80, 0.36, -0.48,
+	-0.60, -0.48, 0.64);
+
+void main(void)
+{
+	vec2 p = vUV;
+
+	// camera movement	
+	float an = 0.5*0.1;
+	vec3 ro = vec3(2.5*cos(an), 1.0, 2.5*sin(an));
+	vec3 ta = vec3(0.0, 1.0, 0.0);
+	// camera matrix
+	vec3 ww = normalize(ta - ro);
+	vec3 uu = normalize(cross(ww, vec3(0.0, 1.0, 0.0)));
+	vec3 vv = normalize(cross(uu, ww));
+	// create view ray
+	vec3 rd = normalize(p.x*uu + p.y*vv + 1.5*ww);
+
+	// sphere center	
+	vec3 sc = vec3(0.0, 1.0, 0.0);
+
+	// raytrace
+	float tmin = 10000.0;
+	vec3  nor = vec3(0.0);
+	float occ = 1.0;
+	vec3  pos = vec3(0.0);
+
+	// raytrace-plane
+	float h = (0.0 - ro.y) / rd.y;
+	if (h>0.0)
+	{
+		tmin = h;
+		nor = vec3(0.0, 1.0, 0.0);
+		pos = ro + h*rd;
+		vec3 di = sc - pos;
+		float l = length(di);
+		occ = 1.0 - dot(nor, di / l)*1.0*1.0 / (l*l);
+	}
 
-
-void main(void) {
-
-
-	/*vec3 rock = vec3(0.53, 0.53, 0.53);
-	vec3 roll = vec3(0.18, 0.18, 0.18);
-
-	float ratioy = mod(vUV.y * 50.0, fbm(vUV * 8.0));
-
-	rock = rock * ratioy;
-	roll = roll * ratioy;
-
-	vec3 stone = mix(rock, roll, 0.5);
-
-
-	gl_FragColor = vec4(stone, 1.0);*/
-
-
-	/* voronoi.frag */
-
-
-	vec2 seeds[16];
-	vec3 colors[16];
-
-	colors[0] = vec3(0.53, 0.53, 0.53);
-	colors[1] = vec3(0.28, 0.28, 0.28);
-	colors[2] = vec3(0.23, 0.23, 0.23);
-	colors[3] = vec3(0.38, 0.38, 0.38);
-	colors[4] = vec3(0.63, 0.63, 0.63);
-	colors[5] = vec3(0.78, 0.78, 0.78);
-	colors[6] = vec3(0.93, 0.93, 0.93);
-	colors[7] = vec3(0.73, 0.73, 0.73);
-	colors[8] = vec3(0.43, 0.43, 0.43);
-	colors[9] = vec3(0.11, 0.11, 0.11);
-	colors[10] = vec3(0.12, 0.12, 0.12);
-	colors[11] = vec3(0.64, 0.64, 0.64);
-	colors[12] = vec3(0.79, 0.79, 0.79);
-	colors[13] = vec3(0.43, 0.43, 0.43);
-	colors[14] = vec3(0.21, 0.21, 0.21);
-	colors[15] = vec3(0.37, 0.37, 0.37);
-
-	seeds[0] = vec2(0.1, 0.9);
-	seeds[1] = vec2(0.2, 0.8);
-	seeds[2] = vec2(0.3, 0.7);
-	seeds[3] = vec2(0.4, 0.4);
-	seeds[4] = vec2(0.5, 0.5);
-	seeds[5] = vec2(0.6, 0.3);
-	seeds[6] = vec2(0.7, 0.2);
-	seeds[7] = vec2(0.8, 0.3);
-	seeds[8] = vec2(0.9, 0.4);
-	seeds[9] = vec2(1.0, 0.3);
-	seeds[10] = vec2(0.5, 0.4);
-	seeds[11] = vec2(0.7, 0.8);
-	seeds[12] = vec2(0.5, 0.3);
-	seeds[13] = vec2(0.7, 0.7);
-	seeds[14] = vec2(0.3, 0.3);
-	seeds[15] = vec2(0.7, 0.7);
-
-	float dist = distance(seeds[0], vPosition);
-	vec3 color = colors[0];
-
-	float hotpoint = 1.0;
-	float current = 1.0;
-	for (int i = 1; i < 16; i++) {
-		current = distance(seeds[i], vPosition);
-		if (current < dist) {
-			hotpoint++;
-			color = colors[i];
-			dist = current;
+	// raytrace-sphere
+	vec3  ce = ro - sc;
+	float b = dot(rd, ce);
+	float c = dot(ce, ce) - 1.0;
+	h = b*b - c;
+	if (h>0.0)
+	{
+		h = -b - sqrt(h);
+		if (h<tmin)
+		{
+			tmin = h;
+			nor = normalize(ro + h*rd - sc);
+			occ = 0.5 + 0.5*nor.y;
 		}
 	}
 
-	if (hotpoint > 3.0)
-		color = mix(vec3(0.39, 0.32, 0), color, 1.0 / hotpoint);
+	// shading/lighting	
+	vec3 col = vec3(0.9);
+	if (tmin<100.0)
+	{
+		pos = ro + tmin*rd;
+
+		float f = voronoi(4.0*pos).x;
 
-	gl_FragColor = vec4(color, 1.0);
+		f *= occ;
+		col = vec3(f*1.2);
+		col = mix(col, vec3(0.9), 1.0 - exp(-0.003*tmin*tmin));
+	}
 
+	col = sqrt(col);
 
 
+	gl_FragColor = vec4(col, 1.0);
 }

文件差異過大導致無法顯示
+ 62 - 11
babylon.2.0-alpha.debug.js


文件差異過大導致無法顯示
+ 3 - 3
babylon.2.0-alpha.js


+ 9 - 0
babylon.2.0.d.ts

@@ -1954,6 +1954,15 @@ declare module BABYLON {
     class RoadProceduralTexture extends ProceduralTexture {
         constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean);
     }
+    class BrickProceduralTexture extends ProceduralTexture {
+        private _numberOfBricksHeight;
+        private _numberOfBricksWidth;
+        constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean);
+        public updateShaderUniforms(): void;
+        public numberOfBricksHeight : number;
+        public cloudColor : number;
+        public numberOfBricksWidth : number;
+    }
 }
 declare module BABYLON {
     class Color3 {