更多有趣示例 尽在
小红砖社区
示例
HTML
CSS
body {
background-color: #000;
color: #fff;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
margin: 0;
overflow: hidden;
padding: 0px;
position: relative;
}
a {
color: #00b7ff;
}
JS
'use strict';
var LowPolyCloudWebGLDemo = LowPolyCloudWebGLDemo || {
};
LowPolyCloudWebGLDemo.settings = function() {
const sunColor = '#fcd2a8',
sunIntensity = 0.53,
ambientLight = '#8c8c8c',
skyColor = '#67b4e7',
skyWireframe = false,
cloudsColor = '#ffffff',
cloudsOpacity = 0.79,
cloudsWireframe = false,
cloudsBaseVelocity = 0.008,
homepage = 'https://github.com/enesser/low-poly-clouds-webgl';
let gui = new dat.GUI({
width: 360
});
gui.close();
let settingsSchema = {
reset: function() {
this.sunColor = sunColor;
this.sunIntensity = sunIntensity;
this.ambientLight = ambientLight;
this.skyColor = skyColor;
this.skyWireframe = skyWireframe;
this.cloudsColor = cloudsColor;
this.cloudsOpacity = cloudsOpacity;
this.cloudsWireframe = cloudsWireframe;
this.cloudsBaseVelocity = cloudsBaseVelocity;
for (let folder in gui.__folders) {
for (let i in gui.__folders[folder].__controllers) {
gui.__folders[folder].__controllers[i].updateDisplay();
}
}
},
homepage: function() {
window.open(homepage, '_blank');
}
};
function bindSettingsSchemaToUi(settingsSchema) {
let sunFolder = gui.addFolder('Sun');
sunFolder.addColor(settingsSchema, 'sunColor');
sunFolder.add(settingsSchema, 'sunIntensity').min(-10).max(50);
sunFolder.addColor(settingsSchema, 'ambientLight');
let skyFolder = gui.addFolder('Sky');
skyFolder.addColor(settingsSchema, 'skyColor');
skyFolder.add(settingsSchema, 'skyWireframe');
let cloudsFolder = gui.addFolder('Clouds');
cloudsFolder.addColor(settingsSchema, 'cloudsColor');
cloudsFolder.add(settingsSchema, 'cloudsOpacity').min(0).max(1);
cloudsFolder.add(settingsSchema, 'cloudsWireframe');
cloudsFolder.add(settingsSchema, 'cloudsBaseVelocity').min(0).max(3);
gui.add(settingsSchema, 'reset');
gui.add(settingsSchema, 'homepage');
}
settingsSchema.reset();
bindSettingsSchemaToUi(settingsSchema);
return settingsSchema;
};
LowPolyCloudWebGLDemo.world = function() {
const pathWorldModel = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/168282/low-poly-clouds.obj';
const pathWorldMaterial = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/168282/low-poly-clouds.mtl';
let worldObject = {
isLoaded: false,
percentLoaded: 0,
background: null,
backgroundMaterial: null,
cloudsMaterial: null,
clouds: []
};
function mapMaterialsTexturesToObject(worldObject) {
let loader = new THREE.OBJMTLLoader();
loader.load(pathWorldModel, pathWorldMaterial, (o) => {
const cloudBaseSize = 1.8;
worldObject.clouds = [];
worldObject.background = o.children.find(child => child.name === 'Plane_Plane.003');
worldObject.background.receiveShadow = true;
worldObject.backgroundMaterial = worldObject.background.children[1].material;
worldObject.backgroundMaterial.depthWrite = false;
worldObject.backgroundMaterial.receiveShadow = true;
for (let cloudTemplate of[
o.children.find(child => child.name === 'Cloud2_Icosphere.011'),
o.children.find(child => child.name === 'Cloud1_Icosphere.005')
]) {
worldObject.cloudsMaterial = cloudTemplate.children[1].material;
cloudTemplate.position.setY(cloudTemplate.position.y - 10);
for (let i = 0; i < 8; i++) {
let cloud = cloudTemplate.clone();
let cloudSize = cloudBaseSize + (Math.random() * 1.2);
cloud.scale.set(cloudSize, cloudSize, cloudSize);
let posXFlip = (Math.floor(Math.random() * 2) + 1) % 2 === 0 ? 1 : -1;
let posYFlip = (Math.floor(Math.random() * 2) + 1) % 2 === 0 ? 1 : -1;
let posX = cloud.position.x + (Math.random() * 50);
let posY = posYFlip > 0 ? cloud.position.y + (Math.random() * 3) : cloud.position.y - (Math.random() * 3);
cloud.position.setX(posX * posXFlip);
cloud.position.setY(posY);
cloud.position.setZ(cloud.position.z + (Math.random() * 3));
cloud.velocityAccelerator = (Math.random() * 0.008);
worldObject.clouds.push(cloud);
}
}
worldObject.cloudsMaterial.transparent = true;
worldObject.cloudsMaterial.depthWrite = false;
worldObject.isLoaded = true;
},
(xhr) => {
if (xhr.lengthComputable) {
worldObject.percentLoaded = xhr.loaded / xhr.total * 100;
}
},
(xhr) => {
console.error('Error loading world materials and textures', xhr);
});
}
mapMaterialsTexturesToObject(worldObject);
return worldObject;
};
((window, document) => {
window.onload = () => {
let scene,
camera,
renderer,
directionalLightColor,
directionalLight,
ambientLightColor,
ambientLight,
settings,
world,
windowHalfX = window.innerWidth / 2,
windowHalfY = window.innerHeight / 2;
(function init() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.set(0, 4, 64);
camera.lookAt(scene.position);
renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
let container = document.createElement('div');
document.body.appendChild(container);
container.appendChild(renderer.domElement);
directionalLightColor = new THREE.Color();
directionalLight = new THREE.DirectionalLight(directionalLightColor);
directionalLight.position.set(-20, 9.7, 14);
scene.add(directionalLight);
ambientLightColor = new THREE.Color();
ambientLight = new THREE.AmbientLight(ambientLightColor);
scene.add(ambientLight);
settings = LowPolyCloudWebGLDemo.settings();
world = LowPolyCloudWebGLDemo.world();
world.isAddedToScene = false;
window.addEventListener('resize', () => {
windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}, false);
})();
(function render() {
directionalLight.intensity = settings.sunIntensity;
directionalLightColor.setStyle(settings.sunColor);
directionalLight.color = directionalLightColor;
ambientLightColor.setStyle(settings.ambientLight);
ambientLight.color = ambientLightColor;
if (world.isLoaded) {
if (!world.isAddedToScene) {
scene.add(world.background);
for (let cloud of world.clouds) {
scene.add(cloud);
}
world.isAddedToScene = true;
}
world.backgroundMaterial.color.setStyle(settings.skyColor);
world.backgroundMaterial.wireframe = settings.skyWireframe;
world.cloudsMaterial.color.setStyle(settings.cloudsColor);
world.cloudsMaterial.opacity = settings.cloudsOpacity;
world.cloudsMaterial.wireframe = settings.cloudsWireframe;
for (let cloud of world.clouds) {
let newX = cloud.position.x + (settings.cloudsBaseVelocity + cloud.velocityAccelerator);
if (newX > 60) {
newX = -60;
}
cloud.position.setX(newX);
}
}
renderer.render(scene, camera);
requestAnimationFrame(render);
})();
};
})(window, document);