SHARE
TWEET

Untitled

a guest Jul 19th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def raytrace (origin,pix,dep,norm,i,j):
  2.     ray_origin = origin
  3.     ray_destination = (i,j,0)
  4.     s1 = (i-ray_origin[0],j-ray_origin[1],0-ray_origin[2])
  5.     n1 = 1
  6.     n2 = 1.33
  7.  
  8.     N_surf = norm/255
  9.  
  10.     s1_mg = np.sqrt(sum(i**2 for i in s1))
  11.     N_surf_mg = np.sqrt(sum(i**2 for i in N_surf))
  12.  
  13.     #print(s1_mg,N_surf_mg)
  14.  
  15.     RI = n1/n2
  16.     N_ratio = (N_surf/N_surf_mg)
  17.     s1_ratio = (s1/s1_mg)
  18.     part_1 = np.dot(RI,np.cross(N_ratio,(np.cross(-N_ratio,s1_ratio))))
  19.     part_2 = (N_ratio)*np.sqrt(1-np.dot(np.dot((RI)**2,np.cross(N_ratio,s1_ratio)),np.cross(N_ratio,s1_ratio)))
  20.     s2 = part_1-part_2
  21.     s2_mg = np.sqrt(sum(i**2 for i in s2))
  22.  
  23.     #print(s2, s2_mg)
  24.  
  25.     theta_1 = math.acos((np.dot(np.negative(s1),N_surf))/(-s1_mg*N_surf_mg))
  26.     theta_2 = math.asin(RI * math.sin(theta_1))
  27.     #print(theta_1,theta_2)
  28.     depth_position = (i,j,dep)
  29.     s2_unit = (s2[0]/s2_mg,s2[1]/s2_mg,s2[2]/s2_mg)
  30.  
  31.     #actual ray magnitude when the depth is known
  32.     s2_final_mg = dep/math.cos(theta_2)
  33.     s2_final_vertex = (s2_final_mg * s2_unit[0],s2_final_mg * s2_unit[1],s2_final_mg * s2_unit[2])
  34.  
  35.     #print(s2_final_vertex)
  36.     return s2_final_vertex
  37.  
  38.  
  39.  
  40. def main():
  41.     path_background = "tex0.png"
  42.     path_normal = "normal_001900_1.33.npy"
  43.     path_depth = "depth_001900_1.33.npy"
  44.  
  45.     in_image = Image.open(path_background)
  46.     out_image = Image.open(path_background)
  47.  
  48.     pix_in = in_image.load()
  49.     pix_out = out_image.load()
  50.     #print(ref.size)
  51.     #print(pix_in[255,255])
  52.  
  53.  
  54.     normal = np.load(path_normal)
  55.     normal = np.array(normal)
  56.     #print(normal[0,0,:])
  57.  
  58.     depth = np.load(path_depth)
  59.     depth = np.array(depth)
  60.     #print(depth[0,0])
  61.     origin = (123,123,7) #camera right in the middle of the 256x256 image at some height
  62.  
  63.     for i in range(0,256):
  64.         for j in range(0,256):
  65.             (valx,valy,valz) = raytrace_generic(origin,pix_in[i,j],depth[i,j],normal[i,j,:],i,j)
  66.             print(valx,valy)
  67.             pix_out[i,j] = pix_in[valx*255, valy*255]
  68.  
  69.     out_image.save('example_raytrace.png')
  70.  
  71.  
  72. if __name__ == "__main__":
  73.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top