index.js 2.24 KB
Newer Older
1
import invariant from "invariant";
2
import { Shaders, runtime } from "gl-react";
3 4 5
import isAnimated from "gl-react/src/isAnimated";
import makeSurface from "./makeSurface";
import GLCanvas from "./GLCanvas";
6 7 8
import {NativeModules, View, Animated, Image} from "react-native";
import resolveAssetSource from "react-native/Libraries/Image/resolveAssetSource";

9
const {RNGLContext} = NativeModules;
10 11 12 13 14
invariant(RNGLContext,
`gl-react-native: the native module is not available.
Make sure you have properly configured it.
See README install instructions.

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
15
NativeModules.RNGLContext is %s`, RNGLContext);
16 17

// Hook Shaders to RNGLContext
18 19 20 21 22 23 24 25 26
Shaders.setImplementation({
  add: (id, shader) =>
  new Promise((resolve, reject) =>
    RNGLContext.addShader(id, shader, (error, result) => {
      if (error) reject(error);
      else resolve(result);
    })),
  remove: id => RNGLContext.removeShader(id)
});
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
if (__DEV__) {
  runtime.decorateVDOMContent = vdom => {
    if (vdom && vdom.type === Image && !vdom.props.glReactUseImage) {
      console.warn(
`gl-react: Found a ReactNative.Image element. This is not performant. Try one of these:
- pass-in directly the image URL in your uniforms.
- use gl-react-image which implements the same Image API directly in OpenGL. https://github.com/gre/gl-react-image
- If you need more features like padding, explicitly setting image size, you can implement your own shader.

If you still want to do this, add a glReactUseImage prop to the Image to disable this warning.
`);
    }
    return vdom;
  };
}

44
module.exports = {
45
  resolveAssetSource,
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  Surface: makeSurface({
    View,
    GLCanvas,
    dimensionInvariant: (value, field) =>
      isAnimated(value)
      ? invariant(false, "GL.Surface "+field+" prop cannot be an Animated object. Use GL.AnimatedSurface instead")
      : invariant(typeof value === "number" && value > 0, "GL.Surface: "+field+" prop must be a strictly positive number")
  }),
  AnimatedSurface: makeSurface({
    View: Animated.View,
    GLCanvas: Animated.createAnimatedComponent(GLCanvas),
    dimensionInvariant: (value, field) =>
      invariant(
        isAnimated(value) || typeof value === "number" && value > 0,
        "GL.AnimatedSurface: "+field+" must be a strictly positive number OR an Animated object"
      )
  }),
63
};