Skip to content

Commit

Permalink
fix: ability to change texture
Browse files Browse the repository at this point in the history
  • Loading branch information
ridvanaltun committed Jun 22, 2022
1 parent 18c1ae8 commit 5e00416
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 39 deletions.
15 changes: 5 additions & 10 deletions README.md
Expand Up @@ -240,12 +240,7 @@ For more details about changeParameter API read our article [here](https://help.

**Change Float:**

This method allows the user to change float parameters during runtime. The method requires a Game Object name, Component name, Parameter name, and a float value.

- `gameObject` is the name of the Game Object in the hierarchy as visible in the Studio. The name should be unique for the method to work properly.
- `component` is the internal component type.
- `parameter` is the name or the parameter to be changed, for example, the name of the blendshape on a mesh. The parameter must exist for the component.
- `value` is the new value to be applied to the parameter.
This method allows the user to change the value of blendshape parameters during runtime.

```tsx
import type {IChangeParamaterFloat} from 'react-native-deepar';
Expand All @@ -255,7 +250,7 @@ deepARRef?.current?.changeParameterFloat(params: IChangeParamaterFloat) => void;

**Change Vector4:**

Same as above except the method takes four float values which represent one Vector4. Can be used to change material color. If multiple Game Objects share the same material, changing the parameter once for any Game Object using the material will change it for all Game Objects. To change a uniform on a material, such as color, the parameter must use the internal uniform name. These names can be found in the shader files.
This method is used to change the certain color of a Game Object at runtime.

```tsx
import type {IChangeParamaterVec4} from 'react-native-deepar';
Expand All @@ -265,7 +260,7 @@ deepARRef?.current?.changeParameterVec4(params: IChangeParamaterVec4) => void;

**Change Vector3:**

Same as above except the method takes three float values which represent one Vector3. Can be used to change the transform of a Game Object at runtime. Rotations should be written as Euler angles in degrees. To set a parameter on GameObject, the component parameter should be an empty string.
This method is used to change the transform of a Game Object at runtime, so here you can change the object position, rotation or scale.

```tsx
import type {IChangeParamaterVec3} from 'react-native-deepar';
Expand All @@ -275,7 +270,7 @@ deepARRef?.current?.changeParameterVec3(params: IChangeParamaterVec3) => void;

**Change Boolean:**

Same as above except val takes a bool value. Can be used to disable or enable GameObjects at runtime. To set a parameter on GameObject, the component parameter should be an empty string.
Let say you want to put a button in your app that enables or disables Game Object at runtime. (let's say you want your filter character to put their glasses on or take them off) This parameter helps you to enable/disable the value.Here is the code to apply this parameter:

```tsx
import type {IChangeParamaterBool} from 'react-native-deepar';
Expand All @@ -297,7 +292,7 @@ deepARRef?.current?.changeParameterString(params: IChangeParamaterString) => voi

**Change Texture:**

This method allows the user to change the texture at runtime. The component should be "MeshRenderer". The parameter is the name of the texture that should be changed, this name can be found in the shader that is applied to the material/GameObject.
This method allows the user to load an image and set it as a texture during runtime. This can be useful if you want to leverage our background segmentation feature, and change the background in your filter.

```tsx
import type {IChangeParamaterTexture} from 'react-native-deepar';
Expand Down
@@ -1,10 +1,8 @@
package com.reactnativedeepar;

import android.content.ContentResolver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Base64;
import android.widget.FrameLayout;

import com.facebook.infer.annotation.Assertions;
Expand All @@ -16,7 +14,6 @@
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.annotations.ReactProp;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -245,27 +242,31 @@ public void receiveCommand(RNTDeepAR deepARView, int commandId, @Nullable Readab
String parameter = args.getString(2);
String value = args.getString(3);
String type = args.getString(4);
Bitmap image = null;

try {
switch (type) {
case "URL":
URL url = new URL(value);
image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
deepARView.changeParameterTexture(gameObject, component, parameter, image);
break;
case "URI":
ContentResolver contentResolver = context.getContentResolver();
Uri imageUri = Uri.parse(value);
image = MediaStore.Images.Media.getBitmap(contentResolver, imageUri);
break;
case "PATH":
image = BitmapFactory.decodeFile(value);
break;
}
deepARView.changeParameterTexture(gameObject, component, parameter, image);
} catch(IOException e) {
System.out.println(e);
switch (type) {
case "URL":
new Thread(new Runnable(){
@Override
public void run() {
try {
URL url = new URL(value);
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
deepARView.changeParameterTexture(gameObject, component, parameter, image);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}).start();
break;
case "BASE64":
byte[] decodedString = Base64.decode(value, Base64.DEFAULT);
Bitmap image = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
deepARView.changeParameterTexture(gameObject, component, parameter, image);
break;
case "PATH":
Bitmap pathImage = BitmapFactory.decodeFile(value);
deepARView.changeParameterTexture(gameObject, component, parameter, pathImage);
break;
}
}
return;
Expand Down
32 changes: 31 additions & 1 deletion example/src/screens/HomeScreen.tsx
Expand Up @@ -6,8 +6,12 @@ import {
Image,
Platform,
StyleSheet,
Alert,
} from 'react-native';
import DeepARView, {IDeepARHandle} from 'react-native-deepar';
import DeepARView, {
IDeepARHandle,
TextureSourceTypes,
} from 'react-native-deepar';
import RNFetchBlob from 'rn-fetch-blob';

import {Button} from '../components';
Expand Down Expand Up @@ -157,6 +161,32 @@ const HomeScreen = ({navigation}: {navigation: any}) => {
setVideoMode(true);
}}
/>
<Button
style={styles.upLeftButton}
text="Random Background Image"
onPress={() => {
const isBackgroundSegmantation =
Effects[currEffectIndex].name === 'background_segmentation';

if (isBackgroundSegmantation === false) {
return Alert.alert(
'Open Background Segmentation Effect First!'
);
}

RNFetchBlob.config({})
.fetch('GET', 'https://random.imagecdn.app/450/800')
.then((res) => {
deepARRef?.current?.changeParameterTexture({
gameObject: 'Background',
component: 'MeshRenderer',
parameter: 's_texColor',
type: TextureSourceTypes.BASE64,
value: res.base64(),
});
});
}}
/>
</View>
<Button
style={styles.switchCameraButton}
Expand Down
2 changes: 1 addition & 1 deletion ios/RNTDeepARViewManager.m
Expand Up @@ -385,7 +385,7 @@ - (UIView *)view {
[view changeParameterTexture:gameObject component:component parameter:parameter image:image];
}

if([type isEqual: @"URI"]) {
if([type isEqual: @"BASE64"]) {
NSData *data = [[NSData alloc]initWithBase64EncodedString:value options:NSDataBase64DecodingIgnoreUnknownCharacters];
UIImage *image = [UIImage imageWithData:data];

Expand Down
2 changes: 1 addition & 1 deletion src/DeepARView.tsx
Expand Up @@ -211,7 +211,7 @@ const DeepARView = forwardRef<IDeepARHandle, IDeepARProps>(
UIManager.dispatchViewManagerCommand(
findNodeHandle(nativeRef.current),
UIManager.getViewManagerConfig(NATIVE_VIEW_KEY).Commands
.changeParameterTextureWithPath,
.changeParameterTexture,
[gameObject, component, parameter, value, type]
);
},
Expand Down
2 changes: 1 addition & 1 deletion src/index.tsx
Expand Up @@ -8,7 +8,7 @@ export enum CameraFacing {
}

export enum TextureSourceTypes {
URI = 'URI',
BASE64 = 'BASE64',
URL = 'URL',
PATH = 'PATH',
}
Expand Down

0 comments on commit 5e00416

Please sign in to comment.