diff --git a/addons/ShaderLib/Maths/Scalar/SmoothMax.gd b/addons/ShaderLib/Maths/Scalar/SmoothMax.gd new file mode 100644 index 0000000..11da039 --- /dev/null +++ b/addons/ShaderLib/Maths/Scalar/SmoothMax.gd @@ -0,0 +1,56 @@ +@tool +class_name VisualShaderNodeScalarSmoothMax extends VisualShaderNodeCustom + +func _get_name() -> String: + return "SmoothMax" + +func _get_category() -> String: + return "Maths/Scalar" + +func _get_description() -> String: + return "Returns the maximum value between A and B, but smooths out the intersections of A and B based on T." + +func _get_return_icon_type() -> PortType: + return PORT_TYPE_SCALAR + +func _get_input_port_count() -> int: + return 3 + +func _get_input_port_name(port: int) -> String: + match port: + 0: + return "a" + 1: + return "b" + _: + return "t" + +func _get_input_port_type(port: int) -> PortType: + return PORT_TYPE_SCALAR + +func _get_input_port_default_value(port: int) -> Variant: + match port: + 2: + return 0.5 + _: + return 0.0 + +func _get_output_port_count() -> int: + return 1 + +func _get_output_port_name(port: int) -> String: + return "op" + +func _get_output_port_type(port: int) -> PortType: + return PORT_TYPE_SCALAR + +func _get_global_code(mode: Shader.Mode) -> String: + var code: String = preload("SmoothMax.gdshaderinc").code + return code + +func _get_code(input_vars: Array[String], output_vars: Array[String], mode: Shader.Mode, type: VisualShader.Type) -> String: + var a: String = input_vars[0] + var b: String = input_vars[1] + var t: String = input_vars[2] + + return output_vars[0] + " = smoothmax(%s, %s, %s);" % [a, b, t] diff --git a/addons/ShaderLib/Maths/Scalar/SmoothMax.gdshaderinc b/addons/ShaderLib/Maths/Scalar/SmoothMax.gdshaderinc new file mode 100644 index 0000000..0c1dcf0 --- /dev/null +++ b/addons/ShaderLib/Maths/Scalar/SmoothMax.gdshaderinc @@ -0,0 +1,4 @@ +float smoothmax(float a, float b, float t) { + float h = clamp(.5 + .5 * (b - a) / -t, 0, 1); + return mix(b, a, h) + t * h * (1. - h); +} \ No newline at end of file diff --git a/addons/ShaderLib/Maths/Scalar/SmoothMin.gd b/addons/ShaderLib/Maths/Scalar/SmoothMin.gd new file mode 100644 index 0000000..19b0d17 --- /dev/null +++ b/addons/ShaderLib/Maths/Scalar/SmoothMin.gd @@ -0,0 +1,56 @@ +@tool +class_name VisualShaderNodeScalarSmoothMin extends VisualShaderNodeCustom + +func _get_name() -> String: + return "SmoothMin" + +func _get_category() -> String: + return "Maths/Scalar" + +func _get_description() -> String: + return "Returns the minimum value between A and B, but smooths out the intersections of A and B based on T." + +func _get_return_icon_type() -> PortType: + return PORT_TYPE_SCALAR + +func _get_input_port_count() -> int: + return 3 + +func _get_input_port_name(port: int) -> String: + match port: + 0: + return "a" + 1: + return "b" + _: + return "t" + +func _get_input_port_type(port: int) -> PortType: + return PORT_TYPE_SCALAR + +func _get_input_port_default_value(port: int) -> Variant: + match port: + 2: + return 0.5 + _: + return 0.0 + +func _get_output_port_count() -> int: + return 1 + +func _get_output_port_name(port: int) -> String: + return "op" + +func _get_output_port_type(port: int) -> PortType: + return PORT_TYPE_SCALAR + +func _get_global_code(mode: Shader.Mode) -> String: + var code: String = preload("SmoothMin.gdshaderinc").code + return code + +func _get_code(input_vars: Array[String], output_vars: Array[String], mode: Shader.Mode, type: VisualShader.Type) -> String: + var a: String = input_vars[0] + var b: String = input_vars[1] + var t: String = input_vars[2] + + return output_vars[0] + " = smoothmin(%s, %s, %s);" % [a, b, t] diff --git a/addons/ShaderLib/Maths/Scalar/SmoothMin.gdshaderinc b/addons/ShaderLib/Maths/Scalar/SmoothMin.gdshaderinc new file mode 100644 index 0000000..e9c7621 --- /dev/null +++ b/addons/ShaderLib/Maths/Scalar/SmoothMin.gdshaderinc @@ -0,0 +1,4 @@ +float smoothmin(float a, float b, float t) { + float h = clamp(.5 + .5 * (b - a) / t, 0, 1); + return mix(b, a, h) - t * h * (1. - h); +} \ No newline at end of file diff --git a/addons/ShaderLib/Maths/Vector/Project.gd b/addons/ShaderLib/Maths/Vector/Project.gd index 6333216..27c0c7a 100644 --- a/addons/ShaderLib/Maths/Vector/Project.gd +++ b/addons/ShaderLib/Maths/Vector/Project.gd @@ -62,6 +62,10 @@ func _get_property_name(index: int) -> String: func _get_property_options(index: int) -> PackedStringArray: return ["Vector2", "Vector3"] +func _get_global_code(mode: Shader.Mode) -> String: + var code: String = preload("Project.gdshaderinc").code + return code + func _get_code(input_vars: Array[String], output_vars: Array[String], mode: Shader.Mode, type: VisualShader.Type) -> String: var vector_a: String = input_vars[0] var vector_b: String = input_vars[1] @@ -69,6 +73,6 @@ func _get_code(input_vars: Array[String], output_vars: Array[String], mode: Shad match vector_index: 0: - return output_vars[0] + " = vec2(%s.xy) * (dot(%s.xy, %s.xy) / dot(%s.xy, %s.xy));" % [vector_b, vector_a, vector_b, vector_b, vector_b] + return output_vars[0] + " = project_2d(%s, %s);" % [vector_a, vector_b] _: - return output_vars[0] + " = %s * (dot(%s, %s) / dot(%s, %s));" % [vector_b, vector_a, vector_b, vector_b, vector_b] + return output_vars[0] + " = project_3d(%s, %s);" % [vector_a, vector_b] diff --git a/addons/ShaderLib/Maths/Vector/Project.gdshaderinc b/addons/ShaderLib/Maths/Vector/Project.gdshaderinc new file mode 100644 index 0000000..b3b27ae --- /dev/null +++ b/addons/ShaderLib/Maths/Vector/Project.gdshaderinc @@ -0,0 +1,7 @@ +vec2 project_2d(vec2 a, vec2 b) { + return b * (dot(a, b) / dot(b, b)); +} + +vec3 project_3d(vec3 a, vec3 b) { + return b * (dot(a, b) / dot(b, b)); +} \ No newline at end of file diff --git a/addons/ShaderLib/Maths/Vector/ProjectOnPlane.gd b/addons/ShaderLib/Maths/Vector/ProjectOnPlane.gd index 4abdfa0..fb60ab0 100644 --- a/addons/ShaderLib/Maths/Vector/ProjectOnPlane.gd +++ b/addons/ShaderLib/Maths/Vector/ProjectOnPlane.gd @@ -11,12 +11,7 @@ func _get_description() -> String: return "Projects a vector onto a plane defined by a normal orthogonal to the plane." func _get_return_icon_type() -> PortType: - var vector_index: int = get_option_index(0) - match vector_index: - 0: - return PORT_TYPE_VECTOR_2D - _: - return PORT_TYPE_VECTOR_3D + return PORT_TYPE_VECTOR_3D func _get_input_port_count() -> int: return 2 @@ -29,12 +24,7 @@ func _get_input_port_name(port: int) -> String: return "plane normal" func _get_input_port_type(port: int) -> PortType: - var vector_index: int = get_option_index(0) - match vector_index: - 0: - return PORT_TYPE_VECTOR_2D - _: - return PORT_TYPE_VECTOR_3D + return PORT_TYPE_VECTOR_3D func _get_output_port_count() -> int: return 1 @@ -43,31 +33,13 @@ func _get_output_port_name(port: int) -> String: return "vector" func _get_output_port_type(port: int) -> PortType: - var vector_index: int = get_option_index(0) - match vector_index: - 0: - return PORT_TYPE_VECTOR_2D - _: - return PORT_TYPE_VECTOR_3D + return PORT_TYPE_VECTOR_3D -func _get_property_count() -> int: - return 1 - -func _get_property_default_index(index: int) -> int: - return 0 - -func _get_property_name(index: int) -> String: - return "" - -func _get_property_options(index: int) -> PackedStringArray: - return ["Vector2", "Vector3"] +func _get_global_code(mode: Shader.Mode) -> String: + var code: String = preload("ProjectOnPlane.gdshaderinc").code + return code func _get_code(input_vars: Array[String], output_vars: Array[String], mode: Shader.Mode, type: VisualShader.Type) -> String: var vector_a: String = input_vars[0] var plane_normal: String = input_vars[1] - var vector_index: int = get_option_index(0) - match vector_index: - 0: - return output_vars[0] + " = %s.xy - (%s.xy * (dot(%s.xy, %s.xy) / dot(%s.xy, %s.xy)));" % [vector_a, plane_normal, vector_a, plane_normal, plane_normal, plane_normal] - _: - return output_vars[0] + " = %s - (%s * (dot(%s, %s) / dot(%s, %s)));" % [vector_a, plane_normal, vector_a, plane_normal, plane_normal, plane_normal] + return output_vars[0] + " = project_on_plane(%s, %s);" % [vector_a, plane_normal] diff --git a/addons/ShaderLib/Maths/Vector/ProjectOnPlane.gdshaderinc b/addons/ShaderLib/Maths/Vector/ProjectOnPlane.gdshaderinc new file mode 100644 index 0000000..ba49e1c --- /dev/null +++ b/addons/ShaderLib/Maths/Vector/ProjectOnPlane.gdshaderinc @@ -0,0 +1,3 @@ +vec3 project_on_plane(vec3 vector, vec3 plane_normal) { + return vector - (plane_normal * (dot(vector, plane_normal) / dot(plane_normal, plane_normal))); +} \ No newline at end of file diff --git a/addons/ShaderLib/Maths/Wave/SawtoothWave.gd b/addons/ShaderLib/Maths/Wave/SawtoothWave.gd index 2b82b9f..f487fb4 100644 --- a/addons/ShaderLib/Maths/Wave/SawtoothWave.gd +++ b/addons/ShaderLib/Maths/Wave/SawtoothWave.gd @@ -70,6 +70,10 @@ func _get_property_name(index: int) -> String: func _get_property_options(index: int) -> PackedStringArray: return ["Vector1", "Vector2", "Vector3", "Vector4"] +func _get_global_code(mode: Shader.Mode) -> String: + var code: String = preload("SawtoothWave.gdshaderinc").code + return code + func _get_code(input_vars: Array[String], output_vars: Array[String], mode: Shader.Mode, type: VisualShader.Type) -> String: var input: String var vector_index: int = get_option_index(0) @@ -86,4 +90,12 @@ func _get_code(input_vars: Array[String], output_vars: Array[String], mode: Shad if input_vars[0]: input = input_vars[0] - return output_vars[0] + " = 2.0 * (%s - floor(0.5 + %s));" % [input, input] + match vector_index: + 0: + return output_vars[0] + " = sawtooth_wave(vec4(%s)).x;" % [input] + 1: + return output_vars[0] + " = sawtooth_wave(vec4(%s, 0.0, 0.0)).xy;" % [input] + 2: + return output_vars[0] + " = sawtooth_wave(vec4(%s, 0.0)).xyz;" % [input] + _: + return output_vars[0] + " = sawtooth_wave(%s);" % [input] diff --git a/addons/ShaderLib/Maths/Wave/SawtoothWave.gdshaderinc b/addons/ShaderLib/Maths/Wave/SawtoothWave.gdshaderinc new file mode 100644 index 0000000..f27a23f --- /dev/null +++ b/addons/ShaderLib/Maths/Wave/SawtoothWave.gdshaderinc @@ -0,0 +1,3 @@ +vec4 sawtooth_wave(vec4 input) { + return 2. * (input - floor(.5 + input)); +} \ No newline at end of file diff --git a/addons/ShaderLib/Maths/Wave/SquareWave.gd b/addons/ShaderLib/Maths/Wave/SquareWave.gd index 3d6e2f4..24c3628 100644 --- a/addons/ShaderLib/Maths/Wave/SquareWave.gd +++ b/addons/ShaderLib/Maths/Wave/SquareWave.gd @@ -71,6 +71,10 @@ func _get_property_name(index: int) -> String: func _get_property_options(index: int) -> PackedStringArray: return ["Vector1", "Vector2", "Vector3", "Vector4"] +func _get_global_code(mode: Shader.Mode) -> String: + var code: String = preload("SquareWave.gdshaderinc").code + return code + func _get_code(input_vars: Array[String], output_vars: Array[String], mode: Shader.Mode, type: VisualShader.Type) -> String: var input: String var vector_index: int = get_option_index(0) @@ -87,4 +91,12 @@ func _get_code(input_vars: Array[String], output_vars: Array[String], mode: Shad if input_vars[0]: input = input_vars[0] - return output_vars[0] + " = 1.0 - 2.0 * round(fract(%s));" % [input] + match vector_index: + 0: + return output_vars[0] + " = square_wave(vec4(%s)).x;" % [input] + 1: + return output_vars[0] + " = square_wave(vec4(%s, 0.0, 0.0)).xy;" % [input] + 2: + return output_vars[0] + " = square_wave(vec4(%s, 0.0)).xyz;" % [input] + _: + return output_vars[0] + " = square_wave(%s);" % [input] diff --git a/addons/ShaderLib/Maths/Wave/SquareWave.gdshaderinc b/addons/ShaderLib/Maths/Wave/SquareWave.gdshaderinc new file mode 100644 index 0000000..dc376d0 --- /dev/null +++ b/addons/ShaderLib/Maths/Wave/SquareWave.gdshaderinc @@ -0,0 +1,3 @@ +vec4 square_wave(vec4 input) { + return 1. - 2. * round(fract(input)); +} \ No newline at end of file diff --git a/addons/ShaderLib/Maths/Wave/TriangleWave.gd b/addons/ShaderLib/Maths/Wave/TriangleWave.gd index 07d3d3e..b5d4328 100644 --- a/addons/ShaderLib/Maths/Wave/TriangleWave.gd +++ b/addons/ShaderLib/Maths/Wave/TriangleWave.gd @@ -69,6 +69,10 @@ func _get_property_name(index: int) -> String: func _get_property_options(index: int) -> PackedStringArray: return ["Vector1", "Vector2", "Vector3", "Vector4"] +func _get_global_code(mode: Shader.Mode) -> String: + var code: String = preload("TriangleWave.gdshaderinc").code + return code + func _get_code(input_vars: Array[String], output_vars: Array[String], mode: Shader.Mode, type: VisualShader.Type) -> String: var input: String var vector_index: int = get_option_index(0) @@ -85,4 +89,12 @@ func _get_code(input_vars: Array[String], output_vars: Array[String], mode: Shad if input_vars[0]: input = input_vars[0] - return output_vars[0] + " = 2.0 * abs(2.0 * (%s - floor(0.5 + %s))) - 1.0;" % [input, input] + match vector_index: + 0: + return output_vars[0] + " = triangle_wave(vec4(%s)).x;" % [input] + 1: + return output_vars[0] + " = triangle_wave(vec4(%s, 0.0, 0.0)).xy;" % [input] + 2: + return output_vars[0] + " = triangle_wave(vec4(%s, 0.0)).xyz;" % [input] + _: + return output_vars[0] + " = triangle_wave(%s);" % [input] diff --git a/addons/ShaderLib/Maths/Wave/TriangleWave.gdshaderinc b/addons/ShaderLib/Maths/Wave/TriangleWave.gdshaderinc new file mode 100644 index 0000000..2642c07 --- /dev/null +++ b/addons/ShaderLib/Maths/Wave/TriangleWave.gdshaderinc @@ -0,0 +1,3 @@ +vec4 triangle_wave(vec4 input) { + return 2. * abs(2. * (input - floor(.5 + input))) - 1.; +} \ No newline at end of file diff --git a/documentation/Documentation.md b/documentation/Documentation.md index fd387e7..b55c4c3 100644 --- a/documentation/Documentation.md +++ b/documentation/Documentation.md @@ -1,5 +1,5 @@ # Installation -Copy the contents of **_addons/ShaderLib_** into the same folder in your project. No activation needed. Custom visual shader nodes work the same way as standard visual shader nodes. +Hit download as zip button in the git repo. Copy the contents of **_addons/ShaderLib_** into the same folder in your project. No activation needed. Custom visual shader nodes work the same way as standard visual shader nodes. If you don't immediatly see new nodes under **_Addons_** category, simply reload your project. # Uninstallation @@ -30,6 +30,11 @@ For example if you want to rotate UV in your **_.gdshader_** file, you can use `

Maths nodes

+

 Scalar

+ +

  SmoothMax node

+

  SmoothMin node

+

 Vector

  Distance

diff --git a/documentation/Nodes/Maths/Scalar/SmoothMax.md b/documentation/Nodes/Maths/Scalar/SmoothMax.md new file mode 100644 index 0000000..550e437 --- /dev/null +++ b/documentation/Nodes/Maths/Scalar/SmoothMax.md @@ -0,0 +1,29 @@ +# SmoothMax node +Returns the maximum value between A and B, but smooths out the intersections of A and B based on T. T is the smoothing value. If 0 is passed at T, function will act as a standard max(), if negative value is passed it will act as [SmoothMin node](/documentation/Nodes/Maths/Scalar/SmoothMin.md). +
+ +**Inputs** +|Name|Type|Binding|Description| +|---|---|---|---| +|a|float|none|Input A| +|b|float|none|Input B| +|t|float|none|Smoothing input| + +**Outputs** +|Name|Type|Binding|Description| +|---|---|---|---| +|op|float|None|Smooth maximum between A and B| + +**ShaderInc location** +
`res://addons/ShaderLib/Maths/Scalar/SmoothMax.gdshaderinc` + +**Method signature** +
`float smoothmax(float a, float b, float t)` + +**Parameters** +|Name|Type|Description| +|---|---|---| +|a|float|Input A| +|b|float|Input B| +|t|float|Smoothing input| +___ \ No newline at end of file diff --git a/documentation/Nodes/Maths/Scalar/SmoothMin.md b/documentation/Nodes/Maths/Scalar/SmoothMin.md new file mode 100644 index 0000000..08d6829 --- /dev/null +++ b/documentation/Nodes/Maths/Scalar/SmoothMin.md @@ -0,0 +1,29 @@ +# SmoothMin node +Returns the minimum value between A and B, but smooths out the intersections of A and B based on T. T is the smoothing value. If 0 is passed at T, function will act as a standard min(), if negative value is passed it will act as [SmoothMax node](/documentation/Nodes/Maths/Scalar/SmoothMax.md). +
+ +**Inputs** +|Name|Type|Binding|Description| +|---|---|---|---| +|a|float|none|Input A| +|b|float|none|Input B| +|t|float|none|Smoothing input| + +**Outputs** +|Name|Type|Binding|Description| +|---|---|---|---| +|op|float|None|Smooth minimum between A and B| + +**ShaderInc location** +
`res://addons/ShaderLib/Maths/Scalar/SmoothMin.gdshaderinc` + +**Method signature** +
`float smoothmin(float a, float b, float t)` + +**Parameters** +|Name|Type|Description| +|---|---|---| +|a|float|Input A| +|b|float|Input B| +|t|float|Smoothing input| +___ \ No newline at end of file diff --git a/documentation/Nodes/Maths/Vector/Project.md b/documentation/Nodes/Maths/Vector/Project.md index 637d86d..f06413b 100644 --- a/documentation/Nodes/Maths/Vector/Project.md +++ b/documentation/Nodes/Maths/Vector/Project.md @@ -17,4 +17,23 @@ Projects vector A onto vector B. |Name|Type|Binding|Description| |---|---|---|---| |vector|vector3|None|Output vector| + +**ShaderInc location** +
`res://addons/ShaderLib/Maths/Vector/Project.gdshaderinc` + +**Method signature** +
`vec2 project_2d(vec2 a, vec2 b)` +
`vec3 project_3d(vec3 a, vec3 b)` + +**Parameters for 2D** +|Name|Type|Description| +|---|---|---| +|a|vec2|Vector A| +|b|vec2|Vector B| + +**Parameters for 3D** +|Name|Type|Description| +|---|---|---| +|a|vec3|Vector A| +|b|vec3|Vector B| ___ \ No newline at end of file diff --git a/documentation/Nodes/Maths/Vector/ProjectOnPlane.md b/documentation/Nodes/Maths/Vector/ProjectOnPlane.md index bee7531..e5442fc 100644 --- a/documentation/Nodes/Maths/Vector/ProjectOnPlane.md +++ b/documentation/Nodes/Maths/Vector/ProjectOnPlane.md @@ -2,19 +2,26 @@ Projects a vector onto a plane defined by a normal orthogonal to the plane.
-**Controls** -|Options|Description| -|---|---| -|Vector2, Vector3|Vector type to use for calculation| - **Inputs** |Name|Type|Binding|Description| |---|---|---|---| -|vector|Dynamic vector|none|Vector to project| -|plane normal|Dynamic vector|none|Normal orthogonal of the plane| +|vector|vector3|none|Vector to project| +|plane normal|vector3|none|Normal orthogonal of the plane| **Outputs** |Name|Type|Binding|Description| |---|---|---|---| |vector|vector3|None|Output vector| + +**ShaderInc location** +
`res://addons/ShaderLib/Maths/Vector/ProjectOnPlane.gdshaderinc` + +**Method signature** +
`vec3 project_on_plane(vec3 vector, vec3 plane_normal)` + +**Parameters** +|Name|Type|Description| +|---|---|---| +|vector|vec3|Input vector| +|plane_normal|vec3|Normal vector to the plane| ___ \ No newline at end of file diff --git a/documentation/Nodes/Maths/Wave/SawtoothWave.md b/documentation/Nodes/Maths/Wave/SawtoothWave.md index 369cec9..424c8f1 100644 --- a/documentation/Nodes/Maths/Wave/SawtoothWave.md +++ b/documentation/Nodes/Maths/Wave/SawtoothWave.md @@ -16,4 +16,15 @@ Returns a sawtooth wave from the value of input in. Resulting outp |Name|Type|Binding|Description| |---|---|---|---| |out|Dynamic vector|None|Output value| + +**ShaderInc location** +
`res://addons/ShaderLib/Maths/Wave/SawtoothWave.gdshaderinc` + +**Method signature** +
`vec4 sawtooth_wave(vec4 input)` + +**Parameters** +|Name|Type|Description| +|---|---|---| +|input|vec4|Input vector| ___ \ No newline at end of file diff --git a/documentation/Nodes/Maths/Wave/SquareWave.md b/documentation/Nodes/Maths/Wave/SquareWave.md index 54739e0..822243c 100644 --- a/documentation/Nodes/Maths/Wave/SquareWave.md +++ b/documentation/Nodes/Maths/Wave/SquareWave.md @@ -16,4 +16,15 @@ Returns a square wave from the value of input in. Resulting output |Name|Type|Binding|Description| |---|---|---|---| |out|Dynamic vector|None|Output value| + +**ShaderInc location** +
`res://addons/ShaderLib/Maths/Wave/SquareWave.gdshaderinc` + +**Method signature** +
`vec4 square _wave(vec4 input)` + +**Parameters** +|Name|Type|Description| +|---|---|---| +|input|vec4|Input vector| ___ \ No newline at end of file diff --git a/documentation/Nodes/Maths/Wave/TriangleWave.md b/documentation/Nodes/Maths/Wave/TriangleWave.md index 6d50b5e..70740e0 100644 --- a/documentation/Nodes/Maths/Wave/TriangleWave.md +++ b/documentation/Nodes/Maths/Wave/TriangleWave.md @@ -16,4 +16,15 @@ Returns a triangle wave from the value of input in. Resulting outp |Name|Type|Binding|Description| |---|---|---|---| |out|Dynamic vector|None|Output value| + +**ShaderInc location** +
`res://addons/ShaderLib/Maths/Wave/TriangleWave.gdshaderinc` + +**Method signature** +
`vec4 triangle_wave(vec4 input)` + +**Parameters** +|Name|Type|Description| +|---|---|---| +|input|vec4|Input vector| ___ \ No newline at end of file