# @triadica/lagopus

> Based on:

Latest version **0.2.0** (published 2024-11-19) · 0 weekly downloads

## Install

```sh
npm install @triadica/lagopus
pnpm add @triadica/lagopus
yarn add @triadica/lagopus
bun add @triadica/lagopus
```

## Health

**Score 25/100 (F)** — status: maintenance-mode.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; low quality score; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2024-11-19 |
| First published | 2023-02-26 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | ESM |
| Dependencies | 3 |
| Unpacked size | 168.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | jiyinyiyong |

## Links

- npm: https://www.npmjs.com/package/@triadica/lagopus
- npm.io page: https://npm.io/package/@triadica/lagopus

## Dependencies (3)

- [ismobilejs](https://npm.io/package/ismobilejs.md) ^1.1.1
- [query-string](https://npm.io/package/query-string.md) ^8.1.0
- [@triadica/touch-control](https://npm.io/package/@triadica/touch-control.md) ^0.0.4-a1

## Recent versions

- 0.2.0 (latest) — 2024-11-19
- 0.1.4 — 2024-11-18
- 0.1.3 — 2024-11-17
- 0.1.2 — 2024-11-16
- 0.1.1 — 2024-11-16
- 0.0.23 — 2024-04-15
- 0.0.22 — 2024-04-14
- 0.0.21 — 2024-04-10
- 0.0.20 — 2024-02-29
- 0.0.19 — 2023-12-16
- 0.0.18 — 2023-10-03
- 0.0.17 — 2023-07-04
- 0.0.16 — 2023-06-23
- 0.0.16-a1 — 2023-06-22
- 0.0.15 — 2023-06-18
- … 25 more at https://npm.io/package/@triadica/lagopus/versions

## README

## WebGPU tiny example

Based on:

- https://medium.com/@carmencincotti/drawing-a-triangle-with-webgpu-53d48fb1ba8
- https://codepen.io/alaingalvan/pen/GRgvLGw

> WebGPU support has landed on latest Chrome.

### Usage

> Read full example in https://github.com/Triadica/lagopus-ts-workflow .

Defining object:

```js
object({
  shader: triangleWgsl,
  topology: "triangle-list",
  attrsList: [
    { field: "position", format: "float32x4" },
    { field: "color", format: "float32x4" },
  ],
  // vertex params
  data: [
    { position: [120.0, 120.0, 30, 1], color: [1, 0, 0, 1] },
    { position: [128.0, 120.0, 30, 1], color: [1, 0, 0, 1] },
    { position: [120.0, 126.0, 38, 1], color: [1, 0, 0, 1] },
  ],
  hitRegion: {
    radius: 4,
    position: [124, 123, 34],
    onHit: (e, d) => {
      console.log("hit", e);
      d("hit", { x: e.clientX, y: e.clientY });
    },
  },
  // in @group(0) for uniforms
  getParams: () => {
    return [(Date.now() / 400) % 1, 0, 0, 0];
  },
  // in @group(1) for textures
  textures: [],
});
```

Shader:

```wgsl
struct UBO {
  cone_back_scale: f32,
  viewport_ratio: f32,
  look_distance: f32,
  forward: vec3f,
  // direction up overhead, better unit vector
  upward: vec3f,
  rightward: vec3f,
  camera_position: vec3f,
};

@group(0) @binding(0) var<uniform> uniforms: UBO;

// your custom params
struct Params {
  _pad: f32
}

@group(0) @binding(1) var<uniform> params Params;

// perspective

struct PointResult {
  pointPosition: vec3f,
  r: f32,
  s: f32,
};

fn transform_perspective(p: vec3f) -> PointResult {
  let forward = uniforms.forward;
  let upward = uniforms.upward;
  let rightward = uniforms.rightward;
  let look_distance = uniforms.look_distance;
  let camera_position = uniforms.camera_position;

  let moved_point: vec3f = p - camera_position;

  let s: f32 = uniforms.cone_back_scale;

  let r: f32 = dot(moved_point, forward) / look_distance;

  // if (r < (s * -0.9)) {
  //   // make it disappear with depth test since it's probably behind the camera
  //   return PointResult(vec3(0.0, 0.0, 10000.), r, s);
  // }

  let screen_scale: f32 = (s + 1.0) / (r + s);
  let y_next: f32 = dot(moved_point, upward) * screen_scale;
  let x_next: f32 = dot(moved_point, rightward) * screen_scale;
  let z_next: f32 = r;

  return PointResult(
    vec3(x_next, y_next / uniforms.viewport_ratio, z_next),
    r, s
  );
}

// main

struct VertexOut {
  @builtin(position) position : vec4f,
  @location(0) color : vec4f,
};

@vertex
fn vertex_main(
  @location(0) position: vec4f,
  @location(1) color: vec4f
) -> VertexOut {
  var output: VertexOut;
  let p = transform_perspective(position.xyz).pointPosition;
  let scale: f32 = 0.002;
  output.position = vec4(p[0]*scale, p[1]*scale, p[2]*scale, 1.0);
  // output.position = position;
  output.color = color;
  return output;
}

@fragment
fn fragment_main(vtx_out: VertexOut) -> @location(0) vec4f {
  return vtx_out.color;
  // return vec4f(0.0, 0.0, 1.0, 1.0);
}
```

Uniforms is passed from `params`, [whose layout is quite tricky](https://www.w3.org/TR/WGSL/#structure-member-layout).

### License

MIT

---
_Source: https://npm.io/package/@triadica/lagopus · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
