浏览代码

Merge branch 'webgl' of github.com:ncannasse/h3d into webgl

ncannasse 12 年之前
父节点
当前提交
2aa88ec669
共有 4 个文件被更改,包括 28 次插入9 次删除
  1. 4 1
      h3d/impl/Shader.hx
  2. 1 1
      h3d/impl/Shaders.hx
  3. 16 5
      h3d/impl/WebglDriver.hx
  4. 7 2
      h3d/mat/MeshMaterial.hx

+ 4 - 1
h3d/impl/Shader.hx

@@ -17,6 +17,7 @@ enum ShaderType {
 	Mat4;
 	Tex2d;
 	TexCube;
+	Byte4;
 	Struct( field : String, t : ShaderType );
 	Index( index : Int, t : ShaderType );
 }
@@ -64,14 +65,16 @@ class ShaderMacros {
 		var pos = Context.getLocalClass().get().pos;
 		var fields = Context.getBuildFields();
 		var hasVertex = false, hasFragment = false;
-		var r_uni = ~/uniform[ \t]+((lowp|mediump|highp)[ \t]+)?([A-Za-z0-9_]+)[ \t]+([A-Za-z0-9_]+)/;
+		var r_uni = ~/uniform[ \t]+((lowp|mediump|highp)[ \t]+)?([A-Za-z0-9_]+)[ \t]+([A-Za-z0-9_]+)[ \t]*(\/\*([A-Za-z0-9_]+)\*\/)?/;
 		function addUniforms( code : String ) {
 			while( r_uni.match(code) ) {
 				var name = r_uni.matched(4);
 				var type = r_uni.matched(3);
+				var hint = r_uni.matched(6);
 				code = r_uni.matchedRight();
 				var t = switch( type ) {
 				case "float": macro : Float;
+				case "vec4" if( hint == "byte4" ): macro : Int;
 				case "vec2", "vec3", "vec4": macro : h3d.Vector;
 				case "mat3", "mat4": macro : h3d.Matrix;
 				case "sampler2D", "samplerCube": macro : h3d.mat.Texture;

+ 1 - 1
h3d/impl/Shaders.hx

@@ -37,7 +37,7 @@ class PointShader extends h3d.impl.Shader {
 	";
 	static var FRAGMENT = "
 		varying mediump tuv;
-		uniform vec4 color;
+		uniform vec4 color /*byte4*/;
 		
 		void main(void) {
 			if( 1 - dot(tuv, tuv) < 0 ) discard;

+ 16 - 5
h3d/impl/WebglDriver.hx

@@ -189,7 +189,7 @@ class WebglDriver extends Driver {
 	
 	function typeSize( t : Shader.ShaderType ) {
 		return switch( t ) {
-		case Float: 1;
+		case Float, Byte4: 1;
 		case Vec2: 2;
 		case Vec3: 3;
 		case Vec4: 4;
@@ -270,7 +270,8 @@ class WebglDriver extends Driver {
 		// extract attributes from code (so we know the offset and stride)
 		var r = ~/attribute[ \t\r\n]+([A-Za-z0-9_]+)[ \t\r\n]+([A-Za-z0-9_]+)/;
 		var offset = 0;
-		while( r.match(code) ) {
+		var ccode = code;
+		while( r.match(ccode) ) {
 			var aname = r.matched(2);
 			var atype = decodeType(r.matched(1));
 			var a = amap.get(aname);
@@ -278,11 +279,12 @@ class WebglDriver extends Driver {
 			if( a != null )
 				inst.attribs.push( { name : aname, type : atype, etype : GL.FLOAT, size : size, index : a.index, offset : offset } );
 			offset += size;
-			code = r.matchedRight();
+			ccode = r.matchedRight();
 		}
 		inst.stride = offset;
 		
 		// list uniforms needed by shader
+		var allCode = code + gl.getShaderSource(fs);
 		var nuni = gl.getProgramParameter(p, GL.ACTIVE_UNIFORMS);
 		inst.uniforms = [];
 		var texIndex = -1;
@@ -295,6 +297,14 @@ class WebglDriver extends Driver {
 			switch( t ) {
 			case Tex2d, TexCube:
 				texIndex++;
+			case Vec4:
+				var r = new EReg(inf.name + "[ \\t]*\\/\\*([A-Za-z0-9_]+)\\*\\/", "");
+				if( r.match(allCode) )
+					switch( r.matched(1) ) {
+					case "byte4":
+						t = Byte4;
+					default:
+					}
 			default:
 			}
 			var name = inf.name;
@@ -319,10 +329,8 @@ class WebglDriver extends Driver {
 				index : texIndex,
 			});
 		}
-			
 		inst.program = p;
 		return inst;
-		
 	}
 
 	override function selectShader( shader : Shader ) : Bool {
@@ -383,6 +391,9 @@ class WebglDriver extends Driver {
 			var v = val[index];
 			if( v == null ) throw "Missing shader index " + index;
 			setUniform(v, u, t);
+		case Byte4:
+			var v : Int = val;
+			gl.uniform4f(u.loc, ((v >> 16) & 0xFF) / 255, ((v >> 8) & 0xFF) / 255, (v & 0xFF) / 255, (v >>> 24) / 255);
 		default:
 			throw "Unsupported uniform " + u.type;
 		}

+ 7 - 2
h3d/mat/MeshMaterial.hx

@@ -208,6 +208,11 @@ private class MeshShader extends h3d.impl.Shader {
 			if( hasZBias ) cst.push("#define hasZBias");
 		} else {
 			if( killAlpha ) cst.push("#define killAlpha");
+			if( colorAdd != null ) cst.push("#define hasColorAdd");
+			if( colorMul != null ) cst.push("#define hasColorMul");
+			if( colorMatrix != null ) cst.push("#define hasColorMatrix");
+			if( hasAlphaMap ) cst.push("#define hasAlphaMap");
+			if( hasGlow ) cst.push("#define hasGlow");
 			if( hasVertexColorAdd || lightSystem != null ) cst.push("#define hasFragColor");
 		}
 		return cst.join("\n");
@@ -339,8 +344,8 @@ private class MeshShader extends h3d.impl.Shader {
 		varying mediump vec4 tshadowPos;
 
 		uniform sampler2D tex;
-		uniform lowp vec3 colorAdd;
-		uniform lowp vec3 colorMul;
+		uniform lowp vec4 colorAdd;
+		uniform lowp vec4 colorMul;
 		uniform mediump mat4 colorMatrix;
 		
 		uniform lowp float killAlphaThreshold;