LoadFBX.vue 3.8 KB
Newer Older
X
xiesi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<template>
  <div id="fbxdiv" class="LoadFBX"></div>
</template>

<script>
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import { FBXLoader } from "three/examples/jsm/loaders/FBXLoader";

export default {
  name: "LoadFBX",
  props: {
    fbxfilepath: String,
  },
  data() {
    return {};
  },
  mounted() {
19 20 21
    setTimeout(() => {
      console.log(
        "mounted" + document.getElementById("fbxdiv") + this.fbxfilepath
X
xiesi 已提交
22 23
      );

24 25 26 27 28
      const container = document.getElementById("fbxdiv");
      let camera, scene, renderer;
      const clock = new THREE.Clock();
      let mixer;

X
xiesi 已提交
29 30 31 32
      const mw = document.documentElement.clientWidth * 0.7;
      const mh = document.documentElement.clientHeight;
      console.log(mw + "," + mh);

33
      const init = () => {
X
xiesi 已提交
34
        camera = new THREE.PerspectiveCamera(45, mw / mh, 1, 2000);
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
        camera.position.set(100, 200, 300);

        scene = new THREE.Scene();
        scene.background = new THREE.Color(0xa0a0a0);

        const hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444);
        hemiLight.position.set(0, 200, 0);
        scene.add(hemiLight);

        const dirLight = new THREE.DirectionalLight(0xffffff);
        dirLight.position.set(0, 200, 100);
        dirLight.castShadow = true;
        dirLight.shadow.camera.top = 180;
        dirLight.shadow.camera.bottom = -100;
        dirLight.shadow.camera.left = -120;
        dirLight.shadow.camera.right = 120;
        scene.add(dirLight);

        // ground
        const mesh = new THREE.Mesh(
          new THREE.PlaneGeometry(2000, 2000),
          new THREE.MeshPhongMaterial({ color: 0x999999, depthWrite: false })
        );
        mesh.rotation.x = -Math.PI / 2;
        mesh.receiveShadow = true;
        scene.add(mesh);

        const grid = new THREE.GridHelper(2000, 20, 0x000000, 0x000000);
        grid.material.opacity = 0.2;
        grid.material.transparent = true;
        scene.add(grid);

        // model
        const loader = new FBXLoader();
        loader.load(
          "http://localhost/myfbx.php?filepath=" + this.fbxfilepath,
          function (object) {
            mixer = new THREE.AnimationMixer(object);

            if (object.animations[0] != null) {
              const action = mixer.clipAction(object.animations[0]);
              action.play();
X
xiesi 已提交
77 78
            }

79 80 81 82 83 84 85 86 87 88
            object.traverse(function (child) {
              if (child.isMesh) {
                child.castShadow = true;
                child.receiveShadow = true;
              }
            });

            scene.add(object);
          }
        );
X
xiesi 已提交
89

90 91
        renderer = new THREE.WebGLRenderer({ antialias: true });
        renderer.setPixelRatio(window.devicePixelRatio);
X
xiesi 已提交
92
        renderer.setSize(mw, mh);
X
xiesi 已提交
93

94 95
        renderer.shadowMap.enabled = true;
        container.appendChild(renderer.domElement);
X
xiesi 已提交
96

97 98 99
        const controls = new OrbitControls(camera, renderer.domElement);
        controls.target.set(0, 100, 0);
        controls.update();
X
xiesi 已提交
100

101
        window.addEventListener("resize", onWindowResize);
X
xiesi 已提交
102

103 104 105 106
        // stats
        //stats = new Stats();
        //container.appendChild(stats.dom);
      };
X
xiesi 已提交
107

108 109 110
      const onWindowResize = () => {
        camera.aspect = container.clientWidth / container.clientHeight;
        camera.updateProjectionMatrix();
X
xiesi 已提交
111

112 113
        renderer.setSize(container.clientWidth, container.clientHeight);
      };
X
xiesi 已提交
114

115 116 117 118 119 120 121
      const animate = () => {
        requestAnimationFrame(animate);
        const delta = clock.getDelta();
        if (mixer) mixer.update(delta);
        renderer.render(scene, camera);
        //stats.update();
      };
X
xiesi 已提交
122

123 124 125
      init();
      animate();
    }, 1000);
X
xiesi 已提交
126
  },
谢斯 已提交
127 128 129 130 131
  methods: {
    hello() {
      console.log("hello");
    },
  },
X
xiesi 已提交
132 133 134 135 136 137 138 139
};
</script>

<style lang="scss" scoped>
#fbxdiv {
  width: 100%;
  height: 100%;
}
140 141 142 143 144

canvas {
  height: 100%;
  width: 100%;
}
X
xiesi 已提交
145
</style>