graphics3d/toon/toonview3d
툰 3D 를 노드 사각형 안에 그리는 컴포넌트. (2D 노드 트리 사이에 3D 패스를 끼운다)
Component that draws toon 3D inside the node rectangle. (Inserts a 3D pass between the 2D node tree)
import { WorldNode, ToonView3D, ToonDrawQueue, MeshBuilder, createSphereGeometry, Matrix4, Vector3 } from "./vanilla.js";
const node = new WorldNode();
node.setContentSizeXY(320, 240);
const view = node.addComponent(ToonView3D);
view.getCamera().setPerspective(0.8, 0.1, 100);
view.getCamera().lookAt(Vector3.create(0, 1.5, 3), Vector3.create(0, 0.5, 0), Vector3.create(0, 1, 0));
const builder = new MeshBuilder();
builder.addPart(createSphereGeometry(0.5, 24, 16), Matrix4.createTranslation(0, 0.5, 0), "#f0b080");
const arrays = builder.readArrays();
const queue = new ToonDrawQueue();
let mesh = null;
view.setDrawCallback((renderer, camera) => {
if (mesh === null) {
mesh = renderer.createMesh(arrays);
}
queue.clear();
queue.addMesh(mesh, Matrix4.createIdentity());
queue.draw(renderer, camera);
});ToonView3Dextends Component
툰 3D 를 노드 사각형 안에 그리는 컴포넌트. (2D 노드 트리 사이에 3D 패스를 끼운다)
- draw 에서: 노드의 내용 영역(0, 0, 내용 크기)을 지금 변환으로 드로잉 버퍼 픽셀의 사각형으로 옮기고(돌았으면 감싸는 사각형), Graphic.runExternalPass 로 앞의 2D 묶음을 그리고 깊이를 비운 뒤, 렌더러의 화면 사각형(setScreenRect)을 걸고 그리기 함수를 부른다. 끝나면 화면 사각형을 풀고 restoreRenderState 로 2D 상태를 되돌린다.
- 게임이 draw 앞에 같은 컨텍스트로 WebGL 을 직접 써도(그림자 지도 같은 것) 된다. 게임 프레임 버퍼를 묶었으면 들어올 때 것으로 돌려 두고 스텐실은 바꾸지 않는다. (Graphic.runExternalPass 머리말)
- 렌더러는 처음 그릴 때 짓는다. 셰이더를 짓지 못하는 기기면 다시 짓지 않고 대체 그리기 함수(setFallbackDrawCallback)로 2D 를 그린다. 여러 노드가 렌더러 하나를 함께 쓰려면 setRenderer 로 넣는다.
- 카메라(getCamera)의 가로 세로 비는 그릴 때마다 사각형의 비로 맞춘다. 3D 점 위의 2D 표시는 같은 카메라의 projectToView 로 셈한다.
- 깊이 비우기: 부모 Mask 의 자르기 안이면 Adreno 660 함정(Graphic.clearDepthBuffer 머리말)이 있다. 자르기를 거는 쪽이 beginClipRect 앞에서 clearDepthBuffer 를 부르고 이 컴포넌트는 setDepthCleared(false) 로 둔다.
- 그리기 함수: (renderer, camera, screenRect, webGL2RenderingContext, output) => void. screenRect 는 지금 프레임 버퍼에서 그릴 사각형(Rect, 픽셀, 왼쪽 위 원점)이다. output 은 { outputFramebuffer, outputViewport } 다. 지금 묶인 프레임 버퍼(null 이면 캔버스)와 그 안의 GL 뷰포트([x, y, 너비, 높이], 정수, 왼쪽 아래 원점). 보통 ToonDrawQueue 에 담아 queue.draw(renderer, camera) 하거나 renderer.beginFrameWithCamera 와 endFrame 사이에 그린다. ToonPostProcess 를 쓰면 renderer.clearScreenRect() 를 부르고 실수 렌더 타겟에 그린 뒤 end 에 output 의 프레임 버퍼와 뷰포트를 준다. (그래야 렌더 스케일, 투명도 층과 상관없이 맞는 자리에 낸다)
Component that draws toon 3D inside the node rectangle. (Inserts a 3D pass between the 2D node tree)
- 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), draws the preceding 2D batch with Graphic.runExternalPass and clears depth, then sets the renderer's screen rectangle (setScreenRect) and calls the draw function. When done, releases the screen rectangle and restores the 2D state with restoreRenderState.
- A game may use WebGL directly through the same context before draw (a shadow map and the like). If it bound its own framebuffer it puts back the one it found, and it does not change the stencil. (Graphic.runExternalPass header)
- 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 (setFallbackDrawCallback). To share one renderer across multiple nodes, pass it in with setRenderer.
- The aspect ratio of the camera (getCamera) is matched to the rectangle's ratio on every draw. 2D markers over 3D points are computed with the same camera's projectToView.
- Depth clearing: inside a parent Mask's clip there is the Adreno 660 pitfall (Graphic.clearDepthBuffer header). The side that applies the clip calls clearDepthBuffer before beginClipRect, and this component is set with setDepthCleared(false).
- Draw function: (renderer, camera, screenRect, webGL2RenderingContext, output) => void. screenRect is the rectangle to draw in the current framebuffer (Rect, pixels, top-left origin). output is { outputFramebuffer, outputViewport }: the framebuffer bound now (null for the canvas) and the GL viewport inside it ([x, y, width, height], integers, bottom-left origin). Usually queue into ToonDrawQueue and call queue.draw(renderer, camera), or draw between renderer.beginFrameWithCamera and endFrame. When using ToonPostProcess, call renderer.clearScreenRect(), draw into a float render target, then give end the framebuffer and viewport of output. (so it lands in the right place regardless of render scale and opacity layers)
import { WorldNode, ToonView3D, Rect, Vector3 } from "./vanilla.js";
const node = new WorldNode();
node.setContentSizeXY(400, 300);
const view = node.addComponent(ToonView3D);
const camera = view.getCamera();
camera.setPerspective(0.8, 0.1, 100);
camera.setAspectRatio(400 / 300);
camera.lookAt(Vector3.create(0, 2, 4), Vector3.create(0, 0, 0), Vector3.create(0, 1, 0));
const labelPosition = camera.projectToView(0, 1, 0, Rect.create(0, 0, 400, 300));
const isLabelShown = labelPosition.isInFront && labelPosition.x >= 0 && labelPosition.x <= 400;렌더 스케일 설정. (3D 만 이 배율의 판에 그려 선형 거르기로 늘여 얹는다, UI 는 기기 화소 그대로. 유니티 URP renderScale, 언리얼 화면 비율과 같은 뜻)
- 1(기본)이면 지금처럼 화면에 바로 그린다. 1 이 아니면 판(내용 영역의 화면 크기 x 배율, 깊이 포함, 캔버스가 안티에일리어싱이면 멀티샘플 4)에 투명하게 비운 뒤 그리고, 2D 그리기 길로 source-over 로 얹는다. (2D 자르기를 따르고, 3D 가 덮지 않은 곳은 아래 2D 가 비친다)
- 판은 제 깊이를 쓰므로 setDepthCleared(false) 로 앞의 3D 깊이를 이어 쓰던 것은 배율이 1 일 때만 된다.
- 그리기 함수는 넘겨받은 출력(output)의 프레임 버퍼와 뷰포트로 내야 배율과 상관없이 맞는 자리에 낸다. (ToonPostProcess.end 같은 것)
Set the render scale. (only the 3D is drawn into a target at this scale and stretched on with linear filtering, the UI stays at device pixels. Same meaning as Unity URP renderScale and Unreal screen percentage)
- At 1 (default) it draws straight to the screen as before. Otherwise it clears a target (the on-screen size of the content area x scale, with depth, multisampled with 4 samples when the canvas is anti-aliased) to transparent, draws into it and puts it on with source-over through the 2D drawing path. (it follows 2D clipping, and the 2D below shows where the 3D does not cover)
- The target has its own depth, so reusing earlier 3D depth with setDepthCleared(false) only works at scale 1.
- The draw function must output to the framebuffer and viewport of the output it receives to land in the right place at any scale. (ToonPostProcess.end and so on)
- renderScalenumber
- 배율. (0.05 ~ 4, 1 이면 화면에 바로, 수가 아니면 1)Scale. (0.05 to 4, 1 draws straight to the screen, 1 when not a number)
짝Counterpart: vanilla-engine ToonView3D.set_render_scale
import { WorldNode, ToonView3D } from "./vanilla.js";
const node = new WorldNode();
node.setContentSizeXY(320, 240);
const view = node.addComponent(ToonView3D);
view.setRenderScale(0.5);
const renderScale = view.getRenderScale();렌더 스케일 반환.
Return the render scale.
반환Returnsnumber 배율. (1 이면 화면에 바로 그린다)Scale. (1 draws straight to the screen)
짝Counterpart: vanilla-engine ToonView3D.get_render_scale
그리기 함수 설정. ((renderer, camera, screenRect, webGL2RenderingContext, output) => void, null 이면 그리지 않음)
- screenRect 는 지금 프레임 버퍼에서 그릴 사각형(Rect, 픽셀, 왼쪽 위 원점, 그리기마다 고쳐 쓰는 같은 객체)이다.
- output 은 { outputFramebuffer, outputViewport } 다. outputFramebuffer 는 지금 묶인 프레임 버퍼(null 이면 캔버스), outputViewport 는 그 안의 GL 뷰포트([x, y, 너비, 높이], 정수, 왼쪽 아래 원점)다. (러스트 판 ToonViewOutput 의 output_framebuffer, output_viewport) ToonPostProcess.end 처럼 따로 내는 것은 이 둘로 내야 렌더 스케일, 투명도 층과 상관없이 맞는 자리에 낸다.
Set the draw function. ((renderer, camera, screenRect, webGL2RenderingContext, output) => void, null draws nothing)
- screenRect is the rect to draw in the current framebuffer (Rect, pixels, top-left origin, the same object rewritten on every draw).
- output is { outputFramebuffer, outputViewport }. outputFramebuffer is the framebuffer bound now (null for the canvas), and outputViewport is the GL viewport inside it ([x, y, width, height], integers, bottom-left origin). (output_framebuffer and output_viewport of Rust ToonViewOutput) Anything output separately, such as ToonPostProcess.end, must use these two to land in the right place regardless of render scale and opacity layers.
- drawCallbackFunction | null
- (renderer, camera, screenRect, webGL2RenderingContext, output) => void. (null 이면 그리지 않음)(renderer, camera, screenRect, webGL2RenderingContext, output) => void. (does not draw if null)
짝Counterpart: vanilla-engine ToonView3D.set_draw_callback
import { WorldNode, ToonView3D, ToonPostProcess } from "./vanilla.js";
const node = new WorldNode();
node.setContentSizeXY(320, 240);
const view = node.addComponent(ToonView3D);
let postProcess = null;
view.setDrawCallback((renderer, camera, screenRect, webGL2RenderingContext, output) => {
if (postProcess === null) {
postProcess = new ToonPostProcess(webGL2RenderingContext, screenRect.size.x, screenRect.size.y);
renderer.setHdrOutput(postProcess.getUseFloatColor());
}
renderer.clearScreenRect();
postProcess.resize(screenRect.size.x, screenRect.size.y);
postProcess.begin([0.05, 0.05, 0.08]);
renderer.beginFrameWithCamera(camera);
renderer.endFrame();
postProcess.end(output.outputFramebuffer, output.outputViewport);
});대체 그리기 함수 설정. (렌더러를 짓지 못한 기기에서 2D 로, (graphic, contentSize) => void)
Sets the fallback draw function. (2D on devices where the renderer could not be built, (graphic, contentSize) => void)
- fallbackDrawCallbackFunction | null
- (graphic, contentSize) => void. 렌더러를 짓지 못한 기기에서 부른다. (null 이면 그리지 않음)(graphic, contentSize) => void. Called on devices where the renderer could not be built. (Draws nothing if null)
짝Counterpart: vanilla-engine ToonView3D.set_fallback_draw_callback
import { WorldNode, ToonView3D, Rect } from "./vanilla.js";
const node = new WorldNode();
node.setContentSizeXY(320, 240);
const view = node.addComponent(ToonView3D);
view.setFallbackDrawCallback((graphic, contentSize) => {
graphic.setFillColor("#203040");
graphic.drawRect(Rect.create(0, 0, contentSize.x, contentSize.y));
});그리기 전에 깊이를 비울지 설정. (기본 true)
Sets whether to clear depth before drawing. (default true)
- isDepthClearedboolean
- 그리기 전에 깊이를 비울지. (부모 Mask 의 자르기 안이면 false 로 두고 자르는 쪽이 비운다)Whether to clear depth before drawing. (Inside a parent Mask clip, leave it false and let the clipping side clear)
짝Counterpart: vanilla-engine ToonView3D.set_depth_cleared
import { WorldNode, ToonView3D } from "./vanilla.js";
const node = new WorldNode();
node.setContentSizeXY(200, 200);
const view = node.addComponent(ToonView3D);
view.setDepthCleared(false);그리기 전에 깊이를 비우는지 반환.
Returns whether depth is cleared before drawing.
반환Returnsboolean 그리기 전에 깊이를 비우면 true.true if depth is cleared before drawing.
짝Counterpart: vanilla-engine ToonView3D.is_depth_cleared
렌더러 넣기. (여러 노드가 하나를 함께 쓸 때, null 이면 처음 그릴 때 짓는다)
Sets the renderer. (When multiple nodes share one, if null it is built on the first draw)
- rendererToonRenderer | null
- 함께 쓸 렌더러. (null 이면 처음 그릴 때 짓는다)Renderer to share. (Built on the first draw if null)
짝Counterpart: vanilla-engine ToonView3D.set_renderer
import { WorldNode, ToonView3D } from "./vanilla.js";
const leftNode = new WorldNode();
leftNode.setContentSizeXY(200, 200);
const rightNode = new WorldNode();
rightNode.setContentSizeXY(200, 200);
const leftView = leftNode.addComponent(ToonView3D);
const rightView = rightNode.addComponent(ToonView3D);
leftView.setDrawCallback((renderer) => {
if (rightView.getRenderer() === null) {
rightView.setRenderer(renderer);
}
});렌더러 반환. (아직 짓지 않았거나 짓지 못했으면 null)
Returns the renderer. (null if not yet built or could not be built)
반환ReturnsToonRenderer | null 렌더러. (아직 짓지 않았거나 짓지 못했으면 null)Renderer. (null if not built yet or could not be built)
짝Counterpart: vanilla-engine ToonView3D.get_renderer
렌더러를 짓지 못했는지.
Whether the renderer could not be built.
반환Returnsboolean 렌더러를 짓지 못했으면 true. (그때는 대체 그리기 함수로 그린다)true if the renderer could not be built. (Then the fallback draw function draws)
짝Counterpart: vanilla-engine ToonView3D.is_renderer_failed
카메라 반환.
Returns the camera.
반환ReturnsCamera3D 이 컴포넌트의 카메라. (가로 세로 비는 그릴 때마다 사각형에 맞춘다)The camera of this component. (Its aspect ratio is matched to the rectangle on every draw)
짝Counterpart: vanilla-engine ToonView3D.get_camera
지난 그리기의 화면 사각형 반환. (드로잉 버퍼 픽셀)
Returns the screen rectangle of the last draw. (drawing buffer pixels)
반환ReturnsRect 드로잉 버퍼 픽셀의 사각형. (왼쪽 위가 원점, 아직 그리지 않았으면 빈 사각형, 복사본)Rectangle in drawing buffer pixels. (top-left origin, an empty rectangle before the first draw, a copy)
짝Counterpart: vanilla-engine ToonView3D.get_screen_rect
렌더러 읽기. (처음이면 짓고, 짓지 못하면 null 이고 다시 짓지 않는다)
Reads the renderer. (Builds it the first time, null if it cannot be built, and it is not rebuilt)
- graphicobject
- Graphic
반환ReturnsToonRenderer | null 렌더러. (짓지 못했으면 null)Renderer. (null if it could not be built)
짝Counterpart: vanilla-engine ToonView3D.read_renderer
내용 영역을 드로잉 버퍼 픽셀의 사각형으로. (지금 변환으로 네 귀를 옮겨 감싸는 사각형)
Content area to a rectangle in drawing buffer pixels. (Bounding rectangle of the four corners mapped with the current transform)
- graphicobject
- Graphic. (지금 변환을 읽는다)Graphic. (Reads the current transform)
- contentSizeobject
- Vector2
짝Counterpart: vanilla-engine ToonView3D.refresh_screen_rect
노드에서 떨어짐. (배율 판을 지운다, 다시 붙으면 처음 쓸 때 다시 짓는다)
Detached from the node. (deletes the scale target, which is built again on first use after reattaching)
- nodeobject
- 떨어진 노드.The node detached from.
내용 영역 안에서만 그리는지 반환. (가위로 자른다)
Returns whether drawing is limited to the content area. (Clipped with scissor)
반환Returnsboolean 늘 true.Always true.