Manipulating 2D Lighting with editor version 2021.2.19f1 with C# - c#

I'm not sure what I am doing wrong, but I am currently having an issue manipulating the 2D lighting of objects. Everything looks normal in visual studio, no errors, but when I try to initialize the code in Unity, it errors out and says the object doesn't have Light on it. 2d lighting seems to be a script, instead of a different type of object like with 3d lighting, but I can't reference the script. It's very hard to find info on this because it seems like the URP gets updated very regularly, and nothing I've found explains what I need to do to manipulate the settings like intensity.
Does anyone have any advice for me?

First create a new 2D light by going to Light < 2D < Point Light 2D (You can use any Light).
Now you should have a 2D light in your scene. Next attach a script to the light for example LightManipulator.cs.
Inside of LightManipulator.cs you can now reference the 2D light by declaring it as a public variable.
public Light2D light;
You can now change various values of this light. For example setting the intensity to 0.5f.
light.intensity = 0.5f;
EDIT: You have to import this at the top using UnityEngine.Experimental.Rendering.Universal;

Related

Unity light not showing up over background sprite

I have a power-up GameObject with a light component attached to it.
The sprite shows up fine, however, the halo effect does not work when the GameObject is placed over another sprite (eg a background wall).
I think its a problem with the material (as I have tried other things such as changing the culling mask, ordering layer, etc) - however, I don't know which properties to change in order to make the light visible.
I am using an area light.
I hope I saw correctly your game is 2D. If so, check out 2D Lights

changing friction in c# Unity

I am working on a project in my game programming class, and I want to change the friction of a physics2D material, and I can't find anything about changing it. Do you need to import anything, and what would the code be? thanks!
There are a couple ways you can do this.
From the UI
From the code (in C#)
gameObject.GetComponent(CircleCollider2D).sharedMaterial.friction = 0.0f
Where gameObject is your gameObject
Refs
Physics Material 2d
Box Collider 2d
Shared Material

splatmap textures not applying in build in unity

im procedurally creating my heightmaps and declaring my terrain in the scripts
in the unity editor, my splatmaps work perfectly, however when i build my terrain is just plain black.
is there a setting that i may have missed when building that is required to make the splats work ?.
editor
http://answers.unity3d.com/storage/temp/31838-untitle2d.png
build
http://answers.unity3d.com/storage/temp/31839-untitled.png
i load them in using code with
SplatPrototype[] terrainTexture = new SplatPrototype[4];
terrainTexture[0] = new SplatPrototype();
terrainTexture[0].texture = (Texture2D)Resources.Load("stone");
terrainTexture[1] = new SplatPrototype();
terrainTexture[1].texture = (Texture2D)Resources.Load("sand");
terrainTexture[2] = new SplatPrototype();
terrainTexture[2].texture = (Texture2D)Resources.Load("grass");
terrainTexture[3] = new SplatPrototype();
terrainTexture[3].texture = (Texture2D)Resources.Load("snow");
edit
i attempted adding the textures to the code as variables draged in rather than using resource.load, also didnt work
edit
i found a work around which makes it work, but its unfortunate that i coudnt find a proper solution.
can anyone see something that could be the cause of the issue
http://
answers.unity3d.com/questions/171150/terrain-basemap-texture-through-code.html
You need to include an extra shader in your build. Go to edit -> project settings -> graphics. In the inspector increase the size. In the new slot select Nature/Terrain/Diffuse.
The reason you need to do this is that Unity will search for any assets that are referenced in the scene to decide what to include in the build and what to leave out. Since your terrain is built procedurally there is no terrain object and no terrain material present, and the required shader is not included. By following the above steps you basically tell Unity to include it anyway because you know you are going to need it.
I would add in a few things to the above answer:
There is a prototypes refresh, called TerrainData.RefreshPrototypes() that is supposed to refresh all available prototypes, including the detail textures and trees. Since the ground textures are part of the prototypes listing, this will help unity update the new textures for the terrain. (And, it also works inside of the Editor.)
Also, others have found that when they change the textures' data (ie, changing the pixel data) that it causes unity to update the materials used for the terrain. They made a blank terrain, assigned blank base textures to it, and then at runtime loaded up the height map, splat maps, and overwrote the textures. This caused unity to display the newly created terrains.
I hope that this helps out some, as most of the terrain asset is a highly undocumented area (and rather dysfunctional, too)... Good Luck!

XNA BasicEffect, strange result on terrain with texture

At the moment i got a game project going where i will use the BasicEffect from XNA with defaultlighting on.
Simply put in class RenderLibManager
effect = new BasicEffect(device);
effect.EnableDefaultLighting = true;
This is working well on the normal 3D models, we got trees and a guy from the MSDN samples.
The problem is when i try to fetch the same basic effect from RenderLibManager to the world terrain file. I fetch it by doing
BasicEffect effect;
effect = RenderLibManager.effect;
And then i set the texture, cameraViewMatrix and cameraProjectionMatrix.
So when i start the game the terrain has a very dark blueish lighting. I don't really know why, but i was hoping someone could point me in the right direction.
Best regards,
Kerrai
EDIT
I actually found the error on my own. After a very long investigation i had forgotten to generate the normals of the terrain, or even set them in the vertex buffer.
Thank you all for looking into the answer at least

XNA - How to access a texture from .x model?

I am just getting into XNA programming and have been unable to figure out how can I access the texture from a ".x" model. I am using a custom shader to display my model (just a cube with a texture mapped on it) with the filters set to point. To do this I needed to pass the effect my texture file which needed to be imported separately from my model or else it would complain since it is included in my model as well. This works perfectly how I want it, however this isn't really an agreeable method when I have many different models with their own textures.
My question is:
How am I able to access the texture included in my model directly from it and send that to my shader? Or am I able to access it directly with HLSL?
What I have tried:
I have found posts saying that it can assigned to a texture variable with:
Texture2d texture = ((BasicEffect)model.Meshes[0].Effects[0]).Texture
When I tried this the game runs but the cubes are just black. I can see that the texture variable is holding info and has the right dimensions but I can't tell if it is correctly holding the actual image. When I used just the BasicEffect they rendered just fine with their texture.
Update:
I have managed to get this to work after a little bit of fiddling. My game loads in a few hundred of the same cube and upon creation of each it would try save the texture of the model using the code above and then go through the mesh parts and change the effects to my custom effect. I discovered that the first cube created would save the texture okay but any subsequent cubes created would complain that they can't be cast as a BasicEffect. This resulted in one textured cube and then a lot of black ones. I am guessing that when it reuses the same model over and over like that it will just use the one that was modified to use my custom effect which was done on the first instance of the cube. Is this normal? I have got them all to render as textured by changing the texture variable to static.
Please observe that you are assigning the texture of your model to a temporary Texture2D variable, and not setting the Texture present in the Effect currently tied to your mesh.
If you do the following:
Texture2D textureToSet = Content.Load<Texture2D>("MyTex");
//Keep in mind that this method requires a basic effect type and that only one
//effect is present on each mesh to work properly.
foreach(Mesh mesh in model.Meshes)
{
((BasicEffect)(mesh.Effects[0])).Texture = textureToSet;
}
The quirky stuff going on inside the foreach is simply that you are grabbing the effect, then casting it to a BasicEffect and using its Texture property to give it a new texture to draw when used. Please see the documentation and Shawn's blog for a more detailed introduction.
If anyone else is wondering about this as I was then saving the texture using:
Texture2d texture = ((BasicEffect)model.Meshes[0].Effects[0]).Texture
This does work but there is one thing to watch for which is what was causing me problems. If you change the effect of the model from the default BasicEffect for one instance it will be changed for every instance of the model created thereafter. So you will only be able to use the above code before you change the effect for the first time on a particular model.
I later found this book which describes exactly how to extract the texture and other information from a model: 3D Graphics with XNA Game Studio 4.0 by Sean James - Specifically chapter 2

Categories

Resources