Made gridline shader use data directly

This commit is contained in:
Luke Hubmayer-Werner 2020-05-23 16:21:48 +09:30
parent b8ab57f6ea
commit 37f4aa70fd
3 changed files with 64 additions and 29 deletions

View File

@ -3,28 +3,57 @@ shader_type canvas_item;
uniform float rows = 44.0; // Really int but don't want to recast all the time
uniform float cols = 63.0; // ^
uniform vec4 color_unmarked: hint_color = vec4(0.5, 0.5, 0.5, 1.0);
uniform vec4 color_marked: hint_color = vec4(0.0, 0.0, 1.0, 1.0);
uniform vec4 color_crossed: hint_color = vec4(1.0, 0.0, 0.0, 1.0);
uniform float line_thickness = 0.1; // Cell scale
uniform float line_offset = 0.05; // Should be half of thickness to be centered, but might need tweaking for small values
void fragment(){
vec2 frag_pos = UV * vec2(cols, rows);
vec2 frag_marginal = fract(frag_pos+0.05);
vec2 frag_marginal_2 = fract(frag_pos-0.4);
vec2 grid_data = texture(TEXTURE, (frag_pos+0.05)/vec2(cols+1.0, rows+1.0)).rg;
bvec2 frag_marginal = lessThan(fract(frag_pos+line_offset), vec2(line_thickness));
bvec2 frag_marginal_x = lessThan(fract(frag_pos-0.425), vec2(0.15));
// vec2 grid_data = texture(TEXTURE, (frag_pos+line_offset)/vec2(cols+1.0, rows+1.0)).rg;
// float grid_data_s = texture(TEXTURE, ((frag_pos+line_offset)/vec2(cols+1.0, rows+1.0)).yx).a*128.0; //*17.0;
// COLOR = vec4(grid_data_s, grid_data_s, grid_data_s, 1.0);
int grid_data_s = int(texture(TEXTURE, ((frag_pos+line_offset)/vec2(cols+1.0, rows+1.0)).yx).r*255.0);
ivec2 grid_data = ivec2(grid_data_s%4, grid_data_s/4);
COLOR = vec4(0.0);
if (frag_marginal.x < 0.1){
if (grid_data.g >= 0.99){
if (frag_marginal_2.y <= 0.2)
COLOR = vec4(1.0, 0.0, 0.0, 1.0);
}else if (grid_data.g >= 0.49){
COLOR = vec4(0.0, 0.0, 1.0, 1.0);
if (frag_marginal.x){
if (grid_data.g >= 2){
if (frag_marginal_x.y)
COLOR = color_crossed;
}else if (grid_data.g == 1){
COLOR = color_marked;
}else
COLOR = vec4(0.5, 0.5, 0.5, 1.0);
COLOR = color_unmarked;
}
if (frag_marginal.y < 0.1){
if (grid_data.r >= 0.99){
if (frag_marginal_2.x <= 0.2)
COLOR = vec4(1.0, 0.0, 0.0, 1.0);
}else if (grid_data.r >= 0.49){
COLOR = vec4(0.0, 0.0, 1.0, 1.0);
if (frag_marginal.y){
if (grid_data.r >= 2){
if (frag_marginal_x.x)
COLOR = color_crossed;
}else if (grid_data.r == 1){
COLOR = color_marked;
}else
COLOR = vec4(0.5, 0.5, 0.5, 1.0);
COLOR = color_unmarked;
}
// if (frag_marginal.x){
// if (grid_data.g >= 0.99){
// if (frag_marginal_x.y)
// COLOR = color_crossed;
// }else if (grid_data.g >= 0.49){
// COLOR = color_marked;
// }else
// COLOR = color_unmarked;
// }
// if (frag_marginal.y){
// if (grid_data.r >= 0.99){
// if (frag_marginal_x.x)
// COLOR = color_crossed;
// }else if (grid_data.r >= 0.49){
// COLOR = color_marked;
// }else
// COLOR = color_unmarked;
// }
}

View File

@ -3,10 +3,15 @@
[ext_resource path="res://scripts/GameField.gd" type="Script" id=1]
[ext_resource path="res://GridLines.shader" type="Shader" id=2]
[sub_resource type="ShaderMaterial" id=2]
[sub_resource type="ShaderMaterial" id=1]
shader = ExtResource( 2 )
shader_param/rows = 44.0
shader_param/cols = 63.0
shader_param/color_unmarked = Color( 0.5, 0.5, 0.5, 1 )
shader_param/color_marked = Color( 0, 0, 1, 1 )
shader_param/color_crossed = Color( 1, 0, 0, 1 )
shader_param/line_thickness = 0.1
shader_param/line_offset = 0.05
[node name="Control" type="Control"]
anchor_right = 1.0
@ -31,7 +36,7 @@ __meta__ = {
}
[node name="TextureRect" type="TextureRect" parent="GameField"]
material = SubResource( 2 )
material = SubResource( 1 )
margin_top = -2.0
margin_right = 40.0
margin_bottom = 38.0

View File

@ -13,7 +13,7 @@ const IntArray2D = Common.IntArray2D
onready var cell_numbers: ByteArray2D
const NUMBER_NONE := 255
onready var cell_colors: ByteArray2D
onready var corner_marks: IntArray2D
onready var corner_marks: ByteArray2D #IntArray2D
var cell_image := Image.new()
var cell_texture := ImageTexture.new()
@ -81,14 +81,15 @@ func regenerate_cell_texture() -> void:
cell_texture.create_from_image(cell_image, 0)
func regenerate_grid_texture() -> void:
# grid_image.create_from_data(rows, cols, false, Image.FORMAT_R8, cell_corners._array)
grid_image.create(cols+1, rows+1, false, Image.FORMAT_RGBA8)
grid_image.lock()
for row in rows+1:
for col in cols+1:
var flags = corner_marks.get_cell(row, col)
grid_image.set_pixel(col, row, Color(float(flags&3)/2.0, float((flags>>2)&3)/2.0, 0.0))
grid_image.unlock()
grid_image.create_from_data(rows+1, cols+1, false, Image.FORMAT_L8, corner_marks._array)
# grid_image.create(cols+1, rows+1, false, Image.FORMAT_R8)
# grid_image.lock()
# for row in rows+1:
# for col in cols+1:
# var flags = corner_marks.get_cell(row, col)
## grid_image.set_pixel(col, row, Color(float(flags&3)/2.0, float((flags>>2)&3)/2.0, 0.0))
# grid_image.set_pixel(col, row, Color(float(flags&3)/2.0, float((flags>>2)&3)/2.0, 0.0))
# grid_image.unlock()
grid_texture.create_from_image(grid_image, 0)
$TextureRect.set_texture(grid_texture)
@ -180,7 +181,7 @@ func load_puzzle(filename: String):
cols = int(file.get_line())
cell_numbers = ByteArray2D.new(rows, cols)
cell_colors = ByteArray2D.new(rows, cols)
corner_marks = IntArray2D.new(rows+1, cols+1, 0)
corner_marks = ByteArray2D.new(rows+1, cols+1, 0)
var row = 0
var stage = 0