检测成功则返回NSArray *,ARHitTestResult中包含检测类型,订交点的距离,平面的ARAnchor。留意只有检测到ARHitTestResultTypeExistingPlane和ARHitTestResultTypeExistingPlaneUsingExtent才会有ARAnchor。这四个检测类型是可以经由过程|的方法同时存在的,比如ARHitTestResultTypeEstimatedHorizontalPlane | ARHitTestResultTypeExistingPlane。
光线强度调节
ARKit还供给了一个检测光照强度的功能,重要为了让3D模型的光照和情况的光照强度保持一致。在ARFrame中有一个lightEstimate的变量,如不雅检测光照强度成功,则会有值。值的类型为ARLightEstimate,个中只包含一个变量ambientIntensity。在3D光照模型中,它对应情况光,它的值大年夜0 ~ 2000。应用OpenGL衬着时,可以应用这个值调剂光照模型中的情况光强度。
ARHitTestResultTypeExistingPlane, 已经检测出来的平面,检测时忽视瞥ゆ本身大年夜小,把它看做一个无穷大年夜的平面。
ARKit的理论常识差不多到此停止了,下一篇将会介绍若何应用OpenGL ES衬着ARFrame里的内容。
实现篇
本文所用OpenGL基本代率攀来自OpenGL ES系列,具备衬着几何体,纹理等基本功能,实现细节将不赘述。
集成ARKit的关键代码都在ARGLBaseViewController中。我们来看一下它的代码。
处理ARFrame
- (void)session:(ARSession *)session didUpdateFrame:(ARFrame *)frame { // 同步YUV信息到 yTexture 和 uvTexture CVPixelBufferRef pixelBuffer = frame.capturedImage; GLsizei imageWidth = (GLsizei)CVPixelBufferGetWidthOfPlane(pixelBuffer, 0); GLsizei imageHeight = (GLsizei)CVPixelBufferGetHeightOfPlane(pixelBuffer, 0); void * baseAddress = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0); glBindTexture(GL_TEXTURE_2D, self.yTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, imageWidth, imageHeight, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, baseAddress); glBindTexture(GL_TEXTURE_2D, 0); imageWidth = (GLsizei)CVPixelBufferGetWidthOfPlane(pixelBuffer, 1); imageHeight = (GLsizei)CVPixelBufferGetHeightOfPlane(pixelBuffer, 1); void *laAddress = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1); glBindTexture(GL_TEXTURE_2D, self.uvTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, imageWidth, imageHeight, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, laAddress); glBindTexture(GL_TEXTURE_2D, 0); self.videoPlane.yuv_yTexture = self.yTexture; self.videoPlane.yuv_uvTexture = self.uvTexture; [self setupViewport: CGSizeMake(imageHeight, imageWidth)]; // 同步摄像机 matrix_float4x4 cameraMatrix = matrix_invert([frame.camera transform]); GLKMatrix4 newCameraMatrix = GLKMatrix4Identity; for (int col = 0; col < 4; ++col) { for (int row = 0; row < 4; ++row) { newCameraMatrix.m[col * 4 + row] = cameraMatrix.columns[col][row]; } } self.cameraMatrix = newCameraMatrix; GLKVector3 forward = GLKVector3Make(-self.cameraMatrix.m13, -self.cameraMatrix.m23, -self.cameraMatrix.m33); GLKMatrix4 rotationMatrix = GLKMatrix4MakeRotation(M_PI / 2, forward.x, forward.y, forward.z); self.cameraMatrix = GLKMatrix4Multiply(rotationMatrix, newCameraMatrix);}膳绫擎的代码展示了若何处理ARKit捕获的ARFrame,ARFrame的capturedImage存储了摄像头捕获的图片信息,类型是CVPixelBufferRef。默认情况下,图片信息的格局是YUV,经由过程两个Plane来存储,也可以懂得为两张图片。一张格局是Y(Luminance),保存了明度信息,另一张是UV(Chrominance、Chroma),保存了色度和浓度。我们须要把这两张图分别绑定到不呵9依υ?理上,然后在Shader中应用算法将YUV转换成RGB。下面是处理纹理的Fragment Shader,应用公式进行色彩转换。
precision highp float;varying vec3 fragNormal;varying vec2 fragUV;uniform float elapsedTime;uniform mat4 normalMatrix;uniform sampler2D yMap;uniform sampler2D uvMap;void main(void) { vec4 Y_planeColor = texture2D(yMap, fragUV); vec4 CbCr_planeColor = texture2D(uvMap, fragUV); float Cb, Cr, Y; float R ,G, B; Y = Y_planeColor.r * 255.0; Cb = CbCr_planeColor.r * 255.0 - 128.0; Cr = CbCr_planeColor.a * 255.0 - 128.0; R = 1.402 * Cr + Y; G = -0.344 * Cb - 0.714 * Cr + Y; B = 1.772 * Cb + Y; vec4 videoColor = vec4(R / 255.0, G / 255.0, B / 255.0, 1.0); gl_FragColor = videoColor;}VideoPlane
VideoPlane是为了显示视频编写的几何体,它可以或许接收两个纹理,Y和UV。
@interface VideoPlane : GLObject@property (assign, nonatomic) GLuint yuv_yTexture;@property (assign, nonatomic) GLuint yuv_uvTexture;- (instancetype)initWithGLContext:(GLContext *)context;- (void)update:(NSTimeInterval)timeSinceLastUpdate;- (void)draw:(GLContext *)glContext;@end...- (void)draw:(GLContext *)glContext { [glContext setUniformMatrix4fv:@"modelMatrix" value:self.modelMatrix]; bool canInvert; GLKMatrix4 normalMatrix = GLKMatrix4InvertAndTranspose(self.modelMatrix, &canInvert); [glContext setUniformMatrix4fv:@"normalMatrix" value:canInvert ? normalMatrix : GLKMatrix4Identity]; [glContext bindTextureName:self.yuv_yTexture to:GL_TEXTURE0 uniformName:@"yMap"]; [glContext bindTextureName:self.yuv_uvTexture to:GL_TEXTURE1 uniformName:@"uvMap"]; [glContext drawTrianglesWithVAO:vao vertexCount:6];}
推荐阅读
如何配置并集成iRedMail服务到Samba4 AD DC中
在本教程中,将进修若何修改供给邮件办事的 iRedMail 重要守护过程,响应地,Postfix 用于邮件传输,Dovecot 将邮件传送到帐户邮箱,以便将它们集成到 Samba4 AD 域控制器中。请求在 Cen>>>详细阅读
地址:http://www.17bianji.com/lsqh/36092.html
1/2 1

网友点评
精彩导读
科技快报
品牌展示