SlideShare une entreprise Scribd logo
1  sur  84
Télécharger pour lire hors ligne
gl.enchant.js で始める
              WebGL 3Dプログラミング
               株式会社ユビキタスエンターテインメント
                  秋葉原リサーチセンター
                      高橋諒




12年4月23日月曜日
自己紹介

              高橋 諒
              @rtsan
              株式会社ユビキタスエンターテインメント
              秋葉原リサーチセンター
              gl.enchant.jsの開発




12年4月23日月曜日
2Dと3Dの違いって?



12年4月23日月曜日
例
         シューティングゲーム


12年4月23日月曜日
オブジェクトの位置は(x, y)で表される


                X


          Y




               (x, y)


12年4月23日月曜日
オブジェクトの位置は(x, y, z)で表される




              Y

                  (x, y, z)
                   X
      Z

12年4月23日月曜日
Z軸が増えた

              X


          Y
                           Y

                               (x, y, z)

              (x, y)            X
                       Z

12年4月23日月曜日
2Dは点回転




12年4月23日月曜日
3Dは軸回転




12年4月23日月曜日
回転が複雑になる




12年4月23日月曜日
gl.enchant.js
                   とは


12年4月23日月曜日
gl.enchant.jsとは

              •enchant.jsのプラグイン
              •WebGLを使ったゲームを作れる
              •インターフェースは2Dとほぼ同じ

12年4月23日月曜日
glMatrix.js


              •行列・ベクトル演算のライブラリ
              •内部で演算に使用している


12年4月23日月曜日
gl.enchant.jsで
                 できること


12年4月23日月曜日
モデルが読み込める




12年4月23日月曜日
基本図形が使える




12年4月23日月曜日
回転が簡単にできる

                      Yaw



                            Pitch
               Roll


12年4月23日月曜日
シェーディングができる



   ambient + diffuse + specular = result
 [0.3, 0.3, 0.3, 1.0]   [0.5, 0.5, 0.5, 1.0]   [0.5, 0.5, 0.5, 1.0]


        0.3                   0.5                    0.5


12年4月23日月曜日
視点が簡単に動かせる




12年4月23日月曜日
物理エンジンが使える




12年4月23日月曜日
gl.enchant.js
                を使ってみる


12年4月23日月曜日
やること
0.サンプル

1.画面にオブジェクトを出す

2.オブジェクトを操作する(移動・回転)

3.視点を操作する

12年4月23日月曜日
まずサンプル



12年4月23日月曜日
Hello, Cube
   1 enchant();
   2 window.onload = function() {
   3     var game = new Game(640, 640);
   4     game.onload = function() {
   5         var scene = new Scene3D();
   6         var box = new Cube();
   7         scene.addChild(box);
   8     };
   9     game.start();
  10 };


12年4月23日月曜日
12年4月23日月曜日
画面にオブジェクトを出す




              プリミティブ   モデル


12年4月23日月曜日
の場合
              プリミティブ




12年4月23日月曜日
primitive.gl.enchant.js

              •基本図形の呼び出し
               箱 → Box(sx, sy, sz),

                 → Cube(s)

               平面→ Plane(s)



12年4月23日月曜日
primitive.gl.enchant.js

              •基本図形の呼び出し
               球 → Sphere(r)

               円筒 → Cylinder(r, h)

               トーラス → Torus(r, r2)



12年4月23日月曜日
12年4月23日月曜日
プリミティブ

    game.onload = function() {
      ......
       var box = new Cube(1);
       ......
       scene.addChild(box);
       ......




12年4月23日月曜日
テクスチャをはる

      ......
      var box = new Cube(1);
      box.mesh.texture =
         new Texture('enchant.png');
      box.mesh.texture.ambient =
         [ 0.8, 0.8, 0.8, 1.0 ];

      scene.addChild(box);
      ......




12年4月23日月曜日
マテリアル
        各パラメータで陰影、反射の具合を設定する




   ambient + diffuse + specular = result
 [0.3, 0.3, 0.3, 1.0]   [0.5, 0.5, 0.5, 1.0]   [0.5, 0.5, 0.5, 1.0]


        0.3                   0.5                    0.5


12年4月23日月曜日
の場合

              モデル




12年4月23日月曜日
collada.gl.enchant.js

              •Collada(.dae)ファイルの読み込み
              •ジョイント,   アニメーションは未対応


              •game.preloadから読み込める

12年4月23日月曜日
モデルの読み込み

    var game = new Game(640, 640);

    game.preload('droid.dae');
    game.onload = function() {
      ......

       var droid =
         game.assets['droid.dae'].clone();

       scene.addChild(droid);
       ......




12年4月23日月曜日
note

              •プリロードされたデータはSprite3D
               のインスタンスとして格納されている


              •clone()かset()で複製して使う

12年4月23日月曜日
オブジェクトを操作する

        •移動
        •回転
        •拡大縮小

12年4月23日月曜日
表示オブジェクトの移動

        •translate(x,   y, z); 


        •x,    y, zプロパティの変更
                                      Y


        •どちらも平行移動
                                          X
                                  Z
12年4月23日月曜日
平行移動だと



                                       Z

              translate(2, 0, 1)
                                   X

12年4月23日月曜日
直感的でない



                                       Z

              translate(2, 0, 1)
                                   X

12年4月23日月曜日
表示オブジェクトの移動
        •forward(s)
              → オブジェクトのZ軸方向移動

        •sidestep(s)
              → オブジェクトのX軸方向移動   Y

        •altitude(s)
              → オブジェクトのY軸方向移動       X
                            Z
12年4月23日月曜日
向きにあった移動



                                   Z

                forward(1.5)
                               X

12年4月23日月曜日
表示オブジェクトの回転
        •rotateRoll(rad)
              → オブジェクトのZ軸回転

        •rotatePitch(rad)
              → オブジェクトのX軸回転       Yaw

        •rotateYaw(rad)
              → オブジェクトのY軸回転             Pitch
                           Roll
12年4月23日月曜日
表示オブジェクトの拡大縮小

        •scaleX,    scaleY, scaleZ
              → 拡大率の指定
                                     Y
        •scale(sx,   sy, sz)
              → 拡大率をかける

                                         X
                                 Z
12年4月23日月曜日
Z軸で縮小すると
              droid.scaleZ = 0.0125;




12年4月23日月曜日
視点を操作する



                      を使います

         (Camera3D)

12年4月23日月曜日
カメラの構造
               カメラの位置




12年4月23日月曜日
カメラの構造
                注視点




12年4月23日月曜日
カメラの構造
               上ベクトル




12年4月23日月曜日
上ベクトルで
              カメラの傾きが決まる




12年4月23日月曜日
12年4月23日月曜日
カメラの移動
              •forward(s)
               → カメラのZ軸方向移動

              •sidestep(s)
               → カメラのX軸方向移動
                                  Z
              •altitude(s)
               → カメラのY軸方向移動
                              X   Y

12年4月23日月曜日
カメラの回転
         •rotateRoll(rad)
              → カメラの視線ベクトル回転

         •rotatePitch(rad)
              → カメラのX軸回転
                                   Z
         •rotateYaw(rad)
              → カメラの上ベクトル回転
                               X   Y

12年4月23日月曜日
視点の操作
  center(0, 0, 0)

    Z
              X

    Y




                  (0, 0, 10)
12年4月23日月曜日
視点の操作
  center(0, 0, 0)

    Z
              X

    Y




   (-10, 0, 10)
12年4月23日月曜日
物理エンジンを使う



12年4月23日月曜日
physics.gl.enchant.js

              •手軽に物理シミュレートが利用できる
              •拘束条件(ジョイント)・軟体
               (布など)には未対応



12年4月23日月曜日
physics.gl.enchant.js


              •ammo.jsが必要
               → javascriptの物理演算ライブラリ

              •primitive.gl.enchant.jsも必要


12年4月23日月曜日
変わるところ


         •Scene3D()    → PhyScene3D()


         •Sprite3D()    → PhySprite3D(rigid)

              → 剛体オブジェクトを渡す




12年4月23日月曜日
剛体

                         0.5




        var rigid = new RigidCube(0.5, 5)


12年4月23日月曜日
変わるところ


         •RigidBox(sx,   sy, sz, mass)

              → 大きさ、質量を設定する




12年4月23日月曜日
Primitive


         •Cube(s)   → PhyCube(s, mass)

              → 剛体オブジェクトは内部で作られる




12年4月23日月曜日
力を加える
  •applyCentralImpulse(px,   py, pz)
       → 物体の中心に力を加える


  •applyImpulse(px,   py, pz, x, y, z)
       → 指定した位置に力を加える


  •clearForces()
       → 物体にかかっている力をリセットする

12年4月23日月曜日
力を加える


              (0, 25, -100)




12年4月23日月曜日
力を加える




12年4月23日月曜日
シミュレートの再生・停止


         •PhyScene3D.play()
              → 物理世界の時間をうごかす

         •PhyScene3D.stop()
              → 物理世界の時間をとめる




12年4月23日月曜日
当たり判定


         •PhySprite3D.contactTest()
              → 物理オブジェクト同士の厳密な当たり
              判定




12年4月23日月曜日
実例



12年4月23日月曜日
とんとんずもうゲーム



12年4月23日月曜日
とんとんずもう

          •飛行機の中で作った(40分くらい)
          •ドロイドくん(.dae)を物理化
          •クリックすることで力を与える(だけ)

12年4月23日月曜日
ドロイドくんの物理化
var rigid =
    new RigidCylinder(0.3, 1, 5);

var player = new PhySprite3D(rigid);

player.addChild(
    game.assets['droid.dae'].clone())

player.childNodes[0].y = -1;
12年4月23日月曜日
剛体を定義
                   r

                       h




  var rigid = new PhyCylinder(0.3, 1, 5)


12年4月23日月曜日
合わせる
         rigid




  var player =
                           player.addChild(...);
     new PhySprite(rigid);

12年4月23日月曜日
ざくざくコイン



12年4月23日月曜日
ざくざくコイン

          •コイン落としゲーム
          •ハッカソンで作った(3∼5時間)
          •ほとんどの処理を物理エンジンに任せる

12年4月23日月曜日
初期化

          •コインを空中にランダムに設置
              •play()すると勝手にセットされる


12年4月23日月曜日
処理

          •押し出しバーを周期的に動かす
              → コインの挙動は物理エンジン任せ


          •落ちたコインは当たり判定で回収

12年4月23日月曜日
tips



12年4月23日月曜日
暗い




12年4月23日月曜日
Q:空を描くには?



12年4月23日月曜日
A:でかい球で世界を包む



12年4月23日月曜日
空
    var sky = new Sphere(50);
    sky.mesh.reverse();
    sky.mesh.texture
       = new Texture('sky.png');
    sky.mesh.texture.ambient
       = [ 1.0, 1.0, 1.0, 1.0 ];
    sky.mesh.texture.diffuse
       = [ 0.0, 0.0, 0.0, 1.0 ];
    sky.mesh.texture.specular
       = [ 0.0, 0.0, 0.0, 1.0 ];
    scene.addChild(box);




12年4月23日月曜日
明るくなった




12年4月23日月曜日

Contenu connexe

En vedette

Disposição das equipes jc 2013
Disposição das equipes jc 2013Disposição das equipes jc 2013
Disposição das equipes jc 2013Major Ribamar
 
Freelance Translator 2.0
Freelance Translator 2.0Freelance Translator 2.0
Freelance Translator 2.0Mike Sekine
 
Volumes of solids of revolution dfs
Volumes of solids of revolution dfsVolumes of solids of revolution dfs
Volumes of solids of revolution dfsFarhana Shaheen
 
John Perry
John PerryJohn Perry
John Perryadubose
 
Significance of Numbers in life Dr. Farhana Shaheen
Significance of Numbers in life Dr. Farhana ShaheenSignificance of Numbers in life Dr. Farhana Shaheen
Significance of Numbers in life Dr. Farhana ShaheenFarhana Shaheen
 
Three dimensional space dfs
Three dimensional space dfsThree dimensional space dfs
Three dimensional space dfsFarhana Shaheen
 
All analysis
All analysisAll analysis
All analysisAmber_
 
Cody Hardy
Cody HardyCody Hardy
Cody Hardyadubose
 
Shelby Cooper
Shelby CooperShelby Cooper
Shelby Cooperadubose
 
Quantification and specification of data corruption in Computer Forensic's in...
Quantification and specification of data corruption in Computer Forensic's in...Quantification and specification of data corruption in Computer Forensic's in...
Quantification and specification of data corruption in Computer Forensic's in...Mariagrazia Cinti
 
Caleb Evans
Caleb EvansCaleb Evans
Caleb Evansadubose
 
Сравнение SEO с интернет маркетингом
Сравнение SEO с интернет маркетингомСравнение SEO с интернет маркетингом
Сравнение SEO с интернет маркетингомОлександр Мілютін
 
Derivatives in graphing-dfs
Derivatives in graphing-dfsDerivatives in graphing-dfs
Derivatives in graphing-dfsFarhana Shaheen
 
enchant js workshop on Calpoly
enchant js workshop  on Calpolyenchant js workshop  on Calpoly
enchant js workshop on CalpolyRyo Shimizu
 
Callie Hodge
Callie HodgeCallie Hodge
Callie Hodgeadubose
 

En vedette (20)

my first ppt
my first pptmy first ppt
my first ppt
 
Tp7
Tp7Tp7
Tp7
 
Disposição das equipes jc 2013
Disposição das equipes jc 2013Disposição das equipes jc 2013
Disposição das equipes jc 2013
 
Freelance Translator 2.0
Freelance Translator 2.0Freelance Translator 2.0
Freelance Translator 2.0
 
Volumes of solids of revolution dfs
Volumes of solids of revolution dfsVolumes of solids of revolution dfs
Volumes of solids of revolution dfs
 
John Perry
John PerryJohn Perry
John Perry
 
Significance of Numbers in life Dr. Farhana Shaheen
Significance of Numbers in life Dr. Farhana ShaheenSignificance of Numbers in life Dr. Farhana Shaheen
Significance of Numbers in life Dr. Farhana Shaheen
 
Three dimensional space dfs
Three dimensional space dfsThree dimensional space dfs
Three dimensional space dfs
 
All analysis
All analysisAll analysis
All analysis
 
Cody Hardy
Cody HardyCody Hardy
Cody Hardy
 
Shelby Cooper
Shelby CooperShelby Cooper
Shelby Cooper
 
Quantification and specification of data corruption in Computer Forensic's in...
Quantification and specification of data corruption in Computer Forensic's in...Quantification and specification of data corruption in Computer Forensic's in...
Quantification and specification of data corruption in Computer Forensic's in...
 
Caleb Evans
Caleb EvansCaleb Evans
Caleb Evans
 
Сравнение SEO с интернет маркетингом
Сравнение SEO с интернет маркетингомСравнение SEO с интернет маркетингом
Сравнение SEO с интернет маркетингом
 
Beauty of numbers
Beauty of numbersBeauty of numbers
Beauty of numbers
 
Derivatives in graphing-dfs
Derivatives in graphing-dfsDerivatives in graphing-dfs
Derivatives in graphing-dfs
 
enchant js workshop on Calpoly
enchant js workshop  on Calpolyenchant js workshop  on Calpoly
enchant js workshop on Calpoly
 
17 семинар
17 семинар17 семинар
17 семинар
 
Jc 2013-notafin2
Jc 2013-notafin2Jc 2013-notafin2
Jc 2013-notafin2
 
Callie Hodge
Callie HodgeCallie Hodge
Callie Hodge
 

Beginning gl.enchant