我正在尝试使用 Threejs 中的纹理在顶部和底部放置不同的图像。但是我在底部和顶部都得到了相同的图像。下面是我正在使用的代码。我的要求是使用 ThreeJs 中的纹理在两侧显示不同的图像。
注意: 1. 在我的代码中,我使用了 ThreeJs 版本(r125),这是必要的,我不能使用旧版本。
<html lang="en">
<head>
<title>three.js webgl - geometry - shapes</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
<style>
body {
background-color: #f0f0f0;
color: #444;
}
</style>
</head>
<body>
<div id="info">texture on shapes</div>
<script type="module">
import * as THREE from 'https://threejsfundamentals.org/threejs/resources/threejs/r125/build/three.module.js';
import { OrbitControls } from "https://threejsfundamentals.org/threejs/resources/threejs/r125/examples/jsm/controls/OrbitControls.js";
let container;
let camera, scene, renderer;
let group;
let windowHalfX = window.innerWidth / 2;
init();
animate();
function init() {
container = document.createElement('div');
document.body.appendChild(container);
scene = new THREE.Scene();
scene.background = new THREE.Color(0xbbbbbb);
camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.set(0, -150, -300);
scene.add(camera);
const light = new THREE.PointLight(0xffffff, 0.8);
camera.add(light);
group = new THREE.Group();
group.rotation.y = Math.PI;
scene.add(group);
const helper = new THREE.GridHelper(500, 10);
helper.rotation.x = Math.PI / 2;
group.add(helper);
const rectWidth = 120;
const rectHeight = 200;
const rectangleShape = new THREE.Shape()
.moveTo(0, 0)
.lineTo(0, rectHeight)
.lineTo(rectWidth, rectHeight)
.lineTo(rectWidth, 0)
.lineTo(0, 0);
const extrudeSettings = { depth: 10, bevelEnabled: true, bevelSegments: 2, steps: 1, bevelSize: 1, bevelThickness: 1 };
const geometry = new THREE.ExtrudeGeometry(rectangleShape, extrudeSettings);
var textureLoader1 = new THREE.TextureLoader();
var topTexture = textureLoader1.load("https://threejsfundamentals.org/threejs/resources/images/tree-01.png"); // Top side Image
topTexture.repeat.set(1 / rectWidth, 1 / rectHeight);
var textureLoader2 = new THREE.TextureLoader();
var bottomTexture = textureLoader2.load("https://threejsfundamentals.org/threejs/resources/images/tree-02.png"); // Bottom side Image
bottomTexture.repeat.set(1 / rectWidth, 1 / rectHeight);
var frontMaterial = new THREE.MeshPhongMaterial({ map: topTexture, side: THREE.FrontSide });
var backMaterial = new THREE.MeshPhongMaterial({ map: bottomTexture, side: THREE.BackSide });
var sideMaterial = new THREE.MeshPhongMaterial({ color: 0xD9D934 });
var materials = [frontMaterial, sideMaterial, sideMaterial, sideMaterial, sideMaterial, backMaterial];
var material = new THREE.MeshFaceMaterial(materials);
let mesh = new THREE.Mesh(geometry, material);
mesh.position.set(-60, -100, 0);
// Edited but faces not found
for (var face in mesh.geometry.faces) {
if (mesh.geometry.faces[face].normal.z < -0.9) {
mesh.geometry.faces[face].materialIndex = 5;
}
}
group.add(mesh);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 0, 0);
controls.update();
window.addEventListener('resize', onWindowResize);
}
function onWindowResize() {
windowHalfX = window.innerWidth / 2;
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
requestAnimationFrame(animate);
render();
}
function render() {
renderer.render(scene, camera);
}
</script>
</body>
</html>