This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
forked from rnc-archive/react-native-webgl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicTexture.js
84 lines (82 loc) · 2.29 KB
/
BasicTexture.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//@flow
import React from "react";
import { StyleSheet, Text, View } from "react-native";
import { WebGLView } from "react-native-webgl";
export default class App extends React.Component {
onContextCreate = (gl: WebGLRenderingContext) => {
const rngl = gl.getExtension("RN");
gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(
gl.ARRAY_BUFFER,
new Float32Array([-1, -1, -1, 4, 4, -1]),
gl.STATIC_DRAW
);
const vertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(
vertexShader,
`\
attribute vec2 p;
varying vec2 uv;
void main() {
gl_Position = vec4(p,0.0,1.0);
uv = 0.5 * (p+1.0);
}`
);
gl.compileShader(vertexShader);
const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(
fragmentShader,
`\
precision highp float;
varying vec2 uv;
uniform sampler2D t;
void main() {
gl_FragColor = mix(texture2D(t, uv), vec4(1.0), min(pow(2.*uv.x-1.,6.)+pow(2.*uv.y-1.,6.)+ 0.1 * step(fract(20.*uv.y+cos(16.*uv.x)), 0.2), 1.0));
}`
);
gl.compileShader(fragmentShader);
var program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
gl.useProgram(program);
var p = gl.getAttribLocation(program, "p");
gl.enableVertexAttribArray(p);
gl.vertexAttribPointer(p, 2, gl.FLOAT, false, 0, 0);
const tLocation = gl.getUniformLocation(program, "t");
rngl
.loadTexture({ image: "https://i.imgur.com/wxqlQkh.jpg", yflip: true })
.then(({ texture }) => {
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.uniform1i(tLocation, 0);
gl.drawArrays(gl.TRIANGLES, 0, 3);
gl.flush();
rngl.endFrame();
});
};
render() {
return (
<View style={styles.container}>
<WebGLView
style={styles.webglView}
onContextCreate={this.onContextCreate}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center"
},
webglView: {
width: 300,
height: 200
}
});