View.md 2.11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
# GL.View

`GL.View` is a React Component that renders a given shader with uniforms (parameters to send to the shader).

**Quick Examples:**

Renders a "standalone" shader:

```js
render () {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
11 12 13 14
  return <GL.View
    shader={shaders.myEffect}
    width={200}
    height={100} />;
15 16 17 18 19 20 21
}
```

Renders a shader with uniform parameters:

```js
render () {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
22 23 24 25
  return <GL.View
    shader={shaders.myEffect2}
    width={200}
    height={100}
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    uniforms={{
      floatValue: 0.5,
      vec3Value: [ 1, 0.5, 0.5 ]
    }} />;
    /*
    // in myEffect2:
    uniform float floatValue;
    uniform vec3 vec3Value;
    */
}
```

Renders a shader with an image (texture):

```js
render () {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
42 43 44 45
  return <GL.View
    shader={shaders.myEffect3}
    width={200}
    height={100}
46 47 48 49 50 51 52 53 54 55 56 57 58
    uniforms={{
      textureName: {{ uri: "...url" }} // RN convention
    }} />;
    /*
    // in myEffect3:
    uniform sampler2D textureName;
    */
}
```


## Props

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
59 60 61 62
- **`shader`** *(id created by GL.Shaders.create)* **(required)**: The shader to use for rendering the `GL.View`.
- **`width`** and **`height`** *(Number)* **(required)**: the size of the view.
- **`uniforms`** *(object)*: an object that contains all uniform parameters to send to the shader. The key is the uniform name and the value is whatever value that makes sense for the uniform's type (see below).
- **`opaque`** *(bool)*: specify if the view should be opaque. By default, it is true, meaning that the GL View won't support texture opacity and alpha channel.
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
63
- **...any other props** get directly passed to the underlying view.
64 65 66 67 68 69 70 71 72 73 74 75 76 77

## Uniform types

Here is the correspondance of GLSL and JavaScript types.

- `int`, `float`, `bool` : Number (e.g: `42`).
- `sampler2D` : an object with an `uri` (`require("image!id")` is also supported), exactly like the `source` prop of `React.Image`.
- `vecN`,`ivecN`,`bvecN` where N is {2,3,4} : an array of N Number (e.g: `[1, 2, 3.5]` for a `vec3`)

Complex struct types and uniform array **are not** currently supported.

## Note on textures

Images given to uniforms props are always loaded as they are. If you want resize/crop features, you can use an `React.Image` inside a `GL.Target`.