phaser/src/gameobjects/mesh/Mesh.js

155 lines
4.4 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
2017-05-16 23:07:52 +00:00
var Class = require('../../utils/Class');
var Components = require('../components');
2018-02-06 15:04:20 +00:00
var GameObject = require('../GameObject');
2017-05-16 23:07:52 +00:00
var MeshRender = require('./MeshRender');
2018-02-06 15:04:20 +00:00
/**
2018-02-07 15:27:21 +00:00
* @classdesc
2018-02-06 15:04:20 +00:00
* A Mesh Game Object.
*
* @class Mesh
* @extends Phaser.GameObjects.GameObject
* @memberOf Phaser.GameObjects
* @constructor
* @webglOnly
* @since 3.0.0
*
* @extends Phaser.GameObjects.Components.Alpha
* @extends Phaser.GameObjects.Components.BlendMode
* @extends Phaser.GameObjects.Components.Depth
* @extends Phaser.GameObjects.Components.Flip
* @extends Phaser.GameObjects.Components.GetBounds
* @extends Phaser.GameObjects.Components.Origin
* @extends Phaser.GameObjects.Components.Pipeline
* @extends Phaser.GameObjects.Components.ScaleMode
* @extends Phaser.GameObjects.Components.Size
* @extends Phaser.GameObjects.Components.Texture
* @extends Phaser.GameObjects.Components.Transform
* @extends Phaser.GameObjects.Components.Visible
* @extends Phaser.GameObjects.Components.ScrollFactor
*
* @param {Phaser.Scene} scene - The Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
* @param {number} x - The horizontal position of this Game Object in the world.
* @param {number} y - The vertical position of this Game Object in the world.
2018-02-06 17:17:34 +00:00
* @param {array} vertices - An array containing the vertices data for this Mesh.
* @param {array} uv - An array containing the uv data for this Mesh.
* @param {array} colors - An array containing the color data for this Mesh.
* @param {array} alphas - An array containing the alpha data for this Mesh.
2018-02-06 15:04:20 +00:00
* @param {string} texture - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager.
* @param {string|integer} [frame] - An optional frame from the Texture this Game Object is rendering with.
*/
2017-05-16 23:07:52 +00:00
var Mesh = new Class({
Extends: GameObject,
Mixins: [
Components.Alpha,
Components.BlendMode,
Components.Depth,
2017-05-16 23:07:52 +00:00
Components.Flip,
Components.GetBounds,
Components.Origin,
2018-01-29 21:46:48 +00:00
Components.Pipeline,
2017-05-16 23:07:52 +00:00
Components.ScaleMode,
Components.Size,
Components.Texture,
Components.Transform,
Components.Visible,
2017-06-22 02:19:03 +00:00
Components.ScrollFactor,
2017-05-16 23:07:52 +00:00
MeshRender
],
initialize:
2018-01-23 19:29:47 +00:00
function Mesh (scene, x, y, vertices, uv, colors, alphas, texture, frame)
2017-05-16 23:07:52 +00:00
{
GameObject.call(this, scene, 'Mesh');
2017-05-16 23:07:52 +00:00
this.setTexture(texture, frame);
this.setPosition(x, y);
this.setSizeToFrame();
this.setOrigin();
2018-01-29 21:46:48 +00:00
this.initPipeline('TextureTintPipeline');
2017-05-16 23:07:52 +00:00
2017-05-17 16:12:17 +00:00
if (vertices.length !== uv.length)
{
2018-02-06 15:04:20 +00:00
throw new Error('Mesh Vertex count must match UV count');
2017-05-17 16:12:17 +00:00
}
2017-06-28 08:03:13 +00:00
var verticesUB = (vertices.length / 2) | 0;
if (colors.length > 0 && colors.length < verticesUB)
2017-05-22 19:29:27 +00:00
{
2018-02-06 15:04:20 +00:00
throw new Error('Mesh Color count must match Vertex count');
2017-05-22 19:29:27 +00:00
}
2017-06-28 08:03:13 +00:00
if (alphas.length > 0 && alphas.length < verticesUB)
2017-05-22 19:29:27 +00:00
{
2018-02-06 15:04:20 +00:00
throw new Error('Mesh Alpha count must match Vertex count');
2017-05-22 19:29:27 +00:00
}
var i;
2017-05-22 19:29:27 +00:00
if (colors.length === 0)
{
2017-06-28 08:03:13 +00:00
for (i = 0; i < verticesUB; ++i)
2017-05-22 19:29:27 +00:00
{
colors[i] = 0xFFFFFF;
}
}
if (alphas.length === 0)
{
2017-06-28 08:03:13 +00:00
for (i = 0; i < verticesUB; ++i)
2017-05-22 19:29:27 +00:00
{
alphas[i] = 1.0;
}
}
2017-05-17 16:12:17 +00:00
2018-02-06 15:04:20 +00:00
/**
* [description]
*
* @name Phaser.GameObjects.Mesh#vertices
* @type {Float32Array}
* @since 3.0.0
*/
2017-05-17 16:12:17 +00:00
this.vertices = new Float32Array(vertices);
2018-02-06 15:04:20 +00:00
/**
* [description]
*
* @name Phaser.GameObjects.Mesh#uv
* @type {Float32Array}
* @since 3.0.0
*/
2017-05-17 16:12:17 +00:00
this.uv = new Float32Array(uv);
2018-02-06 15:04:20 +00:00
/**
* [description]
*
* @name Phaser.GameObjects.Mesh#colors
* @type {Uint32Array}
* @since 3.0.0
*/
2017-05-22 19:29:27 +00:00
this.colors = new Uint32Array(colors);
2018-02-06 15:04:20 +00:00
/**
* [description]
*
* @name Phaser.GameObjects.Mesh#alphas
* @type {Float32Array}
* @since 3.0.0
*/
2017-05-22 19:29:27 +00:00
this.alphas = new Float32Array(alphas);
2017-05-16 23:07:52 +00:00
}
});
module.exports = Mesh;