Can I use a GPUBuffer which is the output of a compute shader as a Three.js or Babylon.js vertex position buffer?

I am experimenting whith webgpu. I found out that transferring data between cpu and gpu can be quite slow.

My goal is to simulate particle physics on the gpu using webgpu’s compute shaders, then use the ease of use of Three.js or Babylon.js to display the result.

Currently, my simulation works, but I transfer the data from and to the GPU every frame, for computing the new position, and then for displaying using Three.js, as you can see in the code below.

import { Cloth } from "./physim.js";
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { InitDevice } from './webgpu/webgpu.js';

import * as THREE from 'three';
import WebGPURenderer from 'three/examples/jsm/renderers/webgpu/WebGPURenderer.js';

export async function Simulate() {
    const gpu = await InitDevice();
    const cloth = new Cloth(gpu);

    const width = window.innerWidth * 0.9;
    const height = window.innerHeight * 0.9;
    const scene = new THREE.Scene();
    const camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 1000);

    const renderer = new WebGPURenderer({
        antialias: true,
        forceWebGL: false,
    });
    renderer.setSize(width, height);
    document.body.appendChild(renderer.domElement);
    const controls = new OrbitControls(camera, renderer.domElement);

    const geometry = new THREE.BufferGeometry();

    // Get a Float32Array reference representing the vertex positions
    const vertices = cloth.GetPositions();

    // Get a Uint32Array reference representing the vertex indices
    const indices = cloth.GetVertexIndices();

    geometry.setIndex(indices);
    geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 4));

    const material = new THREE.MeshBasicMaterial({ color: 0xff0000, wireframe: true });
    const mesh = new THREE.Mesh(geometry, material);

    scene.add(mesh);
    // Calculate the bounding box of the mesh
    const box = new THREE.Box3().setFromObject(mesh);

    // Get the center of the bounding box
    const center = box.getCenter(new THREE.Vector3());

    // Set the camera's position to the center of the bounding box
    camera.position.copy(center);

    // Move the camera back so the object isn't too large in the view
    camera.position.z += 5;

    // Make the controls look at the center of the bounding box
    controls.target.copy(center);

    async function animate() {
        // uodate the vertices positions buffer. It is quite slow because the buffer is transffered to and from the GPU every frame
        await cloth.Update(0.001);
        geometry.getAttribute("position").needsUpdate = true;
        await renderer.render(scene, camera);
        controls.update();
        requestAnimationFrame(animate);
    }

    await animate()
}

Is there a way to tell Three.js or Babylon.js to use a buffer (as vertex positions) that is already on the GPU, in order to maximize performance? I would like to keep using wgsl shaders instead of library specific languages for the computing part.

In other terms, specifically for Three.js, can I use a GPUBuffer as a BufferAttribute?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật