我试图渲染一个 opengl 视图(CAEAGLLayer)透明,这样我就可以在它后面放一个 UIView。我正在使用以下设置来实现透明的 opengl 背景。
CAEAGLLayer* layer = (CAEAGLLayer*)eaglLayer;
layer.opaque = NO;
layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking,
kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
nil
];
我的问题是我的 opengl 场景中的对象现在是透明的,我可以看穿它们(大约 50% 的透明度)。
如何使opengl场景的背景透明,同时场景中的对象仍然不透明。
请注意,所有图层、UIView 和 UIWindows 的 alpha 值为 1.0,并且场景中的片段着色器不会导致问题,因为当 layer.opaque 设置为 YES 时,一切都是纯色。我还应该补充一点,如果有帮助的话,这是一个统一的项目。
编辑:
部分代码由统一设置。
int OpenEAGL_UnityCallback(UIWindow** window, int* screenWidth, int* screenHeight, int* openglesVersion)
{
CGRect rect = [[UIScreen mainScreen] bounds];
// Create a full-screen window
_window = [[UIWindow alloc] initWithFrame:rect];
EAGLView* view = [[EAGLView alloc] initWithFrame:rect];
UnityViewController *controller = [[UnityViewController alloc] init];
sGLViewController = controller;
#if defined(__IPHONE_3_0)
if( _ios30orNewer )
controller.wantsFullScreenLayout = TRUE;
#endif
controller.view = view;
CreateSplashView( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ? (UIView*)_window : (UIView*)view );
CreateActivityIndicator(_splashView);
// add only now so controller have chance to reorient *all* added views
[_window addSubview:view];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
[_window bringSubviewToFront:_splashView];
if( !UnityUseOSAutorotation() )
{
_autorotEnableHandling = true;
[[NSNotificationCenter defaultCenter] postNotificationName: UIDeviceOrientationDidChangeNotification object: [UIDevice currentDevice]];
}
// reposition activity indicator after we rotated views
if (_activityIndicator)
_activityIndicator.center = CGPointMake([_splashView bounds].size.width/2, [_splashView bounds].size.height/2);
int openglesApi =
#if defined(__IPHONE_3_0) && USE_OPENGLES20_IF_AVAILABLE
kEAGLRenderingAPIOpenGLES2;
#else
kEAGLRenderingAPIOpenGLES1;
#endif
for (; openglesApi >= kEAGLRenderingAPIOpenGLES1 && !_context; --openglesApi)
{
if (!UnityIsRenderingAPISupported(openglesApi))
continue;
_context = [[EAGLContext alloc] initWithAPI:openglesApi];
}
if (!_context)
return false;
if (![EAGLContext setCurrentContext:_context]) {
_context = 0;
return false;
}
const GLuint colorFormat = UnityUse32bitDisplayBuffer() ? GL_RGBA8_OES : GL_RGB565_OES;
if (!CreateWindowSurface(view, colorFormat, GL_DEPTH_COMPONENT16_OES, UnityGetDesiredMSAASampleCount(MSAA_DEFAULT_SAMPLE_COUNT), NO, &_surface)) {
return false;
}
glViewport(0, 0, _surface.w, _surface.h);
[_window makeKeyAndVisible];
[view release];
*window = _window;
*screenWidth = _surface.w;
*screenHeight = _surface.h;
*openglesVersion = _context.API;
_glesContextCreated = true;
return true;
}
void PresentSurface(EAGLSurfaceDesc* surface)
{
if(_skipPresent)
{
UNITY_DBG_LOG ("SKIP PresentSurface:\n");
return;
}
UNITY_DBG_LOG ("PresentSurface:\n");
EAGLContext *oldContext = [EAGLContext currentContext];
if (oldContext != _context)
[EAGLContext setCurrentContext:_context];
PreparePresentSurfaceGLES(surface);
// presentRenderbuffer presents currently bound RB, so make sure we have the correct one bound
GLES_CHK( glBindRenderbufferOES(GL_RENDERBUFFER_OES, surface->renderbuffer) );
if(![_context presentRenderbuffer:GL_RENDERBUFFER_OES])
printf_console("failed to present renderbuffer (%s:%i)\n", __FILE__, __LINE__ );
AfterPresentSurfaceGLES(surface);
if(oldContext != _context)
[EAGLContext setCurrentContext:oldContext];
}
bool CreateSurface(EAGLView *view, EAGLSurfaceDesc* surface)
{
CAEAGLLayer* eaglLayer = (CAEAGLLayer*)surface->eaglLayer;
assert(eaglLayer == [view layer]);
CGSize newSize = [eaglLayer bounds].size;
newSize.width = roundf(newSize.width);
newSize.height = roundf(newSize.height);
#ifdef __IPHONE_4_0
int resolution = UnityGetTargetResolution();
if ( (resolution == kTargetResolutionNative || resolution == kTargetResolutionHD)
&& [view respondsToSelector:@selector(setContentScaleFactor:)]
&& [[UIScreen mainScreen] respondsToSelector:@selector(scale)]
)
{
CGFloat scaleFactor = [UIScreen mainScreen].scale;
[view setContentScaleFactor:scaleFactor];
newSize.width = roundf(newSize.width * scaleFactor);
newSize.height = roundf(newSize.height * scaleFactor);
UnitySetInputScaleFactor(scaleFactor);
}
#endif
surface->w = newSize.width;
surface->h = newSize.height;
UNITY_DBG_LOG ("CreateWindowSurface: FBO\n");
CreateSurfaceGLES(surface);
GLES_CHK( glBindRenderbufferOES(GL_RENDERBUFFER_OES, surface->renderbuffer) );
return true;
}
extern "C" bool AllocateRenderBufferStorageFromEAGLLayer(void* eaglLayer)
{
return [_context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)eaglLayer];
}
extern "C" void InitEAGLLayer(void* eaglLayer, bool use32bitColor)
{
CAEAGLLayer* layer = (CAEAGLLayer*)eaglLayer;
const NSString* colorFormat = use32bitColor ? kEAGLColorFormatRGBA8 : kEAGLColorFormatRGB565;
layer.opaque = NO;
layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking,
colorFormat, kEAGLDrawablePropertyColorFormat,
nil
];
}