Skip to content

Instantly share code, notes, and snippets.

@objective-audio
Created March 13, 2012 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save objective-audio/2027722 to your computer and use it in GitHub Desktop.
Save objective-audio/2027722 to your computer and use it in GitHub Desktop.
Unityのテクスチャを書き換えるプラグイン
//texIDはTexture.GetNativeTextureID()で取得したGLのTexture
//texWidthとtexHeightはテクスチャのサイズ
//strは書き込みたい文字列
void WriteTextToTexture_(int texID, int texWidth, int texHeight, const char *str) {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
Byte *bitmapBuffer = calloc(texHeight, texWidth * 4);
CGContextRef bitmapContext = CGBitmapContextCreate(bitmapBuffer, texWidth, texHeight, 8, texWidth * 4, colorSpace, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);
UIGraphicsPushContext(bitmapContext);
[[UIColor whiteColor] set];
CGRect drawRect = CGRectMake(0, 0, texWidth, texHeight);
NSString *text = [NSString stringWithCString:str encoding:NSUTF8StringEncoding];
[text drawInRect:drawRect withFont:[UIFont systemFontOfSize:120] lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentLeft];
UIGraphicsPopContext();
glBindTexture(GL_TEXTURE_2D, texID);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texWidth, texHeight, GL_RGBA, GL_UNSIGNED_BYTE, bitmapBuffer);
glBindTexture(GL_TEXTURE_2D, 0);
free(bitmapBuffer);
CGContextRelease(bitmapContext);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment