-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from PatriceVignola/add-android-support
✨ Add Android support - with huge kudos @PatriceVignola
- Loading branch information
Showing
45 changed files
with
1,751 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
buildscript { | ||
repositories { | ||
jcenter() | ||
} | ||
dependencies { | ||
classpath 'com.android.tools.build:gradle:1.3.1' | ||
} | ||
} | ||
|
||
apply plugin: 'com.android.library' | ||
|
||
android { | ||
compileSdkVersion 23 | ||
buildToolsVersion "23.0.1" | ||
|
||
defaultConfig { | ||
minSdkVersion 16 | ||
targetSdkVersion 22 | ||
versionCode 1 | ||
versionName "1.0" | ||
} | ||
lintOptions { | ||
abortOnError false | ||
} | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
compile 'com.facebook.react:react-native:+' | ||
compile fileTree(dir: "libs", include: ["*.jar"]) | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.rnglmodelview"> | ||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.rnglmodelview; | ||
|
||
import com.facebook.react.ReactActivity; | ||
|
||
public class MainActivity extends ReactActivity { | ||
|
||
/** | ||
* Returns the name of the main component registered from JavaScript. | ||
* This is used to schedule rendering of the component. | ||
*/ | ||
@Override | ||
protected String getMainComponentName() { | ||
return "RNGLModelView"; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
android/src/main/java/com/rnglmodelview/MainApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.rnglmodelview; | ||
|
||
import android.app.Application; | ||
|
||
import com.facebook.react.ReactApplication; | ||
import com.facebook.react.ReactNativeHost; | ||
import com.facebook.react.ReactPackage; | ||
import com.facebook.react.shell.MainReactPackage; | ||
import com.facebook.soloader.SoLoader; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class MainApplication extends Application implements ReactApplication { | ||
|
||
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) { | ||
@Override | ||
public boolean getUseDeveloperSupport() { | ||
return BuildConfig.DEBUG; | ||
} | ||
|
||
@Override | ||
protected List<ReactPackage> getPackages() { | ||
return Arrays.<ReactPackage>asList( | ||
new MainReactPackage(), | ||
new RNGLModelViewPackage() | ||
); | ||
} | ||
|
||
@Override | ||
protected String getJSMainModuleName() { | ||
return "index"; | ||
} | ||
}; | ||
|
||
@Override | ||
public ReactNativeHost getReactNativeHost() { | ||
return mReactNativeHost; | ||
} | ||
|
||
@Override | ||
public void onCreate() { | ||
super.onCreate(); | ||
SoLoader.init(this, /* native exopackage */ false); | ||
} | ||
} |
184 changes: 184 additions & 0 deletions
184
android/src/main/java/com/rnglmodelview/RNGLModelView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
package com.rnglmodelview; | ||
|
||
import android.content.Context; | ||
import android.opengl.GLSurfaceView; | ||
|
||
import com.rnglmodelview.exceptions.ModelObjectNotSupportedException; | ||
import com.threed.jpct.Loader; | ||
import com.threed.jpct.Matrix; | ||
import com.threed.jpct.Object3D; | ||
import com.threed.jpct.Texture; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
public class RNGLModelView extends GLSurfaceView { | ||
|
||
private RNGLModelViewRenderer mRenderer; | ||
|
||
private Object3D mModel; | ||
|
||
private float mModelRotateX = 0; | ||
private float mModelRotateY = 0; | ||
private float mModelRotateZ = 0; | ||
private float mModelTranslateX = 0; | ||
private float mModelTranslateY = 0; | ||
private float mModelTranslateZ = 0; | ||
private float mModelScaleX = 1; | ||
private float mModelScaleY = 1; | ||
private float mModelScaleZ = 1; | ||
|
||
public RNGLModelView(Context context) { | ||
super(context); | ||
setEGLContextClientVersion(2); | ||
|
||
mRenderer = new RNGLModelViewRenderer(context); | ||
setRenderer(mRenderer); | ||
} | ||
|
||
public void setModel(String modelFileName) { | ||
mModel = loadModel(modelFileName); | ||
|
||
// In jpct, the coordinate system is rotated 180 degrees around x and Object3D forces the mesh | ||
// into that orientation. Since we want to keep the OpenGL-like coordinate system, we force the | ||
// mesh back into its previous rotation. | ||
mModel.rotateX((float)Math.PI); | ||
mModel.rotateMesh(); | ||
mModel.clearRotation(); | ||
|
||
mRenderer.setModel(mModel); | ||
updateModelTransform(); | ||
} | ||
|
||
public void setModelTexture(@Nullable String textureFileName) { | ||
mRenderer.setTexture(loadTexture(textureFileName)); | ||
} | ||
|
||
public void setAnimate(@Nullable boolean animate) { | ||
mRenderer.setAnimate(animate); | ||
} | ||
|
||
public void setModelRotateX(@Nullable float rotateX) { | ||
mModelRotateX = rotateX; | ||
updateModelTransform(); | ||
} | ||
|
||
public void setModelRotateY(@Nullable float rotateY) { | ||
mModelRotateY = rotateY; | ||
updateModelTransform(); | ||
} | ||
|
||
public void setModelRotateZ(@Nullable float rotateZ) { | ||
mModelRotateZ = rotateZ; | ||
updateModelTransform(); | ||
} | ||
|
||
public void setModelScale(@Nullable float scale) { | ||
mModelScaleX = scale; | ||
mModelScaleY = scale; | ||
mModelScaleZ = scale; | ||
updateModelTransform(); | ||
} | ||
|
||
public void setModelScaleX(@Nullable float scaleX) { | ||
mModelScaleX = scaleX; | ||
updateModelTransform(); | ||
} | ||
|
||
public void setModelScaleY(@Nullable float scaleY) { | ||
mModelScaleY = scaleY; | ||
updateModelTransform(); | ||
} | ||
|
||
public void setModelScaleZ(@Nullable float scaleZ) { | ||
mModelScaleZ = scaleZ; | ||
updateModelTransform(); | ||
} | ||
|
||
public void setModelTranslateX(@Nullable float translateX) { | ||
mModelTranslateX = translateX; | ||
updateModelTransform(); | ||
} | ||
|
||
public void setModelTranslateY(@Nullable float translateY) { | ||
mModelTranslateY = translateY; | ||
updateModelTransform(); | ||
} | ||
|
||
public void setModelTranslateZ(@Nullable float translateZ) { | ||
mModelTranslateZ = translateZ; | ||
updateModelTransform(); | ||
} | ||
|
||
private Object3D loadModel(String modelFileName) { | ||
String modelFileNameArray[] = modelFileName.split("\\."); | ||
String extension = modelFileNameArray[modelFileNameArray.length - 1].toLowerCase(); | ||
|
||
Object3D model = null; | ||
|
||
try { | ||
InputStream modelStream = getContext().getAssets().open(modelFileName); | ||
|
||
switch (extension) { | ||
case "obj": | ||
model = Object3D.mergeAll(Loader.loadOBJ(modelStream, null, 1)); | ||
break; | ||
case "3ds": | ||
model = Object3D.mergeAll(Loader.load3DS(modelStream, 1)); | ||
break; | ||
case "md2": | ||
model = Loader.loadMD2(modelStream, 1); | ||
break; | ||
case "asc": | ||
model = Loader.loadASC(modelStream, 1, false); | ||
break; | ||
case "model": | ||
model = RNGLModelViewModelLoader.loadMODEL(modelStream); | ||
break; | ||
} | ||
} catch (IOException | ModelObjectNotSupportedException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return model; | ||
} | ||
|
||
private Texture loadTexture(String textureFileName) { | ||
Texture texture = null; | ||
|
||
try { | ||
InputStream textureStream = getContext().getAssets().open(textureFileName); | ||
texture = new Texture(textureStream); | ||
|
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return texture; | ||
} | ||
|
||
private void updateModelTransform() { | ||
if (mModel != null) { | ||
Matrix rotationMatrix = new Matrix(); | ||
|
||
// First, we scale the identity matrix | ||
rotationMatrix.setRow(0, mModelScaleX, 0, 0, 0); | ||
rotationMatrix.setRow(1, 0, mModelScaleY, 0, 0); | ||
rotationMatrix.setRow(2, 0, 0, mModelScaleZ, 0); | ||
|
||
// Second, we rotate the scaled matrix | ||
rotationMatrix.rotateZ((float)Math.toRadians(mModelRotateZ)); | ||
rotationMatrix.rotateY((float)Math.toRadians(mModelRotateY)); | ||
rotationMatrix.rotateX((float)Math.toRadians(mModelRotateX)); | ||
|
||
// Finally, we create the translation matrix | ||
Matrix translatonMatrix = new Matrix(); | ||
translatonMatrix.translate(mModelTranslateX, mModelTranslateY, mModelTranslateZ); | ||
|
||
mModel.setTranslationMatrix(translatonMatrix); | ||
mModel.setRotationMatrix(rotationMatrix); | ||
} | ||
} | ||
} |
Oops, something went wrong.