Skip to content

Add new functions to spriteAI SDK #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions dev-docs/misc-spriteai-functions-documentation.md
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dev-docs write some docs on this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dev-docs write docs on this codefile

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dev-docs write some docs on this

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dev-docs write some docs on this codefile

Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,108 @@ const spriteWithBorder = await sprite.generateSpriteWithBorder('knight', { r: 25
console.log(spriteWithBorder);
```

### 6. generateEnvironmentSprites
**Location**: `/spriteAI/index.js`

Generates environment tilesets based on a description.

```javascript
async function generateEnvironmentSprites(description, options = {})
```

#### Parameters:
- `description` (string): Description of the environment to generate.
- `options` (object): Configuration options for the tileset generation.

#### Options:
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| elements | number | 4 | Number of different elements in the tileset |
| size | string | '1024x1024' | Output image size |
| style | string | 'pixel-art' | Art style to use |
| padding | number | 1 | Padding between tiles |
| theme | string | 'fantasy' | Theme of the environment |

#### Example Usage:
```javascript
const environmentSprites = await generateEnvironmentSprites('a fantasy forest', {
elements: 6,
size: '1024x1024',
style: 'pixel-art',
padding: 2,
theme: 'fantasy',
save: true
});
console.log(environmentSprites.tileset);
```

### 7. generateItemSprites
**Location**: `/spriteAI/index.js`

Generates item collections based on a description.

```javascript
async function generateItemSprites(description, options = {})
```

#### Parameters:
- `description` (string): Description of the items to generate.
- `options` (object): Configuration options for the item generation.

#### Options:
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| itemCount | number | 4 | Number of different items in the collection |
| size | string | '1024x1024' | Output image size |
| style | string | 'pixel-art' | Art style to use |
| padding | number | 1 | Padding between items |
| itemType | string | 'equipment' | Type of items to generate |
| background | string | 'white' | Background color of the items |

#### Example Usage:
```javascript
const itemSprites = await generateItemSprites('medieval weapons', {
itemCount: 8,
size: '1024x1024',
style: 'pixel-art',
padding: 2,
itemType: 'weapon',
background: 'transparent',
save: true
});
console.log(itemSprites.itemSheet);
```

### 8. fetchAvailableAnimationStates
**Location**: `/spriteAI/index.js`

Fetches available animation states.

```javascript
async function fetchAvailableAnimationStates()
```

#### Example Usage:
```javascript
const animationStates = await fetchAvailableAnimationStates();
console.log(animationStates);
```

### 9. fetchAvailableSpriteStyles
**Location**: `/spriteAI/index.js`

Fetches available sprite styles.

```javascript
async function fetchAvailableSpriteStyles()
```

#### Example Usage:
```javascript
const spriteStyles = await fetchAvailableSpriteStyles();
console.log(spriteStyles);
```

## How to Extend

1. **Add New Image Processing Function**
Expand Down
55 changes: 54 additions & 1 deletion spriteAI/README.md
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dev-docs write some docs on this

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dev-docs write some docs on this

Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,59 @@ removeBackgroundColor(inputPath, outputPath, targetColor, colorThreshold)
});
```

### Generating Environment Sprites

To generate environment tilesets, you can use the `generateEnvironmentSprites` function from the `spriteAI/index.js` file. Here’s an example:

```javascript
import { generateEnvironmentSprites } from './spriteAI/index.js';

const description = 'a fantasy forest';
const options = {
elements: 6,
size: '1024x1024',
style: 'pixel-art',
padding: 2,
theme: 'fantasy',
save: true
};

generateEnvironmentSprites(description, options)
.then(result => {
console.log('Environment sprites generated:', result.tileset);
})
.catch(error => {
console.error('Error generating environment sprites:', error);
});
```

### Generating Item Sprites

To generate item collections, you can use the `generateItemSprites` function from the `spriteAI/index.js` file. Here’s an example:

```javascript
import { generateItemSprites } from './spriteAI/index.js';

const description = 'medieval weapons';
const options = {
itemCount: 8,
size: '1024x1024',
style: 'pixel-art',
padding: 2,
itemType: 'weapon',
background: 'transparent',
save: true
};

generateItemSprites(description, options)
.then(result => {
console.log('Item sprites generated:', result.itemSheet);
})
.catch(error => {
console.error('Error generating item sprites:', error);
});
```

## Directory Structure

- `index.js`: Main logic for generating spritesheets and removing backgrounds.
Expand All @@ -83,4 +136,4 @@ Contributions are welcome! Please open an issue or submit a pull request for any

## License

This project is licensed under the MIT License. See the LICENSE file for more details.
This project is licensed under the MIT License. See the LICENSE file for more details.
3 changes: 1 addition & 2 deletions spriteAI/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// filepath: /spriteAI/index.js
import OpenAI from "openai";
import axios from "axios";
import sharp from "sharp";
Expand Down Expand Up @@ -230,4 +229,4 @@ export const generateItemSprites = async function(description, options = {}) {
}
}
};
};
};
66 changes: 65 additions & 1 deletion tests/sprite.test.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dev-docs write docs on this codefile

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const sprite = require('./index').sprite;
const fs = require('fs');
const path = require('path');
const sharp = require('sharp');
const { generateEnvironmentSprites, generateItemSprites, fetchAvailableAnimationStates, fetchAvailableSpriteStyles } = require('../spriteAI/index');

describe('sprite', () => {
describe('generateSprite', () => {
Expand Down Expand Up @@ -46,4 +47,67 @@ describe('sprite', () => {

// Add more test cases as needed
});
});
});

describe('generateEnvironmentSprites', () => {
it('should generate environment sprites with the correct dimensions', async () => {
const description = 'a fantasy forest';
const options = {
elements: 6,
size: '1024x1024',
style: 'pixel-art',
padding: 2,
theme: 'fantasy',
save: false
};
const result = await generateEnvironmentSprites(description, options);

expect(result).toBeDefined();
expect(result.tileset).toBeDefined();
expect(result.metadata).toBeDefined();
expect(result.metadata.elements).toBe(options.elements);
expect(result.metadata.theme).toBe(options.theme);
});
});

describe('generateItemSprites', () => {
it('should generate item sprites with the correct dimensions', async () => {
const description = 'medieval weapons';
const options = {
itemCount: 8,
size: '1024x1024',
style: 'pixel-art',
padding: 2,
itemType: 'weapon',
background: 'transparent',
save: false
};
const result = await generateItemSprites(description, options);

expect(result).toBeDefined();
expect(result.itemSheet).toBeDefined();
expect(result.metadata).toBeDefined();
expect(result.metadata.itemCount).toBe(options.itemCount);
expect(result.metadata.itemType).toBe(options.itemType);
});
});

describe('fetchAvailableAnimationStates', () => {
it('should fetch available animation states', async () => {
const result = await fetchAvailableAnimationStates();

expect(result).toBeDefined();
expect(Array.isArray(result)).toBe(true);
expect(result.length).toBeGreaterThan(0);
});
});

describe('fetchAvailableSpriteStyles', () => {
it('should fetch available sprite styles', async () => {
const result = await fetchAvailableSpriteStyles();

expect(result).toBeDefined();
expect(Array.isArray(result)).toBe(true);
expect(result.length).toBeGreaterThan(0);
});
});