xxxxxxxxxx
111
let m;
let tempVertices = [];
let tempVertexNormals = [];
let tempUvs = [];
let tempFaces = [];
function setup() {
createCanvas(400, 400, WEBGL);
m = createSpike();
strokeWeight(0.5);
noStroke();
fill(250, 170, 0);
}
function draw() {
background(80, 0, 0);
rotateZ(frameCount * 0.01);
rotateX(frameCount * 0.01);
rotateY(frameCount * 0.01);
ambientLight(140, 140, 0);
pointLight(255, 255, 0, width/2, -height/2, width);
scale(.5);
model(m);
}
function createSpike() {
let detailX = 14;
let detailY = 15;
let t = 2 * detailX - 1;
return new p5.Geometry(detailX, detailY, function () {
// bottomRadius, topRadius, c_height, detailX, offset
buildGeometry(40, 60, 90, detailX, -200);
for (let i = 0; i <= t; i++) {
this.vertices.push(tempVertices[i]);
this.vertexNormals.push(tempVertexNormals[i]);
}
buildGeometry(60, 40, 90, detailX, -110);
for (let i = 0; i <= t; i++) {
this.vertices.push(tempVertices[i]);
this.vertexNormals.push(tempVertexNormals[i]);
}
buildGeometry(40, 60, 90, detailX, -20);
for (let i = 0; i <= t; i++) {
this.vertices.push(tempVertices[i]);
this.vertexNormals.push(tempVertexNormals[i]);
}
buildGeometry(60, 100, 70, detailX, 70);
for (let i = 0; i <= t; i++) {
this.vertices.push(tempVertices[i]);
this.vertexNormals.push(tempVertexNormals[i]);
}
buildGeometry(100, 180, 100, detailX, 140);
for (let i = 0; i <= t; i++) {
this.vertices.push(tempVertices[i]);
this.vertexNormals.push(tempVertexNormals[i]);
}
buildGeometry(180, 150, 30, detailX, 240);
for (let i = 0; i <= t; i++) {
this.vertices.push(tempVertices[i]);
this.vertexNormals.push(tempVertexNormals[i]);
}
buildGeometry(150, 120, -30, detailX, 270);
for (let i = 0; i <= t; i++) {
this.vertices.push(tempVertices[i]);
this.vertexNormals.push(tempVertexNormals[i]);
}
buildGeometry(120, 0, 50, detailX, 240);
for (let i = 0; i <= t; i++) {
this.vertices.push(tempVertices[i]);
this.vertexNormals.push(tempVertexNormals[i]);
}
let startIndex = 0;
for (let i = 0; i < detailY; i++) {
for (let j = 0; j < detailX; j++) {
const nexti = (j + 1) % detailX;
this.faces.push([startIndex + j, startIndex + nexti, startIndex + detailX + nexti]);
this.faces.push([startIndex + j, startIndex + detailX + nexti, startIndex + detailX + j]);
}
startIndex += detailX;
}
});
}
function buildGeometry(bottomRadius, topRadius, c_height, detailX, offset){
tempVertices = [];
tempVertexNormals = [];
tempUvs = [];
slant = atan2(bottomRadius - topRadius, c_height);
sinSlant = sin(slant);
cosSlant = cos(slant);
for (let i = 0; i <= 1; i++) {
let y = offset + c_height * i;
if(i == 0) radius = bottomRadius;
else radius = topRadius;
for(let j = 0; j < detailX; j++) {
const ur = TWO_PI * j / (detailX - 1);
tempVertices.push(new p5.Vector(sin(ur) * radius, y, cos(ur) * radius));
tempVertexNormals.push(new p5.Vector(sin(ur) * cosSlant, sinSlant, cos(ur) * cosSlant));
}
}
}