CSS3DRenderer, CSS3DObject, OrthographicCamera API 结合使用案例

本文主要是介绍CSS3DRenderer, CSS3DObject, OrthographicCamera API 结合使用案例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

CSS3DRenderer, CSS3DObject, OrthographicCamera API 结合使用案例


<!DOCTYPE html>
<html><head><title>three.js css3d - orthographic</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;}a {color: #f00;}#info {color: #000000;}</style></head><body><div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> css3d - orthographic</div><script type="importmap">{"imports": {"three": "../build/three.module.js","three/addons/": "./jsm/"}}</script><script type="module">import * as THREE from 'three';import { OrbitControls } from 'three/addons/controls/OrbitControls.js';import { CSS3DRenderer, CSS3DObject } from 'three/addons/renderers/CSS3DRenderer.js';import { GUI } from 'three/addons/libs/lil-gui.module.min.js';let camera, scene, renderer;let scene2, renderer2;const frustumSize = 500;init();animate();function init() {const aspect = window.innerWidth / window.innerHeight;camera = new THREE.OrthographicCamera( frustumSize * aspect / - 2, frustumSize * aspect / 2, frustumSize / 2, frustumSize / - 2, 1, 1000 );camera.position.set( - 200, 200, 200 );scene = new THREE.Scene();scene.background = new THREE.Color( 0xf0f0f0 );scene2 = new THREE.Scene();const material = new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, wireframeLinewidth: 1, side: THREE.DoubleSide } );// leftcreatePlane(100, 100,'chocolate',new THREE.Vector3( - 50, 0, 0 ),new THREE.Euler( 0, - 90 * THREE.MathUtils.DEG2RAD, 0 ));// rightcreatePlane(100, 100,'saddlebrown',new THREE.Vector3( 0, 0, 50 ),new THREE.Euler( 0, 0, 0 ));// topcreatePlane(100, 100,'yellowgreen',new THREE.Vector3( 0, 50, 0 ),new THREE.Euler( - 90 * THREE.MathUtils.DEG2RAD, 0, 0 ));// bottomcreatePlane(300, 300,'seagreen',new THREE.Vector3( 0, - 50, 0 ),new THREE.Euler( - 90 * THREE.MathUtils.DEG2RAD, 0, 0 ));//renderer = new THREE.WebGLRenderer();renderer.setPixelRatio( window.devicePixelRatio );renderer.setSize( window.innerWidth, window.innerHeight );document.body.appendChild( renderer.domElement );renderer2 = new CSS3DRenderer();renderer2.setSize( window.innerWidth, window.innerHeight );renderer2.domElement.style.position = 'absolute';renderer2.domElement.style.top = 0;document.body.appendChild( renderer2.domElement );const controls = new OrbitControls( camera, renderer2.domElement );controls.minZoom = 0.5;controls.maxZoom = 2;function createPlane( width, height, cssColor, pos, rot ) {const element = document.createElement( 'div' );element.style.width = width + 'px';element.style.height = height + 'px';element.style.opacity = 0.75;element.style.background = cssColor;const object = new CSS3DObject( element );object.position.copy( pos );object.rotation.copy( rot );scene2.add( object );const geometry = new THREE.PlaneGeometry( width, height );const mesh = new THREE.Mesh( geometry, material );mesh.position.copy( object.position );mesh.rotation.copy( object.rotation );scene.add( mesh );}window.addEventListener( 'resize', onWindowResize );createPanel();}function onWindowResize() {const aspect = window.innerWidth / window.innerHeight;camera.left = - frustumSize * aspect / 2;camera.right = frustumSize * aspect / 2;camera.top = frustumSize / 2;camera.bottom = - frustumSize / 2;camera.updateProjectionMatrix();renderer.setSize( window.innerWidth, window.innerHeight );renderer2.setSize( window.innerWidth, window.innerHeight );}function animate() {requestAnimationFrame( animate );renderer.render( scene, camera );renderer2.render( scene2, camera );}function createPanel() {const panel = new GUI();const folder1 = panel.addFolder( 'camera setViewOffset' ).close();const settings = {'setViewOffset'() {folder1.children[ 1 ].enable().setValue( window.innerWidth );folder1.children[ 2 ].enable().setValue( window.innerHeight );folder1.children[ 3 ].enable().setValue( 0 );folder1.children[ 4 ].enable().setValue( 0 );folder1.children[ 5 ].enable().setValue( window.innerWidth );folder1.children[ 6 ].enable().setValue( window.innerHeight );},'fullWidth': 0,'fullHeight': 0,'offsetX': 0,'offsetY': 0,'width': 0,'height': 0,'clearViewOffset'() {folder1.children[ 1 ].setValue( 0 ).disable();folder1.children[ 2 ].setValue( 0 ).disable();folder1.children[ 3 ].setValue( 0 ).disable();folder1.children[ 4 ].setValue( 0 ).disable();folder1.children[ 5 ].setValue( 0 ).disable();folder1.children[ 6 ].setValue( 0 ).disable();camera.clearViewOffset();}};folder1.add( settings, 'setViewOffset' );folder1.add( settings, 'fullWidth', window.screen.width / 4, window.screen.width * 2, 1 ).onChange( ( val ) => updateCameraViewOffset( { fullWidth: val } ) ).disable();folder1.add( settings, 'fullHeight', window.screen.height / 4, window.screen.height * 2, 1 ).onChange( ( val ) => updateCameraViewOffset( { fullHeight: val } ) ).disable();folder1.add( settings, 'offsetX', 0, 256, 1 ).onChange( ( val ) => updateCameraViewOffset( { x: val } ) ).disable();folder1.add( settings, 'offsetY', 0, 256, 1 ).onChange( ( val ) => updateCameraViewOffset( { y: val } ) ).disable();folder1.add( settings, 'width', window.screen.width / 4, window.screen.width * 2, 1 ).onChange( ( val ) => updateCameraViewOffset( { width: val } ) ).disable();folder1.add( settings, 'height', window.screen.height / 4, window.screen.height * 2, 1 ).onChange( ( val ) => updateCameraViewOffset( { height: val } ) ).disable();folder1.add( settings, 'clearViewOffset' );}function updateCameraViewOffset( { fullWidth, fullHeight, x, y, width, height } ) {if ( ! camera.view ) {camera.setViewOffset( fullWidth || window.innerWidth, fullHeight || window.innerHeight, x || 0, y || 0, width || window.innerWidth, height || window.innerHeight );} else {camera.setViewOffset( fullWidth || camera.view.fullWidth, fullHeight || camera.view.fullHeight, x || camera.view.offsetX, y || camera.view.offsetY, width || camera.view.width, height || camera.view.height );}}</script></body>
</html>

本内容来源于小豆包,想要更多内容请跳转小豆包 》

这篇关于CSS3DRenderer, CSS3DObject, OrthographicCamera API 结合使用案例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/817508

相关文章

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

MyBatis分页查询实战案例完整流程

《MyBatis分页查询实战案例完整流程》MyBatis是一个强大的Java持久层框架,支持自定义SQL和高级映射,本案例以员工工资信息管理为例,详细讲解如何在IDEA中使用MyBatis结合Page... 目录1. MyBATis框架简介2. 分页查询原理与应用场景2.1 分页查询的基本原理2.1.1 分

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1