![見出し画像](https://assets.st-note.com/production/uploads/images/53950028/rectangle_large_type_2_cca2b5761d25eed4cc2f31e3cfd78d67.png?width=1200)
お手軽VR「A-Frame」
Web技術で、VRを実現しようとしたら…
昔々でしたら「VRML」とか最近でしたら「Flash」で実現してました。
「HTML5」だと、CSSで可能ですし、JavaScriptのCanvas API・WebGL、three.jsとかを使用すると可能ですね。
ただ初心者がお手軽にするには少々ハードルがあります。そこで思い出したのが「A-Frame」。2015年からMozillaさんが始めたもので、タグと属性を記述すればVRが楽しめるすぐれものです。
1.Webページのテンプレート作成
では、次の雛形を作成しましょう。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>お手軽VR</title>
<script src="https://aframe.io/releases/1.0.4/aframe.min.js"></script>
</head>
<body>
<!-- ① -->
</body>
</html>
A-FrameのJavaScriptライブラリを読み込みます。今回使用するバージョンは1.0.4です。A-Frameのバージョンによって機能が使えたり使えなかったりするので気をつけましょう。
ここからは「<!--①-->」から下に書き込んでいきましょう。
2.場所の確保
VRの場所を確保しましょう。<a-scene></a-scene>を用います。
<body>
<!-- ① -->
<a-scene background="color:black">
<!-- ② -->
</a-scene>
</body>
・background属性:背景色を指定(color:white)
![スクリーンショット 2021-06-05 23.29.04](https://assets.st-note.com/production/uploads/images/53948685/picture_pc_7489f721b5cce79089b114fb75aac4ce.png)
sceneの中の「<!--②-->」から下に光源・それぞれの単純な形「プリミティブ」を記述します。
3.平面
床は平面「<a-plane></a-plane>」で表しましょう
<!--床-->
<a-plane
position="0 0 -3"
rotation="-90 0 0"
width="6"
height="3"
color="#955629"
shadow
></a-plane>
・position:x,y,zの位置("0 0 -3")
・rotation:x,y,z回転("-90 0 0")
・width:幅、height:高さ
・color:色(#〜はカラーコード。16進数で色を表す)
・shadow:影を表示
![スクリーンショット 2021-06-05 23.29.13](https://assets.st-note.com/production/uploads/images/53948723/picture_pc_62ec73716f75780d2129cf207dcb610e.png)
4.立方体
立方体は「<a-box></a-box>」です。
<!--立方体-->
<a-box
position="-2 1 -3"
rotation="0 0 0"
color="lime"
width="1"
height="1"
depth="1"
shadow
></a-box>
・width:幅、height:高さ、depth:奥行き
![スクリーンショット 2021-06-05 23.29.20](https://assets.st-note.com/production/uploads/images/53948744/picture_pc_1182f50e93674e1d97649ad140424c1a.png)
5.光源
光(<a-entity light="〜">)の向きや色を初期値から変更しましょう。
<!--光-->
<a-entity
light="
type:directional;
castShadow:true;
color:white;"
position="-1 1 1"
></a-entity>
<!--反射光-->
<a-entity
light="
type:ambient;
color:gray;"
></a-entity>
・type:directional;:光源、type:ambient;:反射光
・castShadow:true;:光を影に影響させる
![スクリーンショット 2021-06-05 23.29.27](https://assets.st-note.com/production/uploads/images/53948765/picture_pc_83fb8aa210e9e7d96968badf42658bac.png)
6.球体
球体は「<a-sphere></a-sphere>」です
<!--球体-->
<a-sphere
position="0 0.5 -3"
radius="0.5"
color="pink"
shadow
></a-sphere>
・radius:半径
![スクリーンショット 2021-06-05 23.29.34](https://assets.st-note.com/production/uploads/images/53948795/picture_pc_4a2753bed0dd7c15b439648e837f18b6.png)
7.円形
円形は「<a-circle></a-circle>」です。
<!--円形-->
<a-circle
position="0 0.5 -3"
rotation="-90 0 0"
radius="1"
color="gray"
shadow
></a-circle>
![スクリーンショット 2021-06-05 23.29.41](https://assets.st-note.com/production/uploads/images/53948816/picture_pc_f690ef85c858ec95a2cf6cccc078d425.png)
8.角柱
角柱は「<a-cylinder></a-cylinder>」です。
<!--円柱-->
<a-cylinder
position="2 1 -3"
rotation="0 0 0"
radius="0.5"
height="1"
segments-radial="5"
color="yellow"
shadow
></a-cylinder>
・segments-radial:角形(ここでは五角形) 数を増やすと円柱になる
![スクリーンショット 2021-06-05 23.29.49](https://assets.st-note.com/production/uploads/images/53948825/picture_pc_4e3b118b0ee0c960c35f589a8892f46e.png)
9.アニメーション
それぞれのプリミティブを動かしてみましょう。animation属性(animation="〜")を使います
<!--立方体-->
<a-box
position="-2 1 -3"
rotation="0 0 0"
color="lime"
width="1"
height="1"
depth="1"
shadow
animation="
property: depth;
to:2;
dur: 1000;
easing:linear;
loop: true;
dir:alternate;"
></a-box>
・property:アニメーションしたい属性(depth属性:奥行き)
・to:その属性の変化値(1→2)
・dur:アニメーションの周期(ミリ秒。1000は1秒)
・easing:アニメーションのスタイル(linear:等速)
・loop:繰り返し(true:繰り返す)
・dir:方向(alternate:交互に繰り返す)
<!--球体-->
<a-sphere
position="0 0.5 -3"
radius="0.5"
color="pink"
shadow
animation="
property: position;
to:0 1.5 -3;
dur: 2000;
easing:easeInQuad;
loop: true;
dir:alternate;"
></a-sphere>
・property:(position属性:位置)
・to:その属性の変化値("0 0.5 -3"→0 1.5 -3)
・easing:(easeInQuad:自然な状態でゆっくり始まり、加速してゆっくり終わる)
<!--円形-->
<a-circle
position="0 0.5 -3"
rotation="-90 0 0"
radius="1"
color="gray"
shadow
animation="
property: position;
to:0 1.5 -3;
dur: 2000;
easing:easeInQuad;
loop: true;
dir:alternate;"
></a-circle>
・to:その属性の変化値("0 0.5 -3"→0 1.5 -3)球体とともに移動
<!--円柱-->
<a-cylinder
position="2 1 -3"
rotation="0 0 0"
radius="0.5"
height="1"
segments-radial="5"
color="yellow"
shadow
animation="
property:rotation;
to:360 0 0;
dur: 1000;
easing:linear;
loop: true;
dir:normal;"
></a-cylinder>
・property:(rotation属性:回転)
・to:その属性の変化値("0 0 0"→360 0 0)
・dir:(normal:順方向)
10.インスペクタ
Macだと「control+option+i」、Windowsだと「Ctrl+Alt+i」で、インスペクタ(グラフィカル空間で編集できる仕組み)を表示させることができます。検証用に利用ください。(左側:要素リスト、右側:各種値、Back To Scene:VR画面に戻る)
![スクリーンショット 2021-06-24 1.07.12](https://assets.st-note.com/production/uploads/images/55284438/picture_pc_0d653df43b69fdd6c837512a91adc49c.png)
11.スマホで閲覧
VRメガネを取り付けて、スマホを横長にして、「VR」をクリックすれば、素敵なことが起こります。
![画像1](https://assets.st-note.com/production/uploads/images/37608888/picture_pc_26f3498ce24a811a7f37a5375a8ebd04.jpg?width=1200)
さらに興味のある方はオフィシャルページへどうぞ。
【PR】
①初心者向けHTML5対面教室(東京・大阪)
②HTML5の検定にチャレンジ!(オンライン)
この記事が気に入ったらサポートをしてみませんか?