如何将图像放入 Three.js 纹理
How can i put image in Three.js texture
我试图将纹理放入 Three.js 对象中,但是当我应用图像时,对象全黑了。一些警报显示在代码上但没有错误。上图显示结果。有什么建议吗?
<!DOCTYPE html>
<html>
<head>
<title>Meu Primeiro WebGL</title>
<style type="text/css">canvas {width: 100%; height: 100%}</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<script src="three.js"></script>
<script>
var scene = new THREE.Scene();
var loader = new THREE.ImageLoader();
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);
//primeiro cubo
var cubegeometry = new THREE.CubeGeometry(5,0.45,5);
var cubematerial = new THREE.MeshBasicMaterial({wireframe: false, color: 0x0033FF});
var cube = new THREE.Mesh(cubegeometry, cubematerial);
//segundo objeto
var texture = THREE.ImageUtils.loadTexture('wood.jpg');
var cbmaterial = new THREE.MeshPhongMaterial({map: texture});
var cbgeometry = new THREE.CubeGeometry(5.05,0.5,3.75);
var cb = new THREE.Mesh(cbgeometry, cbmaterial);
scene.add(cube);
scene.add(cb);
camera.position.z = 6
var render = function(){
requestAnimationFrame(render);
cube.rotation.y += 0.02;
cube.rotation.x += 0.01;
cube.rotation.z += 0.00;
如果场景中没有任何灯光,那可能就是问题所在。至少添加一些环境光。例如:
var ambient = new THREE.AmbientLight( 0x555555 );
scene.add( ambient );
我试图将纹理放入 Three.js 对象中,但是当我应用图像时,对象全黑了。一些警报显示在代码上但没有错误。上图显示结果。有什么建议吗?
<!DOCTYPE html>
<html>
<head>
<title>Meu Primeiro WebGL</title>
<style type="text/css">canvas {width: 100%; height: 100%}</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<script src="three.js"></script>
<script>
var scene = new THREE.Scene();
var loader = new THREE.ImageLoader();
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);
//primeiro cubo
var cubegeometry = new THREE.CubeGeometry(5,0.45,5);
var cubematerial = new THREE.MeshBasicMaterial({wireframe: false, color: 0x0033FF});
var cube = new THREE.Mesh(cubegeometry, cubematerial);
//segundo objeto
var texture = THREE.ImageUtils.loadTexture('wood.jpg');
var cbmaterial = new THREE.MeshPhongMaterial({map: texture});
var cbgeometry = new THREE.CubeGeometry(5.05,0.5,3.75);
var cb = new THREE.Mesh(cbgeometry, cbmaterial);
scene.add(cube);
scene.add(cb);
camera.position.z = 6
var render = function(){
requestAnimationFrame(render);
cube.rotation.y += 0.02;
cube.rotation.x += 0.01;
cube.rotation.z += 0.00;
如果场景中没有任何灯光,那可能就是问题所在。至少添加一些环境光。例如:
var ambient = new THREE.AmbientLight( 0x555555 );
scene.add( ambient );