This repository has been archived by the owner on Dec 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
BasicTextureScene.js
134 lines (113 loc) · 3.42 KB
/
BasicTextureScene.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
'use strict';
import React from 'react';
import Exponent from 'exponent';
const vertSrc = `
precision highp float;
attribute vec2 position;
varying vec2 uv;
void main () {
uv = position;
gl_Position = vec4(1.0 - 2.0 * position, 0, 1);
}`;
const fragSrc = `
precision highp float;
uniform sampler2D texture;
varying vec2 uv;
void main () {
gl_FragColor = texture2D(texture, vec2(uv.x, uv.y));
}`;
export default class BasicScene extends React.Component {
static meta = {
description: 'Basic Scene with Texture',
};
constructor(props, context) {
super(props, context);
this.state = {
ready: false,
};
}
componentDidMount() {
(async () => {
this._textureAsset = Exponent.Asset.fromModule(
require('../Assets/avatar2.png'));
await this._textureAsset.downloadAsync();
this.setState({ ready: true });
})();
}
render() {
return this.state.ready ? (
<Exponent.GLView
style={this.props.style}
onContextCreate={this._onContextCreate}
/>
) : (
<Exponent.Components.AppLoading />
);
}
_onContextCreate = (gl) => {
gl.enableLogging = true;
// Compile vertex and fragment shader
const vert = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vert, vertSrc);
gl.compileShader(vert);
const frag = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(frag, fragSrc);
gl.compileShader(frag);
// Link together into a program
const program = gl.createProgram();
gl.attachShader(program, vert);
gl.attachShader(program, frag);
gl.linkProgram(program);
// Save position attribute
const positionAttrib = gl.getAttribLocation(program, 'position');
// Create buffer
const buffer = gl.createBuffer();
// Create texture
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA,
128, 128, 0,
gl.RGBA, gl.UNSIGNED_BYTE,
this._textureAsset);
// 1, 1, 0,
// gl.RGBA, gl.UNSIGNED_BYTE,
// new Uint8Array([255, 0, 0, 255]));
gl.uniform1i(gl.getUniformLocation(program, 'texture'), 0);
/// Animate!
let skip = false;
const animate = () => {
try {
if (skip) {
// return;
}
// Clear
gl.clearColor(0, 0, 1, 1);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// Bind texture
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture);
// Bind buffer, program and position attribute for use
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.useProgram(program);
gl.enableVertexAttribArray(positionAttrib);
gl.vertexAttribPointer(positionAttrib, 2, gl.FLOAT, false, 0, 0);
// Buffer data and draw!
const verts = new Float32Array([
-2, 0, 0, -2, 2, 2,
]);
gl.bufferData(gl.ARRAY_BUFFER, verts, gl.STATIC_DRAW);
gl.drawArrays(gl.TRIANGLES, 0, verts.length / 2);
// Submit frame
gl.flush();
gl.endFrameEXP();
} finally {
skip = !skip;
gl.enableLogging = false;
requestAnimationFrame(animate);
}
};
animate();
}
}