pubgv3/assets/scripts/shaders/pointLight.js
zhuguoqing ff550d5d6a init
2022-05-22 10:32:02 +08:00

95 lines
3.4 KiB
JavaScript

const renderEngine = cc.renderer.renderEngine;
const renderer = renderEngine.renderer;
let ShaderMaterial = require('ShaderMaterial');
var timelimit =1
const shader = {
name: 'pointLight',
params: [
{ name: 'u_lightPos', type: renderer.PARAM_FLOAT3 },
{ name: 'u_lightColor', type: renderer.PARAM_FLOAT3 },
{ name: 'u_ambientColor', type: renderer.PARAM_FLOAT3 },
{ name: 'u_normals', type: renderer.PARAM_TEXTURE_2D },
{ name: 'u_brightness', type: renderer.PARAM_FLOAT },
{ name: 'u_cutoffRadius', type: renderer.PARAM_FLOAT },
{ name: 'u_halfRadius', type: renderer.PARAM_FLOAT },
],
start(material,params) {
material.cusparmas = {}
material.cusparmas._start = 0;
material.setParamValue('u_normals', params.normaltexture._texture);
material.setParamValue('u_lightColor', cc.v3(params.u_lightColor.r,params.u_lightColor.g,params.u_lightColor.b));
material.setParamValue('u_ambientColor', cc.v3(params.u_ambientColor.r,params.u_ambientColor.g,params.u_ambientColor.b));
material.setParamValue('u_lightPos', params.u_lightPos);
material.setParamValue('u_brightness', 2.0);
material.setParamValue('u_cutoffRadius', 800);
material.setParamValue('u_halfRadius', 0.8);
},
// update(material,dt) {
// // material.cusparmas._start+=dt*0.2
// // //var deltatime = Math.sin(( material.cusparmas._start)/1000);
// // material.setParamValue('time',Math.sin(material.cusparmas._start)*2);
// },
defines:[],
frag:
`
varying vec2 uv0;
uniform sampler2D u_normals;
uniform sampler2D texture;
uniform vec3 u_lightPos;
uniform vec3 u_lightColor;
uniform vec3 u_ambientColor;
uniform float u_brightness;
uniform float u_cutoffRadius;
uniform float u_halfRadius;
void main(void)
{
vec3 DirectionalLight = normalize(u_lightPos);
vec3 nor =texture2D(u_normals, uv0).rgb;
float diffuseIntensity = max(0.0,dot(DirectionalLight,nor));
vec3 lightcolor = vec3(diffuseIntensity,diffuseIntensity,diffuseIntensity);
vec4 texColor=texture2D(texture, uv0);
gl_FragColor =vec4(texColor.rgb+lightcolor, texColor.a);
// vec4 texColor=texture2D(texture, uv0);
// vec3 normal=texture2D(u_normals, uv0).rgb;
// // normal=normal*2.0-1.0;
// vec3 lightVec = - u_lightPos;
// vec3 posToLight = -normalize(lightVec);
// float normDotPosToLight=max(0.0,dot(normal,posToLight));
// float intercept = u_cutoffRadius * u_halfRadius;
// float dx_1 = 0.5 / intercept;
// float dx_2 = 0.5 / (u_cutoffRadius - intercept);
// float offset = 0.5 + intercept * dx_2;
// float lightDist = length(lightVec);
// float falloffTermNear = clamp((1.0 - lightDist * dx_1), 0.0, 1.0);
// float falloffTermFar = clamp((offset - lightDist * dx_2), 0.0, 1.0);
// float falloffSelect = step(intercept, lightDist);
// float falloffTerm = (1.0 - falloffSelect) * falloffTermNear + falloffSelect * falloffTermFar;
// vec3 diffuse = normDotPosToLight*u_lightColor*falloffTerm ;//* u_brightness * falloffTerm * u_lightColor;
// gl_FragColor = vec4(texColor.rgb * (diffuse + u_ambientColor), texColor.a);
}
`,
};
ShaderMaterial.addShader(shader);