mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 15:52:40 +08:00
@@ -74,6 +74,10 @@ export class Material extends Asset {
|
||||
@property
|
||||
set technique (val: number) {
|
||||
this._techIdx = val;
|
||||
if (val !== this._techIdx) {
|
||||
this._techIdx = val;
|
||||
this.update();
|
||||
}
|
||||
}
|
||||
get technique () {
|
||||
return this._techIdx;
|
||||
|
||||
@@ -266,6 +266,7 @@ export interface IPrimitive {
|
||||
export interface IRenderingSubmesh {
|
||||
inputAssembler: GFXInputAssembler;
|
||||
primitiveMode: GFXPrimitiveMode;
|
||||
doubleSided?: boolean;
|
||||
}
|
||||
|
||||
export class RenderingMesh {
|
||||
|
||||
@@ -22,12 +22,13 @@
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
import { ray } from '../../3d/geom-utils';
|
||||
import { Component } from '../../components/component';
|
||||
import { ccclass, executeInEditMode, menu, property } from '../../core/data/class-decorator';
|
||||
import { Enum, Rect } from '../../core/value-types';
|
||||
import { Enum, Rect, Vec3 } from '../../core/value-types';
|
||||
import { color4, toRadian } from '../../core/vmath';
|
||||
import { GFXClearFlag } from '../../gfx/define';
|
||||
import { Camera, CameraProjection } from '../../renderer/scene/camera';
|
||||
import { Camera } from '../../renderer/scene/camera';
|
||||
|
||||
/**
|
||||
* !#en The light source type
|
||||
@@ -61,7 +62,7 @@ const CameraClearFlag = Enum({
|
||||
SKYBOX: GFXClearFlag.SKYBOX | GFXClearFlag.DEPTH | GFXClearFlag.STENCIL,
|
||||
SOLID_COLOR: GFXClearFlag.COLOR | GFXClearFlag.DEPTH | GFXClearFlag.STENCIL,
|
||||
DEPTH_ONLY: GFXClearFlag.DEPTH | GFXClearFlag.STENCIL,
|
||||
DONT_CLEAR: 0,
|
||||
DONT_CLEAR: GFXClearFlag.NONE,
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -298,4 +299,22 @@ export class CameraComponent extends Component {
|
||||
public onDestroy () {
|
||||
|
||||
}
|
||||
|
||||
public screenPointToRay (x: number, y: number, out?: ray) {
|
||||
if (!out) { out = ray.create(); }
|
||||
if (this._camera) { this._camera.screenPointToRay(out, x, y); }
|
||||
return out;
|
||||
}
|
||||
|
||||
public worldToScreen (worldPos: Vec3, out?: Vec3) {
|
||||
if (!out) { out = new Vec3(); }
|
||||
if (this._camera) { this._camera.worldToScreen(out, worldPos); }
|
||||
return out;
|
||||
}
|
||||
|
||||
public screenToWorld (screenPos: Vec3, out?: Vec3) {
|
||||
if (!out) { out = this.node.getWorldPosition(); }
|
||||
if (this._camera) { this._camera.screenToWorld(out, screenPos); }
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
import Model from '../../renderer/scene/model';
|
||||
import { Model } from '../../renderer/scene/model';
|
||||
import { ccclass, property, menu, executionOrder, executeInEditMode } from '../../core/data/class-decorator';
|
||||
import { Mesh } from '../assets/mesh';
|
||||
import Enum from '../../core/value-types/enum';
|
||||
@@ -116,7 +116,6 @@ export default class ModelComponent extends RenderableComponent {
|
||||
|
||||
set mesh(val) {
|
||||
this._mesh = val;
|
||||
this._updateModels();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -185,8 +184,7 @@ export default class ModelComponent extends RenderableComponent {
|
||||
}
|
||||
|
||||
_updateModels() {
|
||||
if (!this.enabled)
|
||||
return;
|
||||
if (!this.enabled) return;
|
||||
let meshCount = this._mesh ? this._mesh.subMeshCount : 0;
|
||||
|
||||
for (let i = 0; i < meshCount; ++i) {
|
||||
|
||||
Vendored
+1
@@ -28,5 +28,6 @@ import { CCObject } from '../core/data/object';
|
||||
|
||||
export class RawAsset extends CCObject {
|
||||
_uuid: string;
|
||||
public constructor();
|
||||
static isRawAssetType(ctor: Function): boolean;
|
||||
}
|
||||
|
||||
@@ -222,7 +222,7 @@ class color4 {
|
||||
* @param a - The color.
|
||||
* @return out.
|
||||
*/
|
||||
public static array (out: number[], a: color4) {
|
||||
public static array<Out extends IWritableArrayLike<number>> (out: Out, a: color4) {
|
||||
const scale = (a instanceof cc.Color || a.a > 1) ? 1 / 255 : 1;
|
||||
out[0] = a.r * scale;
|
||||
out[1] = a.g * scale;
|
||||
|
||||
@@ -502,6 +502,7 @@ export interface IGFXColor {
|
||||
}
|
||||
|
||||
export enum GFXClearFlag {
|
||||
NONE = 0,
|
||||
COLOR = 1,
|
||||
DEPTH = 2,
|
||||
STENCIL = 4,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { GFXBuffer } from './buffer';
|
||||
import { GFXCommandBuffer } from './command-buffer';
|
||||
import * as GFXDefines from './define';
|
||||
import { GFXDevice } from './device';
|
||||
import { GFXFramebuffer } from './framebuffer';
|
||||
import { GFXInputAssembler } from './input-assembler';
|
||||
@@ -27,3 +28,5 @@ cc.GFXPipelineState = GFXPipelineState;
|
||||
cc.GFXCommandBuffer = GFXCommandBuffer;
|
||||
cc.GFXQueue = GFXQueue;
|
||||
cc.WebGLGFXDevice = WebGLGFXDevice;
|
||||
|
||||
Object.assign(cc, GFXDefines);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { GFXBindingType, GFXBufferTextureCopy, GFXBufferUsageBit, GFXColorMask,
|
||||
GFXCullMode, GFXFormat, GFXFormatInfos, GFXFormatSize, GFXLoadOp, GFXMemoryUsageBit,
|
||||
GFXShaderType, GFXTextureFlagBit, GFXTextureLayout, GFXTextureViewType,
|
||||
GFXType, IGFXColor, IGFXRect, WebGLEXT} from '../define';
|
||||
GFXType, IGFXColor, IGFXRect, WebGLEXT } from '../define';
|
||||
import { WebGLGFXCommandAllocator } from './webgl-command-allocator';
|
||||
import { WebGLGFXDevice } from './webgl-device';
|
||||
import { WebGLAttrib, WebGLGPUBindingLayout, WebGLGPUBuffer, WebGLGPUFramebuffer,
|
||||
|
||||
@@ -26,7 +26,7 @@ export class ForwardFlow extends RenderFlow {
|
||||
if (!mainWindow || !mainWindow.framebuffer) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
this.createStage(ForwardStage, {
|
||||
name: 'ForwardStage',
|
||||
priority: ForwardStagePriority.FORWARD,
|
||||
|
||||
@@ -48,7 +48,8 @@ export class ForwardStage extends RenderStage {
|
||||
this._renderArea.height = camera.height;
|
||||
|
||||
cmdBuff.begin();
|
||||
cmdBuff.beginRenderPass( this._framebuffer as GFXFramebuffer, this._renderArea, this._clearColors, this._clearDepth, this._clearStencil);
|
||||
cmdBuff.beginRenderPass(this._framebuffer as GFXFramebuffer, this._renderArea,
|
||||
[camera.clearColor], camera.clearDepth, camera.clearStencil);
|
||||
|
||||
cmdBuff.execute(queue.opaqueCmdBuffs);
|
||||
cmdBuff.execute(queue.transparentCmdBuffs);
|
||||
|
||||
@@ -2,10 +2,10 @@ import { Material } from '../../3d/assets/material';
|
||||
import ImageAsset from '../../assets/image-asset';
|
||||
import Texture2D from '../../assets/texture-2d';
|
||||
import { GFXCommandBuffer } from '../../gfx/command-buffer';
|
||||
import { GFXCommandBufferType} from '../../gfx/define';
|
||||
import { GFXCommandBufferType } from '../../gfx/define';
|
||||
import { GFXFramebuffer } from '../../gfx/framebuffer';
|
||||
import { Camera } from '../../renderer/scene/camera';
|
||||
import Model from '../../renderer/scene/model';
|
||||
import { Model } from '../../renderer/scene/model';
|
||||
import { Scene } from '../../scene-graph';
|
||||
import { RenderFlow } from '../render-flow';
|
||||
import { IRenderStageInfo, RenderStage } from '../render-stage';
|
||||
|
||||
@@ -19,8 +19,6 @@ export enum RenderPassStage {
|
||||
DEFAULT = 100,
|
||||
}
|
||||
|
||||
const v_camPos = cc.v3();
|
||||
|
||||
export class UBOGlobal {
|
||||
public static TIME_OFFSET: number = 0;
|
||||
public static SCREEN_SIZE_OFFSET: number = UBOGlobal.TIME_OFFSET + 4;
|
||||
@@ -37,16 +35,16 @@ export class UBOGlobal {
|
||||
|
||||
public static BLOCK: GFXUniformBlock = {
|
||||
binding: 30, name: 'CCGlobal', members: [
|
||||
{ name: 'u_time', type: GFXType.FLOAT4, count: 1 },
|
||||
{ name: 'u_screenSize', type: GFXType.FLOAT4, count: 1 },
|
||||
{ name: 'u_screenScale', type: GFXType.FLOAT4, count: 1 },
|
||||
{ name: 'u_matView', type: GFXType.MAT4, count: 1 },
|
||||
{ name: 'u_matViewInv', type: GFXType.MAT4, count: 1 },
|
||||
{ name: 'u_matProj', type: GFXType.MAT4, count: 1 },
|
||||
{ name: 'u_matProjInv', type: GFXType.MAT4, count: 1 },
|
||||
{ name: 'u_matViewProj', type: GFXType.MAT4, count: 1 },
|
||||
{ name: 'u_matViewProjInv', type: GFXType.MAT4, count: 1 },
|
||||
{ name: 'u_cameraPos', type: GFXType.FLOAT4, count: 1 },
|
||||
{ name: 'cc_time', type: GFXType.FLOAT4, count: 1 },
|
||||
{ name: 'cc_screenSize', type: GFXType.FLOAT4, count: 1 },
|
||||
{ name: 'cc_screenScale', type: GFXType.FLOAT4, count: 1 },
|
||||
{ name: 'cc_matView', type: GFXType.MAT4, count: 1 },
|
||||
{ name: 'cc_matViewInv', type: GFXType.MAT4, count: 1 },
|
||||
{ name: 'cc_matProj', type: GFXType.MAT4, count: 1 },
|
||||
{ name: 'cc_matProjInv', type: GFXType.MAT4, count: 1 },
|
||||
{ name: 'cc_matViewProj', type: GFXType.MAT4, count: 1 },
|
||||
{ name: 'cc_matViewProjInv', type: GFXType.MAT4, count: 1 },
|
||||
{ name: 'cc_cameraPos', type: GFXType.FLOAT4, count: 1 },
|
||||
],
|
||||
};
|
||||
|
||||
@@ -164,7 +162,10 @@ export abstract class RenderPipeline {
|
||||
this._renderPasses.clear();
|
||||
}
|
||||
|
||||
public createFlow<T extends RenderFlow> (clazz: new (pipeline: RenderPipeline) => T, info: IRenderFlowInfo): RenderFlow | null {
|
||||
public createFlow<T extends RenderFlow> (
|
||||
clazz: new (pipeline: RenderPipeline) => T,
|
||||
info: IRenderFlowInfo,
|
||||
): RenderFlow | null {
|
||||
const flow: RenderFlow = new clazz(this);
|
||||
if (flow.initialize(info)) {
|
||||
this._flows.push(flow);
|
||||
@@ -353,7 +354,7 @@ export abstract class RenderPipeline {
|
||||
model._updateTransform();
|
||||
|
||||
// frustum culling
|
||||
if (!intersect.aabb_frustum(model.boundingShape, camera.frustum)) {
|
||||
if (model.worldBounds && !intersect.aabb_frustum(model.worldBounds, camera.frustum)) {
|
||||
// console.log('model is not in view frustum.');
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { vec3 } from '../core/vmath';
|
||||
import { GFXCommandBuffer } from '../gfx/command-buffer';
|
||||
import { Pass } from '../renderer/core/pass';
|
||||
import { Camera } from '../renderer/scene/camera';
|
||||
import Model from '../renderer/scene/model';
|
||||
import { Model } from '../renderer/scene/model';
|
||||
|
||||
const _pos: vec3 = vec3.create();
|
||||
const _cameraPos: vec3 = vec3.create();
|
||||
|
||||
@@ -75,6 +75,7 @@ export class Effect {
|
||||
const prog = getProgram(passInfo.program);
|
||||
passInfo.shader = cc.game._programLib.getGFXShader(passInfo.program, defines || {});
|
||||
passInfo.renderPass = cc.director.root.pipeline.getRenderPass(passInfo.stage || RenderPassStage.DEFAULT);
|
||||
passInfo.globals = cc.director.root.pipeline.globalUBO;
|
||||
passInfo.blocks = prog.blocks;
|
||||
passInfo.samplers = prog.samplers;
|
||||
const pass = new Pass(cc.game._gfxDevice);
|
||||
|
||||
@@ -21,6 +21,7 @@ export interface IPassInfoFull extends IPassInfo {
|
||||
samplers: ISamplerInfo[];
|
||||
shader: GFXShader;
|
||||
renderPass: GFXRenderPass;
|
||||
globals: GFXBuffer;
|
||||
}
|
||||
|
||||
const _type2fn = {
|
||||
@@ -154,6 +155,9 @@ export class Pass {
|
||||
}, 0); // === u.size
|
||||
buffer.update(block.buffer);
|
||||
}
|
||||
// also bind builtin globals
|
||||
this._bindingLayout.bindBuffer(UBOGlobal.BLOCK.binding, info.globals);
|
||||
|
||||
for (const u of info.samplers) {
|
||||
this._handleMap[u.name] = genHandle(GFXBindingType.SAMPLER, u.type, u.binding);
|
||||
const sampler = device.createSampler({
|
||||
@@ -258,5 +262,5 @@ export class Pass {
|
||||
get programName () { return this._programName; }
|
||||
get pipelineState () { return this._pipelineState as GFXPipelineState; }
|
||||
get bindingLayout () { return this._bindingLayout as GFXBindingLayout; }
|
||||
get primitive(): GFXPrimitiveMode { return this._primitive; }
|
||||
get primitive (): GFXPrimitiveMode { return this._primitive; }
|
||||
}
|
||||
|
||||
@@ -8,9 +8,9 @@ import { Effect } from './core/effect';
|
||||
import InputAssembler from './core/input-assembler';
|
||||
import View from './core/view';
|
||||
|
||||
import Light from './scene/light';
|
||||
import { Light } from './scene/light';
|
||||
import { Camera } from './scene/camera';
|
||||
import Model from './scene/model';
|
||||
import { Model } from './scene/model';
|
||||
import Scene from './scene/scene';
|
||||
|
||||
import LineBatchModel from './models/line-batch-model';
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
import { vec3, color3 } from '../../core/vmath';
|
||||
import { RecyclePool } from '../../3d/memop';
|
||||
|
||||
import Model from '../scene/model';
|
||||
import { Model } from '../scene/model';
|
||||
|
||||
export default class LineBatchModel extends Model {
|
||||
constructor() {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
import gfx from '../gfx';
|
||||
import InputAssembler from '../core/input-assembler';
|
||||
|
||||
import Model from '../scene/model';
|
||||
import { Model } from '../scene/model';
|
||||
|
||||
function _createInputAssembler(out) {
|
||||
let vb = new gfx.VertexBuffer(
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
import Model from '../scene/model';
|
||||
import { Model } from '../scene/model';
|
||||
|
||||
export default class SkinningModel extends Model {
|
||||
constructor() {
|
||||
@@ -18,4 +18,4 @@ export default class SkinningModel extends Model {
|
||||
setJointsMatrixArray(a) {
|
||||
this._jointsMatrixArray = a;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
import { RecyclePool } from '../../3d/memop';
|
||||
|
||||
import Model from '../scene/model';
|
||||
import { Model } from '../scene/model';
|
||||
|
||||
export default class SpriteBatchModel extends Model {
|
||||
constructor() {
|
||||
|
||||
@@ -54,7 +54,7 @@ export class Camera {
|
||||
private _frustum: frustum = new frustum();
|
||||
private _node: Node | null = null;
|
||||
private _view: RenderView | null = null;
|
||||
private _visibility: number;
|
||||
private _visibility: number = 0;
|
||||
|
||||
constructor (scene: RenderScene, info: ICameraInfo) {
|
||||
this._scene = scene;
|
||||
@@ -233,43 +233,16 @@ export class Camera {
|
||||
return this._frustum;
|
||||
}
|
||||
|
||||
public set visibility (vis: number) {
|
||||
set visibility (vis) {
|
||||
this._visibility = vis;
|
||||
}
|
||||
public get visibility (): number {
|
||||
get visibility () {
|
||||
return this._visibility;
|
||||
}
|
||||
|
||||
/**
|
||||
* transform a screen position to a world space ray
|
||||
*/
|
||||
public screenPointToRay (x: number, y: number, out: ray): ray {
|
||||
out = out || ray.create();
|
||||
|
||||
const cx = this._viewport.x * this._width;
|
||||
const cy = this._viewport.y * this._height;
|
||||
const cw = this._viewport.w * this._width;
|
||||
const ch = this._viewport.h * this._height;
|
||||
|
||||
// far plane intersection
|
||||
vec3.set(v_a, (x - cx) / cw * 2 - 1, (y - cy) / ch * 2 - 1, 1);
|
||||
vec3.transformMat4(v_a, v_a, this._matViewProjInv);
|
||||
|
||||
if (this._proj === CameraProjection.PERSPECTIVE) {
|
||||
// camera origin
|
||||
if (this._node) { this._node.getWorldPosition(v_b); }
|
||||
} else {
|
||||
// near plane intersection
|
||||
vec3.set(v_b, (x - cx) / cw * 2 - 1, (y - cy) / ch * 2 - 1, -1);
|
||||
vec3.transformMat4(v_b, v_b, this._matViewProjInv);
|
||||
}
|
||||
|
||||
return ray.fromPoints(out, v_b, v_a);
|
||||
}
|
||||
|
||||
public changeTargetDisplay (val: number) {
|
||||
const scene = this._scene;
|
||||
const win = scene.root.windows[val - 1] || scene.root.mainWindow;
|
||||
const win = scene.root.windows[val] || scene.root.mainWindow;
|
||||
if (win) {
|
||||
this._width = win.width;
|
||||
this._height = win.height;
|
||||
@@ -287,14 +260,41 @@ export class Camera {
|
||||
this._aspect = this._width / this._height;
|
||||
}
|
||||
|
||||
/**
|
||||
* transform a screen position to a world space ray
|
||||
*/
|
||||
public screenPointToRay (x: number, y: number, out: ray): ray {
|
||||
out = out || ray.create();
|
||||
|
||||
const cx = this._viewport.x * this._width;
|
||||
const cy = this._viewport.y * this._height;
|
||||
const cw = this._viewport.width * this._width;
|
||||
const ch = this._viewport.height * this._height;
|
||||
|
||||
// far plane intersection
|
||||
vec3.set(v_a, (x - cx) / cw * 2 - 1, (y - cy) / ch * 2 - 1, 1);
|
||||
vec3.transformMat4(v_a, v_a, this._matViewProjInv);
|
||||
|
||||
if (this._proj === CameraProjection.PERSPECTIVE) {
|
||||
// camera origin
|
||||
if (this._node) { this._node.getWorldPosition(v_b); }
|
||||
} else {
|
||||
// near plane intersection
|
||||
vec3.set(v_b, (x - cx) / cw * 2 - 1, (y - cy) / ch * 2 - 1, -1);
|
||||
vec3.transformMat4(v_b, v_b, this._matViewProjInv);
|
||||
}
|
||||
|
||||
return ray.fromPoints(out, v_b, v_a);
|
||||
}
|
||||
|
||||
/**
|
||||
* transform a screen position to world space
|
||||
*/
|
||||
public screenToWorld (out: vec3, screenPos: vec3): vec3 {
|
||||
const cx = this._viewport.x * this._width;
|
||||
const cy = this._viewport.y * this._height;
|
||||
const cw = this._viewport.w * this._width;
|
||||
const ch = this._viewport.h * this._height;
|
||||
const cw = this._viewport.width * this._width;
|
||||
const ch = this._viewport.height * this._height;
|
||||
|
||||
if (this._proj === CameraProjection.PERSPECTIVE) {
|
||||
// calculate screen pos in far clip plane
|
||||
@@ -331,8 +331,8 @@ export class Camera {
|
||||
public worldToScreen (out: vec3, worldPos: vec3): vec3 {
|
||||
const cx = this._viewport.x * this._width;
|
||||
const cy = this._viewport.y * this._height;
|
||||
const cw = this._viewport.w * this._width;
|
||||
const ch = this._viewport.h * this._height;
|
||||
const cw = this._viewport.width * this._width;
|
||||
const ch = this._viewport.height * this._height;
|
||||
|
||||
vec3.transformMat4(out, worldPos, this.matViewProjInv);
|
||||
out.x = cx + (out.x + 1) * 0.5 * cw;
|
||||
|
||||
@@ -47,7 +47,7 @@ function _computePointLightViewProjMatrix(light, outView, outProj) {
|
||||
* A representation of a light source.
|
||||
* Could be a point light, a spot light or a directional light.
|
||||
*/
|
||||
export default class Light {
|
||||
export class Light {
|
||||
/**
|
||||
* Setup a default directional light with no shadows
|
||||
*/
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
import { Material } from '../../3d/assets/material';
|
||||
import { IRenderingSubmesh } from '../../3d/assets/mesh';
|
||||
import { aabb } from '../../3d/geom-utils';
|
||||
import { Vec3 } from '../../core/value-types';
|
||||
import { mat4, vec3 } from '../../core/vmath';
|
||||
import { mat4 } from '../../core/vmath';
|
||||
import { GFXBuffer } from '../../gfx/buffer';
|
||||
import { GFXCommandBuffer } from '../../gfx/command-buffer';
|
||||
import { GFXBufferUsageBit, GFXCommandBufferType, GFXFormat, GFXMemoryUsageBit } from '../../gfx/define';
|
||||
import { GFXInputAssembler, IGFXInputAttribute } from '../../gfx/input-assembler';
|
||||
import { GFXBufferUsageBit, GFXCommandBufferType, GFXMemoryUsageBit } from '../../gfx/define';
|
||||
import { UBOLocal } from '../../pipeline/render-pipeline';
|
||||
import { Node } from '../../scene-graph';
|
||||
import { Effect } from '../core/effect';
|
||||
import { Pass } from '../core/pass';
|
||||
import { RenderScene } from './render-scene';
|
||||
import { IRenderingSubmesh } from '../../3d/assets/mesh';
|
||||
|
||||
const _temp_floatx16 = new Float32Array(16);
|
||||
const _temp_mat4 = mat4.create();
|
||||
@@ -20,7 +19,7 @@ const _temp_mat4 = mat4.create();
|
||||
/**
|
||||
* A representation of a model
|
||||
*/
|
||||
export default class Model {
|
||||
export class Model {
|
||||
|
||||
private _scene: RenderScene | null;
|
||||
private _id: number;
|
||||
@@ -36,8 +35,8 @@ export default class Model {
|
||||
private _cameraID: number;
|
||||
private _userKey: number;
|
||||
private _castShadow: boolean;
|
||||
private _boundingShape: aabb;
|
||||
private _bsModelSpace: aabb;
|
||||
private _worldBounds: aabb;
|
||||
private _modelBounds: aabb;
|
||||
private _material: Material | null;
|
||||
private _cmdBuffers: GFXCommandBuffer[];
|
||||
private _localUniforms: UBOLocal | null;
|
||||
@@ -61,7 +60,6 @@ export default class Model {
|
||||
this._cameraID = -1;
|
||||
this._userKey = -1;
|
||||
this._castShadow = false;
|
||||
this._boundingShape = aabb.create(0, 0, 0, 50, 50, 50);
|
||||
this._material = null;
|
||||
this._cmdBuffers = new Array<GFXCommandBuffer>();
|
||||
this._localUniforms = null;
|
||||
@@ -98,10 +96,10 @@ export default class Model {
|
||||
}
|
||||
|
||||
public _updateTransform () {
|
||||
if (!this._node._hasChanged || !this._boundingShape) { return; }
|
||||
if (!this._node._hasChanged || !this._worldBounds) { return; }
|
||||
this._node.updateWorldTransformFull();
|
||||
this._bsModelSpace.transform(this._node._mat, this._node._pos,
|
||||
this._node._rot, this._node._scale, this._boundingShape);
|
||||
this._modelBounds.transform(this._node._mat, this._node._pos,
|
||||
this._node._rot, this._node._scale, this._worldBounds);
|
||||
}
|
||||
|
||||
public updateRenderData () {
|
||||
@@ -126,8 +124,8 @@ export default class Model {
|
||||
*/
|
||||
public createBoundingShape (minPos: Vec3, maxPos: Vec3) {
|
||||
if (!minPos || !maxPos) { return; }
|
||||
this._bsModelSpace = aabb.fromPoints(aabb.create(), minPos, maxPos);
|
||||
this._boundingShape = aabb.clone(this._bsModelSpace);
|
||||
this._modelBounds = aabb.fromPoints(aabb.create(), minPos, maxPos);
|
||||
this._worldBounds = aabb.clone(this._modelBounds);
|
||||
}
|
||||
|
||||
public enable (isEnable: boolean) {
|
||||
@@ -158,12 +156,18 @@ export default class Model {
|
||||
* Set the input assembler
|
||||
* @param {InputAssembler} ia
|
||||
*/
|
||||
set subMeshData(sm: IRenderingSubmesh) {
|
||||
set subMeshData (sm: IRenderingSubmesh | null) {
|
||||
this._subMeshObject = sm;
|
||||
}
|
||||
get subMeshData () {
|
||||
return this._subMeshObject;
|
||||
}
|
||||
|
||||
get boundingShape (): aabb {
|
||||
return this._boundingShape;
|
||||
get worldBounds (): aabb {
|
||||
return this._worldBounds;
|
||||
}
|
||||
get modelBounds (): aabb {
|
||||
return this._modelBounds;
|
||||
}
|
||||
|
||||
get viewID (): number {
|
||||
@@ -186,11 +190,12 @@ export default class Model {
|
||||
}
|
||||
}
|
||||
|
||||
set material(material: Material) {
|
||||
set material (material: Material) {
|
||||
this._material = material;
|
||||
for (let i = 0; i < this._material.passes.length; i++) {
|
||||
if (this._material.passes[i].primitive !== (this._subMeshObject as IRenderingSubmesh).primitiveMode)
|
||||
if (this._material.passes[i].primitive !== (this._subMeshObject as IRenderingSubmesh).primitiveMode) {
|
||||
cc.error('the model(%d)\'s primitive type doesn\'t match its pass\'s');
|
||||
}
|
||||
this.recordCommandBuffer(i);
|
||||
}
|
||||
for (let i = this._cmdBuffers.length - 1; i >= this._material.passes.length; i--) {
|
||||
@@ -213,7 +218,7 @@ export default class Model {
|
||||
if (this._localUBO) {
|
||||
pass.bindingLayout.bindBuffer(UBOLocal.BLOCK.binding, this._localUBO);
|
||||
}
|
||||
pass.bindingLayout.update();
|
||||
pass.update();
|
||||
this._cmdBuffers[index].begin();
|
||||
this._cmdBuffers[index].bindPipelineState(pass.pipelineState);
|
||||
this._cmdBuffers[index].bindBindingLayout(pass.bindingLayout);
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
import { intersect, ray, triangle } from '../../3d/geom-utils';
|
||||
import { RecyclePool } from '../../3d/memop';
|
||||
import { _createSceneFun, Root } from '../../core/root';
|
||||
import { mat4, vec3 } from '../../core/vmath';
|
||||
import { Layers } from '../../scene-graph/layers';
|
||||
import { Camera, ICameraInfo } from './camera';
|
||||
import Light from './light';
|
||||
import Model from './model';
|
||||
import { Light } from './light';
|
||||
import { Model } from './model';
|
||||
import { GFXPrimitiveMode } from '../../gfx/define';
|
||||
|
||||
export interface IRenderSceneInfo {
|
||||
name: string;
|
||||
@@ -13,6 +18,11 @@ export interface ISceneNodeInfo {
|
||||
// parent: Node;
|
||||
}
|
||||
|
||||
export interface IRaycastResult {
|
||||
node: Node;
|
||||
distance: number;
|
||||
}
|
||||
|
||||
export class RenderScene {
|
||||
|
||||
public get root (): Root {
|
||||
@@ -144,4 +154,64 @@ export class RenderScene {
|
||||
public generateModelId (): number {
|
||||
return this._modelId++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cast a ray into the scene, record all the intersected models in the result array
|
||||
* @param worldRay - the testing ray
|
||||
* @param mask - the layer mask to filter the models
|
||||
* @return the results array
|
||||
*/
|
||||
public raycast (worldRay: ray, mask: number = Layers.RaycastMask): IRaycastResult[] {
|
||||
pool.reset();
|
||||
for (const m of this._models) {
|
||||
const node = m.node;
|
||||
if (!cc.Layers.check(node.layer, mask) || !m.modelBounds) { continue; }
|
||||
// transform ray back to model space
|
||||
mat4.invert(m4, node.getWorldMatrix(m4));
|
||||
vec3.transformMat4(modelRay.o, worldRay.o, m4);
|
||||
vec3.normalize(modelRay.d, vec3.transformMat4Normal(modelRay.d, worldRay.d, m4));
|
||||
// broadphase
|
||||
distance = intersect.ray_aabb(modelRay, m.modelBounds);
|
||||
if (distance <= 0) { continue; }
|
||||
const subModel = m.subMeshData;
|
||||
if (!subModel || subModel.primitiveMode !== GFXPrimitiveMode.TRIANGLE_LIST) { continue; }
|
||||
const vbuffer = subModel.inputAssembler.getVertexBuffer(0);
|
||||
const ibuffer = subModel.inputAssembler.indexBuffer;
|
||||
if (!vbuffer || !vbuffer.buffer || !ibuffer || !ibuffer.buffer) { continue; }
|
||||
// narrowphase
|
||||
distance = Infinity;
|
||||
const vb = new Float32Array(vbuffer.buffer);
|
||||
const ib = new Uint16Array(ibuffer.buffer);
|
||||
const sides = subModel.doubleSided;
|
||||
for (let j = 0; j < ib.length; j += 3) {
|
||||
const i0 = ib[j] * 3;
|
||||
const i1 = ib[j + 1] * 3;
|
||||
const i2 = ib[j + 2] * 3;
|
||||
vec3.set(tri.a, vb[i0], vb[i0 + 1], vb[i0 + 2]);
|
||||
vec3.set(tri.b, vb[i1], vb[i1 + 1], vb[i1 + 2]);
|
||||
vec3.set(tri.c, vb[i2], vb[i2 + 1], vb[i2 + 2]);
|
||||
const dist = intersect.ray_triangle(modelRay, tri, sides);
|
||||
if (dist <= 0 || dist > distance) { continue; }
|
||||
distance = dist;
|
||||
}
|
||||
if (distance < Infinity) {
|
||||
const r = pool.add();
|
||||
r.node = node;
|
||||
r.distance = distance * vec3.magnitude(vec3.mul(v3, modelRay.d, node._scale));
|
||||
results[pool.length - 1] = r;
|
||||
}
|
||||
}
|
||||
results.length = pool.length;
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
const modelRay = ray.create();
|
||||
const v3 = vec3.create();
|
||||
const m4 = mat4.create();
|
||||
let distance = Infinity;
|
||||
const tri = triangle.create();
|
||||
const pool = new RecyclePool(() => {
|
||||
return { node: null, distance: Infinity };
|
||||
}, 8);
|
||||
const results: IRaycastResult[] = [];
|
||||
|
||||
@@ -199,62 +199,4 @@ class Scene {
|
||||
}
|
||||
}
|
||||
|
||||
if (CC_EDITOR) {
|
||||
/**
|
||||
* Cast a ray into the scene, record all the intersected models in the result array
|
||||
* @param {Ray} worldRay the testing ray
|
||||
* @param {Object[]} results the results array
|
||||
* @param {number} mask the layer mask to filter the models
|
||||
* @return {Object[]} the results array
|
||||
*/
|
||||
Scene.prototype.raycast = (function() {
|
||||
let modelRay = ray.create();
|
||||
let v3 = vec3.create();
|
||||
let m4 = mat4.create();
|
||||
let distance = Infinity;
|
||||
let tri = triangle.create();
|
||||
let pool = new RecyclePool(() => {
|
||||
return { node: null, distance: Infinity };
|
||||
}, 8);
|
||||
let results = [];
|
||||
return function(worldRay, mask = Layers.RaycastMask) {
|
||||
pool.reset();
|
||||
for (let i = 0; i < this._models.length; i++) {
|
||||
let m = this._models.data[i], node = m._node;
|
||||
if (!cc.Layers.check(node.layer, mask) || !m._bsModelSpace) continue;
|
||||
// transform ray back to model space
|
||||
mat4.invert(m4, node.getWorldMatrix(m4));
|
||||
vec3.transformMat4(modelRay.o, worldRay.o, m4);
|
||||
vec3.normalize(modelRay.d, vec3.transformMat4Normal(modelRay.d, worldRay.d, m4));
|
||||
// broadphase
|
||||
if ((distance = intersect.ray_aabb(modelRay, m._bsModelSpace)) <= 0) continue;
|
||||
let ia = m._inputAssembler;
|
||||
if (ia._primitiveType == gfx.PT_TRIANGLES) {
|
||||
// narrowphase
|
||||
distance = Infinity;
|
||||
let vb = ia._vertexBuffer[gfx.ATTR_POSITION];
|
||||
let ib = ia._indexBuffer._data, sides = ia.doubleSided;
|
||||
for (let j = 0; j < ib.length; j += 3) {
|
||||
let i0 = ib[j] * 3, i1 = ib[j + 1] * 3, i2 = ib[j + 2] * 3;
|
||||
vec3.set(tri.a, vb[i0], vb[i0 + 1], vb[i0 + 2]);
|
||||
vec3.set(tri.b, vb[i1], vb[i1 + 1], vb[i1 + 2]);
|
||||
vec3.set(tri.c, vb[i2], vb[i2 + 1], vb[i2 + 2]);
|
||||
let dist = intersect.ray_triangle(modelRay, tri, sides);
|
||||
if (dist <= 0 || dist > distance) continue;
|
||||
distance = dist;
|
||||
}
|
||||
}
|
||||
if (distance < Infinity) {
|
||||
let r = pool.add();
|
||||
r.node = node;
|
||||
r.distance = distance * vec3.magnitude(vec3.mul(v3, modelRay.d, node._scale));
|
||||
results[pool.length - 1] = r;
|
||||
}
|
||||
}
|
||||
results.length = pool.length;
|
||||
return results;
|
||||
};
|
||||
})();
|
||||
}
|
||||
|
||||
export default Scene;
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
import gfx from './gfx';
|
||||
import InputAssembler from './core/input-assembler';
|
||||
import { GFXBufferUsageBit, GFXMemoryUsageBit, GFXFormat, GFXAttributeName } from '../gfx/define';
|
||||
|
||||
/**
|
||||
* @param {gfx.Device} device
|
||||
* @param {Object} data
|
||||
*/
|
||||
export function createIA(device, data) {
|
||||
if (!data.positions) {
|
||||
console.error('The data must have positions field');
|
||||
|
||||
@@ -41,8 +41,9 @@ class Node extends BaseNode {
|
||||
@property
|
||||
protected _layer = Layers.Default; // the layer this node belongs to
|
||||
|
||||
// local rotation in euler angles, maintained here so that rotation angles could be greater than 360 degree.
|
||||
@property
|
||||
protected _euler = new Vec3(); // local rotation in euler angles, maintained here so that rotation angles could be greater than 360 degree.
|
||||
protected _euler = new Vec3();
|
||||
|
||||
// world transform
|
||||
protected _pos = new Vec3();
|
||||
|
||||
@@ -53,8 +53,6 @@
|
||||
const camComp = cameraNode.addComponent(cc.CameraComponent);
|
||||
const mainWindow = root.mainWindow;
|
||||
|
||||
cc.director.runSceneImmediate(scene);
|
||||
|
||||
const material = new cc.Material();
|
||||
material.effectName = 'test'; // parsed effect file is embedded in cocos/3d/builtin/effects.js
|
||||
material.setProperty('u_color', cc.color('#ff0000'));
|
||||
@@ -67,6 +65,8 @@
|
||||
modelCom.material = material;
|
||||
modelCom.mesh = cc.utils.createMesh(cc.game._renderContext, cc.primitives.box());
|
||||
modelNode.setRotationFromEuler(45, 45, 45);
|
||||
|
||||
cc.director.runSceneImmediate(scene);
|
||||
}
|
||||
cc.game.run({ id: 'canvas' }, onStart);
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user