gpt4 book ai didi

javascript - 通过 Three.js 中的 getImageData 像素颜色数据循环创建多个立方体

转载 作者:行者123 更新时间:2023-11-28 06:46:21 28 4
gpt4 key购买 nike

我使用一个简单的 3x3.png 文件来检索颜色/不透明度数据。 (9 个像素中的每一个都有不同的颜色/不透明度)。

.png 已加载,并且应该位于屏幕外的单独 Canvas 中,然后 9 个立方体应该以 3x3 的形式绘制。

我认为这确实接近工作,但显然有些问题。

<!DOCTYPE html>
<head>
<title>a png made into cubes per pixel</title>
<style>body {margin: 0px;background-color: #99F;overflow: hidden;}</style>
<script src="js/three.min.js"></script>
</head>
<body>

<!-- the 3x3 png image pre-loading -->
<img id='image1' src='3x3.png' onload="javascript:loaded();">
<canvas id='canvas'></canvas>


<script>
function loaded(){

var img = new Image();
img.src = document.getElementById("image1").src;
var ctx = document.getElementById("canvas").getContext("2d");
ctx.drawImage(img, 0, 0);

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 );
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
camera.position.z = 5;
var geometry = new THREE.BoxGeometry( 1, 1, 1 );

for (ix = 0; ix < img.width; ix++) {
for (iy = 0; iy < img.height; iy++) {
// get next pixel data
var data = ctx.getImageData( ix, iy, 1, 1 ).data;
var floats = data.slice( 0, 4 ).map( function( val ) { return val / 255; });
var computedcolor = THREE.Color( floats[0], floats[1], floats[2] );
var computedopacity = floats[4];

var material = new THREE.MeshBasicMaterial( { color: computedcolor, opacity: computedopacity, side: THREE.DoubleSide } );
var cube = new THREE.Mesh( geometry, material );
scene.add( cube );
cube.position.x = ix; //move the cube positions to rebuild the image using cubes instead of pixels
cube.position.y = iy;
}
}

var render = function () {
requestAnimationFrame( render );
camera.position.z = camera.position.z + .01;
renderer.render(scene, camera);
}
render();
}
</script>
</body>
</html>

最佳答案

var Computedopacity = floats[4] 应该改为 var Computedopacity = floats[3]

但是,我不知道这是否能解决问题。我没有尝试找出问题所在,而是从头开始编写了您想要执行的操作:

function boxelizeImage( image ) {

// 2D

var canvas = document.createElement( 'canvas' );
canvas.width = image.width;
canvas.height = image.height;

var context = canvas.getContext( '2d' );
context.drawImage( image, 0, 0 );

var data = context.getImageData( 0, 0, canvas.width, canvas.height ).data;

// 3D

var group = new THREE.Group();
var geometry = new THREE.BoxGeometry( 1, 1, 1 );

for ( var i = 0, j = 0; i < data.length; i += 4, j ++ ) {

var color = new THREE.Color();
color.fromArray( data, i );
color.multiplyScalar( 1 / 255 );

if ( color.getHex() === 0 ) continue;

var material = new THREE.MeshBasicMaterial( {
color: color,
wireframe: true,
} );
var mesh = new THREE.Mesh( geometry, material );
mesh.position.x = j % canvas.width;
mesh.position.y = - Math.floor( j / canvas.width );
group.add( mesh );

}

return group;

}

//

var camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 500 );
camera.position.x = 15;
camera.position.y = -10;
camera.position.z = 80;

var scene = new THREE.Scene();

var image = new Image();
image.addEventListener( 'load', function () {
var group = boxelizeImage( image );
scene.add( group );
renderer.render( scene, camera );
} );
image.src = '../img/logo.png';

var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

http://jsfiddle.net/44mj229c/

关于javascript - 通过 Three.js 中的 getImageData 像素颜色数据循环创建多个立方体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33385952/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com