@@ -6,14 +6,19 @@ import THREE from "./three";
6
6
7
7
export default class App extends React . Component {
8
8
requestId : * ;
9
+ rotating = true ;
10
+ renderer ;
11
+ scene ;
12
+ camera ;
13
+ cube ;
14
+
9
15
componentWillUnmount ( ) {
10
16
cancelAnimationFrame ( this . requestId ) ;
11
17
}
12
- onContextCreate = ( gl : WebGLRenderingContext ) => {
13
- const rngl = gl . getExtension ( "RN" ) ;
14
18
19
+ onContextCreate = ( gl : WebGLRenderingContext ) => {
15
20
const { drawingBufferWidth : width , drawingBufferHeight : height } = gl ;
16
- const renderer = new THREE . WebGLRenderer ( {
21
+ this . renderer = new THREE . WebGLRenderer ( {
17
22
canvas : {
18
23
width,
19
24
height,
@@ -24,53 +29,62 @@ export default class App extends React.Component {
24
29
} ,
25
30
context : gl
26
31
} ) ;
27
- renderer . setSize ( width , height ) ;
28
- renderer . setClearColor ( 0x000000 , 1 ) ;
32
+ this . renderer . setSize ( width , height ) ;
33
+ this . renderer . setClearColor ( 0x000000 , 1 ) ;
29
34
30
- let camera , scene ;
31
- let cube ;
35
+ this . init ( width , height ) ;
36
+ // animate();
37
+ } ;
38
+ toggleRotation = ( ) => {
39
+ this . rotating = ! this . rotating ;
40
+ } ;
32
41
33
- function init ( ) {
34
- camera = new THREE . PerspectiveCamera ( 75 , width / height , 1 , 1100 ) ;
35
- camera . position . y = 150 ;
36
- camera . position . z = 500 ;
37
- scene = new THREE . Scene ( ) ;
42
+ init = ( width , height ) => {
43
+ this . camera = new THREE . PerspectiveCamera ( 75 , width / height , 1 , 1100 ) ;
44
+ this . camera . position . y = 150 ;
45
+ this . camera . position . z = 500 ;
46
+ this . scene = new THREE . Scene ( ) ;
38
47
39
- let geometry = new THREE . BoxGeometry ( 200 , 200 , 200 ) ;
40
- for ( let i = 0 ; i < geometry . faces . length ; i += 2 ) {
41
- let hex = Math . random ( ) * 0xffffff ;
42
- geometry . faces [ i ] . color . setHex ( hex ) ;
43
- geometry . faces [ i + 1 ] . color . setHex ( hex ) ;
44
- }
48
+ let geometry = new THREE . BoxGeometry ( 200 , 200 , 200 ) ;
49
+ for ( let i = 0 ; i < geometry . faces . length ; i += 2 ) {
50
+ let hex = Math . random ( ) * 0xffffff ;
51
+ geometry . faces [ i ] . color . setHex ( hex ) ;
52
+ geometry . faces [ i + 1 ] . color . setHex ( hex ) ;
53
+ }
45
54
46
- let material = new THREE . MeshBasicMaterial ( {
47
- vertexColors : THREE . FaceColors ,
48
- overdraw : 0.5
49
- } ) ;
55
+ let material = new THREE . MeshBasicMaterial ( {
56
+ vertexColors : THREE . FaceColors ,
57
+ overdraw : 0.5
58
+ } ) ;
50
59
51
- cube = new THREE . Mesh ( geometry , material ) ;
52
- cube . position . y = 150 ;
53
- scene . add ( cube ) ;
54
- }
55
- const animate = ( ) => {
56
- this . requestId = requestAnimationFrame ( animate ) ;
57
- renderer . render ( scene , camera ) ;
60
+ this . cube = new THREE . Mesh ( geometry , material ) ;
61
+ this . cube . position . y = 150 ;
62
+ this . scene . add ( this . cube ) ;
63
+ } ;
58
64
59
- cube . rotation . y += 0.05 ;
65
+ animate = ( ) => {
66
+ if ( this . renderer ) {
67
+ // this.requestId = requestAnimationFrame(animate);
68
+ this . renderer . render ( this . scene , this . camera ) ;
60
69
61
- gl . flush ( ) ;
62
- rngl . endFrame ( ) ;
63
- } ;
70
+ if ( this . rotating ) {
71
+ this . cube . rotation . y += 0.05 ;
72
+ }
64
73
65
- init ( ) ;
66
- animate ( ) ;
74
+ const rngl = this . renderer . context . getExtension ( "RN" ) ;
75
+ rngl . endFrame ( ) ;
76
+ }
67
77
} ;
78
+
68
79
render ( ) {
69
80
return (
70
- < View style = { styles . container } >
81
+ < View style = { styles . container } onTouchStart = { this . toggleRotation } >
71
82
< WebGLView
72
83
style = { styles . webglView }
73
84
onContextCreate = { this . onContextCreate }
85
+ onFrame = { ( ) => {
86
+ this . animate ( ) ;
87
+ } }
74
88
/>
75
89
</ View >
76
90
) ;
0 commit comments