diff --git a/cocos/3d/builtin/effects.js b/cocos/3d/builtin/effects.js index 0bacfd6b65..5a34563427 100644 --- a/cocos/3d/builtin/effects.js +++ b/cocos/3d/builtin/effects.js @@ -1,12 +1,70 @@ // absolute essential effects export default [ + { + "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 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_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform vec4 cc_cameraSize;\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 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":[]} @@ -25,7 +83,7 @@ export 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_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_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform vec4 cc_cameraSize;\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", + "vert": "\nattribute vec3 a_position;\nattribute vec2 a_texCoord;\nattribute vec4 a_color;\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": [], @@ -42,8 +100,8 @@ export default [ "shaders": [ { "name": "builtin-effect-unlit|unlit-vs:vert|unlit-fs:frag", - "vert": "\nattribute vec3 a_position;\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_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform vec4 cc_cameraSize;\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[2];\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[0] + color[1];\n #endif\n return o;\n}\nvoid main() { gl_FragColor = frag(); }\n", + "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", + "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":[]}, {"name":"CC_USE_SKINNING", "type":"boolean", "defines":[]}, @@ -55,8 +113,8 @@ export default [ {"name":"mainTiling", "type":14, "count":1, "size":8}, {"name":"mainOffset", "type":14, "count":1, "size":8} ]}, - {"name": "Constant", "size": 32, "defines": ["USE_COLOR"], "binding": 1, "members": [ - {"name":"color", "type":16, "count":2, "size":32} + {"name": "Constant", "size": 16, "defines": ["USE_COLOR"], "binding": 1, "members": [ + {"name":"color", "type":16, "count":1, "size":16} ]} ], "samplers": [ diff --git a/cocos/3d/framework/light-component.js b/cocos/3d/framework/light-component.js deleted file mode 100644 index 1822c7e15a..0000000000 --- a/cocos/3d/framework/light-component.js +++ /dev/null @@ -1,458 +0,0 @@ -/**************************************************************************** - Copyright (c) 2013-2016 Chukong Technologies Inc. - Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. - - http://www.cocos2d-x.org - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - ****************************************************************************/ -import { Color, Enum } from '../../core/value-types'; -import { toRadian } from '../../core/vmath'; -import { ccclass, menu, property, executeInEditMode } from "../../core/data/class-decorator"; -import { Component } from '../../components/component'; - -/** - * !#en The light source type - * - * !#ch 光源类型 - * @static - * @enum LightComponent.Type - */ -const LightType = Enum({ - /** - * !#en The direction of light - * - * !#ch 平行光 - * @property Directional - * @readonly - * @type {Number} - */ - Directional: 0, - /** - * !#en The point of light - * - * !#ch 点光源 - * @property Point - * @readonly - * @type {Number} - */ - Point: 1, - /** - * !#en The spot of light - * - * !#ch 聚光灯 - * @property Spot - * @readonly - * @type {Number} - */ - Spot: 2, -}); - -/** - * !#en The shadow type - * - * !#ch 阴影类型 - * @static - * @enum LightComponent.ShadowType - */ -const LightShadowType = Enum({ - /** - * !#en No shadows - * - * !#ch 阴影关闭 - * @property None - * @readonly - * @type {Number} - */ - None: 0, - /** - * !#en Soft shadows - * - * !#ch 软阴影 - * @property Soft - * @readonly - * @type {Number} - */ - Soft: 1, - /** - * !#en Hard shadows - * - * !#ch 阴硬影 - * @property Hard - * @readonly - * @type {Number} - */ - Hard: 2, -}); - -/** - * !#en The Light Component - * - * !#ch 光源组件 - * @class LightComponent - * @extends Component - */ -@ccclass('cc.LightComponent') -@menu('Components/LightComponent') -@executeInEditMode -export default class LightComponent extends Component { - @property - _type = LightType.Directional; - - @property - _color = Color.WHITE; - - @property - _intensity = 1; - - @property - _range = 1; - - @property - _spotAngle = 60; - - @property - _spotExp = 1; - - @property - _shadowType = LightShadowType.None; - - @property - _shadowResolution = 1024; - - @property - _shadowDarkness = 0.5; - - @property - _shadowMinDepth = 1; - - @property - _shadowMaxDepth = 1000; - - @property - _shadowDepthScale = 250; - - @property - _shadowFrustumSize = 50; - - @property - _shadowBias = 0.0005; - - /** - * !#en The light source type - * - * !#ch 光源类型 - * - * @type {Number} - */ - @property({ - type: LightType - }) - get type() { - return this._type; - } - - set type(val) { - this._type = val; - - let type = renderer.LIGHT_DIRECTIONAL; - if (this._type === LightType.Point) { - type = renderer.LIGHT_POINT; - } else if (this._type === LightType.Spot) { - type = renderer.LIGHT_SPOT; - } - if (this._light) - this._light.setType(type); - } - - /** - * !#en The light source color - * - * !#ch 光源颜色 - * @type {Color} - */ - @property - get color() { - return this._color; - } - - set color(val) { - this._color = val; - if (this._light) - this._light.setColor(val.r / 255, val.g / 255, val.b / 255); - } - - /** - * !#en The light source intensity - * - * !#ch 光源强度 - * @type {Number} - */ - @property - get intensity() { - return this._intensity; - } - - set intensity(val) { - this._intensity = val; - if (this._light) - this._light.setIntensity(val); - } - - /** - * !#en The light range, used for spot and point light - * - * !#ch 针对聚光灯和点光源设置光源范围 - * @type {Number} - */ - @property - get range() { - return this._range; - } - - set range(val) { - this._range = val; - if (this._light) - this._light.setRange(val); - } - - /** - * !#en The spot light cone angle - * - * !#ch 聚光灯锥角 - * @type {Number} - */ - @property - get spotAngle() { - return this._spotAngle; - } - - set spotAngle(val) { - this._spotAngle = val; - if (this._light) - this._light.setSpotAngle(toRadian(val)); - } - - /** - * !#en The spot light exponential - * - * !#ch 聚光灯指数 - * @type {Number} - */ - @property - get spotExp() { - return this._spotExp; - } - - set spotExp(val) { - this._spotExp = val; - if (this._light) - this._light.setSpotExp(val); - } - - /** - * !#en The shadow type - * - * !#ch 阴影类型 - * @type {Number} shadowType - */ - @property({ - type: LightShadowType - }) - get shadowType() { - return this._shadowType; - } - - set shadowType(val) { - this._shadowType = val; - - let type = renderer.SHADOW_NONE; - if (val === LightShadowType.Hard) { - type = renderer.SHADOW_HARD; - } else if (val === LightShadowType.Soft) { - type = renderer.SHADOW_SOFT; - } - if (this._light) - this._light.setShadowType(type); - } - - /** - * !#en The shadow resolution - * - * !#ch 阴影分辨率 - * - * @type {Number} - */ - @property - get shadowResolution() { - return this._shadowResolution; - } - - set shadowResolution(val) { - this._shadowResolution = val; - if (this._light) - this._light.setShadowResolution(val); - } - - /** - * !#en The shadow darkness - * - * !#ch 阴影灰度值 - * - * @type {Number} - */ - @property - get shadowDarkness() { - return this._shadowDarkness; - } - - set shadowDarkness(val) { - this._shadowDarkness = val; - if (this._light) - this._light.setShadowDarkness(val); - } - - /** - * !#en The shadow min depth - * - * !#ch 阴影最小深度 - * - * @type {Number} - */ - @property - get shadowMinDepth() { - return this._shadowMinDepth; - } - - set shadowMinDepth(val) { - this._shadowMinDepth = val; - if (this._light) - this._light.setShadowMinDepth(val); - } - - /** - * !#en The shadow max depth - * - * !#ch 阴影最大深度 - * - * @type {Number} - */ - @property - get shadowMaxDepth() { - return this._shadowMaxDepth; - } - - set shadowMaxDepth(val) { - this._shadowMaxDepth = val; - if (this._light) - this._light.setShadowMaxDepth(val); - } - - /** - * !#en The shadow depth scale - * - * !#ch 阴影深度比例 - * - * @type {Number} - */ - @property - get shadowDepthScale() { - return this._shadowDepthScale; - } - - set shadowDepthScale(val) { - this._shadowDepthScale = val; - if (this._light) - this._light.setShadowDepthScale(val); - } - - /** - * !#en The shadow frustum size - * - * !#ch 阴影截锥体大小 - * - * @type {Number} - */ - @property - get shadowFrustumSize() { - return this._shadowFrustumSize; - } - - set shadowFrustumSize(val) { - this._shadowFrustumSize = val; - if (this._light) - this._light.setShadowFrustumSize(val); - } - - /** - * !#en The shadow bias - * - * !#ch 阴影偏移量 - * - * @type {Number} - */ - @property - get shadowBias() { - return this._shadowBias; - } - - set shadowBias(val) { - this._shadowBias = val; - if (this._light) - this._light.setShadowBias(val); - } - - static Type = LightType; - - static ShadowType = LightShadowType; - - constructor() { - super(); - } - - onLoad() { - - } - - onEnable() { - // this._light = this._getRenderScene().createLight(this.name); - // this._light.setNode(this.node); - this.type = this._type; - this.color = this._color; - this.intensity = this._intensity; - this.range = this._range; - this.spotAngle = this._spotAngle; - this.spotExp = this._spotExp; - this.shadowType = this._shadowType; - this.shadowResolution = this._shadowResolution; - this.shadowDarkness = this._shadowDarkness; - this.shadowMaxDepth = this._shadowMaxDepth; - this.shadowDepthScale = this._shadowDepthScale; - this.shadowFrustumSize = this._shadowFrustumSize; - this.shadowBias = this._shadowBias; - } - - onDisable() { - // this._getRenderScene().destroyLight(this._light); - } - - onDestroy() { - - } -} diff --git a/cocos/3d/framework/light-component.ts b/cocos/3d/framework/light-component.ts new file mode 100644 index 0000000000..a15be397b5 --- /dev/null +++ b/cocos/3d/framework/light-component.ts @@ -0,0 +1,301 @@ +/**************************************************************************** + Copyright (c) 2013-2016 Chukong Technologies Inc. + Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ +import { Component } from '../../components/component'; +import { ccclass, executeInEditMode, menu, property } from '../../core/data/class-decorator'; +import { Color, Enum } from '../../core/value-types'; +import { toRadian } from '../../core/vmath'; +import { Light, LightType } from '../../renderer/scene/light'; +import { PointLight } from '../../renderer/scene/point-light'; +import { SpotLight } from '../../renderer/scene/spot-light'; + +/** + * !#en The light source type + * + * !#ch 光源类型 + * @static + * @enum LightComponent.Type + */ +const Type = Enum({ + /** + * !#en The direction of light + * + * !#ch 平行光 + * @property Directional + * @readonly + * @type {Number} + */ + DIRECTIONAL: LightType.DIRECTIONAL, + /** + * !#en The point of light + * + * !#ch 点光源 + * @property Point + * @readonly + * @type {Number} + */ + POINT: LightType.POINT, + /** + * !#en The spot of light + * + * !#ch 聚光灯 + * @property Spot + * @readonly + * @type {Number} + */ + SPOT: LightType.SPOT, +}); + +/** + * !#en The shadow type + * + * !#ch 阴影类型 + * @static + * @enum LightComponent.ShadowType + */ +const LightShadowType = Enum({ + /** + * !#en No shadows + * + * !#ch 阴影关闭 + * @property None + * @readonly + * @type {Number} + */ + None: 0, + /** + * !#en Soft shadows + * + * !#ch 软阴影 + * @property Soft + * @readonly + * @type {Number} + */ + Soft: 1, + /** + * !#en Hard shadows + * + * !#ch 阴硬影 + * @property Hard + * @readonly + * @type {Number} + */ + Hard: 2, +}); + +/** + * !#en The Light Component + * + * !#ch 光源组件 + * @class LightComponent + * @extends Component + */ +@ccclass('cc.LightComponent') +@menu('Components/LightComponent') +@executeInEditMode +export default class LightComponent extends Component { + + public static Type = Type; + public static ShadowType = LightShadowType; + + @property + protected _type = Type.DIRECTIONAL; + @property + protected _color = Color.WHITE; + @property + protected _intensity = 1; + @property + protected _range = 10; + @property + protected _spotAngle = 60; + + // @property + // protected _shadowType = LightShadowType.None; + // @property + // protected _shadowResolution = 1024; + // @property + // protected _shadowDarkness = 0.5; + // @property + // protected _shadowMinDepth = 1; + // @property + // protected _shadowMaxDepth = 1000; + // @property + // protected _shadowDepthScale = 250; + // @property + // protected _shadowFrustumSize = 50; + // @property + // protected _shadowBias = 0.0005; + + protected _light: Light | null = null; + protected _colorData = [1, 1, 1]; + + /** + * !#en The light source type + * + * !#ch 光源类型 + * + * @type {Number} + */ + @property({ + type: Type, + }) + get type () { + return this._type; + } + + set type (val) { + this._type = val; + this._destroyLight(); + this._createLight(); + } + + /** + * !#en The light source color + * + * !#ch 光源颜色 + * @type {Color} + */ + @property + get color () { + return this._color; + } + + 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; + if (this._light) { + this._light.color = this._colorData; + } + } + + /** + * !#en The light source intensity + * + * !#ch 光源强度 + * @type {Number} + */ + @property + get intensity () { + return this._intensity; + } + + 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; + if (this._light) { + this._light.color = this._colorData; + } + } + + /** + * !#en The light range, used for spot and point light + * + * !#ch 针对聚光灯和点光源设置光源范围 + * @type {Number} + */ + @property + get range () { + return this._range; + } + + set range (val) { + this._range = val; + if (this._light && this._light instanceof PointLight || this._light instanceof SpotLight) { + this._light.range = val; + } + } + + /** + * !#en The spot light cone angle + * + * !#ch 聚光灯锥角 + * @type {Number} + */ + @property + get spotAngle () { + return this._spotAngle; + } + + set spotAngle (val) { + this._spotAngle = val; + if (this._light && this._light instanceof SpotLight) { + this._light.spotAngle = toRadian(val); + } + } + + public onEnable () { + if (this._light) { this._light.enabled = true; return; } + this._createLight(); + } + + public onDisable () { + if (this._light) { this._light.enabled = false; } + } + + public onDestroy () { + this._destroyLight(); + } + + protected _createLight () { + if (!this.node._scene || this._light) { return; } + const scene = this._getRenderScene(); + switch (this._type) { + case Type.DIRECTIONAL: + this._light = scene.createDirectionalLight(this.name); + break; + case Type.POINT: + this._light = scene.createPointLight(this.name); + this.range = this._range; + break; + case Type.SPOT: + this._light = scene.createSpotLight(this.name); + this.range = this._range; + this.spotAngle = this._spotAngle; + break; + } + if (!this._light) { console.warn('we don\'t support this many lights in forward pipeline.'); } + this.color = this._color; + } + + protected _destroyLight () { + if (!this.node._scene || !this._light) { return; } + const scene = this._getRenderScene(); + switch (this._type) { + case Type.DIRECTIONAL: + scene.destroyDirectionalLight(this._light); + break; + case Type.POINT: + scene.destroyPointLight(this._light); + break; + case Type.SPOT: + scene.destroySpotLight(this._light); + break; + } + this._light = null; + } +} diff --git a/cocos/3d/framework/model-component.ts b/cocos/3d/framework/model-component.ts index fbdf94f85e..3436895072 100644 --- a/cocos/3d/framework/model-component.ts +++ b/cocos/3d/framework/model-component.ts @@ -22,6 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ +import { builtinResMgr } from '../../3d/builtin'; import { ccclass, executeInEditMode, executionOrder, menu, property } from '../../core/data/class-decorator'; import Enum from '../../core/value-types/enum'; import { Model } from '../../renderer/scene/model'; @@ -196,15 +197,15 @@ export class ModelComponent extends RenderableComponent { this._model = null; } - if (this._model) { - this._model.enabled = this.enabledInHierarchy; - } - this._model = this._createModel(); this._model!.createBoundingShape(this._mesh.minPosition, this._mesh.maxPosition); this._updateModelParams(); + + if (this._model) { + this._model.enabled = this.enabledInHierarchy; + } } protected _createModel () { diff --git a/cocos/pipeline/render-pipeline.ts b/cocos/pipeline/render-pipeline.ts index 52bcc31cd6..fc3c6c12d7 100644 --- a/cocos/pipeline/render-pipeline.ts +++ b/cocos/pipeline/render-pipeline.ts @@ -25,14 +25,23 @@ export class UBOGlobal { public static MAT_VIEW_PROJ_OFFSET: number = UBOGlobal.MAT_PROJ_INV_OFFSET + 16; public static MAT_VIEW_PROJ_INV_OFFSET: number = UBOGlobal.MAT_VIEW_PROJ_OFFSET + 16; public static CAMERA_POS_OFFSET: number = UBOGlobal.MAT_VIEW_PROJ_INV_OFFSET + 16; - public static COUNT: number = UBOGlobal.CAMERA_POS_OFFSET + 4; + public static DIR_LIGHT_DIR_OFFSET: number = UBOGlobal.CAMERA_POS_OFFSET + 4; + public static DIR_LIGHT_COLOR_OFFSET: number = UBOGlobal.DIR_LIGHT_DIR_OFFSET + 16; + public static POINT_LIGHT_POS_RANGE_OFFSET: number = UBOGlobal.DIR_LIGHT_COLOR_OFFSET + 16; + public static POINT_LIGHT_COLOR_OFFSET: number = UBOGlobal.POINT_LIGHT_POS_RANGE_OFFSET + 16; + public static SPOT_LIGHT_POS_RANGE_OFFSET: number = UBOGlobal.POINT_LIGHT_COLOR_OFFSET + 16; + public static SPOT_LIGHT_DIR_OFFSET: number = UBOGlobal.SPOT_LIGHT_POS_RANGE_OFFSET + 16; + public static SPOT_LIGHT_COLOR_OFFSET: number = UBOGlobal.SPOT_LIGHT_DIR_OFFSET + 16; + public static COUNT: number = UBOGlobal.SPOT_LIGHT_COLOR_OFFSET + 16; public static SIZE: number = UBOGlobal.COUNT * 4; public static BLOCK: GFXUniformBlock = { binding: 30, name: 'CCGlobal', members: [ + // constants { name: 'cc_time', type: GFXType.FLOAT4, count: 1 }, { name: 'cc_screenSize', type: GFXType.FLOAT4, count: 1 }, { name: 'cc_screenScale', type: GFXType.FLOAT4, count: 1 }, + // transform { name: 'cc_matView', type: GFXType.MAT4, count: 1 }, { name: 'cc_matViewInv', type: GFXType.MAT4, count: 1 }, { name: 'cc_matProj', type: GFXType.MAT4, count: 1 }, @@ -40,6 +49,14 @@ export class UBOGlobal { { name: 'cc_matViewProj', type: GFXType.MAT4, count: 1 }, { name: 'cc_matViewProjInv', type: GFXType.MAT4, count: 1 }, { name: 'cc_cameraPos', type: GFXType.FLOAT4, count: 1 }, + // 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 }, ], }; @@ -335,6 +352,24 @@ export abstract class RenderPipeline { _vec4Array[3] = 1.0; this._uboGlobal.view.set(_vec4Array, UBOGlobal.CAMERA_POS_OFFSET); + 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); + } + 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); + } + 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); + } + // update ubos this._globalUBO!.update(this._uboGlobal.view); } diff --git a/cocos/renderer/core/effect.ts b/cocos/renderer/core/effect.ts index 461477c87a..125ae93f1d 100644 --- a/cocos/renderer/core/effect.ts +++ b/cocos/renderer/core/effect.ts @@ -24,13 +24,17 @@ export class Effect { const passes: Pass[] = new Array(passNum); for (let k = 0; k < passNum; ++k) { const passInfo = tech.passes[k] as IPassInfoFull; + const shader = programLib.getGFXShader(cc.game._gfxDevice, + passInfo.program, defines && defines[k] || {}); + const renderPass = cc.director.root.pipeline.getRenderPass(passInfo.stage || RenderPassStage.DEFAULT); const prog = programLib.getTemplate(passInfo.program); - passInfo.shader = programLib.getGFXShader(cc.game._gfxDevice, - passInfo.program, defines && defines[k] || {})!; - passInfo.renderPass = cc.director.root.pipeline.getRenderPass(passInfo.stage || RenderPassStage.DEFAULT); + passInfo.blocks = prog.blocks; + passInfo.samplers = prog.samplers; passInfo.globals = cc.director.root.pipeline.globalUBO; - passInfo.blocks = prog!.blocks; - passInfo.samplers = prog!.samplers; + if (shader) { passInfo.shader = shader; } + else { console.warn(`create shader ${passInfo.program} failed`); } + if (renderPass) { passInfo.renderPass = renderPass; } + else { console.warn(`illegal pass stage in ${effect.name}`); } const pass = new Pass(cc.game._gfxDevice); pass.initialize(passInfo); passes[k] = pass; diff --git a/cocos/renderer/core/pass.ts b/cocos/renderer/core/pass.ts index 8b2f5d27fb..3b5e0c3bec 100644 --- a/cocos/renderer/core/pass.ts +++ b/cocos/renderer/core/pass.ts @@ -199,7 +199,9 @@ export class Pass { } public getHandle (name: string) { - return this._handleMap[name]; + const handle = this._handleMap[name]; + if (!handle) { console.warn(`illegal property name ${name}`); } + return handle; } public getBinding (name: string) { diff --git a/cocos/renderer/scene/directional-light.ts b/cocos/renderer/scene/directional-light.ts new file mode 100644 index 0000000000..cde70935a4 --- /dev/null +++ b/cocos/renderer/scene/directional-light.ts @@ -0,0 +1,20 @@ +import { Vec3 } from '../../core/value-types'; +import { vec3 } from '../../core/vmath'; +import { Light, LightType } from './light'; +import { RenderScene } from './render-scene'; + +export class DirectionalLight extends Light { + protected _direction = new Float32Array(4); + + set direction (val: Vec3) { + vec3.array(this._direction, val); + } + get directionArray () { + return this._direction; + } + + constructor (scene: RenderScene, name: string) { + super(scene, name); + this._type = LightType.DIRECTIONAL; + } +} diff --git a/cocos/renderer/scene/light.ts b/cocos/renderer/scene/light.ts index 6ff69abc81..45379330e2 100644 --- a/cocos/renderer/scene/light.ts +++ b/cocos/renderer/scene/light.ts @@ -1,27 +1,44 @@ import { RenderScene } from './render-scene'; export enum LightType { + UNKNOWN, DIRECTIONAL, POINT, + SPOT, } export class Light { + protected _color = new Float32Array(4); + protected _enabled = false; - public get type (): LightType { - return this._type; + set enabled (val) { + this._enabled = val; + } + get enabled () { + return this._enabled; } - public get name (): string { + set color (val: number[]) { + this._color.set(val); + } + get colorData () { + return this._color; + } + + get type (): LightType { + return this._type; + } + get name (): string { return this._name; } - private _scene: RenderScene; - private _type: LightType; - private _name: string; + protected _scene: RenderScene; + protected _type: LightType; + protected _name: string; - constructor (scene: RenderScene, type: LightType, name: string) { + constructor (scene: RenderScene, name: string) { this._scene = scene; - this._type = type; this._name = name; + this._type = LightType.UNKNOWN; } } diff --git a/cocos/renderer/scene/point-light.ts b/cocos/renderer/scene/point-light.ts new file mode 100644 index 0000000000..68af391dc0 --- /dev/null +++ b/cocos/renderer/scene/point-light.ts @@ -0,0 +1,24 @@ +import { Vec3 } from '../../core/value-types'; +import { vec3 } from '../../core/vmath'; +import { Light, LightType } from './light'; +import { RenderScene } from './render-scene'; + +export class PointLight extends Light { + protected _positionAndRange = new Float32Array(4); + + set position (val: Vec3) { + vec3.array(this._positionAndRange, val); + } + set range (val: number) { + this._positionAndRange[3] = val; + } + + get positionAndRange () { + return this._positionAndRange; + } + + constructor (scene: RenderScene, name: string) { + super(scene, name); + this._type = LightType.POINT; + } +} diff --git a/cocos/renderer/scene/render-scene.ts b/cocos/renderer/scene/render-scene.ts index 73e16820a5..9be39b8f7c 100644 --- a/cocos/renderer/scene/render-scene.ts +++ b/cocos/renderer/scene/render-scene.ts @@ -8,8 +8,11 @@ import { GFXPrimitiveMode } from '../../gfx/define'; import { Node } from '../../scene-graph'; import { Layers } from '../../scene-graph/layers'; import { Camera, ICameraInfo } from './camera'; -import { Light, LightType } from './light'; +import { DirectionalLight } from './directional-light'; +import { Light } from './light'; import { Model } from './model'; +import { PointLight } from './point-light'; +import { SpotLight } from './spot-light'; export interface IRenderSceneInfo { name: string; @@ -28,23 +31,31 @@ export interface IRaycastResult { export class RenderScene { - public get root (): Root { + get root (): Root { return this._root; } - public get name (): string { + get name (): string { return this._name; } - public get cameras (): Camera[] { + get cameras (): Camera[] { return this._cameras; } - public get pointLights (): Light[] { + get directionalLights (): DirectionalLight[] { + return this._directionalLights; + } + + get pointLights (): PointLight[] { return this._pointLights; } - public get models (): Model[] { + get spotLights (): SpotLight[] { + return this._spotLights; + } + + get models (): Model[] { return this._models; } @@ -55,7 +66,9 @@ export class RenderScene { private _root: Root; private _name: string = ''; private _cameras: Camera[] = []; - private _pointLights: Light[] = []; + private _pointLights: PointLight[] = []; + private _directionalLights: DirectionalLight[] = []; + private _spotLights: SpotLight[] = []; private _models: Model[] = []; private _modelId: number = 0; @@ -104,8 +117,9 @@ export class RenderScene { return null; } - public createPointLight (name: string): Light { - const light = new Light(this, LightType.POINT, name); + public createPointLight (name: string): Light | null { + if (this._pointLights.length >= 4) { return null; } + const light = new PointLight(this, name); this._pointLights.push(light); return light; } @@ -119,10 +133,50 @@ export class RenderScene { } } + public createDirectionalLight (name: string): Light | null { + if (this._directionalLights.length >= 4) { return null; } + const light = new DirectionalLight(this, name); + this._directionalLights.push(light); + return light; + } + + public destroyDirectionalLight (light: Light) { + for (let i = 0; i < this._directionalLights.length; ++i) { + if (this._directionalLights[i] === light) { + this._directionalLights.slice(i); + return; + } + } + } + + public createSpotLight (name: string): Light | null { + if (this._spotLights.length >= 4) { return null; } + const light = new SpotLight(this, name); + this._spotLights.push(light); + return light; + } + + public destroySpotLight (light: Light) { + for (let i = 0; i < this._spotLights.length; ++i) { + if (this._spotLights[i] === light) { + this._spotLights.slice(i); + return; + } + } + } + public destroyPointLights () { this._pointLights = []; } + public destroyDirectionalLights () { + this._directionalLights = []; + } + + public destroySpotLights () { + this._spotLights = []; + } + public createModel (clazz: new (scene: RenderScene, node: Node) => T, node: Node): T { const model = new clazz(this, node); this._models.push(model); diff --git a/cocos/renderer/scene/spot-light.ts b/cocos/renderer/scene/spot-light.ts new file mode 100644 index 0000000000..30ce89a366 --- /dev/null +++ b/cocos/renderer/scene/spot-light.ts @@ -0,0 +1,34 @@ +import { Vec3 } from '../../core/value-types'; +import { vec3 } from '../../core/vmath'; +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); + + set position (val: Vec3) { + vec3.array(this._positionAndRange, val); + } + set range (val: number) { + this._positionAndRange[3] = val; + } + set direction (val: Vec3) { + vec3.array(this._direction, val); + } + set spotAngle (val: number) { + this._direction[3] = val; + } + + get positionAndRange () { + return this._positionAndRange; + } + get directionArray () { + return this._direction; + } + + constructor (scene: RenderScene, name: string) { + super(scene, name); + this._type = LightType.DIRECTIONAL; + } +} diff --git a/playground/simple.html b/playground/simple.html index b3788fa6d6..aef133da14 100644 --- a/playground/simple.html +++ b/playground/simple.html @@ -58,13 +58,17 @@ cameraNode.addComponent(FirstPersonCamera); const camComp = cameraNode.addComponent(cc.CameraComponent); + const lightNode = new cc.Node('Light'); + lightNode.parent = scene; + lightNode.setRotationFromEuler(70, 0, 0); + const lightComp = lightNode.addComponent(cc.LightComponent); + const texture = createTexture(cc.game.canvas.getElementsByTagName('img')); 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-unlit'; - material.setProperty('color', [ cc.Color.RED, cc.Color.BLUE ]); + material.effectName = 'builtin-effect-phong'; const len = 50; const models = [], passes = []; @@ -80,7 +84,7 @@ // material instance initialized here passes.push(modelCom.material.passes[0]); } - const handle = passes[0].getHandle('color'); + const handle = passes[0].getHandle('diffuseColor'); { const material = new cc.Material(); @@ -116,8 +120,8 @@ const x = Math.cos(rad + t), y = Math.sin(rad + t); let z = m._lpos.z + dt; if (z > len * 2) z = 0; m.setPosition(x * 2, y * 2, z); - // color.fromHSV((x + 1) * 0.5, 0.5, 1); - // passes[i].setUniform(handle, color); + color.fromHSV((x + 1) * 0.5, 0.5, 1); + passes[i].setUniform(handle, color); }); }); diff --git a/tslint.json b/tslint.json index fcac35efac..8f4d116805 100644 --- a/tslint.json +++ b/tslint.json @@ -17,7 +17,8 @@ "ban-types": false, "no-bitwise": false, "no-empty": false, - "radix": false + "radix": false, + "one-line": false, }, "rulesDirectory": [] }