From 63980b45814833dabef5ba32ba0e743f9db9dd9f Mon Sep 17 00:00:00 2001 From: HK-SHAO Date: Sun, 30 Oct 2022 21:56:36 +0800 Subject: [PATCH] opt DrawBoard.gd --- app/shared/script/DrawBoard.gd | 17 ++-- app/shared/script/FreeCamera3D.gd | 2 +- app/test/scene/test_camera_scene.tscn | 4 +- app/test/scene/test_drawer_scene.tscn | 2 +- app/test/shader/test_drawer_line.gdshader | 110 ++++++++++++++++++++++ 5 files changed, 121 insertions(+), 14 deletions(-) diff --git a/app/shared/script/DrawBoard.gd b/app/shared/script/DrawBoard.gd index 138cfd2..a6382cb 100644 --- a/app/shared/script/DrawBoard.gd +++ b/app/shared/script/DrawBoard.gd @@ -4,8 +4,8 @@ extends Control @export var default_line: Line2D -@export_range(0, 2) var smooth: float = 0.2 -@export_range(0, 100000) var sensitivity: float = 5000 +@export_range(0, 2) var smooth: float = 0.5 +@export_range(0, 10000) var sensitivity: float = 5000 @onready var lines: Node2D = $Lines @@ -68,21 +68,20 @@ func add_point(point: Vector2) -> void: func update_ink(delta: float) -> void: var d := brush_position.distance_squared_to(ink_position) - var s := smooth * sensitivity / (sensitivity + d) + var s := smooth * sensitivity / (1 + sensitivity + d) - # ink_position = brush_position - var delta_position := brush_position - ink_position - # delta = exp(s * log(delta)) - delta = delta / ( (1 - delta) * s + delta ) + delta /= ((1 - delta) * s + delta) # delta = exp(s * log(delta)) delta = clamp(delta, 0.01, 1) + var delta_position := brush_position - ink_position ink_position += delta_position * delta func update_point() -> void: var d := ink_position.distance_squared_to(point_position) - point_position = ink_position - if d > 0: add_point(point_position) + if d > 1: + add_point(point_position) + point_position = ink_position func _process(delta: float) -> void: if is_left_pressed: diff --git a/app/shared/script/FreeCamera3D.gd b/app/shared/script/FreeCamera3D.gd index e9069e2..7a8b227 100644 --- a/app/shared/script/FreeCamera3D.gd +++ b/app/shared/script/FreeCamera3D.gd @@ -57,7 +57,7 @@ func _process(delta: float) -> void: if direction.length() != 0: - _velocity += (max_speed - _velocity) * delta * 0.1 * sensitivity + _velocity += (max_speed - _velocity) * delta * 0.01 * sensitivity _translate = direction * _velocity * delta else: _velocity = min_speed; diff --git a/app/test/scene/test_camera_scene.tscn b/app/test/scene/test_camera_scene.tscn index 14d8eb7..3bf5c01 100644 --- a/app/test/scene/test_camera_scene.tscn +++ b/app/test/scene/test_camera_scene.tscn @@ -153,10 +153,9 @@ script = ExtResource("4_mg5wx") camera = NodePath("../../Node3D/FreeCamera3D") [node name="Node3D" type="Node3D" parent="."] +visible = false [node name="Node3D" type="Node3D" parent="Node3D"] -process_mode = 4 -visible = false [node name="WorldEnvironment" type="WorldEnvironment" parent="Node3D/Node3D"] environment = SubResource("Environment_dw3fv") @@ -167,7 +166,6 @@ shadow_enabled = true [node name="MeshInstance3D" type="MeshInstance3D" parent="Node3D/Node3D"] transform = Transform3D(3, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0) -visible = false material_override = SubResource("ShaderMaterial_cgygt") mesh = SubResource("BoxMesh_782f1") skeleton = NodePath("../..") diff --git a/app/test/scene/test_drawer_scene.tscn b/app/test/scene/test_drawer_scene.tscn index 91195be..dc0da12 100644 --- a/app/test/scene/test_drawer_scene.tscn +++ b/app/test/scene/test_drawer_scene.tscn @@ -68,7 +68,7 @@ text = "123456" [node name="Line2D" type="Line2D" parent="."] material = SubResource("ShaderMaterial_lqxjv") -width = 20.0 +width = 30.0 joint_mode = 2 begin_cap_mode = 2 end_cap_mode = 2 diff --git a/app/test/shader/test_drawer_line.gdshader b/app/test/shader/test_drawer_line.gdshader index c3bf793..1fc1a8b 100644 --- a/app/test/shader/test_drawer_line.gdshader +++ b/app/test/shader/test_drawer_line.gdshader @@ -1,6 +1,116 @@ shader_type canvas_item; +// The MIT License +// Copyright © 2013 Inigo Quilez +// https://www.youtube.com/c/InigoQuilez +// https://iquilezles.org/ +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +// I've not seen anybody out there computing correct cell interior distances for Voronoi +// patterns yet. That's why they cannot shade the cell interior correctly, and why you've +// never seen cell boundaries rendered correctly. +// +// However, here's how you do mathematically correct distances (note the equidistant and non +// degenerated grey isolines inside the cells) and hence edges (in yellow): +// +// https://iquilezles.org/articles/voronoilines +// +// More Voronoi shaders: +// +// Exact edges: https://www.shadertoy.com/view/ldl3W8 +// Hierarchical: https://www.shadertoy.com/view/Xll3zX +// Smooth: https://www.shadertoy.com/view/ldB3zc +// Voronoise: https://www.shadertoy.com/view/Xd23Dh + +#define ANIMATE + +vec2 hash2( vec2 p ) +{ + // procedural white noise + return fract(sin(vec2(dot(p,vec2(127.1,311.7)),dot(p,vec2(269.5,183.3))))*43758.5453); +} + +vec3 voronoi( in vec2 x ) +{ + vec2 n = floor(x); + vec2 f = fract(x); + + //---------------------------------- + // first pass: regular voronoi + //---------------------------------- + vec2 mg, mr; + + float md = 8.0; + for( int j=-1; j<=1; j++ ) + for( int i=-1; i<=1; i++ ) + { + vec2 g = vec2(float(i),float(j)); + vec2 o = hash2( n + g ); + #ifdef ANIMATE + o = 0.5 + 0.5*sin( TIME + 6.2831*o ); + #endif + vec2 r = g + o - f; + float d = dot(r,r); + + if( d0.00001 ) + md = min( md, dot( 0.5*(mr+r), normalize(r-mr) ) ); + } + + return vec3( md, mr ); +} + +void mainImage( out vec4 fragColor, in vec2 uv ) +{ + vec2 p = uv; + + vec3 c = voronoi( 8.0*p ); + + // isolines + vec3 col = c.x*(0.5 + 0.5*sin(64.0*c.x))*vec3(1.0); + // borders + col = mix( vec3(1.0,0.6,0.0), col, smoothstep( 0.04, 0.07, c.x ) ); + // feature points + float dd = length( c.yz ); + col = mix( vec3(1.0,0.6,0.1), col, smoothstep( 0.0, 0.12, dd) ); + col += vec3(1.0,0.6,0.1)*(1.0-smoothstep( 0.0, 0.04, dd)); + + fragColor = vec4(col,1.0); +} + void fragment() { COLOR.r = 1.0; COLOR.g = 0.0; + + vec4 color; + + vec2 fragCoord = FRAGCOORD.xy; + vec2 resolution = 1. / SCREEN_PIXEL_SIZE.xy; + vec2 uv = (2. * fragCoord - resolution.xy) / min(resolution.x, resolution.y); + + mainImage(color, uv); + + + COLOR = color; }