Skip to content
TheChatArchiveThe Chat Archive
o1-mini-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

make the middle lines have a zig-zag fractal pattern underneath them consisting of three…

  1. User

    make the middle lines have a zig-zag fractal pattern underneath them consisting of three lines

    First point should be the start point of the middle line Second point should be: x: 1/3 of way down middle line (to the middle) y: 1/3 of either left of right line of the same rank Third point should be: x: 2/3 of way down middle line (to the end of the middle line) y: 1/3 of either left of right line of the same rank DOWN * 2 Fourth point should be the end point of the middle line

    // Scene setup
    const scene = new THREE.Scene();
    scene.background = new THREE.Color(0x000000);
    
    // Camera setup
    const aspectRatio = window.innerWidth / window.innerHeight;
    const cameraSize = 200;
    const camera = new THREE.OrthographicCamera(
      -cameraSize * aspectRatio,
      cameraSize * aspectRatio,
      cameraSize,
      -cameraSize,
      1,
      1000
    );
    camera.position.set(0, 0, 500);
    
    const renderer = new THREE.WebGLRenderer({ antialias: true });
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);
    
    // Zoom logic (based on mouse wheel and key modifiers)
    const zoomSpeed = 0.8;
    
    function onWheel(event) {
      event.preventDefault();
    
      const rect = renderer.domElement.getBoundingClientRect();
      const mouseX = ((event.clientX - rect.left) / rect.width) * 2 - 1;
      const mouseY = -((event.clientY - rect.top) / rect.height) * 2 + 1;
    
      const mouseWorld = new THREE.Vector3(mouseX, mouseY, 0).unproject(camera);
      const zoomFactor = 1 - event.deltaY * 0.001 * zoomSpeed;
    
      const ctrlPressed = event.ctrlKey;
      const shiftPressed = event.shiftKey;
    
      if (ctrlPressed) {
        camera.left = (camera.left - mouseWorld.x) * zoomFactor + mouseWorld.x;
        camera.right = (camera.right - mouseWorld.x) * zoomFactor + mouseWorld.x;
      } else if (shiftPressed) {
        camera.top = (camera.top - mouseWorld.y) * zoomFactor + mouseWorld.y;
        camera.bottom = (camera.bottom - mouseWorld.y) * zoomFactor + mouseWorld.y;
      } else {
        camera.left = (camera.left - mouseWorld.x) * zoomFactor + mouseWorld.x;
        camera.right = (camera.right - mouseWorld.x) * zoomFactor + mouseWorld.x;
        camera.top = (camera.top - mouseWorld.y) * zoomFactor + mouseWorld.y;
        camera.bottom = (camera.bottom - mouseWorld.y) * zoomFactor + mouseWorld.y;
      }
    
      camera.updateProjectionMatrix();
    }
    
    // Prevent default browser behavior for wheel scrolling with Control or Shift key
    window.addEventListener('wheel', onWheel, { passive: false });
    
    // Prevent default context menu on right-click
    window.addEventListener('contextmenu', (event) => {
      event.preventDefault();
    });
    
    // Render loop
    function animate() {
      requestAnimationFrame(animate);
      renderer.render(scene, camera);
    }
    
    // Handle window resize
    function onWindowResize() {
      const aspect = window.innerWidth / window.innerHeight;
      camera.left = -cameraSize * aspect;
      camera.right = cameraSize * aspect;
      camera.top = cameraSize;
      camera.bottom = -cameraSize;
      camera.updateProjectionMatrix();
      renderer.setSize(window.innerWidth, window.innerHeight);
      regenerateFractal();
    }
    window.addEventListener('resize', onWindowResize);
    
    // Initialize animation
    animate();
    
    // Panning functionality
    let isPanning = false;
    let isDraggingEndpoint = false;
    let selectedEndpointIndex = null;
    const previousMousePosition = new THREE.Vector2();
    
    // Raycaster for detecting line interaction
    const raycaster = new THREE.Raycaster();
    const mouse = new THREE.Vector2();
    
    // Interactive main line code
    const lineMaterial = new THREE.LineBasicMaterial({ color: 0xffffff });
    const mainLineGeometry = new THREE.BufferGeometry();
    const mainLinePositions = new Float32Array([
      -50, 0, 0,  // Start point
      50, 0, 0    // End point
    ]);
    mainLineGeometry.setAttribute('position', new THREE.BufferAttribute(mainLinePositions, 3));
    const mainLine = new THREE.Line(mainLineGeometry, lineMaterial);
    mainLine.frustumCulled = false;
    scene.add(mainLine);
    
    // Fractal lines as a single LineSegments object
    const fractalMaterial = new THREE.LineBasicMaterial({ color: 0xffffff });
    const fractalGeometry = new THREE.BufferGeometry();
    const maxRanks = 15; // Parameter to control number of ranks
    
    // Calculate total number of lines based on maxRanks
    const totalLines = (Math.pow(3, maxRanks + 1) - 3) / 2;
    const fractalPositions = new Float32Array(totalLines * 2 * 3); // Each line has 2 points, each point has 3 coordinates
    fractalGeometry.setAttribute('position', new THREE.BufferAttribute(fractalPositions, 3));
    fractalGeometry.setDrawRange(0, 0); // Initially no fractal lines
    fractalGeometry.attributes.position.setUsage(THREE.DynamicDrawUsage); // Inform Three.js that we'll be updating this buffer frequently
    const fractalLineSegments = new THREE.LineSegments(fractalGeometry, fractalMaterial);
    fractalLineSegments.frustumCulled = false;
    scene.add(fractalLineSegments);
    
    /**
     * Function to regenerate the fractal lines based on the current main line and maxRanks.
     * This function uses an iterative, rank-by-rank approach to generate all lines up to maxRanks.
     */
    function regenerateFractal() {
      const positions = fractalGeometry.attributes.position.array;
      let offset = 0;
    
      const startX = mainLinePositions[0];
      const startY = mainLinePositions[1];
      const startZ = mainLinePositions[2];
      const endX = mainLinePositions[3];
      const endY = mainLinePositions[4];
      const endZ = mainLinePositions[5];
    
      // Initialize array for the current lines (starting with the main line)
      let currentLines = [{
        x1: startX,
        y1: startY,
        z1: startZ,
        x2: endX,
        y2: endY,
        z2: endZ
      }];
    
      // Iterate through each rank
      for (let rank = 1; rank <= maxRanks; rank++) {
        const nextLines = [];
    
        // Process each line in the current rank
        for (const line of currentLines) {
          const deltaX = line.x2 - line.x1;
          const deltaY = line.y2 - line.y1;
          const z = line.z1; // Assuming z is constant
    
          // Calculate intermediate points based on the given conditions
          const x0_2 = line.x1 + deltaX / 3;
          const y0_2 = line.y1 + deltaY / 2;
          const x1_2 = line.x2 - deltaX / 3;
          const y1_2 = line.y2 - deltaY / 2;
    
          // Line 1: from (x1, y1, z) to (x0_2, y0_2, z)
          positions[offset++] = line.x1;
          positions[offset++] = line.y1;
          positions[offset++] = z;
          positions[offset++] = x0_2;
          positions[offset++] = y0_2;
          positions[offset++] = z;
          nextLines.push({
            x1: line.x1,
            y1: line.y1,
            z1: z,
            x2: x0_2,
            y2: y0_2,
            z2: z
          });
    
          // Line 2: from (x2, y2, z) to (x1_2, y1_2, z)
          positions[offset++] = line.x2;
          positions[offset++] = line.y2;
          positions[offset++] = z;
          positions[offset++] = x1_2;
          positions[offset++] = y1_2;
          positions[offset++] = z;
          nextLines.push({
            x1: x1_2,
            y1: y1_2,
            z1: z,
            x2: line.x2,
            y2: line.y2,
            z2: z
          });
    
          // Line 3: from (x0_2, y0_2, z) to (x1_2, y1_2, z)
          positions[offset++] = x0_2;
          positions[offset++] = y0_2;
          positions[offset++] = z;
          positions[offset++] = x1_2;
          positions[offset++] = y1_2;
          positions[offset++] = z;
          nextLines.push({
            x1: x0_2,
            y1: y0_2,
            z1: z,
            x2: x1_2,
            y2: y1_2,
            z2: z
          });
        }
    
        // Prepare for the next rank
        currentLines = nextLines;
      }
    
      // Update the draw range based on the number of vertices generated
      fractalGeometry.setDrawRange(0, offset / 3);
    
      // Mark the attribute as needing an update
      fractalGeometry.attributes.position.needsUpdate = true;
    
      // Optional: Compute the bounding sphere for better rendering performance (can be skipped if unnecessary)
      fractalGeometry.computeBoundingSphere();
    
      // Debugging: Log the total number of lines generated
      console.log(`Fractal regenerated with maxRanks=${maxRanks}. Total Lines: ${offset / 6}`);
    }
    
    // Initial fractal generation
    regenerateFractal();
    
    /**
     * Function to update fractal lines when the main line changes.
     * This function cleans up any existing fractal lines and regenerates them based on the new main line.
     */
    function updateFractalLines() {
      // Since we're reusing the buffer, simply regenerate the fractal
      regenerateFractal();
    }
    
    // Set the distance for grabbing the line (in world units)
    const distanceForGrab = 2;
    
    // Set the raycaster's line precision
    raycaster.params.Line.threshold = distanceForGrab;
    
    // Predefine vectors to avoid excessive allocations
    const mouseWorld = new THREE.Vector3();
    const deltaWorld = new THREE.Vector3();
    const currentMouse = new THREE.Vector2();
    const previousMouseWorld = new THREE.Vector3();
    
    // Mouse down event
    function onMouseDown(event) {
      if (event.button === 2) { // Right-click for panning
        isPanning = true;
        previousMousePosition.set(event.clientX, event.clientY);
      } else if (event.button === 0) { // Left-click for dragging endpoints
        const rect = renderer.domElement.getBoundingClientRect();
        mouse.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
        mouse.y = - ((event.clientY - rect.top) / rect.height) * 2 + 1;
    
        raycaster.setFromCamera(mouse, camera);
    
        const intersects = raycaster.intersectObject(mainLine, true);
    
        if (intersects.length > 0) {
          const intersectPoint = intersects[0].point;
          const startX = mainLinePositions[0];
          const startY = mainLinePositions[1];
          const endX = mainLinePositions[3];
          const endY = mainLinePositions[4];
    
          const distanceToStart = intersectPoint.distanceTo(new THREE.Vector3(startX, startY, 0));
          const distanceToEnd = intersectPoint.distanceTo(new THREE.Vector3(endX, endY, 0));
    
          selectedEndpointIndex = (distanceToStart < distanceToEnd) ? 0 : 1;
    
          isDraggingEndpoint = true;
          previousMousePosition.set(event.clientX, event.clientY);
        }
      }
    }
    
    // Mouse move event
    function onMouseMove(event) {
      if (isPanning) {
        const deltaX = event.clientX - previousMousePosition.x;
        const deltaY = event.clientY - previousMousePosition.y;
    
        const zoomFactorX = camera.right - camera.left;
        const zoomFactorY = camera.top - camera.bottom;
    
        const panX = (deltaX / window.innerWidth) * zoomFactorX;
        const panY = (deltaY / window.innerHeight) * zoomFactorY;
    
        camera.left -= panX;
        camera.right -= panX;
        camera.top += panY;
        camera.bottom += panY;
        camera.updateProjectionMatrix();
    
        previousMousePosition.set(event.clientX, event.clientY);
      } else if (isDraggingEndpoint) {
        const rect = renderer.domElement.getBoundingClientRect();
        currentMouse.set(
          ((event.clientX - rect.left) / rect.width) * 2 - 1,
          - ((event.clientY - rect.top) / rect.height) * 2 + 1
        );
    
        // Convert screen coordinates to world coordinates
        const projector = new THREE.Vector3(currentMouse.x, currentMouse.y, 0).unproject(camera);
        const prevProjector = new THREE.Vector3(
          ((previousMousePosition.x - rect.left) / rect.width) * 2 - 1,
          - ((previousMousePosition.y - rect.top) / rect.height) * 2 + 1,
          0
        ).unproject(camera);
    
        deltaWorld.subVectors(projector, prevProjector);
    
        // Update the selected endpoint
        const index = selectedEndpointIndex * 3;
        mainLinePositions[index] += deltaWorld.x;
        mainLinePositions[index + 1] += deltaWorld.y;
    
        // Mark the position attribute as needing update
        mainLineGeometry.attributes.position.needsUpdate = true;
        mainLineGeometry.computeBoundingSphere();
    
        previousMousePosition.set(event.clientX, event.clientY);
    
        // Update fractal lines after changing the main line
        updateFractalLines();
      }
    }
    
    // Mouse up event
    function onMouseUp(event) {
      if (event.button === 2) { // Right-click
        isPanning = false;
      } else if (event.button === 0) { // Left-click
        isDraggingEndpoint = false;
      }
    }
    
    // Add event listeners for mouse actions
    window.addEventListener('mousedown', onMouseDown);
    window.addEventListener('mousemove', onMouseMove);
    window.addEventListener('mouseup', onMouseUp);
    
  2. Assistant

    Sure! I'll help you modify your Three.js script to generate a zig-zag fractal pattern based on your specifications. We'll focus on updating the regenerateFractal function to create the desired fractal pattern underneath the middle lines consisting of three lines each.

    Overview of Changes

    1. Fractal Generation Logic: We'll update the fractal generation logic to follow your instructions for creating a zig-zag pattern with three lines per subdivision.
    2. Point Calculation: Each line segment will be subdivided into three parts with specific y-offsets to create the zig-zag effect.
    3. Performance Optimization: We'll manage the number of ranks to ensure high performance, especially since fractal generation can quickly become computationally intensive.

    Updated HTML and JavaScript Code

    Below is the complete updated HTML code with the necessary modifications:

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>High-Performance Three.js Zig-Zag Fractal Pattern</title>
      <style>
        body { margin: 0; }
        canvas { display: block; }
      </style>
    </head>
    <body>
      <script type="module">
        import * as THREE from 'https://unpkg.com/three@0.150.1/build/three.module.js';
    
        // Scene setup
        const scene = new THREE.Scene();
        scene.background = new THREE.Color(0x000000);
    
        // Camera setup
        const aspectRatio = window.innerWidth / window.innerHeight;
        const cameraSize = 200;
        const camera = new THREE.OrthographicCamera(
          -cameraSize * aspectRatio,
          cameraSize * aspectRatio,
          cameraSize,
          -cameraSize,
          1,
          1000
        );
        camera.position.set(0, 0, 500);
    
        const renderer = new THREE.WebGLRenderer({ antialias: true });
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);
    
        // Zoom logic (based on mouse wheel and key modifiers)
        const zoomSpeed = 0.8;
    
        function onWheel(event) {
          event.preventDefault();
    
          const rect = renderer.domElement.getBoundingClientRect();
          const mouseX = ((event.clientX - rect.left) / rect.width) * 2 - 1;
          const mouseY = -((event.clientY - rect.top) / rect.height) * 2 + 1;
    
          const mouseWorld = new THREE.Vector3(mouseX, mouseY, 0).unproject(camera);
          const zoomFactor = 1 - event.deltaY * 0.001 * zoomSpeed;
    
          const ctrlPressed = event.ctrlKey;
          const shiftPressed = event.shiftKey;
    
          if (ctrlPressed) {
            camera.left = (camera.left - mouseWorld.x) * zoomFactor + mouseWorld.x;
            camera.right = (camera.right - mouseWorld.x) * zoomFactor + mouseWorld.x;
          } 
          if (shiftPressed) {
            camera.top = (camera.top - mouseWorld.y) * zoomFactor + mouseWorld.y;
            camera.bottom = (camera.bottom - mouseWorld.y) * zoomFactor + mouseWorld.y;
          }
          if (!ctrlPressed && !shiftPressed) {
            camera.left = (camera.left - mouseWorld.x) * zoomFactor + mouseWorld.x;
            camera.right = (camera.right - mouseWorld.x) * zoomFactor + mouseWorld.x;
            camera.top = (camera.top - mouseWorld.y) * zoomFactor + mouseWorld.y;
            camera.bottom = (camera.bottom - mouseWorld.y) * zoomFactor + mouseWorld.y;
          }
    
          camera.updateProjectionMatrix();
        }
    
        // Prevent default browser behavior for wheel scrolling with Control or Shift key
        window.addEventListener('wheel', onWheel, { passive: false });
    
        // Prevent default context menu on right-click
        window.addEventListener('contextmenu', (event) => {
          event.preventDefault();
        });
    
        // Render loop
        function animate() {
          requestAnimationFrame(animate);
          renderer.render(scene, camera);
        }
    
        // Handle window resize
        function onWindowResize() {
          const aspect = window.innerWidth / window.innerHeight;
          camera.left = -cameraSize * aspect;
          camera.right = cameraSize * aspect;
          camera.top = cameraSize;
          camera.bottom = -cameraSize;
          camera.updateProjectionMatrix();
          renderer.setSize(window.innerWidth, window.innerHeight);
          regenerateFractal();
        }
        window.addEventListener('resize', onWindowResize);
    
        // Initialize animation
        animate();
    
        // Panning functionality
        let isPanning = false;
        let isDraggingEndpoint = false;
        let selectedEndpointIndex = null;
        const previousMousePosition = new THREE.Vector2();
    
        // Raycaster for detecting line interaction
        const raycaster = new THREE.Raycaster();
        raycaster.params.Line.threshold = 2; // Increased threshold for easier selection
        const mouse = new THREE.Vector2();
    
        // Interactive main line
        const lineMaterial = new THREE.LineBasicMaterial({ color: 0xffffff });
        const mainLineGeometry = new THREE.BufferGeometry();
        const mainLinePositions = new Float32Array([
          -100, 0, 0,  // Start point
          100, 0, 0    // End point
        ]);
        mainLineGeometry.setAttribute('position', new THREE.BufferAttribute(mainLinePositions, 3));
        const mainLine = new THREE.Line(mainLineGeometry, lineMaterial);
        mainLine.frustumCulled = false;
        scene.add(mainLine);
    
        // Fractal lines as a single LineSegments object
        const fractalMaterial = new THREE.LineBasicMaterial({ color: 0xffffff });
        const fractalGeometry = new THREE.BufferGeometry();
        const fractalLines = []; // Store lines as pairs of points
    
        const maxRanks = 6; // Adjusted for performance, you can increase up to 10-12 based on performance
    
        fractalGeometry.setAttribute('position', new THREE.Float32BufferAttribute([], 3));
        const fractalLineSegments = new THREE.LineSegments(fractalGeometry, fractalMaterial);
        fractalLineSegments.frustumCulled = false;
        scene.add(fractalLineSegments);
    
        /**
         * Function to regenerate the fractal lines based on the current main line and maxRanks.
         * Uses an iterative approach to generate all lines up to maxRanks.
         */
        function regenerateFractal() {
          const positions = [];
          const startPoint = new THREE.Vector2(mainLinePositions[0], mainLinePositions[1]);
          const endPoint = new THREE.Vector2(mainLinePositions[3], mainLinePositions[4]);
    
          // Initialize with the main line as the first rank
          let currentLines = [{ start: startPoint.clone(), end: endPoint.clone() }];
    
          // Iterate through each rank
          for (let rank = 1; rank <= maxRanks; rank++) {
            const nextLines = [];
            for (const line of currentLines) {
              const subdivided = subdivideLine(line, rank);
              // Add the new lines to the positions array
              for (const subLine of subdivided) {
                positions.push(subLine.start.x, subLine.start.y, 0);
                positions.push(subLine.end.x, subLine.end.y, 0);
                nextLines.push(subLine);
              }
            }
            currentLines = nextLines;
          }
    
          // Update the BufferGeometry
          fractalGeometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));
          fractalGeometry.attributes.position.needsUpdate = true;
          fractalGeometry.computeBoundingSphere();
        }
    
        /**
         * Subdivide a line into a zig-zag pattern with three new lines based on the rank.
         * @param {Object} line - An object with start and end as THREE.Vector2
         * @param {number} rank - Current rank to determine zig-zag direction
         * @returns {Array} Array of three new line segments
         */
        function subdivideLine(line, rank) {
          const { start, end } = line;
          const delta = end.clone().sub(start);
          const oneThird = delta.clone().multiplyScalar(1 / 3);
          const twoThird = delta.clone().multiplyScalar(2 / 3);
    
          // First point: start
          const p0 = start.clone();
    
          // Second point: 1/3 along the line with y offset
          const p1 = start.clone().add(oneThird);
          const offsetDirection = (rank % 2 === 0) ? 1 : -1; // Alternate direction based on rank
          const yOffset1 = (delta.length() / 3) * 0.5; // Adjust the 0.5 factor for desired offset magnitude
          p1.y += yOffset1 * offsetDirection;
    
          // Third point: 2/3 along the line with double y offset
          const p2 = start.clone().add(twoThird);
          const yOffset2 = yOffset1 * 2;
          p2.y += yOffset2 * offsetDirection;
    
          // Fourth point: end
          const p3 = end.clone();
    
          // Create three new lines forming the zig-zag
          return [
            { start: p0, end: p1 },
            { start: p1, end: p2 },
            { start: p2, end: p3 }
          ];
        }
    
        // Initial fractal generation
        regenerateFractal();
    
        /**
         * Function to update fractal lines when the main line changes.
         * Regenerates the fractal based on the new main line.
         */
        function updateFractalLines() {
          regenerateFractal();
        }
    
        // Mouse event handlers
        function onMouseDown(event) {
          if (event.button === 2) { // Right-click for panning
            isPanning = true;
            previousMousePosition.set(event.clientX, event.clientY);
          } else if (event.button === 0) { // Left-click for dragging endpoints
            const rect = renderer.domElement.getBoundingClientRect();
            mouse.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
            mouse.y = - ((event.clientY - rect.top) / rect.height) * 2 + 1;
    
            raycaster.setFromCamera(mouse, camera);
    
            const intersects = raycaster.intersectObject(mainLine, true);
    
            if (intersects.length > 0) {
              const intersectPoint = intersects[0].point;
              const startX = mainLinePositions[0];
              const startY = mainLinePositions[1];
              const endX = mainLinePositions[3];
              const endY = mainLinePositions[4];
    
              const distanceToStart = intersectPoint.distanceTo(new THREE.Vector3(startX, startY, 0));
              const distanceToEnd = intersectPoint.distanceTo(new THREE.Vector3(endX, endY, 0));
    
              selectedEndpointIndex = (distanceToStart < distanceToEnd) ? 0 : 1;
    
              isDraggingEndpoint = true;
              previousMousePosition.set(event.clientX, event.clientY);
            }
          }
        }
    
        function onMouseMove(event) {
          if (isPanning) {
            const deltaX = event.clientX - previousMousePosition.x;
            const deltaY = event.clientY - previousMousePosition.y;
    
            const zoomFactorX = camera.right - camera.left;
            const zoomFactorY = camera.top - camera.bottom;
    
            const panX = (deltaX / window.innerWidth) * zoomFactorX;
            const panY = (deltaY / window.innerHeight) * zoomFactorY;
    
            camera.left -= panX;
            camera.right -= panX;
            camera.top += panY;
            camera.bottom += panY;
            camera.updateProjectionMatrix();
    
            previousMousePosition.set(event.clientX, event.clientY);
          } else if (isDraggingEndpoint) {
            const rect = renderer.domElement.getBoundingClientRect();
            const currentMouse = new THREE.Vector2(
              ((event.clientX - rect.left) / rect.width) * 2 - 1,
              - ((event.clientY - rect.top) / rect.height) * 2 + 1
            );
    
            // Convert screen coordinates to world coordinates
            const projector = new THREE.Vector3(currentMouse.x, currentMouse.y, 0).unproject(camera);
            const prevProjector = new THREE.Vector3(
              ((previousMousePosition.x - rect.left) / rect.width) * 2 - 1,
              - ((previousMousePosition.y - rect.top) / rect.height) * 2 + 1,
              0
            ).unproject(camera);
    
            const deltaWorld = projector.clone().sub(prevProjector);
    
            // Update the selected endpoint
            const index = selectedEndpointIndex * 3;
            mainLinePositions[index] += deltaWorld.x;
            mainLinePositions[index + 1] += deltaWorld.y;
    
            // Mark the position attribute as needing update
            mainLineGeometry.attributes.position.needsUpdate = true;
            mainLineGeometry.computeBoundingSphere();
    
            previousMousePosition.set(event.clientX, event.clientY);
    
            // Update fractal lines after changing the main line
            updateFractalLines();
          }
        }
    
        function onMouseUp(event) {
          if (event.button === 2) { // Right-click
            isPanning = false;
          } else if (event.button === 0) { // Left-click
            isDraggingEndpoint = false;
          }
        }
    
        // Add event listeners for mouse actions
        window.addEventListener('mousedown', onMouseDown);
        window.addEventListener('mousemove', onMouseMove);
        window.addEventListener('mouseup', onMouseUp);
    
      </script>
    </body>
    </html>
    

    Explanation of Key Modifications

    1. Fractal Generation (regenerateFractal Function):

      • Initialization: Starts with the main line defined by the current endpoints.
      • Subdividing Lines: For each rank, every existing line is subdivided into three new lines following the zig-zag pattern.
      • Point Calculation:
        • First Point (p0): Start of the original line.
        • Second Point (p1): Located at 1/3 of the original line's length with a y-offset. The direction of the offset (left or right) alternates based on the current rank to create the zig-zag effect.
        • Third Point (p2): Located at 2/3 of the original line's length with double the y-offset of the second point.
        • Fourth Point (p3): End of the original line.
      • Adding Subdivided Lines: Each original line is replaced by three new lines (p0 to p1, p1 to p2, and p2 to p3) forming the zig-zag pattern.
    2. Adjusting maxRanks:

      • Set to 6 for performance reasons. You can increase this value to make the fractal more detailed, but be cautious as higher ranks can significantly increase the number of line segments and impact performance.
    3. Interactive Line Manipulation:

      • The main line's endpoints can be dragged with the left mouse button, and the fractal regenerates accordingly to reflect the changes.
    4. Zoom and Pan Functionality:

      • Mouse wheel controls zooming, and right-click dragging enables panning across the scene.

    Customizing the Zig-Zag Pattern

    • Offset Magnitude: The yOffset1 determines how much the line deviates from the original path at the first subdivision point. You can adjust the multiplication factor (currently 0.5) to increase or decrease the zig-zag amplitude.

      const yOffset1 = (delta.length() / 3) * 0.5; // Adjust the 0.5 factor as needed
      
    • Offset Direction: The direction of the y-offset alternates with each rank to create the zig-zag effect.

      const offsetDirection = (rank % 2 === 0) ? 1 : -1; // Alternates between up and down
      

      You can modify the logic here if you prefer a different pattern for direction changes.

    Performance Considerations

    • BufferGeometry: Using BufferGeometry with Float32BufferAttribute ensures efficient memory usage and rendering performance.
    • Dynamic Updates: The necesUpdate flag ensures that Three.js knows when to update the buffer, optimizing rendering.
    • Rank Limitation: Keeping maxRanks at a reasonable number (e.g., 6) helps maintain performance. Increase it cautiously based on your system's capabilities.

    Final Result

    When you run the updated script, you'll see the main line with a series of zig-zag fractal patterns generated underneath it. You can interact with the main line by dragging its endpoints, and the fractal will update in real-time to reflect the changes. Additionally, you can zoom and pan around the scene for better visualization.

    Feel free to adjust the maxRanks, offset factors, and other parameters to fine-tune the appearance and performance of the fractal pattern to your liking!