From 0351dec2264b6af2cfd2775f207e50dfe1fe2913 Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Thu, 24 Jan 2019 16:39:40 +0800 Subject: [PATCH] fixed light pipeline --- cocos/3d/builtin/effects.js | 148 +--------------------- cocos/3d/framework/light-component.ts | 8 +- cocos/pipeline/render-pipeline.ts | 61 ++++++--- cocos/renderer/core/program-lib.ts | 1 - cocos/renderer/scene/directional-light.ts | 15 ++- cocos/renderer/scene/light.ts | 12 +- cocos/renderer/scene/point-light.ts | 11 +- cocos/renderer/scene/render-scene.ts | 27 ++-- cocos/renderer/scene/spot-light.ts | 20 ++- playground/simple.html | 4 +- tslint.json | 5 +- 11 files changed, 119 insertions(+), 193 deletions(-) diff --git a/cocos/3d/builtin/effects.js b/cocos/3d/builtin/effects.js index 07d0cf0621..fb49be5870 100644 --- a/cocos/3d/builtin/effects.js +++ b/cocos/3d/builtin/effects.js @@ -1,142 +1,12 @@ // absolute essential effects export default [ - { - "name": "builtin-effect-pbr", - "techniques": [{"passes":[{"program":"builtin-effect-pbr|pbr-vs:vert|pbr-fs:frag", "properties":{"albedo":{"type":17, "value":[1, 1, 1, 1]}, "mainTiling":{"type":14, "value":[1, 1]}, "mainOffset":{"type":14, "value":[0, 0]}, "albedo_texture":{"type":29, "value":"grey"}, "metallic":{"type":13, "value":[0.5]}, "metallic_texture":{"type":29, "value":"grey"}, "roughness":{"type":13, "value":[0.7]}, "roughness_texture":{"type":29, "value":"grey"}, "ao":{"type":13, "value":[0.2]}, "ao_texture":{"type":29, "value":"grey"}, "mra_texture":{"type":29, "value":"grey"}, "emissive":{"type":17, "value":[0, 0, 0, 0]}, "emissive_texture":{"type":29, "value":"grey"}, "normal_texture":{"type":29, "value":"normal"}, "diffuseEnvTexture":{"type":32, "value":"white-cube"}, "specularEnvTexture":{"type":32, "value":"white-cube"}, "brdfLUT":{"type":29, "value":"grey"}, "maxReflectionLod":{"type":13, "value":[9]}, "alphaTestThreshold":{"type":13, "value":[0]}}}, {"program":"builtin-effect-pbr|shadow-depth-vs:vert|shadow-depth-fs:frag"}]}], - "shaders": [ - { - "name": "builtin-effect-pbr|pbr-vs:vert|pbr-fs:frag", - "vert": "\nattribute vec3 a_position;\nattribute vec3 a_normal;\nvarying vec3 pos_w;\nvarying vec3 normal_w;\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_cameraPos;\n uniform vec4 cc_dirLightDirection[4];\n uniform vec4 cc_dirLightColor[4];\n uniform vec4 cc_pointLightPositionAndRange[4];\n uniform vec4 cc_pointLightColor[4];\n uniform vec4 cc_spotLightPositionAndRange[4];\n uniform vec4 cc_spotLightDirection[4];\n uniform vec4 cc_spotLightColor[4];\n uniform mat4 cc_matViewProjLight;\n uniform vec4 cc_shadowParam1;\n uniform vec4 cc_shadowParam2;\n uniform mat4 cc_matWorld;\n uniform mat4 cc_matWorldIT;\n uniform vec2 mainTiling;\n uniform vec2 mainOffset;\n#if USE_ALBEDO_TEXTURE || USE_NORMAL_TEXTURE || USE_MRA_TEXTURE || USE_METALLIC_TEXTURE || USE_ROUGHNESS_TEXTURE || USE_AO_TEXTURE || USE_MRA_TEXTURE || USE_EMISSIVE_TEXTURE\n attribute vec2 a_texCoord1;\n varying vec2 uv0;\n#endif\n#if CC_USE_SKINNING\n \nattribute vec4 a_weights;\nattribute vec4 a_joints;\n uniform mat4 cc_matJoint[128];\n uniform vec4 cc_jointsTextureSize;\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 4.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = texture2D(cc_jointsTexture, vec2(dx * (x + 0.5), y));\n vec4 v2 = texture2D(cc_jointsTexture, vec2(dx * (x + 1.5), y));\n vec4 v3 = texture2D(cc_jointsTexture, vec2(dx * (x + 2.5), y));\n vec4 v4 = texture2D(cc_jointsTexture, vec2(dx * (x + 3.5), y));\n return mat4(v1, v2, v3, v4);\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n return cc_matJoint[int(i)];\n}\n#endif\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x) * a_weights.x +\n getBoneMatrix(a_joints.y) * a_weights.y +\n getBoneMatrix(a_joints.z) * a_weights.z +\n getBoneMatrix(a_joints.w) * a_weights.w;\n}\nvoid skinVertex(inout vec4 a1) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2, inout vec4 a3) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n a3 = m * a3;\n}\n#endif\n#if CC_USE_SHADOW_MAP\n \nvarying vec4 posLightspace;\nvarying float vDepth;\nvoid prepareShadowData(vec3 pos_w) {\n posLightspace = cc_matViewProjLight * vec4(pos_w, 1.0);\n vDepth = (posLightspace.z + cc_shadowParam1.x) / (cc_shadowParam1.x + cc_shadowParam1.y);\n}\n#endif\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n vec4 normal = vec4(a_normal, 0);\n #if CC_USE_SKINNING\n skinVertex(pos, normal);\n #endif\n pos_w = (cc_matWorld * pos).xyz;\n normal_w = (cc_matWorldIT * normal).xyz;\n #if USE_ALBEDO_TEXTURE || USE_NORMAL_TEXTURE || USE_MRA_TEXTURE || USE_METALLIC_TEXTURE || USE_ROUGHNESS_TEXTURE || USE_AO_TEXTURE || USE_MRA_TEXTURE || USE_EMISSIVE_TEXTURE\n uv0 = a_texCoord1 * mainTiling + mainOffset;\n #endif\n #if CC_USE_SHADOW_MAP\n prepareShadowData(pos_w);\n #endif\n return cc_matViewProj * cc_matWorld * pos;\n}\nvoid main() { gl_Position = vert(); }\n", - "frag": "\n#if USE_NORMAL_TEXTURE\n#extension GL_OES_standard_derivatives : enable\n#endif\n#if USE_TEX_LOD\n#extension GL_EXT_shader_texture_lod: enable\n#endif\n#define PI 3.14159265359\n#define PI2 6.28318530718\n#define EPSILON 1e-6\n#define LOG2 1.442695\n#define saturate(a) clamp( a, 0.0, 1.0 )\nvec3 gammaToLinearSpaceRGB(vec3 sRGB) { \n return sRGB * (sRGB * (sRGB * 0.305306011 + 0.682171111) + 0.012522878);\n}\nvec3 linearToGammaSpaceRGB(vec3 RGB) { \n vec3 S1 = sqrt(RGB);\n vec3 S2 = sqrt(S1);\n vec3 S3 = sqrt(S2);\n return 0.585122381 * S1 + 0.783140355 * S2 - 0.368262736 * S3;\n}\nvec4 gammaToLinearSpaceRGBA(vec4 sRGBA) {\n return vec4(gammaToLinearSpaceRGB(sRGBA.rgb), sRGBA.a);\n}\nvec4 linearToGammaSpaceRGBA(vec4 RGBA) {\n return vec4(linearToGammaSpaceRGB(RGBA.rgb), RGBA.a);\n}\nfloat gammaToLinearSpaceExact(float val) {\n if (val <= 0.04045) {\n return val / 12.92;\n } else if (val < 1.0) {\n return pow((val + 0.055) / 1.055, 2.4);\n } else {\n return pow(val, 2.2);\n }\n}\nfloat linearToGammaSpaceExact(float val) {\n if (val <= 0.0) {\n return 0.0;\n } else if (val <= 0.0031308) {\n return 12.92 * val;\n } else if (val < 1.0) {\n return 1.055 * pow(val, 0.4166667) - 0.055;\n } else {\n return pow(val, 0.45454545);\n }\n}\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_cameraPos;\n uniform vec4 cc_dirLightDirection[4];\n uniform vec4 cc_dirLightColor[4];\n uniform vec4 cc_pointLightPositionAndRange[4];\n uniform vec4 cc_pointLightColor[4];\n uniform vec4 cc_spotLightPositionAndRange[4];\n uniform vec4 cc_spotLightDirection[4];\n uniform vec4 cc_spotLightColor[4];\n uniform mat4 cc_matViewProjLight;\n uniform vec4 cc_shadowParam1;\n uniform vec4 cc_shadowParam2;\nstruct LightInfo {\n vec3 lightDir;\n vec3 radiance;\n};\nLightInfo computeDirectionalLighting(\n vec4 lightDirection,\n vec4 lightColor\n) {\n LightInfo ret;\n ret.lightDir = -normalize(lightDirection.xyz);\n ret.radiance = lightColor.rgb;\n return ret;\n}\nLightInfo computePointLighting(\n vec3 worldPosition,\n vec4 lightPositionAndRange,\n vec4 lightColor\n) {\n LightInfo ret;\n vec3 lightDir = lightPositionAndRange.xyz - worldPosition;\n float attenuation = max(0.0, 1.0 - length(lightDir) / lightPositionAndRange.w);\n ret.lightDir = normalize(lightDir);\n ret.radiance = lightColor.rgb * attenuation;\n return ret;\n}\nLightInfo computeSpotLighting(\n vec3 worldPosition,\n vec4 lightPositionAndRange,\n vec4 lightDirection,\n vec4 lightColor\n) {\n LightInfo ret;\n vec3 lightDir = lightPositionAndRange.xyz - worldPosition;\n float attenuation = max(0., 1.0 - length(lightDir) / lightPositionAndRange.w);\n float cosConeAngle = max(0., dot(lightDirection.xyz, -lightDir));\n cosConeAngle = cosConeAngle < lightDirection.w ? 0.0 : cosConeAngle;\n cosConeAngle = pow(cosConeAngle, lightColor.w);\n ret.lightDir = normalize(lightDir);\n ret.radiance = lightColor.rgb * attenuation * cosConeAngle;\n return ret;\n}\nvec3 unpackNormal(vec4 nmap) {\n return nmap.xyz * 2.0 - 1.0;\n}\nvec3 unpackRGBE(vec4 rgbe) {\n return rgbe.rgb * pow(2.0, rgbe.a * 255.0 - 128.0);\n}\n#if CC_USE_SHADOW_MAP\n \nvec4 packDepthToRGBA(float depth) {\n vec4 ret = vec4(1.0, 255.0, 65025.0, 160581375.0) * depth;\n ret = fract(ret);\n ret -= ret.yzww * vec4(1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0, 0.0);\n return ret;\n}\nfloat unpackRGBAToDepth(vec4 color) {\n return dot(color, vec4(1.0, 1.0 / 255.0, 1.0 / 65025.0, 1.0 / 160581375.0));\n}\n \nvarying vec4 posLightspace;\nvarying float vDepth;\nuniform sampler2D cc_shadowMap;\nfloat computeShadow(sampler2D shadowMap, vec4 posLightspace, float bias) {\n vec3 projCoords = posLightspace.xyz / posLightspace.w;\n projCoords = projCoords * 0.5 + 0.5;\n float closestDepth = unpackRGBAToDepth(texture2D(shadowMap, projCoords.xy));\n float currentDepth = projCoords.z;\n float shadow = (currentDepth - bias > closestDepth) ? 0.0 : 1.0;\n return shadow;\n}\nfloat computeFallOff(float esm, vec2 coords, float frustumEdgeFalloff) {\n float mask = smoothstep(1.0 - frustumEdgeFalloff, 1.0, clamp(dot(coords, coords), 0.0, 1.0));\n return mix(esm, 1.0, mask);\n}\nfloat computeShadowESM(sampler2D shadowMap, vec4 posLightspace, float vDepth, float depthScale, float darkness, float frustumEdgeFalloff) {\n vec2 projCoords = posLightspace.xy / posLightspace.w;\n vec2 shadowUV = projCoords * 0.5 + vec2(0.5);\n if (shadowUV.x < 0.0 || shadowUV.x > 1.0 || shadowUV.y < 0.0 || shadowUV.y > 1.0) {\n return 1.0;\n }\n float currentDepth = clamp(vDepth, 0.0, 1.0);\n float closestDepth = unpackRGBAToDepth(texture2D(shadowMap, shadowUV));\n \n float esm = clamp(exp(-depthScale * (currentDepth - closestDepth)), 1.0 - darkness, 1.0);\n return computeFallOff(esm, projCoords, frustumEdgeFalloff);\n}\nfloat computeShadowPCF(sampler2D shadowMap, vec4 posLightspace, float vDepth, float darkness, vec2 texelSize, float frustumEdgeFalloff) {\n vec2 projCoords = posLightspace.xy / posLightspace.w;\n vec2 shadowUV = projCoords * 0.5 + vec2(0.5);\n if (shadowUV.x < 0.0 || shadowUV.x > 1.0 || shadowUV.y < 0.0 || shadowUV.y > 1.0) {\n return 1.0;\n }\n float currentDepth = clamp(vDepth, 0.0, 1.0);\n float visibility = 1.0;\n vec2 poissonDisk[4];\n poissonDisk[0] = vec2(-0.94201624, -0.39906216);\n poissonDisk[1] = vec2(0.94558609, -0.76890725);\n poissonDisk[2] = vec2(-0.094184101, -0.92938870);\n poissonDisk[3] = vec2(0.34495938, 0.29387760);\n if (unpackRGBAToDepth(texture2D(shadowMap, shadowUV + poissonDisk[0] * texelSize)) < currentDepth) visibility -= 0.25;\n if (unpackRGBAToDepth(texture2D(shadowMap, shadowUV + poissonDisk[1] * texelSize)) < currentDepth) visibility -= 0.25;\n if (unpackRGBAToDepth(texture2D(shadowMap, shadowUV + poissonDisk[2] * texelSize)) < currentDepth) visibility -= 0.25;\n if (unpackRGBAToDepth(texture2D(shadowMap, shadowUV + poissonDisk[3] * texelSize)) < currentDepth) visibility -= 0.25;\n return computeFallOff(min(1.0, visibility + 1.0 - darkness), projCoords, frustumEdgeFalloff);\n}\nfloat computeShadow() {\n return computeShadowESM(cc_shadowMap, posLightspace, vDepth, cc_shadowParam1.w, cc_shadowParam2.x, cc_shadowParam2.y);\n}\n#endif\nvarying vec3 pos_w;\nvarying vec3 normal_w;\n#if USE_ALBEDO_TEXTURE || USE_NORMAL_TEXTURE || USE_MRA_TEXTURE || USE_METALLIC_TEXTURE || USE_ROUGHNESS_TEXTURE || USE_AO_TEXTURE || USE_EMISSIVE_TEXTURE\n varying vec2 uv0;\n#endif\n#if USE_IBL\n uniform samplerCube diffuseEnvTexture;\n uniform samplerCube specularEnvTexture;\n uniform sampler2D brdfLUT;\n#endif\n uniform vec4 albedo;\n uniform float metallic;\n uniform float roughness;\n uniform float ao;\n uniform float maxReflectionLod;\n uniform vec4 emissive;\n uniform float alphaTestThreshold;\n#if USE_ALBEDO_TEXTURE\n uniform sampler2D albedo_texture;\n#endif\n#if USE_MRA_TEXTURE\n uniform sampler2D mra_texture;\n#endif\n#if USE_METALLIC_TEXTURE\n uniform sampler2D metallic_texture;\n#endif\n#if USE_ROUGHNESS_TEXTURE\n uniform sampler2D roughness_texture;\n#endif\n#if USE_AO_TEXTURE\n uniform sampler2D ao_texture;\n#endif\n#if USE_EMISSIVE && USE_EMISSIVE_TEXTURE\n uniform sampler2D emissive_texture;\n#endif\n#if USE_NORMAL_TEXTURE\n uniform sampler2D normal_texture;\n \n vec3 getNormalFromTexture() {\n vec3 tangentNormal = texture2D(normal_texture, uv0).rgb * 2.0 - 1.0;\n vec3 q1 = dFdx(pos_w);\n vec3 q2 = dFdy(pos_w);\n vec2 st1 = dFdx(uv0);\n vec2 st2 = dFdy(uv0);\n vec3 N = normalize(normal_w);\n vec3 T = normalize(q1*st2.t - q2*st1.t);\n vec3 B = -normalize(cross(N, T));\n mat3 TBN = mat3(T, B, N);\n return normalize(TBN * tangentNormal);\n }\n#endif\nfloat distributionGGX(vec3 N, vec3 H, float roughness) {\n float a = roughness * roughness;\n float a2 = a * a;\n float NdotH = max(dot(N, H), 0.0);\n float NdotH2 = NdotH * NdotH;\n float nom = a2;\n float denom = (NdotH2 * (a2 - 1.0) + 1.0);\n denom = 3.14159265359 * denom * denom;\n return nom / denom;\n}\nfloat geometrySchlickGGX(float NdotV, float roughness) {\n float r = (roughness + 1.0);\n float k = (r * r) / 8.0;\n float nom = NdotV;\n float denom = NdotV * (1.0 - k) + k;\n return nom / denom;\n}\nfloat geometrySmith(vec3 N, vec3 V, vec3 L, float roughness) {\n float NdotV = max(dot(N, V), 0.0);\n float NdotL = max(dot(N, L), 0.0);\n float ggx2 = geometrySchlickGGX(NdotV, roughness);\n float ggx1 = geometrySchlickGGX(NdotL, roughness);\n return ggx1 * ggx2;\n}\nvec3 fresnelSchlick(float cosTheta, vec3 F0) {\n float fresnel = exp2((-5.55473 * cosTheta - 6.98316) * cosTheta);\n return F0 + (1.0 - F0) * fresnel;\n}\nvec3 fresnelSchlickRoughness(float cosTheta, vec3 F0, float roughness) {\n float fresnel = exp2((-5.55473 * cosTheta - 6.98316) * cosTheta);\n return F0 + (max(vec3(1.0 - roughness), F0) - F0) * fresnel;\n}\nvec3 brdf(LightInfo lightInfo, vec3 N, vec3 V, vec3 F0, vec3 albedo, float metallic, float roughness) {\n vec3 H = normalize(V + lightInfo.lightDir);\n float NDF = distributionGGX(N, H, roughness);\n float G = geometrySmith(N, V, lightInfo.lightDir, roughness);\n vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);\n vec3 nominator = NDF * G * F;\n float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, lightInfo.lightDir), 0.0) + 0.001; \n vec3 specular = nominator / denominator;\n \n vec3 kS = F;\n \n \n \n vec3 kD = vec3(1.0) - kS;\n \n \n \n kD *= 1.0 - metallic;\n float NdotL = max(dot(N, lightInfo.lightDir), 0.0);\n return (kD * albedo / 3.14159265359 + specular) * lightInfo.radiance * NdotL;\n}\nvec4 frag() {\n float opacity = 1.0;\n #if USE_ALBEDO_TEXTURE\n vec4 baseColor = albedo * gammaToLinearSpaceRGBA(texture2D(albedo_texture, uv0));\n vec3 albedo = baseColor.rgb;\n opacity = baseColor.a;\n #else\n opacity = albedo.a;\n vec3 albedo = albedo.rgb;\n #endif\n #if USE_ALPHA_TEST\n if(opacity < alphaTestThreshold) discard;\n #endif\n #if USE_MRA_TEXTURE\n vec3 metalRoughness = texture2D(mra_texture, uv0).rgb;\n float metallic = metalRoughness.r;\n float roughness = metalRoughness.g;\n float ao = metalRoughness.b;\n #else\n #if USE_METALLIC_TEXTURE\n float metallic = texture2D(metallic_texture, uv0).r;\n #endif\n #if USE_ROUGHNESS_TEXTURE\n float roughness = texture2D(roughness_texture, uv0).r;\n #endif\n #if USE_AO_TEXTURE\n float ao = texture2D(ao_texture, uv0).r;\n #endif\n #endif\n vec3 N = normalize(normal_w);\n #if USE_NORMAL_TEXTURE\n N = getNormalFromTexture();\n #endif\n vec3 V = normalize(cc_cameraPos.xyz - pos_w);\n \n \n vec3 F0 = vec3(0.04);\n F0 = mix(F0, albedo, metallic);\n \n vec3 Lo = vec3(0.0);\n #if CC_NUM_DIR_LIGHTS > 0\n for (int i = 0; i < CC_NUM_DIR_LIGHTS; i++) {\n LightInfo info = computeDirectionalLighting(cc_dirLightDirection[i], cc_dirLightColor[i]);\n Lo += brdf(info, N, V, F0, albedo, metallic, roughness);\n }\n #endif\n #if CC_NUM_POINT_LIGHTS > 0\n for (int i = 0; i < CC_NUM_POINT_LIGHTS; i++) {\n LightInfo info = computePointLighting(pos_w, cc_pointLightPositionAndRange[i], cc_pointLightColor[i]);\n Lo += brdf(info, N, V, F0, albedo, metallic, roughness);\n }\n #endif\n #if CC_NUM_SPOT_LIGHTS > 0\n for (int i = 0; i < CC_NUM_SPOT_LIGHTS; i++) {\n LightInfo info = computeSpotLighting(pos_w, cc_spotLightPositionAndRange[i], cc_spotLightDirection[i], cc_spotLightColor[i]);\n Lo += brdf(info, N, V, F0, albedo, metallic, roughness);\n }\n #endif\n #if USE_EMISSIVE\n vec3 emissiveColor = emissive.rgb;\n #if USE_EMISSIVE_TEXTURE\n emissiveColor *= gammaToLinearSpaceRGB(texture2D(emissive_texture, uv0).rgb);\n #endif\n Lo += emissiveColor;\n #endif\n \n vec3 ambient = vec3(0.03) * albedo * ao;\n #if USE_IBL\n \n vec3 F = fresnelSchlickRoughness(max(dot(N, V), 0.0), F0, roughness);\n vec3 kS = F;\n vec3 kD = vec3(1.0) - kS;\n kD *= 1.0 - metallic;\n #if USE_RGBE_IBL_DIFFUSE\n vec3 diffuseEnv = unpackRGBE(textureCube(diffuseEnvTexture, N));\n #else\n vec3 diffuseEnv = textureCube(diffuseEnvTexture, N).rgb;\n #endif\n vec3 diffuse = diffuseEnv * albedo;\n \n vec3 R = reflect(-V, N);\n #if USE_TEX_LOD\n #if USE_RGBE_IBL_SPECULAR\n vec3 specularEnv = unpackRGBE(textureCubeLodEXT(specularEnvTexture, R, roughness * maxReflectionLod));\n #else\n vec3 specularEnv = textureCubeLodEXT(specularEnvTexture, R, roughness * maxReflectionLod).rgb;\n #endif\n #else\n #if USE_RGBE_IBL_SPECULAR\n vec3 specularEnv = unpackRGBE(textureCube(specularEnvTexture, R));\n #else\n vec3 specularEnv = textureCube(specularEnvTexture, R).rgb;\n #endif\n #endif\n vec2 brdf = texture2D(brdfLUT, vec2(max(dot(N, V), 0.0), 1.0 - roughness)).rg;\n vec3 specular = specularEnv * (F * brdf.x + brdf.y);\n ambient = (kD * diffuse + specular) * ao;\n #endif\n vec3 color = ambient + Lo;\n #if CC_USE_SHADOW_MAP\n color *= computeShadow();\n #endif\n \n color = color / (color + vec3(1.0));\n \n vec4 finalColor = vec4(color, opacity);\n return linearToGammaSpaceRGBA(finalColor);\n}\nvoid main() { gl_FragColor = frag(); }\n", - "defines": [ - {"name":"USE_ALBEDO_TEXTURE", "type":"boolean", "defines":[]}, - {"name":"CC_USE_SKINNING", "type":"boolean", "defines":[]}, - {"name":"CC_USE_JOINTS_TEXTURE", "type":"boolean", "defines":["CC_USE_SKINNING"]}, - {"name":"CC_USE_SHADOW_MAP", "type":"boolean", "defines":[]}, - {"name":"USE_NORMAL_TEXTURE", "type":"boolean", "defines":[]}, - {"name":"USE_TEX_LOD", "type":"boolean", "defines":[]}, - {"name":"USE_IBL", "type":"boolean", "defines":[]}, - {"name":"USE_MRA_TEXTURE", "type":"boolean", "defines":[]}, - {"name":"USE_METALLIC_TEXTURE", "type":"boolean", "defines":[]}, - {"name":"USE_ROUGHNESS_TEXTURE", "type":"boolean", "defines":[]}, - {"name":"USE_AO_TEXTURE", "type":"boolean", "defines":[]}, - {"name":"USE_EMISSIVE", "type":"boolean", "defines":[]}, - {"name":"USE_EMISSIVE_TEXTURE", "type":"boolean", "defines":["USE_EMISSIVE"]}, - {"name":"USE_ALPHA_TEST", "type":"boolean", "defines":[]}, - {"name":"CC_NUM_DIR_LIGHTS", "type":"number", "defines":[]}, - {"name":"CC_NUM_POINT_LIGHTS", "type":"number", "defines":[]}, - {"name":"CC_NUM_SPOT_LIGHTS", "type":"number", "defines":[]}, - {"name":"USE_RGBE_IBL_DIFFUSE", "type":"boolean", "defines":["USE_IBL"]}, - {"name":"USE_RGBE_IBL_SPECULAR", "type":"boolean", "defines":["USE_IBL"]} - ], - "blocks": [ - {"name": "TexCoords", "size": 16, "defines": [], "binding": 0, "members": [ - {"name":"mainTiling", "type":14, "count":1, "size":8}, - {"name":"mainOffset", "type":14, "count":1, "size":8} - ]}, - {"name": "Constants", "size": 52, "defines": [], "binding": 1, "members": [ - {"name":"albedo", "type":16, "count":1, "size":16}, - {"name":"metallic", "type":13, "count":1, "size":4}, - {"name":"roughness", "type":13, "count":1, "size":4}, - {"name":"ao", "type":13, "count":1, "size":4}, - {"name":"maxReflectionLod", "type":13, "count":1, "size":4}, - {"name":"emissive", "type":16, "count":1, "size":16}, - {"name":"alphaTestThreshold", "type":13, "count":1, "size":4} - ]} - ], - "samplers": [ - {"name":"diffuseEnvTexture", "type":32, "count":1, "defines":["USE_IBL"], "binding":2}, - {"name":"specularEnvTexture", "type":32, "count":1, "defines":["USE_IBL"], "binding":3}, - {"name":"brdfLUT", "type":29, "count":1, "defines":["USE_IBL"], "binding":4}, - {"name":"albedo_texture", "type":29, "count":1, "defines":["USE_ALBEDO_TEXTURE"], "binding":5}, - {"name":"mra_texture", "type":29, "count":1, "defines":["USE_MRA_TEXTURE"], "binding":6}, - {"name":"metallic_texture", "type":29, "count":1, "defines":["USE_METALLIC_TEXTURE"], "binding":7}, - {"name":"roughness_texture", "type":29, "count":1, "defines":["USE_ROUGHNESS_TEXTURE"], "binding":8}, - {"name":"ao_texture", "type":29, "count":1, "defines":["USE_AO_TEXTURE"], "binding":9}, - {"name":"emissive_texture", "type":29, "count":1, "defines":["USE_EMISSIVE", "USE_EMISSIVE_TEXTURE"], "binding":10}, - {"name":"normal_texture", "type":29, "count":1, "defines":["USE_NORMAL_TEXTURE"], "binding":11} - ], - "dependencies": {"USE_NORMAL_TEXTURE":"OES_standard_derivatives", "USE_TEX_LOD":"EXT_shader_texture_lod"} - }, - { - "name": "builtin-effect-pbr|shadow-depth-vs:vert|shadow-depth-fs:frag", - "vert": "\nattribute vec3 a_position;\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_cameraPos;\n uniform vec4 cc_dirLightDirection[4];\n uniform vec4 cc_dirLightColor[4];\n uniform vec4 cc_pointLightPositionAndRange[4];\n uniform vec4 cc_pointLightColor[4];\n uniform vec4 cc_spotLightPositionAndRange[4];\n uniform vec4 cc_spotLightDirection[4];\n uniform vec4 cc_spotLightColor[4];\n uniform mat4 cc_matViewProjLight;\n uniform vec4 cc_shadowParam1;\n uniform vec4 cc_shadowParam2;\n uniform mat4 cc_matWorld;\n uniform mat4 cc_matWorldIT;\nvarying float vDepth;\n#if CC_USE_SKINNING\n \nattribute vec4 a_weights;\nattribute vec4 a_joints;\n uniform mat4 cc_matJoint[128];\n uniform vec4 cc_jointsTextureSize;\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 4.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = texture2D(cc_jointsTexture, vec2(dx * (x + 0.5), y));\n vec4 v2 = texture2D(cc_jointsTexture, vec2(dx * (x + 1.5), y));\n vec4 v3 = texture2D(cc_jointsTexture, vec2(dx * (x + 2.5), y));\n vec4 v4 = texture2D(cc_jointsTexture, vec2(dx * (x + 3.5), y));\n return mat4(v1, v2, v3, v4);\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n return cc_matJoint[int(i)];\n}\n#endif\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x) * a_weights.x +\n getBoneMatrix(a_joints.y) * a_weights.y +\n getBoneMatrix(a_joints.z) * a_weights.z +\n getBoneMatrix(a_joints.w) * a_weights.w;\n}\nvoid skinVertex(inout vec4 a1) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2, inout vec4 a3) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n a3 = m * a3;\n}\n#endif\nvec4 vert() {\n vec4 pos = vec4(a_position, 1);\n #if CC_USE_SKINNING\n mat4 skinMat = skinMatrix();\n pos = skinMat * pos;\n #endif\n \n vDepth = ((gl_Position.z + cc_shadowParam1.x) / (cc_shadowParam1.x + cc_shadowParam1.y)) + cc_shadowParam1.z;\n return cc_matViewProjLight * cc_matWorld * pos;\n}\nvoid main() { gl_Position = vert(); }\n", - "frag": "\nvarying float vDepth;\nvec4 packDepthToRGBA(float depth) {\n vec4 ret = vec4(1.0, 255.0, 65025.0, 160581375.0) * depth;\n ret = fract(ret);\n ret -= ret.yzww * vec4(1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0, 0.0);\n return ret;\n}\nfloat unpackRGBAToDepth(vec4 color) {\n return dot(color, vec4(1.0, 1.0 / 255.0, 1.0 / 65025.0, 1.0 / 160581375.0));\n}\nvec4 frag() {\n \n \n return packDepthToRGBA(vDepth);\n \n \n}\nvoid main() { gl_FragColor = frag(); }\n", - "defines": [ - {"name":"CC_USE_SKINNING", "type":"boolean", "defines":[]}, - {"name":"CC_USE_JOINTS_TEXTURE", "type":"boolean", "defines":["CC_USE_SKINNING"]} - ], - "blocks": [], - "samplers": [], - "dependencies": {} - } - ] - }, - { - "name": "builtin-effect-phong", - "techniques": [{"passes":[{"program":"builtin-effect-phong|phong-vs:vert|phong-fs:frag", "properties":{"diffuseColor":{"type":17, "value":[0.3, 0.3, 0.3, 1]}, "mainTiling":{"type":14, "value":[1, 1]}, "mainOffset":{"type":14, "value":[0, 0]}, "diffuse_texture":{"type":29, "value":"grey"}, "specularColor":{"type":17, "value":[0, 0, 0, 0]}, "specular_texture":{"type":29, "value":"grey"}, "emissiveColor":{"type":17, "value":[0, 0, 0, 0]}, "emissive_texture":{"type":29, "value":"grey"}, "glossiness":{"type":13, "value":[10]}, "normal_texture":{"type":29, "value":"normal"}, "alphaTestThreshold":{"type":13, "value":[0]}}}, {"program":"builtin-effect-phong|shadow-depth-vs:vert|shadow-depth-fs:frag"}]}], - "shaders": [ - { - "name": "builtin-effect-phong|phong-vs:vert|phong-fs:frag", - "vert": "\nattribute vec3 a_position;\nattribute vec3 a_normal;\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_cameraPos;\n uniform vec4 cc_dirLightDirection[4];\n uniform vec4 cc_dirLightColor[4];\n uniform vec4 cc_pointLightPositionAndRange[4];\n uniform vec4 cc_pointLightColor[4];\n uniform vec4 cc_spotLightPositionAndRange[4];\n uniform vec4 cc_spotLightDirection[4];\n uniform vec4 cc_spotLightColor[4];\n uniform mat4 cc_matViewProjLight;\n uniform vec4 cc_shadowParam1;\n uniform vec4 cc_shadowParam2;\n uniform mat4 cc_matWorld;\n uniform mat4 cc_matWorldIT;\nvarying vec3 normal_w;\nvarying vec3 pos_w;\n uniform vec2 mainTiling;\n uniform vec2 mainOffset;\n#if USE_DIFFUSE_TEXTURE || USE_NORMAL_TEXTURE || USE_EMISSIVE_TEXTURE\n attribute vec2 a_texCoord1;\n varying vec2 uv0;\n#endif\n#if CC_USE_SKINNING\n \nattribute vec4 a_weights;\nattribute vec4 a_joints;\n uniform mat4 cc_matJoint[128];\n uniform vec4 cc_jointsTextureSize;\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 4.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = texture2D(cc_jointsTexture, vec2(dx * (x + 0.5), y));\n vec4 v2 = texture2D(cc_jointsTexture, vec2(dx * (x + 1.5), y));\n vec4 v3 = texture2D(cc_jointsTexture, vec2(dx * (x + 2.5), y));\n vec4 v4 = texture2D(cc_jointsTexture, vec2(dx * (x + 3.5), y));\n return mat4(v1, v2, v3, v4);\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n return cc_matJoint[int(i)];\n}\n#endif\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x) * a_weights.x +\n getBoneMatrix(a_joints.y) * a_weights.y +\n getBoneMatrix(a_joints.z) * a_weights.z +\n getBoneMatrix(a_joints.w) * a_weights.w;\n}\nvoid skinVertex(inout vec4 a1) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2, inout vec4 a3) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n a3 = m * a3;\n}\n#endif\n#if CC_USE_SHADOW_MAP\n \nvarying vec4 posLightspace;\nvarying float vDepth;\nvoid prepareShadowData(vec3 pos_w) {\n posLightspace = cc_matViewProjLight * vec4(pos_w, 1.0);\n vDepth = (posLightspace.z + cc_shadowParam1.x) / (cc_shadowParam1.x + cc_shadowParam1.y);\n}\n#endif\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n vec4 normal = vec4(a_normal, 0);\n #if CC_USE_SKINNING\n skinVertex(pos, normal);\n #endif\n pos_w = (cc_matWorld * pos).xyz;\n normal_w = (cc_matWorldIT * normal).xyz;\n #if CC_USE_SHADOW_MAP\n prepareShadowData(pos_w);\n #endif\n #if USE_DIFFUSE_TEXTURE || USE_NORMAL_TEXTURE || USE_EMISSIVE_TEXTURE\n uv0 = a_texCoord1 * mainTiling + mainOffset;\n #endif\n return cc_matViewProj * cc_matWorld * pos;\n}\nvoid main() { gl_Position = vert(); }\n", - "frag": "\n#if USE_NORMAL_TEXTURE\n#extension GL_OES_standard_derivatives : enable\n#endif\n#define PI 3.14159265359\n#define PI2 6.28318530718\n#define EPSILON 1e-6\n#define LOG2 1.442695\n#define saturate(a) clamp( a, 0.0, 1.0 )\nvec3 gammaToLinearSpaceRGB(vec3 sRGB) { \n return sRGB * (sRGB * (sRGB * 0.305306011 + 0.682171111) + 0.012522878);\n}\nvec3 linearToGammaSpaceRGB(vec3 RGB) { \n vec3 S1 = sqrt(RGB);\n vec3 S2 = sqrt(S1);\n vec3 S3 = sqrt(S2);\n return 0.585122381 * S1 + 0.783140355 * S2 - 0.368262736 * S3;\n}\nvec4 gammaToLinearSpaceRGBA(vec4 sRGBA) {\n return vec4(gammaToLinearSpaceRGB(sRGBA.rgb), sRGBA.a);\n}\nvec4 linearToGammaSpaceRGBA(vec4 RGBA) {\n return vec4(linearToGammaSpaceRGB(RGBA.rgb), RGBA.a);\n}\nfloat gammaToLinearSpaceExact(float val) {\n if (val <= 0.04045) {\n return val / 12.92;\n } else if (val < 1.0) {\n return pow((val + 0.055) / 1.055, 2.4);\n } else {\n return pow(val, 2.2);\n }\n}\nfloat linearToGammaSpaceExact(float val) {\n if (val <= 0.0) {\n return 0.0;\n } else if (val <= 0.0031308) {\n return 12.92 * val;\n } else if (val < 1.0) {\n return 1.055 * pow(val, 0.4166667) - 0.055;\n } else {\n return pow(val, 0.45454545);\n }\n}\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_cameraPos;\n uniform vec4 cc_dirLightDirection[4];\n uniform vec4 cc_dirLightColor[4];\n uniform vec4 cc_pointLightPositionAndRange[4];\n uniform vec4 cc_pointLightColor[4];\n uniform vec4 cc_spotLightPositionAndRange[4];\n uniform vec4 cc_spotLightDirection[4];\n uniform vec4 cc_spotLightColor[4];\n uniform mat4 cc_matViewProjLight;\n uniform vec4 cc_shadowParam1;\n uniform vec4 cc_shadowParam2;\nstruct LightInfo {\n vec3 lightDir;\n vec3 radiance;\n};\nLightInfo computeDirectionalLighting(\n vec4 lightDirection,\n vec4 lightColor\n) {\n LightInfo ret;\n ret.lightDir = -normalize(lightDirection.xyz);\n ret.radiance = lightColor.rgb;\n return ret;\n}\nLightInfo computePointLighting(\n vec3 worldPosition,\n vec4 lightPositionAndRange,\n vec4 lightColor\n) {\n LightInfo ret;\n vec3 lightDir = lightPositionAndRange.xyz - worldPosition;\n float attenuation = max(0.0, 1.0 - length(lightDir) / lightPositionAndRange.w);\n ret.lightDir = normalize(lightDir);\n ret.radiance = lightColor.rgb * attenuation;\n return ret;\n}\nLightInfo computeSpotLighting(\n vec3 worldPosition,\n vec4 lightPositionAndRange,\n vec4 lightDirection,\n vec4 lightColor\n) {\n LightInfo ret;\n vec3 lightDir = lightPositionAndRange.xyz - worldPosition;\n float attenuation = max(0., 1.0 - length(lightDir) / lightPositionAndRange.w);\n float cosConeAngle = max(0., dot(lightDirection.xyz, -lightDir));\n cosConeAngle = cosConeAngle < lightDirection.w ? 0.0 : cosConeAngle;\n cosConeAngle = pow(cosConeAngle, lightColor.w);\n ret.lightDir = normalize(lightDir);\n ret.radiance = lightColor.rgb * attenuation * cosConeAngle;\n return ret;\n}\nstruct Lighting {\n vec3 diffuse;\n vec3 specular;\n};\nLighting brdf(LightInfo info, vec3 N, vec3 V, float glossiness) {\n Lighting res;\n res.diffuse = info.radiance * max(0.0, dot(N, info.lightDir));\n vec3 H = normalize(V + info.lightDir);\n float NdotH = max(0.0, dot(N, H));\n NdotH = pow(NdotH, max(1.0, glossiness * 128.0));\n res.specular = info.radiance * NdotH;\n return res;\n}\nLighting getPhongLighting(vec3 pos_w, vec3 N, vec3 V, float glossiness) {\n Lighting result;\n result.diffuse = vec3(0, 0, 0);\n result.specular = vec3(0, 0, 0);\n #if CC_NUM_DIR_LIGHTS > 0\n for (int i = 0; i < CC_NUM_DIR_LIGHTS; i++) {\n Lighting info = brdf(computeDirectionalLighting(cc_dirLightDirection[i], cc_dirLightColor[i]), N, V, glossiness);\n result.diffuse += info.diffuse;\n result.specular += info.specular;\n }\n #endif\n #if CC_NUM_POINT_LIGHTS > 0\n for (int i = 0; i < CC_NUM_POINT_LIGHTS; i++) {\n Lighting info = brdf(computePointLighting(pos_w, cc_pointLightPositionAndRange[i], cc_pointLightColor[i]), N, V, glossiness);\n result.diffuse += info.diffuse;\n result.specular += info.specular;\n }\n #endif\n #if CC_NUM_SPOT_LIGHTS > 0\n for (int i = 0; i < CC_NUM_SPOT_LIGHTS; i++) {\n Lighting info = brdf(computeSpotLighting(pos_w, cc_spotLightPositionAndRange[i], cc_spotLightDirection[i], cc_spotLightColor[i]), N, V, glossiness);\n result.diffuse += info.diffuse;\n result.specular += info.specular;\n }\n #endif\n return result;\n}\n#if CC_USE_SHADOW_MAP\n \nvec4 packDepthToRGBA(float depth) {\n vec4 ret = vec4(1.0, 255.0, 65025.0, 160581375.0) * depth;\n ret = fract(ret);\n ret -= ret.yzww * vec4(1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0, 0.0);\n return ret;\n}\nfloat unpackRGBAToDepth(vec4 color) {\n return dot(color, vec4(1.0, 1.0 / 255.0, 1.0 / 65025.0, 1.0 / 160581375.0));\n}\n \nvarying vec4 posLightspace;\nvarying float vDepth;\nuniform sampler2D cc_shadowMap;\nfloat computeShadow(sampler2D shadowMap, vec4 posLightspace, float bias) {\n vec3 projCoords = posLightspace.xyz / posLightspace.w;\n projCoords = projCoords * 0.5 + 0.5;\n float closestDepth = unpackRGBAToDepth(texture2D(shadowMap, projCoords.xy));\n float currentDepth = projCoords.z;\n float shadow = (currentDepth - bias > closestDepth) ? 0.0 : 1.0;\n return shadow;\n}\nfloat computeFallOff(float esm, vec2 coords, float frustumEdgeFalloff) {\n float mask = smoothstep(1.0 - frustumEdgeFalloff, 1.0, clamp(dot(coords, coords), 0.0, 1.0));\n return mix(esm, 1.0, mask);\n}\nfloat computeShadowESM(sampler2D shadowMap, vec4 posLightspace, float vDepth, float depthScale, float darkness, float frustumEdgeFalloff) {\n vec2 projCoords = posLightspace.xy / posLightspace.w;\n vec2 shadowUV = projCoords * 0.5 + vec2(0.5);\n if (shadowUV.x < 0.0 || shadowUV.x > 1.0 || shadowUV.y < 0.0 || shadowUV.y > 1.0) {\n return 1.0;\n }\n float currentDepth = clamp(vDepth, 0.0, 1.0);\n float closestDepth = unpackRGBAToDepth(texture2D(shadowMap, shadowUV));\n \n float esm = clamp(exp(-depthScale * (currentDepth - closestDepth)), 1.0 - darkness, 1.0);\n return computeFallOff(esm, projCoords, frustumEdgeFalloff);\n}\nfloat computeShadowPCF(sampler2D shadowMap, vec4 posLightspace, float vDepth, float darkness, vec2 texelSize, float frustumEdgeFalloff) {\n vec2 projCoords = posLightspace.xy / posLightspace.w;\n vec2 shadowUV = projCoords * 0.5 + vec2(0.5);\n if (shadowUV.x < 0.0 || shadowUV.x > 1.0 || shadowUV.y < 0.0 || shadowUV.y > 1.0) {\n return 1.0;\n }\n float currentDepth = clamp(vDepth, 0.0, 1.0);\n float visibility = 1.0;\n vec2 poissonDisk[4];\n poissonDisk[0] = vec2(-0.94201624, -0.39906216);\n poissonDisk[1] = vec2(0.94558609, -0.76890725);\n poissonDisk[2] = vec2(-0.094184101, -0.92938870);\n poissonDisk[3] = vec2(0.34495938, 0.29387760);\n if (unpackRGBAToDepth(texture2D(shadowMap, shadowUV + poissonDisk[0] * texelSize)) < currentDepth) visibility -= 0.25;\n if (unpackRGBAToDepth(texture2D(shadowMap, shadowUV + poissonDisk[1] * texelSize)) < currentDepth) visibility -= 0.25;\n if (unpackRGBAToDepth(texture2D(shadowMap, shadowUV + poissonDisk[2] * texelSize)) < currentDepth) visibility -= 0.25;\n if (unpackRGBAToDepth(texture2D(shadowMap, shadowUV + poissonDisk[3] * texelSize)) < currentDepth) visibility -= 0.25;\n return computeFallOff(min(1.0, visibility + 1.0 - darkness), projCoords, frustumEdgeFalloff);\n}\nfloat computeShadow() {\n return computeShadowESM(cc_shadowMap, posLightspace, vDepth, cc_shadowParam1.w, cc_shadowParam2.x, cc_shadowParam2.y);\n}\n#endif\nvarying vec3 normal_w;\nvarying vec3 pos_w;\n#if USE_DIFFUSE_TEXTURE || USE_NORMAL_TEXTURE || USE_EMISSIVE_TEXTURE\n varying vec2 uv0;\n#endif\nstruct phongMaterial\n{\n vec3 diffuse;\n vec3 emissive;\n vec3 specular;\n float glossiness;\n float opacity;\n};\n uniform vec4 diffuseColor;\n uniform vec4 emissiveColor;\n uniform vec4 specularColor;\n uniform float glossiness;\n uniform float alphaTestThreshold;\n#if USE_DIFFUSE_TEXTURE\n uniform sampler2D diffuse_texture;\n#endif\n#if USE_EMISSIVE && USE_EMISSIVE_TEXTURE\n uniform sampler2D emissive_texture;\n#endif\n#if USE_SPECULAR && USE_SPECULAR_TEXTURE\n uniform sampler2D specular_texture;\n#endif\n#if USE_NORMAL_TEXTURE\n uniform sampler2D normal_texture;\n vec3 getNormal(vec3 pos, vec3 normal) {\n vec3 q0 = vec3( dFdx( pos.x ), dFdx( pos.y ), dFdx( pos.z ) );\n vec3 q1 = vec3( dFdy( pos.x ), dFdy( pos.y ), dFdy( pos.z ) );\n vec2 st0 = dFdx( uv0.st );\n vec2 st1 = dFdy( uv0.st );\n vec3 S = normalize( q0 * st1.t - q1 * st0.t );\n vec3 T = normalize( -q0 * st1.s + q1 * st0.s );\n vec3 N = normal;\n vec3 mapN = texture2D(normal_texture, uv0).rgb * 2.0 - 1.0;\n mapN.xy = 1.0 * mapN.xy;\n mat3 tsn = mat3( S, T, N );\n return normalize( tsn * mapN );\n }\n#endif\nphongMaterial getPhongMaterial() {\n phongMaterial result;\n #if USE_DIFFUSE_TEXTURE\n vec4 baseColor = diffuseColor * gammaToLinearSpaceRGBA(texture2D(diffuse_texture, uv0));\n result.diffuse = baseColor.rgb;\n result.opacity = baseColor.a;\n #else\n result.diffuse = diffuseColor.rgb;\n result.opacity = diffuseColor.a;\n #endif\n #if USE_EMISSIVE\n result.emissive = emissiveColor.rgb;\n #if USE_EMISSIVE_TEXTURE\n result.emissive *= gammaToLinearSpaceRGB(texture2D(emissive_texture, uv0).rgb);\n #endif\n #endif\n #if USE_SPECULAR\n result.specular = specularColor.rgb;\n #if USE_SPECULAR_TEXTURE\n result.specular = gammaToLinearSpaceRGB(texture2D(specular_texture, uv0).rgb);\n #endif\n result.glossiness = glossiness;\n #endif\n return result;\n}\nvec4 composePhongShading(Lighting lighting, phongMaterial mtl, float shadow) {\n vec4 o = vec4(0.0, 0.0, 0.0, 1.0);\n \n o.xyz = lighting.diffuse * mtl.diffuse;\n #if USE_EMISSIVE\n o.xyz += mtl.emissive;\n #endif\n #if USE_SPECULAR\n o.xyz += lighting.specular * mtl.specular;\n #endif\n o.xyz *= shadow;\n o.w = mtl.opacity;\n return o;\n}\nvec4 frag () {\n Lighting phongLighting;\n vec3 viewDirection = normalize(cc_cameraPos.xyz - pos_w);\n phongMaterial mtl = getPhongMaterial();\n #if USE_ALPHA_TEST\n if(mtl.opacity < alphaTestThreshold) discard;\n #endif\n vec3 normal = normalize(normal_w);\n #if USE_NORMAL_TEXTURE\n normal = getNormal(pos_w, normal);\n #endif\n phongLighting = getPhongLighting(pos_w, normal, viewDirection, mtl.glossiness);\n phongLighting.diffuse += vec3(0.5); \n #if CC_USE_SHADOW_MAP\n vec4 finalColor = composePhongShading(phongLighting, mtl, computeShadow());\n #else\n vec4 finalColor = composePhongShading(phongLighting, mtl, 1.0);\n #endif\n return linearToGammaSpaceRGBA(finalColor);\n}\nvoid main() { gl_FragColor = frag(); }\n", - "defines": [ - {"name":"USE_DIFFUSE_TEXTURE", "type":"boolean", "defines":[]}, - {"name":"CC_USE_SKINNING", "type":"boolean", "defines":[]}, - {"name":"CC_USE_JOINTS_TEXTURE", "type":"boolean", "defines":["CC_USE_SKINNING"]}, - {"name":"CC_USE_SHADOW_MAP", "type":"boolean", "defines":[]}, - {"name":"USE_NORMAL_TEXTURE", "type":"boolean", "defines":[]}, - {"name":"CC_NUM_DIR_LIGHTS", "type":"number", "defines":[]}, - {"name":"CC_NUM_POINT_LIGHTS", "type":"number", "defines":[]}, - {"name":"CC_NUM_SPOT_LIGHTS", "type":"number", "defines":[]}, - {"name":"USE_EMISSIVE", "type":"boolean", "defines":[]}, - {"name":"USE_EMISSIVE_TEXTURE", "type":"boolean", "defines":["USE_EMISSIVE"]}, - {"name":"USE_SPECULAR", "type":"boolean", "defines":[]}, - {"name":"USE_SPECULAR_TEXTURE", "type":"boolean", "defines":["USE_SPECULAR"]}, - {"name":"USE_ALPHA_TEST", "type":"boolean", "defines":[]} - ], - "blocks": [ - {"name": "TexCoords", "size": 16, "defines": [], "binding": 0, "members": [ - {"name":"mainTiling", "type":14, "count":1, "size":8}, - {"name":"mainOffset", "type":14, "count":1, "size":8} - ]}, - {"name": "Constants", "size": 56, "defines": [], "binding": 1, "members": [ - {"name":"diffuseColor", "type":16, "count":1, "size":16}, - {"name":"emissiveColor", "type":16, "count":1, "size":16}, - {"name":"specularColor", "type":16, "count":1, "size":16}, - {"name":"glossiness", "type":13, "count":1, "size":4}, - {"name":"alphaTestThreshold", "type":13, "count":1, "size":4} - ]} - ], - "samplers": [ - {"name":"diffuse_texture", "type":29, "count":1, "defines":["USE_DIFFUSE_TEXTURE"], "binding":2}, - {"name":"emissive_texture", "type":29, "count":1, "defines":["USE_EMISSIVE", "USE_EMISSIVE_TEXTURE"], "binding":3}, - {"name":"specular_texture", "type":29, "count":1, "defines":["USE_SPECULAR", "USE_SPECULAR_TEXTURE"], "binding":4}, - {"name":"normal_texture", "type":29, "count":1, "defines":["USE_NORMAL_TEXTURE"], "binding":5} - ], - "dependencies": {"USE_NORMAL_TEXTURE":"OES_standard_derivatives"} - }, - { - "name": "builtin-effect-phong|shadow-depth-vs:vert|shadow-depth-fs:frag", - "vert": "\nattribute vec3 a_position;\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_cameraPos;\n uniform vec4 cc_dirLightDirection[4];\n uniform vec4 cc_dirLightColor[4];\n uniform vec4 cc_pointLightPositionAndRange[4];\n uniform vec4 cc_pointLightColor[4];\n uniform vec4 cc_spotLightPositionAndRange[4];\n uniform vec4 cc_spotLightDirection[4];\n uniform vec4 cc_spotLightColor[4];\n uniform mat4 cc_matViewProjLight;\n uniform vec4 cc_shadowParam1;\n uniform vec4 cc_shadowParam2;\n uniform mat4 cc_matWorld;\n uniform mat4 cc_matWorldIT;\nvarying float vDepth;\n#if CC_USE_SKINNING\n \nattribute vec4 a_weights;\nattribute vec4 a_joints;\n uniform mat4 cc_matJoint[128];\n uniform vec4 cc_jointsTextureSize;\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 4.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = texture2D(cc_jointsTexture, vec2(dx * (x + 0.5), y));\n vec4 v2 = texture2D(cc_jointsTexture, vec2(dx * (x + 1.5), y));\n vec4 v3 = texture2D(cc_jointsTexture, vec2(dx * (x + 2.5), y));\n vec4 v4 = texture2D(cc_jointsTexture, vec2(dx * (x + 3.5), y));\n return mat4(v1, v2, v3, v4);\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n return cc_matJoint[int(i)];\n}\n#endif\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x) * a_weights.x +\n getBoneMatrix(a_joints.y) * a_weights.y +\n getBoneMatrix(a_joints.z) * a_weights.z +\n getBoneMatrix(a_joints.w) * a_weights.w;\n}\nvoid skinVertex(inout vec4 a1) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2, inout vec4 a3) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n a3 = m * a3;\n}\n#endif\nvec4 vert() {\n vec4 pos = vec4(a_position, 1);\n #if CC_USE_SKINNING\n mat4 skinMat = skinMatrix();\n pos = skinMat * pos;\n #endif\n \n vDepth = ((gl_Position.z + cc_shadowParam1.x) / (cc_shadowParam1.x + cc_shadowParam1.y)) + cc_shadowParam1.z;\n return cc_matViewProjLight * cc_matWorld * pos;\n}\nvoid main() { gl_Position = vert(); }\n", - "frag": "\nvarying float vDepth;\nvec4 packDepthToRGBA(float depth) {\n vec4 ret = vec4(1.0, 255.0, 65025.0, 160581375.0) * depth;\n ret = fract(ret);\n ret -= ret.yzww * vec4(1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0, 0.0);\n return ret;\n}\nfloat unpackRGBAToDepth(vec4 color) {\n return dot(color, vec4(1.0, 1.0 / 255.0, 1.0 / 65025.0, 1.0 / 160581375.0));\n}\nvec4 frag() {\n \n \n return packDepthToRGBA(vDepth);\n \n \n}\nvoid main() { gl_FragColor = frag(); }\n", - "defines": [ - {"name":"CC_USE_SKINNING", "type":"boolean", "defines":[]}, - {"name":"CC_USE_JOINTS_TEXTURE", "type":"boolean", "defines":["CC_USE_SKINNING"]} - ], - "blocks": [], - "samplers": [], - "dependencies": {} - } - ] - }, { "name": "builtin-effect-skybox", "techniques": [{"passes":[{"rasterizerState":{"cullMode":0}, "depthStencilState":{"depthTest":false, "depthWrite":false}, "program":"builtin-effect-skybox|sky-vs|sky-fs", "properties":{"cubeMap":{"type":32, "value":"white-cube"}}, "priority":245}]}], "shaders": [ { "name": "builtin-effect-skybox|sky-vs|sky-fs", - "vert": "\n attribute vec3 a_position;\n \n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_cameraPos;\n uniform vec4 cc_dirLightDirection[4];\n uniform vec4 cc_dirLightColor[4];\n uniform vec4 cc_pointLightPositionAndRange[4];\n uniform vec4 cc_pointLightColor[4];\n uniform vec4 cc_spotLightPositionAndRange[4];\n uniform vec4 cc_spotLightDirection[4];\n uniform vec4 cc_spotLightColor[4];\n uniform mat4 cc_matViewProjLight;\n uniform vec4 cc_shadowParam1;\n uniform vec4 cc_shadowParam2;\n varying vec3 viewDir;\n void main() {\n mat4 matViewRotOnly = mat4(mat3(cc_matView));\n vec4 clipPos = cc_matProj * matViewRotOnly * vec4(a_position, 1.0);\n gl_Position = clipPos.xyww;\n viewDir = a_position;\n }\n", + "vert": "\n attribute vec3 a_position;\n \n#define MAX_DIR_LIGHTS 4\n#define MAX_POINT_LIGHTS 4\n#define MAX_SPOT_LIGHTS 4\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_cameraPos;\n uniform vec4 cc_dirLightDirection[4];\n uniform vec4 cc_dirLightColor[4];\n uniform vec4 cc_pointLightPositionAndRange[4];\n uniform vec4 cc_pointLightColor[4];\n uniform vec4 cc_spotLightPositionAndRange[4];\n uniform vec4 cc_spotLightDirection[4];\n uniform vec4 cc_spotLightColor[4];\n uniform mat4 cc_matViewProjLight;\n uniform vec4 cc_shadowParam1;\n uniform vec4 cc_shadowParam2;\n varying vec3 viewDir;\n void main() {\n mat4 matViewRotOnly = mat4(mat3(cc_matView));\n vec4 clipPos = cc_matProj * matViewRotOnly * vec4(a_position, 1.0);\n gl_Position = clipPos.xyww;\n viewDir = a_position;\n }\n", "frag": "\n varying vec3 viewDir;\n uniform samplerCube cubeMap;\n \nvec3 gammaToLinearSpaceRGB(vec3 sRGB) { \n return sRGB * (sRGB * (sRGB * 0.305306011 + 0.682171111) + 0.012522878);\n}\nvec3 linearToGammaSpaceRGB(vec3 RGB) { \n vec3 S1 = sqrt(RGB);\n vec3 S2 = sqrt(S1);\n vec3 S3 = sqrt(S2);\n return 0.585122381 * S1 + 0.783140355 * S2 - 0.368262736 * S3;\n}\nvec4 gammaToLinearSpaceRGBA(vec4 sRGBA) {\n return vec4(gammaToLinearSpaceRGB(sRGBA.rgb), sRGBA.a);\n}\nvec4 linearToGammaSpaceRGBA(vec4 RGBA) {\n return vec4(linearToGammaSpaceRGB(RGBA.rgb), RGBA.a);\n}\nfloat gammaToLinearSpaceExact(float val) {\n if (val <= 0.04045) {\n return val / 12.92;\n } else if (val < 1.0) {\n return pow((val + 0.055) / 1.055, 2.4);\n } else {\n return pow(val, 2.2);\n }\n}\nfloat linearToGammaSpaceExact(float val) {\n if (val <= 0.0) {\n return 0.0;\n } else if (val <= 0.0031308) {\n return 12.92 * val;\n } else if (val < 1.0) {\n return 1.055 * pow(val, 0.4166667) - 0.055;\n } else {\n return pow(val, 0.45454545);\n }\n}\n \nvec3 unpackNormal(vec4 nmap) {\n return nmap.xyz * 2.0 - 1.0;\n}\nvec3 unpackRGBE(vec4 rgbe) {\n return rgbe.rgb * pow(2.0, rgbe.a * 255.0 - 128.0);\n}\n void main() {\n #if USE_RGBE_CUBEMAP\n vec3 c = unpackRGBE(textureCube(cubeMap, viewDir));\n c = linearToGammaSpaceRGB(c / (1.0 + c));\n gl_FragColor = vec4(c, 1.0);\n #else\n gl_FragColor = textureCube(cubeMap, viewDir);\n #endif\n }\n", "defines": [ {"name":"USE_RGBE_CUBEMAP", "type":"boolean", "defines":[]} @@ -151,20 +21,16 @@ export default [ }, { "name": "builtin-effect-sprite", - "techniques": [{"passes":[{"depthStencilState":{"depthTest":true, "depthWrite":false}, "blendState":{"targets":[{"blend":true, "blendSrc":2, "blendDst":4, "blendDstAlpha":4}]}, "program":"builtin-effect-sprite|sprite-vs:vert|sprite-fs:frag", "properties":{"u_texSampler":{"type":29, "value":"default", "sampler":{"minFilter":2, "magFilter":2, "mipFilter":0, "addressU":2, "addressV":2}}}}]}], + "techniques": [{"passes":[{"depthStencilState":{"depthTest":true, "depthWrite":false}, "blendState":{"targets":[{"blend":true, "blendSrc":2, "blendDst":4, "blendDstAlpha":4}]}, "program":"builtin-effect-sprite|sprite-vs:vert|sprite-fs:frag", "properties":{"mainTexture":{"type":29, "value":"default"}}}]}], "shaders": [ { "name": "builtin-effect-sprite|sprite-vs:vert|sprite-fs:frag", - "vert": "\nattribute vec3 a_position;\nattribute vec2 a_texCoord;\nattribute vec4 a_color;\n uniform mat4 cc_matViewProj;\nvarying vec2 v_uv0;\nvarying vec4 v_color;\nvec4 vert () {\n vec4 pos = cc_matViewProj * vec4(a_position, 1);\n v_uv0 = a_texCoord;\n v_color = a_color;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n", - "frag": "\nuniform sampler2D u_texSampler;\nvarying vec2 v_uv0;\nvarying vec4 v_color;\nvec4 frag () {\n return texture2D(u_texSampler, v_uv0) * v_color;\n}\nvoid main() { gl_FragColor = frag(); }\n", + "vert": "\nattribute vec3 a_position;\nattribute vec2 a_texCoord;\nattribute vec4 a_color;\n#define MAX_DIR_LIGHTS 4\n#define MAX_POINT_LIGHTS 4\n#define MAX_SPOT_LIGHTS 4\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_cameraPos;\n uniform vec4 cc_dirLightDirection[4];\n uniform vec4 cc_dirLightColor[4];\n uniform vec4 cc_pointLightPositionAndRange[4];\n uniform vec4 cc_pointLightColor[4];\n uniform vec4 cc_spotLightPositionAndRange[4];\n uniform vec4 cc_spotLightDirection[4];\n uniform vec4 cc_spotLightColor[4];\n uniform mat4 cc_matViewProjLight;\n uniform vec4 cc_shadowParam1;\n uniform vec4 cc_shadowParam2;\n uniform mat4 cc_matWorld;\n uniform mat4 cc_matWorldIT;\nvarying vec2 uv0;\nvarying vec4 color;\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n pos = cc_matViewProj * cc_matWorld * pos;\n uv0 = a_texCoord;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n", + "frag": "\nuniform sampler2D mainTexture;\nvarying vec2 uv0;\nvarying vec4 color;\nvec4 frag () {\n vec4 o = vec4(1, 1, 1, 1);\n o *= texture2D(mainTexture, uv0);\n o *= color;\n return o;\n}\nvoid main() { gl_FragColor = frag(); }\n", "defines": [], - "blocks": [ - {"name": "UI", "size": 64, "defines": [], "binding": 0, "members": [ - {"name":"cc_matViewProj", "type":26, "count":1, "size":64} - ]} - ], + "blocks": [], "samplers": [ - {"name":"u_texSampler", "type":29, "count":1, "defines":[], "binding":1} + {"name":"mainTexture", "type":29, "count":1, "defines":[], "binding":0} ], "dependencies": {} } @@ -176,7 +42,7 @@ export default [ "shaders": [ { "name": "builtin-effect-unlit|unlit-vs:vert|unlit-fs:frag", - "vert": "\nattribute vec3 a_position;\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_cameraPos;\n uniform vec4 cc_dirLightDirection[4];\n uniform vec4 cc_dirLightColor[4];\n uniform vec4 cc_pointLightPositionAndRange[4];\n uniform vec4 cc_pointLightColor[4];\n uniform vec4 cc_spotLightPositionAndRange[4];\n uniform vec4 cc_spotLightDirection[4];\n uniform vec4 cc_spotLightColor[4];\n uniform mat4 cc_matViewProjLight;\n uniform vec4 cc_shadowParam1;\n uniform vec4 cc_shadowParam2;\n uniform mat4 cc_matWorld;\n uniform mat4 cc_matWorldIT;\n#if USE_TEXTURE\n attribute vec2 a_texCoord;\n uniform vec2 mainTiling;\n uniform vec2 mainOffset;\n varying vec2 uv0;\n#endif\n#if CC_USE_SKINNING\n \nattribute vec4 a_weights;\nattribute vec4 a_joints;\n uniform mat4 cc_matJoint[128];\n uniform vec4 cc_jointsTextureSize;\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 4.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = texture2D(cc_jointsTexture, vec2(dx * (x + 0.5), y));\n vec4 v2 = texture2D(cc_jointsTexture, vec2(dx * (x + 1.5), y));\n vec4 v3 = texture2D(cc_jointsTexture, vec2(dx * (x + 2.5), y));\n vec4 v4 = texture2D(cc_jointsTexture, vec2(dx * (x + 3.5), y));\n return mat4(v1, v2, v3, v4);\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n return cc_matJoint[int(i)];\n}\n#endif\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x) * a_weights.x +\n getBoneMatrix(a_joints.y) * a_weights.y +\n getBoneMatrix(a_joints.z) * a_weights.z +\n getBoneMatrix(a_joints.w) * a_weights.w;\n}\nvoid skinVertex(inout vec4 a1) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2, inout vec4 a3) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n a3 = m * a3;\n}\n#endif\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n #if CC_USE_SKINNING\n skinVertex(pos);\n #endif\n pos = cc_matViewProj * cc_matWorld * pos;\n #if USE_TEXTURE\n uv0 = a_texCoord * mainTiling + mainOffset;\n #endif\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n", + "vert": "\nattribute vec3 a_position;\n#define MAX_DIR_LIGHTS 4\n#define MAX_POINT_LIGHTS 4\n#define MAX_SPOT_LIGHTS 4\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_cameraPos;\n uniform vec4 cc_dirLightDirection[4];\n uniform vec4 cc_dirLightColor[4];\n uniform vec4 cc_pointLightPositionAndRange[4];\n uniform vec4 cc_pointLightColor[4];\n uniform vec4 cc_spotLightPositionAndRange[4];\n uniform vec4 cc_spotLightDirection[4];\n uniform vec4 cc_spotLightColor[4];\n uniform mat4 cc_matViewProjLight;\n uniform vec4 cc_shadowParam1;\n uniform vec4 cc_shadowParam2;\n uniform mat4 cc_matWorld;\n uniform mat4 cc_matWorldIT;\n#if USE_TEXTURE\n attribute vec2 a_texCoord;\n uniform vec2 mainTiling;\n uniform vec2 mainOffset;\n varying vec2 uv0;\n#endif\n#if CC_USE_SKINNING\n \nattribute vec4 a_weights;\nattribute vec4 a_joints;\n uniform mat4 cc_matJoint[128];\n uniform vec4 cc_jointsTextureSize;\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 4.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = texture2D(cc_jointsTexture, vec2(dx * (x + 0.5), y));\n vec4 v2 = texture2D(cc_jointsTexture, vec2(dx * (x + 1.5), y));\n vec4 v3 = texture2D(cc_jointsTexture, vec2(dx * (x + 2.5), y));\n vec4 v4 = texture2D(cc_jointsTexture, vec2(dx * (x + 3.5), y));\n return mat4(v1, v2, v3, v4);\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n return cc_matJoint[int(i)];\n}\n#endif\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x) * a_weights.x +\n getBoneMatrix(a_joints.y) * a_weights.y +\n getBoneMatrix(a_joints.z) * a_weights.z +\n getBoneMatrix(a_joints.w) * a_weights.w;\n}\nvoid skinVertex(inout vec4 a1) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2, inout vec4 a3) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n a3 = m * a3;\n}\n#endif\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n #if CC_USE_SKINNING\n skinVertex(pos);\n #endif\n pos = cc_matViewProj * cc_matWorld * pos;\n #if USE_TEXTURE\n uv0 = a_texCoord * mainTiling + mainOffset;\n #endif\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n", "frag": "\n#if USE_TEXTURE\n uniform sampler2D mainTexture;\n varying vec2 uv0;\n#endif\n#if USE_COLOR\n uniform vec4 color;\n#endif\nvec4 frag () {\n vec4 o = vec4(1, 1, 1, 1);\n #if USE_TEXTURE\n o *= texture2D(mainTexture, uv0);\n #endif\n #if USE_COLOR\n o *= color;\n #endif\n return o;\n}\nvoid main() { gl_FragColor = frag(); }\n", "defines": [ {"name":"USE_TEXTURE", "type":"boolean", "defines":[]}, diff --git a/cocos/3d/framework/light-component.ts b/cocos/3d/framework/light-component.ts index 361d058cf6..ba271c4f83 100644 --- a/cocos/3d/framework/light-component.ts +++ b/cocos/3d/framework/light-component.ts @@ -251,8 +251,8 @@ export default class LightComponent extends Component { } public onEnable () { - if (this._light) { this._light.enabled = true; return; } this._createLight(); + if (this._light) { this._light.enabled = true; return; } } public onDisable () { @@ -268,14 +268,14 @@ export default class LightComponent extends Component { const scene = this._getRenderScene(); switch (this._type) { case Type.DIRECTIONAL: - this._light = scene.createDirectionalLight(this.name); + this._light = scene.createDirectionalLight(this.name, this.node); break; case Type.POINT: - this._light = scene.createPointLight(this.name); + this._light = scene.createPointLight(this.name, this.node); this.range = this._range; break; case Type.SPOT: - this._light = scene.createSpotLight(this.name); + this._light = scene.createSpotLight(this.name, this.node); this.range = this._range; this.spotAngle = this._spotAngle; break; diff --git a/cocos/pipeline/render-pipeline.ts b/cocos/pipeline/render-pipeline.ts index fc3c6c12d7..901713962f 100644 --- a/cocos/pipeline/render-pipeline.ts +++ b/cocos/pipeline/render-pipeline.ts @@ -15,6 +15,10 @@ import { RenderQueue } from './render-queue'; import { RenderView } from './render-view'; export class UBOGlobal { + public static MAX_DIR_LIGHTS = 4; + public static MAX_POINT_LIGHTS = 4; + public static MAX_SPOT_LIGHTS = 4; + public static TIME_OFFSET: number = 0; public static SCREEN_SIZE_OFFSET: number = UBOGlobal.TIME_OFFSET + 4; public static SCREEN_SCALE_OFFSET: number = UBOGlobal.SCREEN_SIZE_OFFSET + 4; @@ -50,13 +54,13 @@ export class UBOGlobal { { 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 }, + { name: 'cc_dirLightDirection', type: GFXType.FLOAT4, count: UBOGlobal.MAX_DIR_LIGHTS }, + { name: 'cc_dirLightColor', type: GFXType.FLOAT4, count: UBOGlobal.MAX_DIR_LIGHTS }, + { name: 'cc_pointLightPositionAndRange', type: GFXType.FLOAT4, count: UBOGlobal.MAX_POINT_LIGHTS }, + { name: 'cc_pointLightColor', type: GFXType.FLOAT4, count: UBOGlobal.MAX_POINT_LIGHTS }, + { name: 'cc_spotLightPositionAndRange', type: GFXType.FLOAT4, count: UBOGlobal.MAX_SPOT_LIGHTS }, + { name: 'cc_spotLightDirection', type: GFXType.FLOAT4, count: UBOGlobal.MAX_SPOT_LIGHTS }, + { name: 'cc_spotLightColor', type: GFXType.FLOAT4, count: UBOGlobal.MAX_SPOT_LIGHTS }, ], }; @@ -79,6 +83,7 @@ export class UBOLocal { public view: Float32Array = new Float32Array(UBOLocal.COUNT); } +const _idVec4Array = Float32Array.from([0, 0, 0, 1]); const _vec4Array = new Float32Array(4); const _mat4Array = new Float32Array(16); const _outMat = new Mat4(); @@ -354,20 +359,42 @@ export abstract class RenderPipeline { 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); + for (let i = 0; i < UBOGlobal.MAX_DIR_LIGHTS; i++) { + const light = dLights[i]; + if (light && light.enabled) { + light.update(); + this._uboGlobal.view.set(light.directionArray, UBOGlobal.DIR_LIGHT_DIR_OFFSET + 4 * i); + this._uboGlobal.view.set(light.colorData, UBOGlobal.DIR_LIGHT_COLOR_OFFSET + 4 * i); + } else { + this._uboGlobal.view.set(_idVec4Array, UBOGlobal.DIR_LIGHT_DIR_OFFSET + 4 * i); + this._uboGlobal.view.set(_idVec4Array, 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); + for (let i = 0; i < UBOGlobal.MAX_POINT_LIGHTS; i++) { + const light = pLights[i]; + if (light && light.enabled) { + light.update(); + this._uboGlobal.view.set(light.positionAndRange, UBOGlobal.POINT_LIGHT_POS_RANGE_OFFSET + 4 * i); + this._uboGlobal.view.set(light.colorData, UBOGlobal.POINT_LIGHT_COLOR_OFFSET + 4 * i); + } else { + this._uboGlobal.view.set(_idVec4Array, UBOGlobal.POINT_LIGHT_POS_RANGE_OFFSET + 4 * i); + this._uboGlobal.view.set(_idVec4Array, 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); + for (let i = 0; i < UBOGlobal.MAX_SPOT_LIGHTS; i++) { + const light = sLights[i]; + if (light && light.enabled) { + light.update(); + this._uboGlobal.view.set(light.positionAndRange, UBOGlobal.SPOT_LIGHT_POS_RANGE_OFFSET + 4 * i); + this._uboGlobal.view.set(light.directionArray, UBOGlobal.SPOT_LIGHT_DIR_OFFSET + 4 * i); + this._uboGlobal.view.set(light.colorData, UBOGlobal.SPOT_LIGHT_COLOR_OFFSET + 4 * i); + } else { + this._uboGlobal.view.set(_idVec4Array, UBOGlobal.SPOT_LIGHT_POS_RANGE_OFFSET + 4 * i); + this._uboGlobal.view.set(_idVec4Array, UBOGlobal.SPOT_LIGHT_DIR_OFFSET + 4 * i); + this._uboGlobal.view.set(_idVec4Array, UBOGlobal.SPOT_LIGHT_COLOR_OFFSET + 4 * i); + } } // update ubos diff --git a/cocos/renderer/core/program-lib.ts b/cocos/renderer/core/program-lib.ts index 7945b1bf94..2bfc0a874f 100644 --- a/cocos/renderer/core/program-lib.ts +++ b/cocos/renderer/core/program-lib.ts @@ -125,7 +125,6 @@ class ProgramLib { if (program !== undefined) { return program; } - defines.CC_NUM_DIR_LIGHTS = 1; // get template const tmpl = this._templates[name]; diff --git a/cocos/renderer/scene/directional-light.ts b/cocos/renderer/scene/directional-light.ts index c58f678aba..0072aa447b 100644 --- a/cocos/renderer/scene/directional-light.ts +++ b/cocos/renderer/scene/directional-light.ts @@ -1,7 +1,12 @@ -import { Vec3 } from '../../core/value-types'; +import { Vec3, Quat } from '../../core/value-types'; import { vec3 } from '../../core/vmath'; import { Light, LightType } from './light'; import { RenderScene } from './render-scene'; +import { Node } from '../../scene-graph/node'; + +const _forward = new Vec3(0, 0, 1); +const _v3 = new Vec3(); +const _qt = new Quat(); export class DirectionalLight extends Light { protected _direction = Float32Array.from([0, 0, 1, 0]); @@ -13,8 +18,12 @@ export class DirectionalLight extends Light { return this._direction; } - constructor (scene: RenderScene, name: string) { - super(scene, name); + constructor (scene: RenderScene, node: Node, name: string) { + super(scene, node, name); this._type = LightType.DIRECTIONAL; } + + public update () { + this.direction = vec3.transformQuat(_v3, _forward, this._node.getWorldRotation(_qt)); + } } diff --git a/cocos/renderer/scene/light.ts b/cocos/renderer/scene/light.ts index 526b708991..0c706a24a1 100644 --- a/cocos/renderer/scene/light.ts +++ b/cocos/renderer/scene/light.ts @@ -1,4 +1,5 @@ import { RenderScene } from './render-scene'; +import { Node } from '../../scene-graph/node'; export enum LightType { UNKNOWN, @@ -10,6 +11,10 @@ export enum LightType { export class Light { protected _color = Float32Array.from([1, 1, 1, 1]); protected _enabled = false; + protected _scene: RenderScene; + protected _node: Node; + protected _type: LightType; + protected _name: string; set enabled (val) { this._enabled = val; @@ -32,12 +37,11 @@ export class Light { return this._name; } - protected _scene: RenderScene; - protected _type: LightType; - protected _name: string; + public update () {} - constructor (scene: RenderScene, name: string) { + constructor (scene: RenderScene, node: Node, name: string) { this._scene = scene; + this._node = node; this._name = name; this._type = LightType.UNKNOWN; } diff --git a/cocos/renderer/scene/point-light.ts b/cocos/renderer/scene/point-light.ts index bfd677c175..a7b52f3c2d 100644 --- a/cocos/renderer/scene/point-light.ts +++ b/cocos/renderer/scene/point-light.ts @@ -2,6 +2,9 @@ import { Vec3 } from '../../core/value-types'; import { vec3 } from '../../core/vmath'; import { Light, LightType } from './light'; import { RenderScene } from './render-scene'; +import { Node } from '../../scene-graph/node'; + +const _v3 = new Vec3(); export class PointLight extends Light { protected _positionAndRange = Float32Array.from([0, 0, 0, 10]); @@ -17,8 +20,12 @@ export class PointLight extends Light { return this._positionAndRange; } - constructor (scene: RenderScene, name: string) { - super(scene, name); + constructor (scene: RenderScene, node: Node, name: string) { + super(scene, node, name); this._type = LightType.POINT; } + + public update () { + this.position = this._node.getWorldPosition(_v3); + } } diff --git a/cocos/renderer/scene/render-scene.ts b/cocos/renderer/scene/render-scene.ts index 9be39b8f7c..258c907ffc 100644 --- a/cocos/renderer/scene/render-scene.ts +++ b/cocos/renderer/scene/render-scene.ts @@ -13,6 +13,7 @@ import { Light } from './light'; import { Model } from './model'; import { PointLight } from './point-light'; import { SpotLight } from './spot-light'; +import { UBOGlobal } from '../../pipeline/render-pipeline'; export interface IRenderSceneInfo { name: string; @@ -97,7 +98,7 @@ export class RenderScene { public destroyCamera (camera: Camera) { for (let i = 0; i < this._cameras.length; ++i) { if (this._cameras[i] === camera) { - this._cameras.slice(i); + this._cameras.splice(i, 1); return; } } @@ -117,9 +118,9 @@ export class RenderScene { return null; } - public createPointLight (name: string): Light | null { - if (this._pointLights.length >= 4) { return null; } - const light = new PointLight(this, name); + public createPointLight (name: string, node: Node): Light | null { + if (this._pointLights.length >= UBOGlobal.MAX_POINT_LIGHTS) { return null; } + const light = new PointLight(this, node, name); this._pointLights.push(light); return light; } @@ -127,15 +128,15 @@ export class RenderScene { public destroyPointLight (light: Light) { for (let i = 0; i < this._pointLights.length; ++i) { if (this._pointLights[i] === light) { - this._pointLights.slice(i); + this._pointLights.splice(i, 1); return; } } } - public createDirectionalLight (name: string): Light | null { - if (this._directionalLights.length >= 4) { return null; } - const light = new DirectionalLight(this, name); + public createDirectionalLight (name: string, node: Node): Light | null { + if (this._directionalLights.length >= UBOGlobal.MAX_DIR_LIGHTS) { return null; } + const light = new DirectionalLight(this, node, name); this._directionalLights.push(light); return light; } @@ -143,15 +144,15 @@ export class RenderScene { public destroyDirectionalLight (light: Light) { for (let i = 0; i < this._directionalLights.length; ++i) { if (this._directionalLights[i] === light) { - this._directionalLights.slice(i); + this._directionalLights.splice(i, 1); return; } } } - public createSpotLight (name: string): Light | null { - if (this._spotLights.length >= 4) { return null; } - const light = new SpotLight(this, name); + public createSpotLight (name: string, node: Node): Light | null { + if (this._spotLights.length >= UBOGlobal.MAX_SPOT_LIGHTS) { return null; } + const light = new SpotLight(this, node, name); this._spotLights.push(light); return light; } @@ -159,7 +160,7 @@ export class RenderScene { public destroySpotLight (light: Light) { for (let i = 0; i < this._spotLights.length; ++i) { if (this._spotLights[i] === light) { - this._spotLights.slice(i); + this._spotLights.splice(i, 1); return; } } diff --git a/cocos/renderer/scene/spot-light.ts b/cocos/renderer/scene/spot-light.ts index dd56fd8c1f..f741b94093 100644 --- a/cocos/renderer/scene/spot-light.ts +++ b/cocos/renderer/scene/spot-light.ts @@ -1,10 +1,15 @@ -import { Vec3 } from '../../core/value-types'; +import { Vec3, Quat } from '../../core/value-types'; import { vec3 } from '../../core/vmath'; import { Light, LightType } from './light'; import { RenderScene } from './render-scene'; +import { Node } from '../../scene-graph/node'; + +const _forward = new Vec3(0, 0, 1); +const _v3 = new Vec3(); +const _qt = new Quat(); export class SpotLight extends Light { - protected _direction = Float32Array.from([0, 0, 1, 0]); + protected _direction = Float32Array.from([0, 0, 1, Math.cos(Math.PI / 6)]); protected _positionAndRange = Float32Array.from([0, 0, 0, 10]); set position (val: Vec3) { @@ -17,7 +22,7 @@ export class SpotLight extends Light { vec3.array(this._direction, val); } set spotAngle (val: number) { - this._direction[3] = val; + this._direction[3] = Math.cos(val * 0.5); } get positionAndRange () { @@ -27,8 +32,13 @@ export class SpotLight extends Light { return this._direction; } - constructor (scene: RenderScene, name: string) { - super(scene, name); + constructor (scene: RenderScene, node: Node, name: string) { + super(scene, node, name); this._type = LightType.DIRECTIONAL; } + + public update () { + this.position = this._node.getWorldPosition(_v3); + this.direction = vec3.transformQuat(_v3, _forward, this._node.getWorldRotation(_qt)); + } } diff --git a/playground/simple.html b/playground/simple.html index b02fcdb872..93fcfb7096 100644 --- a/playground/simple.html +++ b/playground/simple.html @@ -68,7 +68,7 @@ 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-pbr'; + material.effectName = 'builtin-effect-unlit'; const len = 50; const models = [], passes = []; @@ -84,7 +84,7 @@ // material instance initialized here passes.push(modelCom.material.passes[0]); } - const handle = passes[0].getHandle('albedo'); + const handle = passes[0].getHandle('color'); { const material = new cc.Material(); diff --git a/tslint.json b/tslint.json index 8f4d116805..605e6fb73e 100644 --- a/tslint.json +++ b/tslint.json @@ -9,6 +9,10 @@ true, "single" ], + "one-line": [ + true, + "check-open-brace" + ], "no-console": false, "space-before-function-paren": true, "max-classes-per-file": false, @@ -18,7 +22,6 @@ "no-bitwise": false, "no-empty": false, "radix": false, - "one-line": false, }, "rulesDirectory": [] }