/*
 * @Author: wuhao930406 1148547900@qq.com
 * @Date: 2023-08-03 11:31:00
 * @LastEditors: wuhao930406 1148547900@qq.com
 * @LastEditTime: 2023-08-24 14:27:24
 * @FilePath: /editor-plus/src/pages/components/GlftBlock/index.jsx
 * @Description:
 *
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
 */
import { Gltf, OrbitControls, Stage } from "@react-three/drei";
import { Canvas } from "@react-three/fiber";
import { useRef } from "react";
import "./index.less";

function Sphere({ position, CoordinatePoint, CurGongjianPoint }) {
  const meshRef = useRef();

  return (
    <mesh
      ref={meshRef}
      scale={100}
      position={
        position
          ? Object.values(position)?.map((it, i) => {
              const offset = Object.values(CoordinatePoint);
              const offset1 = Object.values(CurGongjianPoint);
              let res =
                parseFloat(it * 10000000) +
                parseFloat(offset[i] * 10000000000) -
                parseFloat(offset1[i] * 10000000000);
              res = res / 10000000000;
              const diyoffset = i === 0 ? 0.021 : i === 1 ? 0.012 : 0.005;
              return (res + diyoffset) * 100;
            })
          : []
      }
    >
      <sphereGeometry attach="geometry" args={[0.006, 32, 32]} />
      <meshPhysicalMaterial attach="material" color="red" />
    </mesh>
  );
}

function PointViewer({ position, CoordinatePoint, CurGongjianPoint }) {
  return (
    <div className="bar-block-component">
      {CurGongjianPoint && (
        <Canvas
          shadows
          dpr={[1, 1.5]}
          gl={{ antialias: false }}
          camera={{ position: [8, 8, 16], fov: 75 }}
          style={{ height: "100%", width: "100%" }}
          id={"sada"}
        >
          <color attach="background" args={["white"]} />
          <Stage
            intensity={0.5}
            preset="rembrandt"
            shadows={"contact"}
            adjustCamera={1}
            environment={{
              path: "/",
              files: "env.hdr",
            }}
          >
            <axesHelper args={[20]}></axesHelper>
            <Gltf
              src={"./model.glb"}
              receiveShadow
              castShadow
              position={[0, 0, 0]}
              scale={240}
              inject={
                <meshPhysicalMaterial
                  color="white"
                  metalness={1} // 控制金属感，1 表示完全金属
                  roughness={0} // 控制光滑度，0 表示非常光滑
                />
              }
            />
            <Sphere
              position={position}
              CoordinatePoint={CoordinatePoint}
              CurGongjianPoint={CurGongjianPoint}
            ></Sphere>
          </Stage>
          <OrbitControls></OrbitControls>
        </Canvas>
      )}
    </div>
  );
}

export default PointViewer;
