import React, { useRef } from "react";
import { Canvas, useFrame } from "@react-three/fiber";
import * as THREE from "three";
const RotatingTriangle = () => {
const groupRef = useRef();
// Rotate both triangle and boundary together
useFrame(() => {
if (groupRef.current) {
groupRef.current.rotation.y += 0.01;
groupRef.current.rotation.x += 0.005;
}
});
// Define Triangle Geometry
const triangleGeometry = new THREE.BufferGeometry();
const vertices = new Float32Array([
0, 1, 0, // Top vertex
-1, -1, 0, // Bottom left
1, -1, 0 // Bottom right
]);
triangleGeometry.setAttribute("position", new THREE.BufferAttribute(vertices, 3));
// Define Square Boundary Geometry
const boundaryGeometry = new THREE.BufferGeometry();
const boundaryVertices = new Float32Array([
-1, 1, 0, 1, 1, 0, // Top edge
1, 1, 0, 1, -1, 0, // Right edge
1, -1, 0, -1, -1, 0, // Bottom edge
-1, -1, 0, -1, 1, 0 // Left edge
]);
boundaryGeometry.setAttribute("position", new THREE.BufferAttribute(boundaryVertices, 3));
return (
<group ref={groupRef}>
{/* Triangle Mesh */}
<mesh geometry={triangleGeometry}>
<meshBasicMaterial color="red" side={THREE.DoubleSide} />
</mesh>
{/* Square Boundary */}
<lineSegments geometry={boundaryGeometry}>
<lineBasicMaterial color="blue" linewidth={2} />
</lineSegments>
</group>
);
};
export default function App() {
return (
<Canvas>
<ambientLight />
<RotatingTriangle />
</Canvas>
);
}