-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: standalone and simplier device orientation example
- Loading branch information
Showing
5 changed files
with
121 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"slug": "interaction_device_orientation", | ||
"en": { | ||
"name": "Device Orientation", | ||
"description": "Click once on the screen to active. The cube moves based on your devices orientation.", | ||
"keywords": ["interaction", "deviceorientation", "orientation"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
precision highp float; | ||
|
||
varying vec3 v_viewPosition; | ||
varying vec3 v_viewNormal; | ||
varying vec2 v_uv; | ||
|
||
uniform sampler2D map; | ||
uniform vec3 viewLightPosition; | ||
|
||
void main() { | ||
|
||
vec3 albedo = texture2D(map, v_uv).xyz; | ||
vec3 directionToLight = normalize( viewLightPosition - v_viewPosition ); | ||
float lambertianIntensity = dot( directionToLight, v_viewNormal ); | ||
|
||
gl_FragColor = vec4( albedo * lambertianIntensity, 1. ); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { DeviceOrientation } from "../../../lib/controllers/DeviceOrientation"; | ||
import { boxGeometry } from "../../../lib/geometry/primitives/boxGeometry"; | ||
import { ShaderMaterial } from "../../../lib/materials/ShaderMaterial"; | ||
import { Matrix4 } from "../../../lib/math/Matrix4"; | ||
import { | ||
makeMatrix4Inverse, | ||
makeMatrix4Perspective, | ||
makeMatrix4RotationFromQuaternion, | ||
makeMatrix4Translation, | ||
} from "../../../lib/math/Matrix4.Functions"; | ||
import { Vector3 } from "../../../lib/math/Vector3"; | ||
import { makeBufferGeometryFromGeometry } from "../../../lib/renderers/webgl/buffers/BufferGeometry"; | ||
import { DepthTestFunc, DepthTestState } from "../../../lib/renderers/webgl/DepthTestState"; | ||
import { makeProgramFromShaderMaterial } from "../../../lib/renderers/webgl/programs/Program"; | ||
import { RenderingContext } from "../../../lib/renderers/webgl/RenderingContext"; | ||
import { makeTexImage2DFromTexture } from "../../../lib/renderers/webgl/textures/TexImage2D"; | ||
import { fetchImage } from "../../../lib/textures/loaders/Image"; | ||
import { Texture } from "../../../lib/textures/Texture"; | ||
import fragmentSource from "./fragment.glsl"; | ||
import vertexSource from "./vertex.glsl"; | ||
|
||
async function init(): Promise<null> { | ||
const geometry = boxGeometry(0.75, 0.75, 0.75); | ||
const material = new ShaderMaterial(vertexSource, fragmentSource); | ||
const texture = new Texture(await fetchImage("/assets/textures/uv_grid_opengl.jpg")); | ||
|
||
const context = new RenderingContext(document.getElementById("framebuffer") as HTMLCanvasElement); | ||
const canvasFramebuffer = context.canvasFramebuffer; | ||
window.addEventListener("resize", () => canvasFramebuffer.resize()); | ||
|
||
const program = makeProgramFromShaderMaterial(context, material); | ||
const uniforms = { | ||
localToWorld: new Matrix4(), | ||
worldToView: makeMatrix4Translation(new Vector3(0, 0, -1)), | ||
viewToScreen: makeMatrix4Perspective(-0.25, 0.25, 0.25, -0.25, 0.1, 4.0), | ||
viewLightPosition: new Vector3(0, 0, 0), | ||
map: makeTexImage2DFromTexture(context, texture), | ||
}; | ||
const bufferGeometry = makeBufferGeometryFromGeometry(context, geometry); | ||
const depthTestState = new DepthTestState(true, DepthTestFunc.Less); | ||
|
||
let deviceOrientation: DeviceOrientation | undefined = undefined; | ||
|
||
const body = document.getElementsByTagName("body")[0]; | ||
body.addEventListener( | ||
"click", | ||
() => { | ||
if (deviceOrientation === undefined) { | ||
deviceOrientation = new DeviceOrientation(); | ||
} | ||
}, | ||
false, | ||
); | ||
|
||
function animate(): void { | ||
requestAnimationFrame(animate); | ||
|
||
if (deviceOrientation !== undefined) { | ||
uniforms.localToWorld = makeMatrix4Inverse(makeMatrix4RotationFromQuaternion(deviceOrientation.orientation)); | ||
} | ||
canvasFramebuffer.renderBufferGeometry(program, uniforms, bufferGeometry, depthTestState); | ||
} | ||
|
||
animate(); | ||
|
||
return null; | ||
} | ||
|
||
init(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
attribute vec3 position; | ||
attribute vec3 normal; | ||
attribute vec2 uv; | ||
|
||
uniform mat4 localToWorld; | ||
uniform mat4 worldToView; | ||
uniform mat4 viewToScreen; | ||
|
||
varying vec3 v_viewPosition; | ||
varying vec3 v_viewNormal; | ||
varying vec2 v_uv; | ||
|
||
void main() { | ||
|
||
v_viewNormal = normalize( ( worldToView * localToWorld * vec4( normal, 0. ) ).xyz ); | ||
v_viewPosition = ( worldToView * localToWorld * vec4( position, 1. ) ).xyz; | ||
v_uv = uv; | ||
gl_Position = viewToScreen * vec4( v_viewPosition, 1. ); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters