# I'm currently working on a custom depth fade node for material editor, I kind of need help

Hello Blenderartists!

I hope you’re all doing well. I’ve been working on a custom depth fade node for Blender, inspired by Unreal Engine’s functionality. I’ve made good progress so far, but I’m stuck on one particular issue.

When I try to convert a numpy array into a grayscale image output, the resulting image is always black. I’ve tried various approaches, but I can’t seem to figure out what’s causing this problem. Can anyone here provide some guidance or point me in the right direction?

Any help would be greatly appreciated. Thank you all!

You mention you’ve tried various approaches- tell us what you have tried so we don’t post a bunch of things you’ve already done. show us your code, show us your data- you haven’t given us much to go on other than “I have a problem”.

Have you tried remapping your depth range? depth is logarithmic if I’m not mistaken, which would show up as either solid white or black to the human eye.

1 Like

Hi @testure sry about that, before converting it to image, i am facing problems in ray casting, itself.

I have two codes, one that I have created myself, and one that i have searched online and found.

lets me show my code first

``````####My Code 1
def calculate_rays():
scene = bpy.context.scene
depsgraph = bpy.context.evaluated_depsgraph_get()
rays_casted = 0
hit_distances = []

camera = scene.camera

# Get the dimensions of the render resolution
width = scene.render.resolution_x
height = scene.render.resolution_y

#reducing dimensions
reduction_factor = 10
reduced_width = width // reduction_factor
reduced_height = height // reduction_factor
aspect_ratio = reduced_width / reduced_height

# Get the camera's field of view (FOV) and aspect ratio
fov = camera.data.angle

# Convert camera angle to radians

# Calculate the near plane height based on the FOV and near plane distance
near_plane_height = 2 * camera.data.clip_start * math.tan(camera_angle_radians / 2)

# Calculate the near plane width based on the aspect ratio
near_plane_width = near_plane_height * aspect_ratio

# Define the origin of the rays (camera location) and maximum length of the ray
origin = scene.camera.location
ray_length = 2000  # Increase the ray length

# Get the camera matrix
camera_matrix = scene.camera.matrix_world

# __Init__numpy
hit_distances = np.full((reduced_height, reduced_width), ray_length, dtype=float)
normalized_hit_distances = np.zeros((reduced_height, reduced_width), dtype=float)

# Convert planes to meshes
convert_planes_to_meshes()

# Create a BVH tree from the scene objects
objects = [obj for obj in bpy.data.objects if obj.type == 'MESH']
bvh = bvhtree.BVHTree.FromObject(objects[0], depsgraph)

for obj in bpy.data.objects:
if obj.type == 'MESH':
print(obj.name)

hit_count = 0

# Get the camera location
camera_location = bpy.context.scene.camera.location

for y in range(reduced_height):
for x in range(reduced_width):
# Calculate the normalized device coordinates (NDC)
ndc_x = (((2 * x) / reduced_width) - 1)*aspect_ratio
ndc_y = (((2 * y) / reduced_height) - 1)

# Calculate the view space coordinates
view_x = ndc_x * near_plane_width * 2
view_y = ndc_y * near_plane_height * 2
view_z = -camera.data.clip_start

# Create the direction vector in view space
direction_view = Vector((view_x,  view_y,  view_z))

# Transform the direction vector from view space to world space
direction_world = (camera_matrix.inverted() @ direction_view).normalized()

# Define a far-away destination point for the ray
destination = origin + direction_world * ray_length

# Perform ray casting using the BVH tree
hit, hit_location,_,_,_,_ = scene.ray_cast(depsgraph, origin, destination)

# Perform ray casting
if hit:
ray_hit_location = hit_location
ray_distance = (hit_location - origin).length
hit_count += 1

else:
ray_distance = ray_length

rays_casted += 1
hit_distances[y,x] = ray_distance

#            print(f"Ray Direction: {direction_world}")
#            print(f"Ray Origin: {origin}")

print(f"Total hit count: {ray_hit_location}")
print(f"Total hit count: {hit_count}")
print(f"Total rays casted: {rays_casted}")
print(f"Hit distances: {hit_distances}")
``````

ohk, I think in this code, I have made some mistakes in this part, from the calculation of ndc to calculation of Transforming the direction vector from view space to world space, thats why I am not getting correct hits

and the other code is taken and modified, but i dont understand that code image to world space calculation part

def calculate_rays():
scene = bpy.context.scene
depsgraph = bpy.context.evaluated_depsgraph_get()
rays_casted = 0
hit_distances = []

``````# camera object which defines ray source
cam = bpy.data.objects['Camera']

# get vectors which define view frustum of camera
frame = cam.data.view_frame(scene=bpy.context.scene)
topRight = frame[0]
bottomRight = frame[1]
bottomLeft = frame[2]
topLeft = frame[3]

ray_length = 100

# Get the camera matrix
camera_matrix = scene.camera.matrix_world

# number of pixels in X/Y direction
width = scene.render.resolution_x
height = scene.render.resolution_y

#optimize resolution
reduction_factor = 4
reduced_width = width // reduction_factor
reduced_height = height // reduction_factor

# __Init__numpy
hit_distances = np.full((reduced_height, reduced_width), ray_length, dtype=float)
normalized_hit_distances = np.zeros((reduced_height, reduced_width), dtype=float)

# Convert planes to meshes
convert_planes_to_meshes()

# Create a BVH tree from the scene objects
objects = [obj for obj in bpy.data.objects if obj.type == 'MESH']
bvh = bvhtree.BVHTree.FromObject(objects[0], depsgraph)

for obj in bpy.data.objects:
if obj.type == 'MESH':
print(obj.name)

hit_count = 0

# iterate over all X/Y coordinates
for x in range(reduced_height):
for y in range(reduced_width):
# get current pixel vector from camera center to pixel
pixelVector = Vector((x, y, topLeft[2]))

# rotate that vector according to camera rotation
pixelVector.rotate(cam.matrix_world.to_quaternion())

# calculate direction vector
direction = (pixelVector - cam.location).normalized()

# Apply perspective projection from camera space to world space
direction_world = (cam.matrix_world @ direction).normalized()
#direction_world += cam.location
direction_world *=-1

# Define a far-away destination point for the ray
destination = cam.location + direction_world * ray_length

hit, loc, _, _,_,_ = scene.ray_cast(depsgraph,cam.location, destination)

if hit==(True):
ray_distance = (loc - cam.location).length
hit_count += 1

else:
ray_distance = ray_length

rays_casted += 1
hit_distances[x,y] = ray_distance

print(f"Total hit count: {hit_count}")
print(f"Total rays casted: {rays_casted}")
print(f"Hit distances: {hit_distances}")
``````

I dont understand this code correctly, so I want to first correct my ray casting and then I can create a correct depth image

both the codes are giving differnt hits and can you please tell guide me here

Finally got it working