easy-three

create.shape

create.shape(props : Object) : Mesh

props - 設定オブジェクト。
  • shapes (Array) : シェイプの配列 (デフォルト : [])。
  • position (Array) : 位置 (デフォルト : [0, 0, 0])。
  • rotation (Array) : 回転 (デフォルト : [0, 0, 0])。
  • option (Object) : オプション (デフォルト : {color: Default.color })。
  • material (String | Material | Material[]) : マテリアルタイプ、またはマテリアルオブジェクト、またはマテリアルオブジェクトの配列 (デフォルト :Default.material)。
  • castShadow (Boolean) : 別のオブジェクトに影を落とすかどうか (デフォルト : true)。
  • receiveShadow (Boolean) : 別のオブジェクトからの影を受けるかどうか (デフォルト : true)。
  • doubleSide (Boolean) : 両面表示にするか? (デフォルト : false)。
  • upsideDown (Boolean) : 裏面表示にするか? (デフォルト : false)。
  • autoAdd (Boolean) : 自動でシーンに追加 (デフォルト : true)。

任意の形のシェイプを作成してシーンに追加します。

直線を用いる場合、shapes には position として 2要素 (x, y) の配列をもつオブジェクトの配列を指定します。

create.shape({
  shapes: [
    { position: [0, 0] },
    { position: [1, 0] },
    { position: [1, 1] },
    { position: [0, 1] }
  ]
})

ベジエ曲線を用いる場合、shapes には position として 6要素 (cp1X, cp1Y, cp2X, cp2Y, x, y) の配列をもつオブジェクトの配列を指定し、 type に "curve" を指定します。
この場合、cp1 は制御点1(始点側の制御点)、cp2 は制御点2(終点側の制御点)、x, y は終点を表します。

create.shape({
  shapes: [
    { position: [0, 0] },
    { position: [0.2, 0.3, 0.8, 0.3, 1, 0], type: "curve" },
    { position: [0.7, 0.2, 0.7, 0.8, 1, 1], type: "curve" },
    { position: [0.8, 0.7, 0.2, 0.7, 0, 1], type: "curve" },
    { position: [0.3, 0.8, 0.3, 0.2, 0, 0], type: "curve" }
  ]
})

コードの例

直線によるシェイプ

const { camera, create, animate, THREE, Default } = init()
camera.position.set(0, 0, 2)
create.ambientLight()
create.directionalLight()

const shape = create.shape({
  shapes: [
    { position: [0, 0] },
    { position: [1, 0] },
    { position: [1, 1] },
    { position: [0, 1] }
  ],
  option: {
    color: Default.color,
    side: THREE.DoubleSide
  }
});

animate(({ delta }) => {
  shape.rotation.x += delta;
  shape.rotation.y += delta;
});

ベジエ曲線を使ったシェイプ

const { camera, create, animate, THREE, Default } = init()
camera.position.set(0, 0, 2)
create.ambientLight()
create.directionalLight()

const shape = create.shape({
  shapes: [
    { position: [0, 0] },
    { position: [0.2, 0.3, 0.8, 0.3, 1, 0], type: "curve" },
    { position: [0.7, 0.2, 0.7, 0.8, 1, 1], type: "curve" },
    { position: [0.8, 0.7, 0.2, 0.7, 0, 1], type: "curve" },
    { position: [0.3, 0.8, 0.3, 0.2, 0, 0], type: "curve" }
  ],
  option: {
    color: Default.color,
    side: THREE.DoubleSide,
  },
});

animate(({ delta }) => {
  shape.rotation.x += delta;
  shape.rotation.y += delta;
})