area lighted gizmos (#3451)

* area lighted gizmos

* better effect parsing

* logical OR in shader info extraction
This commit is contained in:
Yun Hsiao Wu
2018-11-02 11:06:54 +08:00
committed by Huabing.Xu
parent e540797785
commit 12ef67f6b4
7 changed files with 275 additions and 21 deletions
+7 -6
View File
@@ -89,7 +89,7 @@ function extractDefines(tokens, defines, cache) {
} else if (!ifprocessor.test(str[0])) continue;
if (str[0] === '#elif') { curDefs.pop(); save(t.line); }
let defs = [];
str.splice(1).forEach(s => {
str.splice(1).some(s => {
id = s.match(ident);
if (id) { // is identifier
defs.push(id[0]);
@@ -97,6 +97,7 @@ function extractDefines(tokens, defines, cache) {
if (df) return; // first encounter
defines.push(df = { name: id[0], type: 'boolean' });
} else if (comparators.test(s)) df.type = 'number';
else if (s === '||') return true;
});
curDefs.push(defs); save(t.line);
}
@@ -141,7 +142,7 @@ function extractParams(tokens, cache, uniforms, attributes, extensions) {
}
}
let precedingSpaces = '\n ';
let toOneLiner = o => '\n ' + JSON.stringify(o).replace(/([,:])/g, '$1 ');
function buildTemplates(dest, path, cache) {
let files = fsJetpack.find(path, { matching: ['**/*.vert'] });
let code = '';
@@ -175,16 +176,16 @@ function buildTemplates(dest, path, cache) {
code += ` vert: '${vert}',\n`;
code += ` frag: '${frag}',\n`;
code += ' defines: [';
code += defines.map(d => precedingSpaces + JSON.stringify(d));
code += defines.map(toOneLiner);
code += defines.length ? '\n ],\n' : '],\n';
code += ' uniforms: [';
code += uniforms.map(d => precedingSpaces + JSON.stringify(d));
code += uniforms.map(toOneLiner);
code += uniforms.length ? '\n ],\n' : '],\n';
code += ' attributes: [';
code += attributes.map(d => precedingSpaces + JSON.stringify(d));
code += attributes.map(toOneLiner);
code += attributes.length ? '\n ],\n' : '],\n';
code += ' extensions: [';
code += extensions.map(d => precedingSpaces + JSON.stringify(d));
code += extensions.map(toOneLiner);
code += extensions.length ? '\n ],\n' : '],\n';
code += ' },\n';
}
+6 -3
View File
@@ -23,6 +23,8 @@ let _typeMap = {
[enums.PARAM_MAT4]: Mat4,
[enums.PARAM_TEXTURE_2D]: Texture2D,
[enums.PARAM_TEXTURE_CUBE]: TextureCube,
number: Number,
boolean: Boolean,
default: CCObject
};
let getInstanceType = function(t) { return _typeMap[t] || _typeMap.default; };
@@ -137,6 +139,7 @@ let cloneObjArray = function(val) { return val.map(obj => Object.assign({}, obj)
let _ctorMap = {
[Number]: v => v || 0,
[Boolean]: v => v || false,
[Vec2]: v => v ? cc.v2(v[0], v[1]) : cc.v2(),
[Vec3]: v => v ? cc.v3(v[0], v[1], v[2]) : cc.v3(),
[Vec4]: v => v ? cc.v4(v[0], v[1], v[2], v[3]) : cc.v4(),
@@ -201,7 +204,7 @@ Effect.parseType = function(json) {
tech.passes.forEach(pass => {
let program = lib.getTemplate(pass.program);
program.defines.forEach(define => {
types[define.name] = (define.type === 'boolean' ? Boolean : Number);
types[define.name] = getInstanceType(define.type);
});
program.uniforms.forEach(uniform => {
types[uniform.name] = getInstanceType(uniform.type);
@@ -220,7 +223,7 @@ Effect.registerEffect = function(json) {
// add all the defines too, for now
programs.forEach(program => {
program.defines.forEach(define => {
properties[define.name] = define.type === 'boolean' ? false : 0;
properties[define.name] = getInstanceCtor(define.type)();
});
});
cc.Class({
@@ -261,7 +264,7 @@ Effect.parseEffect = function(json) {
// defines
let defines = programs.reduce((acc, cur) => acc = acc.concat(cur.defines), []);
defines = cloneObjArray(defines);
defines.forEach(d => d.value = (d.type === 'boolean' ? false : 0));
defines.forEach(d => d.value = getInstanceCtor(d.type)());
// extensions
let extensions = programs.reduce((acc, cur) => acc = acc.concat(cur.extensions), []);
extensions = cloneObjArray(extensions);
+248 -4
View File
@@ -2,15 +2,259 @@
#include <gamma-correction.frag>
uniform vec3 eye;
uniform vec4 color;
varying vec3 normal_w;
varying vec3 pos_w;
varying vec3 pos_l;
mat3 transpose(mat3 v) {
mat3 tmp;
tmp[0] = vec3(v[0].x, v[1].x, v[2].x);
tmp[1] = vec3(v[0].y, v[1].y, v[2].y);
tmp[2] = vec3(v[0].z, v[1].z, v[2].z);
return tmp;
}
void ClipQuadToHorizon(inout vec3 L[5], out int n) {
// detect clipping config
int config = 0;
if (L[0].z > 0.0) config += 1;
if (L[1].z > 0.0) config += 2;
if (L[2].z > 0.0) config += 4;
if (L[3].z > 0.0) config += 8;
config = 15;
// clip
n = 0;
if (config == 0)
{
// clip all
}
else if (config == 1) // V1 clip V2 V3 V4
{
n = 3;
L[1] = -L[1].z * L[0] + L[0].z * L[1];
L[2] = -L[3].z * L[0] + L[0].z * L[3];
}
else if (config == 2) // V2 clip V1 V3 V4
{
n = 3;
L[0] = -L[0].z * L[1] + L[1].z * L[0];
L[2] = -L[2].z * L[1] + L[1].z * L[2];
}
else if (config == 3) // V1 V2 clip V3 V4
{
n = 4;
L[2] = -L[2].z * L[1] + L[1].z * L[2];
L[3] = -L[3].z * L[0] + L[0].z * L[3];
}
else if (config == 4) // V3 clip V1 V2 V4
{
n = 3;
L[0] = -L[3].z * L[2] + L[2].z * L[3];
L[1] = -L[1].z * L[2] + L[2].z * L[1];
}
else if (config == 5) // V1 V3 clip V2 V4) impossible
{
n = 0;
}
else if (config == 6) // V2 V3 clip V1 V4
{
n = 4;
L[0] = -L[0].z * L[1] + L[1].z * L[0];
L[3] = -L[3].z * L[2] + L[2].z * L[3];
}
else if (config == 7) // V1 V2 V3 clip V4
{
n = 5;
L[4] = -L[3].z * L[0] + L[0].z * L[3];
L[3] = -L[3].z * L[2] + L[2].z * L[3];
}
else if (config == 8) // V4 clip V1 V2 V3
{
n = 3;
L[0] = -L[0].z * L[3] + L[3].z * L[0];
L[1] = -L[2].z * L[3] + L[3].z * L[2];
L[2] = L[3];
}
else if (config == 9) // V1 V4 clip V2 V3
{
n = 4;
L[1] = -L[1].z * L[0] + L[0].z * L[1];
L[2] = -L[2].z * L[3] + L[3].z * L[2];
}
else if (config == 10) // V2 V4 clip V1 V3) impossible
{
n = 0;
}
else if (config == 11) // V1 V2 V4 clip V3
{
n = 5;
L[4] = L[3];
L[3] = -L[2].z * L[3] + L[3].z * L[2];
L[2] = -L[2].z * L[1] + L[1].z * L[2];
}
else if (config == 12) // V3 V4 clip V1 V2
{
n = 4;
L[1] = -L[1].z * L[2] + L[2].z * L[1];
L[0] = -L[0].z * L[3] + L[3].z * L[0];
}
else if (config == 13) // V1 V3 V4 clip V2
{
n = 5;
L[4] = L[3];
L[3] = L[2];
L[2] = -L[1].z * L[2] + L[2].z * L[1];
L[1] = -L[1].z * L[0] + L[0].z * L[1];
}
else if (config == 14) // V2 V3 V4 clip V1
{
n = 5;
L[4] = -L[0].z * L[3] + L[3].z * L[0];
L[0] = -L[0].z * L[1] + L[1].z * L[0];
}
else if (config == 15) // V1 V2 V3 V4
{
n = 4;
}
if (n == 3)
L[3] = L[0];
if (n == 4)
L[4] = L[0];
}
// https://eheitzresearch.wordpress.com/415-2/
float IntegrateEdge(vec3 v1, vec3 v2) {
float cosTheta = dot(v1, v2);
float theta = acos(cosTheta);
return cross(v1, v2).z * ((theta > 0.001) ? theta/sin(theta) : 4.0);
}
vec3 LTC_Evaluate(vec3 N, vec3 V, vec3 P, mat3 Minv, vec3 points[4]) {
// construct orthonormal basis around N
vec3 T1, T2;
T1 = normalize(V - N*dot(V, N));
T2 = cross(N, T1);
// rotate area light in (T1, T2, N) basis
Minv = Minv * transpose(mat3(T1, T2, N));
// polygon (allocate 5 vertices for clipping)
vec3 L[5];
L[0] = Minv * (points[0] - P);
L[1] = Minv * (points[1] - P);
L[2] = Minv * (points[2] - P);
L[3] = Minv * (points[3] - P);
int n;
ClipQuadToHorizon(L, n);
if (n == 0)
return vec3(0, 0, 0);
// project onto sphere
L[0] = normalize(L[0]);
L[1] = normalize(L[1]);
L[2] = normalize(L[2]);
L[3] = normalize(L[3]);
L[4] = normalize(L[4]);
// integrate
float sum = 0.0;
sum += IntegrateEdge(L[0], L[1]);
sum += IntegrateEdge(L[1], L[2]);
sum += IntegrateEdge(L[2], L[3]);
if (n >= 4)
sum += IntegrateEdge(L[3], L[4]);
if (n == 5)
sum += IntegrateEdge(L[4], L[0]);
sum = max(0.0, sum);
vec3 Lo_i = vec3(sum, sum, sum);
return Lo_i;
}
#if PLANE_CONTROLLER
#include <common.frag>
uniform float width;
uniform float height;
uniform float rotx;
uniform float roty;
uniform vec3 center;
vec3 rotation_x(vec3 v, float a) {
vec3 r;
r.x = v.x;
r.y = v.y*cos(a) - v.z*sin(a);
r.z = v.y*sin(a) + v.z*cos(a);
return r;
}
vec3 rotation_y(vec3 v, float a) {
vec3 r;
r.x = v.x*cos(a) + v.z*sin(a);
r.y = v.y;
r.z = -v.x*sin(a) + v.z*cos(a);
return r;
}
vec3 rotation_xy(vec3 v, float ax, float ay) {
return rotation_y(rotation_x(v, ax), ay);
}
struct Rect {
vec3 center;
vec3 dirx;
vec3 diry;
float halfx;
float halfy;
};
void InitRect(out Rect rect)
{
rect.center = center;
rect.dirx = rotation_xy(vec3(1, 0, 0), rotx * PI2, roty * PI2);
rect.diry = rotation_xy(vec3(0, 1, 0), rotx * PI2, roty * PI2);
rect.halfx = 0.5 * width;
rect.halfy = 0.5 * height;
}
void InitRectPoints(Rect rect, out vec3 points[4])
{
vec3 ex = rect.halfx * rect.dirx;
vec3 ey = rect.halfy * rect.diry;
points[0] = rect.center - ex - ey;
points[1] = rect.center + ex - ey;
points[2] = rect.center + ex + ey;
points[3] = rect.center - ex + ey;
}
#endif
void main () {
vec3 N = normalize(normal_w);
vec3 L = normalize(vec3(1, 2, 3));
vec3 N = normalize(normal_w);
vec3 V = normalize(eye - pos_w);
vec3 diffuse = color.rgb * (0.2 + max(0.0, dot(N, L)) * 0.8);
// vec3 L = normalize(vec3(1, 2, 3));
// vec3 diffuse = color.rgb * (0.2 + max(0.0, dot(N, L)) * 0.8);
gl_FragColor = linearToGammaSpaceRGBA(vec4(diffuse, color.a));
vec3 points[4];
#if PLANE_CONTROLLER
Rect rect;
InitRect(rect);
InitRectPoints(rect, points);
#else
// { center: (30, 40, 50), width: 40, height: 40, rotx: -0.125, roty: 0.125 }
points[0] = vec3(25.86, 25.86, 74.14);
points[1] = vec3(54.14, 25.86, 45.86);
points[2] = vec3(34.14, 54.14, 25.86);
points[3] = vec3(5.86, 54.14, 54.14);
#endif
vec3 diffuse = color.rgb * (0.2 + LTC_Evaluate(N, V, pos_l, mat3(1), points) * 0.8);
gl_FragColor = linearToGammaSpaceRGBA(vec4(diffuse, color.a));
}
+6 -5
View File
@@ -8,14 +8,15 @@ uniform mat4 viewProj;
uniform mat3 normalMatrix;
varying vec3 normal_w;
varying vec3 pos_w;
varying vec3 pos_l;
void main () {
vec4 pos = vec4(a_position, 1);
pos = viewProj * model * pos;
pos_l = a_position;
pos_w = (model * pos).xyz;
normal_w = normalMatrix * a_normal;
vec4 normal = vec4(a_normal, 0);
normal_w = normalMatrix * normal.xyz;
gl_Position = pos;
gl_Position = viewProj * model * pos;
}
+1 -1
View File
@@ -23,7 +23,7 @@ uniform vec3 eye;
varying vec3 pos_w;
varying vec3 normal_w;
#if USE_NORMAL_TEXTURE || USE_ALBEDO_TEXTURE || USE_MRA_TEXTURE || USE_METALLIC_TEXTURE || USE_ROUGHNESS_TEXTURE || USE_AO_TEXTURE || USE_EMISSIVE_TEXTURE
#if USE_ALBEDO_TEXTURE || USE_NORMAL_TEXTURE || USE_MRA_TEXTURE || USE_METALLIC_TEXTURE || USE_ROUGHNESS_TEXTURE || USE_AO_TEXTURE || USE_EMISSIVE_TEXTURE
varying vec2 uv0;
#endif
+2 -2
View File
@@ -10,7 +10,7 @@ uniform mat4 model;
uniform mat4 viewProj;
uniform mat3 normalMatrix;
#if USE_NORMAL_TEXTURE || USE_ALBEDO_TEXTURE || USE_MRA_TEXTURE || USE_METALLIC_TEXTURE || USE_ROUGHNESS_TEXTURE || USE_AO_TEXTURE || USE_EMISSIVE_TEXTURE
#if USE_ALBEDO_TEXTURE || USE_NORMAL_TEXTURE || USE_MRA_TEXTURE || USE_METALLIC_TEXTURE || USE_ROUGHNESS_TEXTURE || USE_AO_TEXTURE || USE_EMISSIVE_TEXTURE
attribute vec2 a_uv0;
uniform vec2 mainTiling;
uniform vec2 mainOffset;
@@ -44,7 +44,7 @@ void main () {
pos_w = (model * pos).xyz;
pos = viewProj * model * pos;
#if USE_NORMAL_TEXTURE || USE_ALBEDO_TEXTURE || USE_MRA_TEXTURE || USE_METALLIC_TEXTURE || USE_ROUGHNESS_TEXTURE || USE_AO_TEXTURE || USE_EMISSIVE_TEXTURE
#if USE_ALBEDO_TEXTURE || USE_NORMAL_TEXTURE || USE_MRA_TEXTURE || USE_METALLIC_TEXTURE || USE_ROUGHNESS_TEXTURE || USE_AO_TEXTURE || USE_EMISSIVE_TEXTURE
uv0 = a_uv0 * mainTiling + mainOffset;
#endif
+5
View File
@@ -40,6 +40,11 @@ class Node extends BaseNode {
EventTarget.call(this);
}
onRestore() {
super.onRestore();
this.invalidateChildren();
}
// ===============================
// hierarchy
// ===============================