vanilladocs
vanillapunch.com
API / graphics3d/toon/toonview3d

graphics3d/toon/toonview3d

툰 3D 를 노드 사각형 안에 그리는 컴포넌트. (vanilla.js graphics3d/toon/toonview3d.js 의 ToonView3D)

  • draw 에서: 노드의 내용 영역(0, 0, 내용 크기)을 지금 변환으로 드로잉 버퍼 픽셀의 사각형으로 옮기고(돌았으면 감싸는 사각형), Graphic::run_external_pass 로 앞의 2D 묶음을 내보내고 상태 기억을 잊고 깊이를 비운 뒤, 렌더러의 화면 사각형(set_screen_rect)을 걸고 그리기 함수를 부른다. 끝나면 화면 사각형을 풀고 restore_render_state 로 2D 상태를 되돌린다. (그리기 함수가 패닉해도 둘 다 되돌린다)
  • 렌더러는 처음 그릴 때 짓는다. 셰이더를 짓지 못하는 기기면 다시 짓지 않고 대체 그리기 함수(set_fallback_draw_callback)로 2D 를 그린다. 여러 노드가 렌더러 하나를 함께 쓰려면 set_renderer 로 넣는다.
  • 카메라(get_camera_mut)의 가로 세로 비는 그릴 때마다 사각형의 비로 맞춘다. 3D 점 위의 2D 표시는 같은 카메라의 project_to_view 로 셈한다.
  • 깊이 비우기: 부모 Mask 의 자르기 안이면 Adreno 660 함정(Graphic::clear_depth_buffer 머리말)이 있다. 자르기를 거는 쪽이 begin_clip_rect 앞에서 clear_depth_buffer 를 부르고 이 컴포넌트는 set_depth_cleared(false) 로 둔다.
  • 그리기 함수: (렌더러, 카메라, 화면 사각형, GL, 출력). 화면 사각형은 지금 프레임 버퍼에서 그릴 사각형(픽셀, 왼쪽 위 원점)이다. 출력(ToonViewOutput)은 지금 묶인 프레임 버퍼(None 이면 기본 프레임 버퍼)와 그 안의 GL 뷰포트([x, y, 너비, 높이], 정수, 왼쪽 아래 원점)다. 보통 ToonDrawQueue 에 담아 queue.draw(gl, renderer, camera) 하거나 begin_frame_with_camera 와 end_frame 사이에 그린다. ToonPostProcess 를 쓰면 renderer.clear_screen_rect() 를 부르고 실수 렌더 타겟에 그린 뒤 end 에 출력의 프레임 버퍼와 뷰포트를 준다. (그래야 렌더 스케일, 투명도 층과 상관없이 맞는 자리에 낸다)
  • 렌더 스케일(set_render_scale, 0.05 ~ 4, 기본 1): 1 이 아니면 3D 만 내용 영역의 화면 크기 x 배율의 판(깊이 24, 기본 프레임 버퍼가 멀티샘플이면 표본 4)에 투명하게 비운 뒤 그리고, 2D 그리기 길로 선형 거르기, source-over 로 늘여 얹는다. (UI 는 기기 화소 그대로, 유니티 URP renderScale, 언리얼 화면 비율과 같은 뜻) 판은 제 깊이를 쓰므로 set_depth_cleared(false) 로 앞의 3D 깊이를 이어 쓰던 것은 배율이 1 일 때만 된다. 판은 노드에서 떨어질 때(detach)와 버려질 때(Drop) 지운다.
  • 원본과 다른 것:
  • 공유 렌더러는 Rc<RefCell<ToonRenderer>> 다. 원본은 객체 참조를 나눠 든다. 그리는 동안 렌더러를 빌려 그리기 함수에 넘긴다.
  • 원본 화면 사각형 객체의 outputFramebuffer, outputViewport 는 다섯째 인자 ToonViewOutput 의 output_framebuffer, output_viewport 다.

Component that draws toon 3D inside the node rectangle. (ToonView3D of vanilla.js graphics3d/toon/toonview3d.js)

  • In draw: maps the node's content area (0, 0, content size) with the current transform to a rectangle in drawing buffer pixels (the bounding rectangle if rotated), flushes the preceding 2D batch with Graphic::run_external_pass, forgets the cached state and clears depth, then sets the renderer's screen rectangle (set_screen_rect) and calls the draw function. When done, releases the screen rectangle and restores the 2D state with restore_render_state. (Both are restored even if the draw function panics)
  • The renderer is built on the first draw. On devices that cannot build the shaders, it is not rebuilt and 2D is drawn with the fallback draw function (set_fallback_draw_callback). To share one renderer across multiple nodes, pass it in with set_renderer.
  • The aspect ratio of the camera (get_camera_mut) is matched to the rectangle's ratio on every draw. 2D markers over 3D points are computed with the same camera's project_to_view.
  • Depth clearing: inside a parent Mask's clip there is the Adreno 660 pitfall (Graphic::clear_depth_buffer header). The side that applies the clip calls clear_depth_buffer before begin_clip_rect, and this component is set with set_depth_cleared(false).
  • Draw function: (renderer, camera, screen rectangle, GL, output). The screen rectangle is the rectangle to draw in the current framebuffer (pixels, top-left origin). The output (ToonViewOutput) is the framebuffer bound now (None means the default framebuffer) and the GL viewport inside it ([x, y, width, height], integers, bottom-left origin). Usually queue into ToonDrawQueue and call queue.draw(gl, renderer, camera), or draw between begin_frame_with_camera and end_frame. When using ToonPostProcess, call renderer.clear_screen_rect(), draw into a float render target, then give end the framebuffer and viewport of the output. (That way it lands in the right place regardless of render scale or transparency layers)
  • Render scale (set_render_scale, 0.05 ~ 4, default 1): if not 1, only 3D is cleared to transparent and drawn into a target of the content area screen size x scale (depth 24, 4 samples if the default framebuffer is multisampled), then stretched on through the 2D drawing path with linear filtering and source-over. (UI stays at device pixels, same meaning as Unity URP renderScale and Unreal screen percentage) The target uses its own depth, so continuing the earlier 3D depth with set_depth_cleared(false) only works at scale 1. The target is deleted when detached from the node (detach) and when dropped (Drop).
  • Differences from the original:
  • The shared renderer is Rc<RefCell<ToonRenderer>>. The original shares object references. While drawing, the renderer is borrowed and passed to the draw function.
  • outputFramebuffer, outputViewport of the original screen rectangle object are output_framebuffer, output_viewport of the fifth argument ToonViewOutput.

ToonViewDrawCallbacktype

소스source

그리기 함수. (렌더러, 카메라, 화면 사각형, GL, 출력)

Draw function. (renderer, camera, screen rectangle, GL, output)

pub type ToonViewDrawCallback = Box<dyn FnMut(&mut ToonRenderer, &Camera3D, Rect, &GlContext, ToonViewOutput)>

ToonViewOutputstruct traits Clone, Copy, Debug, PartialEq, Eq

소스source

그리기 함수의 출력. (원본 화면 사각형 객체의 outputFramebuffer, outputViewport)

  • ToonPostProcess::end 처럼 따로 내는 것은 이 둘로 내야 렌더 스케일, 투명도 층과 상관없이 맞는 자리에 낸다.

Output of the draw function. (outputFramebuffer, outputViewport of the original screen rectangle object)

  • Anything that outputs separately, such as ToonPostProcess::end, must output with these two to land in the right place regardless of render scale or transparency layers.
예제Example기본 프레임 버퍼에 그리는 출력Output drawn to the default framebuffer
use vanilla::ToonViewOutput;

let output = ToonViewOutput { output_framebuffer: None, output_viewport: [0, 0, 960, 540] };
assert!(output.output_framebuffer.is_none());
assert_eq!(output.output_viewport[2], 960);
필드fieldpub output_framebuffer: Option<GlFramebuffer>#0.4.0 부터since 0.4.0소스source

지금 묶인 프레임 버퍼. (None 이면 기본 프레임 버퍼)

The framebuffer bound now. (None means the default framebuffer)

반환ReturnsOption<GlFramebuffer>

필드fieldpub output_viewport: [i32; 4]#0.4.0 부터since 0.4.0소스source

그 안의 GL 뷰포트. ([x, y, 너비, 높이], 정수 픽셀, 왼쪽 아래 원점)

The GL viewport inside it. ([x, y, width, height], integer pixels, bottom-left origin)

반환Returns[i32; 4]

ToonViewFallbackDrawCallbacktype

소스source

대체 그리기 함수. (렌더러를 짓지 못한 기기에서 2D 로, 노드 변환이 걸린 Graphic 과 내용 크기)

Fallback draw function. (2D on devices where the renderer could not be built, the Graphic with the node transform applied and the content size)

pub type ToonViewFallbackDrawCallback = Box<dyn FnMut(&mut Graphic, Vector2)>

ToonView3Dstruct traits Default, Component, Drop

짝Counterpart: vanilla.js ToonView3D소스source

툰 3D 를 노드 사각형 안에 그리는 컴포넌트. (머리말 참고)

Component that draws toon 3D inside the node rectangle. (see the header comment)

예제Example노드 사각형 안에 툰 3D 장면 그리기Draw a toon 3D scene inside a node rectangle
use vanilla::{create_sphere_geometry, Camera3D, GlContext, Matrix4, MeshBuilder, NodeTree, Rect, ToonDrawOptions, ToonDrawQueue, ToonMesh, ToonPartOptions, ToonRenderer, ToonView3D, ToonViewOutput, Vector3};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let view_id = node_tree.create_child(root_id, "view");
node_tree[view_id].set_content_size_xy(320.0, 240.0);
let view = node_tree[view_id].add_component(ToonView3D::new());
view.get_camera_mut().set_perspective(0.8, 0.1, 100.0);
view.get_camera_mut().look_at(Vector3::create(0.0, 1.5, 3.0), Vector3::create(0.0, 0.5, 0.0), Vector3::create(0.0, 1.0, 0.0));
let mut builder = MeshBuilder::new();
builder.add_part(&create_sphere_geometry(0.5, 24, 16, std::f64::consts::PI), &Matrix4::create_translation(0.0, 0.5, 0.0), "#f0b080", ToonPartOptions::default());
let arrays = builder.read_arrays();
let mut mesh: Option<ToonMesh> = None;
view.set_draw_callback(Some(Box::new(move |renderer: &mut ToonRenderer, camera: &Camera3D, _screen_rect: Rect, gl: &vanilla::GlContext, _output: ToonViewOutput| {
	if mesh.is_none() {
		mesh = renderer.create_mesh(gl, &arrays);
	}
	if let Some(mesh) = mesh.as_ref() {
		let mut queue = ToonDrawQueue::new();
		queue.add_mesh(mesh, &Matrix4::create_identity(), ToonDrawOptions::default());
		queue.draw(gl, renderer, camera);
	}
})));
assert!(view.get_renderer().is_none());
assert!(view.is_depth_cleared());
assert_eq!(view.get_render_scale(), 1.0);
Draw a toon 3D scene inside a node rectangle
생성자constructorpub fn new() -> ToonView3D#0.4.0 부터since 0.4.0소스source

생성. (깊이를 비움)

Constructor. (clears depth)

반환ReturnsToonView3D 새 컴포넌트. (렌더러는 처음 그릴 때 짓는다)New component. (The renderer is built on the first draw)

짝Counterpart: vanilla.js ToonView3D.constructor

메서드methodpub fn set_render_scale(&mut self, render_scale: f64)#0.4.0 부터since 0.4.0소스source

렌더 스케일 설정. (3D 만 이 배율의 판에 그려 선형 거르기로 늘여 얹는다, UI 는 기기 화소 그대로. 유니티 URP renderScale, 언리얼 화면 비율과 같은 뜻)

  • 1(기본)이면 화면에 바로 그린다. 1 이 아니면 판(내용 영역의 화면 크기 x 배율, 깊이 포함, 기본 프레임 버퍼가 멀티샘플이면 표본 4)에 투명하게 비운 뒤 그리고, 2D 그리기 길로 source-over 로 얹는다. (2D 자르기를 따르고, 3D 가 덮지 않은 곳은 아래 2D 가 비친다)
  • 판은 제 깊이를 쓰므로 set_depth_cleared(false) 로 앞의 3D 깊이를 이어 쓰던 것은 배율이 1 일 때만 된다.
  • 그리기 함수는 넘겨받은 출력(ToonViewOutput)의 프레임 버퍼와 뷰포트로 내야 배율과 상관없이 맞는 자리에 낸다. (ToonPostProcess::end 같은 것)

Sets the render scale. (Only 3D is drawn into a target at this scale and stretched on with linear filtering; UI stays at device pixels. Same meaning as Unity URP renderScale and Unreal screen percentage)

  • At 1 (default) it draws directly to the screen. Otherwise it clears a target (content area screen size x scale, with depth, 4 samples if the default framebuffer is multisampled) to transparent, draws into it, and composites it with source-over through the 2D drawing path. (It follows 2D clipping, and the 2D below shows through where 3D does not cover)
  • The target uses its own depth, so continuing the earlier 3D depth with set_depth_cleared(false) only works at scale 1.
  • The draw function must output with the framebuffer and viewport of the output it receives (ToonViewOutput) to land in the right place regardless of scale. (Such as ToonPostProcess::end)
render_scalef64
배율. (0.05 ~ 4, 1 이면 화면에 바로, 유한한 수가 아니면 1)Scale. (0.05 ~ 4, 1 draws directly to the screen, 1 if not a finite number)

짝Counterpart: vanilla.js ToonView3D.setRenderScale

예제Example3D 만 반 해상도로 그리기Drawing only the 3D at half resolution
use vanilla::{NodeTree, ToonView3D};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let view_id = node_tree.create_child(root_id, "view");
node_tree[view_id].set_content_size_xy(320.0, 240.0);
let view = node_tree[view_id].add_component(ToonView3D::new());
view.set_render_scale(0.5);
assert_eq!(view.get_render_scale(), 0.5);
메서드methodpub fn get_render_scale(&self) -> f64#0.4.0 부터since 0.4.0소스source

렌더 스케일 반환.

Returns the render scale.

반환Returnsf64 배율. (1 이면 화면에 바로 그린다)Scale. (1 draws directly to the screen)

짝Counterpart: vanilla.js ToonView3D.getRenderScale

메서드methodpub fn set_draw_callback(&mut self, draw_callback: Option<ToonViewDrawCallback>)#0.4.0 부터since 0.4.0소스source

그리기 함수 설정. (None 이면 그리지 않음)

Sets the draw function. (nothing is drawn when None)

draw_callbackOption<ToonViewDrawCallback>
(렌더러, 카메라, 화면 사각형, GL, 출력) 를 받는 함수. (None 이면 그리지 않음) 화면 사각형은 지금 프레임 버퍼에서 그릴 사각형(픽셀, 왼쪽 위 원점), 출력은 지금 묶인 프레임 버퍼와 그 안의 GL 뷰포트(왼쪽 아래 원점)다.Function that receives (renderer, camera, screen rectangle, GL, output). (Draws nothing if None) The screen rectangle is the rectangle to draw in the current framebuffer (pixels, top-left origin), and the output is the framebuffer bound now and the GL viewport inside it (bottom-left origin).

짝Counterpart: vanilla.js ToonView3D.setDrawCallback

메서드methodpub fn set_fallback_draw_callback(&mut self, fallback_draw_callback: Option<ToonViewFallbackDrawCallback>)#0.4.0 부터since 0.4.0소스source

대체 그리기 함수 설정. (렌더러를 짓지 못한 기기에서 2D 로)

Sets the fallback draw function. (2D on devices where the renderer could not be built)

fallback_draw_callbackOption<ToonViewFallbackDrawCallback>
(graphic, content_size) 를 받는 함수. 렌더러를 짓지 못한 기기에서 부른다. (None 이면 그리지 않음)Function that receives (graphic, content_size). Called on devices where the renderer could not be built. (Draws nothing if None)

짝Counterpart: vanilla.js ToonView3D.setFallbackDrawCallback

메서드methodpub fn set_depth_cleared(&mut self, is_depth_cleared: bool)#0.4.0 부터since 0.4.0소스source

그리기 전에 깊이를 비울지 설정. (기본 true)

Sets whether to clear depth before drawing. (default true)

is_depth_clearedbool
그리기 전에 깊이를 비울지. (부모 Mask 의 자르기 안이면 false 로 두고 자르는 쪽이 비운다)Whether to clear depth before drawing. (Inside a parent Mask clip, leave it false and let the clipping side clear)

짝Counterpart: vanilla.js ToonView3D.setDepthCleared

메서드methodpub fn is_depth_cleared(&self) -> bool#0.4.0 부터since 0.4.0소스source

그리기 전에 깊이를 비우는지 반환.

Returns whether depth is cleared before drawing.

반환Returnsbool 그리기 전에 깊이를 비우면 true.true if depth is cleared before drawing.

짝Counterpart: vanilla.js ToonView3D.isDepthCleared

메서드methodpub fn set_renderer(&mut self, renderer: Option<Rc<RefCell<ToonRenderer>>>)#0.4.0 부터since 0.4.0소스source

렌더러 넣기. (여러 노드가 하나를 함께 쓸 때, None 이면 처음 그릴 때 짓는다)

Sets the renderer. (when multiple nodes share one, if None it is built on the first draw)

rendererOption<Rc<RefCell<ToonRenderer>>>
함께 쓸 렌더러. (None 이면 처음 그릴 때 짓는다)Renderer to share. (Built on the first draw if None)

짝Counterpart: vanilla.js ToonView3D.setRenderer

메서드methodpub fn get_renderer(&self) -> Option<&Rc<RefCell<ToonRenderer>>>#0.4.0 부터since 0.4.0소스source

렌더러 반환. (아직 짓지 않았거나 짓지 못했으면 None)

Returns the renderer. (None if not yet built or could not be built)

반환ReturnsOption<&Rc<RefCell<ToonRenderer>>> 렌더러. (아직 짓지 않았거나 짓지 못했으면 None)Renderer. (None if not built yet or could not be built)

짝Counterpart: vanilla.js ToonView3D.getRenderer

메서드methodpub fn is_renderer_failed(&self) -> bool#0.4.0 부터since 0.4.0소스source

렌더러를 짓지 못했는지.

Whether the renderer could not be built.

반환Returnsbool 렌더러를 짓지 못했으면 true. (그때는 대체 그리기 함수로 그린다)true if the renderer could not be built. (Then the fallback draw function draws)

짝Counterpart: vanilla.js ToonView3D.isRendererFailed

메서드methodpub fn get_camera(&self) -> &Camera3D#0.4.0 부터since 0.4.0소스source

카메라 반환.

Returns the camera.

반환Returns&Camera3D 이 컴포넌트의 카메라. (가로 세로 비는 그릴 때마다 사각형에 맞춘다)The camera of this component. (Its aspect ratio is matched to the rectangle on every draw)

짝Counterpart: vanilla.js ToonView3D.getCamera

메서드methodpub fn get_camera_mut(&mut self) -> &mut Camera3D#0.4.0 부터since 0.4.0소스source

카메라 반환. (투영과 보는 자리를 정할 때)

Returns the camera. (for setting the projection and view position)

반환Returns&mut Camera3D 이 컴포넌트의 카메라. (가로 세로 비는 그릴 때마다 사각형에 맞춘다)The camera of this component. (Its aspect ratio is matched to the rectangle on every draw)

짝Counterpart: vanilla.js ToonView3D.getCamera

메서드methodpub fn get_screen_rect(&self) -> Rect#0.4.0 부터since 0.4.0소스source

지난 그리기의 화면 사각형 반환. (드로잉 버퍼 픽셀)

Returns the screen rectangle of the last draw. (drawing buffer pixels)

반환ReturnsRect 드로잉 버퍼 픽셀의 사각형. (왼쪽 위가 원점, 아직 그리지 않았으면 빈 사각형)Rectangle in drawing buffer pixels. (Top left is the origin, an empty rectangle if not drawn yet)

짝Counterpart: vanilla.js ToonView3D.getScreenRect

메서드methodpub fn read_renderer(&mut self, graphic: &mut Graphic) -> Option<Rc<RefCell<ToonRenderer>>>#0.4.0 부터since 0.4.0소스source

렌더러 읽기. (처음이면 짓고, 짓지 못하면 None 이고 다시 짓지 않는다)

Reads the renderer. (builds it the first time, None if it cannot be built, and it is not rebuilt)

graphic&mut Graphic
그래픽. (처음이면 그 GL 로 렌더러를 짓는다)Graphic. (On the first call, builds the renderer with its GL)

반환ReturnsOption<Rc<RefCell<ToonRenderer>>> 렌더러. (짓지 못했으면 None)Renderer. (None if it could not be built)

짝Counterpart: vanilla.js ToonView3D.readRenderer

메서드methodpub fn refresh_screen_rect(&mut self, graphic: &Graphic, content_size: Vector2)#0.4.0 부터since 0.4.0소스source

내용 영역을 드로잉 버퍼 픽셀의 사각형으로. (지금 변환으로 네 귀를 옮겨 감싸는 사각형)

Content area to a rectangle in drawing buffer pixels. (Bounding rectangle of the four corners mapped with the current transform)

graphic&Graphic
Graphic. (지금 변환을 읽는다)Graphic. (Reads the current transform)
content_sizeVector2
내용 영역의 크기.Size of the content area.

짝Counterpart: vanilla.js ToonView3D.refreshScreenRect