Skip to content

How can I make an animation? #4

Closed

Description

@Nanran

Hello!I am interested in your project name raytracer on github. I wonder how to generate "animation.gif", I can not find the source code. Could you please help me out?Thank you! I would be appreciate if you could send your all source code to me.

Activity

  1. rafael-fuente commented on Nov 27, 2020

    @rafael-fuente
    Owner

    Hi! The method that can be used to make animations is create_animation. You can found it in animation.py

    This method takes as an argument the scene you want to animate, and a function named update_scene that tells how the scene is changed every frame. This method will create an image for each frame that later you can convert into a video using FFmpeg

    Here is a script that animates example1.py

    from sightpy import *
    
    
    # First, we set up the scene to animate like in example1.py:
    
    # define materials to use
    gold_metal = Glossy(diff_color = rgb(1., .572, .184), n = vec3(0.15+3.58j, 0.4+2.37j, 1.54+1.91j), roughness = 0.0, spec_coeff = 0.2, diff_coeff= 0.8) # n = index of refraction
    bluish_metal = Glossy(diff_color = rgb(0.0, 0, 0.1), n = vec3(1.3+1.91j, 1.3+1.91j, 1.4+2.91j), roughness = 0.2,spec_coeff = 0.5, diff_coeff= 0.3)
    
    floor =  Glossy(diff_color = image("checkered_floor.png", repeat = 80.),   
    	            n = vec3(1.2+ 0.3j, 1.2+ 0.3j, 1.1+ 0.3j), roughness = 0.2, spec_coeff = 0.3, diff_coeff= 0.9 )
    
    
    # Set Scene 
    Sc = Scene(ambient_color = rgb(0.05, 0.05, 0.05))
    
    
    angle = -np.pi/2 * 0.3
    Sc.add_Camera(look_from = vec3(2.5*np.sin(angle), 0.25, 2.5*np.cos(angle)  -1.5 ), 
    			  look_at = vec3(0., 0.25, -3.), 
    	          screen_width = 400 ,
    	          screen_height = 300)
    
    
    
    Sc.add_DirectionalLight(Ldir = vec3(0.52,0.45, -0.5),  color = rgb(0.15, 0.15, 0.15))
    
    
    Sc.add(Sphere(material = gold_metal, center = vec3(-.75, .1, -3.),radius =  .6, max_ray_depth = 3))
    Sc.add(Sphere(material = bluish_metal, center = vec3(1.25, .1, -3.), radius = .6, max_ray_depth = 3))
    
    Sc.add(Plane(material = floor,  center = vec3(0, -0.5, -3.0), width = 120.0,height = 120.0, u_axis = vec3(1.0, 0, 0), v_axis = vec3(0, 0, -1.0),  max_ray_depth = 3))
    
    #see sightpy/backgrounds
    Sc.add_Background("stormydays.png")
    
    
    # Now we define the method to make the animation:
    
    def update_scene(scene , time):
       #this function updates the scene every frame
    
       # update camera position
        angle = -np.pi/2 * 0.3 + 0.1*time
        scene.camera.look_from = vec3(4.0*np.sin(angle), 0.25, 4.0*np.cos(angle)  - 3. )
    
       # update camera orientation
        scene.camera.cameraFwd = (scene.camera.look_at - scene.camera.look_from).normalize()
        scene.camera.cameraRight = (scene.camera.cameraFwd.cross(vec3(0.,1.,0.))).normalize()
        scene.camera.cameraUp = scene.camera.cameraRight.cross(scene.camera.cameraFwd)
    
    
    #Finally we render the animation:
    
    create_animation(scene = Sc, 
    	         samples_per_pixel = 5, 
    		 fps = 5,        #frames per second
    	         start_time = 0, #starting second
                     final_time = 4, #total seconds
    	         update_scene = update_scene, #function that update the scene every frame 
    	         name = "example1_frame") #name of frames
    

    This script creates a folder named "frames" that contains all frames of the animation.
    To convert them into a video run from the "frames" folder with the command prompt:
    ffmpeg -r 5 -f image2 -s 854x480 -i example1_frame_%d.png -vcodec libx264 -crf 1 -pix_fmt yuv420p example1_animated.mp4

    The video generated with this code is 5fps. For better quality use at least 30fps, although the rendering will take longer.

  2. Nanran commented on Nov 27, 2020

    @Nanran
    Author

    Thank you so much for your help! I have successfully gained the expected results.

  3. changed the title [-]Inquiry about raytracer on github[/-] [+]How can I make an animation?[/+] on Nov 28, 2020
  4. Repository owner locked and limited conversation to collaborators on Jun 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions