three.js 將圖片馬賽克化的示例代碼
這篇郭先生來說說BufferGeometry,類型化數(shù)組和粒子系統(tǒng)的使用,并且讓圖片有馬賽克效果(同理可以讓不清晰的圖片清晰化),如圖所示
1. 解析圖片
解析圖片和上一篇一樣
initCanvas() { canvas = document.createElement('canvas'); content = canvas.getContext('2d'); canvas.width = 1600; canvas.height = 1200; img = new Image(); img.crossOrigin = '*'; img.src = "/static/images/base/girl.jpg"; img.onload = () => { content.drawImage(img, 0, 0, canvas.width, canvas.height); imgDate = content.getImageData(0, 0, canvas.width, canvas.height); this.createPotCloud(); //創(chuàng)建點(diǎn)云 }; }
2. 操作像素點(diǎn)
createPotCloud() { if (points) { scene.remove(points) } let cw = Math.floor(canvas.width / size); let ch = Math.floor(canvas.height / size); particles = cw * ch; geometry = new THREE.BufferGeometry(); positions = new Float32Array(Math.floor(particles * 3)); positions_af = new Float32Array(Math.floor(particles * 3)); var colors = new Float32Array(Math.floor(particles * 3)); for (var i = 0; i < positions.length; i += 1) { positions[3 * i] = - canvas.width / 2 + (i % cw) * size; positions[3 * i + 1] = canvas.height / 2 + Math.floor((-1 - i) / cw) * size; positions[3 * i + 2] = 0; let selectPos = size * (i % cw) + Math.floor(i / cw) * cw * size * size; colors[3 * i] = imgDate.data[4 * selectPos] / 255.0; colors[3 * i + 1] = imgDate.data[4 * selectPos + 1] / 255.0; colors[3 * i + 2] = imgDate.data[4 * selectPos + 2] / 255.0; } geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3)); geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3)) geometry.dynamic = true; geometry.attributes.position.needsUpdate = true; var material = new THREE.PointsMaterial({ size: size, vertexColors: THREE.VertexColors }) points = new THREE.Points(geometry, material); points.name = 'points'; scene.add(points); loaded = true; },
可以不用糾結(jié)代碼,核心思想其實(shí)我們只需要每隔一定的顏色點(diǎn)取出一個(gè)顏色點(diǎn),然后將這個(gè)顏色賦予到geometry的color屬性上,就可以了,也可以每隔兩行、兩列取一個(gè)點(diǎn),但是圖片上面的點(diǎn)是一維的,所以需要一些數(shù)學(xué)方法,
如圖所示,原理很簡(jiǎn)單哦。將不清晰的圖片清晰化,就是需要插入更多的像素點(diǎn),插入的像素點(diǎn),需要根據(jù)已存在的像素點(diǎn)的顏色進(jìn)行插值,比如原圖的像素點(diǎn)1為0xffffff,像素點(diǎn)2的顏色為0xdddddd,則插在兩個(gè)像素點(diǎn)之間像素點(diǎn)的顏色為0xeeeeee,以此類推。
以上就是three.js 將圖片馬賽克化的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于three.js 將圖片馬賽克化的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- three.js如何實(shí)現(xiàn)3D動(dòng)態(tài)文字效果
- three.js 實(shí)現(xiàn)露珠滴落動(dòng)畫效果的示例代碼
- three.js中多線程的使用及性能測(cè)試詳解
- three.js顯示中文字體與tween應(yīng)用詳析
- 微信小游戲中three.js離屏畫布的示例代碼
- three.js 利用uv和ThreeBSP制作一個(gè)快遞柜功能
- three.js著色器材質(zhì)的內(nèi)置變量示例詳解
- vue頁面引入three.js實(shí)現(xiàn)3d動(dòng)畫場(chǎng)景操作
- three.js 制作動(dòng)態(tài)二維碼的示例代碼
- 如何用threejs實(shí)現(xiàn)實(shí)時(shí)多邊形折射
相關(guān)文章
JavaScript將相對(duì)地址轉(zhuǎn)換為絕對(duì)地址示例代碼
本文為大家詳細(xì)介紹下JavaScript怎么將相對(duì)地址轉(zhuǎn)換為絕對(duì)地址,具體的示例如下,感興趣的朋友可以參考下哈,希望對(duì)大家有所幫助2013-07-07JavaScript 短路運(yùn)算的實(shí)現(xiàn)
本文主要介紹了JavaScript 短路運(yùn)算的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06JavaScript生成驗(yàn)證碼并實(shí)現(xiàn)驗(yàn)證功能
這篇文章主要介紹了JavaScript生成驗(yàn)證碼并實(shí)現(xiàn)驗(yàn)證功能的相關(guān)資料,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09一文熟練掌握J(rèn)avaScript的switch用法
在JavaScript中switch語句是一種用于多條件分支的控制語句,下面這篇文章主要給大家介紹了關(guān)于如果通過一文熟練掌握J(rèn)avaScript的switch用法的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01JS實(shí)現(xiàn)運(yùn)動(dòng)緩沖效果的封裝函數(shù)示例
這篇文章主要介紹了JS實(shí)現(xiàn)運(yùn)動(dòng)緩沖效果的封裝函數(shù),涉及JavaScript時(shí)間函數(shù)與數(shù)值運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2018-02-02