You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Three.js中Canvas纹理模糊但原Canvas清晰的问题求助

Three.js中Canvas纹理模糊但原Canvas清晰的问题求助

我现在碰到个棘手的问题:用Canvas作为纹理渲染Three.js平面的时候,平面上的Canvas纹理总是有点模糊,但单独显示的Canvas是清晰的,想请教各位大佬怎么解决这个问题?

Canvas纹理模糊对比图

以下是我用到的代码:

JavaScript代码

import * as THREE from "three";

const image = document.getElementById('origin-img')

/* --- draw 【canvas】 by image--- */
const canvas = document.getElementById('canvas')
canvas.width = image.width
canvas.height = image.height
const ctx = canvas.getContext('2d')
ctx.drawImage(image, 0, 0, image.width, image.height, 0, 0, image.width, image.height)

/* --- threejs scene --- */
const itemWidth = 300
const itemHeight = 300

const scene = new THREE.Scene();
const camera = new THREE.OrthographicCamera(itemWidth / -2, itemWidth / 2, itemHeight / 2, itemHeight / -2, 0, 1000);
camera.position.z = 100;

const renderer = new THREE.WebGLRenderer({
  canvas: document.getElementById('webgl')
});
renderer.setSize(itemWidth, itemHeight);

// create a plane which uses 【canvas】 as texture
const geometry = new THREE.PlaneGeometry(image.width, image.height, 64, 64);
const material = new THREE.MeshBasicMaterial({
  map: new THREE.CanvasTexture(
    canvas,
    THREE.Texture.DEFAULT_MAPPING,
    THREE.ClampToEdgeWrapping,
    THREE.ClampToEdgeWrapping,
    THREE.LinearFilter,
    THREE.LinearMipmapLinearFilter,
    THREE.RGBAFormat,
    THREE.UnsignedByteType,
    1
  )
});
const plane = new THREE.Mesh(geometry, material);
scene.add(plane);

renderer.render(scene, camera);

HTML代码

<div class="app" style="display: flex; justify-content: space-between; width: 1200px;">
  <div class="item-container">
    <div class="item">
      <img id='origin-img' src="./origin-texture.png">
    </div>
    <div class="caption">Origin Image</div>
  </div>
  <div class="item-container">
    <div class="item">
      <canvas id="canvas"></canvas>
    </div>
    <div class="caption">Draw canvas by image</div>
  </div>
  <div class="item-container">
    <div class="item">
      <canvas id="webgl"></canvas>
    </div>
    <div class="caption">In threejs, create a plane which uses canvas as a texture</div>
  </div>
</div>
<script type="module" src="./index.js"></script>

备注:内容来源于stack exchange,提问作者L_T

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 13:13:05