/**************************************************************************** Copyright (c) 2013-2016 Chukong Technologies Inc. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. http://www.cocos2d-x.org Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ // @ts-check import renderer from '../../renderer/index'; import { toRadian } from '../../core/vmath'; import { ccclass, menu, property, executeInEditMode } from "../../core/data/class-decorator"; import { Color, Enum, Rect } from '../../core/value-types'; import enums from '../../renderer/enums'; import Component from '../../components/CCComponent'; /** * @typedef {import('../../core/value-types/index').Color} Color */ /** * !#en The light source type * * !#ch 光源类型 * @static * @enum CameraComponent.Projection */ let CameraProjection = Enum({ /** * !#en The orthogonal camera * * !#ch 正交相机 * @property Ortho * @readonly * @type {Number} */ ORTHO: 0, /** * !#en The perspective camera * * !#ch 透视相机 * @property Perspective * @readonly * @type {Number} */ PERSPECTIVE: 1 }); let CameraClearFlag = Enum({ SKYBOX: enums.CLEAR_SKYBOX | enums.CLEAR_DEPTH | enums.CLEAR_STENCIL, SOLID_COLOR: enums.CLEAR_COLOR | enums.CLEAR_DEPTH | enums.CLEAR_STENCIL, DEPTH_ONLY: enums.CLEAR_DEPTH | enums.CLEAR_STENCIL, DONT_CLEAR: 0 }); /** * !#en The Camera Component * * !#ch 相机组件 * @class CameraComponent * @extends Component */ @ccclass('cc.CameraComponent') @menu('Components/CameraComponent') @executeInEditMode export default class CameraComponent extends Component { @property _projection = CameraProjection.PERSPECTIVE; @property _priority = 0; @property _fov = 45; @property _orthoHeight = 10; @property _near = 0.01; @property _far = 1000.0; @property _color = cc.color('#334C78'); @property _depth = 1; @property _stencil = 0; @property _clearFlags = CameraClearFlag.SOLID_COLOR; @property _rect = new Rect(0, 0, 1, 1); /** * !#en The projection type of the camera * * !#ch 相机的投影类型 * @type {Number} */ @property({ type: CameraProjection }) get projection() { return this._projection; } set projection(val) { this._projection = val; let type = renderer.PROJ_PERSPECTIVE; if (this._projection === CameraProjection.Ortho) { type = renderer.PROJ_ORTHO; } this._camera.setType(type); } /** * !#en The camera priority * * !#ch 相机的优先级,优先级低的优先渲染 * @type {Number} */ @property get priority() { return this._priority; } set priority(val) { this._priority = val; this._camera.setPriority(val); } /** * !#en The camera field of view * * !#ch 相机的视野 * @type {Number} */ @property get fov() { return this._fov; } set fov(val) { this._fov = val; this._camera.setFov(toRadian(val)); } /** * !#en The camera height when in orthogonal mode * * !#ch 在正交模式下的相机高度 * @type {Number} */ @property get orthoHeight() { return this._orthoHeight; } set orthoHeight(val) { this._orthoHeight = val; this._camera.setOrthoHeight(val); } /** * !#en The near clipping distance of the camera * * !#ch 相机的最近剪切距离 * @type {Number} */ @property get near() { return this._near; } set near(val) { this._near = val; this._camera.setNear(val); } /** * !#en The far clipping distance of the camera * * !#ch 相机的最近剪切距离 * @type {Number} */ @property get far() { return this._far; } set far(val) { this._far = val; this._camera.setFar(val); } /** * !#en The clearing color of the camera * * !#ch 在没有天空盒的情况下的屏幕颜色 * @type {Color} */ @property get color() { return this._color; } set color(val) { this._color = val; this._camera.setColor(val.r / 255, val.g / 255, val.b / 255, val.a / 255); } /** * !#en The clearing depth value of the camera * * !#ch 清除相机的深度值 * @type {Number} */ @property get depth() { return this._depth; } set depth(val) { this._depth = val; this._camera.setDepth(val); } /** * !#en The clearing stencil value of the camera * * !#ch 清除相机的模板值 * @type {Number} */ @property get stencil() { return this._stencil; } set stencil(val) { this._stencil = val; this._camera.setStencil(val); } /** * !#en The clearing flags of this camera * * !#ch 清除相机标志位 * @type {Number} */ @property({ type: CameraClearFlag }) get clearFlags() { return this._clearFlags; } set clearFlags(val) { this._clearFlags = val; this._camera.setClearFlags(val); } /** * !#en The screen rect of the camera * * !#ch 相机的屏幕矩形 * @type {Rect} */ @property get rect() { return this._rect; } set rect(val) { this._rect = val; this._camera.setRect(val.x, val.y, val.width, val.height); } static Projection = CameraProjection; constructor() { super(); } onLoad() { this._camera = this._renderScene.createCamera(this.name); this.projection = this._projection; this.priority = this._priority; this.fov = this._fov; this.orthoHeight = this._orthoHeight; this.near = this._near; this.far = this._far; this.color = this._color; this.depth = this._depth; this.stencil = this._stencil; this.clearFlags = this._clearFlags; this.rect = this._rect; this._camera.setStages(renderer.PassStage.DEFAULT | renderer.PassStage.FORWARD); this._camera.setNode(this.node); } onEnable() { this._camera._enable = this.enabled; } onDisable() { this._camera._enable = this.enabled; } onDestroy() { this._renderScene.destroyCamera(this._camera); } /** * transform a screen position to world space * @param out the resulting vector * @param screenPos the screen position to be transformed * @param width framebuffer width * @param height framebuffer height * @returns the resulting vector */ screenToWorld(out, screenPos, width, height) { return this._camera.screenToWorld(out, screenPos, width, height); } }