<< jrManでblend shaderをつくる その2 戻る jrMan shaderでmix()をつかう その2 >>

jrMan shaderでmix()をつかう その1

javaファイルを見ながら、地道に実験してみる。Essential RenderManを参考にして、mixtest shaderを作ってみた。RenderManシェーダの基本ですね。jrManの中でmix()がどうなっているか、src folderのFloatGrid.javaに実装されていました。
mixtest.jpg

/*
SurfaceMixtest.java
*/

import javax.vecmath.Color3f;

import org.jrman.grid.FloatGrid;
import org.jrman.grid.Color3fGrid;
import org.jrman.grid.Vector3fGrid;
import org.jrman.parameters.Declaration;
import org.jrman.parameters.UniformScalarFloat;
import org.jrman.parameters.UniformScalarTuple3f;
import org.jrman.render.ShaderVariables;
import org.jrman.shaders.SurfaceShader;

public class SurfaceMixtest extends SurfaceShader {

private static Vector3fGrid vg1 = new Vector3fGrid();

private static Vector3fGrid vg2 = new Vector3fGrid();

private static FloatGrid fg1 = new FloatGrid();

private static Color3fGrid cg1 = new Color3fGrid();

private static Color3fGrid cg2 = new Color3fGrid();

private static Color3fGrid cg3 = new Color3fGrid();

private static Color3fGrid ct1 = new Color3fGrid();

private static Color3f specularcolor = new Color3f();

private static Color3f color1 = new Color3f();

private static Color3f WHITE = new Color3f(1f, 1f, 1f);

protected void initDefaults() {
defaultParameters.addParameter(
new UniformScalarFloat(new Declaration("Ka", "uniform float"), 1f));
defaultParameters.addParameter(
new UniformScalarFloat(new Declaration("Kd", "uniform float"), .5f));
defaultParameters.addParameter(
new UniformScalarFloat(new Declaration("Ks", "uniform float"), .5f));
defaultParameters.addParameter(
new UniformScalarFloat(new Declaration("roughness", "uniform float"), .1f));
defaultParameters.addParameter(
new UniformScalarTuple3f(
new Declaration("specularcolor", "uniform color"),
1f,
1f,
1f));
defaultParameters.addParameter(
new UniformScalarTuple3f(
new Declaration("color1", "uniform color"),
0f,
1f,
0f));
}

public void shade(ShaderVariables sv) {
super.shade(sv);
UniformScalarFloat paramKa = (UniformScalarFloat) getParameter(sv, "Ka");
final float Ka = paramKa.getValue();
UniformScalarFloat paramKd = (UniformScalarFloat) getParameter(sv, "Kd");
final float Kd = paramKd.getValue();
UniformScalarFloat paramKs = (UniformScalarFloat) getParameter(sv, "Ks");
final float Ks = paramKs.getValue();
UniformScalarFloat paramRoughness = (UniformScalarFloat) getParameter(sv, "roughness");
final float roughness = paramRoughness.getValue();
UniformScalarTuple3f paramSpecularcolor =
(UniformScalarTuple3f) getParameter(sv, "specularcolor");
paramSpecularcolor.getValue(specularcolor);
UniformScalarTuple3f paramColor1 =
(UniformScalarTuple3f) getParameter(sv, "color1");
paramColor1.getValue(color1);

vg1.normalize(sv.N);
vg1.faceforward(vg1, sv.I);
vg2.normalize(sv.I);
vg2.negate(vg2);

fg1.set(sv.t);

cg3.set(color1);

sv.Cs.mix(sv.Cs, cg3, fg1);

ct1.set(sv.Cs);

sv.Oi.set(sv.Os);
ambient(sv, cg1);
cg3.set(Ka);
cg1.mul(cg1, cg3);
diffuse(sv, vg1, cg2);
cg3.set(Kd);
cg2.mul(cg2, cg3);
cg1.add(cg1, cg2);
specular(sv, vg1, vg2, roughness, cg2);
cg3.set(Ks);
cg2.mul(cg2, cg3);
cg3.set(specularcolor);
cg2.mul(cg2, cg3);
cg1.add(cg1, cg2);
cg1.mul(cg1, ct1);
sv.Ci.mul(sv.Os, cg1);
}

}


RIBは以下のようになります。tを使いました。

#mixtest.rib
Display "mixtest.png" "framebuffer" "rgb"
Format 640 480 1
Projection "perspective" "fov" [45]
LightSource "ambientlight" 1 "intensity" [0.2]
LightSource "spotlight" 2 "from" [-1 1 0 ] "to" [0 0 3] "intensity" [10]
Translate 0 0 3
WorldBegin
Color [1 0 0]
Surface "mixtest"
Rotate 90 1 0 0
#Rotate 180 1 0 0
#Patch "bilinear" "P" [-1 -1 0 1 -1 0 -1 1 0 1 1 0]
Sphere 1 -1 1 360
WorldEnd
  • -
  • -

<< jrManでblend shaderをつくる その2 戻る jrMan shaderでmix()をつかう その2 >>