gpt4 book ai didi

javascript - 三个 JS 像素化渲染目标?

转载 作者:行者123 更新时间:2023-11-30 16:05:45 28 4
gpt4 key购买 nike

我试图让一个基本的渲染目标工作,我渲染到 1 个场景,然后将它用作纹理来渲染四边形。我有一个基本的演示,但当我运行它时,结果全是像素化的,就好像它被渲染到一个小屏幕上,然后被拉伸(stretch)到四边形上。

这是我的代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">

<title></title>

<link rel="stylesheet" type="text/css" href="style.css">
<script src="three.min.js" type="text/javascript"></script>

<style>
body {
color: #ffffff;
font-family:Monospace;
font-size:13px;
text-align:center;
font-weight: bold;
background-color: #000000;
margin: 0px;
overflow: hidden;
}

#info {
position: absolute;
top: 0px; width: 100%;
padding: 5px;
}

a {
color: #ffffff;
}

</style>
</head>
<body>
<div id="container"></div>

<script id="vertex-shader-test" type="x-shader/x-vertex">
void main() {
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);
}
</script>

<script id="fragment-shader-test" type="x-shader/x-vertex">
void main() {
gl_FragColor = vec4(1,1,1,1);
}
</script>

<script id="vertex-shader-screen" type="x-shader/x-vertex">
varying vec2 vertexUV;

void main() {
vertexUV = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
</script>

<script id="fragment-shader-screen" type="x-shader/x-fragment">
precision highp float;
varying vec2 vertexUV;
uniform sampler2D rtTexture;

void main() {
gl_FragColor = texture2D(rtTexture, vertexUV);
}
</script>

<script>

var scene = null,
rtScene = null,
camera = null,
cameraRTT = null,
renderer = null,
plane = null;

var rtTexture;

init();
render();

function init()
{
container = document.getElementById( 'container' );

scene = new THREE.Scene();
rtScene = new THREE.Scene();

rtTexture = new THREE.WebGLRenderTarget(window.innerWidth, window.innerHeight, { minFilter: THREE.LinearFilter, magFilter: THREE.NearestFilter, format: THREE.RGBFormat });

camera = new THREE.PerspectiveCamera(20, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 1;

rtCamera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
rtCamera.position.z = 100;
rtCamera.position.y = 10;

var material2 = new THREE.ShaderMaterial({
vertexShader: document.getElementById('vertex-shader-test').textContent,
fragmentShader: document.getElementById('fragment-shader-test').textContent,
});

var geometry = new THREE.PlaneBufferGeometry(100, 100, 200, 200);

plane = new THREE.Mesh(geometry, material2);
plane.rotateX(-Math.PI / 2);
rtScene.add(plane);

var geometry = new THREE.PlaneBufferGeometry(1, 1);

var material = new THREE.ShaderMaterial({
uniforms: {
rtTexture: { type: "t", value: rtTexture }
},
vertexShader: document.getElementById('vertex-shader-screen').textContent,
fragmentShader: document.getElementById('fragment-shader-screen').textContent,
});

var mesh = new THREE.Mesh(geometry, material);

scene.add(mesh);

renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.autoClear = false;

container.appendChild( renderer.domElement );
}

function render() {
requestAnimationFrame(render);

camera.lookAt(new THREE.Vector3(0,0,0));
rtCamera.lookAt(new THREE.Vector3(0,0,0));

renderer.render(rtScene, rtCamera, rtTexture, true);
renderer.render(scene, camera);
}
</script>
</body>
</html>

有什么想法吗?

最佳答案

在渲染将渲染目标映射到它的平面时,您正在使用透视相机。这将导致渲染输出的大小根据视野而变化。

例如,如果您将摄像头的视野从 20 缩小到 10,摄像头将进一步放大,您的步进会更加明显(因为图像已进一步放大)。同样,增加视野会缩小并减少步进。

但是,对于本例中的内容,我会在输出渲染目标的主场景中使用正交投影。

关于javascript - 三个 JS 像素化渲染目标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37079835/

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