Rotate unity lights
spot light, directional light, point light, area light
firstly click color picker
Material
easy to color, shade, texture as well
assigning materials
what is a Material inside of Unity?
A shader and its associated settings
Textures
famous person in computer graphics
Ivan Sutherland, Edwin Catmull, Bui Tuong Phong
project pane -> Assets -> Create -> Shader -> Standard surface shader
HLSL
Shader "Custom/NewSurfaceShader" { Properties { _Color ("Color", Color) = (1,1,1,1) _MainTex ("Albedo (RGB)", 2D) = "white" {} _Glossiness ("Smoothness", Range(0,1)) = 0.5 _Metallic ("Metallic", Range(0,1)) = 0.0 } SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRAM // Physically based Standard lighting model, and enable shadows on all light types #pragma surface surf Standard fullforwardshadows // Use shader model 3.0 target, to get nicer looking lighting #pragma target 3.0 sampler2D _MainTex; struct Input { float2 uv_MainTex; }; half _Glossiness; half _Metallic; fixed4 _Color; void surf (Input IN, inout SurfaceOutputStandard o) { // Albedo comes from a texture tinted by color fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color; o.Albedo = c.rgb; // Metallic and smoothness come from slider variables o.Metallic = _Metallic; o.Smoothness = _Glossiness; o.Alpha = c.a; } ENDCG } FallBack "Diffuse" }
Unity’s Standard Shader attempts to light objects in a “physically-accurate” way. This technique is called Physically-Based Rendering or PBR for short. Instead of defining how an object looks in one lighting environment, you specify the properties of the object (e.g. how metal or plastic it is)
implement head lotation
using UnityEngine; using System.Collections; public class HeadRotation : MonoBehaviour { void Start(){ Input.gyro.enabled = true; } void Update(){ Quaternion att = Input.gyro.attitude; att = Quaternion.Euler(90f, 0f, 0f) * new Quaternion(att.x, att.y, -att.z, -att.w); transform.rotation = att; } }
Complex Model
to create complex models
– use a program like blender or maya
– download from the internet or the unity asset store
position change
you see changing a rotation and scale
Every Unity Game Object has a Transform
The Transforms scale is a multiplier of an object’s original
child parent relationship between the transform
Why do we mostly use triangles to represent 3D
speed, simplicity, convention
We use triangles because they provide a fast way for a computer to represent surfaces, they’re pretty simple structures, and we’ve been using them for quite a while, so computer hardware is optimized for them.
make a cube
go to gameobject and choose cube.
select move tool to change dimension
now go to GameObject and select sphere
now how about cylinder
Game Object -> 3D Object -> Cube
How information flows
Context (notebook)
-> notification
FetchedResultsController
-> delegate
CoreDataTableViewController
-> update
TableView
saving images(BLOBs) and migrating data model
Importing large sets of objects into the database without blocking the user interface
Downloading new objects from a REST service and inserting them into the db.
Saving datasets in the background since saving can take a user-perceivable amount of time.
Filtering By Section
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)-> UITableViewCell{ let note = fetchedResultsController?.object(at: indexPath) as! Note let cell = tableView.dequeueReusableCell(withIdentifier: "Note", for: indexPath) cell.textLabel?.text = note.text return cell }
override func prepare(for segue: UIStoryboardSeque, sender: Any?){ if segue.identifier! == "displayNote"{ if let notesVC = segue.destination as? NotesViewController { let fr = NSFetchRequest<NSFetchRequestResult>(entityName: "Note") fr.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending:false),NSSortDescriptor(key: "text", ascending: true)] let indexPath = tableView.indexPathForSelectedRow! let notebook = fetchedResultsController?.object(at: indexPath) let pred = NSPredicate(format: "notebook = %@", argumentArray: [notebook!]) fr.predicate = pred let fc = NSFetchedResultsController(fetchRequest: fr, managedObjectContext:fetchedResultsController!.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil) notesVC.fetchedResultsController = fc } } }
coredata
-How the table displays changes
-How a user can add a new note
-Saving and persisting changes
-Memory management
class CoreDataTableViewController: UITableViewController { var fetchedResultsController : NSFetchedResultsController<NSFetchRequestsResult>?{ didSet { fetchedResultsController?.delegate = self executeSearch() tableView.reloadData() } } init(fetchedResultsController fc : NSFetchedResultsController<NSFetchRequestResult>, style: UITableViewStyle = .plain){ fetchedResultsController = fc super.init(style: style) } required init?(coder aDecoder: NSCoder){ super.init(coder: aDecoder) } }
class NotebooksViewController: CoreDataTableViewController { // MARK: Life Cycle overeride func viewDidLoad(){ super.viewDidLoad() title = "CoolNotes" let delegate = UIApplication.shared.delegate as! AppDelegate let stack = delegate.stack let fr = NSFetchRequest<NSFetchRequestResult>(entityName: "Notebook") fr.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true), NSSortDescriptor(key: "creationDate", ascending: false)] fetchedResultsController = NSFetchedResultsController(fetchRequest: fr, managedObjectContext: stack.context, sectionNameKeyPath: nil, cacheName: nil) } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let nb = fetchedResultsController!.object(at: indexPath) as! Notebook let cell = tableView.dequeueReusableCell(withIdentifier: "NotebookCell", for: indexPath) cell.textLabel?.text = nb.name cell.detailTextLabel?.text = "\(nb.notes!.count) notes" return cell } }
Creating managed objects
import Foundation import CoreData extesion Note { @NSManaged var creationDate: NSDate? @NSManaged var text: String? @NSManaged var notebook: Notebook? }
import Foundation import CoreData extesion NSManagedObject { convention init(text:String = "New Note", context: NSManagedObjectContext){ if let ent = NSEntityDescription.entityForName("Note", inManagedObjectContext; context){ self.init(entity: ent, insertIntoManagedObjectContext: context) self.text = text self.creationDate = NSDate() } else { fatalError("Unable to find Entity name!") } } }
relationship
// Don't worry about the @NSManaged thing. // it's just a cue to the compiler that the property // will be managed by Core Date class Note: NSManagedObject { @NSManaged var notebook : Notebook } class Notebook: NSManagedObject { @NSManaged var note : ???? }
import CoreData struct CoreDataStack { // MARK: - Properties private let model : NSManagedObjectModel private let coordinator : MSPersistentStoreCoordinator private let modelURL : NSURL private let dbURL : NSURL let context : NSManagedObjectContext // MARK: - Initializers init?(modelName: String){ guard let modelURL = NSBundle.mainBundle().URLForResource(modelName, withExtension: "momd") else { print("Unable to find \(modelName)in the main bundle") return nil} self.modelURL = model URL // Try to create the model from the URL guard let model = NSManagedObjectModel(contentsOfURL: modelURL) else{ print("unable to create a model from \(modelURL)") } self.model = model coordinator = NSPersistentStoreCoordinator(managedObjectModel: model) context = NSManagedObjectContext(concurrencyType: . MainQueueConcurrencyType) context.persistentStoreCoordinator = coordinator // Add a SQLite store located in the documents folder let fm = NSFileManager.defaultManager() guard let docUrl = fm.URLsForDirectory(.DocumentDirectory, inDomains: UserDomainMask).first else { print("manage to people the document folder") return nil } } }
Core Data intro
-core data intro
-core data architecture
-create a model, managed objects, and begin our own app
core data
Model(managed objects), View, Controller
Object Model
(aka “managed Object Model”)
-specifies app classes and relationships
Managed Object
-class:NSManagedObject
-example:a character of a game app
-saves the contents of its properties to a DB file
Context
-class:NSManagedObject+Context
-imagine as a place
-where objects live and where operations take place
Fetch request
-searches context for certain managed objects
Fetched Results Controller
-part of controller layer
-controls how data from a fetch request is displayed in a view
Stores
-where managed objects are stored
Store coordinator
-allow you to have multiple stores