【发布时间】:2020-04-19 14:32:29
【问题描述】:
我正在尝试显示一个立方体,其一侧显示我在其上绘制一些文本的图像。我尝试了几种方法,但图像和文本总是最终呈现为黑色。相反,如果我只是使用 ThreeJS 文本加载器之类的东西直接加载图像,我可以很好地看到图像(当然上面没有文本)。我的代码出了什么问题,以至于我在初始化期间看不到所需的图像以及我在上面打印的文本?
var scene, camera, renderer;
var WIDTH = window.innerWidth;
var HEIGHT = window.innerHeight;
var SPEED = 0.01;
var cube = null;
var imgBackSide = null;
function init() {
// Create a document element to house the back side image for cards.
imgBackSide = document.createElement('img');
imgBackSide.src = '/images/cards/card-back-side-400x400.png';
scene = new THREE.Scene();
initCamera();
initRenderer();
initCube();
document.body.appendChild(renderer.domElement);
}
function initCamera() {
camera = new THREE.PerspectiveCamera(70, WIDTH / HEIGHT, 1, 10);
camera.position.set(0, 3.5, 5);
camera.lookAt(scene.position);
}
function initRenderer() {
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(WIDTH, HEIGHT);
}
function canvasDrawText(ctx, canvas, text, x, y) {
// if x isn't provided
if ( x === undefined || x === null ){
let textSize = ctx.measureText(text);
x = (canvas.width - textSize.width) / 2;
}
// if y isn't provided
if ( y === undefined || y === null ){
let textSize = ctx.measureText(text);
y = (canvas.height - textSize.height) / 2;
}
// actually draw the text
// ctx.fillStyle = fillStyle;
ctx.fillText(text, x, y);
}
function newCardBacksideTexture(cardBackSideText) {
// Create an IMG element to hold the card back side image and load it.
let canvas = document.createElement('canvas');
let ctx = canvas.getContext('2d');
canvas.width = 400;
canvas.height = 400;
ctx.drawImage(imgBackSide, 0, 0);
// Draw the card label on top of the background.
ctx.font = 'bolder 90px Verdana';
canvasDrawText(ctx, canvas, cardBackSideText);
// dynamicTexture.texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return new THREE.CanvasTexture(canvas);
}
function initCube() {
let cubeGeometry = new THREE.BoxGeometry(2, 0.1, 2);
let loader = new THREE.TextureLoader();
let dummy = loader.load("/images/cards/white-square-400x400.png");
let materialArray = [
new THREE.MeshBasicMaterial( { map: loader.load('/images/cards/white-square-400x400.png') } ),
new THREE.MeshBasicMaterial( { map: loader.load('/images/cards/white-square-400x400.png') } ),
// Card face.
new THREE.MeshBasicMaterial( { map: loader.load('/images/card-face.jpg') } ),
// Card back side.
new THREE.MeshBasicMaterial( { map: newCardBacksideTexture('BACK SIDE OF CARD') } ),
//
new THREE.MeshBasicMaterial( { map: loader.load('/images/cards/white-square-400x400.png') } ),
new THREE.MeshBasicMaterial( { map: loader.load('/images/cards/white-square-400x400.png') } ),
];
cube = new THREE.Mesh( cubeGeometry, materialArray );
scene.add(cube);
}
function rotateCube() {
cube.rotation.x -= SPEED * 2;
cube.rotation.y -= SPEED;
cube.rotation.z -= SPEED * 3;
}
function render() {
requestAnimationFrame(render);
rotateCube();
renderer.render(scene, camera);
}
init();
render();
【问题讨论】: