Skip to content

Commit

Permalink
feat: tetrahedral, octahedron, isocahedron, dodecahedron, PrimitiveArray
Browse files Browse the repository at this point in the history
  • Loading branch information
bhouston committed Jun 18, 2020
1 parent 983ddb1 commit 9c86560
Show file tree
Hide file tree
Showing 13 changed files with 825 additions and 18 deletions.
8 changes: 8 additions & 0 deletions examples/gettingstarted/5_reflectiveCube/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html>
<head>
<title>Threeify - Getting Started - 4 Lambert Cube</title>
<script type="module" src="/dist/examples/gettingstarted/5_reflectiveCube/index.js"></script>
</head>
<body></body>
</html>
5 changes: 3 additions & 2 deletions src/examples/gettingstarted/5_reflectiveCube/fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ precision highp float;

uniform sampler2D map;
uniform vec3 viewLightPosition;
uniform samplerCube cubeMap;

varying vec3 v_viewPosition;
varying vec3 v_viewNormal;
Expand All @@ -13,7 +14,7 @@ void main() {

vec3 directionToLight = normalize( viewLightPosition - v_viewPosition );
float lambertianIntensity = dot( directionToLight, v_viewNormal );

gl_FragColor = vec4( albedo * lambertianIntensity, 1.0);
vec3 cubeIntensity = textureCube(cubeMap, normalize(v_viewNormal)).rgb;
gl_FragColor = vec4( cubeIntensity, 1.0);

}
17 changes: 9 additions & 8 deletions src/examples/gettingstarted/5_reflectiveCube/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { box } from "../../../lib/geometry/primitives/Box";
import { tetrahedron } from "../../../lib/geometry/primitives/Octaehdron";
import { fetchImage } from "../../../lib/io/loaders/Image";
import { ShaderMaterial } from "../../../lib/materials/ShaderMaterial";
import { Euler } from "../../../lib/math/Euler";
Expand All @@ -15,16 +15,17 @@ import fragmentSourceCode from "./fragment.glsl";
import vertexSourceCode from "./vertex.glsl";

async function init(): Promise<null> {
const geometry = box(0.75, 0.75, 0.75);
const geometry = tetrahedron(0.35, 0);
console.log(geometry);
const material = new ShaderMaterial(vertexSourceCode, fragmentSourceCode);
const texture = new Texture(await fetchImage("/assets/textures/uv_grid_opengl.jpg"));
const cubeTexture = new CubeTexture([
await fetchImage("/assets/textures/cube/pisa/px.jpg"),
await fetchImage("/assets/textures/cube/pisa/nx.jpg"),
await fetchImage("/assets/textures/cube/pisa/py.jpg"),
await fetchImage("/assets/textures/cube/pisa/ny.jpg"),
await fetchImage("/assets/textures/cube/pisa/pz.jpg"),
await fetchImage("/assets/textures/cube/pisa/nz.jpg"),
await fetchImage("/assets/textures/cube/pisa/px.png"),
await fetchImage("/assets/textures/cube/pisa/nx.png"),
await fetchImage("/assets/textures/cube/pisa/py.png"),
await fetchImage("/assets/textures/cube/pisa/ny.png"),
await fetchImage("/assets/textures/cube/pisa/pz.png"),
await fetchImage("/assets/textures/cube/pisa/nz.png"),
]);

const context = new RenderingContext();
Expand Down
82 changes: 82 additions & 0 deletions src/lib/geometry/Normals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Vector3Array } from "../math/arrays/Vector3Array";
import { Vector3 } from "../math/Vector3";
import { Float32Attribute } from "./Attribute";
import { Geometry } from "./Geometry";

export function computeVertexNormals(geometry: Geometry): void {
const indicesAttribute = geometry.indices;
const attributes = geometry.attributes;

const positionAttribute = attributes["position"];
if (positionAttribute === undefined) {
throw new Error("missing position attribute");
}
let normalAttribute = attributes["normal"];
if (normalAttribute === undefined) {
normalAttribute = new Float32Attribute(new Float32Array(positionAttribute.count * 3), 3);
geometry.attributes["normal"] = normalAttribute;
}

// reset existing normals to zero

const positions = new Vector3Array(positionAttribute.attributeData.arrayBuffer);
const normals = new Vector3Array(normalAttribute.attributeData.arrayBuffer);

for (let i = 0, il = normals.count; i < il; i++) {
normals.set(i, new Vector3());
}

const pA = new Vector3();
const pB = new Vector3();
const pC = new Vector3();
const cb = new Vector3();
const ab = new Vector3();

// indexed elements

if (indicesAttribute !== undefined) {
const indices = new Uint32Array(indicesAttribute.attributeData.arrayBuffer);

const v = new Vector3();

for (let i = 0, il = indices.length; i < il; i += 3) {
const vA = indices[i + 0];
const vB = indices[i + 1];
const vC = indices[i + 2];

positions.get(vA, pA);
positions.get(vB, pB);
positions.get(vC, pC);

cb.copy(pC).sub(pB);
ab.copy(pA).sub(pB);
cb.cross(ab);

normals.add(vA, cb);
normals.add(vB, cb);
normals.add(vC, cb);
}
} else {
// non-indexed elements (unconnected triangle soup)
const v = new Vector3();

for (let i = 0, il = positions.count; i < il; i += 3) {
positions.get(i, pA);
positions.get(i + 1, pB);
positions.get(i + 2, pC);

cb.copy(pC).sub(pB);
ab.copy(pA).sub(pB);
cb.cross(ab);

normals.add(i, cb);
normals.add(i + 1, cb);
normals.add(i + 2, cb);
}
}

const v = new Vector3();
for (let i = 0, il = normals.count; i < il; i += 3) {
normals.set(i, normals.get(i, v).normalize());
}
}
Loading

0 comments on commit 9c86560

Please sign in to comment.