简体   繁体   English

在 canvas 中渲染 Three.js 场景

[英]Render Three.js scene in canvas

I've been trying to render the scene below in a canvas for so many times and it still doesn't work.我一直试图在 canvas 中渲染下面的场景很多次,但它仍然不起作用。
I've tried replacing var renderer = new THREE.WebGLRenderer({antialias: true});我试过替换var renderer = new THREE.WebGLRenderer({antialias: true}); with var renderer = new THREE.WebGLRenderer({ canvas: canvas }); with var renderer = new THREE.WebGLRenderer({ canvas: canvas }); , but instead of rendering it just starts infinitely loading. ,但不是渲染它只是开始无限加载。

Full HTML page: https://jsfiddle.net/1k04rwsx/ ( replace the obj models with something to make it load)完整的 HTML 页面: https://jsfiddle.net/1k04rwsx/替换obj 模型以使其加载)

PS: I'm new to this, sorry if the code looks a bit weird. PS:我是新手,如果代码看起来有点奇怪,对不起。
Huge thanks to anyone who can help!非常感谢任何可以提供帮助的人!

Your loading screen does not disappear because you the fiddle loads OBJs from relative URLs (which is invalid with jsFiddle).您的加载屏幕不会消失,因为您小提琴从相对 URL 加载 OBJ(这对 jsFiddle 无效)。 I've replaced one URL with a link to the official repository and things start working.我已经用官方存储库的链接替换了一个 URL 并且开始工作。

 /** * Generate a scene object with a background color **/ function getScene() { var scene = new THREE.Scene(); scene.background = new THREE.Color(0x111111); return scene; } /** * Generate the camera to be used in the scene. Camera args: * [0] field of view: identifies the portion of the scene * visible at any time (in degrees) * [1] aspect ratio: identifies the aspect ratio of the * scene in width/height * [2] near clipping plane: objects closer than the near * clipping plane are culled from the scene * [3] far clipping plane: objects farther than the far * clipping plane are culled from the scene **/ function getCamera() { var aspectRatio = window.innerWidth / window.innerHeight; var camera = new THREE.PerspectiveCamera(75, aspectRatio, 0.1, 1000); camera.position.set(0, 1, -10); return camera; } /** * Generate the light to be used in the scene. Light args: * [0]: Hexadecimal color of the light * [1]: Numeric value of the light's strength/intensity * [2]: The distance from the light where the intensity is 0 * @param {obj} scene: the current scene object **/ function getLight(scene) { var light = new THREE.PointLight(0xffffff, 1, 0); light.position.set(1, 5, -2); scene.add(light); const lighta = new THREE.AmbientLight( 0x404040 ); // soft white light scene.add( lighta ); var ambientLight = new THREE.AmbientLight(0x111111); scene.add(ambientLight); return light; } /** * Generate the renderer to be used in the scene **/ function getRenderer() { // Create the canvas with a renderer var renderer = new THREE.WebGLRenderer({antialias: true}); //original // Add support for retina displays renderer.setPixelRatio(window.devicePixelRatio); // Specify the size of the canvas renderer.setSize(window.innerWidth, window.innerHeight); // Add the canvas to the DOM document.body.appendChild(renderer.domElement); return renderer; } /** * Generate the controls to be used in the scene * @param {obj} camera: the three.js camera for the scene * @param {obj} renderer: the three.js renderer for the scene **/ function getControls(camera, renderer) { var controls = new THREE.TrackballControls(camera, renderer.domElement); controls.zoomSpeed = 0.4; controls.panSpeed = 0.4; return controls; } /** * Load Nimrud model **/ function loadModel() { var loader = new THREE.OBJLoader(); loader.load( 'https://threejs.org/examples/models/obj/tree.obj', function ( object ) { //this is just a character, can be replaced to test object.rotation.y = 1.5; object.scale.y = 0.7; object.scale.x = 0.7; object.scale.z = 0.7; scene.add( object ); document.querySelector('h1').style.display = 'none'; } ); } /*function loadModel() { var loader = new THREE.OBJLoader(); loader.load( 'avv.obj', function ( object ) { var box = new THREE.Box3().setFromObject( object ); var center = new THREE.Vector3(); box.getCenter( center ); object.position.sub( center ); // center the model object.rotation.y = Math.PI; // rotate the model scene.add( object ); document.querySelector('h1').style.display = 'none'; } ); }*/ /*function loadModel() { var loader = new THREE.OBJLoader(); loader.load( 'avc.obj', function ( object ) { //object.rotation.y = 1.5; object.position.y = -18; object.position.x = -16; object.position.z = -2; scene.add( object ); document.querySelector('h1').style.display = 'none'; } ); }*/ /** * Render; **/ function render() { requestAnimationFrame(render). renderer,render(scene; camera). //controls;update(); }; var scene = getScene(); var camera = getCamera(); var light = getLight(scene); var renderer = getRenderer(), var controls = getControls(camera; renderer); loadModel() render();
 html, body { width: 100%; height: 100%; background: #000; color: #fff; } body { margin: 0; overflow: hidden; } canvas { width: 100%; height: 100%; }.lds-dual-ring { display: inline-block; width: 80px; height: 80px; }.lds-dual-ring:after { content: " "; display: block; width: 64px; height: 64px; margin: 8px; border-radius: 50%; border: 6px solid #fff; border-color: #fff transparent #fff transparent; animation: lds-dual-ring 1.2s linear infinite; } @keyframes lds-dual-ring { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
 <script src='https://cdn.jsdelivr.net/npm/three@0.88/build/three.min.js'></script> <script src='https://cdn.jsdelivr.net/npm/three@0.88/examples/js/controls/TrackballControls.js'></script> <script src='https://cdn.jsdelivr.net/npm/three@0.88/examples/js/loaders/OBJLoader.js'></script> <h1 style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);"><div class="lds-dual-ring"></div></h1>

BTW: Please do not mix three.js classes from different releases.顺便说一句:请不要混合来自不同版本的three.js类。 Always ensure to stick to a single release like in the above code.始终确保坚持使用上述代码中的单个版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM