three实现三维彩色立体反射方块效果代码
代码语言:html
所属分类:三维
代码描述:three实现三维彩色立体反射方块效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> canvas { display: block; width: 100%; height: 100vh; cursor: -webkit-grab; cursor: grab; } div { padding: 1ch; position: fixed; top: 0; left: 0; } </style> </head> <body > <script async src="//repo.bfw.wiki/bfwrepo/js/es-module-shims.1.5.1.j" crossorigin="anonymous"></script> <script type="importmap"> { "imports": { "three": "//repo.bfw.wiki/bfwrepo/js/module/three/build/146/three.module.js", "three/addons/": "//repo.bfw.wiki/bfwrepo/js/module/three/examples/jsm/" } } </script> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/gsap.3.11.0.js"></script> <script type="module"> import * as THREE from 'three' import { OrbitControls } from 'three/addons/controls/OrbitControls.js' import { RoundedBoxGeometry } from 'three/addons/geometries/RoundedBoxGeometry.js' // ---- // params // ---- const SIDE = 0.2 // chunk side length before applying scaling const MARGIN = 0.01 // chunk's margin; gap between chunks is 2*MARGIN const DIMS = new THREE.Vector2(6, 6) // grid dimensions const CHUNK_MAX_DIMS = new THREE.Vector2(4, 4) // max chunk dimensions const COLORS = [ new THREE.Color('white'), new THREE.Color('gray'), new THREE.Color('lightgray'), new THREE.Color('red'), new THREE.Color('blue'), new THREE.Color('yellow'), new THREE.Color('black') ] // credit: mrdoob - three.js/examples/textures/ const ENV_URL = '//repo.bfw.wiki/bfwrepo/image/636d9baf40a77.png' // ---- // main // ---- const renderer = new THREE.WebGLRenderer({ antialias: true }) const scene = new THREE.Scene() const camera = new THREE.PerspectiveCamera(75, 2, 0.1, 100) const controls = new OrbitControls(camera, renderer.domElement) scene.background = new THREE.Color('white') camera.position.set(1, 1, 1) controls.enableDamping = true controls.autoRotate = true controls.maxPolarAngle = (90) * Math.PI / 180 renderer.shadowMap.enabled = true renderer.toneMapping = THREE.ACESFilmicToneMapping renderer.toneMappingExposure = 1.0 renderer.outputEncoding = THREE.sRGBEncoding renderer.physicallyCorrectLights = true new THREE.TextureLoader().load(ENV_URL, (tex) => { tex.mapping = THREE.EquirectangularReflectionMapping tex.encoding = THREE.sRGBEncoding const rt = new THREE.PMREMGenerator(renderer).fromEquirectangular(tex) scene.environment = rt.texture }) const light = new THREE.DirectionalLight() light.position.set(3, 3, -3) light.castShadow = true light.shadow.mapSize.setScalar(1024) scene.add(light) scene.add(new THREE.AmbientLight()) scene.add(new THREE.GridHelper(2, 2)) // ---- // create instanced mesh // ---- const geom = new RoundedBoxGeometry(SIDE, SIDE, SIDE, 2, SIDE / 10).translate(0, SIDE / 2, 0) const mat = new THREE.MeshPhysicalMaterial({ metalness: 0.0, roughness: 0.5, clearcoat: 1.0, envMapIntensity: 20.0 }) const instances_count = DIMS.x * DIMS.y const mesh = new THREE.InstancedMesh(geom, mat, instances_count) mesh.castShadow = true mesh.receiveShadow = true scene.add(mesh) // ---- // init instanced mesh // ---- const chunks = get_chunks(DIMS) for (let i = 0, idx = 0; i < DIMS.y; ++i) { .........完整代码请登录后点击上方下载按钮下载查看
网友评论0