diff --git a/cocos/3d/builtin/effects.js b/cocos/3d/builtin/effects.js index b8c14cddfd..60a303441e 100644 --- a/cocos/3d/builtin/effects.js +++ b/cocos/3d/builtin/effects.js @@ -1,5 +1,77 @@ // 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"}]}], diff --git a/cocos/3d/framework/light-component.ts b/cocos/3d/framework/light-component.ts index a15be397b5..361d058cf6 100644 --- a/cocos/3d/framework/light-component.ts +++ b/cocos/3d/framework/light-component.ts @@ -183,9 +183,10 @@ export default class LightComponent extends Component { set color (val) { this._color = val; - this._colorData[0] = val.r * this._intensity; - this._colorData[1] = val.g * this._intensity; - this._colorData[2] = val.b * this._intensity; + 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; } @@ -204,9 +205,10 @@ export default class LightComponent extends Component { set intensity (val) { this._intensity = val; - this._colorData[0] = this._color.r * val; - this._colorData[1] = this._color.g * val; - this._colorData[2] = this._color.b * 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; } diff --git a/cocos/renderer/core/pass.ts b/cocos/renderer/core/pass.ts index 3b5e0c3bec..9de364d044 100644 --- a/cocos/renderer/core/pass.ts +++ b/cocos/renderer/core/pass.ts @@ -155,12 +155,8 @@ export class Pass { stride: 1, // N/A for blocks usage: GFXBufferUsageBit.UNIFORM, }); - if (buffer) { - this._buffers[u.binding] = buffer; - } else { - console.error('create buffer failed.'); - return; - } + if (buffer) { this._buffers[u.binding] = buffer; } + else { console.error('create buffer failed.'); return; } // buffer data processing system const block: IBlock = this._blocks[u.binding] = { buffer: buffer.buffer as ArrayBuffer, @@ -188,13 +184,12 @@ export class Pass { const inf = info.properties && info.properties[u.name]; const samplerInfo = Object.assign({ name: u.name }, inf && inf.sampler); const sampler = device.createSampler(samplerInfo); - if (sampler) { - this._samplers[u.binding] = sampler; - } else { - console.error('create sampler failed.'); - } + if (sampler) { this._samplers[u.binding] = sampler; } + else { console.error('create sampler failed.'); } const texName = inf && inf.value ? inf.value + '-texture' : _type2default[u.type]; - this._textureViews[u.binding] = builtinResMgr.get(texName).getGFXTextureView()!; + const texture = builtinResMgr.get(texName); + if (texture) { this._textureViews[u.binding] = texture.getGFXTextureView()!; } + else { console.warn(`illegal texture default value ${texName}`); } } } diff --git a/cocos/renderer/core/program-lib.ts b/cocos/renderer/core/program-lib.ts index 2bfc0a874f..7945b1bf94 100644 --- a/cocos/renderer/core/program-lib.ts +++ b/cocos/renderer/core/program-lib.ts @@ -125,6 +125,7 @@ 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 cde70935a4..c58f678aba 100644 --- a/cocos/renderer/scene/directional-light.ts +++ b/cocos/renderer/scene/directional-light.ts @@ -4,7 +4,7 @@ import { Light, LightType } from './light'; import { RenderScene } from './render-scene'; export class DirectionalLight extends Light { - protected _direction = new Float32Array(4); + protected _direction = Float32Array.from([0, 0, 1, 0]); set direction (val: Vec3) { vec3.array(this._direction, val); diff --git a/cocos/renderer/scene/light.ts b/cocos/renderer/scene/light.ts index 45379330e2..526b708991 100644 --- a/cocos/renderer/scene/light.ts +++ b/cocos/renderer/scene/light.ts @@ -8,7 +8,7 @@ export enum LightType { } export class Light { - protected _color = new Float32Array(4); + protected _color = Float32Array.from([1, 1, 1, 1]); protected _enabled = false; set enabled (val) { diff --git a/cocos/renderer/scene/point-light.ts b/cocos/renderer/scene/point-light.ts index 68af391dc0..bfd677c175 100644 --- a/cocos/renderer/scene/point-light.ts +++ b/cocos/renderer/scene/point-light.ts @@ -4,7 +4,7 @@ import { Light, LightType } from './light'; import { RenderScene } from './render-scene'; export class PointLight extends Light { - protected _positionAndRange = new Float32Array(4); + protected _positionAndRange = Float32Array.from([0, 0, 0, 10]); set position (val: Vec3) { vec3.array(this._positionAndRange, val); diff --git a/cocos/renderer/scene/spot-light.ts b/cocos/renderer/scene/spot-light.ts index 30ce89a366..dd56fd8c1f 100644 --- a/cocos/renderer/scene/spot-light.ts +++ b/cocos/renderer/scene/spot-light.ts @@ -4,8 +4,8 @@ import { Light, LightType } from './light'; import { RenderScene } from './render-scene'; export class SpotLight extends Light { - protected _direction = new Float32Array(4); - protected _positionAndRange = new Float32Array(4); + protected _direction = Float32Array.from([0, 0, 1, 0]); + protected _positionAndRange = Float32Array.from([0, 0, 0, 10]); set position (val: Vec3) { vec3.array(this._positionAndRange, val); diff --git a/playground/simple.html b/playground/simple.html index aef133da14..b02fcdb872 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-phong'; + material.effectName = 'builtin-effect-pbr'; 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('diffuseColor'); + const handle = passes[0].getHandle('albedo'); { const material = new cc.Material();