Skip to content

Instantly share code, notes, and snippets.

@tjclement
Created December 15, 2016 14:05
Show Gist options
  • Select an option

  • Save tjclement/36a42ae29748eaabd4ed657dd2b0221f to your computer and use it in GitHub Desktop.

Select an option

Save tjclement/36a42ae29748eaabd4ed657dd2b0221f to your computer and use it in GitHub Desktop.
Unity-webview rendering technique
- (void)update:(int)x y:(int)y deltaY:(float)deltaY buttonDown:(BOOL)buttonDown buttonPress:(BOOL)buttonPress buttonRelease:(BOOL)buttonRelease keyPress:(BOOL)keyPress keyCode:(unsigned short)keyCode keyChars:(const char*)keyChars
{
// [..] Updates internal state, sends through click events to user agent etc.
@synchronized(self) {
if (bitmap == nil)
bitmap = [[webView bitmapImageRepForCachingDisplayInRect:webView.frame] retain];
memset([bitmap bitmapData], 0, [bitmap bytesPerRow] * [bitmap pixelsHigh]);
[webView cacheDisplayInRect:webView.frame toBitmapImageRep:bitmap];
needsDisplay = YES; // TODO (bitmap == nil || [view needsDisplay]);
}
}
- (void)render
{
@synchronized(self) {
if (webView == nil)
return;
if (!needsDisplay)
return;
if (bitmap == nil)
return;
int samplesPerPixel = (int)[bitmap samplesPerPixel];
int rowLength = 0;
int unpackAlign = 0;
glGetIntegerv(GL_UNPACK_ROW_LENGTH, &rowLength);
glGetIntegerv(GL_UNPACK_ALIGNMENT, &unpackAlign);
glPixelStorei(GL_UNPACK_ROW_LENGTH, (GLint)[bitmap bytesPerRow] / samplesPerPixel);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glBindTexture(GL_TEXTURE_2D, textureId);
if (![bitmap isPlanar] && (samplesPerPixel == 3 || samplesPerPixel == 4)) {
glTexSubImage2D(
GL_TEXTURE_2D,
0,
0,
0,
(GLsizei)[bitmap pixelsWide],
(GLsizei)[bitmap pixelsHigh],
samplesPerPixel == 4 ? GL_RGBA : GL_RGB,
GL_UNSIGNED_BYTE,
[bitmap bitmapData]);
}
glPixelStorei(GL_UNPACK_ROW_LENGTH, rowLength);
glPixelStorei(GL_UNPACK_ALIGNMENT, unpackAlign);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment