Apparently this won’t work for instanced shadows r132 and no shadows infact it breaks my instancing and instead of spawning randomly they are spawn on one spot
loader.load("./resources/terrain/lantern.glb", function (geometry) {
console.log(geometry);
let geometry_ = geometry.scene;
geometry_.traverse(function (child) {
child.castShadow = true;
child.receiveShadow = true;
if (child.isMesh) {
child.material.envMapIntensity = 0.8;
// child.material.emissive = new THREE.Color(0xff00ff);
// emissiveMap: animateMap.texture,
child.material.emissiveIntensity = 10.2;
child.material.toneMapped = false;
var instanceMaterial = child.material;
instanceMaterial.onBeforeCompile = (shader) => {
shader.vertexShader =
`
attribute vec3 offset;
attribute vec4 orientation;
vec3 applyQuaternionToVector( vec4 q, vec3 v ){
return v + 2.0 * cross( q.xyz, cross( q.xyz, v ) + q.w * v );
}
` + shader.vertexShader;
shader.vertexShader = shader.vertexShader.replace(
"#include <project_vertex>",
`
vec3 vPosition = applyQuaternionToVector( orientation, transformed );
vec4 mvPosition = modelViewMatrix * vec4( vPosition, 1.0 );
gl_Position = projectionMatrix * modelViewMatrix * vec4( offset + vPosition, 1.0 );
`
);
};
// child.material.blending = THREE.AdditiveBlending;
meshgrass = new THREE.InstancedMesh(child.geometry, instanceMaterial, countgrass);
meshgrass.frustumCulled = false;
meshgrass.customDistanceMaterial = new THREE.MeshDistanceMaterial({
depthPacking: THREE.RGBADepthPacking,
alphaTest: 0.5
});
meshgrass.customDistanceMaterial.onBeforeCompile = (shader) => {
// app specific instancing shader code
shader.vertexShader =
`#define DEPTH_PACKING 3201
attribute vec3 offset;
attribute vec4 orientation;
vec3 applyQuaternionToVector( vec4 q, vec3 v ){
return v + 2.0 * cross( q.xyz, cross( q.xyz, v ) + q.w * v );
}
` + shader.vertexShader;
shader.vertexShader = shader.vertexShader.replace(
"#include <project_vertex>",
`
vec3 vPosition = offset + applyQuaternionToVector( orientation, transformed );
vec4 mvPosition = modelMatrix * vec4( vPosition, 1.0 );
transformed = vPosition;
gl_Position = projectionMatrix * modelViewMatrix * vec4( transformed, 1.0 );`
);
shader.fragmentShader =
"#define DEPTH_PACKING 3201" + "\n" + shader.fragmentShader;
};
meshgrass.castShadow = true;
meshgrass.receiveShadow = true;
scene.add(meshgrass);
for (let i = 0; i < countgrass; i++) {
dummy.updateMatrix();
meshgrass.setMatrixAt(i, dummy.matrix);
}
}
});
});
any ideas?