瀏覽代碼

fixed pixel alignment on texels in DirectX

ncannasse 11 年之前
父節點
當前提交
8495091d55
共有 2 個文件被更改,包括 17 次插入8 次删除
  1. 4 0
      h2d/RenderContext.hx
  2. 13 8
      h3d/shader/Base2d.hx

+ 4 - 0
h2d/RenderContext.hx

@@ -42,6 +42,9 @@ class RenderContext {
 		currentObj = null;
 		bufPos = 0;
 		stride = 0;
+		// todo : we might prefer to auto-detect this by running a test and capturing its output
+		baseShader.pixelAlign = #if flash true #else false #end;
+		baseShader.halfPixelInverse.set(0.5 / engine.width, 0.5 / engine.height);
 		baseShaderList.next = null;
 		initShaders(baseShaderList);
 		engine.selectMaterial(pass);
@@ -69,6 +72,7 @@ class RenderContext {
 	public function setTarget( t : h3d.mat.Texture ) {
 		flush();
 		engine.setTarget(t);
+		baseShader.halfPixelInverse.set(0.5 / (t == null ? engine.width : t.width), 0.5 / (t == null ? engine.height : t.height));
 		begin();
 	}
 

+ 13 - 8
h3d/shader/Base2d.hx

@@ -3,13 +3,13 @@ package h3d.shader;
 class Base2d extends hxsl.Shader {
 
 	static var SRC = {
-		
+
 		@input var input : {
 			var position : Vec2;
 			var uv : Vec2;
 			var color : Vec4;
 		};
-		
+
 		var output : {
 			var position : Vec4;
 			var color : Vec4;
@@ -17,18 +17,21 @@ class Base2d extends hxsl.Shader {
 
 		@param var zValue : Float;
 		@param var texture : Sampler2D;
-		
+
 		var spritePosition : Vec4;
 		var absolutePosition : Vec4;
 		var pixelColor : Vec4;
 		var textureColor : Vec4;
 		@var var calculatedUV : Vec2;
-		
+
 		@const var isRelative : Bool;
 		@param var color : Vec4;
 		@param var absoluteMatrixA : Vec3;
 		@param var absoluteMatrixB : Vec3;
 
+		@const var pixelAlign : Bool;
+		@param var halfPixelInverse : Vec2;
+
 		function __init__() {
 			spritePosition = vec4(input.position, zValue, 1);
 			if( isRelative ) {
@@ -42,16 +45,18 @@ class Base2d extends hxsl.Shader {
 			textureColor = texture.get(calculatedUV);
 			pixelColor *= textureColor;
 		}
-		
+
 		function vertex() {
+			// http://msdn.microsoft.com/en-us/library/windows/desktop/bb219690(v=vs.85).aspx
+			if( pixelAlign ) absolutePosition.xy -= halfPixelInverse;
 			output.position = absolutePosition;
 		}
-		
+
 		function fragment() {
 			output.color = pixelColor;
 		}
-		
+
 	};
 
-	
+
 }