I have created a mesh from a shapeGeometry, added a shader to draw the borders.
to have a smoother result I have used the tessellation modifier to increase the poly count.
- Is there a better way to add points to the geometry (the triangles aren’t in the direction of the shape).
- How could I have a smoother result ( using UVs instead of position in the shader maybe?)
here si the mesh code
let roomShape = new THREE.Shape(array2D);
let roomGeo = new THREE.ShapeGeometry(roomShape, 10);
const tessellateModifier = new TessellateModifier(0.075, 20); // 1, 5) (8, 6)
// roomGeo.rotateX(-Math.PI / 2);
let tesselGeo;
try {
tesselGeo = tessellateModifier.modify(roomGeo);
} catch (e) {
console.warn(e);
}
vertex shader:
vertexShader = `
//varying vec2 vUv;
out float isBorder;
uniform int shapeLen;
uniform vec2 shapeArr[shapeArrayLength];
uniform float uWidth;
varying vec3 vNormal;
varying vec3 vViewDir;
uniform vec3 vDir;
void main(){
// vUv = uv;
// vec2 pos = position.xz ;
vec2 pos = position.xy ;
isBorder = 0.0;
//for each side / corner
for (int i = 0; i < shapeLen ; i++){
//egde vector (AB)
highp vec2 AB;
highp vec2 B;
//edge vector
if( i < (shapeLen-1 )){ int next = i+1;
AB = shapeArr[next] - shapeArr[i] ; }
else{ AB = vec2( shapeArr[0] - shapeArr[i] ) ; } //close the shape
vec2 ABnorm = normalize(AB); //unit vector of the edge
float ABlen = length(AB);//distance( shapeArr[i], B );
//vector corner to position (AX)
vec2 AX = pos - shapeArr[i] ;
float AXlen = length(AX); //distance( shapeArr[i], pos );
vec2 AXnorm = normalize(AX); //unit vector
/* //test corners
float dist = distance(shapeArr[i], pos );
if(dist < .150){isBorder = 1.0;}*/
//dot product to project point to edge
float dotSide = dot(AX, AB);
//if( dotSide >= 0.0 && dotSide <= 1.0){
//isBorder =0.15;
//angle of both vectors
float angle = acos(dot(ABnorm, AXnorm));
//length of projected vector
float APlen = cos(angle) * AXlen;
// distance to edge
float XPlen = sqrt( pow(AXlen, 2.0) - pow(APlen, 2.0));
// check distance to edge
if(XPlen < uWidth ){
// straightborders
isBorder = 1.0;
/*
if(XPlen < uWidth){
isBorder = 1.0;
}
else{
isBorder = mix(uWidth, uWidth*2.0, XPlen);
}*/
}
}
vec4 modelPosition = modelMatrix * vec4(position, 1.0);
vec4 viewPosition = viewMatrix * modelPosition;
vec4 clipPosition = projectionMatrix * viewPosition;
gl_Position = clipPosition;
}
`;```