Play With 3D Models
Now that you've learned how to apply the material to the VR app with GearVR Framework, we are going to learn how to play with 3D model.
Create a GearVR Framework project by copying the template project
Perform the following steps to make sure your project runs correctly
- (if developing for Gear VR) Copy your Oculus signature file to
- Change the
build.gradleto a unique name to avoid naming conflict when you test the app later
- Change the
res/values/strings.xmlto avoid confusion when you debug the app.
Often times when we develop VR applications, we need to show complex 3D models. For example a plane, a castle or even an earth. We're going to learn how to achieve it with GearVR Framework
You can use your own 3D models, just make sure it is one of the following file formats:
- All formats supported by Assimp
How to load 3D models¶
The first step is to place it correctly. Please make sure to copy the files to the following path.
After copying the 3D model files, we can use
GVRAssetLoader class to load them, it is accessible from the context by calling
Using the following code to load the fbx file and texture
GVRMesh dinoMesh = gvrContext.getAssetLoader().loadMesh( new GVRAndroidResource(gvrContext, "trex_mesh.fbx") ); GVRTexture dinoTexture = gvrContext.getAssetLoader().loadTexture( new GVRAndroidResource(gvrContext, "trex_tex_diffuse.png") );
Loading methods have
Future in its API such as
loadFutureTexture is deprecated
After 3D model and texture both loaded, we can add them to the scene with a scene object
GVRSceneObject dinoObj = new GVRSceneObject(gvrContext, dinoMesh, dinoTexture); dinoObj.getTransform().setPosition(0,0,-10); dinoObj.getTransform().rotateByAxis(-90, 1f, 0f, 0f); gvrContext.getMainScene().addSceneObject(dinoObj);
GVRSceneObject instead of use
loadModel() require fbx files to have correct path to texture file which a lot of 3D modeling software failed to produce.
Build and run the app, you should be able to see a T-Rex!
Work with 3D modeling tools¶
Fbx is the recommended format for the GearVR framework. Currently, all major 3D modeling tools support exporting to FBX format.
Complete Source Code for this sample