From 405df6d45895bb7d878592eb83310d1885c2c7b5 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 2 Oct 2023 15:45:13 +0200 Subject: [PATCH] Add random gem coloring --- ATTRIBUTION.md | 32 +++++++++++++++++++++++- color/Colored.gd | 14 +++++++++++ color/colored.tscn | 6 +++++ entity/gem/gem.gd | 11 -------- entity/gem/gem.tscn | 7 +++++- entity/gem/gem_shading_material.gdshader | 25 ++++++++++++++++++ entity/gem/gem_shading_material.tres | 7 ++++++ game/game.tscn | 4 +-- 8 files changed, 91 insertions(+), 15 deletions(-) create mode 100644 color/Colored.gd create mode 100644 color/colored.tscn delete mode 100644 entity/gem/gem.gd create mode 100644 entity/gem/gem_shading_material.gdshader create mode 100644 entity/gem/gem_shading_material.tres diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index 3ccb702..f9b8c3d 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -1,5 +1,35 @@ # Attribution +## Code + +- [Color Conversion Shader](https://github.com/paddy-exe/ShaderFunction-Extras/blob/682301c10a4cc4dbe53b81dade46948e80e25c6a/addons/ShaderFunction-Extras/Color/LicenseInfo.md) + +``` +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* 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. */ +/**************************************************************************/ +``` + ## Fonts -[Press Start Regular](https://www.1001fonts.com/press-start-font.html) by [codeman38](http://www.zone38.net/) +- [Press Start Regular](https://www.1001fonts.com/press-start-font.html) by [codeman38](http://www.zone38.net/) diff --git a/color/Colored.gd b/color/Colored.gd new file mode 100644 index 0000000..9f90406 --- /dev/null +++ b/color/Colored.gd @@ -0,0 +1,14 @@ +extends Node +class_name Colored + +@export var shader: Shader + +@onready var hue: float = Randomizer.rng.randf() +@onready var parent: Sprite2D = get_parent() + + +func _ready(): + var material = ShaderMaterial.new() + material.shader = shader + material.set_shader_parameter("hue", hue) + parent.material = material diff --git a/color/colored.tscn b/color/colored.tscn new file mode 100644 index 0000000..fb95406 --- /dev/null +++ b/color/colored.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://vkvtap437nnf"] + +[ext_resource type="Script" path="res://color/Colored.gd" id="1_wy58a"] + +[node name="Colored" type="Node"] +script = ExtResource("1_wy58a") diff --git a/entity/gem/gem.gd b/entity/gem/gem.gd deleted file mode 100644 index ca10a58..0000000 --- a/entity/gem/gem.gd +++ /dev/null @@ -1,11 +0,0 @@ -extends RigidBody2D - - -# Called when the node enters the scene tree for the first time. -func _ready(): - print($Valuable.value) - - -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta): - pass diff --git a/entity/gem/gem.tscn b/entity/gem/gem.tscn index f5579d9..9bce7f2 100644 --- a/entity/gem/gem.tscn +++ b/entity/gem/gem.tscn @@ -1,9 +1,11 @@ -[gd_scene load_steps=8 format=3 uid="uid://w2er6cmul5yx"] +[gd_scene load_steps=10 format=3 uid="uid://bawprh8kluilq"] [ext_resource type="PhysicsMaterial" uid="uid://c6kn1an85lccr" path="res://entity/coin_physics_material.tres" id="1_vmemd"] [ext_resource type="Texture2D" uid="uid://b21bsbo5f0ed7" path="res://entity/gem/gemstone.png" id="3_6jejy"] +[ext_resource type="PackedScene" uid="uid://vkvtap437nnf" path="res://color/colored.tscn" id="4_2cetq"] [ext_resource type="PackedScene" uid="uid://bk1vvq5rug01m" path="res://collector/collectible.tscn" id="4_idoeu"] [ext_resource type="PackedScene" uid="uid://ujpra0s1kpqi" path="res://value/valuable.tscn" id="5_7bd6c"] +[ext_resource type="Shader" path="res://entity/gem/gem_shading_material.gdshader" id="5_v6ppl"] [ext_resource type="Script" path="res://entity/gem/RandomValue.gd" id="6_3lulr"] [ext_resource type="AudioStream" uid="uid://br25ip30mu174" path="res://audio/conversione/mechanic_sound.wav" id="6_svrnw"] @@ -29,6 +31,9 @@ shape = SubResource("CapsuleShape2D_jkfas") texture_filter = 1 texture = ExtResource("3_6jejy") +[node name="Colored" parent="CollisionShape2D/Sprite" instance=ExtResource("4_2cetq")] +shader = ExtResource("5_v6ppl") + [node name="Collectible" parent="." instance=ExtResource("4_idoeu")] type = &"Silver" diff --git a/entity/gem/gem_shading_material.gdshader b/entity/gem/gem_shading_material.gdshader new file mode 100644 index 0000000..7773a1a --- /dev/null +++ b/entity/gem/gem_shading_material.gdshader @@ -0,0 +1,25 @@ +shader_type canvas_item; + +uniform float hue; + +vec3 hsv_to_rgb(vec3 color) { + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(color.xxx + K.xyz) * 6.0 - K.www); + return color.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), color.y); +} + +vec3 rgb_to_hsv(vec3 color) { + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(color.bg, K.wz), vec4(color.gb, K.xy), step(color.b, color.g)); + vec4 q = mix(vec4(p.xyw, color.r), vec4(color.r, p.yzx), step(p.x, color.r)); + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +void fragment() { + vec3 hsv = rgb_to_hsv(COLOR.rgb); + hsv.x = hue; + vec3 rgb = hsv_to_rgb(hsv); + COLOR.rgb = rgb; +} diff --git a/entity/gem/gem_shading_material.tres b/entity/gem/gem_shading_material.tres new file mode 100644 index 0000000..c660f00 --- /dev/null +++ b/entity/gem/gem_shading_material.tres @@ -0,0 +1,7 @@ +[gd_resource type="ShaderMaterial" load_steps=2 format=3 uid="uid://dbnxaw6yecsyv"] + +[ext_resource type="Shader" path="res://entity/gem/gem_shading_material.gdshader" id="1_h813y"] + +[resource] +shader = ExtResource("1_h813y") +shader_parameter/hue = 0.0 diff --git a/game/game.tscn b/game/game.tscn index 2d8c926..05c4bbe 100644 --- a/game/game.tscn +++ b/game/game.tscn @@ -11,7 +11,7 @@ [ext_resource type="PackedScene" uid="uid://du3005kwybbrh" path="res://entity/coin_gold/coin_gold.tscn" id="7_hh80i"] [ext_resource type="PackedScene" uid="uid://ratkps4plkhl" path="res://converters/copper_to_silver/copper_converter.tscn" id="7_ipeok"] [ext_resource type="PackedScene" uid="uid://but1bdslxp5jl" path="res://converters/silver_to_gold/silver_converter.tscn" id="8_0bbpi"] -[ext_resource type="PackedScene" path="res://entity/gem/gem.tscn" id="8_a3sk0"] +[ext_resource type="PackedScene" uid="uid://bawprh8kluilq" path="res://entity/gem/gem.tscn" id="8_a3sk0"] [ext_resource type="PackedScene" uid="uid://c2rlx7egl7xiv" path="res://collector/universal_collector.tscn" id="9_7op48"] [ext_resource type="PackedScene" uid="uid://c5w3b55aiui6c" path="res://collector/collector.tscn" id="9_evdhb"] [ext_resource type="Theme" uid="uid://ba5utvfhnxa5i" path="res://interface/interface_theme.tres" id="10_sayqn"] @@ -61,7 +61,7 @@ shape = SubResource("RectangleShape2D_4uksi") debug_color = Color(0, 0.6, 0.701961, 0) [node name="Timer" type="Timer" parent="TimeSpawner"] -wait_time = 0.02 +wait_time = 0.1 autostart = true [node name="ButtonSpawner" parent="." node_paths=PackedStringArray("target") instance=ExtResource("3_qwsty")]