threejs实战_canvans纹理

简介: threejs加载canvas纹理

canvans纹理

20200416095321903.gif

  • 在canvas画时钟
  • 将canvas传递给THREE.Texture纹理
  • 将纹理传递给THREE.MeshBasicMareial材质
  • 构造THREE.Mesh
    在3D纹理的基础利用js制作贴图赋予物体
    回顾3D纹理的流程可跳转上一篇博客

    代码重构

    js绘制时钟

    var canvas;
    function clock()
    {
         
         
     canvas = document.createElement('canvas');//创建的canvas传入threejs
     canvas.width=200;
     canvas.height=200;
     var ctx = canvas.getContext('2d');
     if(ctx){
         
         
         var timerId;
         var frameRate = 60;
         function canvObject(){
         
         
             this.x = 0;
             this.y = 0;
             this.rotation = 0;
             this.borderWidth = 2;
             this.borderColor = '#000000';
             this.fill = false;
             this.fillColor = '#ff0000';
             this.update = function(){
         
         
             if(!this.ctx)throw new Error('你没有指定ctx对象。');
             var ctx = this.ctx
             ctx.save();
             ctx.lineWidth = this.borderWidth;
             ctx.strokeStyle = this.borderColor;
             ctx.fillStyle = this.fillColor;
             ctx.translate(this.x, this.y);
             if(this.rotation)ctx.rotate(this.rotation * Math.PI/180);
             if(this.draw)this.draw(ctx);
             if(this.fill)ctx.fill();
             ctx.stroke();
             ctx.restore();
             }
         };
         function Line(){
         
         };
             Line.prototype = new canvObject();
             Line.prototype.fill = false;
             Line.prototype.start = [0,0];
             Line.prototype.end = [5,5];
             Line.prototype.draw = function(ctx){
         
         
             ctx.beginPath();
             ctx.moveTo.apply(ctx,this.start);
             ctx.lineTo.apply(ctx,this.end);
             ctx.closePath();
         };
    
         function Circle(){
         
         };
             Circle.prototype = new canvObject();
             Circle.prototype.draw = function(ctx){
         
         
             ctx.beginPath();
             ctx.arc(0, 0, this.radius, 0, 2 * Math.PI, true);
             ctx.closePath();
         };
    
         var circle = new Circle();
         circle.ctx = ctx;
         circle.x = 100;
         circle.y = 100;
         circle.radius = 90;
         circle.fill = true;
         circle.borderWidth = 6;
         circle.fillColor = '#ffffff';
    
         var hour = new Line();
         hour.ctx = ctx;
         hour.x = 100;
         hour.y = 100;
         hour.borderColor = "#000000";
         hour.borderWidth = 10;
         hour.rotation = 0;
         hour.start = [0,20];
         hour.end = [0,-50];
    
         var minute = new Line();
         minute.ctx = ctx;
         minute.x = 100;
         minute.y = 100;
         minute.borderColor = "#333333";
         minute.borderWidth = 7;
         minute.rotation = 0;
         minute.start = [0,20];
         minute.end = [0,-70];
    
         var seconds = new Line();
         seconds.ctx = ctx;
         seconds.x = 100;
         seconds.y = 100;
         seconds.borderColor = "#ff0000";
         seconds.borderWidth = 4;
         seconds.rotation = 0;
         seconds.start = [0,20];
         seconds.end = [0,-80];
    
         var center = new Circle();
         center.ctx = ctx;
         center.x = 100;
         center.y = 100;
         center.radius = 5;
         center.fill = true;
         center.borderColor = 'orange';
    
         for(var i=0,ls=[],cache;i<12;i++){
         
         
             cache = ls[i] = new Line();
             cache.ctx = ctx;
             cache.x = 100;
             cache.y = 100;
             cache.borderColor = "orange";
             cache.borderWidth = 2;
             cache.rotation = i * 30;
             cache.start = [0,-70];
             cache.end = [0,-80];
         }
    
         timerId = setInterval(function(){
         
         
             // 清除画布
             ctx.clearRect(0,0,200,200);
             // 填充背景色
             ctx.fillStyle = 'orange';
             ctx.fillRect(0,0,200,200);
             // 表盘
             circle.update();
             // 刻度
             for(var i=0;cache=ls[i++];)cache.update();
             // 时针
             hour.rotation = (new Date()).getHours() * 30;
             hour.update();
             // 分针
             minute.rotation = (new Date()).getMinutes() * 6;
             minute.update();
             // 秒针
             seconds.rotation = (new Date()).getSeconds() * 6;
             seconds.update();
             // 中心圆
             center.update();
         },(1000/frameRate)|0);
     }else{
         
         
     alert('您的浏览器不支持Canvas无法预览.');
     }
    }
    

threejs重构

 <style>
        div#canvas-frame {
   
   //渲染画布
            border: none;
            cursor: pointer;
            width: 1000px;
            height: 500px;
            background-color: #ffffff;//白色
            margin:0 auto;//css选择器
        }
    </style>
<script>
    var camera, scene, renderer;
    var mesh;
    var texture;//全局变量声明
    function start()//渲染函数入口
    {
   
   
        clock();
        init();
        animate();
    }

    function init() {
   
   
        //初始化
        width = document.getElementById('canvas-frame').clientWidth;
        height = document.getElementById('canvas-frame').clientHeight;
        renderer = new THREE.WebGLRenderer({
   
   
            antialias : true
        });
        renderer.setSize(width, height);
        document.getElementById('canvas-frame').appendChild(renderer.domElement);
        renderer.setClearColor(0xFFFFFF, 1.0);

        //透视相机
        camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
        camera.position.z = 400;
        scene = new THREE.Scene();


        //以下为加载Canvas纹理
        var geometry = new THREE.CubeGeometry(150,150,150);
        //一个box
        texture = new THREE.Texture(canvas);//canvans材质
        var material = new THREE.MeshBasicMaterial({
   
   map:texture});
        texture.needsUpdate = true;
        mesh =new THREE.Mesh(geometry,material);
        scene.add(mesh);


        window.addEventListener( 'resize', onWindowResize, false );
    }

    function onWindowResize() {
   
   
        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();
        renderer.setSize( window.innerWidth, window.innerHeight );

    }

    function animate() {
   
   //循环渲染
        texture.needsUpdate = true;
        mesh.rotation.y -= 0.01;//旋转
        mesh.rotation.x -= 0.01;
        requestAnimationFrame( animate );
        renderer.render( scene, camera );
       // renderer.setClearColor(0xFFFFFF, 1.0);
    }

</script>

20200416095413206.gif

ok,nice!

目录
相关文章
vite环境引入web worker方法
在 vite 环境中使用 web worker 时,如果遇到生产环境中 worker.js 文件的 MIME 类型被识别为 text/html,导致报错无法运行的情况时,可以参考以下两种方法,原理都是避免编译时产出单独的 worker.js 文件。方法一worker文件不需要包装,引入时后缀增加 ?worker&inline,使用时直接 new ImportedWorker();self.
1621 1
|
前端开发 JavaScript 物联网
JavaScript使用Modbus协议实现RTU设备连云
在阿里云物联网平台下发物模型属性设置数据,HaaS600Kit 接收并解析云端数据后控制 Modbus 继电器设备进行开关动作。
JavaScript使用Modbus协议实现RTU设备连云
|
11月前
|
前端开发 定位技术
Pixi绘制地图和小车
这篇文章讲解了如何使用Pixi.js来绘制地图并在地图上显示小车,包括地图网格的创建和小车图像的定位与移动。
268 1
Pixi绘制地图和小车
|
11月前
|
监控
ThreeJs限制模型拖动的范围
这篇文章讲解了在Three.js中如何限制模型拖动的范围,确保模型在特定边界内移动,提供了实现拖动限制的代码示例和技术细节。
341 1
ThreeJs限制模型拖动的范围
|
9月前
|
人工智能 数据可视化 程序员
2024年值得推荐的4款免费且功能强大的在线文档工具
2024年值得推荐的4款免费且功能强大的在线文档工具
504 4
2024年值得推荐的4款免费且功能强大的在线文档工具
|
移动开发 前端开发 数据处理
探索前端性能优化的新思路:使用Web Workers提升网页响应速度
传统的前端性能优化方法已经不能完全满足日益增长的网页需求。本文提出了一种新的思路,即利用Web Workers技术来提升网页的响应速度。通过将耗时的计算任务交给Web Workers处理,可以避免主线程阻塞,从而提高网页的用户体验。本文将介绍Web Workers的基本原理、使用方法以及在前端性能优化中的应用实例,帮助开发者更好地理解和运用这一技术。
|
存储 移动开发 安全
Flutter加固原理及加密处理
Flutter加固原理及加密处理
297 0
文本,vitepress如何插入图片,做背景图片的设计,Typora如何设置存放图片的位置
文本,vitepress如何插入图片,做背景图片的设计,Typora如何设置存放图片的位置
|
存储 中间件 Go
探索Gin框架:快速构建高性能的Golang Web应用
探索Gin框架:快速构建高性能的Golang Web应用