private void LoadTextureIndex() { List <Texture2D> individualTextures = blocks.GetAllUniqueTextures(); Texture2D neutralTexture = new Texture2D(blocks.TextureSize, blocks.TextureSize) { name = NO_TEXTURE_NAME }; for (int x = 0; x < neutralTexture.width; x++) { for (int y = 0; y < neutralTexture.height; y++) { neutralTexture.SetPixel(x, y, Color.white); } } neutralTexture.Apply(false); for (int i = 0; i < individualTextures.Count; i++) { if (individualTextures[i] == null) { individualTextures[i] = neutralTexture; } Assert.IsTrue(individualTextures[i].width == blocks.TextureSize, individualTextures[i].name + " width must be the same as texture size!"); Assert.IsTrue(individualTextures[i].height == blocks.TextureSize, individualTextures[i].name + " height must be the same as texture size!"); Assert.IsTrue(individualTextures[i].isReadable == true, individualTextures[i].name + " must be marked as readbale!"); } // Generate atlas Texture2D packedTextures = new Texture2D(0, 0, TextureFormat.ARGB32, 0, false); Rect[] rects = packedTextures.PackTextures(individualTextures.ToArray(), 0, 8192, false); // Transfer over the pixels to another texture2d because PackTextures resets the texture format and useMipMaps settings atlas = new Texture2D(packedTextures.width, packedTextures.height, config.AtlasFormat, config.UseMipMaps); atlas.SetPixels(packedTextures.GetPixels(0, 0, packedTextures.width, packedTextures.height)); atlas.filterMode = config.AtlasFiltering; atlas.Apply(); int index = 0; textures.Clear(); for (int i = 0; i < individualTextures.Count; i++) { Rect uvs = rects[index]; if (!textures.TryGetValue(individualTextures[i].name, out TextureCollection collection)) { collection = new TextureCollection(individualTextures[i].name, TextureConfigType.Simple); textures.Add(individualTextures[i].name, collection); } //collection.AddTexture(uvs, new TextureConfig.Texture() { weight = 1, index = 0 }); collection.AddTexture(new Vector2Int((int)(atlas.width * uvs.x / 128), (int)(atlas.height * uvs.y / 128))); index++; } }
public override bool OnSetUp(BlockConfigObject config, World world) { if (!base.OnSetUp(config, world)) { return(false); } if (config is CrossMeshConfigObject crossMeshConfig) { texture = world.textureProvider.GetTextureCollection(crossMeshConfig.Texture.texture); color = crossMeshConfig.Texture.color; } else { Debug.LogError(config.GetType().Name + " config passed to cross mesh block."); return(false); } return(true); }
public static void PrepareTexture(Vector3[] verts, Vector4[] data, Direction direction, TextureCollection textureCollection, bool rotated) { Vector2Int texture = textureCollection.GetTexture(); bool backface = DirectionUtils.IsBackface(direction); PrepareTexture(data, verts, direction, texture, rotated, backface); }