1
1
import * as React from 'react'
2
- import { useCallback , useRef } from 'react'
3
2
4
3
import { Button , StyleSheet , View } from 'react-native'
5
- import {
6
- FilamentView ,
7
- Float3 ,
8
- useRenderCallback ,
9
- useBuffer ,
10
- useModel ,
11
- FilamentProvider ,
12
- useFilamentContext ,
13
- useWorkletCallback ,
14
- } from 'react-native-filament'
15
- import { getAssetPath } from './utils/getAssetPasth'
16
- import { useDefaultLight } from './hooks/useDefaultLight'
4
+ import { FilamentView , useBuffer , useModel , useFilamentContext , useWorkletCallback , Camera , FilamentContext } from 'react-native-filament'
17
5
18
- const penguModelPath = getAssetPath ( 'pengu.glb' )
19
- const leftEyeTexturePath = getAssetPath ( 'eye_full_texture_left_blue.jpg' )
20
- const rightEyeTexturePath = getAssetPath ( 'eye_full_texture_right_blue.jpg' )
21
-
22
- // Camera config:
23
- const cameraPosition : Float3 = [ 0 , 0 , 8 ]
24
- const cameraTarget : Float3 = [ 0 , 0 , 0 ]
25
- const cameraUp : Float3 = [ 0 , 1 , 0 ]
26
- const focalLengthInMillimeters = 28
27
- const near = 0.1
28
- const far = 1000
6
+ import PenguModel from '~/assets/pengu.glb'
7
+ import LeftEyeTexture from '~/assets/eye_full_texture_left_blue.jpg'
8
+ import RightEyeTexture from '~/assets/eye_full_texture_right_blue.jpg'
9
+ import { DefaultLight } from './components/DefaultLight'
29
10
30
11
function Renderer ( ) {
31
- const { camera, view, renderableManager } = useFilamentContext ( )
32
-
33
- useDefaultLight ( )
12
+ const { renderableManager } = useFilamentContext ( )
34
13
35
- const pengu = useModel ( { source : penguModelPath } )
36
- const blueLeftEyeBuffer = useBuffer ( { source : leftEyeTexturePath } )
37
- const blueRightEyeBuffer = useBuffer ( { source : rightEyeTexturePath } )
14
+ const pengu = useModel ( PenguModel )
15
+ const blueLeftEyeBuffer = useBuffer ( { source : LeftEyeTexture } )
16
+ const blueRightEyeBuffer = useBuffer ( { source : RightEyeTexture } )
38
17
39
18
const penguAsset = pengu . state === 'loaded' ? pengu . asset : undefined
40
19
const changeEyes = useWorkletCallback ( ( ) => {
@@ -59,24 +38,12 @@ function Renderer() {
59
38
renderableManager . changeMaterialTextureMap ( rightEye , 'Eye_Right.002' , blueRightEyeBuffer , 'sRGB' )
60
39
} )
61
40
62
- const prevAspectRatio = useRef ( 0 )
63
- useRenderCallback (
64
- useCallback ( ( ) => {
65
- 'worklet'
66
- const aspectRatio = view . getAspectRatio ( )
67
- if ( prevAspectRatio . current !== aspectRatio ) {
68
- prevAspectRatio . current = aspectRatio
69
- // Setup camera lens:
70
- camera . setLensProjection ( focalLengthInMillimeters , aspectRatio , near , far )
71
- }
72
-
73
- camera . lookAt ( cameraPosition , cameraTarget , cameraUp )
74
- } , [ view , camera , prevAspectRatio ] )
75
- )
76
-
77
41
return (
78
42
< View style = { styles . container } >
79
- < FilamentView style = { styles . filamentView } />
43
+ < FilamentView style = { styles . filamentView } >
44
+ < Camera />
45
+ < DefaultLight />
46
+ </ FilamentView >
80
47
< Button
81
48
title = "Change Eyes"
82
49
onPress = { ( ) => {
@@ -89,9 +56,9 @@ function Renderer() {
89
56
90
57
export function ChangeMaterials ( ) {
91
58
return (
92
- < FilamentProvider >
59
+ < FilamentContext >
93
60
< Renderer />
94
- </ FilamentProvider >
61
+ </ FilamentContext >
95
62
)
96
63
}
97
64
0 commit comments