mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-03 17:42:41 +08:00
Vendored
+64
-6
File diff suppressed because one or more lines are too long
@@ -1,458 +0,0 @@
|
||||
/****************************************************************************
|
||||
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.
|
||||
****************************************************************************/
|
||||
import { Color, Enum } from '../../core/value-types';
|
||||
import { toRadian } from '../../core/vmath';
|
||||
import { ccclass, menu, property, executeInEditMode } from "../../core/data/class-decorator";
|
||||
import { Component } from '../../components/component';
|
||||
|
||||
/**
|
||||
* !#en The light source type
|
||||
*
|
||||
* !#ch 光源类型
|
||||
* @static
|
||||
* @enum LightComponent.Type
|
||||
*/
|
||||
const LightType = Enum({
|
||||
/**
|
||||
* !#en The direction of light
|
||||
*
|
||||
* !#ch 平行光
|
||||
* @property Directional
|
||||
* @readonly
|
||||
* @type {Number}
|
||||
*/
|
||||
Directional: 0,
|
||||
/**
|
||||
* !#en The point of light
|
||||
*
|
||||
* !#ch 点光源
|
||||
* @property Point
|
||||
* @readonly
|
||||
* @type {Number}
|
||||
*/
|
||||
Point: 1,
|
||||
/**
|
||||
* !#en The spot of light
|
||||
*
|
||||
* !#ch 聚光灯
|
||||
* @property Spot
|
||||
* @readonly
|
||||
* @type {Number}
|
||||
*/
|
||||
Spot: 2,
|
||||
});
|
||||
|
||||
/**
|
||||
* !#en The shadow type
|
||||
*
|
||||
* !#ch 阴影类型
|
||||
* @static
|
||||
* @enum LightComponent.ShadowType
|
||||
*/
|
||||
const LightShadowType = Enum({
|
||||
/**
|
||||
* !#en No shadows
|
||||
*
|
||||
* !#ch 阴影关闭
|
||||
* @property None
|
||||
* @readonly
|
||||
* @type {Number}
|
||||
*/
|
||||
None: 0,
|
||||
/**
|
||||
* !#en Soft shadows
|
||||
*
|
||||
* !#ch 软阴影
|
||||
* @property Soft
|
||||
* @readonly
|
||||
* @type {Number}
|
||||
*/
|
||||
Soft: 1,
|
||||
/**
|
||||
* !#en Hard shadows
|
||||
*
|
||||
* !#ch 阴硬影
|
||||
* @property Hard
|
||||
* @readonly
|
||||
* @type {Number}
|
||||
*/
|
||||
Hard: 2,
|
||||
});
|
||||
|
||||
/**
|
||||
* !#en The Light Component
|
||||
*
|
||||
* !#ch 光源组件
|
||||
* @class LightComponent
|
||||
* @extends Component
|
||||
*/
|
||||
@ccclass('cc.LightComponent')
|
||||
@menu('Components/LightComponent')
|
||||
@executeInEditMode
|
||||
export default class LightComponent extends Component {
|
||||
@property
|
||||
_type = LightType.Directional;
|
||||
|
||||
@property
|
||||
_color = Color.WHITE;
|
||||
|
||||
@property
|
||||
_intensity = 1;
|
||||
|
||||
@property
|
||||
_range = 1;
|
||||
|
||||
@property
|
||||
_spotAngle = 60;
|
||||
|
||||
@property
|
||||
_spotExp = 1;
|
||||
|
||||
@property
|
||||
_shadowType = LightShadowType.None;
|
||||
|
||||
@property
|
||||
_shadowResolution = 1024;
|
||||
|
||||
@property
|
||||
_shadowDarkness = 0.5;
|
||||
|
||||
@property
|
||||
_shadowMinDepth = 1;
|
||||
|
||||
@property
|
||||
_shadowMaxDepth = 1000;
|
||||
|
||||
@property
|
||||
_shadowDepthScale = 250;
|
||||
|
||||
@property
|
||||
_shadowFrustumSize = 50;
|
||||
|
||||
@property
|
||||
_shadowBias = 0.0005;
|
||||
|
||||
/**
|
||||
* !#en The light source type
|
||||
*
|
||||
* !#ch 光源类型
|
||||
*
|
||||
* @type {Number}
|
||||
*/
|
||||
@property({
|
||||
type: LightType
|
||||
})
|
||||
get type() {
|
||||
return this._type;
|
||||
}
|
||||
|
||||
set type(val) {
|
||||
this._type = val;
|
||||
|
||||
let type = renderer.LIGHT_DIRECTIONAL;
|
||||
if (this._type === LightType.Point) {
|
||||
type = renderer.LIGHT_POINT;
|
||||
} else if (this._type === LightType.Spot) {
|
||||
type = renderer.LIGHT_SPOT;
|
||||
}
|
||||
if (this._light)
|
||||
this._light.setType(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en The light source color
|
||||
*
|
||||
* !#ch 光源颜色
|
||||
* @type {Color}
|
||||
*/
|
||||
@property
|
||||
get color() {
|
||||
return this._color;
|
||||
}
|
||||
|
||||
set color(val) {
|
||||
this._color = val;
|
||||
if (this._light)
|
||||
this._light.setColor(val.r / 255, val.g / 255, val.b / 255);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en The light source intensity
|
||||
*
|
||||
* !#ch 光源强度
|
||||
* @type {Number}
|
||||
*/
|
||||
@property
|
||||
get intensity() {
|
||||
return this._intensity;
|
||||
}
|
||||
|
||||
set intensity(val) {
|
||||
this._intensity = val;
|
||||
if (this._light)
|
||||
this._light.setIntensity(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en The light range, used for spot and point light
|
||||
*
|
||||
* !#ch 针对聚光灯和点光源设置光源范围
|
||||
* @type {Number}
|
||||
*/
|
||||
@property
|
||||
get range() {
|
||||
return this._range;
|
||||
}
|
||||
|
||||
set range(val) {
|
||||
this._range = val;
|
||||
if (this._light)
|
||||
this._light.setRange(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en The spot light cone angle
|
||||
*
|
||||
* !#ch 聚光灯锥角
|
||||
* @type {Number}
|
||||
*/
|
||||
@property
|
||||
get spotAngle() {
|
||||
return this._spotAngle;
|
||||
}
|
||||
|
||||
set spotAngle(val) {
|
||||
this._spotAngle = val;
|
||||
if (this._light)
|
||||
this._light.setSpotAngle(toRadian(val));
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en The spot light exponential
|
||||
*
|
||||
* !#ch 聚光灯指数
|
||||
* @type {Number}
|
||||
*/
|
||||
@property
|
||||
get spotExp() {
|
||||
return this._spotExp;
|
||||
}
|
||||
|
||||
set spotExp(val) {
|
||||
this._spotExp = val;
|
||||
if (this._light)
|
||||
this._light.setSpotExp(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en The shadow type
|
||||
*
|
||||
* !#ch 阴影类型
|
||||
* @type {Number} shadowType
|
||||
*/
|
||||
@property({
|
||||
type: LightShadowType
|
||||
})
|
||||
get shadowType() {
|
||||
return this._shadowType;
|
||||
}
|
||||
|
||||
set shadowType(val) {
|
||||
this._shadowType = val;
|
||||
|
||||
let type = renderer.SHADOW_NONE;
|
||||
if (val === LightShadowType.Hard) {
|
||||
type = renderer.SHADOW_HARD;
|
||||
} else if (val === LightShadowType.Soft) {
|
||||
type = renderer.SHADOW_SOFT;
|
||||
}
|
||||
if (this._light)
|
||||
this._light.setShadowType(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en The shadow resolution
|
||||
*
|
||||
* !#ch 阴影分辨率
|
||||
*
|
||||
* @type {Number}
|
||||
*/
|
||||
@property
|
||||
get shadowResolution() {
|
||||
return this._shadowResolution;
|
||||
}
|
||||
|
||||
set shadowResolution(val) {
|
||||
this._shadowResolution = val;
|
||||
if (this._light)
|
||||
this._light.setShadowResolution(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en The shadow darkness
|
||||
*
|
||||
* !#ch 阴影灰度值
|
||||
*
|
||||
* @type {Number}
|
||||
*/
|
||||
@property
|
||||
get shadowDarkness() {
|
||||
return this._shadowDarkness;
|
||||
}
|
||||
|
||||
set shadowDarkness(val) {
|
||||
this._shadowDarkness = val;
|
||||
if (this._light)
|
||||
this._light.setShadowDarkness(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en The shadow min depth
|
||||
*
|
||||
* !#ch 阴影最小深度
|
||||
*
|
||||
* @type {Number}
|
||||
*/
|
||||
@property
|
||||
get shadowMinDepth() {
|
||||
return this._shadowMinDepth;
|
||||
}
|
||||
|
||||
set shadowMinDepth(val) {
|
||||
this._shadowMinDepth = val;
|
||||
if (this._light)
|
||||
this._light.setShadowMinDepth(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en The shadow max depth
|
||||
*
|
||||
* !#ch 阴影最大深度
|
||||
*
|
||||
* @type {Number}
|
||||
*/
|
||||
@property
|
||||
get shadowMaxDepth() {
|
||||
return this._shadowMaxDepth;
|
||||
}
|
||||
|
||||
set shadowMaxDepth(val) {
|
||||
this._shadowMaxDepth = val;
|
||||
if (this._light)
|
||||
this._light.setShadowMaxDepth(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en The shadow depth scale
|
||||
*
|
||||
* !#ch 阴影深度比例
|
||||
*
|
||||
* @type {Number}
|
||||
*/
|
||||
@property
|
||||
get shadowDepthScale() {
|
||||
return this._shadowDepthScale;
|
||||
}
|
||||
|
||||
set shadowDepthScale(val) {
|
||||
this._shadowDepthScale = val;
|
||||
if (this._light)
|
||||
this._light.setShadowDepthScale(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en The shadow frustum size
|
||||
*
|
||||
* !#ch 阴影截锥体大小
|
||||
*
|
||||
* @type {Number}
|
||||
*/
|
||||
@property
|
||||
get shadowFrustumSize() {
|
||||
return this._shadowFrustumSize;
|
||||
}
|
||||
|
||||
set shadowFrustumSize(val) {
|
||||
this._shadowFrustumSize = val;
|
||||
if (this._light)
|
||||
this._light.setShadowFrustumSize(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en The shadow bias
|
||||
*
|
||||
* !#ch 阴影偏移量
|
||||
*
|
||||
* @type {Number}
|
||||
*/
|
||||
@property
|
||||
get shadowBias() {
|
||||
return this._shadowBias;
|
||||
}
|
||||
|
||||
set shadowBias(val) {
|
||||
this._shadowBias = val;
|
||||
if (this._light)
|
||||
this._light.setShadowBias(val);
|
||||
}
|
||||
|
||||
static Type = LightType;
|
||||
|
||||
static ShadowType = LightShadowType;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
onLoad() {
|
||||
|
||||
}
|
||||
|
||||
onEnable() {
|
||||
// this._light = this._getRenderScene().createLight(this.name);
|
||||
// this._light.setNode(this.node);
|
||||
this.type = this._type;
|
||||
this.color = this._color;
|
||||
this.intensity = this._intensity;
|
||||
this.range = this._range;
|
||||
this.spotAngle = this._spotAngle;
|
||||
this.spotExp = this._spotExp;
|
||||
this.shadowType = this._shadowType;
|
||||
this.shadowResolution = this._shadowResolution;
|
||||
this.shadowDarkness = this._shadowDarkness;
|
||||
this.shadowMaxDepth = this._shadowMaxDepth;
|
||||
this.shadowDepthScale = this._shadowDepthScale;
|
||||
this.shadowFrustumSize = this._shadowFrustumSize;
|
||||
this.shadowBias = this._shadowBias;
|
||||
}
|
||||
|
||||
onDisable() {
|
||||
// this._getRenderScene().destroyLight(this._light);
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
/****************************************************************************
|
||||
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.
|
||||
****************************************************************************/
|
||||
import { Component } from '../../components/component';
|
||||
import { ccclass, executeInEditMode, menu, property } from '../../core/data/class-decorator';
|
||||
import { Color, Enum } from '../../core/value-types';
|
||||
import { toRadian } from '../../core/vmath';
|
||||
import { Light, LightType } from '../../renderer/scene/light';
|
||||
import { PointLight } from '../../renderer/scene/point-light';
|
||||
import { SpotLight } from '../../renderer/scene/spot-light';
|
||||
|
||||
/**
|
||||
* !#en The light source type
|
||||
*
|
||||
* !#ch 光源类型
|
||||
* @static
|
||||
* @enum LightComponent.Type
|
||||
*/
|
||||
const Type = Enum({
|
||||
/**
|
||||
* !#en The direction of light
|
||||
*
|
||||
* !#ch 平行光
|
||||
* @property Directional
|
||||
* @readonly
|
||||
* @type {Number}
|
||||
*/
|
||||
DIRECTIONAL: LightType.DIRECTIONAL,
|
||||
/**
|
||||
* !#en The point of light
|
||||
*
|
||||
* !#ch 点光源
|
||||
* @property Point
|
||||
* @readonly
|
||||
* @type {Number}
|
||||
*/
|
||||
POINT: LightType.POINT,
|
||||
/**
|
||||
* !#en The spot of light
|
||||
*
|
||||
* !#ch 聚光灯
|
||||
* @property Spot
|
||||
* @readonly
|
||||
* @type {Number}
|
||||
*/
|
||||
SPOT: LightType.SPOT,
|
||||
});
|
||||
|
||||
/**
|
||||
* !#en The shadow type
|
||||
*
|
||||
* !#ch 阴影类型
|
||||
* @static
|
||||
* @enum LightComponent.ShadowType
|
||||
*/
|
||||
const LightShadowType = Enum({
|
||||
/**
|
||||
* !#en No shadows
|
||||
*
|
||||
* !#ch 阴影关闭
|
||||
* @property None
|
||||
* @readonly
|
||||
* @type {Number}
|
||||
*/
|
||||
None: 0,
|
||||
/**
|
||||
* !#en Soft shadows
|
||||
*
|
||||
* !#ch 软阴影
|
||||
* @property Soft
|
||||
* @readonly
|
||||
* @type {Number}
|
||||
*/
|
||||
Soft: 1,
|
||||
/**
|
||||
* !#en Hard shadows
|
||||
*
|
||||
* !#ch 阴硬影
|
||||
* @property Hard
|
||||
* @readonly
|
||||
* @type {Number}
|
||||
*/
|
||||
Hard: 2,
|
||||
});
|
||||
|
||||
/**
|
||||
* !#en The Light Component
|
||||
*
|
||||
* !#ch 光源组件
|
||||
* @class LightComponent
|
||||
* @extends Component
|
||||
*/
|
||||
@ccclass('cc.LightComponent')
|
||||
@menu('Components/LightComponent')
|
||||
@executeInEditMode
|
||||
export default class LightComponent extends Component {
|
||||
|
||||
public static Type = Type;
|
||||
public static ShadowType = LightShadowType;
|
||||
|
||||
@property
|
||||
protected _type = Type.DIRECTIONAL;
|
||||
@property
|
||||
protected _color = Color.WHITE;
|
||||
@property
|
||||
protected _intensity = 1;
|
||||
@property
|
||||
protected _range = 10;
|
||||
@property
|
||||
protected _spotAngle = 60;
|
||||
|
||||
// @property
|
||||
// protected _shadowType = LightShadowType.None;
|
||||
// @property
|
||||
// protected _shadowResolution = 1024;
|
||||
// @property
|
||||
// protected _shadowDarkness = 0.5;
|
||||
// @property
|
||||
// protected _shadowMinDepth = 1;
|
||||
// @property
|
||||
// protected _shadowMaxDepth = 1000;
|
||||
// @property
|
||||
// protected _shadowDepthScale = 250;
|
||||
// @property
|
||||
// protected _shadowFrustumSize = 50;
|
||||
// @property
|
||||
// protected _shadowBias = 0.0005;
|
||||
|
||||
protected _light: Light | null = null;
|
||||
protected _colorData = [1, 1, 1];
|
||||
|
||||
/**
|
||||
* !#en The light source type
|
||||
*
|
||||
* !#ch 光源类型
|
||||
*
|
||||
* @type {Number}
|
||||
*/
|
||||
@property({
|
||||
type: Type,
|
||||
})
|
||||
get type () {
|
||||
return this._type;
|
||||
}
|
||||
|
||||
set type (val) {
|
||||
this._type = val;
|
||||
this._destroyLight();
|
||||
this._createLight();
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en The light source color
|
||||
*
|
||||
* !#ch 光源颜色
|
||||
* @type {Color}
|
||||
*/
|
||||
@property
|
||||
get color () {
|
||||
return this._color;
|
||||
}
|
||||
|
||||
set color (val) {
|
||||
this._color = val;
|
||||
this._colorData[0] = val.r * this._intensity;
|
||||
this._colorData[1] = val.g * this._intensity;
|
||||
this._colorData[2] = val.b * this._intensity;
|
||||
if (this._light) {
|
||||
this._light.color = this._colorData;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en The light source intensity
|
||||
*
|
||||
* !#ch 光源强度
|
||||
* @type {Number}
|
||||
*/
|
||||
@property
|
||||
get intensity () {
|
||||
return this._intensity;
|
||||
}
|
||||
|
||||
set intensity (val) {
|
||||
this._intensity = val;
|
||||
this._colorData[0] = this._color.r * val;
|
||||
this._colorData[1] = this._color.g * val;
|
||||
this._colorData[2] = this._color.b * val;
|
||||
if (this._light) {
|
||||
this._light.color = this._colorData;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en The light range, used for spot and point light
|
||||
*
|
||||
* !#ch 针对聚光灯和点光源设置光源范围
|
||||
* @type {Number}
|
||||
*/
|
||||
@property
|
||||
get range () {
|
||||
return this._range;
|
||||
}
|
||||
|
||||
set range (val) {
|
||||
this._range = val;
|
||||
if (this._light && this._light instanceof PointLight || this._light instanceof SpotLight) {
|
||||
this._light.range = val;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en The spot light cone angle
|
||||
*
|
||||
* !#ch 聚光灯锥角
|
||||
* @type {Number}
|
||||
*/
|
||||
@property
|
||||
get spotAngle () {
|
||||
return this._spotAngle;
|
||||
}
|
||||
|
||||
set spotAngle (val) {
|
||||
this._spotAngle = val;
|
||||
if (this._light && this._light instanceof SpotLight) {
|
||||
this._light.spotAngle = toRadian(val);
|
||||
}
|
||||
}
|
||||
|
||||
public onEnable () {
|
||||
if (this._light) { this._light.enabled = true; return; }
|
||||
this._createLight();
|
||||
}
|
||||
|
||||
public onDisable () {
|
||||
if (this._light) { this._light.enabled = false; }
|
||||
}
|
||||
|
||||
public onDestroy () {
|
||||
this._destroyLight();
|
||||
}
|
||||
|
||||
protected _createLight () {
|
||||
if (!this.node._scene || this._light) { return; }
|
||||
const scene = this._getRenderScene();
|
||||
switch (this._type) {
|
||||
case Type.DIRECTIONAL:
|
||||
this._light = scene.createDirectionalLight(this.name);
|
||||
break;
|
||||
case Type.POINT:
|
||||
this._light = scene.createPointLight(this.name);
|
||||
this.range = this._range;
|
||||
break;
|
||||
case Type.SPOT:
|
||||
this._light = scene.createSpotLight(this.name);
|
||||
this.range = this._range;
|
||||
this.spotAngle = this._spotAngle;
|
||||
break;
|
||||
}
|
||||
if (!this._light) { console.warn('we don\'t support this many lights in forward pipeline.'); }
|
||||
this.color = this._color;
|
||||
}
|
||||
|
||||
protected _destroyLight () {
|
||||
if (!this.node._scene || !this._light) { return; }
|
||||
const scene = this._getRenderScene();
|
||||
switch (this._type) {
|
||||
case Type.DIRECTIONAL:
|
||||
scene.destroyDirectionalLight(this._light);
|
||||
break;
|
||||
case Type.POINT:
|
||||
scene.destroyPointLight(this._light);
|
||||
break;
|
||||
case Type.SPOT:
|
||||
scene.destroySpotLight(this._light);
|
||||
break;
|
||||
}
|
||||
this._light = null;
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
import { builtinResMgr } from '../../3d/builtin';
|
||||
import { ccclass, executeInEditMode, executionOrder, menu, property } from '../../core/data/class-decorator';
|
||||
import Enum from '../../core/value-types/enum';
|
||||
import { Model } from '../../renderer/scene/model';
|
||||
@@ -196,15 +197,15 @@ export class ModelComponent extends RenderableComponent {
|
||||
this._model = null;
|
||||
}
|
||||
|
||||
if (this._model) {
|
||||
this._model.enabled = this.enabledInHierarchy;
|
||||
}
|
||||
|
||||
this._model = this._createModel();
|
||||
|
||||
this._model!.createBoundingShape(this._mesh.minPosition, this._mesh.maxPosition);
|
||||
|
||||
this._updateModelParams();
|
||||
|
||||
if (this._model) {
|
||||
this._model.enabled = this.enabledInHierarchy;
|
||||
}
|
||||
}
|
||||
|
||||
protected _createModel () {
|
||||
|
||||
@@ -25,14 +25,23 @@ export class UBOGlobal {
|
||||
public static MAT_VIEW_PROJ_OFFSET: number = UBOGlobal.MAT_PROJ_INV_OFFSET + 16;
|
||||
public static MAT_VIEW_PROJ_INV_OFFSET: number = UBOGlobal.MAT_VIEW_PROJ_OFFSET + 16;
|
||||
public static CAMERA_POS_OFFSET: number = UBOGlobal.MAT_VIEW_PROJ_INV_OFFSET + 16;
|
||||
public static COUNT: number = UBOGlobal.CAMERA_POS_OFFSET + 4;
|
||||
public static DIR_LIGHT_DIR_OFFSET: number = UBOGlobal.CAMERA_POS_OFFSET + 4;
|
||||
public static DIR_LIGHT_COLOR_OFFSET: number = UBOGlobal.DIR_LIGHT_DIR_OFFSET + 16;
|
||||
public static POINT_LIGHT_POS_RANGE_OFFSET: number = UBOGlobal.DIR_LIGHT_COLOR_OFFSET + 16;
|
||||
public static POINT_LIGHT_COLOR_OFFSET: number = UBOGlobal.POINT_LIGHT_POS_RANGE_OFFSET + 16;
|
||||
public static SPOT_LIGHT_POS_RANGE_OFFSET: number = UBOGlobal.POINT_LIGHT_COLOR_OFFSET + 16;
|
||||
public static SPOT_LIGHT_DIR_OFFSET: number = UBOGlobal.SPOT_LIGHT_POS_RANGE_OFFSET + 16;
|
||||
public static SPOT_LIGHT_COLOR_OFFSET: number = UBOGlobal.SPOT_LIGHT_DIR_OFFSET + 16;
|
||||
public static COUNT: number = UBOGlobal.SPOT_LIGHT_COLOR_OFFSET + 16;
|
||||
public static SIZE: number = UBOGlobal.COUNT * 4;
|
||||
|
||||
public static BLOCK: GFXUniformBlock = {
|
||||
binding: 30, name: 'CCGlobal', members: [
|
||||
// constants
|
||||
{ name: 'cc_time', type: GFXType.FLOAT4, count: 1 },
|
||||
{ name: 'cc_screenSize', type: GFXType.FLOAT4, count: 1 },
|
||||
{ name: 'cc_screenScale', type: GFXType.FLOAT4, count: 1 },
|
||||
// transform
|
||||
{ name: 'cc_matView', type: GFXType.MAT4, count: 1 },
|
||||
{ name: 'cc_matViewInv', type: GFXType.MAT4, count: 1 },
|
||||
{ name: 'cc_matProj', type: GFXType.MAT4, count: 1 },
|
||||
@@ -40,6 +49,14 @@ export class UBOGlobal {
|
||||
{ name: 'cc_matViewProj', type: GFXType.MAT4, count: 1 },
|
||||
{ name: 'cc_matViewProjInv', type: GFXType.MAT4, count: 1 },
|
||||
{ name: 'cc_cameraPos', type: GFXType.FLOAT4, count: 1 },
|
||||
// lights
|
||||
{ name: 'cc_dirLightDirection', type: GFXType.FLOAT4, count: 4 },
|
||||
{ name: 'cc_dirLightColor', type: GFXType.FLOAT4, count: 4 },
|
||||
{ name: 'cc_pointLightPositionAndRange', type: GFXType.FLOAT4, count: 4 },
|
||||
{ name: 'cc_pointLightColor', type: GFXType.FLOAT4, count: 4 },
|
||||
{ name: 'cc_spotLightPositionAndRange', type: GFXType.FLOAT4, count: 4 },
|
||||
{ name: 'cc_spotLightDirection', type: GFXType.FLOAT4, count: 4 },
|
||||
{ name: 'cc_spotLightColor', type: GFXType.FLOAT4, count: 4 },
|
||||
],
|
||||
};
|
||||
|
||||
@@ -335,6 +352,24 @@ export abstract class RenderPipeline {
|
||||
_vec4Array[3] = 1.0;
|
||||
this._uboGlobal.view.set(_vec4Array, UBOGlobal.CAMERA_POS_OFFSET);
|
||||
|
||||
const scene = view.camera.scene;
|
||||
const dLights = scene.directionalLights;
|
||||
for (let i = 0; i < dLights.length; i++) {
|
||||
this._uboGlobal.view.set(dLights[i].directionArray, UBOGlobal.DIR_LIGHT_DIR_OFFSET + 4 * i);
|
||||
this._uboGlobal.view.set(dLights[i].colorData, UBOGlobal.DIR_LIGHT_COLOR_OFFSET + 4 * i);
|
||||
}
|
||||
const pLights = scene.pointLights;
|
||||
for (let i = 0; i < pLights.length; i++) {
|
||||
this._uboGlobal.view.set(pLights[i].positionAndRange, UBOGlobal.POINT_LIGHT_POS_RANGE_OFFSET + 4 * i);
|
||||
this._uboGlobal.view.set(pLights[i].colorData, UBOGlobal.POINT_LIGHT_COLOR_OFFSET + 4 * i);
|
||||
}
|
||||
const sLights = scene.spotLights;
|
||||
for (let i = 0; i < sLights.length; i++) {
|
||||
this._uboGlobal.view.set(sLights[i].positionAndRange, UBOGlobal.SPOT_LIGHT_POS_RANGE_OFFSET + 4 * i);
|
||||
this._uboGlobal.view.set(sLights[i].directionArray, UBOGlobal.SPOT_LIGHT_DIR_OFFSET + 4 * i);
|
||||
this._uboGlobal.view.set(sLights[i].colorData, UBOGlobal.SPOT_LIGHT_COLOR_OFFSET + 4 * i);
|
||||
}
|
||||
|
||||
// update ubos
|
||||
this._globalUBO!.update(this._uboGlobal.view);
|
||||
}
|
||||
|
||||
@@ -24,13 +24,17 @@ export class Effect {
|
||||
const passes: Pass[] = new Array(passNum);
|
||||
for (let k = 0; k < passNum; ++k) {
|
||||
const passInfo = tech.passes[k] as IPassInfoFull;
|
||||
const shader = programLib.getGFXShader(cc.game._gfxDevice,
|
||||
passInfo.program, defines && defines[k] || {});
|
||||
const renderPass = cc.director.root.pipeline.getRenderPass(passInfo.stage || RenderPassStage.DEFAULT);
|
||||
const prog = programLib.getTemplate(passInfo.program);
|
||||
passInfo.shader = programLib.getGFXShader(cc.game._gfxDevice,
|
||||
passInfo.program, defines && defines[k] || {})!;
|
||||
passInfo.renderPass = cc.director.root.pipeline.getRenderPass(passInfo.stage || RenderPassStage.DEFAULT);
|
||||
passInfo.blocks = prog.blocks;
|
||||
passInfo.samplers = prog.samplers;
|
||||
passInfo.globals = cc.director.root.pipeline.globalUBO;
|
||||
passInfo.blocks = prog!.blocks;
|
||||
passInfo.samplers = prog!.samplers;
|
||||
if (shader) { passInfo.shader = shader; }
|
||||
else { console.warn(`create shader ${passInfo.program} failed`); }
|
||||
if (renderPass) { passInfo.renderPass = renderPass; }
|
||||
else { console.warn(`illegal pass stage in ${effect.name}`); }
|
||||
const pass = new Pass(cc.game._gfxDevice);
|
||||
pass.initialize(passInfo);
|
||||
passes[k] = pass;
|
||||
|
||||
@@ -199,7 +199,9 @@ export class Pass {
|
||||
}
|
||||
|
||||
public getHandle (name: string) {
|
||||
return this._handleMap[name];
|
||||
const handle = this._handleMap[name];
|
||||
if (!handle) { console.warn(`illegal property name ${name}`); }
|
||||
return handle;
|
||||
}
|
||||
|
||||
public getBinding (name: string) {
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import { Vec3 } from '../../core/value-types';
|
||||
import { vec3 } from '../../core/vmath';
|
||||
import { Light, LightType } from './light';
|
||||
import { RenderScene } from './render-scene';
|
||||
|
||||
export class DirectionalLight extends Light {
|
||||
protected _direction = new Float32Array(4);
|
||||
|
||||
set direction (val: Vec3) {
|
||||
vec3.array(this._direction, val);
|
||||
}
|
||||
get directionArray () {
|
||||
return this._direction;
|
||||
}
|
||||
|
||||
constructor (scene: RenderScene, name: string) {
|
||||
super(scene, name);
|
||||
this._type = LightType.DIRECTIONAL;
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,44 @@
|
||||
import { RenderScene } from './render-scene';
|
||||
|
||||
export enum LightType {
|
||||
UNKNOWN,
|
||||
DIRECTIONAL,
|
||||
POINT,
|
||||
SPOT,
|
||||
}
|
||||
|
||||
export class Light {
|
||||
protected _color = new Float32Array(4);
|
||||
protected _enabled = false;
|
||||
|
||||
public get type (): LightType {
|
||||
return this._type;
|
||||
set enabled (val) {
|
||||
this._enabled = val;
|
||||
}
|
||||
get enabled () {
|
||||
return this._enabled;
|
||||
}
|
||||
|
||||
public get name (): string {
|
||||
set color (val: number[]) {
|
||||
this._color.set(val);
|
||||
}
|
||||
get colorData () {
|
||||
return this._color;
|
||||
}
|
||||
|
||||
get type (): LightType {
|
||||
return this._type;
|
||||
}
|
||||
get name (): string {
|
||||
return this._name;
|
||||
}
|
||||
|
||||
private _scene: RenderScene;
|
||||
private _type: LightType;
|
||||
private _name: string;
|
||||
protected _scene: RenderScene;
|
||||
protected _type: LightType;
|
||||
protected _name: string;
|
||||
|
||||
constructor (scene: RenderScene, type: LightType, name: string) {
|
||||
constructor (scene: RenderScene, name: string) {
|
||||
this._scene = scene;
|
||||
this._type = type;
|
||||
this._name = name;
|
||||
this._type = LightType.UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import { Vec3 } from '../../core/value-types';
|
||||
import { vec3 } from '../../core/vmath';
|
||||
import { Light, LightType } from './light';
|
||||
import { RenderScene } from './render-scene';
|
||||
|
||||
export class PointLight extends Light {
|
||||
protected _positionAndRange = new Float32Array(4);
|
||||
|
||||
set position (val: Vec3) {
|
||||
vec3.array(this._positionAndRange, val);
|
||||
}
|
||||
set range (val: number) {
|
||||
this._positionAndRange[3] = val;
|
||||
}
|
||||
|
||||
get positionAndRange () {
|
||||
return this._positionAndRange;
|
||||
}
|
||||
|
||||
constructor (scene: RenderScene, name: string) {
|
||||
super(scene, name);
|
||||
this._type = LightType.POINT;
|
||||
}
|
||||
}
|
||||
@@ -8,8 +8,11 @@ import { GFXPrimitiveMode } from '../../gfx/define';
|
||||
import { Node } from '../../scene-graph';
|
||||
import { Layers } from '../../scene-graph/layers';
|
||||
import { Camera, ICameraInfo } from './camera';
|
||||
import { Light, LightType } from './light';
|
||||
import { DirectionalLight } from './directional-light';
|
||||
import { Light } from './light';
|
||||
import { Model } from './model';
|
||||
import { PointLight } from './point-light';
|
||||
import { SpotLight } from './spot-light';
|
||||
|
||||
export interface IRenderSceneInfo {
|
||||
name: string;
|
||||
@@ -28,23 +31,31 @@ export interface IRaycastResult {
|
||||
|
||||
export class RenderScene {
|
||||
|
||||
public get root (): Root {
|
||||
get root (): Root {
|
||||
return this._root;
|
||||
}
|
||||
|
||||
public get name (): string {
|
||||
get name (): string {
|
||||
return this._name;
|
||||
}
|
||||
|
||||
public get cameras (): Camera[] {
|
||||
get cameras (): Camera[] {
|
||||
return this._cameras;
|
||||
}
|
||||
|
||||
public get pointLights (): Light[] {
|
||||
get directionalLights (): DirectionalLight[] {
|
||||
return this._directionalLights;
|
||||
}
|
||||
|
||||
get pointLights (): PointLight[] {
|
||||
return this._pointLights;
|
||||
}
|
||||
|
||||
public get models (): Model[] {
|
||||
get spotLights (): SpotLight[] {
|
||||
return this._spotLights;
|
||||
}
|
||||
|
||||
get models (): Model[] {
|
||||
return this._models;
|
||||
}
|
||||
|
||||
@@ -55,7 +66,9 @@ export class RenderScene {
|
||||
private _root: Root;
|
||||
private _name: string = '';
|
||||
private _cameras: Camera[] = [];
|
||||
private _pointLights: Light[] = [];
|
||||
private _pointLights: PointLight[] = [];
|
||||
private _directionalLights: DirectionalLight[] = [];
|
||||
private _spotLights: SpotLight[] = [];
|
||||
private _models: Model[] = [];
|
||||
private _modelId: number = 0;
|
||||
|
||||
@@ -104,8 +117,9 @@ export class RenderScene {
|
||||
return null;
|
||||
}
|
||||
|
||||
public createPointLight (name: string): Light {
|
||||
const light = new Light(this, LightType.POINT, name);
|
||||
public createPointLight (name: string): Light | null {
|
||||
if (this._pointLights.length >= 4) { return null; }
|
||||
const light = new PointLight(this, name);
|
||||
this._pointLights.push(light);
|
||||
return light;
|
||||
}
|
||||
@@ -119,10 +133,50 @@ export class RenderScene {
|
||||
}
|
||||
}
|
||||
|
||||
public createDirectionalLight (name: string): Light | null {
|
||||
if (this._directionalLights.length >= 4) { return null; }
|
||||
const light = new DirectionalLight(this, name);
|
||||
this._directionalLights.push(light);
|
||||
return light;
|
||||
}
|
||||
|
||||
public destroyDirectionalLight (light: Light) {
|
||||
for (let i = 0; i < this._directionalLights.length; ++i) {
|
||||
if (this._directionalLights[i] === light) {
|
||||
this._directionalLights.slice(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public createSpotLight (name: string): Light | null {
|
||||
if (this._spotLights.length >= 4) { return null; }
|
||||
const light = new SpotLight(this, name);
|
||||
this._spotLights.push(light);
|
||||
return light;
|
||||
}
|
||||
|
||||
public destroySpotLight (light: Light) {
|
||||
for (let i = 0; i < this._spotLights.length; ++i) {
|
||||
if (this._spotLights[i] === light) {
|
||||
this._spotLights.slice(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public destroyPointLights () {
|
||||
this._pointLights = [];
|
||||
}
|
||||
|
||||
public destroyDirectionalLights () {
|
||||
this._directionalLights = [];
|
||||
}
|
||||
|
||||
public destroySpotLights () {
|
||||
this._spotLights = [];
|
||||
}
|
||||
|
||||
public createModel<T extends Model> (clazz: new (scene: RenderScene, node: Node) => T, node: Node): T {
|
||||
const model = new clazz(this, node);
|
||||
this._models.push(model);
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import { Vec3 } from '../../core/value-types';
|
||||
import { vec3 } from '../../core/vmath';
|
||||
import { Light, LightType } from './light';
|
||||
import { RenderScene } from './render-scene';
|
||||
|
||||
export class SpotLight extends Light {
|
||||
protected _direction = new Float32Array(4);
|
||||
protected _positionAndRange = new Float32Array(4);
|
||||
|
||||
set position (val: Vec3) {
|
||||
vec3.array(this._positionAndRange, val);
|
||||
}
|
||||
set range (val: number) {
|
||||
this._positionAndRange[3] = val;
|
||||
}
|
||||
set direction (val: Vec3) {
|
||||
vec3.array(this._direction, val);
|
||||
}
|
||||
set spotAngle (val: number) {
|
||||
this._direction[3] = val;
|
||||
}
|
||||
|
||||
get positionAndRange () {
|
||||
return this._positionAndRange;
|
||||
}
|
||||
get directionArray () {
|
||||
return this._direction;
|
||||
}
|
||||
|
||||
constructor (scene: RenderScene, name: string) {
|
||||
super(scene, name);
|
||||
this._type = LightType.DIRECTIONAL;
|
||||
}
|
||||
}
|
||||
@@ -58,13 +58,17 @@
|
||||
cameraNode.addComponent(FirstPersonCamera);
|
||||
const camComp = cameraNode.addComponent(cc.CameraComponent);
|
||||
|
||||
const lightNode = new cc.Node('Light');
|
||||
lightNode.parent = scene;
|
||||
lightNode.setRotationFromEuler(70, 0, 0);
|
||||
const lightComp = lightNode.addComponent(cc.LightComponent);
|
||||
|
||||
const texture = createTexture(cc.game.canvas.getElementsByTagName('img'));
|
||||
|
||||
const material = new cc.Material();
|
||||
material.setDefines({ USE_COLOR: true });
|
||||
// parsed effect file is embedded in cocos/3d/builtin/effects.js
|
||||
material.effectName = 'builtin-effect-unlit';
|
||||
material.setProperty('color', [ cc.Color.RED, cc.Color.BLUE ]);
|
||||
material.effectName = 'builtin-effect-phong';
|
||||
|
||||
const len = 50;
|
||||
const models = [], passes = [];
|
||||
@@ -80,7 +84,7 @@
|
||||
// material instance initialized here
|
||||
passes.push(modelCom.material.passes[0]);
|
||||
}
|
||||
const handle = passes[0].getHandle('color');
|
||||
const handle = passes[0].getHandle('diffuseColor');
|
||||
|
||||
{
|
||||
const material = new cc.Material();
|
||||
@@ -116,8 +120,8 @@
|
||||
const x = Math.cos(rad + t), y = Math.sin(rad + t);
|
||||
let z = m._lpos.z + dt; if (z > len * 2) z = 0;
|
||||
m.setPosition(x * 2, y * 2, z);
|
||||
// color.fromHSV((x + 1) * 0.5, 0.5, 1);
|
||||
// passes[i].setUniform(handle, color);
|
||||
color.fromHSV((x + 1) * 0.5, 0.5, 1);
|
||||
passes[i].setUniform(handle, color);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
+2
-1
@@ -17,7 +17,8 @@
|
||||
"ban-types": false,
|
||||
"no-bitwise": false,
|
||||
"no-empty": false,
|
||||
"radix": false
|
||||
"radix": false,
|
||||
"one-line": false,
|
||||
},
|
||||
"rulesDirectory": []
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user