Hope you are all doing well.
I am working on a fakelight effect with multiple light sources based on MeshStandardMaterial.
Here is the shader code below;
object.material.onBeforeCompile = function(shader) {
shader.vertexShader = `
varying vec3 vTransformedNormal;
` + shader.vertexShader;
shader.vertexShader = shader.vertexShader.replace(
`#include <worldpos_vertex>`,
`
#include <worldpos_vertex>
vTransformedNormal = normalize(normalMatrix * normal);
`
);
shader.fragmentShader = `
uniform vec3 worldPosition;
uniform vec3 lightDirections[3];
uniform vec3 lightColors[3];
uniform float lightIntensities[3];
uniform float uBaseLight;
uniform vec3 uAmbientLight;
varying vec3 vTransformedNormal;
` + shader.fragmentShader;
shader.fragmentShader = shader.fragmentShader.replace(
`vec4 diffuseColor = vec4( diffuse, opacity );`,
`
vec4 diffuseColor = vec4( diffuse, opacity );
vec3 totalLight = uAmbientLight;
for(int i = 0; i < 3; i++) {
float fakeDiffuse = max(dot(vTransformedNormal, lightDirections[i]), uBaseLight);
totalLight += lightColors[i] * fakeDiffuse * lightIntensities[i];
}
diffuseColor.rgb = clamp(diffuseColor.rgb * totalLight, 0.0, 3.0);
`
);
shader.uniforms.worldPosition = { value: new THREE.Vector3() };
shader.uniforms.lightDirections = { value: [new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3()] };
shader.uniforms.lightColors = { value: [new THREE.Vector3(1, 1, 1), new THREE.Vector3(1, 1, 1), new THREE.Vector3(1, 1, 1)] };
shader.uniforms.lightIntensities = { value: [1.0, 1.0, 4.0] };
shader.uniforms.uAmbientLight = { value: new THREE.Vector3(ambientRate, ambientRate, ambientRate) };
object.material.userData.shader = shader;
};
and i pass the light information like this
directionVec.set(lightPos.x - worldRef.position.x, lightPos.y - worldRef.position.y, lightPos.z - worldRef.position.z).normalize();
directionVec.applyQuaternion(worldRef.quaternion);
object.material.userData.shader.uniforms.lightDirections.value[i].copy(directionVec);
when i try like this;
directionVec.set(lightPos.x - worldRef.position.x, lightPos.y - worldRef.position.y, lightPos.z - worldRef.position.z).normalize();
results are like below;
and when i try with this;
directionVec.set(worldRef.position.x - lightPos.x, worldRef.position.y - lightPos.y, worldRef.position.z - lightPos.z).normalize();
this is the result below
I am stuck on this right now. I am not sure has anybody tried it before but it would really welcome if you have any clue how to solve it.
NOTES;
- I tried inverting quaternion
- I tried with EULER
- I also played with the shader but no luck
Thanks !