So I am trying to apply shadows to this shader I have but I get this error:
THREE.WebGLShader: gl.getShaderInfoLog() vertex ERROR: 0:229: 'transformedNormal' : undeclared identifier ERROR: 0:229: 'inverseTransformDirection' : no matching overloaded function found ERROR: 0:229: '=' : dimension mismatch ERROR: 0:229: '=' : cannot convert from 'const mediump float' to 'highp 3-component vector of float' ERROR: 0:254: 'mvPosition' : redefinition
I assume there's missing shader chunk for normals but I have no idea how to implement one as I am very very new to webgl and three I will put the code below can anyone guide me on how I can apply a fix for this?
shader module:
import { mergeUniforms } from './three/src/renderers/shaders/UniformsUtils.js'
import { UniformsLib } from './three/src/renderers/shaders/UniformsLib.js'
export default {
uniforms: mergeUniforms([
UniformsLib.lights,
UniformsLib.fog,
]),
vertexShader: `
#include <common>
#include <fog_pars_vertex>
#include <shadowmap_pars_vertex>
varying vec2 vUv;
uniform float time;
float N (vec2 st) { // https://thebookofshaders.com/10/
return fract( sin( dot( st.xy, vec2(12.9898,78.233 ) ) ) * 43758.5453123);
}
float smoothNoise( vec2 ip ){ // https://www.youtube.com/watch?v=zXsWftRdsvU
vec2 lv = fract( ip );
vec2 id = floor( ip );
lv = lv * lv * ( 3. - 2. * lv );
float bl = N( id );
float br = N( id + vec2( 1, 0 ));
float b = mix( bl, br, lv.x );
float tl = N( id + vec2( 0, 1 ));
float tr = N( id + vec2( 1, 1 ));
float t = mix( tl, tr, lv.x );
return mix( b, t, lv.y );
}
void main() {
#include <begin_vertex>
#include <project_vertex>
#include <worldpos_vertex>
#include <shadowmap_vertex>
#include <fog_vertex>
vUv = uv;
float t = time * 2.;
// VERTEX POSITION
vec4 mvPosition = vec4( position, 1.0 );
#ifdef USE_INSTANCING
mvPosition = instanceMatrix * mvPosition;
#endif
// DISPLACEMENT
float noise = smoothNoise(mvPosition.xz * 0.5 + vec2(0., t));
noise = pow(noise * 0.5 + 0.5, 2.) * 2.;
// here the displacement is made stronger on the blades tips.
float dispPower = 1. - cos( uv.y * 3.1416 * 0.5 );
float displacement = noise * ( 0.3 * dispPower );
mvPosition.z -= displacement;
//
vec4 modelViewPosition = modelViewMatrix * mvPosition;
gl_Position = projectionMatrix * modelViewPosition;
}
`,
fragmentShader: `
#include <common>
#include <packing>
#include <fog_pars_fragment>
#include <bsdfs>
#include <lights_pars_begin>
#include <shadowmap_pars_fragment>
#include <shadowmask_pars_fragment>
#include <dithering_pars_fragment>
varying vec2 vUv;
void main() {
// CHANGE THAT TO YOUR NEEDS
// ------------------------------
vec3 baseColor = vec3( 0.41, 1.0, 0.5 );
float clarity = ( vUv.y * 0.875 ) + 0.125;
vec3 shadowColor = vec3(0, 0, 0);
float shadowPower = 0.5;
// ------------------------------
// it just mixes the shadow color with the frag color
gl_FragColor = vec4( mix( (baseColor * clarity, 1), shadowColor, (1.0 - getShadowMask() ) * shadowPower), 1.0);
#include <fog_fragment>
#include <dithering_fragment>
}
`
};
and in the main I have this:
const uniforms = {
time: {
value: 0
}
}
this.leavesMaterial = new THREE.ShaderMaterial({
...BasicCustomShader,
uniforms,
side: THREE.DoubleSide,
fog: true,
lights: true,
dithering: true,
});
const instanceNumber = 50000;
const dummy = new THREE.Object3D();
const geometry = new THREE.PlaneGeometry( 0.1, 1, 1, 4 );
geometry.translate( 0, 0.5, 0 ); // move grass blade geometry lowest point at 0.
geometry.receiveShadow = true;
const instancedMesh = new THREE.InstancedMesh( geometry, this.leavesMaterial, instanceNumber );
geometry.receiveShadow = true;
this.scene_.add( instancedMesh );
// Position and scale the grass blade instances randomly.
for ( let i=0 ; i<instanceNumber ; i++ ) {
dummy.position.set(
( Math.random() - 0.3 ) * 300,
0,
( Math.random() - 0.3 ) * 300
);
dummy.scale.setScalar( 1.0 + Math.random() * 2.6 );
dummy.scale.y = 1.0 + Math.random() * 5.5;
dummy.rotation.y = Math.random() * Math.PI;
dummy.updateMatrix();
instancedMesh.setMatrixAt( i, dummy.matrix );
}
// animate loop
this.leavesMaterial.uniforms.time.value = clock.getElapsedTime();
this.leavesMaterial.uniformsNeedUpdate = true;
thank you for any help
Via Active questions tagged javascript - Stack Overflow https://ift.tt/vQZ7qGo
Comments
Post a Comment