vue+three.js贴图-自定义贴图.仿写VANS定制功能(three.js+vue)

Three.js仿写

功能源地址: Vans定制功能页面,建议先查看并使用一下vans网站功能。 使用 Three.js+vue 实现 demo演示如下:

思路讲解: 引入模型-->找到需要改变的模型面-->创建新的纹理赋给新材质-->将旧的材质替换掉 纹理大小绑定动态数据值-->用户调整数值-->将更新的值转换后赋值给纹理-->重新替换纹理+材质 代码贴在了下面

模型背后光晕效果在代码中有体现出来。CSS代码也贴出来了

源码展示:html部分

javascript 复制代码
  <div class="main">
    <div class="modelConfig">
      <div class="block">
        <span class="demonstration">贴图大小</span>
        <el-slider v-model="Texturesize"></el-slider>
      </div>
      <div class="block">
        <span class="demonstration">贴图距离右边位置</span>
        <el-slider v-model="left"></el-slider>
      </div>
      <div class="block">
        <span class="demonstration">贴图距离下边位置</span>
        <el-slider v-model="top"></el-slider>
      </div>
    </div>
    <div id="container"></div>
  </div>

源码展示: JS部分 欢迎各位提意见交流

javascript 复制代码
import * as THREE from "three";
import { GLTFLoader } from "@/common/GLTFLoader.js";
import { OrbitControls } from "@/common/OrbitControls.js";

export default {
  data() {
    return {
      controls: null,
      container: null,
      scene: null,
      renderer: null,
      value: 1,
      loader: null,
      Texturesize: 1,
      left: 50,
      top: 50,
      timer: null,
    };
  },
  mounted() {
    this.init();
  },
  watch: {
    Texturesize() {
      clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        let three = document.querySelector("#container > canvas");
        three.remove(1);
        this.init();
      }, 1000);
    },
    left() {
      clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        let three = document.querySelector("#container > canvas");
        three.remove(1);
        this.init();
      }, 1000);
    },
    top() {
      clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        let three = document.querySelector("#container > canvas");
        three.remove(1);
        this.init();
      }, 1000);
    },
  },
  methods: {
    init() {
      this.container = document.querySelector("#container");
      this.camera = new THREE.PerspectiveCamera(45, 1920 / 685, 0.1, 2000);
      this.camera.position.set(0.5, 0.7, 6);
      this.scene = new THREE.Scene();
      var texture = new THREE.TextureLoader().load("texture/433.png");
      texture.generateMipmaps = false;
      texture.anisotropy = true;
      texture.encoding = THREE.sRGBEncoding;
      texture.flipY = false;
      texture.matrix.set(0, 0, 0, 0, 0, 0, 0);
      texture.needsUpdate = true;
      texture.center.set(this.left / 100, this.top / 100);
      texture.repeat.set(
        0.01 /
          (this.Texturesize === 1 ? this.Texturesize : this.Texturesize / 10),
        0.009 /
          (this.Texturesize === 1 ? this.Texturesize : this.Texturesize / 10)
      );
      var tmaterial = new THREE.MeshStandardMaterial({
        map: texture,
        polygonOffset: false,
      });
      new GLTFLoader().setPath("loader/").load("329.glb", (gltf) => {
        console.log(gltf);
        gltf.scene.children[0].children[8].children.forEach((item, index) => {
          if (index == 21) {
            item.material = tmaterial;
          }
        });

        gltf.scene.traverse(function (child) {
          if (child.isMesh) {
            child.material.emissive = child.material.color;
            child.material.emissiveMap = child.material.map;
          }
        });

        this.scene.add(gltf.scene);
      });

      // 此处配置设置背景透明然后css3设置光晕
      this.renderer = new THREE.WebGLRenderer({
        alpha: true,
        antialias: true,
        precision: "highp",
      });
      this.renderer.setClearAlpha(0.0);
      this.renderer.setPixelRatio(window.devicePixelRatio);
      this.renderer.setSize(window.innerWidth, window.innerHeight);
      this.renderer.outputEncoding = THREE.sRGBEncoding;
      this.container.appendChild(this.renderer.domElement);

      this.controls = new OrbitControls(this.camera, this.renderer.domElement);
      this.controls.update();
      // 右键拖拽
      this.controls.enablePan = true;
      this.controls.enableDamping = true;
      window.addEventListener("resize", this.resize);
      this.animate();
    },
    resize() {
      this.camera.aspect = window.innerWidth / window.innerHeight;
      this.camera.updateProjectionMatrix();
      this.renderer.setSize(window.innerWidth, window.innerHeight);
    },
    animate() {
      this.controls.update();
      this.renderer.render(this.scene, this.camera);
      requestAnimationFrame(this.animate);
    },
  },
};

源码展示: CSS代码

css 复制代码
.modelConfig {
  position: absolute;
  left: 20px;
}
.demonstration {
  color: #fff;
}
.block {
  width: 300px;
}
/* 背景光晕 */
#container {
  background: radial-gradient(
    circle at 50%,
    rgb(255, 255, 255),
    rgba(214, 214, 214),
    rgb(97, 97, 97)
  );
}

想要GLB模型的同学可以私信博主(如果有学到干货请动动小手点点赞谢谢)

相关推荐
叶智辽6 小时前
【Three.js与WebGPU】下一代3D技术到底强在哪?
webgl·three.js
叶智辽11 小时前
【Three.js后期处理】如何让你的场景拥有电影级调色
前端·three.js
叶智辽1 天前
【Three.js多相机渲染】如何在同一场景里实现“画中画”效果
three.js·canvas
答案answer2 天前
一个非常实用的Three.js3D模型爆破💥和切割开源插件
前端·github·three.js
叶智辽2 天前
【Three.js内存管理】那些你以为释放了,其实还在占着的资源
性能优化·three.js
烛阴3 天前
Three.js 零基础入门:手把手打造交互式 3D 几何体展示系统
javascript·webgl·three.js
叶智辽4 天前
【ThreeJS调试技巧】那些让 Bug 无所遁形的“脏套路”
webgl·three.js
叶智辽5 天前
【ThreeJS急诊室】一个生产事故:我把客户的工厂渲染“透明”了
webgl·three.js
爱看书的小沐11 天前
【小沐杂货铺】基于Three.js渲染三维无人机Drone(WebGL / vue / react )
javascript·vue.js·react.js·无人机·webgl·three.js·drone
叶智辽18 天前
【ThreeJS实战】5个让我帧率翻倍的小技巧,不用改模型
性能优化·three.js