Kray3 SDK/Defining materials

From Kray
Jump to: navigation, search

Basic material attributes

We start this tutorial with simple mesh scene from previous tutorial


<include highlight="kray3" src="/pub/kray3_script/mesh/box_mesh.kray"></include>{{#ifeq: 1|1|

Run in KrayLive|}}

In this example there is no material definition at all. The box we see is made off default material.

Lets change it.

<syntaxhighlight> // material definition material exampleMaterial; materialSet exampleMaterial,color,(1,0,0); </syntaxhighlight>

Above lines create new material and changes its default color to red (1,0,0)

To make it work we need to attach material to a mesh tag. A mesh tag is an integer attached to every polygon. Its second parameter of meshAddPoly function. In our example...

<syntaxhighlight> // polygons meshAddPoly boxMesh,0,{0,2,6,4}; meshAddPoly boxMesh,0,{5,7,3,1}; meshAddPoly boxMesh,0,{1,3,2,0}; meshAddPoly boxMesh,0,{4,6,7,5}; meshAddPoly boxMesh,0,{0,4,5,1}; meshAddPoly boxMesh,0,{3,7,6,2}; </syntaxhighlight>

... all polygons have mesh tag 0.

To connect material with mesh tag 0 of our mesh we do:

<syntaxhighlight> // attach material to mesh tags meshOp boxMesh,tag,0,material,exampleMaterial; </syntaxhighlight>

Of course we can have multiple materials and many mesh tags. Example:


<include highlight="kray3" src="/pub/kray3_script/mesh/box_mesh_colored.kray"></include>{{#ifeq: 1|1|

Run in KrayLive|}}

Ok. Lets go back to single material example to show how to bind a texture to a mesh.


<include highlight="kray3" src="/pub/kray3_script/mesh/box_mesh_point_uv.kray"></include>{{#ifeq: 1|1|

Run in KrayLive|}}

Example above connects UV coordinates to mesh vertices. This way if two polygons share a vertex they must also share UV coord. There is a way to remove this limitation. Its called poly vertex map in Kray. Poly vertex map attaches UV coord independently from mesh topography. For a box mesh in this example which have 6 polygons, 4 vertex each we need 6*4=24 UV coordinates.


<include highlight="kray3" src="/pub/kray3_script/mesh/box_mesh_poly_uv.kray"></include>{{#ifeq: 1|1|

Run in KrayLive|}}

In examples above texture is applied to color (diffuse) channel. Its not the only option. Texture can modify other surface parameters like reflection for example. To do so we must replace <syntaxhighlight> materialSet exampleMaterial,texture,diffuse,exampleTexture; </syntaxhighlight> with <syntaxhighlight> materialSet exampleMaterial,texture,reflection,exampleTexture; </syntaxhighlight>

we also need to dim diffuse part of material and set maximum reflection level (and keep total amount of specular and diffuse reflection below 1, so it does not reflect more energy then it gets)

<syntaxhighlight> materialSet exampleMaterial,diffuse,0.3; materialSet exampleMaterial,reflection,0.7; </syntaxhighlight>

Full list of available material parameters you can get by typing help materialSet in Kray interactive mode. All available texture parameters can be listed with help metaTextureSet.