2 Star 46 Fork 11

three.js/three-weixin-demo

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
webgl_materials_texture_partialupdate.js 3.68 KB
一键复制 编辑 原始数据 按行查看 历史
zhangjin 提交于 2024-06-25 18:11 +08:00 . .
import * as THREE from 'three';
const {
performance,
document,
window,
HTMLCanvasElement,
requestAnimationFrame,
cancelAnimationFrame,
core,
Event,
Event0
} = THREE .DHTML
var requestId
Page({
onUnload() {
cancelAnimationFrame(requestId, this.canvas)
this.worker && this.worker.terminate()
if(this.canvas) this.canvas = null
setTimeout(() => {
if (this.renderer instanceof THREE.WebGLRenderer) {
this.renderer.dispose()
this.renderer.forceContextLoss()
this.renderer.context = null
this.renderer.domElement = null
this.renderer = null
}
}, 10)
},
webgl_touch(e){
const web_e = (window.platform=="devtools"?Event:Event0).fix(e)
this.canvas.dispatchEvent(web_e)
},
onLoad() {
document.createElementAsync("canvas", "webgl2",this).then(canvas => {
this.canvas = canvas
this.body_load(canvas).then()
})
},
async body_load(canvas3d) {
let camera, scene, renderer, clock, dataTexture, diffuseMap;
let last = 0;
const position = new THREE.Vector2();
const color = new THREE.Color();
init();
function init() {
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
camera.position.z = 2;
scene = new THREE.Scene();
clock = new THREE.Clock();
const loader = new THREE.TextureLoader();
diffuseMap = loader.load( 'textures/floors/FloorsCheckerboard_S_Diffuse.jpg', animate );
diffuseMap.colorSpace = THREE.SRGBColorSpace;
diffuseMap.minFilter = THREE.LinearFilter;
diffuseMap.generateMipmaps = false;
const geometry = new THREE.PlaneGeometry( 2, 2 );
const material = new THREE.MeshBasicMaterial( { map: diffuseMap } );
const mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
//
const width = 32;
const height = 32;
const data = new Uint8Array( width * height * 4 );
dataTexture = new THREE.DataTexture( data, width, height );
//
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
//
window.addEventListener( 'resize', onWindowResize );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestId = requestAnimationFrame( animate );
const elapsedTime = clock.getElapsedTime();
if ( elapsedTime - last > 0.1 ) {
last = elapsedTime;
position.x = ( 32 * THREE.MathUtils.randInt( 1, 16 ) ) - 32;
position.y = ( 32 * THREE.MathUtils.randInt( 1, 16 ) ) - 32;
// generate new color data
updateDataTexture( dataTexture );
// perform copy from src to dest texture to a random position
renderer.copyTextureToTexture( position, dataTexture, diffuseMap );
}
renderer.render( scene, camera );
}
function updateDataTexture( texture ) {
const size = texture.image.width * texture.image.height;
const data = texture.image.data;
// generate a random color and update texture data
color.setHex( Math.random() * 0xffffff );
const r = Math.floor( color.r * 255 );
const g = Math.floor( color.g * 255 );
const b = Math.floor( color.b * 255 );
for ( let i = 0; i < size; i ++ ) {
const stride = i * 4;
data[ stride ] = r;
data[ stride + 1 ] = g;
data[ stride + 2 ] = b;
data[ stride + 3 ] = 1;
}
}
}
})
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/threejs/three-weixin-demo.git
git@gitee.com:threejs/three-weixin-demo.git
threejs
three-weixin-demo
three-weixin-demo
master

搜索帮助