add ambient and main light to scene

This commit is contained in:
jerrywwl
2019-03-04 11:21:07 +08:00
parent d8d56d3cbc
commit a63e82dddc
21 changed files with 347 additions and 164 deletions
+59 -12
View File
File diff suppressed because one or more lines are too long
+8
View File
@@ -134,6 +134,14 @@ class BuiltinResMgr {
});
resources[tonemapMtl._uuid] = tonemapMtl;
// standard material
const standardMtl = new cc.Material();
standardMtl._uuid = 'standard-material';
standardMtl.initialize({
effectName: 'builtin-standard',
});
resources[standardMtl._uuid] = standardMtl;
// default material
const defaultMtl = new cc.Material();
defaultMtl._uuid = 'default-material';
+6 -9
View File
@@ -149,7 +149,6 @@ export class LightComponent extends Component {
// protected _shadowBias = 0.0005;
protected _light: Light | null = null;
protected _colorData = [1, 1, 1];
/**
* !#en The light source type
@@ -185,11 +184,10 @@ export class LightComponent extends Component {
set color (val) {
this._color = val;
const scale = this._intensity / 255;
this._colorData[0] = val.r * scale;
this._colorData[1] = val.g * scale;
this._colorData[2] = val.b * scale;
if (this._light) {
this._light.color = this._colorData;
this._light.color[0] = val.r * scale;
this._light.color[1] = val.g * scale;
this._light.color[2] = val.b * scale;
}
}
@@ -207,11 +205,10 @@ export class LightComponent extends Component {
set intensity (val) {
this._intensity = val;
const scale = val / 255;
this._colorData[0] = this._color.r * scale;
this._colorData[1] = this._color.g * scale;
this._colorData[2] = this._color.b * scale;
if (this._light) {
this._light.color = this._colorData;
this._light.color[0] = this._color.r * scale;
this._light.color[1] = this._color.g * scale;
this._light.color[2] = this._color.b * scale;
}
}
+4
View File
@@ -764,11 +764,15 @@ var game = {
}
{
this._gfxDevice = new cc.WebGLGFXDevice();
/*
if (window.WebGL2RenderingContext) {
this._gfxDevice = new cc.WebGL2GFXDevice();
} else {
this._gfxDevice = new cc.WebGLGFXDevice();
}
*/
this._gfxDevice.initialize({
canvasElm: localCanvas,
+4 -3
View File
@@ -34,7 +34,7 @@ export class WebGLGFXBuffer extends GFXBuffer {
if (this._usage & GFXBufferUsageBit.INDIRECT) {
this._indirectBuffer = { drawInfos: [] };
} else if (this._usage & GFXBufferUsageBit.UNIFORM) {
} else if ((this._usage & GFXBufferUsageBit.UNIFORM) && this._size > 0) {
this._uniformBuffer = new ArrayBuffer(this._size);
}
@@ -85,8 +85,9 @@ export class WebGLGFXBuffer extends GFXBuffer {
this._gpuBuffer.buffer = this._uniformBuffer;
}
this._gpuBuffer.size = this._size;
WebGLCmdFuncResizeBuffer(this._device as WebGLGFXDevice, this._gpuBuffer);
if (this._size > 0) {
WebGLCmdFuncResizeBuffer(this._device as WebGLGFXDevice, this._gpuBuffer);
}
}
}
+8 -12
View File
@@ -565,13 +565,13 @@ export class WebGLCmdPackage {
export function WebGLCmdFuncCreateBuffer (device: WebGLGFXDevice, gpuBuffer: WebGLGPUBuffer) {
const gl = device.gl;
const glUsage: GLenum = gpuBuffer.memUsage & GFXMemoryUsageBit.HOST ? WebGL2RenderingContext.DYNAMIC_DRAW : WebGL2RenderingContext.STATIC_DRAW;
const glUsage: GLenum = gpuBuffer.memUsage & GFXMemoryUsageBit.HOST ? WebGLRenderingContext.DYNAMIC_DRAW : WebGLRenderingContext.STATIC_DRAW;
if (gpuBuffer.usage & GFXBufferUsageBit.VERTEX) {
gpuBuffer.glTarget = WebGLRenderingContext.ARRAY_BUFFER;
const glBuffer = gl.createBuffer();
if (glBuffer) {
if (glBuffer && gpuBuffer.size > 0) {
gpuBuffer.glBuffer = glBuffer;
if (device.stateCache.glArrayBuffer !== gpuBuffer.glBuffer) {
@@ -587,7 +587,7 @@ export function WebGLCmdFuncCreateBuffer (device: WebGLGFXDevice, gpuBuffer: Web
gpuBuffer.glTarget = WebGLRenderingContext.ELEMENT_ARRAY_BUFFER;
const glBuffer = gl.createBuffer();
if (glBuffer) {
if (glBuffer && gpuBuffer.size > 0) {
gpuBuffer.glBuffer = glBuffer;
if (device.stateCache.glElementArrayBuffer !== gpuBuffer.glBuffer) {
@@ -628,7 +628,7 @@ export function WebGLCmdFuncDestroyBuffer (device: WebGLGFXDevice, gpuBuffer: We
export function WebGLCmdFuncResizeBuffer (device: WebGLGFXDevice, gpuBuffer: WebGLGPUBuffer) {
const gl = device.gl;
const glUsage: GLenum = gpuBuffer.memUsage & GFXMemoryUsageBit.HOST ? WebGL2RenderingContext.DYNAMIC_DRAW : WebGL2RenderingContext.STATIC_DRAW;
const glUsage: GLenum = gpuBuffer.memUsage & GFXMemoryUsageBit.HOST ? WebGLRenderingContext.DYNAMIC_DRAW : WebGLRenderingContext.STATIC_DRAW;
if (gpuBuffer.usage & GFXBufferUsageBit.VERTEX) {
@@ -853,7 +853,7 @@ export function WebGLCmdFuncCreateFramebuffer (device: WebGLGFXDevice, gpuFrameb
gpuColorView.gpuTexture.glTexture,
gpuColorView.baseLevel);
attachments.push(WebGL2RenderingContext.COLOR_ATTACHMENT0 + i);
attachments.push(WebGLRenderingContext.COLOR_ATTACHMENT0 + i);
}
}
@@ -874,7 +874,6 @@ export function WebGLCmdFuncCreateFramebuffer (device: WebGLGFXDevice, gpuFrameb
device.WEBGL_draw_buffers.drawBuffersWEBGL(attachments);
}
/*
const status = gl.checkFramebufferStatus(WebGLRenderingContext.FRAMEBUFFER);
if (status !== WebGLRenderingContext.FRAMEBUFFER_COMPLETE) {
switch (status) {
@@ -897,7 +896,6 @@ export function WebGLCmdFuncCreateFramebuffer (device: WebGLGFXDevice, gpuFrameb
default:
}
}
*/
}
}
}
@@ -1301,11 +1299,9 @@ export function WebGLCmdFuncExecuteCmds (device: WebGLGFXDevice, cmdPackage: Web
gl.colorMask(true, true, true, true);
}
if (!cmd0.gpuFramebuffer.isOffscreen) {
const clearColor = cmd0.clearColors[0];
gl.clearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a);
clears |= WebGL2RenderingContext.COLOR_BUFFER_BIT;
}
const clearColor = cmd0.clearColors[0];
gl.clearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a);
clears |= WebGLRenderingContext.COLOR_BUFFER_BIT;
break;
}
-1
View File
@@ -6,7 +6,6 @@ import { GFXCommandBuffer, IGFXCommandBufferInfo } from '../command-buffer';
import {
GFXBufferTextureCopy,
GFXFormat,
GFXFormatInfos,
GFXFormatSize,
GFXQueueType,
} from '../define';
+3 -1
View File
@@ -73,7 +73,9 @@ export class WebGL2GFXBuffer extends GFXBuffer {
if (this._gpuBuffer) {
this._gpuBuffer.size = this._size;
WebGL2CmdFuncResizeBuffer(this._device as WebGL2GFXDevice, this._gpuBuffer);
if (this._size > 0) {
WebGL2CmdFuncResizeBuffer(this._device as WebGL2GFXDevice, this._gpuBuffer);
}
}
}
+43 -68
View File
@@ -25,7 +25,6 @@ import {
IGFXViewport,
WebGLEXT,
} from '../define';
import { GFXUniformBlock } from '../shader';
import { WebGL2GFXCommandAllocator } from './webgl2-command-allocator';
import {
IWebGL2DepthBias,
@@ -52,10 +51,10 @@ import {
import { IWebGL2TexUnit } from './webgl2-state-cache';
const WebGLWraps: GLenum[] = [
WebGL2RenderingContext.REPEAT,
WebGL2RenderingContext.MIRRORED_REPEAT,
WebGL2RenderingContext.CLAMP_TO_EDGE,
WebGL2RenderingContext.CLAMP_TO_EDGE,
WebGLRenderingContext.REPEAT,
WebGLRenderingContext.MIRRORED_REPEAT,
WebGLRenderingContext.CLAMP_TO_EDGE,
WebGLRenderingContext.CLAMP_TO_EDGE,
];
const _uniformValues = new Array<number>(1024 * 4);
@@ -473,51 +472,51 @@ function WebGLGetComponentCount (glType: GLenum): GFXType {
}
const WebGLCmpFuncs: GLenum[] = [
WebGL2RenderingContext.NEVER,
WebGL2RenderingContext.LESS,
WebGL2RenderingContext.EQUAL,
WebGL2RenderingContext.LEQUAL,
WebGL2RenderingContext.GREATER,
WebGL2RenderingContext.NOTEQUAL,
WebGL2RenderingContext.GEQUAL,
WebGL2RenderingContext.ALWAYS,
WebGLRenderingContext.NEVER,
WebGLRenderingContext.LESS,
WebGLRenderingContext.EQUAL,
WebGLRenderingContext.LEQUAL,
WebGLRenderingContext.GREATER,
WebGLRenderingContext.NOTEQUAL,
WebGLRenderingContext.GEQUAL,
WebGLRenderingContext.ALWAYS,
];
const WebGLStencilOps: GLenum[] = [
WebGL2RenderingContext.ZERO,
WebGL2RenderingContext.KEEP,
WebGL2RenderingContext.REPLACE,
WebGL2RenderingContext.INCR,
WebGL2RenderingContext.DECR,
WebGL2RenderingContext.INVERT,
WebGL2RenderingContext.INCR_WRAP,
WebGL2RenderingContext.DECR_WRAP,
WebGLRenderingContext.ZERO,
WebGLRenderingContext.KEEP,
WebGLRenderingContext.REPLACE,
WebGLRenderingContext.INCR,
WebGLRenderingContext.DECR,
WebGLRenderingContext.INVERT,
WebGLRenderingContext.INCR_WRAP,
WebGLRenderingContext.DECR_WRAP,
];
const WebGLBlendOps: GLenum[] = [
WebGL2RenderingContext.FUNC_ADD,
WebGL2RenderingContext.FUNC_SUBTRACT,
WebGL2RenderingContext.FUNC_REVERSE_SUBTRACT,
WebGL2RenderingContext.FUNC_ADD,
WebGL2RenderingContext.FUNC_ADD,
WebGLRenderingContext.FUNC_ADD,
WebGLRenderingContext.FUNC_SUBTRACT,
WebGLRenderingContext.FUNC_REVERSE_SUBTRACT,
WebGLRenderingContext.FUNC_ADD,
WebGLRenderingContext.FUNC_ADD,
];
const WebGLBlendFactors: GLenum[] = [
WebGL2RenderingContext.ZERO,
WebGL2RenderingContext.ONE,
WebGL2RenderingContext.SRC_ALPHA,
WebGL2RenderingContext.DST_ALPHA,
WebGL2RenderingContext.ONE_MINUS_SRC_ALPHA,
WebGL2RenderingContext.ONE_MINUS_DST_ALPHA,
WebGL2RenderingContext.SRC_COLOR,
WebGL2RenderingContext.DST_COLOR,
WebGL2RenderingContext.ONE_MINUS_SRC_COLOR,
WebGL2RenderingContext.ONE_MINUS_DST_COLOR,
WebGL2RenderingContext.SRC_ALPHA_SATURATE,
WebGL2RenderingContext.CONSTANT_COLOR,
WebGL2RenderingContext.ONE_MINUS_CONSTANT_COLOR,
WebGL2RenderingContext.CONSTANT_ALPHA,
WebGL2RenderingContext.ONE_MINUS_CONSTANT_ALPHA,
WebGLRenderingContext.ZERO,
WebGLRenderingContext.ONE,
WebGLRenderingContext.SRC_ALPHA,
WebGLRenderingContext.DST_ALPHA,
WebGLRenderingContext.ONE_MINUS_SRC_ALPHA,
WebGLRenderingContext.ONE_MINUS_DST_ALPHA,
WebGLRenderingContext.SRC_COLOR,
WebGLRenderingContext.DST_COLOR,
WebGLRenderingContext.ONE_MINUS_SRC_COLOR,
WebGLRenderingContext.ONE_MINUS_DST_COLOR,
WebGLRenderingContext.SRC_ALPHA_SATURATE,
WebGLRenderingContext.CONSTANT_COLOR,
WebGLRenderingContext.ONE_MINUS_CONSTANT_COLOR,
WebGLRenderingContext.CONSTANT_ALPHA,
WebGLRenderingContext.ONE_MINUS_CONSTANT_ALPHA,
];
export enum WebGL2Cmd {
@@ -712,7 +711,7 @@ export function WebGL2CmdFuncCreateBuffer (device: WebGL2GFXDevice, gpuBuffer: W
gpuBuffer.glTarget = WebGL2RenderingContext.ARRAY_BUFFER;
const glBuffer = gl.createBuffer();
if (glBuffer) {
if (glBuffer && gpuBuffer.size > 0) {
gpuBuffer.glBuffer = glBuffer;
if (device.stateCache.glArrayBuffer !== gpuBuffer.glBuffer) {
@@ -729,7 +728,7 @@ export function WebGL2CmdFuncCreateBuffer (device: WebGL2GFXDevice, gpuBuffer: W
gpuBuffer.glTarget = WebGL2RenderingContext.ELEMENT_ARRAY_BUFFER;
const glBuffer = gl.createBuffer();
if (glBuffer) {
if (glBuffer && gpuBuffer.size > 0) {
gpuBuffer.glBuffer = glBuffer;
if (device.stateCache.glElementArrayBuffer !== gpuBuffer.glBuffer) {
@@ -746,7 +745,7 @@ export function WebGL2CmdFuncCreateBuffer (device: WebGL2GFXDevice, gpuBuffer: W
gpuBuffer.glTarget = WebGL2RenderingContext.UNIFORM_BUFFER;
const glBuffer = gl.createBuffer();
if (glBuffer) {
if (glBuffer && gpuBuffer.size > 0) {
gpuBuffer.glBuffer = glBuffer;
if (device.stateCache.glUniformBuffer !== gpuBuffer.glBuffer) {
gl.bindBuffer(WebGL2RenderingContext.UNIFORM_BUFFER, gpuBuffer.glBuffer);
@@ -891,7 +890,6 @@ export function WebGL2CmdFuncCreateTexture (device: WebGL2GFXDevice, gpuTexture:
switch (gpuTexture.viewType) {
case GFXTextureViewType.TV2D: {
gpuTexture.viewType = GFXTextureViewType.TV2D;
gpuTexture.glTarget = WebGL2RenderingContext.TEXTURE_2D;
@@ -922,21 +920,10 @@ export function WebGL2CmdFuncCreateTexture (device: WebGL2GFXDevice, gpuTexture:
h = Math.max(1, h >> 1);
}
}
gl.texParameteri(gpuTexture.glTarget, WebGL2RenderingContext.TEXTURE_WRAP_S, WebGL2RenderingContext.CLAMP_TO_EDGE);
gl.texParameteri(gpuTexture.glTarget, WebGL2RenderingContext.TEXTURE_WRAP_T, WebGL2RenderingContext.CLAMP_TO_EDGE);
gl.texParameteri(gpuTexture.glTarget, WebGL2RenderingContext.TEXTURE_MIN_FILTER, WebGL2RenderingContext.LINEAR);
gl.texParameteri(gpuTexture.glTarget, WebGL2RenderingContext.TEXTURE_MAG_FILTER, WebGL2RenderingContext.LINEAR);
gpuTexture.glWrapS = WebGL2RenderingContext.CLAMP_TO_EDGE;
gpuTexture.glWrapT = WebGL2RenderingContext.CLAMP_TO_EDGE;
gpuTexture.glMinFilter = WebGL2RenderingContext.LINEAR;
gpuTexture.glMagFilter = WebGL2RenderingContext.LINEAR;
}
break;
}
case GFXTextureViewType.CUBE: {
gpuTexture.viewType = GFXTextureViewType.CUBE;
gpuTexture.glTarget = WebGL2RenderingContext.TEXTURE_CUBE_MAP;
@@ -973,17 +960,7 @@ export function WebGL2CmdFuncCreateTexture (device: WebGL2GFXDevice, gpuTexture:
}
}
}
gl.texParameteri(gpuTexture.glTarget, WebGL2RenderingContext.TEXTURE_WRAP_S, WebGL2RenderingContext.CLAMP_TO_EDGE);
gl.texParameteri(gpuTexture.glTarget, WebGL2RenderingContext.TEXTURE_WRAP_T, WebGL2RenderingContext.CLAMP_TO_EDGE);
gl.texParameteri(gpuTexture.glTarget, WebGL2RenderingContext.TEXTURE_MIN_FILTER, WebGL2RenderingContext.LINEAR);
gl.texParameteri(gpuTexture.glTarget, WebGL2RenderingContext.TEXTURE_MAG_FILTER, WebGL2RenderingContext.LINEAR);
gpuTexture.glWrapS = WebGL2RenderingContext.CLAMP_TO_EDGE;
gpuTexture.glWrapT = WebGL2RenderingContext.CLAMP_TO_EDGE;
gpuTexture.glMinFilter = WebGL2RenderingContext.LINEAR;
gpuTexture.glMagFilter = WebGL2RenderingContext.LINEAR;
}
break;
}
default: {
@@ -1098,7 +1075,6 @@ export function WebGL2CmdFuncCreateFramebuffer (device: WebGL2GFXDevice, gpuFram
gl.drawBuffers(attachments);
/*
const status = gl.checkFramebufferStatus(WebGL2RenderingContext.FRAMEBUFFER);
if (status !== WebGL2RenderingContext.FRAMEBUFFER_COMPLETE) {
switch (status) {
@@ -1121,7 +1097,6 @@ export function WebGL2CmdFuncCreateFramebuffer (device: WebGL2GFXDevice, gpuFram
default:
}
}
*/
}
}
}
+2 -1
View File
@@ -102,7 +102,8 @@ export class WebGL2GFXDevice extends GFXDevice {
try {
const webGLCtxAttribs: WebGLContextAttributes = {
alpha: true,
antialias: this._isAntialias,
// antialias: this._isAntialias,
antialias: false,
depth: true,
stencil: true,
premultipliedAlpha: this._isPremultipliedAlpha,
+9 -1
View File
@@ -37,7 +37,11 @@ export class UBOGlobal {
public static MAT_VIEW_PROJ_OFFSET: number = 76;
public static MAT_VIEW_PROJ_INV_OFFSET: number = 92;
public static CAMERA_POS_OFFSET: number = 108;
public static COUNT: number = 112;
public static MAIN_LIT_DIR_OFFSET: number = 112;
public static MAIN_LIT_COLOR_OFFSET: number = 116;
public static AMBIENT_SKY_OFFSET: number = 120;
public static AMBIENT_GROUND_OFFSET: number = 124;
public static COUNT: number = 128;
public static SIZE: number = UBOGlobal.COUNT * 4;
public static BLOCK: GFXUniformBlock = {
@@ -54,6 +58,10 @@ 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 },
{ name: 'cc_mainLitDir', type: GFXType.FLOAT4, count: 1 },
{ name: 'cc_mainLitColor', type: GFXType.FLOAT4, count: 1 },
{ name: 'cc_ambientSky', type: GFXType.FLOAT4, count: 1 },
{ name: 'cc_ambientGround', type: GFXType.FLOAT4, count: 1 },
],
};
+3 -3
View File
@@ -20,13 +20,13 @@ export class ForwardFlow extends RenderFlow {
this._priority = info.priority;
const framebuffer = this._pipeline.root.mainWindow!.framebuffer!;
// const framebuffer = this._pipeline.root.mainWindow!.framebuffer!;
this.createStage(ForwardStage, {
name: 'ForwardStage',
priority: ForwardStagePriority.FORWARD,
// framebuffer: this._pipeline.shadingFBO,
framebuffer,
framebuffer: this._pipeline.shadingFBO,
// framebuffer,
});
return true;
+4 -5
View File
@@ -61,12 +61,10 @@ export class ForwardPipeline extends RenderPipeline {
priority: ForwardFlowPriority.FORWARD,
});
/*
this.createFlow(ToneMapFlow, {
name: 'ToneMapFlow',
priority: 0,
});
*/
return true;
}
@@ -118,12 +116,13 @@ export class ForwardPipeline extends RenderPipeline {
const scene = view.camera.scene;
const dLights = scene.directionalLights;
for (let i = 0; i < UBOForwardLights.MAX_DIR_LIGHTS; i++) {
const light = dLights[i];
if (light && light.enabled) {
light.update();
this._uboLights.view.set(light.directionArray, UBOForwardLights.DIR_LIGHT_DIR_OFFSET + i * 4);
this._uboLights.view.set(light.colorData, UBOForwardLights.DIR_LIGHT_COLOR_OFFSET + i * 4);
this._uboLights.view.set(light.color, UBOForwardLights.DIR_LIGHT_COLOR_OFFSET + i * 4);
} else {
this._uboLights.view.set(_idVec4Array, UBOForwardLights.DIR_LIGHT_DIR_OFFSET + i * 4);
this._uboLights.view.set(_idVec4Array, UBOForwardLights.DIR_LIGHT_COLOR_OFFSET + i * 4);
@@ -135,7 +134,7 @@ export class ForwardPipeline extends RenderPipeline {
if (light && light.enabled) {
light.update();
this._uboLights.view.set(light.positionAndRange, UBOForwardLights.POINT_LIGHT_POS_RANGE_OFFSET + i * 4);
this._uboLights.view.set(light.colorData, UBOForwardLights.POINT_LIGHT_COLOR_OFFSET + i * 4);
this._uboLights.view.set(light.color, UBOForwardLights.POINT_LIGHT_COLOR_OFFSET + i * 4);
} else {
this._uboLights.view.set(_idVec4Array, UBOForwardLights.POINT_LIGHT_POS_RANGE_OFFSET + i * 4);
this._uboLights.view.set(_idVec4Array, UBOForwardLights.POINT_LIGHT_COLOR_OFFSET + i * 4);
@@ -148,7 +147,7 @@ export class ForwardPipeline extends RenderPipeline {
light.update();
this._uboLights.view.set(light.positionAndRange, UBOForwardLights.SPOT_LIGHT_POS_RANGE_OFFSET + i * 4);
this._uboLights.view.set(light.directionArray, UBOForwardLights.SPOT_LIGHT_DIR_OFFSET + i * 4);
this._uboLights.view.set(light.colorData, UBOForwardLights.SPOT_LIGHT_COLOR_OFFSET + i * 4);
this._uboLights.view.set(light.color, UBOForwardLights.SPOT_LIGHT_COLOR_OFFSET + i * 4);
} else {
this._uboLights.view.set(_idVec4Array, UBOForwardLights.SPOT_LIGHT_POS_RANGE_OFFSET + i * 4);
this._uboLights.view.set(_idVec4Array, UBOForwardLights.SPOT_LIGHT_DIR_OFFSET + i * 4);
+51 -5
View File
@@ -170,7 +170,7 @@ export abstract class RenderPipeline {
protected createShadingTarget (): boolean {
let colorFmt: GFXFormat;
let depthFmt: GFXFormat;
let depthStencilFmt: GFXFormat;
// Try to use HDR format
if (this._device.hasFeature(GFXFeature.FORMAT_R11G11B10F)) {
@@ -188,15 +188,15 @@ export abstract class RenderPipeline {
}
if (this._device.hasFeature(GFXFeature.FORMAT_D24S8)) {
depthFmt = GFXFormat.D24S8;
depthStencilFmt = GFXFormat.D24S8;
} else {
depthFmt = GFXFormat.D16;
depthStencilFmt = GFXFormat.D16;
}
// colorFmt = GFXFormat.RGBA16F;
console.info('Shading Color Format: ' + GFXFormatInfos[colorFmt].name);
console.info('Shading Depth Format: ' + GFXFormatInfos[depthFmt].name);
console.info('Shading Depth Format: ' + GFXFormatInfos[depthStencilFmt].name);
this._shadingTex = this._device.createTexture({
type: GFXTextureType.TEX2D,
@@ -212,6 +212,20 @@ export abstract class RenderPipeline {
format : colorFmt,
});
this._depthStencilTex = this._device.createTexture({
type : GFXTextureType.TEX2D,
usage : GFXTextureUsageBit.DEPTH_STENCIL_ATTACHMENT | GFXTextureUsageBit.SAMPLED,
format : depthStencilFmt,
width : this._device.nativeWidth,
height : this._device.nativeHeight,
});
this._depthStencilTexView = this._device.createTextureView({
texture : this._depthStencilTex,
type : GFXTextureViewType.TV2D,
format : depthStencilFmt,
});
this._shadingPass = this._device.createRenderPass({
colorAttachments: [{
format: colorFmt,
@@ -222,7 +236,7 @@ export abstract class RenderPipeline {
endLayout: GFXTextureLayout.COLOR_ATTACHMENT_OPTIMAL,
}],
depthStencilAttachment: {
format : depthFmt,
format : depthStencilFmt,
depthLoadOp : GFXLoadOp.CLEAR,
depthStoreOp : GFXStoreOp.STORE,
stencilLoadOp : GFXLoadOp.CLEAR,
@@ -253,6 +267,16 @@ export abstract class RenderPipeline {
this._shadingTex = null;
}
if (this._depthStencilTexView) {
this._depthStencilTexView.destroy();
this._depthStencilTexView = null;
}
if (this._depthStencilTex) {
this._depthStencilTex.destroy();
this._depthStencilTex = null;
}
if (this._shadingFBO) {
this._shadingFBO.destroy();
this._shadingFBO = null;
@@ -380,8 +404,16 @@ export abstract class RenderPipeline {
protected updateUBOs (view: RenderView) {
const camera = view.camera;
const scene = camera.scene;
const device = this._root.device;
const mainLight = scene.mainLight;
if (mainLight && mainLight.enabled) {
mainLight.update();
}
const ambient = scene.ambient;
// update UBOGlobal
_vec4Array[0] = this._root.frameTime;
_vec4Array[1] = 0.0;
@@ -425,6 +457,20 @@ export abstract class RenderPipeline {
_vec4Array[3] = 1.0;
this._defaultUboGlobal!.view.set(_vec4Array, UBOGlobal.CAMERA_POS_OFFSET);
vec3.array(_vec4Array, mainLight.direction);
this._defaultUboGlobal!.view.set(_vec4Array, UBOGlobal.MAIN_LIT_DIR_OFFSET);
_vec4Array.set(mainLight.color);
_vec4Array[3] = 1.0;
this._defaultUboGlobal!.view.set(_vec4Array, UBOGlobal.MAIN_LIT_COLOR_OFFSET);
_vec4Array.set(ambient.skyColor);
_vec4Array[3] = ambient.skyIllum;
this._defaultUboGlobal!.view.set(_vec4Array, UBOGlobal.AMBIENT_SKY_OFFSET);
_vec4Array.set(ambient.groundAlbedo);
this._defaultUboGlobal!.view.set(_vec4Array, UBOGlobal.AMBIENT_GROUND_OFFSET);
// update ubos
this._globalBindings.get(UBOGlobal.BLOCK.name)!.buffer!.update(this._defaultUboGlobal!.view.buffer);
}
+2 -2
View File
@@ -1,6 +1,6 @@
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
import { IPassInfo, IShaderInfo, ISamplerInfo } from '../../3d/assets/effect-asset';
import { IPassInfo, IShaderInfo } from '../../3d/assets/effect-asset';
import { builtinResMgr } from '../../3d/builtin';
import { TextureBase } from '../../assets/texture-base';
import { color4, mat2, mat3, mat4, vec2, vec3, vec4 } from '../../core/vmath';
@@ -13,7 +13,7 @@ import { GFXPipelineLayout } from '../../gfx/pipeline-layout';
import { GFXBlendState, GFXBlendTarget, GFXDepthStencilState,
GFXInputState, GFXPipelineState, GFXRasterizerState } from '../../gfx/pipeline-state';
import { GFXRenderPass } from '../../gfx/render-pass';
import { GFXSampler, IGFXSamplerInfo, GFXSamplerState } from '../../gfx/sampler';
import { GFXSampler, IGFXSamplerInfo } from '../../gfx/sampler';
import { GFXShader } from '../../gfx/shader';
import { GFXTextureView } from '../../gfx/texture-view';
import { RenderPassStage, RenderPriority } from '../../pipeline/define';
+49
View File
@@ -0,0 +1,49 @@
import { vec3 } from '../../core/vmath';
import { RenderScene } from './render-scene';
export class Ambient {
set enabled (val) {
this._enabled = val;
}
get enabled () {
return this._enabled;
}
get skyColor (): Float32Array {
return this._skyColor;
}
set skyColor (color: Float32Array) {
this._skyColor = color;
}
set skyIllum (illum: number) {
this._skyIllum = illum;
}
get skyIllum (): number {
return this._skyIllum;
}
get groundAlbedo (): Float32Array {
return this._groundAlbedo;
}
set groundAlbedo (color: Float32Array) {
this._groundAlbedo = color;
}
protected _enabled = true;
protected _skyColor = Float32Array.from([0.2, 0.5, 0.8, 1.0]);
protected _skyIllum: number = 1.0;
protected _groundAlbedo = Float32Array.from([0.2, 0.2, 0.2, 1.0]);
protected _scene: RenderScene;
constructor (scene: RenderScene) {
this._scene = scene;
}
public update () {}
}
+18 -7
View File
@@ -9,21 +9,32 @@ const _v3 = new Vec3();
const _qt = new Quat();
export class DirectionalLight extends Light {
protected _direction = Float32Array.from([0, 0, 1, 0]);
protected _direction: vec3 = new vec3(0, 0, 1);
protected _directionArray: Float32Array = Float32Array.from([0, 0, 1, 0]);
set direction (val: Vec3) {
vec3.array(this._direction, val);
set direction (val: vec3) {
this._direction = val;
vec3.normalize(this._direction, this._direction);
vec3.array(this._directionArray, this._direction);
}
get directionArray () {
get direction (): vec3 {
return this._direction;
}
constructor (scene: RenderScene, node: Node, name: string) {
super(scene, node, name);
get directionArray (): Float32Array {
return this._directionArray;
}
constructor (scene: RenderScene, name: string) {
super(scene, name);
this._type = LightType.DIRECTIONAL;
}
public update () {
this.direction = vec3.transformQuat(_v3, _forward, this._node.getWorldRotation(_qt));
if (this._node) {
this._direction = vec3.transformQuat(_v3, _forward, this._node.getWorldRotation(_qt));
vec3.array(this._directionArray, this._direction);
}
}
}
+16 -6
View File
@@ -1,5 +1,6 @@
import { Node } from '../../scene-graph/node';
import { RenderScene } from './render-scene';
import { vec4 } from '../../core/vmath';
export enum LightType {
DIRECTIONAL,
@@ -17,10 +18,11 @@ export class Light {
return this._enabled;
}
set color (val: number[]) {
this._color.set(val);
set color (color: Float32Array) {
this._color.set(color);
}
get colorData () {
get color () {
return this._color;
}
@@ -30,16 +32,24 @@ export class Light {
get name (): string {
return this._name;
}
set node (n: Node | null) {
this._node = n;
}
get node (): Node | null {
return this._node;
}
protected _color = Float32Array.from([1, 1, 1, 1]);
protected _enabled = false;
protected _scene: RenderScene;
protected _node: Node;
protected _node: Node | null = null;
protected _type: LightType;
protected _name: string;
constructor (scene: RenderScene, node: Node, name: string) {
constructor (scene: RenderScene, name: string) {
this._scene = scene;
this._node = node;
this._name = name;
this._type = LightType.UNKNOWN;
}
+5 -3
View File
@@ -20,12 +20,14 @@ export class PointLight extends Light {
return this._positionAndRange;
}
constructor (scene: RenderScene, node: Node, name: string) {
super(scene, node, name);
constructor (scene: RenderScene, name: string) {
super(scene, name);
this._type = LightType.POINT;
}
public update () {
this.position = this._node.getWorldPosition(_v3);
if (this._node) {
this.position = this._node.getWorldPosition(_v3);
}
}
}
+47 -21
View File
@@ -5,15 +5,16 @@ import { Root } from '../../core/root';
import { Mat4, Vec3 } from '../../core/value-types';
import { mat4, vec3 } from '../../core/vmath';
import { GFXPrimitiveMode } from '../../gfx/define';
import { UBOForwardLights } from '../../pipeline/define';
import { Layers } from '../../scene-graph/layers';
import { Node } from '../../scene-graph/node';
import { Ambient } from './ambient';
import { Camera, ICameraInfo } from './camera';
import { DirectionalLight } from './directional-light';
import { Light } from './light';
import { Model } from './model';
import { PointLight } from './point-light';
import { SpotLight } from './spot-light';
import { UBOForwardLights } from '../../pipeline/define';
export interface IRenderSceneInfo {
name: string;
@@ -44,6 +45,14 @@ export class RenderScene {
return this._cameras;
}
get ambient (): Ambient {
return this._ambient;
}
get mainLight (): DirectionalLight {
return this._mainLight;
}
get directionalLights (): DirectionalLight[] {
return this._directionalLights;
}
@@ -67,6 +76,9 @@ export class RenderScene {
private _root: Root;
private _name: string = '';
private _cameras: Camera[] = [];
private _ambient: Ambient;
private _mainLight: DirectionalLight;
private _mainLightNode: Node;
private _pointLights: PointLight[] = [];
private _directionalLights: DirectionalLight[] = [];
private _spotLights: SpotLight[] = [];
@@ -75,10 +87,15 @@ export class RenderScene {
constructor (root: Root) {
this._root = root;
this._ambient = new Ambient (this);
this._mainLight = new DirectionalLight(this, name);
this._mainLightNode = new Node('mainLightNode');
this._mainLight.node = this._mainLightNode;
}
public initialize (info: IRenderSceneInfo): boolean {
this._name = info.name;
this._mainLight.direction = new vec3(1.0, -1.0, -1.0);
return true;
}
@@ -110,25 +127,12 @@ export class RenderScene {
this._cameras = [];
}
public createPointLight (name: string, node: Node): Light | null {
if (this._pointLights.length >= UBOForwardLights.MAX_POINT_LIGHTS) { return null; }
const light = new PointLight(this, node, name);
this._pointLights.push(light);
return light;
}
public destroyPointLight (light: Light) {
for (let i = 0; i < this._pointLights.length; ++i) {
if (this._pointLights[i] === light) {
this._pointLights.splice(i, 1);
return;
}
}
}
public createDirectionalLight (name: string, node: Node): Light | null {
public createDirectionalLight (name: string, node?: Node): Light | null {
if (this._directionalLights.length >= UBOForwardLights.MAX_DIR_LIGHTS) { return null; }
const light = new DirectionalLight(this, node, name);
const light = new DirectionalLight(this, name);
if (node) {
light.node = node;
}
this._directionalLights.push(light);
return light;
}
@@ -142,9 +146,31 @@ export class RenderScene {
}
}
public createSpotLight (name: string, node: Node): Light | null {
public createPointLight (name: string, node?: Node): Light | null {
if (this._pointLights.length >= UBOForwardLights.MAX_POINT_LIGHTS) { return null; }
const light = new PointLight(this, name);
if (node) {
light.node = node;
}
this._pointLights.push(light);
return light;
}
public destroyPointLight (light: Light) {
for (let i = 0; i < this._pointLights.length; ++i) {
if (this._pointLights[i] === light) {
this._pointLights.splice(i, 1);
return;
}
}
}
public createSpotLight (name: string, node?: Node): Light | null {
if (this._spotLights.length >= UBOForwardLights.MAX_SPOT_LIGHTS) { return null; }
const light = new SpotLight(this, node, name);
const light = new SpotLight(this, name);
if (node) {
light.node = node;
}
this._spotLights.push(light);
return light;
}
+6 -4
View File
@@ -32,13 +32,15 @@ export class SpotLight extends Light {
return this._direction;
}
constructor (scene: RenderScene, node: Node, name: string) {
super(scene, node, name);
constructor (scene: RenderScene, name: string) {
super(scene, name);
this._type = LightType.DIRECTIONAL;
}
public update () {
this.position = this._node.getWorldPosition(_v3);
this.direction = vec3.transformQuat(_v3, _forward, this._node.getWorldRotation(_qt));
if (this._node) {
this.position = this._node.getWorldPosition(_v3);
this.direction = vec3.transformQuat(_v3, _forward, this._node.getWorldRotation(_qt));
}
}
}