소스 검색

Added code commets

Björn Ritzl 2 년 전
부모
커밋
6d0e6ab0d4
2개의 변경된 파일21개의 추가작업 그리고 0개의 파일을 삭제
  1. 9 0
      examples/resource/modify_atlas/modify_atlas.md
  2. 12 0
      examples/resource/modify_atlas/modify_atlas.script

+ 9 - 0
examples/resource/modify_atlas/modify_atlas.md

@@ -0,0 +1,9 @@
+---
+title: Modify atlas
+brief: This example shows how to replace an image in an atlas
+scripts: modify_atlas.script
+---
+
+The example loads an image bundled as a custom resource (bundled in the game archive) and uses it to replace the first image of an atlas. See code comments for implementation details.
+
+![size](size.png)

+ 12 - 0
examples/resource/modify_atlas/modify_atlas.script

@@ -38,18 +38,29 @@ local function create_buffer_from_image(filename)
 end
 
 local function replace_atlas_image()
+	-- get table with information about an atlas
 	local atlas = resource.get_atlas("/examples/resource/modify_atlas/modify_atlas.a.texturesetc")
+	-- get table with information about the textured used by the atlas
 	local texture = resource.get_texture_info(atlas.texture)
 	pprint(atlas)
 	pprint(texture)
 
+	-- load an image as a Defold buffer
 	local pixel_buffer, width, height = create_buffer_from_image("/examples/resource/modify_atlas/resources/shipYellow_manned.png")
 
+	-- get the UV coordinates of the first image in the atlas
 	local first_uvs = atlas.geometries[1].uvs
+
+	-- this offset should not be necessary but it seems like there is an issue with the
+	-- UVs in Defold 1.5.0
 	local x = first_uvs[1] - 0
 	local y = first_uvs[2] - 6
 	print(x, y)
 	print(width, height)
+
+	-- create a table with texture update information
+	-- we want to update only a sub region of the atlas starting at a
+	-- certain position and with a certain size
 	local texture_info = {
 		type = resource.TEXTURE_TYPE_2D,
 		width = width,
@@ -60,6 +71,7 @@ local function replace_atlas_image()
 		compression_type = resource.COMPRESSION_TYPE_DEFAULT,
 		num_mip_maps = texture.mipmaps,
 	}
+	-- update the atlas texture with the pixels from the provided buffer
 	resource.set_texture(atlas.texture, texture_info, pixel_buffer)
 end