一、在專案中的 gradle 中加入 Rajawali SDK
- 在 build.gradle (Module : app) 內的 dependencies {...} 或在 Project Structure - Dependencies 中加入:
compile 'org.rajawali3d:rajawali:1.0.186-SNAPSHOT@aar' - 在 build.gradle (Project: yourProjectName) 內的 allprojects {repositories {...}} 中加入:
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
二、建立 RajawaliRenderer 渲染器
- 在專案中加入繼承 RajawaliRenderer 的 Class
public class mRender extends RajawaliRenderer {
public mRender(Context context) {
super(context);
}
@Override
protected void initScene() {
}
@Override
public void onOffsetsChanged(float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset) {
}
@Override
public void onTouchEvent(MotionEvent event) {
}
}
- 在 mRender() 結構涵式中設定 FrameRate
public Renderer(Context context) {
super(context);
this.context = context;
setFrameRate(60);
}
- 在 initScene() 中加入之後想要建立的物件 :
Sphere - 球體物件、
DirectionalLight - 定向光、
DebugVisualizer - debug 隔線、
Object3D - 3D 物件,可透過 LoaderOBJ、LoaderAWD、Loader3DSMax 等 Loader 來讀取模型資源檔、
Camera 相機、
...
而在 earthSphere 範例中所使用:
/** 設定燈光 */
directionalLight = new DirectionalLight(1f, .2f, -1.0f);
directionalLight.setColor(1.0f, 1.0f, 1.0f);
directionalLight.setPower(2);
getCurrentScene().addLight(directionalLight);
/** 設定材質 */
Material material = new Material();
material.enableLighting(true);
material.setDiffuseMethod(new DiffuseMethod.Lambert());
material.setColor(0);
Texture earthTexture = new Texture("Earth", R.drawable.earthtruecolor_nasa_big);
try{
material.addTexture(earthTexture);
} catch (ATexture.TextureException error){
Log.d("DEBUG", "TEXTURE ERROR");
}
/** 設定地球球體 */
earthSphere = new Sphere(1, 24, 24);
earthSphere.setMaterial(material);
getCurrentScene().addChild(earthSphere);
getCurrentCamera().setZ(4.2f);
- 透過 Override - onRender() 方法,來更新渲染的物件。
在 earthSphere 範例中所使用:
@Override
public void onRender(final long elapsedTime, final double deltaTime) {
super.onRender(elapsedTime, deltaTime);
earthSphere.rotate(Vector3.Axis.Y, 1.0);
}
public class mRender extends RajawaliRenderer {
public mRender(Context context) {
super(context);
super(context);
}
@Override
protected void initScene() {
}
@Override
public void onOffsetsChanged(float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset) {
}
@Override
public void onTouchEvent(MotionEvent event) {
}
}
@Override
protected void initScene() {
}
@Override
public void onOffsetsChanged(float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset) {
}
@Override
public void onTouchEvent(MotionEvent event) {
}
}
public Renderer(Context context) {
super(context);
this.context = context;
setFrameRate(60);
}
Sphere - 球體物件、
DirectionalLight - 定向光、
DebugVisualizer - debug 隔線、
Object3D - 3D 物件,可透過 LoaderOBJ、LoaderAWD、Loader3DSMax 等 Loader 來讀取模型資源檔、
Camera 相機、
...
而在 earthSphere 範例中所使用:
/** 設定燈光 */
directionalLight = new DirectionalLight(1f, .2f, -1.0f);
directionalLight.setColor(1.0f, 1.0f, 1.0f);
directionalLight.setPower(2);
getCurrentScene().addLight(directionalLight);
/** 設定材質 */
Material material = new Material();
material.enableLighting(true);
material.setDiffuseMethod(new DiffuseMethod.Lambert());
material.setColor(0);
Texture earthTexture = new Texture("Earth", R.drawable.earthtruecolor_nasa_big);
try{
material.addTexture(earthTexture);
} catch (ATexture.TextureException error){
Log.d("DEBUG", "TEXTURE ERROR");
}
/** 設定地球球體 */
earthSphere = new Sphere(1, 24, 24);
earthSphere.setMaterial(material);
getCurrentScene().addChild(earthSphere);
getCurrentCamera().setZ(4.2f);
在 earthSphere 範例中所使用:
@Override
public void onRender(final long elapsedTime, final double deltaTime) {
super.onRender(elapsedTime, deltaTime);
earthSphere.rotate(Vector3.Axis.Y, 1.0);
}
三、在 MainActivity 的 onCreate() 中加載 RajawaliSurfaceView 和設定 Surface 的 Render
在 earthSphere 範例中所使用:
mRender renderer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RajawaliSurfaceView surface = new RajawaliSurfaceView(this);
surface.setFrameRate(60.0);
surface.setRenderMode(IRajawaliSurface.RENDERMODE_WHEN_DIRTY);
// Add mSurface to your root view
addContentView(surface,
new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT));
new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT));
renderer = new mRender(this);
surface.setSurfaceRenderer(renderer);
參考網址:
- Basic Rajawali3D Tutorial for Android
- Rajawali3D基础教程-一个地球旋转的例子
- GitHub - Rajawali/RajawaliExamples: Rajawali 3D Engine ...
- GitHub - Rajawali/Rajawali
- earthtruecolor_nasa_big Image