Exemplo n.º 1
0
        public void prepareShadingState(ShadingState state)
        {
            state.init();
            Instance i = state.getInstance();

            state.getRay().getPoint(state.getPoint());
            Ray     r = state.getRay();
            IShader s = i.getShader(0);

            state.setShader(s != null ? s : this);
            int primID = state.getPrimitiveID();
            int hair   = primID / numSegments;
            int line   = primID % numSegments;
            int vRoot  = hair * 3 * (numSegments + 1);
            int v0     = vRoot + line * 3;

            // tangent vector
            Vector3 v = getTangent(line, v0, state.getV());

            v = state.transformVectorObjectToWorld(v);
            state.setBasis(OrthoNormalBasis.makeFromWV(v, new Vector3(-r.dx, -r.dy, -r.dz)));
            state.getBasis().swapVW();
            // normal
            state.getNormal().set(0, 0, 1);
            state.getBasis().transform(state.getNormal());
            state.getGeoNormal().set(state.getNormal());

            state.getUV().set(0, (line + state.getV()) / numSegments);
        }
Exemplo n.º 2
0
        public void prepareShadingState(ShadingState state)
        {
            state.init();
            state.getRay().getPoint(state.getPoint());
            Instance parent     = state.getInstance();
            Point3   localPoint = state.transformWorldToObject(state.getPoint());

            state.getNormal().set(localPoint.x, localPoint.y, 0);
            state.getNormal().normalize();

            float phi = (float)Math.Atan2(state.getNormal().y, state.getNormal().x);

            if (phi < 0)
            {
                phi += (float)(2.0 * Math.PI);
            }
            state.getUV().x = phi / (float)(2 * Math.PI);
            state.getUV().y = (localPoint.z + 1) * 0.5f;
            state.setShader(parent.getShader(0));
            state.setModifier(parent.getModifier(0));
            // into world space
            Vector3 worldNormal = state.transformNormalObjectToWorld(state.getNormal());
            Vector3 v           = state.transformVectorObjectToWorld(new Vector3(0, 0, 1));

            state.getNormal().set(worldNormal);
            state.getNormal().normalize();
            state.getGeoNormal().set(state.getNormal());
            // compute basis in world space
            state.setBasis(OrthoNormalBasis.makeFromWV(state.getNormal(), v));
        }
Exemplo n.º 3
0
        private bool updateCameraMatrix(int index, ParameterList pl)
        {
            string offset = index < 0 ? "" : string.Format("[{0}]", index);

            if (index < 0)
            {
                index = 0;
            }
            Matrix4 transform = pl.getMatrix(string.Format("transform{0}", offset), null);

            if (transform == null)
            {
                // no transform was specified, check eye/target/up
                Point3  eye    = pl.getPoint(string.Format("eye{0}", offset), null);
                Point3  target = pl.getPoint(string.Format("target{0}", offset), null);
                Vector3 up     = pl.getVector(string.Format("up{0}", offset), null);
                if (eye != null && target != null && up != null)
                {
                    c2w[index] = Matrix4.fromBasis(OrthoNormalBasis.makeFromWV(Point3.sub(eye, target, new Vector3()), up));
                    c2w[index] = Matrix4.translation(eye.x, eye.y, eye.z).multiply(c2w[index]);
                }
                else
                {
                    // the matrix for this index was not specified
                    // return an error, unless this is a regular update
                    return(offset.Length == 0);
                }
            }
            else
            {
                c2w[index] = transform;
            }
            return(true);
        }
Exemplo n.º 4
0
 public SunSkyLight()
 {
     numSkySamples = 64;
     sunDirWorld   = new Vector3(1, 1, 1);
     turbidity     = 6;
     basis         = OrthoNormalBasis.makeFromWV(new Vector3(0, 0, 1), new Vector3(0, 1, 0));
     initSunSky();
 }
Exemplo n.º 5
0
 private void updateBasis(Vector3 center, Vector3 up)
 {
     if (center != null && up != null)
     {
         basis = OrthoNormalBasis.makeFromWV(center, up);
         basis.swapWU();
         basis.flipV();
     }
 }
Exemplo n.º 6
0
 public SunSkyLight()
 {
     numSkySamples   = 64;
     sunDirWorld     = new Vector3(1, 1, 1);
     turbidity       = 6;
     basis           = OrthoNormalBasis.makeFromWV(new Vector3(0, 0, 1), new Vector3(0, 1, 0));
     groundExtendSky = true;
     groundColor     = Color.BLACK;
     initSunSky();
 }
Exemplo n.º 7
0
        public bool update(ParameterList pl, SunflowAPI api)
        {
            Vector3 up   = pl.getVector("up", null);
            Vector3 east = pl.getVector("east", null);

            if (up != null && east != null)
            {
                basis = OrthoNormalBasis.makeFromWV(up, east);
            }
            else if (up != null)
            {
                basis = OrthoNormalBasis.makeFromW(up);
            }
            numSkySamples = pl.getInt("samples", numSkySamples);
            sunDirWorld   = pl.getVector("sundir", sunDirWorld);
            turbidity     = pl.getFloat("turbidity", turbidity);
            // recompute model
            initSunSky();
            return(true);
        }
Exemplo n.º 8
0
        public void prepareShadingState(ShadingState state)
        {
            state.init();
            state.getRay().getPoint(state.getPoint());
            Instance parent     = state.getInstance();
            Point3   localPoint = state.transformWorldToObject(state.getPoint());

            float cx = state.getU();
            float cy = state.getV();
            float cz = state.getW();

            state.getNormal().set(localPoint.x - cx, localPoint.y - cy, localPoint.z - cz);
            state.getNormal().normalize();

            float phi = (float)Math.Atan2(state.getNormal().y, state.getNormal().x);

            if (phi < 0)
            {
                phi += (float)(2.0 * Math.PI);
            }
            float theta = (float)Math.Acos(state.getNormal().z);

            state.getUV().y = theta / (float)Math.PI;
            state.getUV().x = phi / (float)(2 * Math.PI);
            Vector3 v       = new Vector3();

            v.x = -2 * (float)Math.PI * state.getNormal().y;
            v.y = 2 * (float)Math.PI * state.getNormal().x;
            v.z = 0;
            state.setShader(parent.getShader(0));
            state.setModifier(parent.getModifier(0));
            // into world space
            Vector3 worldNormal = state.transformNormalObjectToWorld(state.getNormal());

            v = state.transformVectorObjectToWorld(v);
            state.getNormal().set(worldNormal);
            state.getNormal().normalize();
            state.getGeoNormal().set(state.getNormal());
            // compute basis in world space
            state.setBasis(OrthoNormalBasis.makeFromWV(state.getNormal(), v));
        }
Exemplo n.º 9
0
            public void prepareShadingState(ShadingState state)
            {
                state.init();
                Instance parent = state.getInstance();
                int      primID = state.getPrimitiveID();
                float    u      = state.getU();
                float    v      = state.getV();
                float    w      = 1 - u - v;
                // state.getRay().getPoint(state.getPoint());
                int    tri    = 3 * primID;
                int    index0 = triangleMesh.triangles[tri + 0];
                int    index1 = triangleMesh.triangles[tri + 1];
                int    index2 = triangleMesh.triangles[tri + 2];
                Point3 v0p    = triangleMesh.getPoint(index0);
                Point3 v1p    = triangleMesh.getPoint(index1);
                Point3 v2p    = triangleMesh.getPoint(index2);

                // get object space point from barycentric coordinates
                state.getPoint().x = w * v0p.x + u * v1p.x + v * v2p.x;
                state.getPoint().y = w * v0p.y + u * v1p.y + v * v2p.y;
                state.getPoint().z = w * v0p.z + u * v1p.z + v * v2p.z;
                // move into world space
                state.getPoint().set(state.transformObjectToWorld(state.getPoint()));

                Vector3 ng = Point3.normal(v0p, v1p, v2p);

                if (parent != null)
                {
                    ng = state.transformNormalObjectToWorld(ng);
                }
                ng.normalize();
                state.getGeoNormal().set(ng);
                switch (triangleMesh.normals.interp)
                {
                case ParameterList.InterpolationType.NONE:
                case ParameterList.InterpolationType.FACE:
                {
                    state.getNormal().set(ng);
                    break;
                }

                case ParameterList.InterpolationType.VERTEX:
                {
                    int     i30         = 3 * index0;
                    int     i31         = 3 * index1;
                    int     i32         = 3 * index2;
                    float[] normals     = triangleMesh.normals.data;
                    state.getNormal().x = w * normals[i30 + 0] + u * normals[i31 + 0] + v * normals[i32 + 0];
                    state.getNormal().y = w * normals[i30 + 1] + u * normals[i31 + 1] + v * normals[i32 + 1];
                    state.getNormal().z = w * normals[i30 + 2] + u * normals[i31 + 2] + v * normals[i32 + 2];
                    if (parent != null)
                    {
                        state.getNormal().set(state.transformNormalObjectToWorld(state.getNormal()));
                    }
                    state.getNormal().normalize();
                    break;
                }

                case ParameterList.InterpolationType.FACEVARYING:
                {
                    int     idx         = 3 * tri;
                    float[] normals     = triangleMesh.normals.data;
                    state.getNormal().x = w * normals[idx + 0] + u * normals[idx + 3] + v * normals[idx + 6];
                    state.getNormal().y = w * normals[idx + 1] + u * normals[idx + 4] + v * normals[idx + 7];
                    state.getNormal().z = w * normals[idx + 2] + u * normals[idx + 5] + v * normals[idx + 8];
                    if (parent != null)
                    {
                        state.getNormal().set(state.transformNormalObjectToWorld(state.getNormal()));
                    }
                    state.getNormal().normalize();
                    break;
                }
                }
                float uv00 = 0, uv01 = 0, uv10 = 0, uv11 = 0, uv20 = 0, uv21 = 0;

                switch (triangleMesh.uvs.interp)
                {
                case ParameterList.InterpolationType.NONE:
                case ParameterList.InterpolationType.FACE:
                {
                    state.getUV().x = 0;
                    state.getUV().y = 0;
                    break;
                }

                case ParameterList.InterpolationType.VERTEX:
                {
                    int     i20 = 2 * index0;
                    int     i21 = 2 * index1;
                    int     i22 = 2 * index2;
                    float[] uvs = triangleMesh.uvs.data;
                    uv00 = uvs[i20 + 0];
                    uv01 = uvs[i20 + 1];
                    uv10 = uvs[i21 + 0];
                    uv11 = uvs[i21 + 1];
                    uv20 = uvs[i22 + 0];
                    uv21 = uvs[i22 + 1];
                    break;
                }

                case ParameterList.InterpolationType.FACEVARYING:
                {
                    int     idx = tri << 1;
                    float[] uvs = triangleMesh.uvs.data;
                    uv00 = uvs[idx + 0];
                    uv01 = uvs[idx + 1];
                    uv10 = uvs[idx + 2];
                    uv11 = uvs[idx + 3];
                    uv20 = uvs[idx + 4];
                    uv21 = uvs[idx + 5];
                    break;
                }
                }
                if (triangleMesh.uvs.interp != ParameterList.InterpolationType.NONE)
                {
                    // get exact uv coords and compute tangent vectors
                    state.getUV().x = w * uv00 + u * uv10 + v * uv20;
                    state.getUV().y = w * uv01 + u * uv11 + v * uv21;
                    float   du1 = uv00 - uv20;
                    float   du2 = uv10 - uv20;
                    float   dv1 = uv01 - uv21;
                    float   dv2 = uv11 - uv21;
                    Vector3 dp1 = Point3.sub(v0p, v2p, new Vector3()), dp2 = Point3.sub(v1p, v2p, new Vector3());
                    float   determinant = du1 * dv2 - dv1 * du2;
                    if (determinant == 0.0f)
                    {
                        // create basis in world space
                        state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal()));
                    }
                    else
                    {
                        float invdet = 1 / determinant;
                        // Vector3 dpdu = new Vector3();
                        // dpdu.x = (dv2 * dp1.x - dv1 * dp2.x) * invdet;
                        // dpdu.y = (dv2 * dp1.y - dv1 * dp2.y) * invdet;
                        // dpdu.z = (dv2 * dp1.z - dv1 * dp2.z) * invdet;
                        Vector3 dpdv = new Vector3();
                        dpdv.x = (-du2 * dp1.x + du1 * dp2.x) * invdet;
                        dpdv.y = (-du2 * dp1.y + du1 * dp2.y) * invdet;
                        dpdv.z = (-du2 * dp1.z + du1 * dp2.z) * invdet;
                        if (parent != null)
                        {
                            dpdv = state.transformVectorObjectToWorld(dpdv);
                        }
                        // create basis in world space
                        state.setBasis(OrthoNormalBasis.makeFromWV(state.getNormal(), dpdv));
                    }
                }
                else
                {
                    state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal()));
                }
                int shaderIndex = triangleMesh.faceShaders == null ? 0 : (triangleMesh.faceShaders[primID] & 0xFF);

                state.setShader(parent.getShader(shaderIndex));
            }
Exemplo n.º 10
0
        public void prepareShadingState(ShadingState state)
        {
            state.init();
            Instance parent = state.getInstance();
            int      primID = state.getPrimitiveID();
            float    u      = state.getU();
            float    v      = state.getV();

            state.getRay().getPoint(state.getPoint());
            int    quad   = 4 * primID;
            int    index0 = quads[quad + 0];
            int    index1 = quads[quad + 1];
            int    index2 = quads[quad + 2];
            int    index3 = quads[quad + 3];
            Point3 v0p    = getPoint(index0);
            Point3 v1p    = getPoint(index1);
            Point3 v2p    = getPoint(index2);
            Point3 v3p    = getPoint(index3);
            float  tanux  = (1 - v) * (v1p.x - v0p.x) + v * (v2p.x - v3p.x);
            float  tanuy  = (1 - v) * (v1p.y - v0p.y) + v * (v2p.y - v3p.y);
            float  tanuz  = (1 - v) * (v1p.z - v0p.z) + v * (v2p.z - v3p.z);

            float tanvx = (1 - u) * (v3p.x - v0p.x) + u * (v2p.x - v1p.x);
            float tanvy = (1 - u) * (v3p.y - v0p.y) + u * (v2p.y - v1p.y);
            float tanvz = (1 - u) * (v3p.z - v0p.z) + u * (v2p.z - v1p.z);

            float nx = tanuy * tanvz - tanuz * tanvy;
            float ny = tanuz * tanvx - tanux * tanvz;
            float nz = tanux * tanvy - tanuy * tanvx;

            Vector3 ng = new Vector3(nx, ny, nz);

            ng = state.transformNormalObjectToWorld(ng);
            ng.normalize();
            state.getGeoNormal().set(ng);

            float k00 = (1 - u) * (1 - v);
            float k10 = u * (1 - v);
            float k01 = (1 - u) * v;
            float k11 = u * v;

            switch (normals.interp)
            {
            case ParameterList.InterpolationType.NONE:
            case ParameterList.InterpolationType.FACE:
            {
                state.getNormal().set(ng);
                break;
            }

            case ParameterList.InterpolationType.VERTEX:
            {
                int     i30         = 3 * index0;
                int     i31         = 3 * index1;
                int     i32         = 3 * index2;
                int     i33         = 3 * index3;
                float[] normals1    = this.normals.data;
                state.getNormal().x = k00 * normals1[i30 + 0] + k10 * normals1[i31 + 0] + k11 * normals1[i32 + 0] + k01 * normals1[i33 + 0];
                state.getNormal().y = k00 * normals1[i30 + 1] + k10 * normals1[i31 + 1] + k11 * normals1[i32 + 1] + k01 * normals1[i33 + 1];
                state.getNormal().z = k00 * normals1[i30 + 2] + k10 * normals1[i31 + 2] + k11 * normals1[i32 + 2] + k01 * normals1[i33 + 2];
                state.getNormal().set(state.transformNormalObjectToWorld(state.getNormal()));
                state.getNormal().normalize();
                break;
            }

            case ParameterList.InterpolationType.FACEVARYING:
            {
                int     idx         = 3 * quad;
                float[] normals1    = this.normals.data;
                state.getNormal().x = k00 * normals1[idx + 0] + k10 * normals1[idx + 3] + k11 * normals1[idx + 6] + k01 * normals1[idx + 9];
                state.getNormal().y = k00 * normals1[idx + 1] + k10 * normals1[idx + 4] + k11 * normals1[idx + 7] + k01 * normals1[idx + 10];
                state.getNormal().z = k00 * normals1[idx + 2] + k10 * normals1[idx + 5] + k11 * normals1[idx + 8] + k01 * normals1[idx + 11];
                state.getNormal().set(state.transformNormalObjectToWorld(state.getNormal()));
                state.getNormal().normalize();
                break;
            }
            }
            float uv00 = 0, uv01 = 0, uv10 = 0, uv11 = 0, uv20 = 0, uv21 = 0, uv30 = 0, uv31 = 0;

            switch (uvs.interp)
            {
            case ParameterList.InterpolationType.NONE:
            case ParameterList.InterpolationType.FACE:
            {
                state.getUV().x = 0;
                state.getUV().y = 0;
                break;
            }

            case ParameterList.InterpolationType.VERTEX:
            {
                int     i20  = 2 * index0;
                int     i21  = 2 * index1;
                int     i22  = 2 * index2;
                int     i23  = 2 * index3;
                float[] uvs1 = this.uvs.data;
                uv00 = uvs1[i20 + 0];
                uv01 = uvs1[i20 + 1];
                uv10 = uvs1[i21 + 0];
                uv11 = uvs1[i21 + 1];
                uv20 = uvs1[i22 + 0];
                uv21 = uvs1[i22 + 1];
                uv20 = uvs1[i23 + 0];
                uv21 = uvs1[i23 + 1];
                break;
            }

            case ParameterList.InterpolationType.FACEVARYING:
            {
                int     idx  = quad << 1;
                float[] uvs1 = this.uvs.data;
                uv00 = uvs1[idx + 0];
                uv01 = uvs1[idx + 1];
                uv10 = uvs1[idx + 2];
                uv11 = uvs1[idx + 3];
                uv20 = uvs1[idx + 4];
                uv21 = uvs1[idx + 5];
                uv30 = uvs1[idx + 6];
                uv31 = uvs1[idx + 7];
                break;
            }
            }
            if (uvs.interp != ParameterList.InterpolationType.NONE)
            {
                // get exact uv coords and compute tangent vectors
                state.getUV().x = k00 * uv00 + k10 * uv10 + k11 * uv20 + k01 * uv30;
                state.getUV().y = k00 * uv01 + k10 * uv11 + k11 * uv21 + k01 * uv31;
                float   du1 = uv00 - uv20;
                float   du2 = uv10 - uv20;
                float   dv1 = uv01 - uv21;
                float   dv2 = uv11 - uv21;
                Vector3 dp1 = Point3.sub(v0p, v2p, new Vector3()), dp2 = Point3.sub(v1p, v2p, new Vector3());
                float   determinant = du1 * dv2 - dv1 * du2;
                if (determinant == 0.0f)
                {
                    // create basis in world space
                    state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal()));
                }
                else
                {
                    float invdet = 1 / determinant;
                    // Vector3 dpdu = new Vector3();
                    // dpdu.x = (dv2 * dp1.x - dv1 * dp2.x) * invdet;
                    // dpdu.y = (dv2 * dp1.y - dv1 * dp2.y) * invdet;
                    // dpdu.z = (dv2 * dp1.z - dv1 * dp2.z) * invdet;
                    Vector3 dpdv = new Vector3();
                    dpdv.x = (-du2 * dp1.x + du1 * dp2.x) * invdet;
                    dpdv.y = (-du2 * dp1.y + du1 * dp2.y) * invdet;
                    dpdv.z = (-du2 * dp1.z + du1 * dp2.z) * invdet;
                    dpdv   = state.transformVectorObjectToWorld(dpdv);
                    // create basis in world space
                    state.setBasis(OrthoNormalBasis.makeFromWV(state.getNormal(), dpdv));
                }
            }
            else
            {
                state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal()));
            }
            int shaderIndex = faceShaders == null ? 0 : (faceShaders[primID] & 0xFF);

            state.setShader(parent.getShader(shaderIndex));
            state.setModifier(parent.getModifier(shaderIndex));
        }