From aea2e491c3cfb5e61ee626cb66083357d66cbf07 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 12 Dec 2017 03:27:56 +0900 Subject: [PATCH] graphics: Temporary hack for mobile browers roundTexel doesn't work well on moible browsers --- internal/graphics/shader.go | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/internal/graphics/shader.go b/internal/graphics/shader.go index 01ac96192..5a88a50cb 100644 --- a/internal/graphics/shader.go +++ b/internal/graphics/shader.go @@ -14,6 +14,12 @@ package graphics +import ( + "strings" + + "github.com/hajimehoshi/ebiten/internal/web" +) + type shaderId int const ( @@ -22,11 +28,19 @@ const ( ) func shader(id shaderId) string { - return shaders[id] + s := shaders[id] + defs := []string{} + if web.IsMobileBrowser() { + defs = append(defs, "#define IS_MOBILE_BROWSER") + } + s = strings.Replace(s, "{{Definitions}}", strings.Join(defs, "\n"), -1) + return s } var shaders = map[shaderId]string{ shaderVertexModelview: ` +{{Definitions}} + uniform mat4 projection_matrix; attribute vec2 vertex; attribute vec4 tex_coord; @@ -60,17 +74,28 @@ precision mediump float; #define highp #endif +{{Definitions}} + uniform sampler2D texture; uniform mat4 color_matrix; uniform vec4 color_matrix_translation; -uniform int filter_type; uniform vec2 source_size; +uniform int filter_type; varying vec2 varying_tex_coord; varying vec2 varying_tex_coord_min; varying vec2 varying_tex_coord_max; vec2 roundTexel(vec2 p) { +#if defined(IS_MOBILE_BROWSER) + // This is a temporary hack: on mobile browsers like Android Chrome, + // the code to round a float might cause unexpected behavior. + // This is confirmed with rendering a relatively large (e.g. 4096x512) image + // with specifying a source rectangle near its edges. + // TODO: examples/moire doesn't work anyway. + // TODO: Does this function work on an Android native app? + return p; +#else vec2 factor = 1.0 / (source_size * 256.0); if (factor.x > 0.0) { p.x -= mod(p.x + factor.x * 0.5, factor.x) - factor.x * 0.5; @@ -79,6 +104,7 @@ vec2 roundTexel(vec2 p) { p.y -= mod(p.y + factor.y * 0.5, factor.y) - factor.y * 0.5; } return p; +#endif } vec4 getColorAt(vec2 pos) {