0Pricing
WebAssembly (WASM) for High Performance Apps · บทเรียน

การเรนเดอร์ 2D/3D แบบเรียลไทม์

สร้างงานเรนเดอร์ที่ใช้การคำนวณสูงใน WASM เพื่อให้ได้กราฟิก 2D และ 3D ที่ลื่นไหลและโต้ตอบได้

การเรนเดอร์ 2D/3D แบบเรียลไทม์ เป็นบทเรียน WebAssembly (WASM) for High Performance Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน WebAssembly (WASM) for High Performance Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส WebAssembly (WASM) for High Performance Apps มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Real-time Graphics with WASM

Welcome to creating real-time 2D/3D graphics! This lesson focuses on how WebAssembly (WASM) helps run the complex calculations needed for smooth, interactive visuals.

Real-time rendering means your graphics update continuously, typically many times per second, to create fluid animations and responsive interactions.

The Rendering Loop Explained

Interactive graphics, like games, run on a 'rendering loop'. This loop constantly does two main things:

  • Update State: Calculates new positions, physics, animations, and other game logic.
  • Render Frame: Draws everything onto the screen based on the updated state.

WASM shines in the 'Update State' phase, where many heavy computations happen.

WASM for Math-Heavy Tasks

Many graphics tasks involve intense mathematical operations. Think about:

  • Vector and matrix calculations for 3D transformations.
  • Physics simulations (gravity, collisions).
  • Particle system updates (thousands of particles moving).

WebAssembly's near-native speed makes it perfect for offloading these computations from JavaScript.

Rotating a 2D Point with WASM

Let's see a simple example: rotating a 2D point around an origin. This requires trigonometric functions (sine and cosine). WASM can perform these calculations very efficiently.

Try running this Rust code, which can be compiled to WASM:

#[no_mangle]
pub extern "C" fn rotate_point_2d(x: f32, y: f32, angle_rad: f32, out_ptr: *mut f32) {
    let cos_a = angle_rad.cos();
    let sin_a = angle_rad.sin();
    let new_x = x * cos_a - y * sin_a;
    let new_y = x * sin_a + y * cos_a;
    unsafe {
        *out_ptr = new_x;
        *out_ptr.offset(1) = new_y;
    }
}

// For demonstration, this main function allows local testing.
// In a WASM module, `rotate_point_2d` would be directly exported and called from JavaScript.
fn main() {
    let x = 1.0;
    let y = 0.0;
    let angle = std::f32::consts::PI / 2.0; // 90 degrees
    let mut result_coords = [0.0; 2];
    let out_ptr = result_coords.as_mut_ptr();

    rotate_point_2d(x, y, angle, out_ptr);

    println!("Original: ({}, {})", x, y);
    println!("Rotated by 90 deg: ({:.2}, {:.2})", result_coords[0], result_coords[1]);
}

How JS Calls WASM Graphics Logic

After compiling the Rust code to WASM, JavaScript (JS) loads the module. Then, JS would:

  • Allocate memory in the WASM module for input and output.
  • Pass the point's coordinates (x, y) and rotation angle to the WASM function.
  • Call the rotate_point_2d function.
  • Read the new, rotated coordinates from the WASM memory back into JS.

This allows WASM to do the heavy lifting.

Simple Physics Simulation

Physics engines rely on updating object positions and velocities many times per second. Here's a basic function to update a point's position based on its current position, velocity, and a small time step (delta_time).

This is a core component of many real-time simulations.

#[no_mangle]
pub extern "C" fn update_position(
    pos_x: f32, pos_y: f32,
    vel_x: f32, vel_y: f32,
    delta_time: f32,
    out_ptr: *mut f32
) {
    let new_pos_x = pos_x + vel_x * delta_time;
    let new_pos_y = pos_y + vel_y * delta_time;
    unsafe {
        *out_ptr = new_pos_x;
        *out_ptr.offset(1) = new_pos_y;
    }
}

// For demonstration, this main function allows local testing.
// In a WASM module, `update_position` would be directly exported and called from JavaScript.
fn main() {
    let mut pos_x = 0.0;
    let mut pos_y = 0.0;
    let vel_x = 10.0;
    let vel_y = 5.0;
    let delta_time = 0.1; // 100 milliseconds

    let mut result_coords = [0.0; 2];
    let out_ptr = result_coords.as_mut_ptr();

    println!("Initial Position: ({}, {})", pos_x, pos_y);

    update_position(pos_x, pos_y, vel_x, vel_y, delta_time, out_ptr);
    pos_x = result_coords[0];
    pos_y = result_coords[1];
    println!("Position after 0.1s: ({:.2}, {:.2})", pos_x, pos_y);

    update_position(pos_x, pos_y, vel_x, vel_y, delta_time, out_ptr);
    pos_x = result_coords[0];
    pos_y = result_coords[1];
    println!("Position after 0.2s: ({:.2}, {:.2})", pos_x, pos_y);
}

Handling Many Objects Efficiently

Imagine a game with hundreds or thousands of objects (characters, particles, debris). Each might need its position, rotation, and physics updated every single frame.

Running these updates in JavaScript can become slow. WASM, however, can process large arrays of data and perform these calculations much faster, keeping your application responsive.

Dynamic Particle Effects with WASM

Particle systems are visual effects like smoke, fire, or explosions. They involve creating, moving, and destroying thousands of small particles.

The logic for each particle's behavior, its interaction with the environment, and its lifetime calculations are computationally demanding. WASM is an excellent choice for managing these complex particle system updates efficiently.

Complex 3D Transformations

In 3D graphics, objects are moved, rotated, and scaled using matrix multiplications. These operations are fundamental for displaying scenes correctly and animating them.

A single 3D scene can involve hundreds or thousands of these matrix operations per frame. WASM's ability to perform these calculations at high speed is crucial for smooth and interactive 3D experiences.

WASM's Role in Rendering

Which of the following tasks are best suited for WebAssembly in a real-time 2D/3D rendering application?

Recap: Real-time Rendering

In this lesson, we explored how WebAssembly significantly boosts real-time 2D/3D rendering performance by handling computationally intensive tasks:

  • WASM is ideal for the 'update state' part of the rendering loop.
  • It excels at math-heavy operations like rotations, physics, and matrix transformations.
  • WASM can efficiently manage and update large numbers of objects, such as particles in visual effects.

By offloading these tasks, WASM helps create smoother, more interactive graphics.

คำถามที่พบบ่อย

บทเรียน “การเรนเดอร์ 2D/3D แบบเรียลไทม์” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การเรนเดอร์ 2D/3D แบบเรียลไทม์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebAssembly (WASM) for High Performance Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebAssembly (WASM) for High Performance Apps มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การเรนเดอร์ 2D/3D แบบเรียลไทม์”

สร้างงานเรนเดอร์ที่ใช้การคำนวณสูงใน WASM เพื่อให้ได้กราฟิก 2D และ 3D ที่ลื่นไหลและโต้ตอบได้ คุณปฏิบัติ WebAssembly (WASM) for High Performance Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebAssembly (WASM) for High Performance Apps หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebAssembly (WASM) for High Performance Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การเรนเดอร์ 2D/3D แบบเรียลไทม์” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน WebAssembly (WASM) for High Performance Apps นี้ได้ไหม

ได้ บทเรียน WebAssembly (WASM) for High Performance Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การผสาน WASM กับ WebGL/WebGPU
  2. การเรนเดอร์ 2D/3D แบบเรียลไทม์
  3. การพัฒนาเกมด้วย WebAssembly
  4. การประมวลผลเสียงและการสตรีมทรัพยากรใน WASM
← กลับไปที่ WebAssembly (WASM) for High Performance Apps