Guest User

Untitled

a guest
Jun 25th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. # hair.rb NB: run this script directly with jruby
  2.  
  3. require 'library/sunflow_api/library/sunflow_api.jar'
  4. require 'library/sunflow/library/sunflow.jar'
  5. require 'library/sunflow/library/janino.jar'
  6.  
  7. class BasicHair
  8.  
  9. API = Java::Com::briansteen::SunflowAPIAPI
  10. SMath = Java::Org::sunflow::math
  11. JColor = java.awt.Color
  12.  
  13. attr_reader :width, :height, :n_particles, :sunflow
  14.  
  15. def initialize width = 640, height = 480
  16. @width = width
  17. @height = height
  18. @n_particles = 20
  19. # create a new API instance
  20. @sunflow = API.new
  21. # set width and height
  22.  
  23. @sunflow.set_width(width)
  24. @sunflow.set_height(height)
  25. # set background color
  26. @sunflow.set_background(1, 1, 1)
  27. # set camera
  28. @sunflow.set_camera_position(0, 7, 5)
  29. @sunflow.set_camera_target(2, 0.5, 0)
  30. @sunflow.set_thinlens_camera("thinLensCamera", 50, width/height)
  31. # set basic light
  32. @sunflow.set_point_light("myPointLight", SMath::Point3.new(0, 5, 5),
  33. JColor.new(255, 255, 255))
  34. @sunflow.set_directional_light("myDirectionalLight", SMath::Point3.new(-2, 3, 0),
  35. SMath::Vector3.new(0, 0, 0), 3, JColor.new(1, 1, 1))
  36. # @sunflow.setSphereLight("mySphereLight", SMath::Point3.new(0, 30, -5),
  37. # JColor.new(0, 0, 255), 32, 10)
  38. # draw a ground plane
  39. @sunflow.draw_plane("ground", SMath::Point3.new(0, 0, 0), SMath::Vector3.new(0, 1, 0))
  40. # coordinates array
  41.  
  42. @sunflow.draw_box("boxname", 0, 0, 0, 1)
  43. end
  44.  
  45. def create_scene
  46. hair_widths = [0.025]
  47. # create particle coordinates
  48. 350.times do |j|
  49. # particle start position
  50. particle_x = Math.cos(j * 0.5) * j * 0.0015
  51. particle_y = 0
  52. particle_z = Math.sin(j * 0.5) * j * 0.0015
  53.  
  54. hair_coordinates = Array.new(n_particles * 3)
  55.  
  56. array_index = -1
  57.  
  58. n_particles.times do |i|
  59. particle_x += 0.1 + Math.cos(i * 0.15 + j * 0.05) * 0.13
  60. particle_y -= Math.sin(particle_z * 0.01 + j * 0.05) * 0.125 +
  61. Math.cos(i * 0.5 + particle_y) * 0.125
  62. particle_z += Math.sin(i) * 0.25 + particle_y * 0.01
  63. hair_coordinates[array_index += 1] = particle_x
  64. hair_coordinates[array_index += 1] = particle_y
  65. hair_coordinates[array_index += 1] = particle_z
  66. end
  67.  
  68. # set ambient occlusion shader
  69. @sunflow.setAmbientOcclusionShader("myAmbientOcclusionShader#{j}", JColor.new(55, 55, 55),
  70. JColor.new(0, 0, 0), 16, 1)
  71. # set glass shader
  72. # @sunflow.setGlassShader("myGlassShader", JColor.new(1, 1, 1), 2.5, 3, JColor.new(1, 1, 1))
  73. # set shiny-diffuse shader
  74. # @sunflow.setShinyDiffuseShader("myShinyShader", JColor.new(55, 55, 55), 0.8)
  75.  
  76. # draw object
  77. @sunflow.draw_hair("hair#{j}", n_particles - 2, hair_coordinates.to_java(:float),
  78. hair_widths.to_java(:float))
  79. end
  80. end
  81.  
  82. def render_scene filename = nil
  83. sunflow.setIrradianceCacheGIEngine(32, 0.4, 1, 15, nil)
  84. # render
  85. sunflow.render() unless filename
  86. if filename
  87. begin # test for dodgy filename/path
  88. file = File.new(filename, "w")
  89. file.close
  90. sunflow.render(filename) # save as png image
  91. rescue
  92. puts "Warning #{filename} is not writable"
  93. end
  94. end
  95. end
  96.  
  97. end
  98.  
  99. hair = BasicHair.new 200, 200 # NB: render small is useful for quick for checking
  100. hair.create_scene
  101. hair.render_scene # default is to render in sunflow frame ie not to file
Add Comment
Please, Sign In to add comment