mittsu-opengl 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +18 -0
  3. data/.github/workflows/build-workflow.yml +67 -0
  4. data/.gitignore +12 -0
  5. data/.rubocop.yml +1158 -0
  6. data/CODE_OF_CONDUCT.md +13 -0
  7. data/Gemfile +9 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +195 -0
  10. data/Rakefile +8 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +7 -0
  13. data/install-glfw.ps1 +13 -0
  14. data/lib/mittsu/generic_lib.rb +116 -0
  15. data/lib/mittsu/glfw/lib.rb +58 -0
  16. data/lib/mittsu/glfw/window.rb +231 -0
  17. data/lib/mittsu/opengl/buffer.rb +13 -0
  18. data/lib/mittsu/opengl/default_target.rb +50 -0
  19. data/lib/mittsu/opengl/geometry_group.rb +758 -0
  20. data/lib/mittsu/opengl/geometry_like.rb +132 -0
  21. data/lib/mittsu/opengl/gl_debug.rb +85 -0
  22. data/lib/mittsu/opengl/gl_extensions.rb +42 -0
  23. data/lib/mittsu/opengl/gl_mittsu_params.rb +53 -0
  24. data/lib/mittsu/opengl/helper.rb +120 -0
  25. data/lib/mittsu/opengl/implementation.rb +31 -0
  26. data/lib/mittsu/opengl/lib.rb +19 -0
  27. data/lib/mittsu/opengl/light_renderer.rb +43 -0
  28. data/lib/mittsu/opengl/material_basics.rb +57 -0
  29. data/lib/mittsu/opengl/plugins/shadow_map_plugin.rb +416 -0
  30. data/lib/mittsu/opengl/plugins/sprite_fragment.glsl +38 -0
  31. data/lib/mittsu/opengl/plugins/sprite_plugin.rb +250 -0
  32. data/lib/mittsu/opengl/plugins/sprite_vertex.glsl +31 -0
  33. data/lib/mittsu/opengl/program.rb +250 -0
  34. data/lib/mittsu/opengl/renderer.rb +1028 -0
  35. data/lib/mittsu/opengl/shader/chunk.rb +11 -0
  36. data/lib/mittsu/opengl/shader/chunks/alphamap_fragment.glsl +5 -0
  37. data/lib/mittsu/opengl/shader/chunks/alphamap_pars_fragment.glsl +5 -0
  38. data/lib/mittsu/opengl/shader/chunks/alphatest_fragment.glsl +5 -0
  39. data/lib/mittsu/opengl/shader/chunks/bumpmap_pars_fragment.glsl +40 -0
  40. data/lib/mittsu/opengl/shader/chunks/color_fragment.glsl +5 -0
  41. data/lib/mittsu/opengl/shader/chunks/color_pars_fragment.glsl +5 -0
  42. data/lib/mittsu/opengl/shader/chunks/color_pars_vertex.glsl +5 -0
  43. data/lib/mittsu/opengl/shader/chunks/color_vertex.glsl +5 -0
  44. data/lib/mittsu/opengl/shader/chunks/common.glsl +60 -0
  45. data/lib/mittsu/opengl/shader/chunks/default_vertex.glsl +15 -0
  46. data/lib/mittsu/opengl/shader/chunks/defaultnormal_vertex.glsl +21 -0
  47. data/lib/mittsu/opengl/shader/chunks/envmap_fragment.glsl +62 -0
  48. data/lib/mittsu/opengl/shader/chunks/envmap_pars_fragment.glsl +21 -0
  49. data/lib/mittsu/opengl/shader/chunks/envmap_pars_vertex.glsl +7 -0
  50. data/lib/mittsu/opengl/shader/chunks/envmap_vertex.glsl +17 -0
  51. data/lib/mittsu/opengl/shader/chunks/fog_fragment.glsl +26 -0
  52. data/lib/mittsu/opengl/shader/chunks/fog_pars_fragment.glsl +15 -0
  53. data/lib/mittsu/opengl/shader/chunks/lightmap_fragment.glsl +5 -0
  54. data/lib/mittsu/opengl/shader/chunks/lightmap_pars_fragment.glsl +6 -0
  55. data/lib/mittsu/opengl/shader/chunks/lightmap_pars_vertex.glsl +5 -0
  56. data/lib/mittsu/opengl/shader/chunks/lightmap_vertex.glsl +5 -0
  57. data/lib/mittsu/opengl/shader/chunks/lights_lambert_pars_vertex.glsl +43 -0
  58. data/lib/mittsu/opengl/shader/chunks/lights_lambert_vertex.glsl +196 -0
  59. data/lib/mittsu/opengl/shader/chunks/lights_phong_fragment.glsl +243 -0
  60. data/lib/mittsu/opengl/shader/chunks/lights_phong_pars_fragment.glsl +58 -0
  61. data/lib/mittsu/opengl/shader/chunks/lights_phong_pars_vertex.glsl +5 -0
  62. data/lib/mittsu/opengl/shader/chunks/lights_phong_vertex.glsl +5 -0
  63. data/lib/mittsu/opengl/shader/chunks/linear_to_gamma_fragment.glsl +2 -0
  64. data/lib/mittsu/opengl/shader/chunks/logdepthbuf_fragment.glsl +5 -0
  65. data/lib/mittsu/opengl/shader/chunks/logdepthbuf_pars_fragment.glsl +12 -0
  66. data/lib/mittsu/opengl/shader/chunks/logdepthbuf_pars_vertex.glsl +11 -0
  67. data/lib/mittsu/opengl/shader/chunks/logdepthbuf_vertex.glsl +15 -0
  68. data/lib/mittsu/opengl/shader/chunks/map_fragment.glsl +9 -0
  69. data/lib/mittsu/opengl/shader/chunks/map_pars_fragment.glsl +11 -0
  70. data/lib/mittsu/opengl/shader/chunks/map_pars_vertex.glsl +6 -0
  71. data/lib/mittsu/opengl/shader/chunks/map_particle_fragment.glsl +5 -0
  72. data/lib/mittsu/opengl/shader/chunks/map_particle_pars_fragment.glsl +6 -0
  73. data/lib/mittsu/opengl/shader/chunks/map_vertex.glsl +5 -0
  74. data/lib/mittsu/opengl/shader/chunks/morphnormal_vertex.glsl +12 -0
  75. data/lib/mittsu/opengl/shader/chunks/morphtarget_pars_vertex.glsl +13 -0
  76. data/lib/mittsu/opengl/shader/chunks/morphtarget_vertex.glsl +20 -0
  77. data/lib/mittsu/opengl/shader/chunks/normalmap_pars_fragment.glsl +27 -0
  78. data/lib/mittsu/opengl/shader/chunks/shadowmap_fragment.glsl +216 -0
  79. data/lib/mittsu/opengl/shader/chunks/shadowmap_pars_fragment.glsl +19 -0
  80. data/lib/mittsu/opengl/shader/chunks/shadowmap_pars_vertex.glsl +6 -0
  81. data/lib/mittsu/opengl/shader/chunks/shadowmap_vertex.glsl +9 -0
  82. data/lib/mittsu/opengl/shader/chunks/skinbase_vertex.glsl +8 -0
  83. data/lib/mittsu/opengl/shader/chunks/skinning_pars_vertex.glsl +47 -0
  84. data/lib/mittsu/opengl/shader/chunks/skinning_vertex.glsl +20 -0
  85. data/lib/mittsu/opengl/shader/chunks/skinnormal_vertex.glsl +20 -0
  86. data/lib/mittsu/opengl/shader/chunks/specularmap_fragment.glsl +12 -0
  87. data/lib/mittsu/opengl/shader/chunks/specularmap_pars_fragment.glsl +5 -0
  88. data/lib/mittsu/opengl/shader/chunks/worldpos_vertex.glsl +17 -0
  89. data/lib/mittsu/opengl/shader/lib/basic/basic_fragment.rbsl +37 -0
  90. data/lib/mittsu/opengl/shader/lib/basic/basic_uniforms.rbslu +3 -0
  91. data/lib/mittsu/opengl/shader/lib/basic/basic_vertex.rbsl +33 -0
  92. data/lib/mittsu/opengl/shader/lib/cube/cube_fragment.rbsl +12 -0
  93. data/lib/mittsu/opengl/shader/lib/cube/cube_uniforms.rbslu +2 -0
  94. data/lib/mittsu/opengl/shader/lib/cube/cube_vertex.rbsl +12 -0
  95. data/lib/mittsu/opengl/shader/lib/depth_rgba/depth_rgba_fragment.rbsl +26 -0
  96. data/lib/mittsu/opengl/shader/lib/depth_rgba/depth_rgba_uniforms.rbslu +0 -0
  97. data/lib/mittsu/opengl/shader/lib/depth_rgba/depth_rgba_vertex.rbsl +12 -0
  98. data/lib/mittsu/opengl/shader/lib/lambert/lambert_fragment.rbsl +56 -0
  99. data/lib/mittsu/opengl/shader/lib/lambert/lambert_uniforms.rbslu +7 -0
  100. data/lib/mittsu/opengl/shader/lib/lambert/lambert_vertex.rbsl +37 -0
  101. data/lib/mittsu/opengl/shader/lib/particle_basic/particle_basic_fragment.rbsl +27 -0
  102. data/lib/mittsu/opengl/shader/lib/particle_basic/particle_basic_uniforms.rbslu +2 -0
  103. data/lib/mittsu/opengl/shader/lib/particle_basic/particle_basic_vertex.rbsl +25 -0
  104. data/lib/mittsu/opengl/shader/lib/phong/phong_fragment.rbsl +45 -0
  105. data/lib/mittsu/opengl/shader/lib/phong/phong_uniforms.rbslu +11 -0
  106. data/lib/mittsu/opengl/shader/lib/phong/phong_vertex.rbsl +43 -0
  107. data/lib/mittsu/opengl/shader/lib.rb +45 -0
  108. data/lib/mittsu/opengl/shader/rbsl_loader.rb +168 -0
  109. data/lib/mittsu/opengl/shader/templates/fragment.glsl.erb +105 -0
  110. data/lib/mittsu/opengl/shader/templates/vertex.glsl.erb +143 -0
  111. data/lib/mittsu/opengl/shader/uniforms_lib.rb +86 -0
  112. data/lib/mittsu/opengl/shader/uniforms_utils.rb +31 -0
  113. data/lib/mittsu/opengl/shader.rb +56 -0
  114. data/lib/mittsu/opengl/state.rb +205 -0
  115. data/lib/mittsu/opengl/version.rb +5 -0
  116. data/lib/mittsu/opengl.rb +2 -0
  117. data/lib/mittsu/opengl_implementation/core/buffer_geometry.rb +11 -0
  118. data/lib/mittsu/opengl_implementation/core/geometry.rb +346 -0
  119. data/lib/mittsu/opengl_implementation/core/object_3d.rb +134 -0
  120. data/lib/mittsu/opengl_implementation/lights/ambient_light.rb +26 -0
  121. data/lib/mittsu/opengl_implementation/lights/directional_light.rb +35 -0
  122. data/lib/mittsu/opengl_implementation/lights/hemisphere_light.rb +39 -0
  123. data/lib/mittsu/opengl_implementation/lights/light.rb +55 -0
  124. data/lib/mittsu/opengl_implementation/lights/point_light.rb +36 -0
  125. data/lib/mittsu/opengl_implementation/lights/spot_light.rb +47 -0
  126. data/lib/mittsu/opengl_implementation/materials/line_basic_material.rb +16 -0
  127. data/lib/mittsu/opengl_implementation/materials/material.rb +274 -0
  128. data/lib/mittsu/opengl_implementation/materials/mesh_basic_material.rb +21 -0
  129. data/lib/mittsu/opengl_implementation/materials/mesh_lambert_material.rb +33 -0
  130. data/lib/mittsu/opengl_implementation/materials/mesh_phong_material.rb +44 -0
  131. data/lib/mittsu/opengl_implementation/materials/point_cloud_material.rb +27 -0
  132. data/lib/mittsu/opengl_implementation/materials/shader_material.rb +11 -0
  133. data/lib/mittsu/opengl_implementation/objects/group.rb +9 -0
  134. data/lib/mittsu/opengl_implementation/objects/line.rb +45 -0
  135. data/lib/mittsu/opengl_implementation/objects/mesh.rb +70 -0
  136. data/lib/mittsu/opengl_implementation/objects/point_cloud.rb +39 -0
  137. data/lib/mittsu/opengl_implementation/objects/sprite.rb +12 -0
  138. data/lib/mittsu/opengl_implementation/scenes/scene.rb +9 -0
  139. data/lib/mittsu/opengl_implementation/textures/compressed_texture.rb +20 -0
  140. data/lib/mittsu/opengl_implementation/textures/cube_texture.rb +77 -0
  141. data/lib/mittsu/opengl_implementation/textures/data_texture.rb +21 -0
  142. data/lib/mittsu/opengl_implementation/textures/render_target.rb +124 -0
  143. data/lib/mittsu/opengl_implementation/textures/texture.rb +107 -0
  144. data/mittsu-opengl.gemspec +36 -0
  145. metadata +314 -0
@@ -0,0 +1,134 @@
1
+ module Mittsu
2
+ class Object3D
3
+ attr_accessor :morph_target_influences, :renderer, :initted
4
+ attr_reader :model_view_matrix
5
+ attr_writer :active
6
+
7
+ def active?
8
+ @active
9
+ end
10
+
11
+ def init
12
+ if !@initted
13
+ puts " --- INIT #{self.name}" if DEBUG
14
+
15
+ @initted = true
16
+ @model_view_matrix = Matrix4.new
17
+ @normal_matrix = Matrix3.new
18
+
19
+ add_event_listener(:removed, @renderer.method(:on_object_removed))
20
+ end
21
+
22
+ if geometry.nil?
23
+ # ImmediateRenderObject
24
+ else
25
+ if !geometry.initted
26
+ geometry.initted = true
27
+ geometry.add_event_listener(:dispose, @renderer.method(:on_geometry_dispose))
28
+ if geometry.is_a?(BufferGeometry)
29
+ @renderer.info[:memory][:geometries] += 1
30
+ else
31
+ init_geometry
32
+ end
33
+ # TODO: when PointCloud exists
34
+ # when PointCloud
35
+ # if geometry[:_opengl_vertex_buffer].nil?
36
+ # create_particle_buffers(geometry)
37
+ # init_particle_buffers(geometry, object)
38
+ #
39
+ # geometry.vertices_need_update = true
40
+ # geometry.colors_need_update = true
41
+ # end
42
+ end
43
+ end
44
+
45
+ if !@active
46
+ @active = true
47
+
48
+ add_opengl_object
49
+ # TODO: when ImmediateRenderObject exists
50
+ # if object.is_a? ImmediateRenderObject || object.immediate_render_callback
51
+ # add_buffer_immediate(@renderer.instance_variable_get(:@_opengl_objects_immediate), self)
52
+ # end
53
+ end
54
+ end
55
+
56
+ def project(renderer)
57
+ puts " --- PROJECT #{self.name}" if DEBUG
58
+ @renderer = renderer
59
+ return unless visible
60
+ init
61
+
62
+ # TODO!!! FIXME!!!
63
+ opengl_objects = @renderer.instance_variable_get(:@_opengl_objects)[id]
64
+
65
+ if opengl_objects && (!frustum_culled || @renderer.object_in_frustum?(self))
66
+ opengl_objects.each do |opengl_object|
67
+ # TODO!!! FIXME!!!
68
+ @renderer.send(:unroll_buffer_material, opengl_object)
69
+
70
+ opengl_object.render = true
71
+ if @renderer.sort_objects?
72
+ @_vector3 ||= Vector3.new
73
+
74
+ @_vector3.set_from_matrix_position(matrix_world)
75
+ @_vector3.apply_projection(@renderer.proj_screen_matrix)
76
+
77
+ opengl_object[:z] = @_vector3.z
78
+ end
79
+ end
80
+ end
81
+
82
+ project_children
83
+ end
84
+
85
+ def setup_matrices(camera)
86
+ @model_view_matrix.multiply_matrices(camera.matrix_world_inverse, matrix_world)
87
+ @normal_matrix.normal_matrix(@model_view_matrix)
88
+ @model_view_matrix
89
+ end
90
+
91
+ def load_uniforms_matrices(uniforms)
92
+ GL.UniformMatrix4fv(uniforms['modelViewMatrix'],
93
+ 1, GL::FALSE,
94
+ array_to_ptr_easy(@model_view_matrix.elements))
95
+
96
+ if uniforms['normalMatrix']
97
+ GL.UniformMatrix3fv(uniforms['normalMatrix'],
98
+ 1, GL::FALSE,
99
+ array_to_ptr_easy(@normal_matrix.elements))
100
+ end
101
+ end
102
+
103
+ def buffer_material(geometry_group)
104
+ if material.is_a?(MeshFaceMaterial)
105
+ material.materials[geometry_group.material_index]
106
+ else
107
+ material
108
+ end
109
+ end
110
+
111
+ def init_geometry
112
+ # NOOP
113
+ end
114
+
115
+ def add_opengl_object
116
+ # NOOP
117
+ end
118
+
119
+ def deinit
120
+ @initted = nil
121
+ @model_view_matrix = nil
122
+ @normal_matrix = nil
123
+ @active = nil
124
+ end
125
+
126
+ protected
127
+
128
+ def project_children
129
+ children.each do |child|
130
+ child.project(@renderer)
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,26 @@
1
+ module Mittsu
2
+ class AmbientLight
3
+ TYPE = :ambient
4
+
5
+ class Cache < Struct.new(:length, :count, :value)
6
+ def initialize
7
+ super(0, 0, [0.0, 0.0, 0.0])
8
+ end
9
+
10
+ def reset
11
+ self.length = 0
12
+ self.value[0] = 0.0
13
+ self.value[1] = 0.0
14
+ self.value[2] = 0.0
15
+ end
16
+ end
17
+
18
+ def setup_specific(_)
19
+ @cache.value[0] += color.r
20
+ @cache.value[1] += color.g
21
+ @cache.value[2] += color.b
22
+ end
23
+
24
+ def self.null_remaining_lights(_); end
25
+ end
26
+ end
@@ -0,0 +1,35 @@
1
+ module Mittsu
2
+ class DirectionalLight
3
+ TYPE = :directional
4
+
5
+ class Cache < Struct.new(:length, :count, :colors, :positions)
6
+ def initialize
7
+ super(0, 0, [], [])
8
+ end
9
+
10
+ def reset
11
+ self.length = 0
12
+ end
13
+ end
14
+
15
+ def setup_specific(index)
16
+ offset = index * 3
17
+
18
+ @_direction.set_from_matrix_position(matrix_world)
19
+ @_vector3.set_from_matrix_position(target.matrix_world)
20
+ @_direction.sub(@_vector3)
21
+ @_direction.normalize
22
+
23
+ positions = @cache.positions
24
+ positions[offset] = @_direction.x
25
+ positions[offset + 1] = @_direction.y
26
+ positions[offset + 2] = @_direction.z
27
+
28
+ OpenGL::Helper.set_color_linear(@cache.colors, offset, color, intensity)
29
+ end
30
+
31
+ def to_sym
32
+ :directional
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,39 @@
1
+ module Mittsu
2
+ class HemisphereLight
3
+ TYPE = :hemi
4
+
5
+ class Cache < Struct.new(:length, :count, :sky_colors, :ground_colors, :positions)
6
+ def initialize
7
+ super(0, 0, [], [], [])
8
+ end
9
+
10
+ def reset
11
+ self.length = 0
12
+ end
13
+ end
14
+
15
+ def setup_specific(index)
16
+ offset = index * 3
17
+
18
+ @_direction.set_from_matrix_position(matrix_world)
19
+ @_direction.normalize
20
+
21
+ positions = @cache.positions
22
+ positions[offset] = @_direction.x
23
+ positions[offset + 1] = @_direction.y
24
+ positions[offset + 2] = @_direction.z
25
+
26
+ OpenGL::Helper.set_color_linear(@cache.sky_colors, offset, color, intensity)
27
+ OpenGL::Helper.set_color_linear(@cache.ground_colors, offset, ground_color, intensity)
28
+ end
29
+
30
+ def self.null_remaining_lights(cache)
31
+ super(cache, cache.ground_colors)
32
+ super(cache, cache.sky_colors)
33
+ end
34
+
35
+ def to_sym
36
+ :hemi
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,55 @@
1
+ module Mittsu
2
+ class Light
3
+ attr_accessor :camera_helper
4
+
5
+ # def initialize(light, renderer)
6
+ # super
7
+ # @light = light
8
+ # @light_renderer = renderer.light_renderer
9
+ #
10
+ #
11
+ # @_direction = Vector3.new
12
+ # @_vector3 = Vector3.new
13
+ # end
14
+
15
+ def type
16
+ self.class::TYPE
17
+ end
18
+
19
+ def setup(light_renderer)
20
+ @light_renderer = light_renderer
21
+ @cache ||= @light_renderer.cache[type]
22
+ @cache.count += 1
23
+
24
+ return unless visible
25
+
26
+ @_direction ||= Vector3.new
27
+ @_vector3 ||= Vector3.new
28
+
29
+ setup_specific(@cache.length)
30
+
31
+ @cache.length += 1
32
+ end
33
+
34
+ def project(renderer)
35
+ @renderer = renderer
36
+ return unless visible
37
+ init
38
+ # TODO!!! FIXME!!!
39
+ @renderer.instance_variable_get(:@lights) << self
40
+ project_children
41
+ end
42
+
43
+ def self.null_remaining_lights(cache, colors = nil)
44
+ colors ||= cache.colors
45
+ count = [colors.length, cache.count * 3].max
46
+ (cache.length * 3).upto(count - 1).each { |i|
47
+ colors[i] = 0.0
48
+ }
49
+ end
50
+
51
+ def to_sym
52
+ :other
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,36 @@
1
+ module Mittsu
2
+ class PointLight
3
+ TYPE = :point
4
+
5
+ class Cache < Struct.new(:length, :count, :colors, :distances, :positions, :decays)
6
+ def initialize
7
+ super(0, 0, [], [], [], [])
8
+ end
9
+
10
+ def reset
11
+ self.length = 0
12
+ end
13
+ end
14
+
15
+ def setup_specific(index)
16
+ offset = index * 3;
17
+
18
+ OpenGL::Helper.set_color_linear(@cache.colors, offset, color, intensity)
19
+
20
+ @_vector3.set_from_matrix_position(matrix_world)
21
+
22
+ positions = @cache.positions
23
+ positions[offset] = @_vector3.x
24
+ positions[offset + 1] = @_vector3.y
25
+ positions[offset + 2] = @_vector3.z
26
+
27
+ # distance is 0 if decay is 0, because there is no attenuation at all.
28
+ @cache.distances[index] = distance
29
+ @cache.decays[index] = distance.zero? ? 0.0 : decay
30
+ end
31
+
32
+ def to_sym
33
+ :point
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,47 @@
1
+ module Mittsu
2
+ class SpotLight
3
+ TYPE = :spot
4
+
5
+ class Cache < Struct.new(:length, :count, :colors, :directions, :distances, :positions, :exponents, :angles_cos, :decays)
6
+ def initialize
7
+ super(0, 0, [], [], [], [], [], [], [])
8
+ end
9
+
10
+ def reset
11
+ self.length = 0
12
+ end
13
+ end
14
+
15
+ def setup_specific(index)
16
+ offset = index * 3
17
+
18
+ OpenGL::Helper.set_color_linear(@cache.colors, offset, color, intensity)
19
+
20
+ @_direction.set_from_matrix_position(matrix_world)
21
+
22
+ positions = @cache.positions
23
+ positions[offset] = @_direction.x
24
+ positions[offset + 1] = @_direction.y
25
+ positions[offset + 2] = @_direction.z
26
+
27
+ @cache.distances[index] = distance
28
+
29
+ @_vector3.set_from_matrix_position(target.matrix_world)
30
+ @_direction.sub(@_vector3)
31
+ @_direction.normalize
32
+
33
+ directions = @cache.directions
34
+ directions[offset] = @_direction.x
35
+ directions[offset + 1] = @_direction.y
36
+ directions[offset + 2] = @_direction.z
37
+
38
+ @cache.angles_cos[index] = ::Math.cos(angle)
39
+ @cache.exponents[index] = exponent;
40
+ @cache.decays[index] = distance.zero? ? 0.0 : decay
41
+ end
42
+
43
+ def to_sym
44
+ :spot
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,16 @@
1
+ module Mittsu
2
+ class LineBasicMaterial
3
+ def refresh_uniforms(uniforms)
4
+ uniforms['diffuse'].value = color
5
+ uniforms['opacity'].value = opacity
6
+ end
7
+
8
+ def init_shader
9
+ @shader = OpenGL::Shader::Lib.create_shader(shader_id)
10
+ end
11
+
12
+ def shader_id
13
+ :basic
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,274 @@
1
+ module Mittsu
2
+ class Material
3
+ # TODO: init_shader for these material-types
4
+ # MeshDepthMaterial => :depth, # TODO...
5
+ # MeshNormalMaterial => :normal, # TODO...
6
+ # LineDashedMaterial => :dashed, # TODO...
7
+ # PointCloudMaterial => :particle_basic # TODO...
8
+
9
+ attr_accessor :shadow_pass
10
+ attr_reader :shader, :uniforms_list
11
+
12
+ def init(lights, fog, object, renderer)
13
+ @renderer = renderer
14
+
15
+ add_event_listener(:dispose, @renderer.method(:on_material_dispose))
16
+
17
+ init_shader
18
+
19
+ self.program = find_or_create_program(lights, fog, object)
20
+
21
+ count_supported_morph_attributes(program.attributes)
22
+
23
+ @uniforms_list = get_uniforms_list
24
+ end
25
+
26
+ def set(renderer)
27
+ @renderer = renderer
28
+
29
+ if transparent
30
+ @renderer.state.set_blending(blending, blend_equation, blend_src, blend_dst, blend_equation_alpha, blend_src_alpha, blend_dst_alpha)
31
+ else
32
+ @renderer.state.set_blending(NoBlending)
33
+ end
34
+
35
+ @renderer.state.set_depth_test(depth_test)
36
+ @renderer.state.set_depth_write(depth_write)
37
+ @renderer.state.set_color_write(color_write)
38
+ @renderer.state.set_polygon_offset(polygon_offset, polygon_offset_factor, polygon_offset_units)
39
+ end
40
+
41
+ def needs_face_normals?
42
+ shading == FlatShading
43
+ end
44
+
45
+ def clear_custom_attributes
46
+ attributes.each do |attribute|
47
+ attribute.needs_update = false
48
+ end
49
+ end
50
+
51
+ def custom_attributes_dirty?
52
+ attributes.each do |attribute|
53
+ return true if attribute.needs_update
54
+ end
55
+ false
56
+ end
57
+
58
+ def refresh_uniforms(_)
59
+ # NOOP
60
+ end
61
+
62
+ def needs_camera_position_uniform?
63
+ env_map
64
+ end
65
+
66
+ def needs_view_matrix_uniform?
67
+ skinning
68
+ end
69
+
70
+ def needs_lights?
71
+ lights
72
+ end
73
+
74
+ protected
75
+
76
+ def init_shader
77
+ @shader = {
78
+ uniforms: uniforms,
79
+ vertex_shader: vertex_shader,
80
+ fragment_shader: fragment_shader
81
+ }
82
+ end
83
+
84
+ def shader_id
85
+ nil
86
+ end
87
+
88
+ private
89
+
90
+ def allocate_lights(lights)
91
+ lights.reject { |light|
92
+ light.only_shadow || !light.visible
93
+ }.each_with_object({
94
+ directional: 0, point: 0, spot: 0, hemi: 0, other: 0
95
+ }) { |light, counts|
96
+ counts[light.to_sym] += 1
97
+ }
98
+ end
99
+
100
+ def allocate_shadows(lights)
101
+ max_shadows = 0
102
+
103
+ lights.each do |light|
104
+ next unless light.cast_shadow
105
+
106
+ max_shadows += 1 if light.is_a?(SpotLight)
107
+ max_shadows += 1 if light.is_a?(DirectionalLight) && !light.shadow_cascade
108
+ end
109
+
110
+ max_shadows
111
+ end
112
+
113
+ def allocate_bones(object = nil)
114
+ if @renderer.supports_bone_textures? && object && object.skeleton && object.skeleton.use_vertex_texture
115
+ return 1024
116
+ end
117
+
118
+ # default for when object is not specified
119
+ # ( for example when prebuilding shader
120
+ # to be used with multiple objects )
121
+ #
122
+ # - leave some extra space for other uniforms
123
+ # - limit here is ANGLE's 254 max uniform vectors
124
+ # (up to 54 should be safe)
125
+
126
+ n_vertex_uniforms = (GL.GetParameter(GL::MAX_VERTEX_UNIFORM_COMPONENTS) / 4.0).floor
127
+ n_vertex_matrices = ((n_vertex_uniforms - 20) / 4.0).floor
128
+
129
+ max_bones = n_vertex_matrices
130
+
131
+ # TODO: when SkinnedMesh exists
132
+ # if !object.nil? && object.is_a?(SkinnedMesh)
133
+ # max_bones = [object.skeleton.bones.length, max_bones].min
134
+ #
135
+ # if max_bones < object.skeleton.bones.length
136
+ # puts "WARNING: OpenGL::Renderer: too many bones - #{object.skeleton.bones.length}, this GPU supports just #{max_bones}"
137
+ # end
138
+ # end
139
+
140
+ max_bones
141
+ end
142
+
143
+ def count_supported_morph_attributes(attributes)
144
+ if morph_targets
145
+ self.num_supported_morph_normals = count_supported_morph_attribute(attributes, 'morphTarget', @renderer.max_morph_normals)
146
+ end
147
+ if morph_normals
148
+ self.num_supported_morph_normals = count_supported_morph_attribute(attributes, 'morphNormal', @renderer.max_morph_normals)
149
+ end
150
+ end
151
+
152
+ def count_supported_morph_attribute(attributes, base, max)
153
+ max.times.reduce do |num, i|
154
+ attribute = attributes["#{base}#{i}"]
155
+ attribute && attribute >= 0 ? num + 1 : num
156
+ end
157
+ end
158
+
159
+ def get_uniforms_list
160
+ @shader[:uniforms].map { |(key, uniform)|
161
+ location = program.uniforms[key]
162
+ if location
163
+ [uniform, location]
164
+ end
165
+ }.compact
166
+ end
167
+
168
+ def program_parameters(lights, fog, object)
169
+ # heuristics to create shader paramaters according to lights in the scene
170
+ # (not to blow over max_lights budget)
171
+
172
+ max_light_count = allocate_lights(lights)
173
+ max_shadows = allocate_shadows(lights)
174
+ max_bones = allocate_bones(object)
175
+
176
+ {
177
+ supports_vertex_textures: @renderer.supports_vertex_textures?,
178
+
179
+ map: !!map,
180
+ env_map: !!env_map,
181
+ env_map_mode: env_map && env_map.mapping,
182
+ light_map: !!light_map,
183
+ bump_map: !!light_map,
184
+ normal_map: !!normal_map,
185
+ specular_map: !!specular_map,
186
+ alpha_map: !!alpha_map,
187
+
188
+ combine: combine,
189
+
190
+ vertex_colors: vertex_colors,
191
+
192
+ fog: fog,
193
+ use_fog: fog,
194
+ # fog_exp: fog.is_a?(FogExp2), # TODO: when FogExp2 exists
195
+
196
+ flat_shading: shading == FlatShading,
197
+
198
+ size_attenuation: size_attenuation,
199
+ logarithmic_depth_buffer: @renderer.logarithmic_depth_buffer,
200
+
201
+ skinning: skinning,
202
+ max_bones: max_bones,
203
+ use_vertex_texture: @renderer.supports_bone_textures?,
204
+
205
+ morph_targets: morph_targets,
206
+ morph_normals: morph_normals,
207
+ max_morph_targets: @renderer.max_morph_targets,
208
+ max_morph_normals: @renderer.max_morph_normals,
209
+
210
+ max_dir_lights: max_light_count[:directional],
211
+ max_point_lights: max_light_count[:point],
212
+ max_spot_lights: max_light_count[:spot],
213
+ max_hemi_lights: max_light_count[:hemi],
214
+
215
+ max_shadows: max_shadows,
216
+ shadow_map_enabled: @renderer.shadow_map_enabled? && object.receive_shadow && max_shadows > 0,
217
+ shadow_map_type: @renderer.shadow_map_type,
218
+ shadow_map_debug: @renderer.shadow_map_debug,
219
+ shadow_map_cascade: @renderer.shadow_map_cascade,
220
+
221
+ alpha_test: alpha_test,
222
+ metal: metal,
223
+ wrap_around: wrap_around,
224
+ double_sided: side == DoubleSide,
225
+ flip_sided: side == BackSide
226
+ }
227
+ end
228
+
229
+ def program_slug(parameters)
230
+ chunks = []
231
+
232
+ if shader_id
233
+ chunks << shader_id
234
+ else
235
+ chunks << fragment_shader
236
+ chunks << vertex_shader
237
+ end
238
+
239
+ if !defines.nil?
240
+ defines.each do |(name, define)|
241
+ chunks << name
242
+ chunks << define
243
+ end
244
+ end
245
+
246
+ parameters.each do |(name, parameter)|
247
+ chunks << name
248
+ chunks << parameter
249
+ end
250
+
251
+ chunks.join
252
+ end
253
+
254
+ def find_or_create_program(lights, fog, object)
255
+ parameters = program_parameters(lights, fog, object)
256
+ code = program_slug(parameters)
257
+
258
+ program = @renderer.programs.find do |program_info|
259
+ program_info.code == code
260
+ end
261
+
262
+ if program.nil?
263
+ program = OpenGL::Program.new(@renderer, code, self, parameters)
264
+ @renderer.programs.push(program)
265
+
266
+ @renderer.info[:memory][:programs] = @renderer.programs.length
267
+ else
268
+ program.used_times += 1
269
+ end
270
+
271
+ program
272
+ end
273
+ end
274
+ end
@@ -0,0 +1,21 @@
1
+ require 'mittsu/opengl/material_basics'
2
+
3
+ module Mittsu
4
+ class MeshBasicMaterial
5
+ include OpenGL::MaterialBasics
6
+
7
+ def refresh_uniforms(uniforms)
8
+ refresh_uniforms_basic(uniforms)
9
+ end
10
+
11
+ protected
12
+
13
+ def init_shader
14
+ @shader = OpenGL::Shader::Lib.create_shader(shader_id)
15
+ end
16
+
17
+ def shader_id
18
+ :basic
19
+ end
20
+ end
21
+ end