vue 项目中引用three.js demo作为登录页的背景遇到的坑

先上效果图
在这里插入图片描述
其实就是觉得 iview 2.0 的官网的首页这个动画挺好看的, 移到自己的项目上
官网demo地址
接下来是遇到的坑

1

打开源码可以看到3个大大的script标签

		<script type="x-shader/x-vertex" id="vertexshader">
			attribute float scale;
			void main() {
				vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
				gl_PointSize = scale * ( 300.0 / - mvPosition.z );
				gl_Position = projectionMatrix * mvPosition;
			}
		</script>
		
		<script type="x-shader/x-fragment" id="fragmentshader">
			uniform vec3 color;
			void main() {
				if ( length( gl_PointCoord - vec2( 0.5, 0.5 ) ) > 0.475 ) discard;
				gl_FragColor = vec4( color, 1.0 );
			}
		</script>

这两个script标签我一开始是想在vue 中的动态生成–> 像这样

const script = document.creatElement('script')
script.type = 'x-shader/x-vertex'
script.id = 'fragmentshader'
script.textContent = `
void main() {
				vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
				gl_PointSize = scale * ( 300.0 / - mvPosition.z );
				gl_Position = projectionMatrix * mvPosition;
			}`
body.document.appendChild('script')

但是!!!
这样动态插入的结果就是在

vertexShader: document.getElementById( 'vertexshader' ).textContent,

document.getElementById( ‘vertexshader’ ) = null
自然而然就会报错了

解决办法
那就只能放在vue 项目中的index.html中了,

<!DOCTYPE html>
<html style="font-size: 50px">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <script type="text/javascript" src="./static/qiniu1.x/qiniu.min.js"></script>
    <link rel="shortcut icon" href="./static/favicon.ico"/>
    <!--<script src="http://webapi.amap.com/maps?v=1.4.8&key=1811521a87ef706155ff7cb020619c81"></script>-->
    <title>花忆-平台管理</title>
  </head>
  <body>
    <div id="app"></div>
    <!-- built files will be auto injected -->
    <!--<script src="/static/tx_webim_1.7/webim.js"></script>-->
    <!--读取图片信息-->
    <script type="text/javascript" src="./static/exifJs/exif.js"></script>

    <!--登录页动画相关-->
    <script type="x-shader/x-vertex" id="vertexshader">
    attribute float scale;
    void main() {
      vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
      gl_PointSize = scale * ( 300.0 / - mvPosition.z );
      gl_Position = projectionMatrix * mvPosition;
    }
    </script>
    <!--登录页动画相关-->
    <script type="x-shader/x-fragment" id="fragmentshader">
    			uniform vec3 color;
    			void main() {
    				if ( length( gl_PointCoord - vec2( 0.5, 0.5 ) ) > 0.475 ) discard;
    				gl_FragColor = vec4( color, 1.0 );
    			}
    </script>
  </body>
</html>

首先当然是 npm i three -S
然后就是在组件中引用啦
但是
import THREE from 'three'这样引用是不可以的,
需要这样引用import * as THREE from 'three'

是我自己犯彪的一个错误导致的
我是把官网的第三个script标签直接Ctrl + C Ctrl + v 到自己的vue 组件中

然后编译的时候就会在控制台报这个错误

 WARNING  Compiled with 1 warnings                                                                                                                                               12:26:38

 warning  in ./src/components/common/loginAnimate.vue

21:2-16 "export 'default' (imported as '__vue_script__') was not found in '!!babel-loader!../../../node_modules/vue-loader/lib/selector?type=script&index=0!./loginAnimate.vue'

我TM 这是啥错误, CSDN上有个兄弟说是因为引入了两个script标签导致的, 我眼睛都看瞎了也没有找到还有另外一个script 标签
百度全网也没有找到问题的解决办法
只能搬出GOOGLE
这个问题下边找到了这样一句话
We couldn’t see the whole project you mentioned. But I guess this error means you don’t export anything in script.
简而言之就是我他吗的在script 中没有export default (因为script 标签是我复制过来的)

export default {
    name: 'loginAnimate'
  }

加上这个 OK FINE

在这里插入图片描述
我是智障

在这里插入图片描述
这一条白边看到没, 因为这5px 导致右边出现滚动条, 难受啊难受╮(╯﹏╰)╭
参考了另一个兄弟用div包图片导致div下边缘多出几px的边距
在样式中添加

#loginAnimate canvas{
    /*设置display 为 block 可以消除下方5px 的边距!!!*/
    display: block!important;
    width: 100%!important;
    height: 100% !important;
  }

dispaly: block就可以了

修改点点的颜色

var material = new THREE.ShaderMaterial({
      uniforms: {
        color: {value: new THREE.Color(0x097bdb)},
      },
      vertexShader: document.getElementById('vertexshader').textContent,
      fragmentShader: document.getElementById('fragmentshader').textContent
    });

是在这里修改
背景颜色我暂时还没弄,有点急做其他的事情.

修改鼠标位置和角度的我是参考了
iview 首页的底部的canvas粒子动画是怎么实现的?有这种框架么?或者有什么思路求指导

在这里插入图片描述
登录后要移除动画啦
刚开始是想着用isLogin来判断
如果isLogin == true 的时候就 把动画的display = none
可是发现isLogin 数据更新不及时 需要刷新页面 也就是this.$router.go(0)
但是这样效果很不好,页面会卡顿

解决:
脑袋秀逗了, 直接在登录成功的回调函数中移除这个元素就好了

let loginAnimate = document.getElementById('loginAnimate')
 // loginAnimate.parentNode.removeChild(loginAnimate)
loginAnimate.remove()

两种方法都可以的

总结
以上就是新人在vue 中简单使用three.js 遇到的坑, 如有错误之处,请大家指出.

另外分享一个网站 http://i-remember.fr/en/ 挺有趣, 不忙的时候可以去看看

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 你可以在Vue3使用Three.js,首先需要安装Three.js库,然后在Vue组件引入Three.js库,可以使用import语句引入。接着,你可以在Vue组件使用Three.js的API来创建3D场景、模型等。需要注意的是,在Vue3使用Three.js时,需要使用Vue的生命周期函数来管理Three.js的渲染和更新。具体实现方法可以参考Three.js官方文档和Vue3官方文档。 ### 回答2: 在Vue3使用Three.js可以通过以下步骤实现: 1. 在你的Vue项目安装Three.js库。可以使用npm或者yarn来安装,命令如下: ``` npm install three ``` 或 ``` yarn add three ``` 2. 在Vue组件引入Three.js库。在需要使用Three.js的组件文件,添加以下代码: ```js import * as THREE from 'three'; ``` 这样,你就可以在该组件使用Three.js提供的所有功能和类。 3. 创建Three.js场景和渲染器。在Vue组件的`mounted`钩子函数,创建一个空的Three.js场景,并将其渲染到HTML面上的某个容器,代码如下: ```js const scene = new THREE.Scene(); const renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setSize(window.innerWidth, window.innerHeight); document.getElementById('container').appendChild(renderer.domElement); ``` 4. 添加和渲染Three.js的物体。在场景创建并添加需要渲染的物体,如立方体等,然后通过调用渲染器的`render`函数将场景渲染到面上,代码如下: ```js const geometry = new THREE.BoxGeometry(); const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }); const cube = new THREE.Mesh(geometry, material); scene.add(cube); renderer.render(scene, camera); // 其camera为相机对象,需要提前创建并设置 ``` 5. 在Vue组件使用动画循环更新场景。你可以使用Vue的`watch`侦听器或者使用Vue提供的动画插件,来持续更新Three.js场景的状态,并在每一帧重新渲染场景,以实现动态效果。 以上就是在Vue3使用Three.js的基本步骤。通过这些步骤,你可以在Vue项目使用Three.js创建出交互式和动态的3D图形。 ### 回答3: 在Vue3使用Three.js的步骤如下: 1. 安装Three.js:可以通过npm或者yarn命令行安装Three.js库。在项目根目录打开终端,执行以下命令: ``` npm install three ``` 或者 ``` yarn add three ``` 2. 创建Vue组件:在Vue项目创建一个新的Vue组件,用来承载Three.js场景和渲染器。可以在方法或者生命周期函数编写Three.js逻辑。 3. 引入Three.js:在Vue组件使用import语句引入Three.js库: ```javascript import * as THREE from 'three' ``` 4. 创建场景、渲染器和相机:在Vue组件的mounted生命周期函数创建Three.js所需的场景、渲染器和相机: ```javascript mounted () { const scene = new THREE.Scene() const renderer = new THREE.WebGLRenderer() const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000) renderer.setSize(window.innerWidth, window.innerHeight) this.$refs.container.appendChild(renderer.domElement) } ``` 5. 添加物体和光源:可以在Vue组件的mounted生命周期函数添加物体和光源到场景: ```javascript const geometry = new THREE.BoxGeometry() const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }) const cube = new THREE.Mesh(geometry, material) scene.add(cube) const light = new THREE.PointLight(0xffffff) light.position.set(0, 0, 10) scene.add(light) ``` 6. 渲染场景:在Vue组件的mounted生命周期函数使用requestAnimationFrame函数循环渲染场景: ```javascript function animate () { requestAnimationFrame(animate) renderer.render(scene, camera) } animate() ``` 7. 更新和交互:可以在Vue组件监听鼠标和键盘事件,并根据需要更新Three.js场景。 这些是在Vue3使用Three.js的基本步骤。根据具体需求,还可以使用其他Three.js功能,如纹理贴图、动画等等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值