Hi, I'm trying to create a CollisionHeightMap based on my current heightmap calculations. Here is the code.
@export var terrain : MeshInstance3D
var _simplex3dNoise : NoiseTexture2D
var _cellular2dNoise : NoiseTexture2D
var _cellular_height : Image
var _simplex_height : Image
var Height_Dune
var Height_Detail
var collisoin_decimation := 2
var material : ShaderMaterial
var offsetUVs : Vector2
@export var sc : float = 2
func _ready():
material = terrain.get_surface_override_material(0)
_cellular2dNoise = material.get_shader_parameter("_cellular2dNoise") as NoiseTexture2D
_simplex3dNoise = material.get_shader_parameter("_simplex3dNoise") as NoiseTexture2D
await _cellular2dNoise.changed
await _simplex3dNoise.changed
create_collision()
get_parent().start_generating_collision = true
func create_collision():
var float_array : PackedFloat32Array = []
Height_Dune = material.get_shader_parameter("Height_Dune")
Height_Detail = material.get_shader_parameter("Height_Detail")
scale.x = terrain.scale.x * 2243/_cellular2dNoise.get_height() * collisoin_decimation
scale.z = terrain.scale.z * 2243/_cellular2dNoise.get_width()* collisoin_decimation
_cellular_height = _cellular2dNoise.get_image() as Image
_simplex_height = _simplex3dNoise.get_image() as Image
var collision_height = _cellular_height.get_height()/ collisoin_decimation + 1
var collision_width = _simplex_height.get_width()/ collisoin_decimation + 1
var shap = HeightMapShape3D.new()
shape = shap
shap.map_width = collision_width
shap.map_depth = collision_height
for y in collision_height:
for x in collision_width:
var c = Vector2(fposmod(x,collision_height ), fposmod(y,collision_width))
float_array.push_back(CalculateHeight(c))
shape.map_data = float_array
#create_collision_anchors
func CalculateHeight(uv : Vector2) -> float:
var offsetUV = uv + offsetUVs
var SimplexNoise = _simplex_height.get_pixel(offsetUV.x, offsetUV.y).r;
var simplexed_uv = lerp(offsetUV, Vector2(SimplexNoise, SimplexNoise), .1);
var DunesWorleyNoise = _cellular_height.get_pixel(simplexed_uv.x, simplexed_uv.y).r * Height_Dune;
var DetailWorleyNoise = _cellular_height.get_pixel(simplexed_uv.x, simplexed_uv.y * 0.05).r;
var DetailSimplexNoise = _simplex_height.get_pixel(simplexed_uv.x, simplexed_uv.y).r;
var overlayed_details
if (DetailWorleyNoise < 0.5):
overlayed_details = 2.0 * DetailWorleyNoise * DetailSimplexNoise;
else:
overlayed_details = 1.0 - 2.0 * (1.0 - DetailSimplexNoise) * (1.0 - DetailWorleyNoise);
overlayed_details *= Height_Detail;
var overlayed_height;
if (DunesWorleyNoise < 0.5):
overlayed_height = 2.0 * DunesWorleyNoise * overlayed_details;
else:
overlayed_height = 1.0 - 2.0 * (1.0 - overlayed_details) * (1.0 - DunesWorleyNoise);
return overlayed_height;
