mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 08:42:43 +08:00
Vendored
+40
@@ -1,4 +1,44 @@
|
||||
export default [
|
||||
{
|
||||
"name": "test",
|
||||
"techniques": [{"queue": 0, "lod": 500, "passes": [{"stage": 100, "program": "f4c45e6cc7794309d86202e6ec9036a3871fd1ec167a9e02eb1e1574"}], "priority": 0}],
|
||||
"properties": {"u_color": {"type": 17, "value": [1, 1, 1, 1]}, "u_sampler": {"type": 29, "value": null}},
|
||||
"shaders": [
|
||||
{
|
||||
"name": "f4c45e6cc7794309d86202e6ec9036a3871fd1ec167a9e02eb1e1574",
|
||||
"vert": `
|
||||
attribute vec3 a_position;
|
||||
attribute vec2 a_texCoord;
|
||||
uniform mat4 cc_matWorld;
|
||||
varying vec2 v_texCoord;
|
||||
void main() {
|
||||
gl_Position = cc_matWorld*vec4(a_position, 1.0);
|
||||
v_texCoord = a_texCoord;
|
||||
}
|
||||
`,
|
||||
"frag": `
|
||||
varying vec2 v_texCoord;
|
||||
uniform vec4 u_color;
|
||||
uniform sampler2D u_sampler;
|
||||
void main() {
|
||||
|
||||
gl_FragColor = texture2D(u_sampler, v_texCoord);
|
||||
gl_FragColor.r = u_color.r;
|
||||
}
|
||||
`,
|
||||
"defines": [],
|
||||
"blocks": [
|
||||
{"name": "Constants", "size": 16, "defines": [], "binding": 0, "members": [
|
||||
{"name": "u_color", "type": 16, "count": 1, "size": 16}
|
||||
]}
|
||||
],
|
||||
"samplers": [
|
||||
{"name": "u_sampler", "type": 29, "count": 1, "defines": [], "binding": 1}
|
||||
],
|
||||
"dependencies": {}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "builtin-effect-unlit",
|
||||
"techniques": [{"queue": "opaque", "lod": 100, "passes": [{"properties": {"color": {"type": 17, "value": [1, 1, 1, 1]}, "mainTiling": {"type": 14, "value": [1, 1]}, "mainOffset": {"type": 14, "value": [0, 0]}, "mainTexture": {"type": 29, "value": "grey"}}, "program": "e238b675dacba30ad29961fac06cb18d8c771e16d4fb0f9662acbe3c"}]}],
|
||||
|
||||
@@ -75,7 +75,12 @@ export abstract class GFXBindingLayout {
|
||||
}
|
||||
}
|
||||
|
||||
public getBindingUnit (binding: number): GFXBindingUnit {
|
||||
return this._bindingUnits[binding];
|
||||
public getBindingUnit(binding: number): GFXBindingUnit {
|
||||
for (let i = 0; i < this._bindingUnits.length; i++) {
|
||||
if (this._bindingUnits[i].binding === binding) {
|
||||
return this._bindingUnits[i];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { RenderFlow, RenderFlowInfo } from '../render-flow';
|
||||
import { RenderPipeline } from '../render-pipeline';
|
||||
import { TestMaterialStage } from './test-material-stage';
|
||||
import { TestStage } from './test-stage';
|
||||
import { RenderFlow, RenderFlowInfo } from "../render-flow";
|
||||
import { RenderPipeline } from "../render-pipeline";
|
||||
// import { TestStage } from "./test-stage";
|
||||
import { TestMaterialStage } from "./test-material-stage";
|
||||
import { TestStage } from "./test-stage";
|
||||
import { TestModelStage } from "./test-model-stage";
|
||||
|
||||
export enum ForwardStagePriority {
|
||||
FORWARD = 0,
|
||||
@@ -32,18 +34,19 @@ export class ForwardFlow extends RenderFlow {
|
||||
priority: ForwardStagePriority.FORWARD,
|
||||
framebuffer: mainWindow.framebuffer,
|
||||
});
|
||||
/* *
|
||||
this.createStage<TestMaterialStage>(TestMaterialStage, {
|
||||
name: 'TestMaterialStage',
|
||||
/* */
|
||||
|
||||
this.createStage<TestModelStage>(TestModelStage, {
|
||||
name: "TestModelStage",
|
||||
priority: ForwardStagePriority.FORWARD,
|
||||
framebuffer: mainWindow.framebuffer,
|
||||
});
|
||||
/* */
|
||||
this.createStage<TestStage>(TestStage, {
|
||||
name: 'TestStage',
|
||||
priority: ForwardStagePriority.FORWARD,
|
||||
framebuffer: mainWindow.framebuffer,
|
||||
});
|
||||
// this.createStage<TestStage>(TestStage, {
|
||||
// name: 'TestStage',
|
||||
// priority: ForwardStagePriority.FORWARD,
|
||||
// framebuffer: mainWindow.framebuffer,
|
||||
// });
|
||||
/* */
|
||||
|
||||
return true;
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
import { Material } from '../../3d/assets/material';
|
||||
import { GFXCommandBuffer } from '../../gfx/command-buffer';
|
||||
import {
|
||||
GFXBufferTextureCopy, GFXCommandBufferType, GFXFormat,
|
||||
GFXTextureFlagBit, GFXTextureType, GFXTextureUsageBit, GFXTextureViewType, GFXType
|
||||
} from '../../gfx/define';
|
||||
import { GFXFramebuffer } from '../../gfx/framebuffer';
|
||||
import { GFXInputAssembler } from '../../gfx/input-assembler';
|
||||
import { GFXTexture } from '../../gfx/texture';
|
||||
import { GFXTextureView } from '../../gfx/texture-view';
|
||||
import { RenderFlow } from '../render-flow';
|
||||
import { RenderStage, RenderStageInfo } from '../render-stage';
|
||||
import { RenderView } from '../render-view';
|
||||
import Model from '../../renderer/scene/model';
|
||||
import { Scene } from '../../scene-graph';
|
||||
import { EffectAsset } from '../../3d/assets/effect-asset';
|
||||
import { UBOLocal } from '../render-pipeline';
|
||||
import Texture2D from '../../assets/texture-2d';
|
||||
import ImageAsset from '../../assets/image-asset';
|
||||
|
||||
export class TestModelStage extends RenderStage {
|
||||
|
||||
private _material: Material = new Material();
|
||||
private _model: Model;
|
||||
private _scene: Scene;
|
||||
private _init: boolean = false;
|
||||
private _textureAsset: Texture2D | null = null;
|
||||
|
||||
constructor(flow: RenderFlow) {
|
||||
super(flow);
|
||||
}
|
||||
|
||||
public initialize(info: RenderStageInfo): boolean {
|
||||
|
||||
if (info.name !== undefined) {
|
||||
this._name = info.name;
|
||||
}
|
||||
|
||||
this._priority = info.priority;
|
||||
this._framebuffer = info.framebuffer;
|
||||
if (!this._framebuffer) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this._cmdBuff = this._device.createCommandBuffer({
|
||||
allocator: this._device.commandAllocator,
|
||||
type: GFXCommandBufferType.PRIMARY,
|
||||
});
|
||||
|
||||
// load texture
|
||||
const imgElms = this._device.canvas.getElementsByTagName('img');
|
||||
if (imgElms.length) {
|
||||
if (!this.loadTexture(<HTMLImageElement>imgElms[0])) {
|
||||
console.error('Load texture failed.');
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
console.error('Not found image, load texture failed.');
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public destroy() {
|
||||
|
||||
if (this._texView) {
|
||||
this._texView.destroy();
|
||||
this._texView = null;
|
||||
}
|
||||
|
||||
if (this._texture) {
|
||||
this._texture.destroy();
|
||||
this._texture = null;
|
||||
}
|
||||
|
||||
if (this._cmdBuff) {
|
||||
this._cmdBuff.destroy();
|
||||
this._cmdBuff = null;
|
||||
}
|
||||
}
|
||||
|
||||
public render(view: RenderView) {
|
||||
|
||||
if (!this._init) {
|
||||
|
||||
|
||||
this._material.effectName = 'test'; // parsed-effect file is embedded in cocos/3d/builtin/effects.js
|
||||
this._material.setProperty('u_sampler', this._textureAsset);
|
||||
this._material.setProperty('u_color', cc.color('#ff0000'));
|
||||
this._material.inited = true;
|
||||
|
||||
this._scene = new Scene();
|
||||
this._scene._scene = this._scene;
|
||||
let modelCom = this._scene.addComponent('cc.ModelComponent');
|
||||
modelCom.material = this._material;
|
||||
modelCom.mesh = cc.utils.createMesh(cc.game._renderContext, cc.primitives.box());
|
||||
this._model = modelCom._models[0];
|
||||
this._scene.setRotationFromEuler(45, 45, 45);
|
||||
this._scene._rot = this._scene._lrot;
|
||||
this._init = true;
|
||||
}
|
||||
|
||||
const cmdBuff = <GFXCommandBuffer>this._cmdBuff;
|
||||
this._renderArea.width = view.width;
|
||||
this._renderArea.height = view.height;
|
||||
this._model._updateTransform();
|
||||
this._model.updateRenderData();
|
||||
cmdBuff.begin();
|
||||
cmdBuff.beginRenderPass(<GFXFramebuffer>this._framebuffer, this._renderArea, this._clearColors,
|
||||
this._clearDepth, this._clearStencil);
|
||||
|
||||
cmdBuff.execute(this._model.commandBuffers);
|
||||
cmdBuff.endRenderPass();
|
||||
cmdBuff.end();
|
||||
|
||||
this._device.queue.submit([cmdBuff]);
|
||||
}
|
||||
|
||||
private loadTexture (image: HTMLImageElement): boolean {
|
||||
const textureAsset = this._textureAsset = new Texture2D();
|
||||
textureAsset.image = new ImageAsset(image);
|
||||
|
||||
const texture = textureAsset.getGFXTexture();
|
||||
if (!texture) { return false; }
|
||||
const texView = textureAsset.getGFXTextureView();
|
||||
if (!texView) { return false; }
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,8 @@ export enum RenderPassStage {
|
||||
DEFAULT = 100,
|
||||
}
|
||||
|
||||
|
||||
|
||||
export class UBOGlobal {
|
||||
public static TIME_OFFSET: number = 0;
|
||||
public static SCREEN_SIZE_OFFSET: number = UBOGlobal.TIME_OFFSET + 4;
|
||||
@@ -34,7 +36,7 @@ export class UBOGlobal {
|
||||
public static SIZE: number = UBOGlobal.COUNT * 4;
|
||||
|
||||
public static BLOCK: GFXUniformBlock = {
|
||||
binding: 0, name: 'Global', members: [
|
||||
binding: 30, name: 'Global', 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 },
|
||||
@@ -53,19 +55,15 @@ export class UBOGlobal {
|
||||
|
||||
export class UBOLocal {
|
||||
public static MAT_WORLD_OFFSET: number = 0;
|
||||
public static MAT_WORLD_VIEW_OFFSET: number = UBOLocal.MAT_WORLD_OFFSET + 16;
|
||||
public static MAT_WORLD_VIEW_PROJ_OFFSET: number = UBOLocal.MAT_WORLD_VIEW_OFFSET + 16;
|
||||
public static WORLD_POS_OFFSET: number = UBOLocal.MAT_WORLD_VIEW_PROJ_OFFSET + 16;
|
||||
public static COUNT: number = UBOLocal.WORLD_POS_OFFSET + 4;
|
||||
public static MAT_WORLD_IT_OFFSET: number = UBOLocal.MAT_WORLD_OFFSET + 16;
|
||||
public static COUNT: number = UBOLocal.MAT_WORLD_IT_OFFSET + 16;
|
||||
public static SIZE: number = UBOLocal.COUNT * 4;
|
||||
|
||||
public static BLOCK: GFXUniformBlock = {
|
||||
binding: 1, name: 'Local', members: [
|
||||
{ name: 'u_matWorld', type: GFXType.MAT4, count: 1 },
|
||||
{ name: 'u_matWorldView', type: GFXType.MAT4, count: 1 },
|
||||
{ name: 'u_matWorldViewProj', type: GFXType.MAT4, count: 1 },
|
||||
{ name: 'u_worldPos', type: GFXType.FLOAT4, count: 1 },
|
||||
],
|
||||
binding: 31, name: "CCLocal", members: [
|
||||
{ name: "cc_matWorld", type: GFXType.MAT4, count: 1 },
|
||||
{ name: "cc_matWorldIT", type: GFXType.MAT4, count: 1 }
|
||||
]
|
||||
};
|
||||
|
||||
public view: Float32Array = new Float32Array(UBOLocal.COUNT);
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
import { IDefineInfo, IShaderInfo } from '../../3d/assets/effect-asset';
|
||||
import { GFXShaderType } from '../../gfx/define';
|
||||
import { IDefineInfo, IShaderInfo, IBlockInfo, IBlockMember } from '../../3d/assets/effect-asset';
|
||||
import { GFXShaderType, GFXGetTypeSize } from '../../gfx/define';
|
||||
import { GFXDevice } from '../../gfx/device';
|
||||
import { GFXShader } from '../../gfx/shader';
|
||||
import { GFXShader, GFXUniformBlock, GFXUniform } from '../../gfx/shader';
|
||||
import { IDefineMap } from './effect';
|
||||
import { UBOGlobal, UBOLocal } from '../../pipeline/render-pipeline';
|
||||
|
||||
function _generateDefines(
|
||||
device: GFXDevice,
|
||||
@@ -125,6 +126,8 @@ export class ProgramLib {
|
||||
const vert = customDef + tmpl.vert;
|
||||
const frag = customDef + tmpl.frag;
|
||||
|
||||
tmpl.blocks = tmpl.blocks.concat(convertToBlockInfo(UBOGlobal.BLOCK), convertToBlockInfo(UBOLocal.BLOCK));
|
||||
|
||||
program = this._device.createShader({
|
||||
name,
|
||||
blocks: tmpl.blocks,
|
||||
@@ -138,3 +141,28 @@ export class ProgramLib {
|
||||
return program;
|
||||
}
|
||||
}
|
||||
|
||||
function convertToUniformInfo(uniform: GFXUniform): IBlockMember {
|
||||
return {
|
||||
name: uniform.name,
|
||||
type: uniform.type,
|
||||
count: uniform.count,
|
||||
size: GFXGetTypeSize(uniform.type) * uniform.count
|
||||
}
|
||||
}
|
||||
|
||||
function convertToBlockInfo(block: GFXUniformBlock): IBlockInfo {
|
||||
let members: IBlockMember[] = [];
|
||||
let size: number = 0;
|
||||
for (let i = 0; i < block.members.length; i++) {
|
||||
members.push(convertToUniformInfo(block.members[i]));
|
||||
size += members[i].size;
|
||||
}
|
||||
return {
|
||||
name: block.name,
|
||||
binding: block.binding,
|
||||
defines: [],
|
||||
members: members,
|
||||
size: size
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,39 +1,47 @@
|
||||
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
import { Material } from '../../3d/assets/material';
|
||||
import { aabb } from '../../3d/geom-utils';
|
||||
import { IGFXInputAttribute, GFXInputAssembler } from '../../gfx/input-assembler';
|
||||
import { GFXFormat, GFXBufferUsageBit, GFXMemoryUsageBit, GFXCommandBufferType } from '../../gfx/define';
|
||||
import { RenderScene } from './render-scene';
|
||||
import { vec3, mat4 } from '../../core/vmath';
|
||||
import { Vec3 } from '../../core/value-types';
|
||||
import { GFXCommandBuffer } from '../../gfx/command-buffer';
|
||||
import { GFXCommandBufferType } from '../../gfx/define';
|
||||
import { GFXInputAssembler } from '../../gfx/input-assembler';
|
||||
import { Node } from '../../scene-graph';
|
||||
import { Effect } from '../core/effect';
|
||||
import { Pass } from '../core/pass';
|
||||
import { RenderScene } from './render-scene';
|
||||
import { UBOLocal } from '../../pipeline/render-pipeline';
|
||||
import { GFXBuffer } from '../../gfx/buffer';
|
||||
|
||||
let _temp_floatx16 = new Float32Array(16);
|
||||
let _temp_mat4 = mat4.create();
|
||||
|
||||
/**
|
||||
* A representation of a model
|
||||
*/
|
||||
export default class Model {
|
||||
|
||||
public _scene: RenderScene | null;
|
||||
public _id: number;
|
||||
public _enable: boolean;
|
||||
public _type: string;
|
||||
public _poolID: number;
|
||||
public _isEnable: boolean;
|
||||
public _node: Node;
|
||||
public _inputAssembler: GFXInputAssembler | null;
|
||||
public _effect: Effect | null;
|
||||
public _defines: Object;
|
||||
public _dependencies: Object;
|
||||
public _viewID: number;
|
||||
public _cameraID: number;
|
||||
public _userKey: number;
|
||||
public _castShadow: boolean;
|
||||
public _boundingShape: aabb;
|
||||
public _bsModelSpace: aabb;
|
||||
public _material: Material | null;
|
||||
public _cmdBuffers: GFXCommandBuffer[];
|
||||
private _scene: RenderScene | null;
|
||||
private _id: number;
|
||||
private _enable: boolean;
|
||||
private _type: string;
|
||||
private _poolID: number;
|
||||
private _isEnable: boolean;
|
||||
private _node: Node;
|
||||
private _inputAssembler: GFXInputAssembler | null;
|
||||
private _effect: Effect | null;
|
||||
private _defines: Object;
|
||||
private _dependencies: Object;
|
||||
private _viewID: number;
|
||||
private _cameraID: number;
|
||||
private _userKey: number;
|
||||
private _castShadow: boolean;
|
||||
private _boundingShape: aabb;
|
||||
private _bsModelSpace: aabb;
|
||||
private _material: Material | null;
|
||||
private _cmdBuffers: GFXCommandBuffer[];
|
||||
private _localUniforms: UBOLocal | null;
|
||||
private _localUBO: GFXBuffer;
|
||||
/**
|
||||
* Setup a default empty model
|
||||
*/
|
||||
@@ -57,6 +65,18 @@ export default class Model {
|
||||
this._boundingShape = null;
|
||||
this._material = null;
|
||||
this._cmdBuffers = new Array<GFXCommandBuffer>();
|
||||
this._localUniforms = null;
|
||||
}
|
||||
|
||||
initialize() {
|
||||
this._localUniforms = new UBOLocal();
|
||||
this._localUBO = cc.director.root.device.createBuffer({
|
||||
usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST,
|
||||
memUsage: GFXMemoryUsageBit.HOST,
|
||||
size: UBOLocal.SIZE,
|
||||
stride: UBOLocal.SIZE,
|
||||
});
|
||||
this._localUBO.update(this._localUniforms.view);
|
||||
}
|
||||
|
||||
set scene (scene: RenderScene | null) {
|
||||
@@ -82,6 +102,15 @@ export default class Model {
|
||||
this._node._rot, this._node._scale, this._boundingShape);
|
||||
}
|
||||
|
||||
updateRenderData() {
|
||||
mat4.array(_temp_floatx16, this._node._mat);
|
||||
this._node._mat.invert(_temp_mat4);
|
||||
this._localUniforms.view.set(_temp_floatx16, UBOLocal.MAT_WORLD_OFFSET);
|
||||
mat4.array(_temp_floatx16, _temp_mat4);
|
||||
this._localUniforms.view.set(_temp_floatx16, UBOLocal.MAT_WORLD_IT_OFFSET);
|
||||
this._localUBO.update(this._localUniforms.view);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the bounding shape of this model
|
||||
* @param {vec3} minPos the min position of the model
|
||||
@@ -163,6 +192,8 @@ export default class Model {
|
||||
if (this._cmdBuffers[index] == null) {
|
||||
this._cmdBuffers[index] = cc.director.root.device.createCommandBuffer(cmdBufferInfo);
|
||||
}
|
||||
pass.bindingLayout.bindBuffer(UBOLocal.BLOCK.binding, this._localUBO);
|
||||
pass.bindingLayout.update();
|
||||
this._cmdBuffers[index].begin();
|
||||
this._cmdBuffers[index].bindPipelineState(pass.pipelineState);
|
||||
this._cmdBuffers[index].bindBindingLayout(pass.bindingLayout);
|
||||
|
||||
@@ -119,6 +119,7 @@ export class RenderScene {
|
||||
|
||||
public createModel<T extends Model>(clazz: new () => T): Model {
|
||||
let model = new clazz;
|
||||
model.initialize();
|
||||
model.scene = this;
|
||||
this._models.push(model);
|
||||
return model;
|
||||
|
||||
Reference in New Issue
Block a user