Skip to content

Commit

Permalink
temp: b
Browse files Browse the repository at this point in the history
  • Loading branch information
akorzunin committed Aug 22, 2024
1 parent 8e4e909 commit e37ac78
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 123 deletions.
1 change: 1 addition & 0 deletions src/components/controls/LoopControls.gd
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func _process(delta: float) -> void:
else:
handle_free_spin_input(delta)

# TODO make statc
func handle_free_spin_input(delta: float):
var rotation: Quaternion
var rs := ROTATION_SPEED / 10. * delta
Expand Down
2 changes: 1 addition & 1 deletion src/components/utils/Op.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ static func xor(arg1: bool, arg2: bool) -> bool:
return arg1 != arg2

static func v3(arr: Array) -> Vector3:
return Vector3(arr[0], arr[3], arr[2])
return Vector3(arr[0], arr[1], arr[2])
14 changes: 12 additions & 2 deletions src/components/utils/ShaderUtils.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class_name ShaderUtils

static func set_shader_param(node: MeshInstance3D, _name: String, value: Variant, idx: int = 0):
var m = node.get_active_material(0) as ShaderMaterial
if not m:
return
if idx == 0:
m.set_shader_parameter(_name, value)
elif idx == 1 and m.next_pass:
Expand All @@ -18,8 +20,16 @@ static func create_shader_material(_shader: Shader) -> ShaderMaterial:
sm.shader = _shader
return sm

static func apply_shaders(_shaders: Array[Shader], node: MeshInstance3D) -> MeshInstance3D:
static func apply_shaders(_shaders: Array, node: MeshInstance3D) -> MeshInstance3D:
for i in _shaders.size():
var m := create_shader_material(_shaders[i])
node.set("material_override" + "/next_pass".repeat(i), m)
# TODO: figure out how to do it w/ set and property path
#node.set("material_override" + ":next_pass".repeat(i), m)
match i:
0: node.material_override = m
1: node.material_override.next_pass = m
2: node.material_override.next_pass.next_pass = m
3: node.material_override.next_pass.next_pass.next_pass = m
4: node.material_override.next_pass.next_pass.next_pass.next_pass = m
5: node.material_override.next_pass.next_pass.next_pass.next_pass.next_pass = m
return node
4 changes: 3 additions & 1 deletion src/globals.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ var D = {
init_pos = Quaternion(0, 0.707, 0, 0.707).normalized(),
}
var data := {}
var settings := {}
var settings := {
SCALE_FACTOR = DafaultConfig.settings.game_settings.SCALE_FACTOR,
}

# global signals
enum FontType {HEX, EMOJI}
Expand Down
1 change: 0 additions & 1 deletion src/models/icosahedron/Icosahedron.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ shader = ExtResource("5_s1usq")
shader_parameter/pulse_enabled = false
shader_parameter/colorflow_enbled = false
shader_parameter/cutplate_visible = true
shader_parameter/use_web_colors = false
shader_parameter/cutplane = Vector4(-0.577, -0.577, 0.577, 0.794)
shader_parameter/color = Vector3(0, 1, 0)

Expand Down
124 changes: 65 additions & 59 deletions src/models/icosahedron/components/Icosahedron.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const EDGE_NOISE = preload('res://src/models/icosahedron/resources/edge_noise.re
@export var shader_type: int = 0:
set(val):
shader_type = val
set_shader_type(val)
#set_shader_type(val)
var show_face_numbers: bool
@export var inital_transfrm := Quaternion()
@onready var cut_plane: CutPlane = $CutPlane
@onready var mesh_icosahedron: MeshIcosahedron = $MeshIcosahedron
Expand All @@ -22,77 +23,82 @@ var cutplane_vector := Vector3(1,1,1).normalized()
var scale_timer: ScaleTimer
var sf: float

func set_shader_type(_type: int):
var variant = IcosahedronVarints.figure_variants[_type]
if variant.get("cutplane"):
set_cutplane(variant.cutplane)
set_color(G.theme.figure_variants.get(variant.name, G.theme.base_color))

func init(shader_args: Dictionary, transform_args: Dictionary = {}) -> Icosahedron:
scale_factor = G.settings.SCALE_FACTOR
scaling_enabled = G.settings.get("SCALING_ENABLED", true)
DEBUG_VISUAL = G.settings.get("DEBUG_VISUAL", false)
shader_type = shader_args.get("type", 0)
inital_transfrm = transform_args.get("quat", Quaternion())
scale_timer = transform_args.get("scale_timer")

func with_type(type: int):
shader_type = type
return self

func set_cutplane(v: Vector4):
Utils.set_shader_param(mesh_icosahedron, "cutplane", v)
Utils.set_shader_param(mesh_icosahedron, "cutplane", v, 1)
Utils.set_shader_param(mesh_icosahedron, "cutplane", v, 3)
Utils.set_shader_param(mesh_icosahedron, "noise_pattern", EDGE_NOISE, 3)

cutplane_vector = Vector3(v.x, v.y, v.z).normalized()
if DEBUG_VISUAL:
var ray = RayCast3D.new()
ray.target_position = $Collider.get_cutplane_vector()
mesh_icosahedron.add_child(ray)

func set_color(arr: Array):
var c = Vector3(arr[0], arr[1], arr[2])
Utils.set_shader_param(mesh_icosahedron, "color", c)
Utils.set_shader_param(mesh_icosahedron, "color", c, 1)
Utils.set_shader_param(mesh_icosahedron, "color", c, 3)
func with_face_numbers(show: bool):
show_face_numbers = show
return self
#
#func set_shader_type(_type: int):
#var variant = IcosahedronVarints.figure_variants[_type]
#if variant.get("cutplane"):
#set_cutplane(variant.cutplane)
#set_color(G.theme.figure_variants.get(variant.name, G.theme.base_color))
#
#func init(shader_args: Dictionary, transform_args: Dictionary = {}) -> Icosahedron:
#scale_factor = G.settings.SCALE_FACTOR
#scaling_enabled = G.settings.get("SCALING_ENABLED", true)
#DEBUG_VISUAL = G.settings.get("DEBUG_VISUAL", false)
#shader_type = shader_args.get("type", 0)
#inital_transfrm = transform_args.get("quat", Quaternion())
#scale_timer = transform_args.get("scale_timer")
#
#return self
#
#func set_cutplane(v: Vector4):
#Utils.set_shader_param(mesh_icosahedron, "cutplane", v)
#Utils.set_shader_param(mesh_icosahedron, "cutplane", v, 1)
#Utils.set_shader_param(mesh_icosahedron, "cutplane", v, 3)
#Utils.set_shader_param(mesh_icosahedron, "noise_pattern", EDGE_NOISE, 3)
#
#cutplane_vector = Vector3(v.x, v.y, v.z).normalized()
#if DEBUG_VISUAL:
#var ray = RayCast3D.new()
#ray.target_position = $Collider.get_cutplane_vector()
#mesh_icosahedron.add_child(ray)

#func set_color(arr: Array):
#var c = Vector3(arr[0], arr[1], arr[2])
#Utils.set_shader_param(mesh_icosahedron, "color", c)
#Utils.set_shader_param(mesh_icosahedron, "color", c, 1)
#Utils.set_shader_param(mesh_icosahedron, "color", c, 3)

const dst := IcosahedronVarints.dst
var inital_transform := Quaternion(0, 0.707, 0, 0.707).normalized()

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
transform.basis = Basis(inital_transform).orthonormalized()
if not DEBUG_VISUAL:
cut_plane.hide()
if scale_timer:
scale_timer.timeout.connect(_on_scale_tick)
if scale_factor:
sf = (scale_factor + 1000.) / 1000.
var variant = IcosahedronVarints.figure_variants[shader_type]
var sm = ShaderMaterial.new()
sm.shader = ICOSAHEDRON_SHADER_V_1
var sm2 = ShaderMaterial.new()
sm2.shader = CUTPLANE_EFFECT_V_2
var sm3 = ShaderMaterial.new()
sm3.shader = OUTLINE_V_1
var sm4 = ShaderMaterial.new()
sm4.shader = EDGE_HIGHLIGHT_V_1
mesh_icosahedron.material_override = sm
mesh_icosahedron.material_override.next_pass = sm2
mesh_icosahedron.material_override.next_pass.next_pass = sm3
mesh_icosahedron.material_override.next_pass.next_pass.next_pass = sm4
if variant.get("cutplane"):
set_cutplane(variant.cutplane)
else:
Utils.set_shader_param(mesh_icosahedron, "cutplate_visible", false)
Utils.set_shader_param(mesh_icosahedron, "enable", false, 1)
Utils.set_shader_param(mesh_icosahedron, "enable", false, 2)
Utils.set_shader_param(mesh_icosahedron, "enable", false, 3)

set_color(G.theme.figure_variants.get(variant.name, G.theme.base_color))

if inital_transfrm:
transform.basis = Basis(inital_transfrm).orthonormalized()

pass
#var variant = IcosahedronVarints.figure_variants[shader_type]
#var sm = ShaderMaterial.new()
#sm.shader = ICOSAHEDRON_SHADER_V_1
#var sm2 = ShaderMaterial.new()
#sm2.shader = CUTPLANE_EFFECT_V_2
#var sm3 = ShaderMaterial.new()
#sm3.shader = OUTLINE_V_1
#var sm4 = ShaderMaterial.new()
#sm4.shader = EDGE_HIGHLIGHT_V_1
#mesh_icosahedron.material_override = sm
#mesh_icosahedron.material_override.next_pass = sm2
#mesh_icosahedron.material_override.next_pass.next_pass = sm3
#mesh_icosahedron.material_override.next_pass.next_pass.next_pass = sm4
#if variant.get("cutplane"):
#set_cutplane(variant.cutplane)
#else:
#Utils.set_shader_param(mesh_icosahedron, "cutplate_visible", false)
#Utils.set_shader_param(mesh_icosahedron, "enable", false, 1)
#Utils.set_shader_param(mesh_icosahedron, "enable", false, 2)
#Utils.set_shader_param(mesh_icosahedron, "enable", false, 3)
#
#set_color(G.theme.figure_variants.get(variant.name, G.theme.base_color))

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _on_scale_tick() -> void:
Expand Down
28 changes: 18 additions & 10 deletions src/models/icosahedron/components/MeshIcosahedron.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,54 @@ const OUTLINE_V_1 = preload('res://src/models/icosahedron/shaders/outline_v1.gds
const EDGE_HIGHLIGHT_V_1 = preload('res://src/models/icosahedron/shaders/edge_highlight_v1.gdshader')
const EDGE_NOISE = preload('res://src/models/icosahedron/resources/edge_noise.res')

const MATERIAL_002 = preload('res://src/models/icosahedron/test/Material.face_index.tres')

@export var applied_shaders := [
ICOSAHEDRON_SHADER_V_1,
CUTPLANE_EFFECT_V_2,
OUTLINE_V_1,
EDGE_HIGHLIGHT_V_1,
]

@onready var icosahedron: Icosahedron = $'..'
@onready var icosahedron: Icosahedron = $".."
@onready var collider: Collider = $'../Collider'

var angle_good := false
var is_alt := false
var is_rotating := false
var currnt_type := -1


@export var currnt_type := -1
var show_face_numbers := false
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
currnt_type = icosahedron.shader_type
show_face_numbers = icosahedron.show_face_numbers
set_type(currnt_type)
if Utils.main_scene(self) in ['MainScene', 'LoopScene', 'MenuScene']:
# wierd trick to not get errors when scaling and rotating mesh when its mounted in runtime
transform.basis = Basis(icosahedron.transform.basis.get_rotation_quaternion())

func set_controlled(state: bool):
Utils.set_shader_param(self, "enable", state, 2)
ShaderUtils.set_shader_param(self, "enable", state, 2)
collider.set_collision_mask_value(1, state)
collider.set_collision_layer_value(1, state)

func set_cutplane(v: Vector4):
for i in applied_shaders.size():
Utils.set_shader_param(self, "cutplane", v, i)
Utils.set_shader_param(self, "noise_pattern", EDGE_NOISE, 3)
ShaderUtils.set_shader_param(self, "cutplane", v, i)
ShaderUtils.set_shader_param(self, "noise_pattern", EDGE_NOISE, i)

func set_color(c: Vector3):
for i in applied_shaders.size():
Utils.set_shader_param(self, "color", c, i)
ShaderUtils.set_shader_param(self, "color", c, i)

func set_type(type: int):
currnt_type = type
var variant: Vector4 = IcosahedronVarints.figure_variants_v2[type]
ShaderUtils.apply_shaders(applied_shaders, self)
if show_face_numbers:
self.material_override = MATERIAL_002
else:
ShaderUtils.apply_shaders(applied_shaders, self)

set_cutplane(variant)
var variant_color: Array = TwTheme.figure_variants_v2[variant]
var variant_color: Array = TwTheme.figure_variants_v2[type]
set_color(Op.v3(variant_color))
4 changes: 2 additions & 2 deletions src/models/icosahedron/components/Variants.gd
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const figure_variants = {

# TDOO: make in sync w/ UV map from discrete_control.tscn
static var figure_variants_v2 := {
0: Quaternion(),
1: FaceLock.left_q.normalized(),
0: Vector4( -0.577, 0.577, 0.577, dst),
1: Vector4( -0.577, 0.577, -0.577, dst),
# ...
}
12 changes: 1 addition & 11 deletions src/models/icosahedron/test/Material.face_index.tres
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
[gd_resource type="StandardMaterial3D" load_steps=4 format=3 uid="uid://b6aokpwbpsi2l"]
[gd_resource type="StandardMaterial3D" load_steps=2 format=3 uid="uid://b6aokpwbpsi2l"]

[ext_resource type="Texture2D" uid="uid://dplxjdemisp4h" path="res://src/models/icosahedron/test/UV_face_index.png" id="1_w4ekc"]
[ext_resource type="Shader" path="res://src/models/icosahedron/shaders/cutplane_effect_v3.gdshader" id="2_x1gid"]

[sub_resource type="ShaderMaterial" id="ShaderMaterial_2ft62"]
render_priority = 0
shader = ExtResource("2_x1gid")
shader_parameter/cutplane = Vector4(0.358, 0, 0.934, 0.794)
shader_parameter/color = Color(0, 1, 0, 1)
shader_parameter/enable = true
shader_parameter/outline_thickness = 0.0

[resource]
resource_name = "Material.002"
next_pass = SubResource("ShaderMaterial_2ft62")
albedo_texture = ExtResource("1_w4ekc")
roughness = 0.5
59 changes: 29 additions & 30 deletions src/models/icosahedron/test/variant_test.gd
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
@tool
extends Node3D

const IcosahedronScene = preload('res://src/models/icosahedron/Icosahedron.tscn')
var G: Globals = Globals.new()
@onready var icosahedron: Icosahedron = $Icosahedron

@export var a := false:
set(val):
var new_figure = IcosahedronScene.instantiate()
new_figure.shader_type = 1
self.add_child(new_figure)
new_figure.owner = get_tree().edited_scene_root
print_debug(new_figure.shader_type)

@export var b := false:
set(val):
for i in self.get_children():
remove_child(i)

@export var c := 0:
set(val):
c = val
var a : Icosahedron = get_child(0)
print_debug(a)
a.shader_type = val

var controlledNode
# TODO get from defaults
var ROTATION_SPEED := 12
@export var show_face_numbers: bool = false
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
#if not get_tree().edited_scene_root.get_node_or_null('./globals'):
#var globals = Globals.new()
#globals.set_name('globals')
#get_tree().edited_scene_root.call_deferred('add_child', globals)
#G = get_tree().edited_scene_root.get_node('./globals')
pass # Replace with function body.
var new_figure = IcosahedronScene.instantiate().with_type(1).with_face_numbers(show_face_numbers)
add_child(new_figure)
controlledNode = new_figure.mesh_icosahedron


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
if not controlledNode:
return
if Input.is_action_just_pressed('ui_accept'):
controlledNode.transform.basis = Basis(Quaternion()).orthonormalized()
handle_free_spin_input(delta)
pass

func handle_free_spin_input(delta: float):
var _rotation: Quaternion
var rs := ROTATION_SPEED / 10. * delta
var is_inverted = false

if Input.is_action_pressed("ui_up"):
_rotation = _rotation * Quaternion(0, 0, -rs, 1, )
if Input.is_action_pressed("ui_down"):
_rotation = _rotation * Quaternion(0, 0, rs, 1, )
if Op.xor(is_inverted, Input.is_action_pressed("ui_right")):
_rotation = _rotation * Quaternion(0, rs, 0, 1, )
if Op.xor(is_inverted, Input.is_action_pressed("ui_left")):
_rotation = _rotation * Quaternion(0, -rs, 0, 1, )
if controlledNode and _rotation:
var t = _rotation.normalized() * controlledNode.quaternion
controlledNode.transform.basis = Basis(t).orthonormalized()
Loading

0 comments on commit e37ac78

Please sign in to comment.