1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//! Port of `three.js/examples/jsm/tsl/display/ParallaxBarrierPassNode.js`.
//!
//! The two eyes of a [`StereoCamera`] interleaved by row, for a parallax
//! barrier display: a fragment whose `screenCoordinate.y` is odd
//! (`mod( y, 2 ) > 1` at the pixel centre `y + 0.5`) shows the left eye, an
//! even one the right.
//!
//! # Divergences
//!
//! * The eyes come from `stereo.update()` with the port's WebGPU-style
//! (`[0, 1]` depth) projection, so they clip near at `near` where three's,
//! copied from a WebGL-style matrix, clip at `2fn / (f + n)`; see
//! [`StereoPassNode`](super::StereoPassNode).
//!
//! # Not ported
//!
//! * `material.contextNode = context( builder.getSharedContext() )`: the
//! port's quad materials build in their own context, as every other
//! display node's do.
//! * `dispose()`: the targets and the material are dropped with the node.
use std::cell::{Ref, RefCell, RefMut};
use std::rc::Rc;
use crate::cameras::{PerspectiveCamera, StereoCamera};
use crate::materials::MeshBasicNodeMaterial;
use crate::math::CoordinateSystem;
use crate::nodes::tsl::{
block, if_else, mod_float, screen_coordinate, texture_sample, to_var, uv, vec4,
};
use crate::nodes::NodeRef;
use crate::objects::QuadMesh;
use crate::renderer::SceneRef;
use crate::textures::Texture;
use super::stereo_composite_pass::CompositeState;
/// `parallaxBarrierPass( scene, camera )`.
pub fn parallax_barrier_pass(
scene: SceneRef,
camera: Rc<RefCell<PerspectiveCamera>>,
) -> ParallaxBarrierPassNode {
ParallaxBarrierPassNode::new(scene, camera)
}
/// `ParallaxBarrierPassNode` — see the module docs.
pub struct ParallaxBarrierPassNode {
state: Rc<CompositeState>,
}
impl ParallaxBarrierPassNode {
/// `ParallaxBarrierPassNode.type`.
pub const TYPE: &'static str = "ParallaxBarrierPassNode";
/// `new ParallaxBarrierPassNode( scene, camera )`, with the material
/// `setup()` builds — an unnamed `NodeMaterial`, as three's is.
pub fn new(scene: SceneRef, camera: Rc<RefCell<PerspectiveCamera>>) -> Self {
let state = CompositeState::new(scene, camera, None, |map_left, map_right| {
let mut material = MeshBasicNodeMaterial::new();
material.fragment_node = Some(parallax_barrier_node(map_left, map_right));
material
});
Self { state }
}
/// `getTextureNode()` — the interleaved image, for the graph downstream.
pub fn node(&self) -> NodeRef {
self.state.node()
}
/// `this.stereo` — its `eyeSep` is the page's slider; its `aspect` is
/// `1`, as each eye fills the whole target.
pub fn stereo(&self) -> RefMut<'_, StereoCamera> {
self.state.stereo()
}
/// `updateStereoCamera( coordinateSystem )` — what `updateBefore()`
/// calls with the renderer's coordinate system.
pub fn update_stereo_camera(&self, coordinate_system: CoordinateSystem) {
self.state.update_stereo_camera(coordinate_system);
}
/// The quad material, for `examples/dump_wgsl.rs` and the dump gate.
#[doc(hidden)]
pub fn quad_material(&self) -> MeshBasicNodeMaterial {
let quad: Ref<'_, QuadMesh> = self.state.quad();
quad.material.clone()
}
}
/// `ParallaxBarrierPassNode.setup()`'s `parallaxBarrier` `Fn()`.
fn parallax_barrier_node(map_left: &Texture, map_right: &Texture) -> NodeRef {
let uv_node = uv();
// `vec4().toVar()` — `Vector4`'s default, `( 0, 0, 0, 1 )`.
let color = to_var(None, vec4(0.0, 0.0, 0.0, 1.0));
block(
vec![
color.clone(),
if_else(
mod_float(screen_coordinate().y(), 2.0).greater_than(1.0),
vec![color.assign(texture_sample(map_left, uv_node.clone()))],
vec![color.assign(texture_sample(map_right, uv_node))],
),
],
color,
)
}