mirror of
https://github.com/DigvijaysinhGohil/Godot-Shader-Lib.git
synced 2025-09-19 11:56:01 +08:00
Ray march node added
This commit is contained in:
83
addons/ShaderLib/RayMarching/RayMarch.gd
Normal file
83
addons/ShaderLib/RayMarching/RayMarch.gd
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
@tool
|
||||||
|
class_name VisualShaderNodeRayMarch extends VisualShaderNodeCustom
|
||||||
|
|
||||||
|
func _get_name() -> String:
|
||||||
|
return "RayMarch"
|
||||||
|
|
||||||
|
func _get_category() -> String:
|
||||||
|
return "RayMarching"
|
||||||
|
|
||||||
|
func _get_description() -> String:
|
||||||
|
return "A simple ray marcher."
|
||||||
|
|
||||||
|
func _get_return_icon_type() -> PortType:
|
||||||
|
return PORT_TYPE_SCALAR
|
||||||
|
|
||||||
|
func _get_input_port_count() -> int:
|
||||||
|
return 6
|
||||||
|
|
||||||
|
func _get_input_port_name(port: int) -> String:
|
||||||
|
match port:
|
||||||
|
0:
|
||||||
|
return "signed distance"
|
||||||
|
1:
|
||||||
|
return "ray origin"
|
||||||
|
2:
|
||||||
|
return "ray direction"
|
||||||
|
3:
|
||||||
|
return "max steps"
|
||||||
|
4:
|
||||||
|
return "max distance"
|
||||||
|
_:
|
||||||
|
return "distance threshold"
|
||||||
|
|
||||||
|
func _get_input_port_type(port: int) -> PortType:
|
||||||
|
match port:
|
||||||
|
1, 2:
|
||||||
|
return PORT_TYPE_VECTOR_3D
|
||||||
|
3:
|
||||||
|
return PORT_TYPE_SCALAR_INT
|
||||||
|
_:
|
||||||
|
return PORT_TYPE_SCALAR
|
||||||
|
|
||||||
|
func _get_input_port_default_value(port: int) -> Variant:
|
||||||
|
match port:
|
||||||
|
1:
|
||||||
|
return Vector3(0, 0, -1)
|
||||||
|
2:
|
||||||
|
return Vector3(0 ,0 ,0)
|
||||||
|
3:
|
||||||
|
return 15
|
||||||
|
4:
|
||||||
|
return 15.0
|
||||||
|
5:
|
||||||
|
return 1e-2
|
||||||
|
_:
|
||||||
|
return null
|
||||||
|
|
||||||
|
func _get_output_port_count() -> int:
|
||||||
|
return 1
|
||||||
|
|
||||||
|
func _get_output_port_name(port: int) -> String:
|
||||||
|
return "distance"
|
||||||
|
|
||||||
|
func _get_output_port_type(port: int) -> PortType:
|
||||||
|
return PORT_TYPE_SCALAR
|
||||||
|
|
||||||
|
func _get_global_code(mode: Shader.Mode) -> String:
|
||||||
|
var code: String = preload("RayMarch.gdshaderinc").code
|
||||||
|
return code
|
||||||
|
|
||||||
|
func _get_code(input_vars: Array[String], output_vars: Array[String], mode: Shader.Mode, type: VisualShader.Type) -> String:
|
||||||
|
var signed_distance: String = "0.0"
|
||||||
|
|
||||||
|
if input_vars[0]:
|
||||||
|
signed_distance = input_vars[0]
|
||||||
|
|
||||||
|
var ray_origin: String = input_vars[1]
|
||||||
|
var ray_direction: String = input_vars[2]
|
||||||
|
var max_steps: String = input_vars[3]
|
||||||
|
var max_dist: String = input_vars[4]
|
||||||
|
var dist_threshold: String = input_vars[5]
|
||||||
|
|
||||||
|
return output_vars[0] + " = ray_march(%s, %s, %s, %s, %s, %s);" % [ray_origin, ray_direction, max_steps, max_dist, dist_threshold, signed_distance]
|
14
addons/ShaderLib/RayMarching/RayMarch.gdshaderinc
Normal file
14
addons/ShaderLib/RayMarching/RayMarch.gdshaderinc
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
float ray_march(vec3 ray_origin, vec3 ray_dir, int max_steps, float max_dist, float dist_threshold, float signed_dist) {
|
||||||
|
ray_dir = normalize(ray_dir);
|
||||||
|
dist_threshold = abs(dist_threshold);
|
||||||
|
float dist_from_origin = 0.;
|
||||||
|
float dist_to_surface;
|
||||||
|
for(int i = 0; i < max_steps; i++) {
|
||||||
|
vec3 point = ray_origin + dist_from_origin * ray_dir;
|
||||||
|
dist_to_surface = signed_dist;
|
||||||
|
dist_from_origin += dist_to_surface;
|
||||||
|
if(dist_to_surface < dist_threshold || dist_to_surface > max_dist)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return dist_from_origin;
|
||||||
|
}
|
@ -72,6 +72,10 @@ For example if you want to rotate UV in your **_.gdshader_** file, you can use `
|
|||||||
<h4><a href="/documentation/Nodes/Procedural/Shapes/RoundedPolygon.md">  Rounded Polygon node</a></h4>
|
<h4><a href="/documentation/Nodes/Procedural/Shapes/RoundedPolygon.md">  Rounded Polygon node</a></h4>
|
||||||
<h4><a href="/documentation/Nodes/Procedural/Shapes/RoundedRectangle.md">  Rounded Rectangle node</a></h4>
|
<h4><a href="/documentation/Nodes/Procedural/Shapes/RoundedRectangle.md">  Rounded Rectangle node</a></h4>
|
||||||
|
|
||||||
|
<h2>Ray marching nodes</h2>
|
||||||
|
|
||||||
|
<h4><a href="/documentation/Nodes/RayMarching/RayMarch.md"> Ray March node</a></h4>
|
||||||
|
|
||||||
<h2>UV nodes</h2>
|
<h2>UV nodes</h2>
|
||||||
|
|
||||||
<h4><a href="/documentation/Nodes/UV/Flipbook.md"> Flipbook node</a></h4>
|
<h4><a href="/documentation/Nodes/UV/Flipbook.md"> Flipbook node</a></h4>
|
||||||
|
19
documentation/Nodes/RayMarching/RayMarch.md
Normal file
19
documentation/Nodes/RayMarching/RayMarch.md
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# Ray March node
|
||||||
|
A simple ray marcher.
|
||||||
|
<hr>
|
||||||
|
|
||||||
|
**Inputs**
|
||||||
|
|Name|Type|Binding|Description|
|
||||||
|
|---|---|---|---|
|
||||||
|
|signed distance|float|none|Signed distance calculated from Signed Distance Functions (SDFs)|
|
||||||
|
|ray origin|vec3|none|Ray origin|
|
||||||
|
|ray direction|vec3|none|Normalized ray direction|
|
||||||
|
|max steps|int|none|Maximum number steps for ray marching|
|
||||||
|
|max distance|float|none|Maximum distance to march along the <b><i>ray direction</i></b>|
|
||||||
|
|distance threshold|float|none|Threshold to check against <b><i>signed distance</i></b> to determine the ray intersection point.|
|
||||||
|
|
||||||
|
**Outputs**
|
||||||
|
|Name|Type|Binding|Description|
|
||||||
|
|---|---|---|---|
|
||||||
|
|distance|float|None|Output ray intersection distance|
|
||||||
|
___
|
Reference in New Issue
Block a user