Coder Social home page Coder Social logo

spritelightkit's Introduction

SpriteLightKit

SpriteLightKit brings back the old two buffered blend trick to get pseudo lighting with just sprites. It handles the setup process of getting that second buffer blended with your normal scene.

how it works

The same scene with two different ambient light setups:

low ambient light brighter ambient light

Setup

  • create an empty GameObject as a child of your main camera and add the SpriteLightKit component
  • add the SpriteLightKitImageEffect component to your main camera and drag the SpriteLightKitBlendImageEffect.shader into the inspector in the shader property
  • set the Light Layer in the SpriteLightKit component, which is the layer you want to place your sprite lights on
  • remove the Light Layer from your main camera's culling mask so that it does not render the sprite lights
  • create some sprites using the SpriteLightMaterial and make sure they are on the Light Layer you chose in the previous step

You can set the ambient lighting by changing the background color of the camera on the SpriteLightKit GameObject. Each of your lights can use the normal sprite tint color to change how it affects the underlying scene. The SpriteLightKitImageEffect has a toggle for 1x or 2x multiplicative blending (bool use2xMultiplicationBlending). 2x is useful for a scene where the sprite lights can lighten or darken a scene. Use colors darker than the ambient color to darken the scene and lighter colors to lighten it.

Lights look best when they are white and falloff to 0 alpha. That lets you use the tint color to color the lights and the tint color.alpha to set the intensity of the lights. Get creative with your light shapes and experiment! If you need to occlude lights (if you have walls where light shouldn't pass for example) you can just use any black sprite and place it so that it blocks the light however you want it to.

Advanced Emissive Features

SpriteLightKit has some extra, more advanced features baked in as well along with some shaders/materials to help you utilize them. The following emissive features are for use on your normal GameObjects that are not on the Light Layer. The emissive materials will write to the stencil buffer which will then be read by the SpriteLightKitImageEffect and lights will not be rendered for those pixels. This basically makes the masked pixels always display as if lights do not affect them. Materials are provided that let you use a Sprite or a Mesh as the emissive mask.

Since the emissive materials main purpose is to write to the stencil buffer you can stick your geometry that uses them behind all other geometry. This gives you some flexibility when using the SpriteLightEmissiveSpriteMaterial. If it is behind your other Sprites/geometry then it will work like a mask. If it is in front of your other Sprites/geometry it will work like a mask and it will also be displayed.

  • SpriteLightEmissiveSpriteMaterial lets you use a Sprite as an emissive mask. Set the Alpha Cutoff of the Material to control which pixels will be discarded.
  • SpriteLightEmissiveMeshMaterial lets you use a Mesh as an emissive mask.

Advanced Shadow Features

SpriteLightKit can also cast simple offset shadows. The current implementation uses a brute force approach to find affected lights but a b-tree or other spatial search will be added to speed things up. The shadow shader is ready to handle approximated planar shadows (via skew and scale in addition to the offset) but it hasn't yet been implemented due to some decisions that need to be made about how to best handle things.

Implementing shadows requires the following steps:

  • stick the SpriteLightKitLightManager on a GameObject in your scene
  • add the SpriteLightKitShadow component to any objects that should project shadows
  • make sure the objects that should cast shadows have the SpriteLightShadowedMaterial on them

Credit

The sweet little town sketch is from the amazing work of @pixelatedcrown. Follow on Twitter and Tumblr to see more awesome art!

License

Attribution-NonCommercial-ShareAlike 3.0 Unported with simple explanation with the attribution clause waived. You are free to use SpriteLightKit in any and all games that you make. You cannot sell SpriteLightKit directly or as part of a larger game asset.

spritelightkit's People

Contributors

brianasu-notdoppler avatar prime31 avatar slonermike avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

spritelightkit's Issues

Stencil buffer not working with render texture.

Hi! It seems that emissive materials not working at all. I'm trying to use EmissiveSprite/EmissiveMesh material or other materials that use the shader that writes to Stencil buffer 2 and it does pretty much nothing.

Combine SpriteLightKit with Occlusion

Hi @prime31 !

I've been following your articles and I can tell you now that shader's are less daunting thanks to you! ๐Ÿค˜

I've tried to combine both articles to make some cool effect but had no success (I feel that i'm almost there).

Screen Shot 2020-04-18 at 18 37 37

I would like to change the sprite's color outside lightning. Right now the region lit is red but i'm trying to achieve the opposite: make the dark area red and the lit area to use sprites original color.

Could you please shed some light on this?

Thank you!

Scene is solid color

After setting up my cameras per the instructions in the README (and the demo scene), my camera just renders a solid pink color.

Screen shot of scene and camera settings
image

image

I toggled a bunch of different options but not sure what's going on here.

Build not working for Mac

I forked the repo & included the latest version in my project. Everything runs fine on my Macbook Pro when starting it from the Unity Editor, however when I build it for Mac & run it the blending does not work properly and produces the following result in windowed mode:

skarmavbild 2016-03-09 kl 12 24 27
Screenshot taken from the SpriteLightKitDemo/SpriteLightKitScene

In full screen mode it works even less. I have confirmed that the windows build is working properly though.

I'll try to see if I can find out what's causing the problem, however if you have any ideas it would be appriciated as I like the core concept & implementation of it!

Regards

Seemingly Random Build Errors

Hi! I've really been enjoying this, but randomly it has seemed to stop working. I keep getting these 2 errors upon building:

Shader error in 'Hidden/Blend Image Effect': variable 'o' used without having been completely initialized at line 62 (on d3d9)

Compiling Vertex program
Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING UNITY_ENABLE_DETAIL_NORMALMAP SHADER_API_DESKTOP UNITY_COLORSPACE_GAMMA

Shader error in 'prime[31]/Sprite Light Kit/Shadowed': variable 'OUT' used without having been completely initialized at line 91 (on d3d9)

Compiling Vertex program with DUMMY
Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING UNITY_ENABLE_DETAIL_NORMALMAP SHADER_API_DESKTOP UNITY_COLORSPACE_GAMMA

I have not added anything to either script or done anything to the kit at all. I tried putting in a fresh kit, restarting my computer, and removing the things that were added before it stopped working, but I always get these errors. Does anybody know how to fix them?

I am in version 5.6.5f and have had no previous errors with it.

2D Occluded/Occluder shader issue...

Forgive me, as I didn't really know where else to go. I've posted about this issue, I've posted a few different times to the Unity forums and haven't received any help.

I'm actually having issues with the demo here:
http://prime31.github.io/stencil-buffer-occlusion/

My issue is, I'm trying to use it for a 2D isometric view game (think the legend of zelda), not a side scrolling game like the example. The issue comes with the depth sorting I'm using:

`using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class IsometricSpriteRenderer : MonoBehaviour
{
void Update()
{
GetComponent().sortingOrder = (int)(transform.position.y * -10);
}

}`

Literally, that's all the code. Nothing complicated. All items that use this code, sit on the same layer.

Now, the shaders work however, only when the Player is in FRONT of an object, not behind. However if I change the "-10" portion of the code to "+10" it will work as I need, but it breaks the depth sorting...

I've reached the end of my rope, does anyone have any ideas?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.