Skip to content

Commit

Permalink
display 3D avatar when applying texture to player
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Jan 12, 2020
1 parent b1ccdb4 commit 6b3446c
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 12 deletions.
25 changes: 17 additions & 8 deletions app/Http/Controllers/TextureController.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,39 +106,48 @@ public function avatarByPlayer(Minecraft $minecraft, Request $request, $name)
{
$player = Player::where('name', $name)->firstOrFail();

return $this->avatar($minecraft, $player->skin, (int) $request->query('size', 100));
return $this->avatar($minecraft, $request, $player->skin);
}

public function avatarByUser(Minecraft $minecraft, Request $request, $uid)
{
$texture = Texture::find(optional(User::find($uid))->avatar);

return $this->avatar($minecraft, $texture, (int) $request->query('size', 100));
return $this->avatar($minecraft, $request, $texture);
}

public function avatarByTexture(Minecraft $minecraft, Request $request, $tid)
{
$texture = Texture::find($tid);

return $this->avatar($minecraft, $texture, (int) $request->query('size', 100));
return $this->avatar($minecraft, $request, $texture);
}

protected function avatar(Minecraft $minecraft, Texture $texture = null, int $size = 100)
protected function avatar(Minecraft $minecraft, Request $request, Texture $texture = null)
{
$size = (int) $request->query('size', 100);
$mode = $request->has('3d') ? '3d' : '2d';

$disk = Storage::disk('textures');
if (is_null($texture) || $disk->missing($texture->hash)) {
return Image::make(storage_path('static_textures/avatar.png'))
return Image::make(resource_path("misc/textures/avatar$mode.png"))
->resize($size, $size)
->response('png', 100);
}

$hash = $texture->hash;
$now = Carbon::now();
$response = Cache::remember(
'avatar-2d-t'.$texture->tid.'-s'.$size,
'avatar-'.$mode.'-t'.$texture->tid.'-s'.$size,
option('enable_avatar_cache') ? $now->addYear() : $now->addMinute(),
function () use ($minecraft, $disk, $hash, $size) {
$image = $minecraft->render2dAvatar($disk->get($hash), 25);
function () use ($minecraft, $disk, $hash, $size, $mode) {
$file = $disk->get($hash);
if ($mode === '3d') {
$image = $minecraft->render3dAvatar($file, 25);
} else {
$image = $minecraft->render2dAvatar($file, 25);
}

$lastModified = Carbon::createFromTimestamp($disk->lastModified($hash));

return Image::make($image)
Expand Down
2 changes: 1 addition & 1 deletion resources/assets/src/components/ApplyToPlayerDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export default {
}
},
avatarUrl(player) {
return `${blessing.base_url}/avatar/${player.tid_skin}?size=35`
return `${blessing.base_url}/avatar/${player.tid_skin}?3d&size=35`
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion resources/assets/src/components/Previewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<script>
import * as skinview3d from 'skinview3d'
import { emit } from '../scripts/event'
import SkinSteve from '../images/textures/steve.png'
import SkinSteve from '../../../misc/textures/steve.png'
export default {
name: 'Previewer',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ test('compute avatar URL', () => {
// eslint-disable-next-line camelcase
const wrapper = mount<Vue & { avatarUrl(player: { tid_skin: number }): string }>(ApplyToPlayerDialog)
const { avatarUrl } = wrapper.vm
expect(avatarUrl({ tid_skin: 1 })).toBe('/avatar/1?size=35')
expect(avatarUrl({ tid_skin: 1 })).toBe('/avatar/1?3d&size=35')
})
2 changes: 1 addition & 1 deletion resources/assets/tests/views/user/Closet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ test('apply texture', async () => {
button.trigger('click')
await flushPromises()
expect(wrapper.find('input[type="radio"]').attributes('value')).toBe('1')
expect(wrapper.find('.model-label > img').attributes('src')).toBe('/avatar/10?size=35')
expect(wrapper.find('.model-label > img').attributes('src')).toBe('/avatar/10?3d&size=35')
expect(wrapper.find('.modal-body').text()).toContain('name')
jest.runAllTimers()
})
Expand Down
2 changes: 2 additions & 0 deletions resources/misc/changelogs/en/5.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
- Optimized performance of invoking texture previewer (skinview3d).
- Changed method of retrieving IP.
- Use `utf8mb4` encoding in MySQL/MariaDB.
- Switched to a new PHP texture renderer.
- Display 3D avatar of player when applying texture to player.

## Fixed

Expand Down
2 changes: 2 additions & 0 deletions resources/misc/changelogs/zh_CN/5.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
- 优化调用材质预览器(skinview3d)的性能
- 修改获取 IP 地址的方法
- MySQL/MariaDB 使用 `utf8mb4` 编码
- 使用新的 PHP 材质渲染器
- 将材质应用到角色时显示角色的 3D 头像

## 修复

Expand Down
File renamed without changes
Binary file added resources/misc/textures/avatar3d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file removed storage/static_textures/broken.png
Binary file not shown.
9 changes: 9 additions & 0 deletions tests/HttpTest/ControllersTest/TextureControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,5 +225,14 @@ public function testAvatarByTexture()
$this->assertEquals(50, $image->width());
$this->assertEquals(50, $image->height());
$this->assertTrue(Cache::has('avatar-2d-t'.$texture->tid.'-s50'));

$image = $this->get('/avatar/'.$texture->tid.'?3d')
->assertSuccessful()
->assertHeader('Content-Type', 'image/png')
->getContent();
$image = Image::make($image);
$this->assertEquals(100, $image->width());
$this->assertEquals(100, $image->height());
$this->assertTrue(Cache::has('avatar-3d-t'.$texture->tid.'-s100'));
}
}

0 comments on commit 6b3446c

Please sign in to comment.