rustwebgl2wasm-bindgenrust-wasmweb-sys

How to invoke (Rust) web-sys AngleInstancedArrays?


https://developer.mozilla.org/en-US/docs/Web/API/ANGLE_instanced_arrays

Given WebGL2RenderingContext as gl.

We have gl.draw_arrays_instanced.

There is also a web-sys object: web_sys::AngleInstancedArrays with functions like draw_arrays_instanced_angle.

The Web-Sys AngleInstancedArrays must be activated by listing the feature in the Cargo.toml under the dependencies.web_sys entry.

I can access the function but I can not manage to provide the first argument required, which is a reference to a web_sys::AngleInstancedArrays struct. There seems to be no way to construct such an object.

There is also the example posted here: https://developer.mozilla.org/en-US/docs/Web/API/ANGLE_instanced_arrays

I tried that approach in Rust and it doesn't work.

enter image description here

More unwrapping just yields a <js_sys::Object>

Then I saw that web-sys has its own web_sys::AngleInstancedArrays, and tried that. Hence the question.

From the code AngleInstancedArrays::draw_arrays_instanced_angle(&AngleInstancedArrays, GL::TRIANGLES, 0, 6, 2);, yields:

enter image description here

That first argument is just for illustration. The compiler is asking for an instance of AngleInstancedArrays but I can't see how to instantiate one.


Solution

  • Think what is missing is a cast. Following the javascript example ANGLE_instanced_arrays. Corresponding in web-sys get_extension, if successful, returned JsObject is of type web_sys::AngleInstancedArrays. Before using function draw_arrays_instanced_angle a cast is needed, together with the call from the question:

    let etx_angle: web_sys::AngleInstancedArrays = object_js.dyn_into().unwrap();
    etx_angle.draw_arrays_instanced_angle(GL::TRIANGLES, 0, 6, 2);
    

    But as the answer above sketches, for WebGL2 the api is simpler WebGl2RenderingContext.draw_arrays_instanced