From 15740faf9fe9fe4be08965098bbf2947e096aeeb Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 14 Aug 2019 22:50:43 +0800 Subject: +Unity Runtime code --- Runtime/Graphics/TextureGenerator.h | 114 ++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 Runtime/Graphics/TextureGenerator.h (limited to 'Runtime/Graphics/TextureGenerator.h') diff --git a/Runtime/Graphics/TextureGenerator.h b/Runtime/Graphics/TextureGenerator.h new file mode 100644 index 0000000..13e9dac --- /dev/null +++ b/Runtime/Graphics/TextureGenerator.h @@ -0,0 +1,114 @@ +#ifndef TEXTUREGENERATOR_H +#define TEXTUREGENERATOR_H + +#include "TextureFormat.h" +#include "Texture3D.h" +#include "Texture.h" +#include "Runtime/Math/Vector3.h" +#include "CubemapTexture.h" + +template +void GenerateTexture (Texture2D *tex, const ColorFunctor &fun) { + AssertIf (!tex); + int maxX = tex->GetGLWidth (), maxY = tex->GetGLHeight(); + T *data = (T*)tex->GetRawImageData(); + int bpp = GetBytesFromTextureFormat (tex->GetTextureFormat()) / sizeof(T); + for (int y = 0; y < maxY; y++) + for (int x = 0; x < maxX; x++) { + fun (tex, data, x, y, maxX, maxY); + data+=bpp; + } +} + +template +void Generate3DTexture (Texture3D *tex, const ColorFunctor &fun) { + AssertIf (!tex); + int maxX = tex->GetGLWidth (), maxY = tex->GetGLHeight(), maxZ = tex->GetDepth(); + T *data = (T*)tex->GetImageDataPointer(); + int bpp = GetBytesFromTextureFormat (tex->GetTextureFormat()) / sizeof(T); + for (int z = 0; z < maxZ; z++) { + for (int y = 0; y < maxY; y++) { + for (int x = 0; x < maxX; x++) { + fun (data, x, y, z, maxX, maxY, maxZ); + data += bpp; + } + } + } +} + +template +void GenerateCubeTexture (Cubemap *cubemap, const ColorFunctor &fun) { + AssertIf (!cubemap); + for (int direction = 0; direction < 6; direction++) { + const int kCubeXRemap[6] = { 2, 2, 0, 0, 0, 0 }; + const int kCubeYRemap[6] = { 1, 1, 2, 2, 1, 1 }; + const int kCubeZRemap[6] = { 0, 0, 1, 1, 2, 2 }; + const float kCubeXSign[6] = { -1.0F, 1.0F, 1.0F, 1.0F, 1.0F, -1.0F }; + const float kCubeYSign[6] = { -1.0F, -1.0F, 1.0F, -1.0F, -1.0F, -1.0F }; + const float kCubeZSign[6] = { 1.0F, -1.0F, 1.0F, -1.0F, 1.0F, -1.0F }; + + int width = cubemap->GetGLWidth (); + int height = cubemap->GetGLHeight (); + + // do the sign scale according to the cubemap specs then flip y sign + Vector3f signScale = Vector3f (kCubeXSign[direction], kCubeYSign[direction], kCubeZSign[direction]); + int byteSize = GetBytesFromTextureFormat (cubemap->GetTextureFormat ()); + + Vector2f invSize (1.0F / (float)width, 1.0F / (float)height); + UInt8* dest = cubemap->GetRawImageData (direction); + for (int y=0;y +Texture2D *BuildTexture (int width, int height, TextureFormat format, const ColorFunctor &fun, bool mipmaps = false) { + Texture2D* tex = CreateObjectFromCode(); + tex->SetHideFlags(Object::kHideAndDontSave); + tex->InitTexture (width, height, format, mipmaps ? Texture2D::kMipmapMask : Texture2D::kNoMipmap, 1); + tex->GetSettings().m_Aniso = 0; // disable aniso + GenerateTexture (tex, fun); + mipmaps ? tex->UpdateImageData() : tex->UpdateImageDataDontTouchMipmap(); + return tex; +} + +template +Texture3D *Build3DTexture (int width, int height, int depth, TextureFormat format, const ColorFunctor &fun) { + Texture3D *tex = CreateObjectFromCode(); + tex->SetHideFlags(Object::kHideAndDontSave); + tex->InitTexture (width, height, depth, format, false); + Generate3DTexture (tex, fun); + tex->UpdateImageData(false); + return tex; +} + +template +Cubemap *BuildCubeTexture (int size, TextureFormat format, const ColorFunctor &fun) { + Cubemap *tex = CreateObjectFromCode(); + tex->SetHideFlags(Object::kHideAndDontSave); + tex->InitTexture (size, size, format, 0, 6); + GenerateCubeTexture (tex, fun); + tex->UpdateImageDataDontTouchMipmap(); + tex->GetSettings ().m_WrapMode = kTexWrapClamp; + tex->ApplySettings (); + return tex; +} + + +#endif -- cgit v1.1-26-g67d0