private void AssertUnlinearizeLinearize(IntVector2 original, IntVector2 dimensions) { IntVector2 actual = QuadMesh.UnlinearizeIndex(QuadMesh.LinearizeIndex(original, dimensions), dimensions); Assert.IsTrue(EqualIntVector2(original, actual), string.Format("expected: {0}, actual: {1}", original, actual)); }
/// <summary> /// Writes a quad to the mesh. /// </summary> /// <param name="quad">The quad to write.</param> public void WriteQuad(QuadMesh quad) { AddTriangleIndices(); AddVertices(quad); AddNormals(quad.Side); AddUVs(quad); }
public void ShowWindowWithChildWindow() { Application.InitSysDependencies(); Win32Window windowA = new Win32Window(); windowA.Init((100, 100), (300, 400), WindowTypes.Regular); windowA.Show(); Win32Window windowB = new Win32Window(windowA); windowB.Init((10, 10), (100, 100), WindowTypes.ClientAreaOnly); windowB.Show(); Win32OpenGLRenderer renderer = new Win32OpenGLRenderer(windowA); var meshA = new QuadMesh(new Rect(150, 150, 50, 50), Color.Blue); var meshB = new QuadMesh(new Rect(10, 10, 50, 50), Color.DarkBlue); while (!windowA.Closed) { windowA.MainLoop(() => { renderer.SetRenderingWindow(windowA); renderer.Clear(Color.Rgb(200, 100, 100)); Win32OpenGLRenderer.DrawMesh(OpenGLMaterial.shapeMaterial, meshA, (int)windowA.ClientSize.Width, (int)windowA.ClientSize.Height); renderer.SwapBuffers(); renderer.SetRenderingWindow(windowB); renderer.Clear(Color.Rgb(100, 200, 100)); Win32OpenGLRenderer.DrawMesh(OpenGLMaterial.shapeMaterial, meshB, (int)windowB.ClientSize.Width, (int)windowB.ClientSize.Height); renderer.SwapBuffers(); }); } }
private void CreateNewVisual() { visual = Slot.AddSlot("ComplexFractalVisual"); FractalType fractalType = fractalData.Evaluate(new FractalData(FractalType.none, new double2(0, 0))).fractalType; if (fractalType == FractalType.mandelbrot) { resultTexture = visual.AttachComponent <MandelbrotTexture>(); } else if (fractalType == FractalType.julia) { resultTexture = visual.AttachComponent <JuliaTexture>(); } else if (fractalType == FractalType.newton) { resultTexture = visual.AttachComponent <NewtonTexture>(); } else { return; } ChangeTextureAttributes(); resultMat = visual.AttachComponent <UnlitMaterial>(false, null); resultMat.BlendMode.Value = BlendMode.Transparent; resultMat.Texture.Target = resultTexture; graph = visual.AttachQuad(new float2(0.6f, 0.4f), resultMat, true); AdjustGraphAttributes(); graphRenderer = visual.AttachComponent <MeshRenderer>(); graphRenderer.Materials.Add(); graphRenderer.Materials[0] = resultMat; graphRenderer.Mesh.Target = graph; }
private bool generateWall(GameObject host, Vector2 bottomSize, Vector2 topSize, float lowerHeight, float upperHeight) { QuadMesh meshBuilder = new QuadMesh(); Vector3 bottomLeft, topLeft, topRight, bottomRight; //+x Wall bottomLeft = new Vector3(bottomSize.x / 2, lowerHeight, bottomSize.y / 2); topLeft = new Vector3(topSize.x / 2, upperHeight, topSize.y / 2); topRight = new Vector3(topSize.x / 2, upperHeight, -topSize.y / 2); bottomRight = new Vector3(bottomSize.x / 2, lowerHeight, -bottomSize.y / 2); meshBuilder.addQuad(bottomLeft, topLeft, topRight, bottomRight); //-x Wall bottomLeft = new Vector3(-bottomSize.x / 2, lowerHeight, -bottomSize.y / 2); topLeft = new Vector3(-topSize.x / 2, upperHeight, -topSize.y / 2); topRight = new Vector3(-topSize.x / 2, upperHeight, topSize.y / 2); bottomRight = new Vector3(-bottomSize.x / 2, lowerHeight, bottomSize.y / 2); meshBuilder.addQuad(bottomLeft, topLeft, topRight, bottomRight); //+z Wall bottomLeft = new Vector3(-bottomSize.x / 2, lowerHeight, bottomSize.y / 2); topLeft = new Vector3(-topSize.x / 2, upperHeight, topSize.y / 2); topRight = new Vector3(topSize.x / 2, upperHeight, topSize.y / 2); bottomRight = new Vector3(bottomSize.x / 2, lowerHeight, bottomSize.y / 2); meshBuilder.addQuad(bottomLeft, topLeft, topRight, bottomRight); //-z Wall bottomLeft = new Vector3(bottomSize.x / 2, lowerHeight, -bottomSize.y / 2); topLeft = new Vector3(topSize.x / 2, upperHeight, -topSize.y / 2); topRight = new Vector3(-topSize.x / 2, upperHeight, -topSize.y / 2); bottomRight = new Vector3(-bottomSize.x / 2, lowerHeight, -bottomSize.y / 2); meshBuilder.addQuad(bottomLeft, topLeft, topRight, bottomRight); Mesh wallMesh = meshBuilder.getMesh(); RoomTools.addMeshWithCol(host, wallMesh, RoomTools.wallRenderMaterial, RoomTools.wallPhysicMaterial); return(true); }
public ScoreSheet(UILayer layer, Font font) : base(layer) { Font = font; fields.Clear(); maxScale = new vec2(fullSize / smallSize); var rc = new ImageRenderComponent(layer.Gl, "Resource/Images/UI/Other/scorePaper2.png"); RenderComponent = rc; imgSize = ((vec2)rc.Image.Size * smallSize).ScaleToScreen(); Quad = new QuadMesh(imgSize); Transform.Translation = new vec2((-layer.UIFrameBuffer.BoundTexture.Size.x / 2) + (Quad.Size.x / 2) + screenPadding, smallSizeHeight); layer.AddComponent(this); Font.Size = 30 * Program.Settings.ScreenRatio.x * 1.5f; for (int i = 0; i < 18; i++) { var t = new SheetField(this, layer, Font, fieldFuncs[i], !nonLockableIndices.Contains(i), new vec2(fieldLocs[i * 2], -fieldLocs[i * 2 + 1])); fields.Add(t); } OnClick += SheetClicked; ClearFields(); }
/// <summary> /// Adds quad vertices to the mesh based on the given rect and layer information. /// </summary> /// <param name="quad">The quad data.</param> void AddVertices(QuadMesh quad) { switch (quad.Side) { case 0: AddQuadVerticesSide0(quad); break; case 1: AddQuadVerticesSide1(quad); break; case 2: AddQuadVerticesSide2(quad); break; case 3: AddQuadVerticesSide3(quad); break; case 4: AddQuadVerticesSide4(quad); break; case 5: AddQuadVerticesSide5(quad); break; } }
public void Setup(string icon) { CastShadow = false; ReceiveShadow = false; _geometry = new QuadMesh(); _geometry.Size = new Vector3(0.5f); _geometry.TextureRepeat = new Vector2(2); _geometry.Build(); boundingBox = new BoundingBox(new Vector3(-0.25f), new Vector3(0.25f)); UnlitMaterial iconMaterial = null; if (s_Materials.ContainsKey(icon)) { iconMaterial = s_Materials[icon]; // Mat is null if the scene was changed. if (iconMaterial == null) { iconMaterial = CreateMaterial(icon); s_Materials[icon] = iconMaterial; } } else { iconMaterial = CreateMaterial(icon); s_Materials.Add(icon, iconMaterial); } material = iconMaterial; }
private void InitMiniMap() { // quadTrans = m_widget_Root.transform.Find( "Quad" ); quadTrans = m_trans_minimapObj; if (quadTrans != null) { //Collider col = quadTrans.GetComponent<MeshCollider>(); //if ( col != null ) col.enabled = false; //quadTrans.gameObject.AddComponent<QuadMesh>(); } m_QuadMesh = quadTrans.GetComponent <QuadMesh>(); if (m_QuadMesh == null) { m_QuadMesh = quadTrans.gameObject.AddComponent <QuadMesh>(); } playerIcon = m_widget_Root.transform.Find("IconContainer/playericon"); MeshRenderer mr = quadTrans.GetComponent <MeshRenderer>(); mapMat = mr.material; mapMat.shader = Resources.Load("Shaders/Custom/Mask") as Shader; // UIParticleWidget pw = quadTrans.GetComponent<UIParticleWidget>(); // if(pw != null) // { // pw.SetParticleDirty(); // } //mainPlayer = ClientGlobal.Instance().MainPlayer; entityTable.Add(GetStrByIconType(IconType.playericon), new List <long>()); entityTable.Add(GetStrByIconType(IconType.npcicon), new List <long>()); entityTable.Add(GetStrByIconType(IconType.mastericon), new List <long>()); entityTable.Add(GetStrByIconType(IconType.otherplayericon), new List <long>()); entityTable.Add(GetStrByIconType(IconType.duiyouicon), new List <long>()); entityTable.Add(GetStrByIconType(IconType.peticon), new List <long>()); entityTable.Add(GetStrByIconType(IconType.robot), new List <long>()); //if (m_slider_Zoomslider != null) //{ // //UISlider[] sliders = obj.GetComponentsInChildren<UISlider>(); // //m_slider_Zoomslider.onChange.Add(OnClick_Zoomslider); // EventDelegate.Add(m_slider_Zoomslider.onChange, OnClick_Zoomslider); //} m_pointParentTrans = m_trans_minimapObj; string maskPath = BigMapPanel.MINIMAP_TEXTURE_PATH + "minimapmask.unity3d"; if (m_maskTextrue != null) { m_maskTextrue.Release(); m_maskTextrue = null; } Engine.RareEngine.Instance().GetRenderSystem().CreateTexture(ref maskPath, ref m_maskTextrue, CreateTextureEvent, null, Engine.TaskPriority.TaskPriority_Immediate); RegisterMiniMapEvents(true); }
private void MakeExplosion() { GameObject explosion = Instantiate(explosionPrefab, Trans.position, Trans.rotation, GameManager.Instance.transform); QuadMesh qm = GetComponent <QuadMesh>(); if (qm != null) { float size = qm.Size * 1.5f; explosion.transform.localScale = new Vector3(size, size, 1); } explosion.transform.localScale = Trans.localScale * 3.5f; }
/// <summary> /// Adds quad uvs to the mesh based on the given rect and layer information. /// </summary> /// <param name="quad">The quad data.</param> void AddUVs(QuadMesh quad) { var specs = new UVLookupTable.UVSpecs { Side = quad.Side, Rotation = quad.TextureRotation, W = quad.W, H = quad.H, Texture = quad.TextureIndex, }; m_UVLookupTable.Find(Mesh.UVs, specs); }
public void Init(IntPtr windowHandle, Size size) { CreateOpenGLContext(windowHandle); OpenGLMaterial.InitCommonMaterials(); ImGui.Development.SpecialMesh.Init(); // Other state GL.Enable(GL.GL_MULTISAMPLE); GL.Disable(GL.GL_CULL_FACE); GL.Disable(GL.GL_DEPTH_TEST); GL.DepthFunc(GL.GL_NEVER); GL.Enable(GL.GL_SCISSOR_TEST); //set-up framebuffer GL.GenFramebuffers(1, framebuffers); GL.GenTextures(1, textures); framebuffer = framebuffers[0]; framebufferColorTexture = textures[0]; GL.BindFramebuffer(GL.GL_FRAMEBUFFER_EXT, framebuffer); //attach color texture to the framebuffer GL.BindTexture(GL.GL_TEXTURE_2D, framebufferColorTexture); GL.TexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, (int)size.Width, (int)size.Height, 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, IntPtr.Zero); GL.FramebufferTexture2D(GL.GL_FRAMEBUFFER_EXT, GL.GL_COLOR_ATTACHMENT0_EXT, GL.GL_TEXTURE_2D, framebufferColorTexture, 0); GL.TexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST); GL.TexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST); GL.TexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, (int)GL.GL_CLAMP); GL.TexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, (int)GL.GL_CLAMP); GL.GetFramebufferAttachmentParameteriv(GL.GL_FRAMEBUFFER_EXT, GL.GL_COLOR_ATTACHMENT0_EXT, GL.GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE, IntBuffer); Utility.CheckGLError(); var alphaBits = IntBuffer[0]; if(alphaBits != 8) { throw new Exception("Framebuffer format is not R8G8B8A8."); } //check if framebuffer is complete if (GL.CheckFramebufferStatus(GL.GL_FRAMEBUFFER_EXT) != GL.GL_FRAMEBUFFER_COMPLETE_EXT) { throw new Exception("Framebuffer is not complete."); } GL.BindFramebuffer(GL.GL_FRAMEBUFFER_EXT, 0); quadMesh = new QuadMesh(size.Width, size.Height); Utility.CheckGLError(); }
void AddQuadVerticesSide5(QuadMesh quad) { int sx = quad.X; int sy = quad.Y; int sz = quad.Offset; int bx = sx + quad.W; int by = sy + quad.H; Mesh.Vertices.Add(new Vector3(sx, sy, sz)); Mesh.Vertices.Add(new Vector3(sx, by, sz)); Mesh.Vertices.Add(new Vector3(bx, by, sz)); Mesh.Vertices.Add(new Vector3(bx, sy, sz)); }
void AddQuadVerticesSide3(QuadMesh quad) { int sx = quad.X; int sy = quad.Offset; int sz = quad.Y; int bx = sx + quad.W; int bz = sz + quad.H; Mesh.Vertices.Add(new Vec3(bx, sy, bz)); Mesh.Vertices.Add(new Vec3(sx, sy, bz)); Mesh.Vertices.Add(new Vec3(sx, sy, sz)); Mesh.Vertices.Add(new Vec3(bx, sy, sz)); }
public void LinearizeIndex_Works() { IntVector2 dimensions = new IntVector2(10, 10); Assert.AreEqual(5, QuadMesh.LinearizeIndex(new IntVector2(5, 0), dimensions)); Assert.AreEqual(15, QuadMesh.LinearizeIndex(new IntVector2(5, 1), dimensions)); Assert.AreEqual(95, QuadMesh.LinearizeIndex(new IntVector2(5, 9), dimensions)); Assert.AreEqual(99, QuadMesh.LinearizeIndex(new IntVector2(9, 9), dimensions)); dimensions = new IntVector2(3, 7); Assert.AreEqual(20, QuadMesh.LinearizeIndex(new IntVector2(2, 6), dimensions)); Assert.AreEqual(15, QuadMesh.LinearizeIndex(new IntVector2(0, 5), dimensions)); }
public override void CreateQuad(Cubeside side) { string textureName; switch (side) { case Cubeside.TOP: { textureName = textureNames[0]; break; } case Cubeside.BOTTOM: { textureName = textureNames[1]; break; } default: { textureName = textureNames[2]; break; } } QuadMesh quadMesh = new QuadMesh(side); TextureAtlasMapper mapper = Game.currentGame.mapper; Vector2[] uvs = mapper.GetMappedUVs(textureName); GameObject quad = new GameObject("Quad"); quad.transform.position = globalPosition; quad.transform.parent = chunk.GameObject.transform; MeshFilter meshFilter = (MeshFilter)quad.AddComponent(typeof(MeshFilter)); Mesh myMesh = quadMesh.mesh; myMesh.uv = uvs; myMesh.RecalculateBounds(); myMesh.name = "my mesh"; meshFilter.mesh = myMesh; MeshRenderer renderer = quad.AddComponent(typeof(MeshRenderer)) as MeshRenderer; }
public Backdrop(Device device, ShaderCache shaderCache) { float size = 5f; Matrix transform = Matrix.Translation(0, size / 2, 0); QuadMesh mesh = GeometricPrimitiveFactory.MakeCube(size).Flip(); this.meshBuffers = new MeshBuffers(device, mesh.AsTriMesh()); this.modelToWorldTransform = new CoordinateNormalMatrixPairConstantBufferManager(device); var vertexShaderAndBytecode = shaderCache.GetVertexShader <Backdrop>("backdrop/Backdrop"); this.vertexShader = vertexShaderAndBytecode; this.inputLayout = new InputLayout(device, vertexShaderAndBytecode.Bytecode, MeshBuffers.InputElements); this.pixelShader = shaderCache.GetPixelShader <Backdrop>("backdrop/Backdrop"); this.transform = transform; }
private void GenGeometry() { QuadMesh chunk = new QuadMesh(); for (int x = 0; x < fieldSize; ++x) { for (int y = 0; y < fieldSize; ++y) { for (int z = 0; z < fieldSize; ++z) { AddCubeFace(chunk, x, y, z); } } } chunk.Build(); MeshFilter filter = GetComponent <MeshFilter>(); filter.sharedMesh = chunk.Mesh; }
public void StartDrawTextMeshToImage(int width, int height) { //create quadMesh if (quadMesh == null) { quadMesh = new QuadMesh(); } //create textures { glyphOpenGLTexture = new OpenGLTexture(); glyphOpenGLTexture.LoadImage(new byte[width * height * 4], width, height); glyphTexture = (uint)glyphOpenGLTexture.GetNativeTextureId(); } { textOpenGLTexture = new OpenGLTexture(); textOpenGLTexture.LoadImage(new byte[width * height * 4], width, height); textTexture = (uint)textOpenGLTexture.GetNativeTextureId(); } //create frame buffer GL.GenFramebuffers(2, textMeshFrameBuffers); this.glyphFrameBuffer = textMeshFrameBuffers[0]; this.textFrameBuffer = textMeshFrameBuffers[1]; //attach textures to framebuffers GL.BindFramebuffer(GL.GL_FRAMEBUFFER_EXT, this.glyphFrameBuffer); GL.FramebufferTexture2D(GL.GL_FRAMEBUFFER_EXT, GL.GL_COLOR_ATTACHMENT0_EXT, GL.GL_TEXTURE_2D, glyphTexture, 0); if (GL.CheckFramebufferStatus(GL.GL_FRAMEBUFFER_EXT) != GL.GL_FRAMEBUFFER_COMPLETE_EXT) { throw new Exception("Framebuffer for glyph is not complete."); } GL.BindFramebuffer(GL.GL_FRAMEBUFFER_EXT, this.textFrameBuffer); GL.FramebufferTexture2D(GL.GL_FRAMEBUFFER_EXT, GL.GL_COLOR_ATTACHMENT0_EXT, GL.GL_TEXTURE_2D, textTexture, 0); if (GL.CheckFramebufferStatus(GL.GL_FRAMEBUFFER_EXT) != GL.GL_FRAMEBUFFER_COMPLETE_EXT) { throw new Exception("Framebuffer for text is not complete."); } }
private bool generateTrapDoor() { doorObj = new GameObject("Door" + Room.id); doorObj.transform.SetParent(roomObj.transform); doorObj.transform.localPosition = Vector3.zero; door = doorObj.AddComponent <TrapDoor>(); door.externalConstructor(new Vector2(dimensions.x, dimensions.z)); //Generate only +z side of trapdoor QuadMesh meshBuilder = new QuadMesh(); Vector3 bottomLeft, topLeft, topRight, bottomRight; float upperHeight = 0f; float lowerHeight = -RoomTools.trapDoorThickness; //upper plane bottomLeft = new Vector3(-dimensions.x / 2, upperHeight, 0f); topLeft = new Vector3(-dimensions.x / 2, upperHeight, dimensions.z / 2); topRight = new Vector3(dimensions.x / 2, upperHeight, dimensions.z / 2); bottomRight = new Vector3(dimensions.x / 2, upperHeight, 0f); meshBuilder.addQuad(bottomLeft, topLeft, topRight, bottomRight); //lower plane bottomLeft = new Vector3(-dimensions.x / 2, lowerHeight, dimensions.z / 2); topLeft = new Vector3(-dimensions.x / 2, lowerHeight, 0f); topRight = new Vector3(dimensions.x / 2, lowerHeight, 0f); bottomRight = new Vector3(dimensions.x / 2, lowerHeight, dimensions.z / 2); meshBuilder.addQuad(bottomLeft, topLeft, topRight, bottomRight); //mid plane bottomLeft = new Vector3(-dimensions.x / 2, lowerHeight, 0f); topLeft = new Vector3(-dimensions.x / 2, upperHeight, 0f); topRight = new Vector3(dimensions.x / 2, upperHeight, 0f); bottomRight = new Vector3(dimensions.x / 2, lowerHeight, 0f); meshBuilder.addQuad(bottomLeft, topLeft, topRight, bottomRight); Mesh doorMesh = meshBuilder.getMesh(); door.setDoorMesh(doorMesh); return(true); }
public virtual void CreateQuad(Cubeside side) { string[] textures = new String[6]; if (textureNames.Length == 1) { for (int j = 0; j < 6; j++) { textures[j] = textureNames[0]; } } else { textures = textureNames; } for (int i = 0; i < textures.Length; i++) { QuadMesh quadMesh = new QuadMesh(side); TextureAtlasMapper mapper = Game.currentGame.mapper; Vector2[] uvs = mapper.GetMappedUVs(textures[i]); GameObject quad = new GameObject("Quad"); quad.transform.position = globalPosition; quad.transform.parent = chunk.GameObject.transform; MeshFilter meshFilter = (MeshFilter)quad.AddComponent(typeof(MeshFilter)); Mesh myMesh = quadMesh.mesh; myMesh.uv = uvs; myMesh.RecalculateBounds(); myMesh.name = "my mesh"; meshFilter.mesh = myMesh; hasCubeSideMesh[side] = true; } }
public void Generate() { MeshBuilder baseMesh1 = new MeshBuilder(); QuadMesh.Create(baseMesh1, Vector3.right * baseSize.x, Vector3.forward * baseSize.y); baseMesh1 = ExtrudeMesh.From(baseMesh1, Vector3.up * height1).Join(baseMesh1); MeshBuilder circleMesh = CircleMesh.Create(height2, 0f, 180f, -Vector3.forward, Vector3.up). Translate(Vector3.up * height1 + Vector3.forward * 0.5f * baseSize.y); circleMesh = ExtrudeMesh.From(circleMesh, Vector3.right * baseSize.x).Join(circleMesh); MeshFilter filter = gameObject.GetComponent <MeshFilter>(); Mesh mesh = MeshBuilder.Join(baseMesh1, circleMesh).CreateMesh(); mesh.RecalculateBounds(); filter.mesh = mesh; }
public Walls(GameMap map) { Name = "Walls for map \"" + map.Name + "\""; Map = map; Navigable = new bool[Map.Width][]; Transparent = new bool[Map.Width][]; for (ushort x = 0; x < Map.Width; x++) { Navigable[x] = new bool[Map.Depth]; Transparent[x] = new bool[Map.Depth]; for (ushort z = 0; z < Map.Depth; z++) { Navigable[x][z] = Assets.IsNavigable(Map.GetMapData(x, z), Map.GetObjectData(x, z)); Transparent[x][z] = Assets.IsTransparent(Map.GetMapData(x, z), Map.GetObjectData(x, z)); } } // realWalls replaces pushwalls with floors. ushort[] realWalls = new ushort[map.MapData.Length]; Array.Copy(map.MapData, realWalls, realWalls.Length); for (uint i = 0; i < realWalls.Length; i++) { if (Assets.PushWalls.Contains(Map.ObjectData[i])) { realWalls[i] = Assets.FloorCodeFirst; } } ushort GetMapData(ushort x, ushort z) => realWalls[Map.GetIndex(x, z)]; AddChild(Ground = new CollisionShape() { Name = "Ground", Shape = new BoxShape() { Extents = new Vector3(Map.Width * Assets.HalfWallWidth, Map.Depth * Assets.HalfWallWidth, Assets.PixelHeight) }, Transform = new Transform( new Basis(Vector3.Right, Mathf.Pi / 2f).Rotated(Vector3.Up, Mathf.Pi / 2f).Orthonormalized(), new Vector3( Map.Width * Assets.HalfWallWidth, Assets.PixelHeight / -2f, Map.Depth * Assets.HalfWallWidth ) ), }); Ground.AddChild(GroundMesh = new MeshInstance() { Name = "Ground Mesh", Mesh = new QuadMesh() { Size = new Vector2(Map.Width * Assets.WallWidth, Map.Depth * Assets.WallWidth), }, MaterialOverride = Map.GroundTile is ushort groundTile && groundTile < Assets.VSwapTextures.Length ? new SpatialMaterial() { AlbedoTexture = Assets.VSwapTextures[groundTile], FlagsUnshaded = true, FlagsDoNotReceiveShadows = true, FlagsDisableAmbientLight = true, FlagsTransparent = false, ParamsCullMode = SpatialMaterial.CullMode.Disabled, ParamsSpecularMode = SpatialMaterial.SpecularMode.Disabled, AnisotropyEnabled = true, RenderPriority = 1, Uv1Scale = new Vector3(Map.Width, Map.Depth, 0f), } : new SpatialMaterial() { AlbedoColor = Assets.Palettes[0][(int)Map.Ground], FlagsUnshaded = true, FlagsDoNotReceiveShadows = true, FlagsDisableAmbientLight = true, FlagsTransparent = false, ParamsCullMode = SpatialMaterial.CullMode.Disabled, ParamsSpecularMode = SpatialMaterial.SpecularMode.Disabled, AnisotropyEnabled = true, RenderPriority = 1, }, });
private void Awake() { instance = this; emptyMesh = new Mesh(); parentQuad = new Quad(0, 0, 100, 100); AutoSubdivide(parentQuad); // NeighbourSubdivide(parentQuad); parentQuad.EnsureMaxDepth(); parentQuad.FinaliseGeometry(min, max, transform.position.y); parentQuad.DeleteUnrequired(min, max, transform.position.y); tris.Clear(); parentQuad.RedrawGeometry(min, max, transform.position.y); Vector3 minT = transform.InverseTransformPoint(min); Vector3 maxT = transform.InverseTransformPoint(max); for (int i = 0; i < verts.Count; i++) { Vector3 localPos = verts[i]; //float realXPos = trans.TransformPoint(localPos).x; //float realZPos = trans.TransformPoint(localPos).z; float realXPos = localPos.x; float realZPos = localPos.y; float percX = ((realXPos - minT.x) * 100) / (maxT.x - minT.x); float percY = ((realZPos - minT.y) * 100) / (maxT.y - minT.y); int pixelX = Mathf.Clamp(Mathf.RoundToInt(percX / 100 * testTexture.width), 0, testTexture.width - 1); int pixelY = Mathf.Clamp(Mathf.RoundToInt(percY / 100 * testTexture.height), 0, testTexture.height - 1); float r = testTexture.GetPixel(pixelX, pixelY).r; float val = r; float Fy = (val * maxHeight); localPos = new Vector3(localPos.x, localPos.y, Fy); verts[i] = localPos; } emptyMesh.vertices = verts.ToArray(); emptyMesh.triangles = tris.ToArray(); emptyMesh.uv = UVs.ToArray(); emptyMesh.RecalculateBounds(); emptyMesh.RecalculateNormals(); emptyMesh.RecalculateTangents(); GetComponent <MeshFilter>().mesh = emptyMesh; GetComponent <MeshCollider>().sharedMesh = emptyMesh; string path = "Assets/TerrainChunks/MeshData/"; AssetDatabase.CreateAsset(emptyMesh, path + ID + "_MESH.mesh"); AssetDatabase.SaveAssets(); verts.Clear(); indexFromVector.Clear(); tris.Clear(); UVs.Clear(); }
private void AddCubeFace(QuadMesh chunk, int x, int y, int z) { if (_map[x, y, z] == -1) { if (x == 0) { chunk.AddFace(new Vector3(x - 1, y, z), QuadMesh.Face.Right, indexColor[(int)ZoneType.Count]); } if (x == fieldSize - 1) { chunk.AddFace(new Vector3(x + 1, y, z), QuadMesh.Face.Left, indexColor[(int)ZoneType.Count]); } if (y == 0) { chunk.AddFace(new Vector3(x, y - 1, z), QuadMesh.Face.Top, indexColor[(int)ZoneType.Count]); } if (y == fieldSize - 1) { chunk.AddFace(new Vector3(x, y + 1, z), QuadMesh.Face.Bottom, indexColor[(int)ZoneType.Count]); } if (z == 0) { chunk.AddFace(new Vector3(x, y, z - 1), QuadMesh.Face.Front, indexColor[(int)ZoneType.Count]); } if (z == fieldSize - 1) { chunk.AddFace(new Vector3(x, y, z + 1), QuadMesh.Face.Back, indexColor[(int)ZoneType.Count]); } return; } ZoneType zoneType = (ZoneType)(_map[x, y, z]); Color color = indexColor[(int)zoneType % indexColor.Length]; QuadMesh.Face faces = QuadMesh.Face.None; if (x > 0 && _map[x - 1, y, z] < 0) { faces |= QuadMesh.Face.Left; } if (x < fieldSize - 1 && _map[x + 1, y, z] < 0) { faces |= QuadMesh.Face.Right; } if (y > 0 && _map[x, y - 1, z] < 0) { faces |= QuadMesh.Face.Bottom; } if (y < fieldSize - 1 && _map[x, y + 1, z] < 0) { faces |= QuadMesh.Face.Top; } if (z > 0 && _map[x, y, z - 1] < 0) { faces |= QuadMesh.Face.Back; } if (z < fieldSize - 1 && _map[x, y, z + 1] < 0) { faces |= QuadMesh.Face.Front; } chunk.AddFace(new Vector3(x, y, z), faces, color); }
private void InitializeTextRenderResources(Size viewportSize) { CreateTextFramebuffer(viewportSize); quadMesh = new QuadMesh(); Utility.CheckGLError(); }
internal byte[] DrawMeshToTexture(Mesh mesh, TextMesh textMesh, int width, int height) { //**Key Point 1** check scissor test: //The initially scissor-test rectangle's size is the size of the window client area. GL.Scissor(-4096, -4096, 8192, 8192);//reset scissor rect to a very big one //create texture that will be rendered onto ITexture texture = new OpenGLTexture(); texture.LoadImage(new byte[width * height * 4], width, height); GL.BindTexture(GL.GL_TEXTURE_2D, 0); Utility.CheckGLError(); //create frame buffer uint[] framebuffers = {0}; GL.GenFramebuffers(1, framebuffers); uint framebuffer = framebuffers[0]; GL.BindFramebuffer(GL.GL_FRAMEBUFFER_EXT, framebuffer); GL.FramebufferTexture2D(GL.GL_FRAMEBUFFER_EXT, GL.GL_COLOR_ATTACHMENT0_EXT, GL.GL_TEXTURE_2D, (uint) texture.GetNativeTextureId(), 0); if (GL.CheckFramebufferStatus(GL.GL_FRAMEBUFFER_EXT) != GL.GL_FRAMEBUFFER_COMPLETE_EXT) { throw new Exception("Framebuffer is not complete."); } //**Key point 2** Be sure the state of a framebuffer is expected. //Check if framebuffer need to be cleared to make it into an expected state. GL.Viewport(0, 0, width, height); GL.ClearColor(1.0f, 1.0f, 1.0f, 1.0f); GL.Clear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT | GL.GL_STENCIL_BUFFER_BIT); Utility.CheckGLError(); float l = 0; float r = width; float x = 0; float b = height; float n = -0.1f; float f = 100.0f; float[] ortho_projection = new float[16] { 2.0f/(r-l), 0.0f, 0.0f, 0.0f, 0.0f, 2.0f/(x-b), 0.0f, 0.0f, 0.0f, 0.0f, 2.0f/(n-f), 0.0f, (r+l)/(l-r), (x+b)/(b-x), (f+n)/(n-f), 1.0f, }; var viewMatrix = GetViewMatrix(width, height); if (mesh!=null && !mesh.IsEmpty) { List<DrawCommand> commandBuffer = mesh.CommandBuffer; VertexBuffer vertexBuffer = mesh.VertexBuffer; IndexBuffer indexBuffer = mesh.IndexBuffer; // **Key Point 3** Blending states have an impact on rendering result. // Check them before **every** draw-call on a framebuffer, **especially the first one**. GL.Enable(GL.GL_BLEND); GL.BlendEquation(GL.GL_FUNC_ADD_EXT); GL.BlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);//doing regular alpha blending GL.Disable(GL.GL_CULL_FACE); GL.Disable(GL.GL_DEPTH_TEST); GL.DepthFunc(GL.GL_NEVER); // Setup viewport, orthographic projection matrix GL.Viewport(0, 0, width, height); // Setup view transformation OpenGLMaterial.shapeMaterial.program.Bind(); OpenGLMaterial.shapeMaterial.program.SetUniformMatrix4("ViewMtx", viewMatrix); OpenGLMaterial.shapeMaterial.program.SetUniformMatrix4("ProjMtx", ortho_projection); // Send vertex and index data GL.BindVertexArray(OpenGLMaterial.shapeMaterial.VaoHandle); GL.BindBuffer(GL.GL_ARRAY_BUFFER, OpenGLMaterial.shapeMaterial.VboHandle); GL.BufferData(GL.GL_ARRAY_BUFFER, vertexBuffer.Count * Marshal.SizeOf<DrawVertex>(), vertexBuffer.Pointer, GL.GL_STREAM_DRAW); GL.BindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, OpenGLMaterial.shapeMaterial.EboHandle); GL.BufferData(GL.GL_ELEMENT_ARRAY_BUFFER, indexBuffer.Count * Marshal.SizeOf<DrawIndex>(), indexBuffer.Pointer, GL.GL_STREAM_DRAW); Utility.CheckGLError(); // Draw var indexBufferOffset = IntPtr.Zero; foreach (var drawCmd in commandBuffer) { if (drawCmd.ElemCount == 0 || drawCmd.ClipRect.IsEmpty) { continue; } var clipRect = drawCmd.ClipRect; if (drawCmd.TextureData != null) { GL.ActiveTexture(GL.GL_TEXTURE0); GL.BindTexture(GL.GL_TEXTURE_2D, (uint) drawCmd.TextureData.GetNativeTextureId()); } GL.Scissor((int) clipRect.X, (int) (height - clipRect.Height - clipRect.Y), (int) clipRect.Width, (int) clipRect.Height); GL.DrawElements(GL.GL_TRIANGLES, drawCmd.ElemCount, GL.GL_UNSIGNED_INT, indexBufferOffset); indexBufferOffset = IntPtr.Add(indexBufferOffset, drawCmd.ElemCount * Marshal.SizeOf<DrawIndex>()); Utility.CheckGLError(); } } //check TextMesh if (textMesh!=null && !textMesh.IsEmpty) { //create texture for glyph OpenGLTexture glyphOpenGLTexture = new OpenGLTexture(); glyphOpenGLTexture.LoadImage(new byte[width * height * 4], width, height); uint glyphTexture = (uint)glyphOpenGLTexture.GetNativeTextureId(); //create frame buffer for glyph uint[] textMeshFrameBuffers = {0}; GL.GenFramebuffers(1, textMeshFrameBuffers); uint glyphFrameBuffer = textMeshFrameBuffers[0]; //attach textures to glyph framebuffer GL.BindFramebuffer(GL.GL_FRAMEBUFFER_EXT, glyphFrameBuffer); GL.FramebufferTexture2D(GL.GL_FRAMEBUFFER_EXT, GL.GL_COLOR_ATTACHMENT0_EXT, GL.GL_TEXTURE_2D, glyphTexture, 0); if (GL.CheckFramebufferStatus(GL.GL_FRAMEBUFFER_EXT) != GL.GL_FRAMEBUFFER_COMPLETE_EXT) { throw new Exception("Framebuffer for glyph is not complete."); } var quadMesh = new QuadMesh(); var commandBuffer = textMesh.Commands; VertexBuffer vertexBuffer = textMesh.VertexBuffer; IndexBuffer indexBuffer = textMesh.IndexBuffer; GL.BindFramebuffer(GL.GL_FRAMEBUFFER_EXT, glyphFrameBuffer); GL.Viewport(0, 0, width, height); GL.ClearColor(0, 0, 0, 0); GL.Clear(GL.GL_COLOR_BUFFER_BIT); // setup render state GL.Enable(GL.GL_BLEND); GL.BlendEquation(GL.GL_FUNC_ADD_EXT); // setup viewport, orthographic projection and view transformation var glyphMaterial = OpenGLMaterial.glyphMaterial; glyphMaterial.program.Bind(); glyphMaterial.program.SetUniformMatrix4("ViewMtx", viewMatrix); glyphMaterial.program.SetUniformMatrix4("ProjMtx", ortho_projection); // send mesh data GL.BindVertexArray(glyphMaterial.VaoHandle); GL.BindBuffer(GL.GL_ARRAY_BUFFER, glyphMaterial.VboHandle); GL.BufferData(GL.GL_ARRAY_BUFFER, vertexBuffer.Count * Marshal.SizeOf<DrawVertex>(), vertexBuffer.Pointer, GL.GL_STREAM_DRAW); GL.BindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, glyphMaterial.EboHandle); GL.BufferData(GL.GL_ELEMENT_ARRAY_BUFFER, indexBuffer.Count * Marshal.SizeOf<DrawIndex>(), indexBuffer.Pointer, GL.GL_STREAM_DRAW); Utility.CheckGLError(); //draw GL.BlendEquation(GL.GL_FUNC_ADD_EXT); GL.BlendFunc(GL.GL_ONE, GL.GL_ONE); var indexBufferOffset = IntPtr.Zero; #if Enable_Jitter for (int j = 0; j < JITTER_PATTERN.Length; j++) { var offset = JITTER_PATTERN[j]; glyphMaterial.program.SetUniform("offset", offset.x, offset.y); indexBufferOffset = IntPtr.Zero;//reset to redraw with a different offset if(j % 2 == 0) { glyphMaterial.program.SetUniform("color", j == 0 ? 1 : 0, j == 2 ? 1 : 0, j == 4 ? 1 : 0, 0); } #else glyphMaterial.program.SetUniform("offset", 0.0f, 0.0f); glyphMaterial.program.SetUniform("color", 1.0f, 1.0f, 1.0f, 0.0f); #endif // Draw text mesh foreach (var drawCommand in commandBuffer) { var clipRect = drawCommand.ClipRect; GL.Scissor((int) clipRect.X, (int) (height - clipRect.Height - clipRect.Y), (int) clipRect.Width, (int) clipRect.Height); GL.DrawElements(GL.GL_TRIANGLES, drawCommand.ElemCount, GL.GL_UNSIGNED_INT, indexBufferOffset); Utility.CheckGLError(); indexBufferOffset = IntPtr.Add(indexBufferOffset, drawCommand.ElemCount * Marshal.SizeOf<DrawIndex>()); } #if Enable_Jitter } #endif Utility.CheckGLError(); // second draw to the framebuffer as a quad GL.BindFramebuffer(GL.GL_FRAMEBUFFER_EXT, framebuffer); var textMaterial = OpenGLMaterial.textMaterial; GL.BlendFunc(GL.GL_ZERO, GL.GL_SRC_COLOR); textMaterial.program.Bind(); textMaterial.program.SetUniform("color", 0.0f, 0.0f, 0.0f, 0.0f); GL.Disable(GL.GL_SCISSOR_TEST); GL.ActiveTexture(GL.GL_TEXTURE0); GL.BindTexture(GL.GL_TEXTURE_2D, glyphTexture); GL.BindVertexArray(textMaterial.VaoHandle); GL.BindBuffer(GL.GL_ARRAY_BUFFER, textMaterial.VboHandle); GL.BufferData(GL.GL_ARRAY_BUFFER, quadMesh.VertexBuffer.Count * Marshal.SizeOf<DrawVertex>(), quadMesh.VertexBuffer.Pointer, GL.GL_STREAM_DRAW); GL.BindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, textMaterial.EboHandle); GL.BufferData(GL.GL_ELEMENT_ARRAY_BUFFER, quadMesh.IndexBuffer.Count * Marshal.SizeOf<DrawIndex>(), quadMesh.IndexBuffer.Pointer, GL.GL_STREAM_DRAW); GL.DrawElements(GL.GL_TRIANGLES, quadMesh.CommandBuffer[0].ElemCount, GL.GL_UNSIGNED_INT, IntPtr.Zero); // first draw to glyph framebuffer GL.DeleteFramebuffers(1, textMeshFrameBuffers); glyphOpenGLTexture.Dispose(); } //read pixels from the framebuffer GL.BindFramebuffer(GL.GL_FRAMEBUFFER_EXT, framebuffer); Utility.CheckGLError(); var pixels = new byte[width * height * 4]; GL.ReadPixels(0, 0, width, height, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, pixels); Utility.CheckGLError(); //dispose OpenGL resources used when rendering mesh and textMesh texture.Dispose(); GL.DeleteFramebuffers(1, framebuffers); Utility.CheckGLError(); return pixels; }
public QuadComponent(UILayer layer, vec2 size) : this(layer) { Quad = new QuadMesh(size.ScaleToScreen()); }
public QuadComponent(UILayer layer, vec2 size, RenderComponent.RenderComponent component) { Layer = layer; Quad = new QuadMesh(size.ScaleToScreen()); RenderComponent = component; }