/// <summary> /// Adds and computes the lighting for a quad from a ComponentCube. /// </summary> private void func01(BlockRendererPrimitive renderer, Vector3 worldPos, Vector3 v0, Vector3 v1, Vector3 v2, Vector3 v3, Vector2[] uvs, BlockPos shift) { // Arg shift is sort of like a normal, but can be (0, 0, 0). if (renderer.forcedLightMode > 0) { // No need to lookup fancy lighting, render a quad. this.addQuad( v0 + worldPos, v1 + worldPos, v2 + worldPos, v3 + worldPos, uvs, renderer.forcedLightMode == 1 ? LightSampleDirection.SELF : LightSampleDirection.UP); } else { // Add vertex colors for lighting. this.vertexColors.Add(this.calculateLightForVertex(v0, shift)); this.vertexColors.Add(this.calculateLightForVertex(v1, shift)); this.vertexColors.Add(this.calculateLightForVertex(v2, shift)); this.vertexColors.Add(this.calculateLightForVertex(v3, shift)); this.addQuad( v0 + worldPos, v1 + worldPos, v2 + worldPos, v3 + worldPos, uvs); } }
public void addCube(BlockRendererPrimitive renderer, Block block, int meta, CubeComponent cube, int renderFace, int worldX, int worldY, int worldZ) { // Define the points. Vector3 ppp = cube.pos; Vector3 ppn = new Vector3(cube.pos.x, cube.pos.y, cube.neg.z); Vector3 npn = new Vector3(cube.neg.x, cube.pos.y, cube.neg.z); Vector3 npp = new Vector3(cube.neg.x, cube.pos.y, cube.pos.z); Vector3 pnp = new Vector3(cube.pos.x, cube.neg.y, cube.pos.z); Vector3 pnn = new Vector3(cube.pos.x, cube.neg.y, cube.neg.z); Vector3 nnn = cube.neg; Vector3 nnp = new Vector3(cube.neg.x, cube.neg.y, cube.pos.z); // Rotate the cube if needed. if (!cube.rotation.isZero()) { Vector3 pivot = new Vector3(16, 16, 16); Quaternion angle = cube.rotation.getAngle(); ppp = MathHelper.rotateVecAround(ppp, pivot, angle); ppn = MathHelper.rotateVecAround(ppn, pivot, angle); npn = MathHelper.rotateVecAround(npn, pivot, angle); npp = MathHelper.rotateVecAround(npp, pivot, angle); pnp = MathHelper.rotateVecAround(pnp, pivot, angle); pnn = MathHelper.rotateVecAround(pnn, pivot, angle); nnn = MathHelper.rotateVecAround(nnn, pivot, angle); nnp = MathHelper.rotateVecAround(nnp, pivot, angle); } // Offset the cube if needed. if (cube.offset != Vector3.zero) { ppp += cube.offset; ppn += cube.offset; npn += cube.offset; npp += cube.offset; pnp += cube.offset; pnn += cube.offset; nnn += cube.offset; nnp += cube.offset; } // Convert the pixel units to world units before rendering. ppp = this.pixelToWorld(ppp); ppn = this.pixelToWorld(ppn); npn = this.pixelToWorld(npn); npp = this.pixelToWorld(npp); pnp = this.pixelToWorld(pnp); pnn = this.pixelToWorld(pnn); nnn = this.pixelToWorld(nnn); nnp = this.pixelToWorld(nnp); Vector3 worldPos = new Vector3(worldX, worldY, worldZ); // Note: The vertices that check if the face is out of the cell, meaning it need adjacent light, is arbitrary, // the same effect should be given requardless of the vertex. // North +Z if ((renderFace & 1) == 1) { this.func01( renderer, worldPos, pnp, ppp, npp, nnp, renderer.getUvPlane(block, meta, Direction.NORTH, cube).getMeshUvs(this.allocatedUvArray), pnp.z >= 0.5f ? BlockPos.north : BlockPos.zero); } // East +X if (((renderFace >> 1) & 1) == 1) { this.func01( renderer, worldPos, pnn, ppn, ppp, pnp, renderer.getUvPlane(block, meta, Direction.EAST, cube).getMeshUvs(this.allocatedUvArray), pnn.x >= 0.5f ? BlockPos.east : BlockPos.zero); } // South -Z if (((renderFace >> 2) & 1) == 1) { this.func01( renderer, worldPos, nnn, npn, ppn, pnn, renderer.getUvPlane(block, meta, Direction.SOUTH, cube).getMeshUvs(this.allocatedUvArray), nnn.z <= -0.5f ? BlockPos.south : BlockPos.zero); } // West -X if (((renderFace >> 3) & 1) == 1) { this.func01( renderer, worldPos, nnp, npp, npn, nnn, renderer.getUvPlane(block, meta, Direction.WEST, cube).getMeshUvs(this.allocatedUvArray), nnp.x <= -0.5f ? BlockPos.west : BlockPos.zero); } // Up +Y if (((renderFace >> 4) & 1) == 1) { this.func01( renderer, worldPos, npn, npp, ppp, ppn, renderer.getUvPlane(block, meta, Direction.UP, cube).getMeshUvs(this.allocatedUvArray), npn.y >= 0.5f ? BlockPos.up : BlockPos.zero); } // Down -Y if (((renderFace >> 5) & 1) == 1) { this.func01( renderer, worldPos, nnp, nnn, pnn, pnp, renderer.getUvPlane(block, meta, Direction.DOWN, cube).getMeshUvs(this.allocatedUvArray), nnn.y <= -0.5f ? BlockPos.down : BlockPos.zero); } }
/// <summary> /// Adds a plane. Optimized for use in BlockRendererCube and this shouldn't be used for anything else. /// </summary> public void addOptimized1x1Plane(BlockRendererPrimitive renderer, Block block, int meta, Vector3 v0, Vector3 v1, Vector3 v2, Vector3 v3, Direction direction) { this.allocatedUvArray = renderer.getUvPlane(block, meta, direction, new CubeComponent()).getMeshUvs(this.allocatedUvArray); this.addQuad( v0, v1, v2, v3, this.allocatedUvArray); // Set vertex colors. bool useSmooth = RenderManager.instance.useSmoothLighting; // Note! sampleAndSetVertexColor is called in the standard lower left clockwise order. if (direction == Direction.NORTH) { if (useSmooth) { this.sampleFor1x1Plane(direction, Direction.EAST, Direction.DOWN); this.sampleFor1x1Plane(direction, Direction.EAST, Direction.UP); this.sampleFor1x1Plane(direction, Direction.WEST, Direction.UP); this.sampleFor1x1Plane(direction, Direction.WEST, Direction.DOWN); } else { this.color4Vertices(LightSampleDirection.NORTH); } } else if (direction == Direction.EAST) { if (useSmooth) { this.sampleFor1x1Plane(direction, Direction.SOUTH, Direction.DOWN); this.sampleFor1x1Plane(direction, Direction.SOUTH, Direction.UP); this.sampleFor1x1Plane(direction, Direction.NORTH, Direction.UP); this.sampleFor1x1Plane(direction, Direction.NORTH, Direction.DOWN); } else { this.color4Vertices(LightSampleDirection.EAST); } } else if (direction == Direction.SOUTH) { if (useSmooth) { this.sampleFor1x1Plane(direction, Direction.WEST, Direction.DOWN); this.sampleFor1x1Plane(direction, Direction.WEST, Direction.UP); this.sampleFor1x1Plane(direction, Direction.EAST, Direction.UP); this.sampleFor1x1Plane(direction, Direction.EAST, Direction.DOWN); } else { this.color4Vertices(LightSampleDirection.SOUTH); } } else if (direction == Direction.WEST) { if (useSmooth) { this.sampleFor1x1Plane(direction, Direction.NORTH, Direction.DOWN); this.sampleFor1x1Plane(direction, Direction.NORTH, Direction.UP); this.sampleFor1x1Plane(direction, Direction.SOUTH, Direction.UP); this.sampleFor1x1Plane(direction, Direction.SOUTH, Direction.DOWN); } else { this.color4Vertices(LightSampleDirection.WEST); } } else if (direction == Direction.UP) { if (useSmooth) { this.sampleFor1x1Plane(direction, Direction.WEST, Direction.SOUTH); this.sampleFor1x1Plane(direction, Direction.WEST, Direction.NORTH); this.sampleFor1x1Plane(direction, Direction.EAST, Direction.NORTH); this.sampleFor1x1Plane(direction, Direction.EAST, Direction.SOUTH); } else { this.color4Vertices(LightSampleDirection.UP); } } else if (direction == Direction.DOWN) { if (useSmooth) { this.sampleFor1x1Plane(direction, Direction.WEST, Direction.NORTH); this.sampleFor1x1Plane(direction, Direction.WEST, Direction.SOUTH); this.sampleFor1x1Plane(direction, Direction.EAST, Direction.SOUTH); this.sampleFor1x1Plane(direction, Direction.EAST, Direction.NORTH); } else { this.color4Vertices(LightSampleDirection.DOWN); } } }