gpt4 book ai didi

javascript - Three.js - json 模型在屏幕上显得太大

转载 作者:行者123 更新时间:2023-12-02 13:47:38 25 4
gpt4 key购买 nike

我正在做 Three.js,我使用了一个 JSON 文件将其加载到 THREE.js 中,我的 JSON 文件是来自 clara.io 的狗。一切正常,问题是 3D 模型太大。有没有办法减少其加载到屏幕上的大小。

<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - loaders - JSON loader</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 {
font-family: Monospace;
background-color: #000;
color: #fff;
margin: 0px;
overflow: hidden;
}
#info {
color: #fff;
position: absolute;
top: 10px;
width: 100%;
text-align: center;
z-index: 100;
display:block;
}
#info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
</style>
</head>

<body>

<script src="three.js"></script>

<script src="Detector.js"></script>
<script src="stats.min.js"></script>

<script>
var container, stats;
var camera, scene, renderer;
var mouseX = 0, mouseY = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
camera.position.z = 4;
// scene
scene = new THREE.Scene();
var ambient = new THREE.AmbientLight( 0x444444 );
scene.add( ambient );
var directionalLight = new THREE.DirectionalLight( 0xffeedd );
directionalLight.position.set( 0, 0, 1 ).normalize();
scene.add( directionalLight );
// BEGIN Clara.io JSON loader code
var objectLoader = new THREE.ObjectLoader();
objectLoader.load("blue-dog.json", function ( obj ) {
scene.add( obj );
} );
// END Clara.io JSON loader code
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
//
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function onDocumentMouseMove( event ) {
mouseX = ( event.clientX - windowHalfX ) / 2;
mouseY = ( event.clientY - windowHalfY ) / 2;
}
//
function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
camera.position.x += ( mouseX - camera.position.x ) * .05;
camera.position.y += ( - mouseY - camera.position.y ) * .05;
camera.lookAt( scene.position );
renderer.render( scene, camera );
}
</script>

</body>
</html>

如果有人能提供任何帮助,请不胜感激。我也在 WAMP 上托管它。 JSON 文件太大,无法粘贴。

最佳答案

您可以设置对象的比例:

objectLoader.load("blue-dog.json", function( obj ){
obj.scale.set( .5, .5, .5 );
scene.add( obj );
});

或者缩小你的相机,但说实话,我不能给你这个例子,因为我不能 100% 确定你想对相机做什么(渲染函数中奇怪的 += -= 让我感到困惑少量)。你可以试试这个:

camera.position.multiplyScalar( 2 ); 
// This should double the distance between your camera and the center of the scene

但只尝试一次(如果您在渲染循环中重复此操作,它将永久缩小,并且会很快发生)。

关于javascript - Three.js - json 模型在屏幕上显得太大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41249096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com