Class: Three::Backends::ThreeJS::RubyWasmAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/three/backends/threejs/ruby_wasm_adapter.rb

Constant Summary collapse

TEXTURE_SLOTS =
%w[
  map
  normalMap
  roughnessMap
  metalnessMap
  aoMap
  emissiveMap
  alphaMap
  bumpMap
  displacementMap
  envMap
  gradientMap
  lightMap
  matcap
  specularMap
  anisotropyMap
  clearcoatMap
  clearcoatNormalMap
  clearcoatRoughnessMap
  transmissionMap
  thicknessMap
  iridescenceMap
  iridescenceThicknessMap
  sheenColorMap
  sheenRoughnessMap
  specularColorMap
  specularIntensityMap
].freeze
ADDON_CONSTRUCTORS =
{
  orbit_controls: {
    owner: "Three::Controls::OrbitControls",
    global: :THREE_ORBIT_CONTROLS,
    import: 'import { OrbitControls } from "three/addons/controls/OrbitControls.js";',
    assignment: "globalThis.THREE_ORBIT_CONTROLS = OrbitControls;"
  },
  effect_composer: {
    owner: "Three::Postprocessing::EffectComposer",
    global: :THREE_EFFECT_COMPOSER,
    import: 'import { EffectComposer } from "three/addons/postprocessing/EffectComposer.js";',
    assignment: "globalThis.THREE_EFFECT_COMPOSER = EffectComposer;"
  },
  render_pass: {
    owner: "Three::Postprocessing::RenderPass",
    global: :THREE_RENDER_PASS,
    import: 'import { RenderPass } from "three/addons/postprocessing/RenderPass.js";',
    assignment: "globalThis.THREE_RENDER_PASS = RenderPass;"
  },
  unreal_bloom_pass: {
    owner: "Three::Postprocessing::UnrealBloomPass",
    global: :THREE_UNREAL_BLOOM_PASS,
    import: 'import { UnrealBloomPass } from "three/addons/postprocessing/UnrealBloomPass.js";',
    assignment: "globalThis.THREE_UNREAL_BLOOM_PASS = UnrealBloomPass;"
  },
  dot_screen_pass: {
    owner: "Three::Postprocessing::DotScreenPass",
    global: :THREE_DOT_SCREEN_PASS,
    import: 'import { DotScreenPass } from "three/addons/postprocessing/DotScreenPass.js";',
    assignment: "globalThis.THREE_DOT_SCREEN_PASS = DotScreenPass;"
  },
  output_pass: {
    owner: "Three::Postprocessing::OutputPass",
    global: :THREE_OUTPUT_PASS,
    import: 'import { OutputPass } from "three/addons/postprocessing/OutputPass.js";',
    assignment: "globalThis.THREE_OUTPUT_PASS = OutputPass;"
  },
  gltf_loader: {
    owner: "Three::Loaders::GLTFLoader",
    global: :THREE_GLTF_LOADER,
    import: 'import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";',
    assignment: "globalThis.THREE_GLTF_LOADER = GLTFLoader;"
  },
  draco_loader: {
    owner: "Three::Loaders::GLTFLoader with DRACO",
    global: :THREE_DRACO_LOADER,
    import: 'import { DRACOLoader } from "three/addons/loaders/DRACOLoader.js";',
    assignment: "globalThis.THREE_DRACO_LOADER = DRACOLoader;"
  },
  rgbe_loader: {
    owner: "Three::Loaders::RGBELoader",
    global: :THREE_RGBE_LOADER,
    import: 'import { HDRLoader } from "three/addons/loaders/HDRLoader.js";',
    assignment: "globalThis.THREE_RGBE_LOADER = HDRLoader;"
  },
  font_loader: {
    owner: "Three::Loaders::FontLoader",
    global: :THREE_FONT_LOADER,
    import: 'import { FontLoader } from "three/addons/loaders/FontLoader.js";',
    assignment: "globalThis.THREE_FONT_LOADER = FontLoader;"
  },
  text_geometry: {
    owner: "Three::TextGeometry",
    global: :THREE_TEXT_GEOMETRY,
    import: 'import { TextGeometry } from "three/addons/geometries/TextGeometry.js";',
    assignment: "globalThis.THREE_TEXT_GEOMETRY = TextGeometry;"
  }
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(three: nil) ⇒ RubyWasmAdapter

Returns a new instance of RubyWasmAdapter.



105
106
107
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 105

def initialize(three: nil)
  @three = three || default_three
end

Instance Method Details

#add_child(parent, child) ⇒ Object



677
678
679
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 677

def add_child(parent, child)
  parent.call(:add, child)
end

#add_event_listener(handle, type, callback) ⇒ Object



158
159
160
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 158

def add_event_listener(handle, type, callback)
  handle.call(:addEventListener, type.to_s, callback)
end

#add_geometry_group(geometry, start, count, material_index) ⇒ Object



520
521
522
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 520

def add_geometry_group(geometry, start, count, material_index)
  geometry.call(:addGroup, start, count, material_index)
end

#animation_clip_duration(clip) ⇒ Object



284
285
286
287
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 284

def animation_clip_duration(clip)
  value = clip[:duration]
  js_present?(value) ? value.to_f : 0.0
end

#animation_clip_name(clip) ⇒ Object



279
280
281
282
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 279

def animation_clip_name(clip)
  value = clip[:name]
  js_present?(value) ? value.to_s : ""
end

#animation_mixer_clip_action(mixer, clip, root = nil) ⇒ Object



293
294
295
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 293

def animation_mixer_clip_action(mixer, clip, root = nil)
  root ? mixer.call(:clipAction, clip, root) : mixer.call(:clipAction, clip)
end

#center_geometry(geometry) ⇒ Object



528
529
530
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 528

def center_geometry(geometry)
  geometry.call(:center)
end

#clear_children(parent) ⇒ Object



681
682
683
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 681

def clear_children(parent)
  parent.call(:clear)
end

#clear_geometry_groups(geometry) ⇒ Object



516
517
518
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 516

def clear_geometry_groups(geometry)
  geometry.call(:clearGroups)
end

#delete_geometry_attribute(geometry, name) ⇒ Object



512
513
514
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 512

def delete_geometry_attribute(geometry, name)
  geometry.call(:deleteAttribute, name.to_s)
end

#dispose(handle) ⇒ Object



712
713
714
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 712

def dispose(handle)
  handle.call(:dispose) if handle.respond_to?(:call)
end

#dispose_controls(control) ⇒ Object



364
365
366
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 364

def dispose_controls(control)
  control.call(:dispose)
end

#dispose_effect_composer(composer) ⇒ Object



184
185
186
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 184

def dispose_effect_composer(composer)
  composer.call(:dispose)
end

#dispose_object3d_subtree(object, remove: true, dispose_geometries: true, dispose_materials: true, dispose_textures: false, dispose_skeletons: true) ⇒ Object



762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 762

def dispose_object3d_subtree(
  object,
  remove: true,
  dispose_geometries: true,
  dispose_materials: true,
  dispose_textures: false,
  dispose_skeletons: true
)
  resources = {
    geometries: JS.global[:Set].new,
    materials: JS.global[:Set].new,
    textures: JS.global[:Set].new,
    skeletons: JS.global[:Set].new
  }

  traverse_object3d(object, proc do |node|
    collect_object3d_resources(
      node,
      resources,
      dispose_geometries: dispose_geometries,
      dispose_materials: dispose_materials,
      dispose_textures: dispose_textures,
      dispose_skeletons: dispose_skeletons
    )
  end)

  remove_from_js_parent(object) if remove

  dispose_js_set(resources[:geometries])
  dispose_js_set(resources[:materials])
  dispose_js_set(resources[:textures])
  dispose_js_set(resources[:skeletons])
  object
end

#effect_composer_add_pass(composer, pass) ⇒ Object



166
167
168
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 166

def effect_composer_add_pass(composer, pass)
  composer.call(:addPass, pass)
end

#effect_composer_render(composer) ⇒ Object



174
175
176
177
178
179
180
181
182
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 174

def effect_composer_render(composer)
  render_helper = JS.global[:__threeRbRenderComposer]
  if render_helper.typeof == "function"
    JS.global[:__threeRbCurrentComposer] = composer
    JS.global.call(:__threeRbRenderComposer)
  else
    composer.call(:render)
  end
end

#effect_composer_set_size(composer, width, height) ⇒ Object



170
171
172
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 170

def effect_composer_set_size(composer, width, height)
  composer.call(:setSize, width, height)
end

#fade_in_animation_action(action, duration) ⇒ Object



325
326
327
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 325

def fade_in_animation_action(action, duration)
  action.call(:fadeIn, duration)
end

#fade_out_animation_action(action, duration) ⇒ Object



329
330
331
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 329

def fade_out_animation_action(action, duration)
  action.call(:fadeOut, duration)
end

#gltf_animations(gltf) ⇒ Object



274
275
276
277
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 274

def gltf_animations(gltf)
  animations = gltf[:animations]
  js_present?(animations) ? animations.to_a : []
end

#intersect_objects(raycaster, objects, recursive: false) ⇒ Object



233
234
235
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 233

def intersect_objects(raycaster, objects, recursive: false)
  raycaster.call(:intersectObjects, js_array(objects), recursive).to_a
end

#intersection_distance(intersection) ⇒ Object



723
724
725
726
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 723

def intersection_distance(intersection)
  value = intersection[:distance]
  js_present?(value) ? value.to_f : nil
end

#intersection_face_index(intersection) ⇒ Object



742
743
744
745
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 742

def intersection_face_index(intersection)
  value = intersection[:faceIndex]
  js_present?(value) ? value.to_i : nil
end

#intersection_index(intersection) ⇒ Object



747
748
749
750
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 747

def intersection_index(intersection)
  value = intersection[:index]
  js_present?(value) ? value.to_i : nil
end

#intersection_instance_id(intersection) ⇒ Object



752
753
754
755
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 752

def intersection_instance_id(intersection)
  value = intersection[:instanceId]
  js_present?(value) ? value.to_i : nil
end

#intersection_object(intersection) ⇒ Object



733
734
735
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 733

def intersection_object(intersection)
  intersection[:object]
end

#intersection_point(intersection) ⇒ Object



728
729
730
731
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 728

def intersection_point(intersection)
  point = intersection[:point]
  js_present?(point) ? js_vector_to_a(point, 3) : nil
end

#intersection_uv(intersection) ⇒ Object



737
738
739
740
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 737

def intersection_uv(intersection)
  uv = intersection[:uv]
  js_present?(uv) ? js_vector_to_a(uv, 2) : nil
end

#load_cube_texture(sources, parameters = {}) ⇒ Object



243
244
245
246
247
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 243

def load_cube_texture(sources, parameters = {})
  texture = @three[:CubeTextureLoader].new.call(:load, js_array(sources))
  update_texture(texture, parameters)
  texture
end

#load_font(source) ⇒ Object



264
265
266
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 264

def load_font(source)
  font_loader_constructor.new.call(:loadAsync, source)
end

#load_gltf(source, draco_decoder_path: nil, draco_decoder_config: nil) ⇒ Object



268
269
270
271
272
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 268

def load_gltf(source, draco_decoder_path: nil, draco_decoder_config: nil)
  loader = gltf_loader_constructor.new
  configure_draco_loader(loader, draco_decoder_path, draco_decoder_config) if draco_decoder_path
  loader.call(:loadAsync, source)
end

#load_rgbe_texture(source, parameters = {}) ⇒ Object



249
250
251
252
253
254
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 249

def load_rgbe_texture(source, parameters = {})
  texture = rgbe_loader_constructor.new.call(:loadAsync, source)
  texture = texture.await if texture.respond_to?(:await)
  update_texture(texture, parameters)
  texture
end

#load_texture(source, parameters = {}) ⇒ Object



237
238
239
240
241
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 237

def load_texture(source, parameters = {})
  texture = @three[:TextureLoader].new.call(:load, source)
  update_texture(texture, parameters)
  texture
end

#new_ambient_light(color, intensity) ⇒ Object



396
397
398
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 396

def new_ambient_light(color, intensity)
  @three[:AmbientLight].new(color, intensity)
end

#new_animation_mixer(root) ⇒ Object



289
290
291
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 289

def new_animation_mixer(root)
  @three[:AnimationMixer].new(root)
end

#new_box_geometry(width, height, depth, width_segments, height_segments, depth_segments) ⇒ Object



479
480
481
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 479

def new_box_geometry(width, height, depth, width_segments, height_segments, depth_segments)
  @three[:BoxGeometry].new(width, height, depth, width_segments, height_segments, depth_segments)
end

#new_buffer_attribute(component_type, array, item_size, normalized) ⇒ Object



499
500
501
502
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 499

def new_buffer_attribute(component_type, array, item_size, normalized)
  typed_array = typed_array(component_type, array)
  @three[:BufferAttribute].new(typed_array, item_size, normalized)
end

#new_buffer_geometryObject



495
496
497
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 495

def new_buffer_geometry
  @three[:BufferGeometry].new
end

#new_directional_light(color, intensity) ⇒ Object



400
401
402
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 400

def new_directional_light(color, intensity)
  @three[:DirectionalLight].new(color, intensity)
end

#new_dot_screen_pass(center, angle, scale) ⇒ Object



196
197
198
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 196

def new_dot_screen_pass(center, angle, scale)
  dot_screen_pass_constructor.new(@three[:Vector2].new(*center), angle, scale)
end

#new_effect_composer(renderer) ⇒ Object



162
163
164
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 162

def new_effect_composer(renderer)
  effect_composer_constructor.new(renderer)
end

#new_fog(color, near, far) ⇒ Object



256
257
258
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 256

def new_fog(color, near, far)
  @three[:Fog].new(color, near, far)
end

#new_fog_exp2(color, density) ⇒ Object



260
261
262
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 260

def new_fog_exp2(color, density)
  @three[:FogExp2].new(color, density)
end

#new_groupObject



380
381
382
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 380

def new_group
  @three[:Group].new
end

#new_hemisphere_light(sky_color, ground_color, intensity) ⇒ Object



408
409
410
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 408

def new_hemisphere_light(sky_color, ground_color, intensity)
  @three[:HemisphereLight].new(sky_color, ground_color, intensity)
end

#new_instanced_mesh(geometry, material, count) ⇒ Object



428
429
430
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 428

def new_instanced_mesh(geometry, material, count)
  @three[:InstancedMesh].new(geometry, material, count)
end

#new_line(geometry, material) ⇒ Object



416
417
418
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 416

def new_line(geometry, material)
  @three[:Line].new(geometry, material)
end

#new_line_basic_material(parameters) ⇒ Object



536
537
538
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 536

def new_line_basic_material(parameters)
  @three[:LineBasicMaterial].new(stringify_keys(parameters))
end

#new_mesh(geometry, material) ⇒ Object



412
413
414
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 412

def new_mesh(geometry, material)
  @three[:Mesh].new(geometry, material)
end

#new_mesh_basic_material(parameters) ⇒ Object



532
533
534
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 532

def new_mesh_basic_material(parameters)
  @three[:MeshBasicMaterial].new(stringify_keys(parameters))
end

#new_mesh_lambert_material(parameters) ⇒ Object



540
541
542
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 540

def new_mesh_lambert_material(parameters)
  @three[:MeshLambertMaterial].new(stringify_keys(parameters))
end

#new_mesh_matcap_material(parameters) ⇒ Object



544
545
546
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 544

def new_mesh_matcap_material(parameters)
  @three[:MeshMatcapMaterial].new(stringify_keys(parameters))
end

#new_mesh_normal_material(parameters) ⇒ Object



548
549
550
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 548

def new_mesh_normal_material(parameters)
  @three[:MeshNormalMaterial].new(stringify_keys(parameters))
end

#new_mesh_phong_material(parameters) ⇒ Object



552
553
554
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 552

def new_mesh_phong_material(parameters)
  @three[:MeshPhongMaterial].new(stringify_keys(parameters))
end

#new_mesh_physical_material(parameters) ⇒ Object



556
557
558
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 556

def new_mesh_physical_material(parameters)
  @three[:MeshPhysicalMaterial].new(stringify_keys(parameters))
end

#new_mesh_standard_material(parameters) ⇒ Object



560
561
562
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 560

def new_mesh_standard_material(parameters)
  @three[:MeshStandardMaterial].new(stringify_keys(parameters))
end

#new_mesh_toon_material(parameters) ⇒ Object



564
565
566
567
568
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 564

def new_mesh_toon_material(parameters)
  material = @three[:MeshToonMaterial].new(stringify_keys(parameters))
  update_material(material, parameters)
  material
end

#new_object3dObject



384
385
386
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 384

def new_object3d
  @three[:Object3D].new
end

#new_orbit_controls(camera, dom_element) ⇒ Object



220
221
222
223
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 220

def new_orbit_controls(camera, dom_element)
  constructor = orbit_controls_constructor
  dom_element ? constructor.new(camera, dom_element) : constructor.new(camera)
end

#new_orthographic_camera(left, right, top, bottom, near, far) ⇒ Object



392
393
394
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 392

def new_orthographic_camera(left, right, top, bottom, near, far)
  @three[:OrthographicCamera].new(left, right, top, bottom, near, far)
end

#new_output_passObject



200
201
202
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 200

def new_output_pass
  output_pass_constructor.new
end

#new_perspective_camera(fov, aspect, near, far) ⇒ Object



388
389
390
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 388

def new_perspective_camera(fov, aspect, near, far)
  @three[:PerspectiveCamera].new(fov, aspect, near, far)
end

#new_plane_geometry(width, height, width_segments, height_segments) ⇒ Object



483
484
485
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 483

def new_plane_geometry(width, height, width_segments, height_segments)
  @three[:PlaneGeometry].new(width, height, width_segments, height_segments)
end

#new_point_light(color, intensity, distance, decay) ⇒ Object



404
405
406
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 404

def new_point_light(color, intensity, distance, decay)
  @three[:PointLight].new(color, intensity, distance, decay)
end

#new_points(geometry, material) ⇒ Object



420
421
422
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 420

def new_points(geometry, material)
  @three[:Points].new(geometry, material)
end

#new_points_material(parameters) ⇒ Object



570
571
572
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 570

def new_points_material(parameters)
  @three[:PointsMaterial].new(stringify_keys(parameters))
end

#new_raycasterObject



225
226
227
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 225

def new_raycaster
  @three[:Raycaster].new
end

#new_render_pass(scene, camera) ⇒ Object



188
189
190
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 188

def new_render_pass(scene, camera)
  render_pass_constructor.new(scene, camera)
end

#new_sceneObject



376
377
378
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 376

def new_scene
  @three[:Scene].new
end

#new_shadow_material(parameters) ⇒ Object



574
575
576
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 574

def new_shadow_material(parameters)
  @three[:ShadowMaterial].new(stringify_keys(parameters))
end

#new_sphere_geometry(radius, width_segments, height_segments, phi_start, phi_length, theta_start, theta_length) ⇒ Object



487
488
489
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 487

def new_sphere_geometry(radius, width_segments, height_segments, phi_start, phi_length, theta_start, theta_length)
  @three[:SphereGeometry].new(radius, width_segments, height_segments, phi_start, phi_length, theta_start, theta_length)
end

#new_sprite(material) ⇒ Object



424
425
426
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 424

def new_sprite(material)
  @three[:Sprite].new(material)
end

#new_sprite_material(parameters) ⇒ Object



578
579
580
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 578

def new_sprite_material(parameters)
  @three[:SpriteMaterial].new(stringify_keys(parameters))
end

#new_text_geometry(text, parameters) ⇒ Object



491
492
493
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 491

def new_text_geometry(text, parameters)
  text_geometry_constructor.new(text, stringify_keys(parameters))
end

#new_unreal_bloom_pass(resolution, strength, radius, threshold) ⇒ Object



192
193
194
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 192

def new_unreal_bloom_pass(resolution, strength, radius, threshold)
  unreal_bloom_pass_constructor.new(@three[:Vector2].new(*resolution), strength, radius, threshold)
end

#new_webgl_renderer(options = {}) ⇒ Object



109
110
111
112
113
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 109

def new_webgl_renderer(options = {})
  parameters = stringify_keys(options)
  parameters["canvas"] = resolve_canvas(options[:canvas] || options["canvas"]) if options[:canvas] || options["canvas"]
  @three[:WebGLRenderer].new(parameters)
end

#object_handle_key(object) ⇒ Object



716
717
718
719
720
721
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 716

def object_handle_key(object)
  return nil unless js_present?(object)

  uuid = object[:uuid]
  js_present?(uuid) ? uuid.to_s : nil
end

#object_transform(object) ⇒ Object



368
369
370
371
372
373
374
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 368

def object_transform(object)
  [
    js_vector_to_a(object[:position], 3),
    js_vector_to_a(object[:quaternion], 4),
    js_vector_to_a(object[:scale], 3)
  ]
end

#play_animation_action(action) ⇒ Object



313
314
315
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 313

def play_animation_action(action)
  action.call(:play)
end

#render(renderer, scene, camera) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 146

def render(renderer, scene, camera)
  render_helper = JS.global[:__threeRbRender]
  if render_helper.typeof == "function"
    JS.global[:__threeRbCurrentRenderer] = renderer
    JS.global[:__threeRbCurrentScene] = scene
    JS.global[:__threeRbCurrentCamera] = camera
    JS.global.call(:__threeRbRender)
  else
    renderer.call(:render, scene, camera)
  end
end

#renderer_dom_element(renderer) ⇒ Object



115
116
117
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 115

def renderer_dom_element(renderer)
  renderer[:domElement]
end

#reset_animation_action(action) ⇒ Object



321
322
323
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 321

def reset_animation_action(action)
  action.call(:reset)
end

#set_animation_action_property(action, name, value) ⇒ Object



309
310
311
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 309

def set_animation_action_property(action, name, value)
  action[name] = value
end

#set_animation_loop(renderer, callback) ⇒ Object



142
143
144
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 142

def set_animation_loop(renderer, callback)
  renderer.call(:setAnimationLoop, callback)
end

#set_clear_color(renderer, color, alpha = 1) ⇒ Object



123
124
125
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 123

def set_clear_color(renderer, color, alpha = 1)
  renderer.call(:setClearColor, color, alpha)
end

#set_control_property(control, name, value) ⇒ Object



352
353
354
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 352

def set_control_property(control, name, value)
  control[name] = value
end

#set_geometry_attribute(geometry, name, attribute) ⇒ Object



508
509
510
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 508

def set_geometry_attribute(geometry, name, attribute)
  geometry.call(:setAttribute, name.to_s, attribute)
end

#set_geometry_draw_range(geometry, start, count) ⇒ Object



524
525
526
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 524

def set_geometry_draw_range(geometry, start, count)
  geometry.call(:setDrawRange, start, count)
end

#set_geometry_index(geometry, attribute) ⇒ Object



504
505
506
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 504

def set_geometry_index(geometry, attribute)
  geometry.call(:setIndex, attribute)
end

#set_instanced_mesh_color_at(mesh, index, color) ⇒ Object



470
471
472
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 470

def set_instanced_mesh_color_at(mesh, index, color)
  mesh.call(:setColorAt, index, @three[:Color].new(*color))
end

#set_instanced_mesh_count(mesh, count) ⇒ Object



456
457
458
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 456

def set_instanced_mesh_count(mesh, count)
  mesh[:count] = count
end

#set_instanced_mesh_instance_color_needs_update(mesh, value) ⇒ Object



474
475
476
477
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 474

def set_instanced_mesh_instance_color_needs_update(mesh, value)
  instance_color = mesh[:instanceColor]
  instance_color[:needsUpdate] = value if js_present?(instance_color)
end

#set_instanced_mesh_instance_matrix_needs_update(mesh, value) ⇒ Object



466
467
468
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 466

def set_instanced_mesh_instance_matrix_needs_update(mesh, value)
  mesh[:instanceMatrix][:needsUpdate] = value
end

#set_instanced_mesh_matrix_at(mesh, index, elements) ⇒ Object



460
461
462
463
464
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 460

def set_instanced_mesh_matrix_at(mesh, index, elements)
  matrix = @three[:Matrix4].new
  matrix.call(:fromArray, js_array(elements))
  mesh.call(:setMatrixAt, index, matrix)
end

#set_mesh_geometry(mesh, geometry) ⇒ Object



440
441
442
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 440

def set_mesh_geometry(mesh, geometry)
  set_object_geometry(mesh, geometry)
end

#set_mesh_material(mesh, material) ⇒ Object



444
445
446
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 444

def set_mesh_material(mesh, material)
  set_object_material(mesh, material)
end

#set_object_geometry(object, geometry) ⇒ Object



432
433
434
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 432

def set_object_geometry(object, geometry)
  object[:geometry] = geometry
end

#set_object_layers(object, mask) ⇒ Object



452
453
454
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 452

def set_object_layers(object, mask)
  object[:layers][:mask] = mask if js_present?(object[:layers])
end

#set_object_material(object, material) ⇒ Object



436
437
438
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 436

def set_object_material(object, material)
  object[:material] = material
end

#set_object_matrix(object, elements) ⇒ Object



605
606
607
608
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 605

def set_object_matrix(object, elements)
  object[:matrix].call(:fromArray, js_array(elements))
  object[:matrixWorldNeedsUpdate] = true
end

#set_object_matrix_auto_update(object, value) ⇒ Object



601
602
603
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 601

def set_object_matrix_auto_update(object, value)
  object[:matrixAutoUpdate] = value
end

#set_object_name(object, name) ⇒ Object



582
583
584
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 582

def set_object_name(object, name)
  object[:name] = name
end

#set_object_shadow(object, cast_shadow, receive_shadow) ⇒ Object



590
591
592
593
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 590

def set_object_shadow(object, cast_shadow, receive_shadow)
  object[:castShadow] = cast_shadow
  object[:receiveShadow] = receive_shadow
end

#set_object_transform(object, position, quaternion, scale) ⇒ Object



595
596
597
598
599
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 595

def set_object_transform(object, position, quaternion, scale)
  object[:position].call(:set, *position)
  object[:quaternion].call(:set, *quaternion)
  object[:scale].call(:set, *scale)
end

#set_object_visible(object, visible) ⇒ Object



586
587
588
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 586

def set_object_visible(object, visible)
  object[:visible] = visible
end

#set_orbit_controls_target(control, target) ⇒ Object



356
357
358
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 356

def set_orbit_controls_target(control, target)
  control[:target].call(:set, *target)
end

#set_postprocessing_pass_property(pass, name, value) ⇒ Object



204
205
206
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 204

def set_postprocessing_pass_property(pass, name, value)
  pass[name] = value
end

#set_postprocessing_pass_uniform(pass, name, value) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 208

def set_postprocessing_pass_uniform(pass, name, value)
  uniform = pass[:uniforms][name]
  return unless js_present?(uniform)

  target = uniform[:value]
  if value.respond_to?(:to_a) && js_present?(target) && target.respond_to?(:call)
    target.call(:set, *value.to_a)
  else
    uniform[:value] = value
  end
end

#set_raycaster_from_camera(raycaster, coords, camera) ⇒ Object



229
230
231
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 229

def set_raycaster_from_camera(raycaster, coords, camera)
  raycaster.call(:setFromCamera, @three[:Vector2].new(*coords), camera)
end

#set_renderer_shadow_map(renderer, enabled: nil, type: nil, auto_update: nil) ⇒ Object



135
136
137
138
139
140
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 135

def set_renderer_shadow_map(renderer, enabled: nil, type: nil, auto_update: nil)
  shadow_map = renderer[:shadowMap]
  shadow_map[:enabled] = enabled unless enabled.nil?
  shadow_map[:type] = type unless type.nil?
  shadow_map[:autoUpdate] = auto_update unless auto_update.nil?
end

#set_renderer_size(renderer, width, height) ⇒ Object



119
120
121
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 119

def set_renderer_size(renderer, width, height)
  renderer.call(:setSize, width, height)
end

#set_renderer_tone_mapping(renderer, value) ⇒ Object



127
128
129
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 127

def set_renderer_tone_mapping(renderer, value)
  renderer[:toneMapping] = value
end

#set_renderer_tone_mapping_exposure(renderer, value) ⇒ Object



131
132
133
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 131

def set_renderer_tone_mapping_exposure(renderer, value)
  renderer[:toneMappingExposure] = value
end

#set_scene_background(scene, background) ⇒ Object



685
686
687
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 685

def set_scene_background(scene, background)
  scene[:background] = background
end

#set_scene_environment(scene, environment) ⇒ Object



689
690
691
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 689

def set_scene_environment(scene, environment)
  scene[:environment] = environment
end

#set_scene_fog(scene, fog) ⇒ Object



693
694
695
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 693

def set_scene_fog(scene, fog)
  scene[:fog] = fog
end

#set_scene_override_material(scene, material) ⇒ Object



697
698
699
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 697

def set_scene_override_material(scene, material)
  scene[:overrideMaterial] = material
end

#set_sprite_center(sprite, center) ⇒ Object



448
449
450
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 448

def set_sprite_center(sprite, center)
  sprite[:center].call(:set, *center)
end

#stop_all_animation_actions(mixer) ⇒ Object



301
302
303
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 301

def stop_all_animation_actions(mixer)
  mixer.call(:stopAllAction)
end

#stop_animation_action(action) ⇒ Object



317
318
319
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 317

def stop_animation_action(action)
  action.call(:stop)
end

#traverse_object3d(object, callback) ⇒ Object



757
758
759
760
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 757

def traverse_object3d(object, callback)
  object.call(:traverse, callback)
  object
end

#uncache_animation_root(mixer, root) ⇒ Object



305
306
307
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 305

def uncache_animation_root(mixer, root)
  mixer.call(:uncacheRoot, root)
end

#update_animation_mixer(mixer, delta) ⇒ Object



297
298
299
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 297

def update_animation_mixer(mixer, delta)
  mixer.call(:update, delta)
end

#update_controls(control) ⇒ Object



360
361
362
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 360

def update_controls(control)
  control.call(:update)
end

#update_fog(fog, color, near, far) ⇒ Object



701
702
703
704
705
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 701

def update_fog(fog, color, near, far)
  fog[:color].call(:setHex, color)
  fog[:near] = near
  fog[:far] = far
end

#update_fog_exp2(fog, color, density) ⇒ Object



707
708
709
710
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 707

def update_fog_exp2(fog, color, density)
  fog[:color].call(:setHex, color)
  fog[:density] = density
end

#update_hemisphere_light(light, sky_color, ground_color, intensity) ⇒ Object



641
642
643
644
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 641

def update_hemisphere_light(light, sky_color, ground_color, intensity)
  update_light(light, sky_color, intensity)
  light[:groundColor].call(:setHex, ground_color)
end

#update_light(light, color, intensity) ⇒ Object



630
631
632
633
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 630

def update_light(light, color, intensity)
  light[:color].call(:setHex, color)
  light[:intensity] = intensity
end

#update_light_shadow(light, parameters) ⇒ Object



646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 646

def update_light_shadow(light, parameters)
  shadow = light[:shadow]
  return unless js_present?(shadow)

  if parameters[:map_size]
    shadow[:mapSize].call(:set, *parameters[:map_size])
  end
  shadow[:bias] = parameters[:bias] unless parameters[:bias].nil?
  shadow[:normalBias] = parameters[:normal_bias] unless parameters[:normal_bias].nil?
  shadow[:radius] = parameters[:radius] unless parameters[:radius].nil?

  camera = shadow[:camera]
  if js_present?(camera) && parameters[:camera]
    parameters[:camera].each do |key, value|
      camera[key] = value
    end
    camera.call(:updateProjectionMatrix)
  end
end

#update_material(material, parameters) ⇒ Object



666
667
668
669
670
671
672
673
674
675
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 666

def update_material(material, parameters)
  parameters.each do |key, value|
    if MATERIAL_COLOR_PARAMETERS.include?(key)
      material[key].call(:setHex, value)
    else
      material[key] = value
    end
  end
  material[:needsUpdate] = true
end

#update_orthographic_camera(camera, left, right, top, bottom, near, far, zoom) ⇒ Object



619
620
621
622
623
624
625
626
627
628
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 619

def update_orthographic_camera(camera, left, right, top, bottom, near, far, zoom)
  camera[:left] = left
  camera[:right] = right
  camera[:top] = top
  camera[:bottom] = bottom
  camera[:near] = near
  camera[:far] = far
  camera[:zoom] = zoom
  camera.call(:updateProjectionMatrix)
end

#update_perspective_camera(camera, fov, aspect, near, far, zoom) ⇒ Object



610
611
612
613
614
615
616
617
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 610

def update_perspective_camera(camera, fov, aspect, near, far, zoom)
  camera[:fov] = fov
  camera[:aspect] = aspect
  camera[:near] = near
  camera[:far] = far
  camera[:zoom] = zoom
  camera.call(:updateProjectionMatrix)
end

#update_point_light(light, color, intensity, distance, decay) ⇒ Object



635
636
637
638
639
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 635

def update_point_light(light, color, intensity, distance, decay)
  update_light(light, color, intensity)
  light[:distance] = distance
  light[:decay] = decay
end

#update_texture(texture, parameters) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/three/backends/threejs/ruby_wasm_adapter.rb', line 333

def update_texture(texture, parameters)
  texture[:mapping] = parameters[:mapping] unless parameters[:mapping].nil?
  texture[:colorSpace] = parameters[:color_space] unless parameters[:color_space].nil?
  texture[:flipY] = parameters[:flip_y] unless parameters[:flip_y].nil?
  texture[:wrapS] = parameters[:wrap_s] unless parameters[:wrap_s].nil?
  texture[:wrapT] = parameters[:wrap_t] unless parameters[:wrap_t].nil?
  texture[:magFilter] = parameters[:mag_filter] unless parameters[:mag_filter].nil?
  texture[:minFilter] = parameters[:min_filter] unless parameters[:min_filter].nil?
  texture[:offset].call(:set, *parameters[:offset]) if parameters[:offset]
  texture[:repeat].call(:set, *parameters[:repeat]) if parameters[:repeat]
  texture[:center].call(:set, *parameters[:center]) if parameters[:center]
  texture[:rotation] = parameters[:rotation] unless parameters[:rotation].nil?
  texture[:matrixAutoUpdate] = parameters[:matrix_auto_update] unless parameters[:matrix_auto_update].nil?
  texture[:matrix].call(:fromArray, js_array(parameters[:matrix])) if parameters[:matrix]
  texture.call(:updateMatrix) if parameters[:matrix_auto_update]
  texture[:needsUpdate] = true
  texture
end