>> In this video, Explorer Hello World sample for StereoKit.
If you're unfamiliar with the steps
to set up a StereoKit Project,
refer to the project setup videos for
guide and instruction on project setup.
For this video, we'll be using
a StereoKit Universal Windows Platform template.
In the Solution Explorer,
there's an assets folder in a program.cs script.
The assets folder is where you can
store assets such as model files.
The program.cs script contains the application logic.
The sample provided displays a rounded cube
that can be grabbed and moved with your hands.
When viewing the app, if you
cannot see the real world around you,
such as a VR experience or viewing on
a PC screen if floor is provided below the cube.
StereoKit automatically provides you with
hands and environment and lighting.
Let's explore the logic that is used to generate this cube.
The code first starts by initializing StereoKit.
During initialization, you can prepare
a few settings like the appName and assetsFolder.
The appName is the name of the app and appears at the top of
the Win 32 window if you are in your app and flat-screen mode.
In addition, the appName is also submitted to OpenXR,
which caps at 128 characters.
The assetsFolder is a folder that
StereoKit will use to look for assets.
You can refer to the reference documentation
for additional settings.
Next, the pose or location of the RoundedCube is set.
The pose is named cubePose and is position at zero on the x-axis,
zero on the y-axis,
and negative 0.5 meters on the z-axis with no rotation.
Next, the RoundedCube is created as
an instance of the model class and is named cube.
A model is a collection of meshes,
materials, and transforms that make up a visual element.
This is a great way to group together complex
objects that have multiple parts in them.
In fact, most model formats are composed this way already.
The model class contains a number of methods for creation.
If you pass in an OBJ,
STL, gITF, or GLB file format,
StereoKit will load that model from file and
assemble materials and transforms from the file information,
but you can also assemble a model
from procedurally generated meshes.
Next, we use the FromMesh method.
The FromMesh method is used to create
a single mesh instance of
the model class using the indicated mesh or material.
A mesh is a single collection of triangular faces
with extra surface information to enhance rendering.
StereoKit meshes are composed of a list of
vertices and a list of indices to connect the vertices into faces.
Nothing more than that is stored here.
Typically, meshes are combined with
materials or added some models in order to draw them.
A material describes a surface of
anything drawn on the graphics card.
It is typically composed of
a Shader and Shader properties like colors,
textures, transparency info, etc.
For the mesh parameter,
the GenerateRoundedCube method is used which returns
a cube mesh with rounded corners sized to the given dimensions.
The GenerateRoundedCube method takes
three parameters: dimension,
edge radius, and subdivisions.
Dimension is of the type Vec3 and
defines the size of the cube on each axis in meters.
For this example, dimension is set to Vec1 times 0.01.
Vec1 is shorthand for a vector where all values are 1.
The edge radius parameter is of
the type flute and defines
the radius of corner rounding in meters.
For this example, edge radius is set to 0.02.
As further FromMesh material parameter,
this parameter is set to Default.Material.
StereoKit has a collection of default assets.
For this example, the default for material is used.
Next, a floor is created,
which displays that there is another view into the real world.
As mentioned, this will occur if you're
viewing the app in an immersive environment,
such as a VR device or on a PC screen.
A floorTransform is created using the TS method,
which creates a transform matrix using both translate and scale.
A floorMaterial is also created,
which leaves a shader from the assetsFolder named floor.hlsl.
A shader is a piece of code that runs on the GPU and
determines how model data gets
transformed into pixels on the screen.
You can provide your own shader code if you need
finer control over how models are rendered.
Finally, the floorMaterial.Transparency is set to blend.
This marks the rendering material is having
a transparent surface that can blend with the content behind it.
This allows you to see through the empty parts of the floor grid,
leaving only the grid lines as visible.
Here in the while statement,
the code draws a model every step of the application.
The application steps each frame
until the StereoKit is told to exit.
The callback code is called every frame after input
and system events but before drawing events.
There's a conditional statement provided to
determine whether the floor displays.
Essentially if the display type is opaque,
which means there's no view into the real world,
then the mesh cube is drawn using
the floor material in floorTransform that was created above.
Next, a handle is created,
which enables you to grab and move objects.
The handle method takes three parameters: ID,
Pose, and Handle.
The ID parameter is of type string
and defines the ID of the Handel Group.
For this example, the ID is Cube.
The pose parameter is of type pose
n and defines the pose state for the handle.
The user will be able to grab this handle and move it around.
For this example, the cubePose is use.
Finally, the handle parameter is of type bounds
and defines the size and location of
the handle relative to the pose.
For this example, the value is set to the bounds of the cube.
The draw method adds the cube model
to the render queue for the current frame.
This method takes one parameter transform,
which is uptight matrix.
This defines a matrix that will transform the model
from model space into the current hierarchy space.
In this example, the value for cubePose is used and
converted into a transform matrix using ToMatrix.
Finally, we can clean up StereoKit
and all its resources using the Shutdown method.
To try out the app, let's build and deploy to HoloLens 2.
If you're unfamiliar with how to build and deploy,
refer to the project setup and deploy video for HoloLens.
After deployment is complete,
the app will start in your device.
You can grab and move the cube directly with
your hands using a pinching motion.
If instead, you choose to
create this project for desktop development,
you'll be able to view the app on
your PC screen and use your mouse to grab and move the cube.
To learn more about StereoKit, visit aka.ms/StereoKit.