Example #1
0
        private static Texture3D Create3DTexture(RayCastManager rtc)
        {
            Texture3D voxelTexture = new Texture3D(rtc.getSizeX(), rtc.getSizeY() / 8, rtc.getSizeZ(), TextureFormat.Alpha8, false);

            voxelTexture.filterMode = FilterMode.Point;
            voxelTexture.wrapMode   = TextureWrapMode.Clamp;
            voxelTexture.SetPixels(ToColorsA(rtc, 8));
            voxelTexture.Apply();
            return(voxelTexture);
        }
Example #2
0
        public static Color[] ToColorsA(RayCastManager rtc, int bits)
        {
            int dgx = rtc.getSizeX();
            int dgy = rtc.getSizeY();
            int dgz = rtc.getSizeZ();

            Color[,,] colors = new Color[dgx, (dgy / bits), dgz];
            for (int gx = 0; gx < dgx; gx++)
            {
                for (int gy = 0; gy < dgy / bits; gy++)
                {
                    for (int gz = 0; gz < dgz; gz++)
                    {
                        int iv = 0;
                        for (int n = 0; n < bits; n++)
                        {
                            iv |= rtc.testBlock(gx, gy * bits + n, gz, RayCastBlockType.Opacity) ? (1 << n) : 0;
                        }
                        float a = iv / (float)((1 << bits) - 1);
                        colors[gx, gy, gz] = new Color(0, 0, 0, a);
                    }
                }
            }


            int sizeX = colors.GetLength(0);
            int sizeY = colors.GetLength(1);
            int sizeZ = colors.GetLength(2);

            Color[] rlt = new Color[sizeX * sizeY * sizeZ];
            for (int x = 0; x < sizeX; x++)
            {
                for (int y = 0; y < sizeY; y++)
                {
                    for (int z = 0; z < sizeZ; z++)
                    {
                        rlt[z * sizeY * sizeX + y * sizeX + x] = colors[x, y, z];
                    }
                }
            }
            return(rlt);
        }
Example #3
0
        public static RayCastResult rayTraceSmall(Vector3 startPos, DirInfo dirInfo, RayCastManager rtm, Face faceIn, float maxLength, RayCastBlockType mask)
        {
            int   X        = (int)(startPos.x + dirInfo.stepX * 0.0001f);
            int   Y        = (int)(startPos.y + dirInfo.stepY * 0.0001f);
            int   Z        = (int)(startPos.z + dirInfo.stepZ * 0.0001f);
            float tMaxX    = (dirInfo.stepX > 0 ? (1 - (startPos.x - X)) : (startPos.x - X)) * dirInfo.tDeltaX;//沿射线走多远才能跳到下一个X格
            float tMaxY    = (dirInfo.stepY > 0 ? (1 - (startPos.y - Y)) : (startPos.y - Y)) * dirInfo.tDeltaY;
            float tMaxZ    = (dirInfo.stepZ > 0 ? (1 - (startPos.z - Z)) : (startPos.z - Z)) * dirInfo.tDeltaZ;
            int   outSizeX = dirInfo.stepX > 0 ? rtm.getSizeX() : -1;
            int   outSizeY = dirInfo.stepY > 0 ? rtm.getSizeY() : -1;
            int   outSizeZ = dirInfo.stepZ > 0 ? rtm.getSizeZ() : -1;

            while (true)
            {
                if (tMaxX < tMaxY)
                {
                    if (tMaxX < tMaxZ)
                    {
                        X += dirInfo.stepX;
                        if (X == outSizeX)
                        {
                            return(new RayCastResult(false, X, Y, Z, dirInfo.stepX > 0 ? Face.FNI_x0 : Face.FNI_x1, tMaxX));
                        }
                        else
                        {
                            if (tMaxX > maxLength)
                            {
                                return(new RayCastResult(false, X, Y, Z, Face.FNI_OutOfLength, tMaxX));
                            }
                            else if (rtm.testBlock(X, Y, Z, mask))
                            {
                                return(new RayCastResult(true, X, Y, Z, dirInfo.stepX > 0 ? Face.FNI_x0 : Face.FNI_x1, tMaxX));
                            }
                        }
                        tMaxX += dirInfo.tDeltaX;
                    }
                    else
                    {
                        Z += dirInfo.stepZ;
                        if (Z == outSizeZ)
                        {
                            return(new RayCastResult(false, X, Y, Z, dirInfo.stepZ > 0 ? Face.FNI_z0 : Face.FNI_z1, tMaxZ));
                        }
                        else
                        {
                            if (tMaxZ > maxLength)
                            {
                                return(new RayCastResult(false, X, Y, Z, Face.FNI_OutOfLength, tMaxZ));
                            }
                            else if (rtm.testBlock(X, Y, Z, mask))
                            {
                                return(new RayCastResult(true, X, Y, Z, dirInfo.stepZ > 0 ? Face.FNI_z0 : Face.FNI_z1, tMaxZ));
                            }
                        }
                        tMaxZ += dirInfo.tDeltaZ;
                    }
                }
                else
                {
                    if (tMaxY < tMaxZ)
                    {
                        Y += dirInfo.stepY;
                        if (Y == outSizeY)
                        {
                            return(new RayCastResult(false, X, Y, Z, dirInfo.stepY > 0 ? Face.FNI_y0 : Face.FNI_y1, tMaxY));
                        }
                        else
                        {
                            if (tMaxY > maxLength)
                            {
                                return(new RayCastResult(false, X, Y, Z, Face.FNI_OutOfLength, tMaxY));
                            }
                            else if (rtm.testBlock(X, Y, Z, mask))
                            {
                                return(new RayCastResult(true, X, Y, Z, dirInfo.stepY > 0 ? Face.FNI_y0 : Face.FNI_y1, tMaxY));
                            }
                        }

                        tMaxY += dirInfo.tDeltaY;
                    }
                    else
                    {
                        Z += dirInfo.stepZ;
                        if (Z == outSizeZ)
                        {
                            return(new RayCastResult(false, X, Y, Z, dirInfo.stepZ > 0 ? Face.FNI_z0 : Face.FNI_z1, tMaxZ));
                        }
                        else
                        {
                            if (tMaxZ > maxLength)
                            {
                                return(new RayCastResult(false, X, Y, Z, Face.FNI_OutOfLength, tMaxZ));
                            }
                            else if (rtm.testBlock(X, Y, Z, mask))
                            {
                                return(new RayCastResult(true, X, Y, Z, dirInfo.stepZ > 0 ? Face.FNI_z0 : Face.FNI_z1, tMaxZ));
                            }
                        }

                        tMaxZ += dirInfo.tDeltaZ;
                    }
                }
            }
        }
Example #4
0
        public static RayCastResult rayCast(Vector3 startPos, Vector3 dir, RayCastManager rtm, float length, RayCastBlockType mask)
        {
            DirInfo dirInfo       = new DirInfo(dir);
            Vector3 startPosInner = startPos;
            float   hitTime       = 0;
            float   startOffset   = 0;
            bool    bHitBounds    = true;//能击中总区域,或者在内部

            if (startPos.x >= 0 && startPos.x < rtm.getSizeX() && startPos.y >= 0 && startPos.y < rtm.getSizeY() && startPos.z >= 0 && startPos.z < rtm.getSizeZ())
            {
                bHitBounds = true;
            }
            else
            {
                bHitBounds = Misc.rayHitAABB(startPos, dir, new Vector3(0, 0, 0), rtm.getSize(), ref hitTime);
                if (hitTime > 0)
                {
                    startOffset = hitTime;
                }
                startPosInner = startPos + dir * startOffset - dir * 0.01f;
            }
            if (bHitBounds)
            {
                RayCastResult rlt = rayTraceSmall(startPosInner, dirInfo, rtm, Face.FNI_Unknown, length - startOffset, mask);
                rlt.hitLength += startOffset;
                return(rlt);
            }
            else
            {
                return(new RayCastResult(false, 0, 0, 0, Face.FNI_Unknown, 0));
            }
        }