11import * as React from 'react'
2- import { useCallback , useRef } from 'react'
32
43import { 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'
175
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'
2910
3011function Renderer ( ) {
31- const { camera, view, renderableManager } = useFilamentContext ( )
32-
33- useDefaultLight ( )
12+ const { renderableManager } = useFilamentContext ( )
3413
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 } )
3817
3918 const penguAsset = pengu . state === 'loaded' ? pengu . asset : undefined
4019 const changeEyes = useWorkletCallback ( ( ) => {
@@ -59,24 +38,12 @@ function Renderer() {
5938 renderableManager . changeMaterialTextureMap ( rightEye , 'Eye_Right.002' , blueRightEyeBuffer , 'sRGB' )
6039 } )
6140
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-
7741 return (
7842 < View style = { styles . container } >
79- < FilamentView style = { styles . filamentView } />
43+ < FilamentView style = { styles . filamentView } >
44+ < Camera />
45+ < DefaultLight />
46+ </ FilamentView >
8047 < Button
8148 title = "Change Eyes"
8249 onPress = { ( ) => {
@@ -89,9 +56,9 @@ function Renderer() {
8956
9057export function ChangeMaterials ( ) {
9158 return (
92- < FilamentProvider >
59+ < FilamentContext >
9360 < Renderer />
94- </ FilamentProvider >
61+ </ FilamentContext >
9562 )
9663}
9764
0 commit comments