three-grass-tufts
Version:
Three.JS extension for rendering realistic grass at speed.
74 lines (67 loc) • 2 kB
JavaScript
;
import {
DefaultLoadingManager,
Matrix4,
PlaneGeometry,
TextureLoader
} from 'three';
/*
* Three.JS extension for rendering realistic grass at speed.
*
* @author @codetheorist
* @class GrassTufts
* @constructor
*/
function GrassTufts(manager) {
this.manager = (manager !== undefined) ? manager : DefaultLoadingManager;
this.texturePath = '';
this.loader = new TextureLoader();
this.textures = [];
this.points = [];
this.materials = [];
this.mesh = [];
this.geometries = {};
}
Object.assign( GrassTufts.prototype, {
createGrassTufts: function (x, y, textures/*, points*/) {
// let merged = new Geometry();
this.geometry = new PlaneGeometry(x, y);
this.geometry.applyMatrix(
new Matrix4().makeTranslation(
0, this.geometry.height / 2, 0
)
);
this.geometry.faces.forEach( (face) => {
face.vertexNormals.forEach( (normal) => {
normal.set(0.0, 1.0, 0.0).normalize();
});
});
let scope = this;
this.loader = new TextureLoader();
if (textures === null) {
this.textureUrls = ['./../images/grass01.png'];
} else {
for(let t = 0; t < textures.length; t++) {
let texture = textures[t];
this.textures.push(this.loader.load(texture))
console.log(scope.textures)
console.log(t, typeof texture)
}
}
// load the texture
// var textureUrl = THREE.createGrassTufts.baseUrl +
// var texture = TextureLoader(textureUrl)
// // build the material
// var material = new MeshPhongMaterial({
// map: texture,
// color: 'grey',
// emissive: 'darkgreen',
// alphaTest: 0.7,
// })
// // create the mesh
// var mesh = new Mesh(merged, material)
// return mesh
}
}
);
export default GrassTufts;