浏览代码

working on std140 layout

Benjamin Guignabert 8 年之前
父节点
当前提交
73c3316301

文件差异内容过多而无法显示
+ 6448 - 6447
dist/preview release/babylon.d.ts


文件差异内容过多而无法显示
+ 6448 - 6447
dist/preview release/babylon.module.d.ts


+ 3 - 1
index.html

@@ -61,10 +61,12 @@
 						sphere.position.y = 1+ 2*i;
 						sphere.material = new BABYLON.StandardMaterial("test");
 						sphere.material.diffuseColor = new BABYLON.Color3(Math.random(), Math.random(), Math.random());
+						sphere.material.emissiveColor = new BABYLON.Color3(Math.random(), Math.random(), Math.random());
+						sphere.material.specularColor = new BABYLON.Color3(Math.random(), Math.random(), Math.random());
 						window.sphere = sphere;
 						// sphere.material.diffuseTexture = new BABYLON.Texture("test.jpeg", scene);
 						var t = 0;
-							sphere.material.freeze();
+							// sphere.material.freeze();
 						scene.registerAfterRender(function() {
 							sphere.material.diffuseColor = new BABYLON.Color3(Math.abs(Math.sin(t/100)), Math.abs(Math.cos(t/200)), Math.abs(Math.sin(t/50)));
 							t++;

+ 3 - 5
src/Materials/babylon.standardMaterial.ts

@@ -748,6 +748,7 @@ module BABYLON {
             this._uniformBuffer.addUniform("vEmissiveInfos", 2);
             this._uniformBuffer.addUniform("vLightmapInfos", 2);
             this._uniformBuffer.addUniform("vSpecularInfos", 2);
+            // this._uniformBuffer.addUniform("fill", 2);
             this._uniformBuffer.addUniform("vBumpInfos", 3);
 
             this._uniformBuffer.addUniform("diffuseMatrix", 16);
@@ -758,15 +759,12 @@ module BABYLON {
             this._uniformBuffer.addUniform("lightmapMatrix", 16);
             this._uniformBuffer.addUniform("specularMatrix", 16);
             this._uniformBuffer.addUniform("bumpMatrix", 16);
-
             this._uniformBuffer.addUniform("refractionMatrix", 16);
             this._uniformBuffer.addUniform("vRefractionInfos", 4);
-            this._uniformBuffer.addUniform("pointSize", 1);
-
-            this._uniformBuffer.addUniform("vSpecularColor", 3);
+            this._uniformBuffer.addUniform("vSpecularColor", 4);
             this._uniformBuffer.addUniform("vEmissiveColor", 3);
             this._uniformBuffer.addUniform("vDiffuseColor", 4);
-
+            this._uniformBuffer.addUniform("pointSize", 1);
         }
 
         public unbind(): void {

+ 34 - 2
src/Materials/babylon.uniformBuffer.ts

@@ -43,8 +43,38 @@ module BABYLON {
 
         // Methods
         private _adaptSizeToLayout(size: number): number {
-            // In std140 layout, uniform size must be multiple of 4 floats
-            return Math.ceil(size / 4) * 4;
+            // std140 layout
+            var uniformSize;
+            if (size <= 2) {
+                uniformSize = size;
+            } else if (size == 2) {
+                uniformSize = Math.ceil(size / 4) * 4;
+            }
+
+            return uniformSize;
+        }
+
+        private _fillAlignment(size: number) {
+            // std140 layout
+
+            // TODO : Debug !!
+            
+            var alignment;
+            if (size <= 2) {
+                alignment = size;
+            } else if (size == 2) {
+                alignment = Math.ceil(size / 4) * 4;
+            }
+
+            if ((this._uniformLocationPointer % alignment) !== 0) {
+                var oldPointer = this._uniformLocationPointer;
+                this._uniformLocationPointer += alignment - (this._uniformLocationPointer % alignment);
+                var diff = this._uniformLocationPointer - oldPointer;
+
+                for (var i = 0; i < diff; i++) {
+                      this._data.push(0); 
+                }
+            }
         }
 
         public addUniform(name: string, size: number | number[]) {
@@ -72,9 +102,11 @@ module BABYLON {
 
             this._uniformNames.push(name);
             this._uniformSizes.push(size);
+            this._fillAlignment(size);
             this._uniformLocations.push(this._uniformLocationPointer);
             this._uniformLocationPointer += size;
 
+
             for (var i = 0; i < size; i++) {
                 this._data.push(data[i]);
             }

+ 2 - 1
src/Shaders/ShadersInclude/pointCloudVertexDeclaration.fx

@@ -1 +1,2 @@
-
+#ifdef POINTSIZE
+#endif

+ 4 - 4
src/Shaders/default.fragment.fx

@@ -1,6 +1,7 @@
 layout(std140, column_major) uniform;
 uniform Material
 {
+	
 	vec4 diffuseLeftColor;
 	vec4 diffuseRightColor;
 	vec4 opacityParts;
@@ -28,13 +29,14 @@ uniform Material
 	mat4 bumpMatrix;
 	mat4 refractionMatrix;
 	vec4 vRefractionInfos;
-	float pointSize;
-	vec3 vSpecularColor;
+	vec4 vSpecularColor;
 	vec3 vEmissiveColor;
 	vec4 vDiffuseColor;
+	float pointSize;
 } uMaterial;
 
 uniform vec3 vEyePosition;
+uniform vec3 vAmbientColor;
 
 #ifdef BUMP
 #extension GL_OES_standard_derivatives : enable
@@ -378,7 +380,6 @@ void main(void) {
 	alpha = clamp(alpha + dot(reflectionColor, vec3(0.3, 0.59, 0.11)), 0., 1.);
 #endif
 
-	// Composition
 #ifdef EMISSIVEASILLUMINATION
 	vec4 color = vec4(clamp(finalDiffuse * baseAmbientColor + finalSpecular + reflectionColor + emissiveColor + refractionColor, 0.0, 1.0), alpha);
 #else
@@ -406,6 +407,5 @@ void main(void) {
 #ifdef CAMERACOLORCURVES
 	color.rgb = applyColorCurves(color.rgb);
 #endif
-
 	gl_FragColor = color;
 }

+ 3 - 2
src/Shaders/default.vertex.fx

@@ -2,6 +2,7 @@
 
 uniform Material
 {
+	
 	vec4 diffuseLeftColor;
 	vec4 diffuseRightColor;
 	vec4 opacityParts;
@@ -29,10 +30,10 @@ uniform Material
 	mat4 bumpMatrix;
 	mat4 refractionMatrix;
 	vec4 vRefractionInfos;
-	float pointSize;
-	vec3 vSpecularColor;
+	vec4 vSpecularColor;
 	vec3 vEmissiveColor;
 	vec4 vDiffuseColor;
+	float pointSize;
 } uMaterial;
 
 // Attributes