Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
0.8.4:atlas-material [2022/06/15 19:50] admin0.8.4:atlas-material [2022/06/17 07:50] (current) – [Background] admin
Line 1: Line 1:
 ====== Texture Atlas (TGorillaSharedAtlasMaterialSource) ====== ====== Texture Atlas (TGorillaSharedAtlasMaterialSource) ======
  
-The **TGorillaSharedAtlasMaterialSource** is inherited from TGorillaSharedMaterialSource.+ 
 +The **TGorillaSharedAtlasMaterialSource** is inherited from [[shared-material|TGorillaSharedMaterialSource]].
 This means it is only a container for central storage of a texture atlas. This means it is only a container for central storage of a texture atlas.
  
-To link one frame of the atlas to a specific 3D object you have to create further **TGorillaAtlasMaterialSource** instances.+To link a single frame of the texture atlas to a specific 3D object you have to create further **TGorillaAtlasMaterialSource** instances
 +Those will should then be linked to your TGorillaSharedAtlasMaterialSource instance.
  
-<file pascal> +===== Background =====
-var GorillaSharedAtlas : TGorillaSharedAtlasMaterialSource; +
-    GorillaAtlasFragment1 : TGorillaAtlasMaterialSource; +
-    GorillaAtlasFragment2 : TGorillaAtlasMaterialSource; +
-     +
-GorillaSharedAtlas :TGorillaSharedAtlasMaterialSource.Create(GorillaViewport1); +
-GorillaSharedAtlas.AtlasRowCount :32; +
-GorillaSharedAtlas.AtlasColCount :32; +
-GorillaSharedAtlas.FrameWidth :16; +
-GorillaSharedAtlas.FrameHeight :16;+
  
-/// create first fragment material for cube component +Having games like Minecraft in mind, texturing seems very simple when using a single atlas textureWorking with FMX materials makes this a struggle
-GorillaAtlasFragment1 := TGorillaAtlasMaterialSource.Create(GorillaViewport1); +Because each time you create a material source and use the atlas texture, FMX will load the texture into the GPU. So if you're having an atlas with 8x8 parts, this will create 64 texture with the size of the original oneThis is just horrible memory management.
-GorillaAtlasFragment1.SharedSource := GorillaSharedAtlas; +
-/// select the frame from the atlas to show +
-GorillaAtlasFragment1.TextureIndex := 8; +
-/// link to first component +
-GorillaCube1.MaterialSource := GorillaAtlasFragment1;+
  
-/// create second fragment material for sphere component +That's not what we want. Gorilla3D offers a solution to you: SharedMaterialSource's!  
-GorillaAtlasFragment2 := TGorillaAtlasMaterialSource.Create(GorillaViewport1); + 
-GorillaAtlasFragment2.SharedSource := GorillaSharedAtlas; +You set up a shared atlas material source once, where you load up your atlas texture. Than you only create referenced materials linked to that source. The reference material shader will use the global texture for rendering, instead of registering its own. 
-/// select the frame from the atlas to show + 
-GorillaAtlasFragment2.TextureIndex := 23; +{{:0.8.4:share_atlas.png?nolink&600|}} 
-/// link to first component + 
-GorillaSphere1.MaterialSource := GorillaAtlasFragment2; + 
-</file>+===== Texture-Atlas =====
  
 A texture atlas (also called a sprite sheet or an image sprite) is an image containing a collection of smaller images, usually packed together to reduce the atlas size.[1] Atlases can consist of uniformly-sized sub-images, or they can consist of images of varying dimensions.[1] A sub-image is drawn using custom texture coordinates to pick it out of the atlas. In an application where many small textures are used frequently, it is often more efficient to store the textures in a texture atlas which is treated as a single unit by the graphics hardware. Storing textures in an atlas reduces the overhead of a context switch by increasing memory locality. Careful alignment may be needed to avoid bleeding between sub textures when used with mipmapping and texture compression. A texture atlas (also called a sprite sheet or an image sprite) is an image containing a collection of smaller images, usually packed together to reduce the atlas size.[1] Atlases can consist of uniformly-sized sub-images, or they can consist of images of varying dimensions.[1] A sub-image is drawn using custom texture coordinates to pick it out of the atlas. In an application where many small textures are used frequently, it is often more efficient to store the textures in a texture atlas which is treated as a single unit by the graphics hardware. Storing textures in an atlas reduces the overhead of a context switch by increasing memory locality. Careful alignment may be needed to avoid bleeding between sub textures when used with mipmapping and texture compression.
  
 [https://en.wikipedia.org/wiki/Texture_atlas] [https://en.wikipedia.org/wiki/Texture_atlas]
 +
 +{{:atlas.png?nolink&400|Sample atlas texture}}
 +
 +
 +===== Configuration =====
  
 To configure a supplied texture atlas in the material use the provided properties: To configure a supplied texture atlas in the material use the provided properties:
  
 ^ Property ^ Notice ^ ^ Property ^ Notice ^
 +| Texture | Texture atlas image to be used for splitting up in single frames. |
 | AtlasRowCount | Defines the number of rows the texture atlas has. | | AtlasRowCount | Defines the number of rows the texture atlas has. |
 | AtlasColCount | Defines the number of columns the texture atlas has. | | AtlasColCount | Defines the number of columns the texture atlas has. |
 | FrameWidth | Defines the width of each frame in pixels. The width has to be a potency of 2. For example: 16, 32, 64, 128, ... | | FrameWidth | Defines the width of each frame in pixels. The width has to be a potency of 2. For example: 16, 32, 64, 128, ... |
 | FrameHeight | Defines the height of each frame in pixels. The height has to be a potency of 2. For example: 16, 32, 64, 128, ... | | FrameHeight | Defines the height of each frame in pixels. The height has to be a potency of 2. For example: 16, 32, 64, 128, ... |
 +
 +
 +===== Example =====
 +
 +In the following example we have added 3 TGorillaAtlasMaterialSource instances from component palette before. We've set the "TextureIndex" property to an index of the sub-texture in our atlas (from left to right, up to down, beginning with 0).
 +We have 3 cubes, to which we will apply the atlas materials.
 +The shared source material will be created at runtime. (There is no design-time component yet!)
 +
 +<file pascal>
 +procedure TForm1.FormCreate(Sender: TObject);
 +var LTexFile : String;
 +begin
 +{$IFDEF MSWINDOWS}
 +  LTexFile := 'atlas.png';
 +{$ENDIF}
 +{$IFDEF ANDROID}
 +  LTexFile := IncludeTrailingPathDelimiter(TPath.GetHomePath()) + 'atlas.png';
 +{$ENDIF}
 +
 +  /// lets create a source material
 +  /// It's used as texture container, which GorillaAtlasMaterialSourceX uses.
 +  /// This will only register the atlas texture once, instead of multiple times
 +  /// CAUTION: currently this is only available at runtime
 +  SharedAtlasSource := TGorillaSharedAtlasMaterialSource.Create(GorillaViewport1);
 +  SharedAtlasSource.Parent := GorillaViewport1;
 +  SharedAtlasSource.AtlasRowCount := 8;
 +  SharedAtlasSource.AtlasColCount := 8;
 +  SharedAtlasSource.FrameWidth := 32;
 +  SharedAtlasSource.FrameHeight := 32;
 +  SharedAtlasSource.Texture.LoadFromFile(LTexFile);
 +
 +  // now link atlas materials to the source
 +  GorillaAtlasMaterialSource1.SharedSource := SharedAtlasSource;
 +  GorillaAtlasMaterialSource2.SharedSource := SharedAtlasSource;
 +  GorillaAtlasMaterialSource3.SharedSource := SharedAtlasSource;
 +
 +  // now link to cubes
 +  GorillaCube1.MaterialSource := GorillaAtlasMaterialSource1;
 +  GorillaCube2.MaterialSource := GorillaAtlasMaterialSource2;
 +  GorillaCube3.MaterialSource := GorillaAtlasMaterialSource3;
 +end;
 +</file>