Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Plot = ({fx, length, resolution}) => {
- const vertices = [];
- const indices = [];
- const radialSegments = 50;
- const rings = resolution;
- const addVertex = (v) => {
- vertices.push(v.x,v.y,v.z);
- }
- const addIndex = (x) => {
- indices.push(x);
- }
- const P = (x,y,dy,r,u) => {
- const normal = -1/dy;
- return {
- x: x + r*Math.cos(u) * 1,
- y: y + r*Math.cos(u) * normal,
- z: r*Math.sin(u)
- }
- }
- const U = (x) => {
- return x/radialSegments * Math.PI * 2;
- }
- const X = (x) => {
- return (x/rings-length/2);
- }
- const r1 = 0.1;
- for(let i = 0; i < length*rings; i++) {
- const x = X(i);
- const x1 = X(i-1);
- const y = fx({x:x});
- const y1 = fx({x:x1});
- const h = 1e-2;
- const dy = (fx({x: x+h}) - fx({x :x-h})) / (2*h)
- const dy1 = (fx({x: x1+h}) - fx({x :x1-h})) / (2*h)
- if(Math.abs(y) >= length/2)
- continue;
- for(let j = 0; j <= radialSegments; j++) {
- const v0 = P( x, y, dy, r1, U(j) );
- const v1 = P( x, y, dy, r1, U(j+1));
- const v2 = P(x1, y1, dy1, r1, U(j+1));
- const v3 = P(x1, y1, dy1, r1, U(j) );
- addVertex(v0);
- addVertex(v1);
- addVertex(v2);
- addVertex(v3);
- const index = vertices.length / 3;
- addIndex(index);
- addIndex(index + 1);
- addIndex(index + 2);
- addIndex(index);
- addIndex(index + 2);
- addIndex(index + 3);
- }
- }
- const positionAttribute = new BufferAttribute(new Float32Array(vertices), 3);
- const indexAttribute = new BufferAttribute(new Uint32Array(indices), 1);
- const customGeometry = new BufferGeometry();
- customGeometry.setAttribute('position', positionAttribute);
- customGeometry.setIndex(indexAttribute);
- customGeometry.computeVertexNormals();
- return (
- <>
- <mesh geometry={customGeometry}>
- <meshStandardMaterial color={0xff0000} side={DoubleSide} flatShading={true}/>
- </mesh>
- </>
- )
- }
Advertisement
Add Comment
Please, Sign In to add comment