版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ZHANGKUN35268/article/details/47019043
这节讲 3D模型的背景透明。 先上效果图。
然后在加XML文件,相信大家看得懂---背景透明的效果
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.keyshare3d.MainActivity"
tools:ignore="MergeRootFrame" >
<FrameLayout
android:id="@+id/frame3d"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</FrameLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView\n\n\n\n\n\n\n\n\n\n\n这是透明背景\nXML中的分层显示\t \t \t PixelFormat.TRANSLUCENT\n_glSurfaceView.setZOrderOnTop(true);" />
</FrameLayout>
虽然只有一个TextView和 FrameLayout,但互相间的背景,并没有覆盖。
然后是MainActivty.java
ublic class MainActivity extends RendererActivity {
Object3dContainer faceObject3D; //3D对象
Object3dContainer faceObject_2; //3D对象
Object3dContainer _bg;
Screen screen; //= new Screen(getApplicationContext()); //摄像头对象,这里没什么用。本来是想背景关联照相机的。
Object3dContainer _plane;
Light _lightRed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screen = new Screen(getApplicationContext());//放在这里不崩溃
FrameLayout ll = (FrameLayout) this.findViewById(R.id.frame3d);//在xml中,指定显示的控件,这里是一个FrameLayout。
ll.addView(_glSurfaceView);//加入到_glSurfaceView中显示。
SurfaceHolder holder = null;
screen.surfaceCreated(holder);
Toast.makeText(MainActivity.this, "执行正常", Toast.LENGTH_SHORT).show();
}
public void initScene() {
_lightRed = new Light();
_lightRed.ambient.setAll(0x88110000);
_lightRed.diffuse.setAll(0xffff0000);
_lightRed.position.setZ(2);
_lightRed.position.setX(2);
_lightRed.type(LightType.POSITIONAL);
scene.lights().add(_lightRed);
scene.backgroundColor().setAll(0x00000000);
scene.lights().add(new Light()); //设置灯光
IParser myParser = Parser.createParser(Parser.Type.MAX_3DS,
getResources(), "com.example.keyshare3d:raw/ffffff", false);
myParser.parse(); //Start parsing the 3D object
faceObject3D = myParser.getParsedObject();
faceObject3D.scale().x = 0.007f;//模型大小缩放
faceObject3D.scale().y = 0.007f;
faceObject3D.scale().z = 0.007f;
faceObject3D.position().x =0;//设置初始位置,平截头体中xyz的坐标位置
faceObject3D.position().y =0;
faceObject3D.position().z =0;
scene.camera().target = faceObject3D.position();//设置镜头位置 -对准模型
Bitmap b = Utils.makeBitmapFromResourceId(this, R.drawable.sssss);
float w = 2f;
float h = w * (float)b.getHeight() / (float)b.getWidth();;
_plane = new Rectangle(w, h, 1,1, new Color4());
_plane.doubleSidedEnabled(true); // ... so that the back of the plane is visible
_plane.normalsEnabled(false);
scene.addChild(_plane);
Shared.textureManager().addTextureId(b, "mickey", false);
_plane.textures().addById("mickey");
b.recycle();
faceObject_2 = faceObject3D.clone();
faceObject_2.position().x = 1;
scene.addChild(faceObject_2);
scene.addChild(faceObject3D);//把模型加入到场景,这样模型才能显示,还可以在faceObject3D中加入新模型,同步显示。
faceObject3D.colorMaterialEnabled(true); //打开材质支持,默认是关闭的
}
public void updateScene() {
// faceObject3D.rotation().x++;//围绕着X轴旋转,正数为顺时针,速度由X的数值变化决定
faceObject3D.rotation().y++;//可以单独围绕着Y轴(或XZ)旋转,这个任意,根据外部传感器同步姿态也可以。
faceObject3D.rotation().z++;//旋转在飞行器上的参数,对应的是pitch,roll,yaw
_plane.rotation().z += 0.2f;
}
}
重点来了。需要在public class MainActivity extends RendererActivity,所继承的RendererActivity中添加代码:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
_initSceneHander = new Handler();
_updateSceneHander = new Handler();
//
// These 4 lines are important.
//
Shared.context(this);
scene = new Scene(this);
Renderer r = new Renderer(scene);
Shared.renderer(r);
_glSurfaceView = new GLSurfaceView(this);
glSurfaceViewConfig();//从这后面开始加
_glSurfaceView.setZOrderOnTop(true);
_glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
_glSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
//设置透明,在这句前面
_glSurfaceView.setRenderer(r);
//Set the rendering mode.
//When renderMode is RENDERMODE_CONTINUOUSLY, the renderer is called repeatedly to re-render the scene.
//When renderMode is RENDERMODE_WHEN_DIRTY, the renderer only rendered when the surface is created, or when requestRender() is called.
//默锟较诧拷锟斤拷为锟斤拷RENDERMODE_CONTINUOUSLY.
_glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
onCreateSetContentView();
}
编译运行, 试试看吧。