graphics3d/ambientocclusioneffect
화면 공간 앰비언트 오클루전. (원본 graphics3d/ambientocclusioneffect.js)
- 깊이 + 노멀 버퍼 → 반구 표본 차폐 → 깊이 인식 블러. 결과는 get_result_texture() 의 r 채널 가시율(1 = 차폐 없음).
- render() 입력은 본 패스의 깊이 텍스처와 월드 노멀 텍스처(알파 = 커버리지), 카메라의 뷰 회전 / 투영 파라미터.
- GLSL 과 커널, 기본값(반경 0.03, 강도 1, 바이어스 0.002, 배율 0.5)은 원본과 같다. 원본 ${SAMPLE_COUNT} 는 숫자 16 으로 옮겼다.
- GL 을 쓰는 함수는 gl 을 받고, 생성과 크기 변경은 GL 객체를 못 만들면 오류(Result)를 돌려준다.
Screen space ambient occlusion. (the original graphics3d/ambientocclusioneffect.js)
- Depth + normal buffers → hemisphere sample occlusion → depth aware blur. The result is the visibility (1 = no occlusion) in the r channel of get_result_texture().
- The render() inputs are the main pass's depth texture and world normal texture (alpha = coverage), and the camera's view rotation / projection parameters.
- The GLSL, kernel and defaults (radius 0.03, intensity 1, bias 0.002, scale 0.5) are the same as the original. The original ${SAMPLE_COUNT} became the number 16.
- Functions that use GL take gl, and creation and resizing return an error (Result) if GL objects cannot be made.
AmbientOcclusionEffectstruct
화면 공간 앰비언트 오클루전.
- 판 셋(차폐, 핑, 퐁)은 RGBA16F 다. 차폐 판의 r 은 가시율, g 는 선형 깊이다.
Screen space ambient occlusion.
- The three buffers (occlusion, ping, pong) are RGBA16F. The occlusion buffer's r is visibility and g is linear depth.
use vanilla::{AmbientOcclusionEffect, Camera3D, RenderTarget, RenderTargetOptions};
fn create_resources(gl: &vanilla::GlContext) -> Result<(RenderTarget, AmbientOcclusionEffect), String> {
let options = RenderTargetOptions { color_attachment_count: 2, use_float_color: true, use_depth_texture: true, ..RenderTargetOptions::default() };
let scene_render_target = RenderTarget::new(gl, 1280, 720, true, Some(options))?;
let mut ambient_occlusion_effect = AmbientOcclusionEffect::new(gl)?;
ambient_occlusion_effect.set_parameters(0.05, 1.2, 0.002);
ambient_occlusion_effect.set_scale(0.5);
ambient_occlusion_effect.resize(gl, 1280, 720)?;
Ok((scene_render_target, ambient_occlusion_effect))
}
fn render_occlusion(gl: &vanilla::GlContext, ambient_occlusion_effect: &AmbientOcclusionEffect, scene_render_target: &RenderTarget, camera: &Camera3D) {
let view_matrix = camera.get_view_matrix();
ambient_occlusion_effect.render(gl, scene_render_target.get_depth_texture(), scene_render_target.get_color_texture(1), view_matrix.get_elements(), camera.get_field_of_view(), camera.get_aspect_ratio(), camera.get_near_distance(), camera.get_far_distance());
let visibility_texture = ambient_occlusion_effect.get_result_texture();
assert!(visibility_texture.is_some());
}생성. (패스 2개 + 반정밀 실수 타겟 3장, resize 로 크기를 맞춘다)
Creates. (2 passes + 3 half float targets, sized with resize)
- gl&GlContext
- GL 컨텍스트. (Graphic::get_gl 로 꺼낸다)GL context. (taken out with Graphic::get_gl)
반환ReturnsResult<AmbientOcclusionEffect, String> 새 이펙트. (반경 0.03, 강도 1, 바이어스 0.002, 배율 0.5, 커널까지 셈한 것) 셰이더 컴파일이나 판 만들기에 실패하면 Err(오류 글).New effect. (radius 0.03, intensity 1, bias 0.002, scale 0.5, with the kernel computed) Err(error text) if shader compilation or buffer creation fails.
짝Counterpart: vanilla.js AmbientOcclusionEffect.constructor
표본 커널 만들기. (GL 무관, 황금각 나선, 높이 고르게, 크기 0.15 ~ 1 제곱 분포)
Makes the sample kernel. (GL independent, golden angle spiral, even heights, sizes in a squared 0.15 ~ 1 distribution)
반환Returns[f32; SAMPLE_COUNT * 3] 표본 SAMPLE_COUNT(16) 개의 x, y, z 를 차례로 이은 배열. (z 가 양수인 반구 안, 표본 길이는 0.15 ~ 1)Array of the x, y, z of SAMPLE_COUNT (16) samples in sequence. (inside the hemisphere with positive z, sample lengths 0.15 ~ 1)
짝Counterpart: vanilla.js AmbientOcclusionEffect.buildSampleKernel
use vanilla::graphics3d::ambientocclusioneffect::SAMPLE_COUNT;
use vanilla::AmbientOcclusionEffect;
let sample_kernel = AmbientOcclusionEffect::build_sample_kernel();
assert_eq!(sample_kernel.len(), SAMPLE_COUNT * 3);
for sample_index in 0..SAMPLE_COUNT {
let x = sample_kernel[sample_index * 3];
let y = sample_kernel[sample_index * 3 + 1];
let z = sample_kernel[sample_index * 3 + 2];
assert!(z > 0.0);
assert!((x * x + y * y + z * z).sqrt() <= 1.0 + 1.0e-5);
}표본 커널 계산. (반구 안에 고르게, 중심 쪽에 더 촘촘히)
Computes the sample kernel. (evenly within the hemisphere, denser toward the center)
짝Counterpart: vanilla.js AmbientOcclusionEffect.computeKernel
크기 변경. (본 패스 해상도, 내부는 축소 배율 적용)
Resizes. (main pass resolution, the downscale factor is applied internally)
- gl&GlContext
- GL 컨텍스트.GL context.
- widthi32
- 본 패스 가로 크기. (픽셀, 판은 배율을 곱해 내림, 최소 1)Width of the main pass. (pixels, the buffers are multiplied by the scale and floored, at least 1)
- heighti32
- 본 패스 세로 크기. (픽셀, 판은 배율을 곱해 내림, 최소 1)Height of the main pass. (pixels, the buffers are multiplied by the scale and floored, at least 1)
반환ReturnsResult<(), String> 판을 맞췄으면 Ok. 다시 짓다 GL 객체를 못 만들면 Err(GL 의 오류 글).Ok if the buffers were fitted. Err(GL error text) if GL objects cannot be made while rebuilding.
짝Counterpart: vanilla.js AmbientOcclusionEffect.resize
pub fn render(&self, gl: &GlContext, depth_texture: Option<GlTexture>, normal_texture: Option<GlTexture>, view_matrix_elements: &[f32; 16], field_of_view_radian: f64, aspect_ratio: f64, near_distance: f64, far_distance: f64)#차폐 렌더링. (차폐 → 가로 블러 → 세로 블러, 결과는 get_result_texture())
- view_matrix_elements 는 뷰 행렬 원소(열 우선 4x4), field_of_view_radian 은 세로 시야각.
- 부수 효과: 깊이/블렌드 비활성, TEXTURE0 활성, 프레임버퍼 바인드가 변경된다.
- 깊이는 0 번, 노멀은 2 번 유닛에 묶었다가 2 번은 끝에 풀어 둔다.
Renders occlusion. (occlusion → horizontal blur → vertical blur, the result is get_result_texture())
- view_matrix_elements is the view matrix elements (column major 4x4), field_of_view_radian is the vertical field of view.
- Side effects: depth/blend disabled, TEXTURE0 active, the framebuffer binding changes.
- Depth is bound to unit 0 and normals to unit 2, and unit 2 is unbound at the end.
- gl&GlContext
- GL 컨텍스트.GL context.
- depth_textureOption<GlTexture>
- 본 패스의 깊이 텍스처. (하드웨어 깊이 0 ~ 1, 1 이면 하늘로 보고 가림 없음)Depth texture of the main pass. (hardware depth 0 ~ 1, 1 is treated as sky with no occlusion)
- normal_textureOption<GlTexture>
- 월드 노멀 텍스처. (rgb 에 -1 ~ 1 노멀을 그대로, 알파 0.5 밑은 빈 곳으로 보고 가림 없음)World normal texture. (-1 ~ 1 normals as is in rgb, alpha below 0.5 is treated as empty with no occlusion)
- view_matrix_elements&[f32; 16]
- 뷰 행렬 원소. (열 우선 4x4, 회전 부분만 쓴다)View matrix elements. (column major 4x4, only the rotation part is used)
- field_of_view_radianf64
- 세로 시야각. (라디안)Vertical field of view. (radians)
- aspect_ratiof64
- 가로 세로 비. (너비 / 높이)Aspect ratio. (width / height)
- near_distancef64
- 투영의 가까운 거리. (깊이를 선형으로 풀 때)Near distance of the projection. (for linearizing depth)
- far_distancef64
- 투영의 먼 거리.Far distance of the projection.
짝Counterpart: vanilla.js AmbientOcclusionEffect.render
결과 텍스처 반환. (render() 이후, r 가시율)
Returns the result texture. (after render(), r visibility)
반환ReturnsOption<GlTexture> 블러를 마친 가시율 텍스처. (퐁 판, r 이 1 이면 가림 없음, g 는 선형 깊이) destroy 뒤면 None.Visibility texture after blurring. (the pong buffer, r of 1 means no occlusion, g is linear depth) None after destroy.
짝Counterpart: vanilla.js AmbientOcclusionEffect.getResultTexture
파라미터 설정. (표본 반경(월드) / 강도 / 깊이 바이어스)
Sets the parameters. (sample radius (world) / strength / depth bias)
- radiusf64
- 표본 반경. (월드 단위, 기본 0.03)Sample radius. (world units, default 0.03)
- intensityf64
- 차폐 강도. (가림 몫에 곱한다, 기본 1)Occlusion intensity. (multiplied by the occluded amount, default 1)
- biasf64
- 깊이 바이어스. (월드 단위, 자기 가림을 막는다, 기본 0.002)Depth bias. (world units, prevents self occlusion, default 0.002)
짝Counterpart: vanilla.js AmbientOcclusionEffect.setParameters
내부 해상도 배율 설정. (resize 전에 호출)
Sets the internal resolution scale. (call before resize)
- scalef64
- 판의 해상도 배율. (본 패스 크기에 곱한다, 기본 0.5, 다음 resize 부터 쓴다)Resolution scale of the buffers. (multiplied by the main pass size, default 0.5, used from the next resize)
짝Counterpart: vanilla.js AmbientOcclusionEffect.setScale
파괴. (GL 리소스 해제)
Destroy. (Releases GL resources)
- gl&GlContext
- 만들 때 쓴 GL 컨텍스트.GL context used at creation.
짝Counterpart: vanilla.js AmbientOcclusionEffect.destroy
차폐 패스 반환.
Returns the occlusion pass.
반환Returns&FullscreenPass 차폐 패스. (깊이와 노멀로 반구 표본 차폐를 셈하는 셰이더)Occlusion pass. (shader that computes hemisphere sample occlusion from depth and normals)
짝Counterpart: vanilla.js AmbientOcclusionEffect.getOcclusionPass
블러 패스 반환.
Returns the blur pass.
반환Returns&FullscreenPass 깊이 인식 블러 패스. (가로, 세로 둘 다 이 패스)Depth aware blur pass. (both horizontal and vertical use this pass)
짝Counterpart: vanilla.js AmbientOcclusionEffect.getBlurPass
차폐 렌더 타겟 반환.
Returns the occlusion render target.
반환Returns&RenderTarget 차폐 렌더 타겟. (블러 전 가시율)Occlusion render target. (visibility before blur)
짝Counterpart: vanilla.js AmbientOcclusionEffect.getOcclusionRenderTarget
핑 렌더 타겟 반환. (가로 블러 결과)
Returns the ping render target. (horizontal blur result)
반환Returns&RenderTarget 핑 렌더 타겟. (가로 블러 결과)Ping render target. (horizontal blur result)
짝Counterpart: vanilla.js AmbientOcclusionEffect.getPingRenderTarget
퐁 렌더 타겟 반환. (최종 결과 보관)
Returns the pong render target. (holds the final result)
반환Returns&RenderTarget 퐁 렌더 타겟. (세로 블러 결과, 결과 텍스처의 주인)Pong render target. (vertical blur result, owner of the result texture)
짝Counterpart: vanilla.js AmbientOcclusionEffect.getPongRenderTarget
표본 반경 반환.
Returns the sample radius.
반환Returnsf64 표본 반경. (월드 단위)Sample radius. (world units)
짝Counterpart: vanilla.js AmbientOcclusionEffect.getRadius
강도 반환.
Returns the strength.
반환Returnsf64 차폐 강도.Occlusion intensity.
짝Counterpart: vanilla.js AmbientOcclusionEffect.getIntensity
깊이 바이어스 반환.
Returns the depth bias.
반환Returnsf64 깊이 바이어스. (월드 단위)Depth bias. (world units)
짝Counterpart: vanilla.js AmbientOcclusionEffect.getBias
내부 해상도 배율 반환.
Returns the internal resolution scale.
반환Returnsf64 판의 해상도 배율.Resolution scale of the buffers.
짝Counterpart: vanilla.js AmbientOcclusionEffect.getScale
상수Constants
반구 표본 수. (프래그먼트마다 회전 노이즈로 흩는다, 셰이더의 16 과 같아야 한다)
Hemisphere sample count. (scattered per fragment with rotation noise, must match the 16 in the shader)
반환Returnsusize