5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

MATLABでゆるキャラを3D描画

Posted at

MATLABのplot3()を悪用してゆるキャラ(?)を3D描画し、動画に書き出しまでの方法になります。

ときどき描き方の問い合わせをいただくので、いい機会だと思ってまとめました。

元々は私が半趣味で書いたコードですので「動けばヨシ!」のノリで書かれています。力技での実装ですので、重いかもしれません。

##今回描くもの
riroshi.png
可愛いですね。どこぞの大学のどこぞの語学クラスのゆるキャラです。
え、見覚えがある? きっと気のせいですよ。

噂ではよく燃やされたりしているそうな。
因みに怒られたらこの記事は消えます()

##コード

DrawPicture.m
figure();
hold on;
% 円を描くのに使用
% ここで細かく刻むと綺麗になる
t = (0:0.2:6.4);
% 描画精度
% ここでdeltaを小さくすると綺麗になる
delta = 0.05;
% frame数の管理
f = 1;
% はじめの視野角
view([0 22]);
axis equal;
grid on;

% ここから描画
% 足
for ii = (0:delta:2.8)
    plot3(-1.75+(1.2+ii/2.8*0.3)*cos(t), (1.2+ii/2.8*0.3)*sin(t), ones(size(t))*ii, 'r-')
    plot3( 1.75+(1.2+ii/2.8*0.3)*cos(t), (1.2+ii/2.8*0.3)*sin(t), ones(size(t))*ii, 'r-')
    drawnow;
    movie(f) = getframe(gcf);
    f = f+1;
end

% 赤ー底面
for ii = (0:delta:3.25)
    plot3(ii*cos(t), ii*sin(t), ones(size(t))*2.8, 'r-')
    drawnow;
    movie(f) = getframe(gcf);
    f = f+1;
end

% 赤ー腰
for ii = (2.8:delta:3.6)
    plot3(3.25*cos(t), 3.25*sin(t), ones(size(t))*ii, 'r-')
    drawnow;
    movie(f) = getframe(gcf);
    f = f+1;
end

% 胴体
for ii = (3.6:delta:9)
    plot3(3.25*cos(t), 3.25*sin(t), ones(size(t))*ii, 'b-')
    drawnow;
    movie(f) = getframe(gcf);
    f = f+1;
end

% 頭部
for ii = (9:delta:10.6)
    plot3(3.25*cos(t), 3.25*sin(t), ones(size(t))*ii, 'w-')
    drawnow;
    movie(f) = getframe(gcf);
    f = f+1;
end
for ii = (10.6:delta:13.85)
    plot3(sqrt(3.25*3.25-(ii-10.6)*(ii-10.6))*cos(t), sqrt(3.25*3.25-(ii-10.6)*(ii-10.6))*sin(t), ones(size(t))*ii, 'w-')
    drawnow;
    movie(f) = getframe(gcf);
    f = f+1;
end

% そで
for ii = (0:delta:0.7)
    plot3(ones(size(t))*( 3.25+ii), sin(t), ones(size(t))*(9-ii/2)+cos(t), 'b-')
    plot3(ones(size(t))*(-3.25-ii), sin(t), ones(size(t))*(9-ii/2)+cos(t), 'b-')
    drawnow;
    movie(f) = getframe(gcf);
    f = f+1;
end
% うで
for ii = (0:delta:1.6)
    plot3(ones(size(t))*( 3.25+ii), 0.8*sin(t), ones(size(t))*(9-ii/2)+0.8*cos(t), 'w-')
    plot3(ones(size(t))*(-3.25-ii), 0.8*sin(t), ones(size(t))*(9-ii/2)+0.8*cos(t), 'w-')
    drawnow;
    movie(f) = getframe(gcf);
    f = f+1;
end
for ii = (1.6:delta:1.8)
    plot3(ones(size(t))*( 3.25+ii), (2-ii)*2*sin(t), ones(size(t))*(9-ii/2)+(2-ii)*2*cos(t), 'w-')
    plot3(ones(size(t))*(-3.25-ii), (2-ii)*2*sin(t), ones(size(t))*(9-ii/2)+(2-ii)*2*cos(t), 'w-')
    drawnow;
    movie(f) = getframe(gcf);
    f = f+1;
end

% 目
plot3(ones(size(t))*( 1)+0.6*cos(t), ones(size(t))*(-3.25), ones(size(t))*12+0.8*sin(t), '-k','LineWidth',2)
plot3(ones(size(t))*(-1)+0.6*cos(t), ones(size(t))*(-3.25), ones(size(t))*12+0.8*sin(t), '-k','LineWidth',2)
drawnow;
movie(f) = getframe(gcf);
f = f+1;
for ii=(0:0.1:1)
    plot3(ones(size(t))*( 1)+0.4*ii*cos(t), ones(size(t))*(-3.25), ones(size(t))*12.3+0.4*ii*sin(t), '-k','LineWidth',2)
    plot3(ones(size(t))*(-1)+0.4*ii*cos(t), ones(size(t))*(-3.25), ones(size(t))*12.3+0.4*ii*sin(t), '-k','LineWidth',2)
end
drawnow;
movie(f) = getframe(gcf);
f = f+1;

% 口
for ii=(0:0.1:0.8)
    plot3([-ii*0.9 ii*0.9], [-3.25 -3.25], [9.4+ii 9.4+ii], '-r','LineWidth',2)
    drawnow;
    movie(f) = getframe(gcf);
    f = f+1;
end
 
plot3([-0.8 0.8], [-3.25 -3.25], [10.2 10.2], '-k','LineWidth',2)
plot3([-0.8 0],   [-3.25 -3.25], [10.2 9.4],  '-k','LineWidth',2)
plot3([0.8 0],    [-3.25 -3.25], [10.2 9.4],  '-k','LineWidth',2)
drawnow;
movie(f) = getframe(gcf);
f = f+1;

% 頬
plot3( 1,   -3.25, 10.8, 'r.','MarkerSize',15)
plot3(-1,   -3.25, 10.8, 'r.','MarkerSize',15)
plot3( 1.5, -3.25, 10.9, 'r.','MarkerSize',15)
plot3(-1.5, -3.25, 10.9, 'r.','MarkerSize',15)
drawnow;
movie(f) = getframe(gcf);
f = f+1;

% 回転
for ii = (0:10:360)
view([ii 22])
drawnow;
movie(f) = getframe(gcf);
f = f+1;
end

% 描画終わり

% 動画書き出し
% ちょっと時間がかかります
v = VideoWriter('YuruChara.mp4', 'MPEG-4');
v.FrameRate = 30;
open(v);
writeVideo(v, movie)
close(v);

###余談
このゆるキャラ、五月祭とか駒場祭とかに行くと本物に会えるとか会えないとか。
(昨年は残念ながら無理でしたが)

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?