我在 Blender 中创建了一个 3D 模型。我使用导出器和 convert_obj_three.py 导出它(两者都导出正常,但没有在浏览器中显示,代码如下)。
我从这里使用了 html/js 下面的代码:
http://dev.opera.com/articles/view/porting-3d-graphics-to-the-web-webgl-intro-part-2/
该站点的原始演示/任何其他 three.js 示例在 Firefox(Mac OS X 10.6.8,AMD Radeon HD 6490M)中运行良好。
所有其他浏览器似乎都有问题。
有趣的是,如果我运行一个简单的:
python -m SimpleHTTPServer 8888
从任何示例目录并想通过 Firefox 查看它,它也不起作用/渲染。
我总是得到背景颜色和文本,但没有渲染。
并且所有需要的三个 .js 都在同一个目录中。
我错过了什么还是这只是随机的?
如何显示一个可以由用户旋转的简单 3D 模型?这段代码正确吗?
谢谢。
<!doctype html>
<html lang="en">
<head>
<title>WebGL made easy</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background:#fff;
padding:0;
margin:0;
overflow:hidden;
font-family:'trebuchet ms', 'lucida grande', 'lucida sans unicode', arial, helvetica, sans-serif;
text-align:center;
}
canvas { pointer-events:none; z-index:10; }
p { font-size: small;}
</style>
</head>
<body>
<script src="Three.js"></script>
<script src="Detector.js"></script>
<script src="RequestAnimationFrame.js"></script>
<script>
if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
var SCREEN_WIDTH = window.innerWidth;
var SCREEN_HEIGHT = window.innerHeight;
var FLOOR = 0;
var container;
var camera, scene;
var webglRenderer;
var zmesh, geometry;
var mouseX = 0, mouseY = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
// camera
camera = new THREE.PerspectiveCamera( 75, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 100000 );
camera.position.z = 75;
//scene
scene = new THREE.Scene();
// lights
var ambient = new THREE.AmbientLight( 0xffffff );
scene.add( ambient );
// more lights
var directionalLight = new THREE.DirectionalLight( 0xffeedd );
directionalLight.position.set( 0, -70, 100 ).normalize();
scene.add( directionalLight );
// renderer
webglRenderer = new THREE.WebGLRenderer();
webglRenderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
webglRenderer.domElement.style.position = "relative";
container.appendChild( webglRenderer.domElement );
// loader
var loader = new THREE.JSONLoader(),
callbackModel = function( geometry ) { createScene( geometry, 90, FLOOR, -50, 105 ) };
loader.load( { model: "mymodel.js", callback: callbackModel } );
}
function createScene( geometry, x, y, z, b ) {
zmesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial() );
zmesh.position.set( 0, 16, 0 );
zmesh.scale.set( 1, 1, 1 );
scene.add( zmesh );
}
function onDocumentMouseMove(event) {
mouseX = ( event.clientX - windowHalfX );
mouseY = ( event.clientY - windowHalfY );
}
function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
zmesh.rotation.set(-mouseY/500 + 1, -mouseX/200, 0);
webglRenderer.render( scene, camera );
}
</script>
</body>
</html>