private RenderBlock CreateBlock(Sprite sprite, Texture2D normal, Texture2D emissive) { var block = GetRenderBlock(); block.Clear(); block.Material.SetTexture(ShaderPropertyTexture, sprite.texture); block.Material.SetTexture(ShaderPropertyNormal, normal); block.Material.SetTexture(ShaderPropertyEmissive, emissive); var pixelsPerUnit = sprite.pixelsPerUnit; var size = new Vector2( sprite.rect.width / pixelsPerUnit, sprite.rect.height / pixelsPerUnit); block.Quad = ProceduralMeshUtility.GenerateQuad(size, new Vector2(0.5f, 0)); return(block); }
private void DrawWithCamera(Camera cam) { if (!cam) { return; } if (_animation == null || _sprite == null) { return; } if (_mesh == null) { _mesh = ProceduralMeshUtility.GenerateQuad(new Vector2(20.48f, 10.24f), new Vector2(0.5f, 0)); } if (_sprite != null) { DrawSprite(); return; } if (!_drawTimer.IsActive) { _frame++; if (_frame >= _animation.Frames.Length) { _frame = 0; } _drawTimer.StartNewTime(_animation.FrameTime * _animation.GetFrame(_frame).Length); } var sprite = _animation.GetSprite(_frame); var matBlock = new MaterialPropertyBlock(); matBlock.SetTexture(_shaderPropertyTexture, sprite.texture); matBlock.SetTexture(_shaderPropertyNormal, _animation.NormalMap); matBlock.SetFloat(_shaderPropertyEmissivePower, _animation.EmissiveMap != null ? 1 : 0); if (_animation.EmissiveMap != null) { matBlock.SetTexture(_shaderPropertyEmissive, _animation.EmissiveMap); } matBlock.SetColor(_shaderPropertyColor, Color.white); matBlock.SetVector(_shaderPropertyUv, SpriteRenderingSystem.GetUv(sprite)); Graphics.DrawMesh(_mesh, GetMatrix(), _mat, 0, null, 0, matBlock, ShadowCastingMode.On); }
private void UpdateSprite(int frameIdx) { var blockMaterial = new MaterialPropertyBlock(); var sprite = _animation.GetSprite(frameIdx); var width = sprite.rect.width; var height = sprite.rect.height; //var gridY = Mathf.FloorToInt(spriteData.Frame / spriteData.FrameGridX); //var gridX = spriteData.Frame % spriteData.FrameGridX; //var pixelCoordsX = gridX * width; //var pixelCoordsY = sprite.texture.height - ((gridY+1) * height); var pixelCoordsX = sprite.rect.x; var pixelCoordsY = sprite.rect.y; float uvWidth = width / sprite.texture.width; float uvHeight = height / sprite.texture.height; var uvOffsetX = pixelCoordsX / sprite.texture.width; var uvOffsetY = pixelCoordsY / sprite.texture.height; blockMaterial.SetVector(_shaderPropertyUv, new Vector4(uvWidth, uvHeight, uvOffsetX, uvOffsetY)); blockMaterial.SetVector(_shaderPropertyColor, Color.white); blockMaterial.SetTexture(_shaderPropertyTexture, sprite.texture); blockMaterial.SetTexture(_shaderPropertyNormal, _animation.NormalMap); if (_animation.EmissiveMap != null) { _meshRenderer.sharedMaterial.EnableKeyword(_shaderEmissiveKeyword); blockMaterial.SetTexture(_shaderPropertyEmissive, _animation.EmissiveMap); } else { _meshRenderer.sharedMaterial.DisableKeyword(_shaderEmissiveKeyword); } _meshRenderer.SetPropertyBlock(blockMaterial); var frame = _animation.GetFrame(frameIdx); var pixelsPerUnit = sprite.pixelsPerUnit; var size = new Vector2(sprite.rect.width / pixelsPerUnit, sprite.rect.height / pixelsPerUnit); if (frame.HasEvent) { var center = _meshFilter.transform.TransformPoint( Mathf.Lerp( -(size.x * 0.5f), (size.x * 0.5f), frame.EventPosition.x), size.y * frame.EventPosition.y, 0); Debug.DrawRay(center, _meshFilter.transform.forward * 5, Color.red, 5f); } if (_meshFilter.sharedMesh == null) { _meshFilter.sharedMesh = ProceduralMeshUtility.GenerateQuad(size, new Vector2(0.5f, 0)); } if (_vertices.Count == 0) { _meshFilter.sharedMesh.GetVertices(_vertices); } Resize(size); if (_spriteCollider != null) { var savedCollider = _animation.GetSpriteCollider(frameIdx); _spriteCollider.UpdateCollider(savedCollider); if (savedCollider != null) { var center = _spriteCollider.transform.TransformPoint(Mathf.Lerp(-(size.x * 0.5f), (size.x * 0.5f), savedCollider .CriticalRect.x), size.y * savedCollider.CriticalRect.y, 0); var colliderSize = new Vector3(savedCollider.CriticalRect.size.x * size.x, savedCollider.CriticalRect.size.y * size.y, 0.5f); DebugExtension.DebugBounds(new Bounds(center, colliderSize), Color.red); } } }