three.js点材质实现星空效果

最终效果如下:

html代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script type="importmap">
        {
            "imports": {
                "three": "../../three/build/three.module.js",
                "three/addons/": "../../three/examples/jsm/"
            }
        }
    </script>
    <script type="module" src="./index.js">

    </script>
</body>

</html>

index.js代码:

import * as THREE from 'three'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
const width = 800;
const height = 500;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
    45,
    width / height,
    0.1,
    1000
);
camera.position.set(100, 100, 100);
camera.lookAt(0, 0, 0);

const renderer = new THREE.WebGLRenderer();
renderer.setSize(width, height);
document.body.appendChild(renderer.domElement);

// 粒子
const arr = [];
const N = 5000;
for (let i = 0; i < N; i++) {
    const x = 300 * Math.random(0, 1) - 80;
    const y = 300 * Math.random(0, 1) - 80;
    const z = 300 * Math.random(0, 1) - 80;
    arr.push(new THREE.Vector3(x, y, z));
}
const geometry = new THREE.BufferGeometry();
geometry.setFromPoints(arr);
const loader = new THREE.TextureLoader();
const texture = loader.load("./stars.png");
const material = new THREE.PointsMaterial({
    color: 0x4d76cf,
    map: texture,
    transparent: true,
    opacity: 1,
    sizeAttenuation:true
})
const point = new THREE.Points(geometry, material);
scene.add(point);

// 粒子球
const sphereGeometry = new THREE.SphereGeometry(30,30,30);
const material2 = new THREE.PointsMaterial({
    color:0x4d76cf
});
const sphere = new THREE.Points(sphereGeometry,material2)
scene.add(sphere);

const OrbitControl = new OrbitControls(camera, renderer.domElement);

function render() {
    point.rotation.y += 0.001;
    sphere.rotation.y += 0.005;
    requestAnimationFrame(render);
    renderer.render(scene, camera);
}
render()

刚开始学习three.js,写的不好请多担待!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用three.js实现贴图的倒影效果可以通过以下步骤实现: 1. 创建一个场景和相机 ``` const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); camera.position.z = 5; ``` 2. 创建一个平面几何体,并设置其位置和旋转,同时为其添加一个材质(使用图片贴图)和一个网格 ``` const geometry = new THREE.PlaneGeometry(2, 2); const texture = new THREE.TextureLoader().load('path/to/texture.jpg'); const material = new THREE.MeshBasicMaterial({map: texture}); const plane = new THREE.Mesh(geometry, material); plane.position.y = -1; plane.rotation.x = -Math.PI / 2; scene.add(plane); ``` 3. 创建一个渲染器,并将其添加到DOM元素中 ``` const renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); ``` 4. 创建一个渲染目标,并将其设置为平面几何体的材质的map属性 ``` const renderTarget = new THREE.WebGLRenderTarget(window.innerWidth, window.innerHeight); material.map = renderTarget.texture; ``` 5. 创建一个镜像相机,并将其放置在平面几何体的下方 ``` const mirrorCamera = new THREE.CubeCamera(0.1, 10, 256); mirrorCamera.position.set(0, -1, 0); scene.add(mirrorCamera); ``` 6. 将场景中的物体渲染到渲染目标中,并将镜像相机的renderTarget设置为渲染目标 ``` mirrorCamera.update(renderer, scene); ``` 7. 将渲染目标渲染到屏幕上 ``` renderer.render(scene, camera); ``` 完整代码如下: ``` const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); camera.position.z = 5; const geometry = new THREE.PlaneGeometry(2, 2); const texture = new THREE.TextureLoader().load('path/to/texture.jpg'); const material = new THREE.MeshBasicMaterial({map: texture}); const plane = new THREE.Mesh(geometry, material); plane.position.y = -1; plane.rotation.x = -Math.PI / 2; scene.add(plane); const renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); const renderTarget = new THREE.WebGLRenderTarget(window.innerWidth, window.innerHeight); material.map = renderTarget.texture; const mirrorCamera = new THREE.CubeCamera(0.1, 10, 256); mirrorCamera.position.set(0, -1, 0); scene.add(mirrorCamera); mirrorCamera.update(renderer, scene); renderer.render(scene, camera); ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值