display 3D avatar when applying texture to player

This commit is contained in:
Pig Fang 2020-01-12 09:57:55 +08:00
parent b1ccdb47f2
commit 6b3446cf86
12 changed files with 34 additions and 12 deletions

View File

@ -106,28 +106,31 @@ class TextureController extends Controller
{
$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);
}
@ -135,10 +138,16 @@ class TextureController extends Controller
$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)

View File

@ -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`
},
},
}

View File

@ -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',

View File

@ -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')
})

View File

@ -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()
})

View File

@ -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

View File

@ -32,6 +32,8 @@
- 优化调用材质预览器skinview3d的性能
- 修改获取 IP 地址的方法
- MySQL/MariaDB 使用 `utf8mb4` 编码
- 使用新的 PHP 材质渲染器
- 将材质应用到角色时显示角色的 3D 头像
## 修复

View File

Before

Width:  |  Height:  |  Size: 662 B

After

Width:  |  Height:  |  Size: 662 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

View File

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 185 B

View File

@ -225,5 +225,14 @@ class TextureControllerTest extends TestCase
$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'));
}
}