Skip to content

Commit f4c8620

Browse files
committedJun 21, 2020
feat: interleaved buffers example (not yet working)
1 parent fe002be commit f4c8620

File tree

5 files changed

+33
-75
lines changed

5 files changed

+33
-75
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
precision highp float;
22

3-
uniform samplerCube cubeMap;
4-
5-
varying vec3 v_viewPosition;
6-
varying vec3 v_viewNormal;
3+
varying vec3 v_color;
74

85
void main() {
96

10-
vec3 reflectDir = reflect( normalize( v_viewPosition ),normalize(v_viewNormal) );
11-
gl_FragColor = textureCube(cubeMap, reflectDir);
7+
gl_FragColor = vec4(v_color, 1.0);
128

139
}
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,29 @@
1-
import { icosahedron } from "../../../lib/geometry/primitives/Polyhedrons";
2-
import { fetchImage } from "../../../lib/io/loaders/Image";
1+
import { Float32Attribute } from "../../../lib/geometry/Attribute";
2+
import { Geometry } from "../../../lib/geometry/Geometry";
3+
import { convertToInterleavedGeometry } from "../../../lib/geometry/Geometry.Functions";
34
import { ShaderMaterial } from "../../../lib/materials/ShaderMaterial";
4-
import { Euler } from "../../../lib/math/Euler";
5-
import { Matrix4 } from "../../../lib/math/Matrix4";
6-
import {
7-
makeMatrix4Perspective,
8-
makeMatrix4RotationFromEuler,
9-
makeMatrix4Translation,
10-
} from "../../../lib/math/Matrix4.Functions";
11-
import { Vector3 } from "../../../lib/math/Vector3";
125
import { BufferGeometry } from "../../../lib/renderers/webgl2/buffers/BufferGeometry";
13-
import { DepthTestFunc, DepthTestState } from "../../../lib/renderers/webgl2/DepthTestState";
146
import { Program } from "../../../lib/renderers/webgl2/programs/Program";
157
import { RenderingContext } from "../../../lib/renderers/webgl2/RenderingContext";
16-
import { TexImage2D } from "../../../lib/renderers/webgl2/textures/TexImage2D";
17-
import { CubeTexture } from "../../../lib/textures/CubeTexture";
188
import fragmentSourceCode from "./fragment.glsl";
199
import vertexSourceCode from "./vertex.glsl";
2010

21-
async function init(): Promise<null> {
22-
const geometry = icosahedron(0.75, 3);
23-
const material = new ShaderMaterial(vertexSourceCode, fragmentSourceCode);
24-
const cubeTexture = new CubeTexture([
25-
await fetchImage("/assets/textures/cube/pisa/px.png"),
26-
await fetchImage("/assets/textures/cube/pisa/nx.png"),
27-
await fetchImage("/assets/textures/cube/pisa/py.png"),
28-
await fetchImage("/assets/textures/cube/pisa/ny.png"),
29-
await fetchImage("/assets/textures/cube/pisa/pz.png"),
30-
await fetchImage("/assets/textures/cube/pisa/nz.png"),
31-
]);
11+
let geometry = new Geometry();
12+
geometry.attributes["position"] = new Float32Attribute([0, 0.5, 0.5, -0.5, -0.5, -0.5], 2);
13+
geometry.attributes["color"] = new Float32Attribute([1, 0, 0, 0, 1, 0, 0, 0, 1], 3);
3214

33-
const context = new RenderingContext();
34-
const canvasFramebuffer = context.canvasFramebuffer;
35-
document.body.appendChild(canvasFramebuffer.canvas);
15+
console.log("geometry", geometry);
16+
geometry = convertToInterleavedGeometry(geometry);
17+
console.log("interleaved", geometry);
3618

37-
const program = new Program(context, material);
38-
const uniforms = {
39-
localToWorld: new Matrix4(),
40-
worldToView: makeMatrix4Translation(new Matrix4(), new Vector3(0, 0, -1)),
41-
viewToScreen: makeMatrix4Perspective(new Matrix4(), -0.25, 0.25, 0.25, -0.25, 0.1, 4.0),
42-
cubeMap: new TexImage2D(context, cubeTexture),
43-
};
44-
const bufferGeometry = new BufferGeometry(context, geometry);
45-
const depthTestState = new DepthTestState(true, DepthTestFunc.Less);
19+
const material = new ShaderMaterial(vertexSourceCode, fragmentSourceCode);
4620

47-
function animate(): void {
48-
requestAnimationFrame(animate);
21+
const context = new RenderingContext();
22+
const canvasFramebuffer = context.canvasFramebuffer;
23+
document.body.appendChild(canvasFramebuffer.canvas);
4924

50-
const now = Date.now();
51-
uniforms.localToWorld = makeMatrix4RotationFromEuler(
52-
uniforms.localToWorld,
53-
new Euler(now * 0.0001, now * 0.00033, now * 0.000077),
54-
);
55-
canvasFramebuffer.renderBufferGeometry(program, uniforms, bufferGeometry, depthTestState);
56-
}
25+
const bufferGeometry = new BufferGeometry(context, geometry);
26+
const program = new Program(context, material);
27+
const uniforms = {};
5728

58-
animate();
59-
60-
return null;
61-
}
62-
63-
init();
29+
canvasFramebuffer.renderBufferGeometry(program, uniforms, bufferGeometry);
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
attribute vec3 position;
2-
attribute vec3 normal;
1+
attribute vec2 position;
2+
attribute vec3 color;
33

4-
varying vec3 v_viewPosition;
5-
varying vec3 v_viewNormal;
6-
7-
uniform mat4 localToWorld;
8-
uniform mat4 worldToView;
9-
uniform mat4 viewToScreen;
4+
varying vec3 v_color;
105

116
void main() {
127

13-
v_viewNormal = normalize( ( worldToView * localToWorld * vec4( normal, 0.0 ) ).xyz );
14-
v_viewPosition = ( worldToView * localToWorld * vec4( position, 1.0 ) ).xyz;
15-
gl_Position = viewToScreen * vec4( v_viewPosition, 1.0 );
8+
v_color = color;
9+
10+
gl_Position = vec4( position.x, position.y, 0.5, 1.0 );
1611

1712
}

‎src/lib/geometry/Attribute.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class Attribute {
2424
if (this.vertexStride < 0) {
2525
this.vertexStride = this.bytesPerVertex;
2626
}
27-
this.count = this.attributeData.arrayBuffer.byteLength / this.bytesPerVertex;
27+
this.count = this.attributeData.arrayBuffer.byteLength / this.vertexStride;
2828
// this.minExtent = minExtent;
2929
// this.maxExtent = maxExtent;
3030
}

‎src/lib/geometry/Geometry.Functions.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ function copyBytesUsingStride(
1717
for (let v = 0; v < vertexCount; v++) {
1818
const sourceOffset = v * bytesPerVertex;
1919
const destOffset = v * byteStridePerVertex + attributeOffset;
20+
console.log(sourceOffset, "->", destOffset, "length: ", bytesPerVertex);
2021
for (let i = 0; i < bytesPerVertex; i++) {
2122
destBytes[destOffset + i] = sourceBytes[sourceOffset + i];
2223
}
2324
}
2425
}
25-
export function convertToInterleavedBuffer(geometry: Geometry): Geometry {
26+
export function convertToInterleavedGeometry(geometry: Geometry): Geometry {
2627
let byteStridePerVertex = 0;
2728
let vertexCount = 0;
2829
for (const name in geometry.attributes) {
@@ -33,7 +34,7 @@ export function convertToInterleavedBuffer(geometry: Geometry): Geometry {
3334
}
3435
}
3536
const interleavedArray = new ArrayBuffer(byteStridePerVertex * vertexCount);
36-
const interleavedData = new AttributeData(interleavedArray, 0, -1, byteStridePerVertex);
37+
const interleavedData = new AttributeData(interleavedArray);
3738

3839
const interleavedGeometry = new Geometry();
3940
interleavedGeometry.indices = geometry.indices;
@@ -52,15 +53,15 @@ export function convertToInterleavedBuffer(geometry: Geometry): Geometry {
5253

5354
interleavedGeometry.attributes[name] = new Attribute(
5455
interleavedData,
55-
byteOffset,
56-
attribute.componentType,
5756
attribute.componentsPerVertex,
58-
vertexCount,
57+
attribute.componentType,
58+
byteStridePerVertex,
59+
byteOffset,
5960
);
6061
byteOffset += attribute.bytesPerVertex;
6162
}
6263
}
63-
return geometry;
64+
return interleavedGeometry;
6465
}
6566

6667
export function computeVertexNormals(geometry: Geometry): void {

0 commit comments

Comments
 (0)
Please sign in to comment.