Skip to content

Commit

Permalink
temp: MeshIcosahedron shader settings
Browse files Browse the repository at this point in the history
  • Loading branch information
akorzunin committed Aug 22, 2024
1 parent fdc4631 commit 7b9f79f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/components/utils/Op.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ class_name Op

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])
25 changes: 25 additions & 0 deletions src/components/utils/ShaderUtils.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
extends RefCounted
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 idx == 0:
m.set_shader_parameter(_name, value)
elif idx == 1 and m.next_pass:
(m.next_pass as ShaderMaterial).set_shader_parameter(_name, value)
elif idx == 2 and m.next_pass and m.next_pass.next_pass:
(m.next_pass.next_pass as ShaderMaterial).set_shader_parameter(_name, value)
elif idx == 3 and m.next_pass and m.next_pass.next_pass and m.next_pass.next_pass.next_pass:
(m.next_pass.next_pass.next_pass as ShaderMaterial).set_shader_parameter(_name, value)
return

static func create_shader_material(_shader: Shader) -> ShaderMaterial:
var sm = ShaderMaterial.new()
sm.shader = _shader
return sm

static func apply_shaders(_shaders: Array[Shader], node: MeshInstance3D) -> MeshInstance3D:
for i in _shaders.size():
var m := create_shader_material(_shaders[i])
node.set("material_override" + "/next_pass".repeat(i), m)
return node
31 changes: 31 additions & 0 deletions src/models/icosahedron/components/MeshIcosahedron.gd
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
extends MeshInstance3D
class_name MeshIcosahedron

const ICOSAHEDRON_SHADER_V_1 = preload('res://src/models/icosahedron/shaders/icosahedron_shader_v1.gdshader')
const CUTPLANE_EFFECT_V_2 = preload("res://src/models/icosahedron/shaders/cutplane_effect_v2.gdshader")
const OUTLINE_V_1 = preload('res://src/models/icosahedron/shaders/outline_v1.gdshader')
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')

@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 collider: Collider = $'../Collider'

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


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
Expand All @@ -19,3 +33,20 @@ func set_controlled(state: bool):
Utils.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)

func set_color(c: Vector3):
for i in applied_shaders.size():
Utils.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)
set_cutplane(variant)
var variant_color: Array = TwTheme.figure_variants_v2[variant]
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 @@ -44,8 +44,8 @@ const figure_variants = {
}

# TDOO: make in sync w/ UV map from discrete_control.tscn
var figure_variants_v2 := {
0: Quaternion().normalized(),
static var figure_variants_v2 := {
0: Quaternion(),
1: FaceLock.left_q.normalized(),
# ...
}
7 changes: 7 additions & 0 deletions src/themes/TwTheme.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ const T = {
default = TwColors.tw.sky._500,
},
}

const figure_variants_v2 := {
0: TwColors.tw.blue._700,
1: TwColors.tw.green._700,
2: TwColors.tw.sky._700,
# ... TODO: color for 0-19 edge from UV map
}

0 comments on commit 7b9f79f

Please sign in to comment.